Images: decode TIFF screen and link proxies at a reduced size (#2381) (#3016)

* Images: decode TIFF screen and link proxies at a reduced size (#2381)

Stream placed TIFFs a strip or tile row at a time through
craft_image_codecs::TiffBands into 1/2, 1/4 or 1/8 RGBA buffers (512 MiB
per band, 1 GiB reduced output) with alpha-weighted box averaging and
embedded ICC / working-CMYK display conversion.

Wire reduced TIFF sizes and estimates into designcraft-render's screen
mip cache (clamping oversized rasters to reduced levels when zooming in
so decode_full does not poison the cache entry) and linked-file proxy
generation.

* Images: round up in finest_level and cover 512 MiB single-strip cap (#2381)

Match the reduced decoder's round-up dimensions (`div_ceil(2)`) when
selecting `finest_level` so odd oversized widths such as 131071x128 pick
a level whose decoded buffer fits `Pixmap` and the 1 GiB RGBA cap before
cropping.

Replace the truncated header-only single-strip assertion with a complete
zlib-compressed single-strip CMYK TIFF above the 512 MiB band limit.

* Images: count orientation/float/Lab in reduced TIFF estimate and lighten tests (#2381)

* Images: include partial-cell map bound in reduced TIFF peak estimate (#2381)

---------

Co-authored-by: TS-Ca <[email protected]>
This commit is contained in:
TS-Ca
2026-11-21 13:00:28 -05:00
committed by GitHub
co-authored by TS-Ca
parent d7401b647b
commit a6d82e9a3c
8 changed files with 832 additions and 35 deletions
+1
View File
@@ -431,6 +431,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m
Newest first.
- **2026-11-19** · A placed TIFF drawn at a half, a quarter or an eighth of its pixel size or less (or past the 1 GiB full-size RGBA cap, #2381) is decoded a band at a time (`TiffBands`) into that reduced size, so large print TIFFs draw on the canvas, in page thumbnails and in linked-file proxies without a full-size decode. No readiness percentage change.
- **2026-11-12** · An idle canvas frame no longer does work for the spreads out of view (part of #1343): page shadows, paper, guides, margins, frame edges and overset marks are painted for the spreads and objects in view, and only their stories are looked up. In a 400-page document with two frames a page, an idle frame painted 5,099 shapes and looked up 400 stories; it now paints 311 and looks up 1, the same as in a 4-page document. No readiness percentage change.
- **2026-11-06** · Preflight warns about white or [Paper] set to overprint (#2401): a fill, stroke or stroke gap, or ruby and kenten, in [Paper] or a colour that lays down no ink (C=0 M=0 Y=0 K=0, a 0% tint; a spot ink, such as a white ink or a varnish, prints whatever colour stands in for it), is reported as `whiteOverprint`, since it disappears on press. Text itself has no overprint, so white text in a frame set to Overprint Fill still knocks out and prints, and is not reported. Preflight no longer says PDF export drops ruby and kenten overprint, which it writes. No readiness percentage change.
- **2026-11-04** · Interface Language › Português (Brasil) translates the whole interface: 996 strings the catalog lacked (tool names, window and guide commands, Keyboard Shortcuts sets, panel docking, Edit › Menus paths, the Japanese typesetting dialogs, special characters, Load Styles, Data Merge, Preflight, font replacement, the Package summary, OpenType features, page presets, About) and File › New's unit names, and a path is "demarcador" throughout. `cargo xtask i18n-report` counts 2,936 of 2,939 strings for pt-BR (1,933 before); only the Control panel's `IT`, `VA` and `Y:` captions stay English, as in every language. No readiness percentage change.
+4
View File
@@ -97,3 +97,7 @@ sync_github_name = true
[people.gmrmad1]
real_name = "Madhav Agarwal"
[people.thib-cal]
display_name = "TS-Ca"
sync_github_name = true
+23 -7
View File
@@ -12,6 +12,7 @@ mod eps;
mod jpeg;
mod psd;
mod resolution;
mod tiff_reduced;
mod tone;
pub use craft_ps::read::is_eps;
pub use eps::{bounding_box as eps_bounding_box, eps_pixel_size, eps_png, eps_proxy, eps_size};
@@ -382,7 +383,7 @@ pub fn too_large_to_decode(bytes: &[u8]) -> bool {
}
/// Whether a `w` × `h` raster takes more than [`MAX_RGBA_BYTES`] as RGBA.
fn over_rgba_budget(w: u32, h: u32) -> bool {
pub(crate) fn over_rgba_budget(w: u32, h: u32) -> bool {
u64::from(w).saturating_mul(u64::from(h)).saturating_mul(4) > MAX_RGBA_BYTES
}
@@ -513,6 +514,17 @@ pub fn reduced_jpeg_estimate(bytes: &[u8], shrink: u8) -> Option<(u32, u32, usiz
jpeg::reduced_estimate(bytes, shrink)
}
/// Whether [`decode_rgba_reduced`] handles this format (JPEGs and TIFFs).
pub fn supports_reduced_decode(bytes: &[u8]) -> bool {
bytes.starts_with(&[0xFF, 0xD8]) || tiff_reduced::is_tiff(bytes)
}
/// Dimensions and estimated peak bytes for an image decoded at `1/2^shrink`, without decoding its
/// pixels. `None` when [`decode_rgba_reduced`] cannot handle the image.
pub fn reduced_estimate(bytes: &[u8], shrink: u8) -> Option<(u32, u32, usize)> {
if tiff_reduced::is_tiff(bytes) { tiff_reduced::reduced_estimate(bytes, shrink) } else { jpeg::reduced_estimate(bytes, shrink) }
}
/// False when `bytes` is a WebP whose `ICCP` chunk declares more bytes than the file holds or than
/// [`colour::MAX_ICC_BYTES`]. The decoder sizes its buffer from the declared length before it reads
/// anything, so such a file must not reach `icc_profile()`; its pixels decode without it.
@@ -541,13 +553,17 @@ pub(crate) fn embedded_icc(bytes: &[u8]) -> Option<Vec<u8>> {
/// The largest `shrink` [`decode_rgba_reduced`] takes: 3, an eighth of each side.
pub const MAX_REDUCED_SHRINK: u8 = jpeg::MAX_SHRINK;
/// [`decode_rgba`] of a JPEG at 1/2^`shrink` of its size (`shrink` 1 to [`MAX_REDUCED_SHRINK`], each
/// side rounded up), read from its compressed blocks without a full-size decode. The embedded ICC
/// profile is applied as [`decode_rgba`] applies it. `None` where only a full decode applies: other
/// formats, CMYK, 12-bit and lossless JPEGs, files whose size [`pixel_size`] cannot read, and files
/// [`decode_rgba`] refuses as too large.
/// [`decode_rgba`] at 1/2^`shrink` of its size (`shrink` 1 to [`MAX_REDUCED_SHRINK`], each side
/// rounded up), read without a full-size decode. The embedded ICC profile (and working CMYK space
/// for CMYK TIFFs) is applied as [`decode_rgba`] applies it. `None` where only a full decode
/// applies: other formats, CMYK/12-bit/lossless JPEGs, non-CMYK separated TIFFs (`InkSet` 2),
/// files whose size [`pixel_size`] cannot read, JPEGs whose full size is over the full decode's
/// limit, and TIFFs whose reduced output or strip/tile band exceeds budget.
pub fn decode_rgba_reduced(bytes: &[u8], shrink: u8) -> Option<image::RgbaImage> {
use colour::{DisplayColour, Pixels, to_display_rgba};
if tiff_reduced::is_tiff(bytes) {
return tiff_reduced::decode_reduced(bytes, shrink);
}
// The size check must have a size to check: a header only the JPEG decoder reads is refused.
pixel_size(bytes)?;
if too_large_to_decode(bytes) {
@@ -572,7 +588,7 @@ pub struct CmykRaster {
}
/// The TIFF page that decoding opens; `None` for other formats.
fn default_tiff_page(bytes: &[u8]) -> Option<craft_image_codecs::TiffPage> {
pub(crate) fn default_tiff_page(bytes: &[u8]) -> Option<craft_image_codecs::TiffPage> {
let mut info = craft_image_codecs::tiff_info(bytes).ok()?;
let page = info.default_page()?;
(page < info.pages.len()).then(|| info.pages.swap_remove(page))
+338
View File
@@ -1146,6 +1146,344 @@ fn reduced_decoding_refuses_progressive_jpegs_whose_full_size_coefficients_are_o
assert_eq!(gray.dimensions(), (1_280, 550));
}
/// PackBits-compressed run of `len` identical bytes (`val`).
fn packbits_repeat(len: usize, val: u8) -> Vec<u8> {
let mut out = Vec::with_capacity((len / 128 + 1) * 2);
let mut left = len;
while left >= 128 {
out.push(129);
out.push(val);
left -= 128;
}
if left > 1 {
out.push((1 - left as isize) as u8);
out.push(val);
} else if left == 1 {
out.push(0);
out.push(val);
}
out
}
/// Minimal little-endian single-IFD TIFF builder supporting multiple strips, arbitrary `RowsPerStrip`,
/// `Compression`, `InkSet` (tag 332), and `ExtraSamples` (tag 338).
#[allow(clippy::too_many_arguments)]
fn custom_striped_tiff(
w: u32,
h: u32,
rows_per_strip: u32,
photometric: u16,
compression: u16,
spp: u16,
strips: &[&[u8]],
extra_sample: Option<u16>,
ink_set: Option<u16>,
) -> Vec<u8> {
custom_striped_tiff_with(w, h, rows_per_strip, photometric, compression, spp, 8, None, None, strips, extra_sample, ink_set)
}
#[allow(clippy::too_many_arguments)]
fn custom_striped_tiff_with(
w: u32,
h: u32,
rows_per_strip: u32,
photometric: u16,
compression: u16,
spp: u16,
bps: u16,
sample_format: Option<u16>,
orientation: Option<u16>,
strips: &[&[u8]],
extra_sample: Option<u16>,
ink_set: Option<u16>,
) -> Vec<u8> {
let n_strips = strips.len();
assert_eq!(n_strips as u32, h.div_ceil(rows_per_strip));
let mut entries: Vec<(u16, u16, u32, u32)> = vec![
(256, 4, 1, w),
(257, 4, 1, h),
(258, 3, u32::from(spp), if spp == 1 { u32::from(bps) } else { 0 }),
(259, 3, 1, u32::from(compression)),
(262, 3, 1, u32::from(photometric)),
(273, 4, n_strips as u32, 0),
(277, 3, 1, u32::from(spp)),
(278, 4, 1, rows_per_strip),
(279, 4, n_strips as u32, 0),
];
if let Some(orient) = orientation {
entries.push((274, 3, 1, u32::from(orient)));
}
if let Some(set) = ink_set {
entries.push((332, 3, 1, u32::from(set)));
}
if let Some(es) = extra_sample {
entries.push((338, 3, 1, u32::from(es)));
}
if let Some(fmt) = sample_format {
entries.push((339, 3, 1, u32::from(fmt)));
}
entries.sort_by_key(|e| e.0);
let ifd_start = 8usize;
let ifd_len = 2 + entries.len() * 12 + 4;
let mut extra_data_off = ifd_start + ifd_len;
let bps_off = extra_data_off;
if spp > 1 {
extra_data_off += usize::from(spp) * 2;
}
let offsets_off = extra_data_off;
if n_strips > 1 {
extra_data_off += n_strips * 4;
}
let counts_off = extra_data_off;
if n_strips > 1 {
extra_data_off += n_strips * 4;
}
let mut data_off = extra_data_off;
let mut strip_offsets = Vec::with_capacity(n_strips);
let mut strip_counts = Vec::with_capacity(n_strips);
for s in strips {
strip_offsets.push(u32::try_from(data_off).unwrap());
strip_counts.push(u32::try_from(s.len()).unwrap());
data_off += s.len();
}
for (tag, _, _, val) in &mut entries {
match *tag {
258 if spp > 1 => *val = bps_off as u32,
273 => *val = if n_strips == 1 { strip_offsets[0] } else { offsets_off as u32 },
279 => *val = if n_strips == 1 { strip_counts[0] } else { counts_off as u32 },
_ => {}
}
}
let mut out = Vec::with_capacity(data_off);
out.extend(b"II*\0");
out.extend(8u32.to_le_bytes());
out.extend((entries.len() as u16).to_le_bytes());
for (tag, kind, count, val) in entries {
out.extend(tag.to_le_bytes());
out.extend(kind.to_le_bytes());
out.extend(count.to_le_bytes());
out.extend(val.to_le_bytes());
}
out.extend(0u32.to_le_bytes());
if spp > 1 {
for _ in 0..spp {
out.extend(bps.to_le_bytes());
}
}
if n_strips > 1 {
for o in strip_offsets {
out.extend(o.to_le_bytes());
}
for c in strip_counts {
out.extend(c.to_le_bytes());
}
}
for s in strips {
out.extend_from_slice(s);
}
out
}
#[test]
fn reduced_tiffs_decode_rgb_cmyk_alpha_and_cross_strip_boundaries() {
// 33 × 25 RGB gradient split across 9 strips of 3 rows each (every 8×8 cell crosses strip joins).
let (w, h, rps) = (33u32, 25u32, 3u32);
let mut strips_owned: Vec<Vec<u8>> = Vec::new();
for y0 in (0..h).step_by(rps as usize) {
let rows = (h - y0).min(rps);
let mut s = Vec::with_capacity((rows * w * 3) as usize);
for dy in 0..rows {
let y = y0 + dy;
for x in 0..w {
s.extend_from_slice(&[((x * 255) / w) as u8, ((y * 255) / h) as u8, 128]);
}
}
strips_owned.push(s);
}
let refs: Vec<&[u8]> = strips_owned.iter().map(Vec::as_slice).collect();
let rgb_tiff = custom_striped_tiff(w, h, rps, 2, 1, 3, &refs, None, None);
let full_rgb = decode_rgba(&rgb_tiff).unwrap();
for (shrink, want_size) in [(1u8, (17, 13)), (2, (9, 7)), (3, (5, 4))] {
let img = decode_rgba_reduced(&rgb_tiff, shrink).unwrap();
assert_eq!(img.dimensions(), want_size, "RGB shrink {shrink}");
let est = reduced_estimate(&rgb_tiff, shrink).unwrap();
assert_eq!((est.0, est.1), want_size);
let step = 1u32 << shrink;
let (mut diff, mut n) = (0u64, 0u64);
for ry in 0..want_size.1 {
for rx in 0..want_size.0 {
let (ux0, ux1) = (rx * step, ((rx + 1) * step).min(w));
let (uy0, uy1) = (ry * step, ((ry + 1) * step).min(h));
let mut sum = [0u64; 3];
let cnt = u64::from((ux1 - ux0) * (uy1 - uy0));
for y in uy0..uy1 {
for x in ux0..ux1 {
let p = full_rgb.get_pixel(x, y).0;
for c in 0..3 {
sum[c] += u64::from(p[c]);
}
}
}
let got = img.get_pixel(rx, ry).0;
for (c, &s) in sum.iter().enumerate() {
let avg = ((s + cnt / 2) / cnt) as u8;
diff += u64::from(got[c].abs_diff(avg));
n += 1;
}
assert_eq!(got[3], 255);
}
}
assert!(diff / n <= 1, "RGB shrink {shrink} mean diff {}", diff / n);
}
// Odd-sized CMYK TIFF via the `tiff` encoder matches `decode_rgba` averaged over each block.
use tiff::encoder::colortype::{CMYK8, CMYKA8};
let mut cmyk_px = Vec::with_capacity((w * h * 4) as usize);
for y in 0..h {
for x in 0..w {
cmyk_px.extend_from_slice(&[0, ((x * 255) / w) as u8, ((y * 255) / h) as u8, 20]);
}
}
let cmyk_tiff = tiff_of::<CMYK8>(w, h, &cmyk_px, false);
let full_cmyk = decode_rgba(&cmyk_tiff).unwrap();
let red_cmyk = decode_rgba_reduced(&cmyk_tiff, 2).unwrap();
assert_eq!(red_cmyk.dimensions(), (9, 7));
for ry in 0..6 {
for rx in 0..8 {
let p = red_cmyk.get_pixel(rx, ry).0;
let q = full_cmyk.get_pixel(rx * 4 + 2, ry * 4 + 2).0;
for c in 0..4 {
assert!(p[c].abs_diff(q[c]) <= 12, "CMYK ({rx},{ry}) c{c}: {} vs {}", p[c], q[c]);
}
}
}
// CMYKA with transparent black pixels beside an opaque pixel: alpha-weighted averaging prevents
// transparent pixels from darkening opaque colour.
#[rustfmt::skip]
let cmyka_px: [u8; 4 * 4 * 5] = [
0, 255, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0,
0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0,
0, 0, 0, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255,
0, 0, 0, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255,
];
let cmyka_tiff = tiff_of::<CMYKA8>(4, 4, &cmyka_px, true);
let reduced_alpha = decode_rgba_reduced(&cmyka_tiff, 1).unwrap();
assert_eq!(reduced_alpha.dimensions(), (2, 2));
let opaque_want = decode_rgba(&tiff_of::<CMYK8>(1, 1, &[0, 255, 255, 0], false)).unwrap().get_pixel(0, 0).0;
let tl = reduced_alpha.get_pixel(0, 0).0;
assert_eq!(tl[3], 64, "quarter opaque: {tl:?}");
for c in 0..3 {
assert!(tl[c].abs_diff(opaque_want[c]) <= 1, "no dark bleed in top-left {tl:?} vs {opaque_want:?}");
}
assert_eq!(reduced_alpha.get_pixel(1, 0).0[3], 0, "fully transparent cell stays alpha 0");
// Non-CMYK InkSet (InkSet = 2) is refused just like `decode_cmyk_tiff`.
let spot_cmyk = custom_striped_tiff(4, 4, 4, 5, 1, 4, &[&[0; 64]], None, Some(2));
assert!(decode_rgba_reduced(&spot_cmyk, 1).is_none());
}
/// Oversized multi-strip TIFFs (> 256 MP / 1 GiB RGBA) are refused by `decode_rgba`, yet decode
/// cleanly at reduced proxy resolution via banded streaming (`#2381`).
#[test]
fn oversized_multi_strip_tiff_decodes_at_reduced_size_while_full_decode_is_refused() {
// 65,536 × 4,097 RGBA8 PackBits (~1.0002 GiB RGBA > MAX_RGBA_BYTES, ~16 MiB compressed).
let (w, h, rps) = (65_536u32, 4_097u32, 64u32);
let pb_row = packbits_repeat(w as usize * 4, 200);
let full_strip = pb_row.repeat(rps as usize);
let rem_rows = (h % rps) as usize;
let rem_strip = pb_row.repeat(rem_rows);
let full_strips = (h / rps) as usize;
let mut strips: Vec<&[u8]> = vec![&full_strip; full_strips];
if rem_rows > 0 {
strips.push(&rem_strip);
}
let big_tiff = custom_striped_tiff(w, h, rps, 2, 32773, 4, &strips, Some(2), None);
assert_eq!(pixel_size(&big_tiff), Some((w, h)));
assert!(too_large_to_decode(&big_tiff));
assert!(decode_rgba(&big_tiff).is_none(), "full decode exceeds 1 GiB RGBA limit");
let reduced = decode_rgba_reduced(&big_tiff, 3).expect("reduced 1/8 decode succeeds via TiffBands");
assert_eq!(reduced.dimensions(), (8_192, 513));
assert_eq!(reduced.get_pixel(0, 0).0, [200, 200, 200, 200]);
assert_eq!(reduced.get_pixel(8_191, 512).0, [200, 200, 200, 200]);
// A complete PackBits single-strip 1-bit monochrome TIFF works within the 512 MiB band cap,
// and is refused before pixel allocation once its single strip exceeds 512 MiB (~1 MiB file).
let mono_row = packbits_repeat(w as usize / 8, 0);
let small_strip = mono_row.repeat(16);
let small_single = custom_striped_tiff_with(w, 16, 16, 1, 32773, 1, 1, None, None, &[&small_strip], None, None);
let small_out = decode_rgba_reduced(&small_single, 3).expect("16-row 1-bit strip is within 512 MiB cap");
assert_eq!(small_out.dimensions(), (8_192, 2));
let over_h = 8_200u32; // 65,536 × 8,200 = 537.4 MiB > 512 MiB band cap
let over_strip = mono_row.repeat(over_h as usize);
let single_strip_over_band = custom_striped_tiff_with(w, over_h, over_h, 1, 32773, 1, 1, None, None, &[&over_strip], None, None);
assert!(reduced_estimate(&single_strip_over_band, 3).is_none());
assert!(decode_rgba_reduced(&single_strip_over_band, 3).is_none());
}
#[test]
fn reduced_tiff_estimate_counts_orientation_copy_and_float_or_lab_buffers() {
let (w, h) = (16u32, 12u32);
let px_count = (w * h) as usize;
// Orientation 2..=4 swaps/flips pixels while keeping `bands.dimensions() == pixel_size`,
// adding a full group copy alongside the band during `TiffBands::decode`.
// Orientations 5..=8 swap stored dimensions relative to `pixel_size` and fall back to full decode.
let rgba_raw = vec![120u8; px_count * 4];
let upright = custom_striped_tiff_with(w, h, h, 2, 1, 4, 8, None, Some(1), &[&rgba_raw], Some(2), None);
let flipped = custom_striped_tiff_with(w, h, h, 2, 1, 4, 8, None, Some(3), &[&rgba_raw], Some(2), None);
let swapped = custom_striped_tiff_with(w, h, h, 2, 1, 4, 8, None, Some(6), &[&rgba_raw], Some(2), None);
let est_upright = reduced_estimate(&upright, 1).unwrap();
let est_flip = reduced_estimate(&flipped, 1).unwrap();
assert_eq!((est_upright.0, est_upright.1), (8, 6));
assert_eq!((est_flip.0, est_flip.1), (8, 6));
assert_eq!(est_flip.2, est_upright.2 + px_count * 4);
assert_eq!(decode_rgba_reduced(&flipped, 1).unwrap().dimensions(), (8, 6));
assert!(reduced_estimate(&swapped, 1).is_none());
assert!(decode_rgba_reduced(&swapped, 1).is_none());
// Lab8 and Rgb32F convert through normalized 32-bit float intermediates before U8 output.
let rgb_raw = vec![128u8; px_count * 3];
let rgb8 = custom_striped_tiff(w, h, h, 2, 1, 3, &[&rgb_raw], None, None);
let lab8 = custom_striped_tiff(w, h, h, 8, 1, 3, &[&rgb_raw], None, None);
let est_rgb8 = reduced_estimate(&rgb8, 1).unwrap();
let est_lab8 = reduced_estimate(&lab8, 1).unwrap();
assert_eq!(est_lab8.2 - est_rgb8.2, px_count * (32 - 4));
assert!(decode_rgba_reduced(&lab8, 1).is_some());
let f32_raw: Vec<u8> = [0.5f32, 0.25, 0.75].repeat(px_count).into_iter().flat_map(f32::to_le_bytes).collect();
let rgb32f = custom_striped_tiff_with(w, h, h, 2, 1, 3, 32, Some(3), None, &[&f32_raw], None, None);
let est_f32 = reduced_estimate(&rgb32f, 1).unwrap();
let partial_16x12 = 32 * (std::mem::size_of::<((u32, u32), ([u32; 4], u32, u16))>() + 1);
assert_eq!(est_f32.2, px_count * 12 + px_count * 32 + partial_16x12 + (8 * 6) * 8);
assert!(decode_rgba_reduced(&rgb32f, 1).is_some());
// Multi-strip with Orientation = 3 crossing shrink boundaries still decodes cleanly.
let strip_rows = 3usize;
let strip_bytes = w as usize * strip_rows * 4;
let rgba_strips: Vec<&[u8]> = (0..4).map(|i| &rgba_raw[i * strip_bytes..(i + 1) * strip_bytes]).collect();
let multi_flip = custom_striped_tiff_with(w, h, strip_rows as u32, 2, 1, 4, 8, None, Some(3), &rgba_strips, Some(2), None);
assert_eq!(decode_rgba_reduced(&multi_flip, 2).unwrap().dimensions(), (4, 3));
// Wide 1-row-per-strip grayscale TIFF counts the bounded partial-cell table at shrink = 3.
let wide_w = 262_136u32;
let wide_row = packbits_repeat(wide_w as usize, 180);
let wide_strips = vec![wide_row.as_slice(); 8];
let wide_tiff = custom_striped_tiff(wide_w, 8, 1, 1, 32773, 1, &wide_strips, None, None);
let est_wide = reduced_estimate(&wide_tiff, 3).unwrap();
assert_eq!((est_wide.0, est_wide.1), (32_767, 1));
let min_partial = 65_536 * (std::mem::size_of::<((u32, u32), ([u32; 4], u32, u16))>() + 1);
assert!(est_wide.2 >= min_partial + wide_w as usize * 8 + 32_767 * 8);
assert_eq!(decode_rgba_reduced(&wide_tiff, 3).unwrap().dimensions(), (32_767, 1));
}
/// Planar CMYK TIFFs and CMYK TIFFs with associated (premultiplied) alpha keep their inks through
/// the shared decoder, so the PDF export's RGB fallback is only for other inks and oversized files.
#[test]
+294
View File
@@ -0,0 +1,294 @@
//! Reduced-size TIFF decoding (1/2, 1/4, 1/8) via banded streaming for screen and link proxies.
use std::collections::HashMap;
use craft_image_codecs::{ChannelLayout, CodecError, InkSet, Limits, SampleType, TiffBands};
use image::RgbaImage;
use crate::CmykRaster;
use crate::colour::{DisplayColour, Pixels, to_display_rgba};
/// Largest `shrink` [`decode_reduced`] accepts: 3, an eighth of each side.
pub(crate) const MAX_SHRINK: u8 = crate::jpeg::MAX_SHRINK;
/// Most bytes one strip or row of tiles may take while streaming: stops single-strip files
/// larger than this before any pixel buffer is allocated.
const MAX_BAND_BYTES: u64 = 512 << 20;
/// Classic (`II*\0`, `MM\0*`) or BigTIFF (`II+\0`, `MM\0+`) signature.
pub(crate) fn is_tiff(bytes: &[u8]) -> bool {
matches!(bytes.get(..4), Some(b"II*\0" | b"MM\0*" | b"II+\0" | b"MM\0+"))
}
fn limits() -> Limits {
Limits { max_alloc: MAX_BAND_BYTES, ..Limits::banded() }
}
/// False when a Separated TIFF uses a non-CMYK ink set (`InkSet` != 1), matching
/// [`crate::decode_cmyk_tiff`].
fn supported_ink_set(bytes: &[u8]) -> bool {
!crate::default_tiff_page(bytes).is_some_and(|page| page.photometric == Some(5) && page.ink_set.is_some_and(|set| set != InkSet::Cmyk))
}
/// Opens the default TIFF page for banded decoding at `1 / 2^shrink`, enforcing that both the
/// stream bands and the reduced RGBA output stay within budget.
fn open_checked(bytes: &[u8], shrink: u8) -> Option<(TiffBands<'_>, u32, u32)> {
if !is_tiff(bytes) || !(1..=MAX_SHRINK).contains(&shrink) || !supported_ink_set(bytes) {
return None;
}
let (pw, ph) = crate::pixel_size(bytes)?;
let bands = TiffBands::open(bytes, None, &limits()).ok()?.upright();
if bands.dimensions() != (pw, ph) {
return None;
}
let step = 1u32 << shrink;
let (rw, rh) = (pw.div_ceil(step), ph.div_ceil(step));
if rw == 0 || rh == 0 || crate::over_rgba_budget(rw, rh) {
return None;
}
Some((bands, rw, rh))
}
type PartialCell = ([u32; 4], u32, u16);
/// Maximum incomplete reduced cells that can coexist in `partial` across sequential full-span
/// [`TiffBands`] callbacks: at most two boundary spans (`2 * max(rw, rh)`, capped at `out_px`).
fn max_partial_cells(rw: u32, rh: u32, out_px: usize) -> Option<usize> {
Some(usize::try_from(rw.max(rh)).ok()?.saturating_mul(2).min(out_px))
}
/// Worst-case `HashMap` allocation for `cells` live entries (`hashbrown` 7/8 load factor rounded
/// up to a power-of-two bucket table, plus one control byte per bucket).
fn partial_map_bytes(cells: usize) -> usize {
if cells == 0 {
return 0;
}
let min_buckets = cells.saturating_mul(8).div_ceil(7).max(8);
let buckets = min_buckets.checked_next_power_of_two().unwrap_or(min_buckets);
buckets.saturating_mul(std::mem::size_of::<((u32, u32), PartialCell)>() + 1)
}
/// Reduced `(width, height, estimated_peak_bytes)` for a TIFF at `1 / 2^shrink` without decoding
/// any pixels. `None` when [`decode_reduced`] cannot handle the file.
pub(crate) fn reduced_estimate(bytes: &[u8], shrink: u8) -> Option<(u32, u32, usize)> {
let (bands, rw, rh) = open_checked(bytes, shrink)?;
let group = bands.group_bytes().ok()?;
let scratch = usize::try_from(bands.decode_scratch_bytes()).ok()?;
let is_cmyk = bands.layout().is_cmyk();
let target_layout = if is_cmyk { ChannelLayout::CmykA } else { ChannelLayout::Rgba };
let out_ch = target_layout.channels();
let src_ch = bands.layout().channels().max(1);
let src_bpp = src_ch.saturating_mul(bands.sample_type().bytes()).max(1);
let group_px = group / src_bpp;
// Turning bands upright (`Orientation != 1`) copies the whole group inside `TiffBands::decode`.
let orient_copy = if (2..=8).contains(&bands.orientation()) { group } else { 0 };
// Float and Lab bands convert through normalized 32-bit float intermediates before narrowing to U8.
let is_lab = matches!(bands.layout(), ChannelLayout::Lab | ChannelLayout::LabA);
let is_float = bands.sample_type().is_float();
let conv_per_px = if bands.layout() == target_layout && bands.sample_type() == SampleType::U8 {
0
} else if !is_lab && !is_float {
out_ch
} else if bands.layout() == target_layout {
4 * src_ch + out_ch
} else {
4 * src_ch + 4 * out_ch + out_ch
};
let conv_bytes = group_px.saturating_mul(conv_per_px);
let group_extra = scratch.max(orient_copy).max(conv_bytes);
let band_peak = group.saturating_add(group_extra);
let out_px = usize::try_from(rw).ok()?.checked_mul(usize::try_from(rh).ok()?)?;
let partial_bytes = partial_map_bytes(max_partial_cells(rw, rh, out_px)?);
// Reduced output buffers (CMYK inks + alpha + RGBA display, or RGBA + premultiplied copy).
let out_bytes = out_px.saturating_mul(if is_cmyk { 9 } else { 8 });
let peak = band_peak.saturating_add(partial_bytes).saturating_add(out_bytes).max(8);
Some((rw, rh, peak))
}
struct AccumTarget<'a> {
w: u32,
h: u32,
rw: u32,
shrink: u8,
step: u32,
channels: usize,
max_partial: usize,
pixels_out: &'a mut [u8],
alpha_out: &'a mut [u8],
partial: &'a mut HashMap<(u32, u32), PartialCell>,
}
fn write_cell(target: &mut AccumTarget<'_>, rx: u32, ry: u32, expected: u16, sums: [u32; 4], sum_a: u32) -> Result<(), CodecError> {
let out_idx = usize::try_from(ry)
.ok()
.and_then(|y| y.checked_mul(usize::try_from(target.rw).ok()?))
.and_then(|row| row.checked_add(usize::try_from(rx).ok()?))
.ok_or_else(|| CodecError::LimitExceeded("reduced pixel index out of range".into()))?;
let c_off = out_idx.checked_mul(4).ok_or_else(|| CodecError::LimitExceeded("reduced buffer offset out of range".into()))?;
let dst = target
.pixels_out
.get_mut(c_off..c_off.saturating_add(4))
.ok_or_else(|| CodecError::InvalidImage("reduced output buffer too small".into()))?;
let exp = u32::from(expected.max(1));
let out_a = ((sum_a + exp / 2) / exp).min(255) as u8;
for (d, &s) in dst[..target.channels].iter_mut().zip(&sums[..target.channels]) {
*d = (s + sum_a / 2).checked_div(sum_a).map_or(0, |v| v.min(255) as u8);
}
if target.channels == 3 {
dst[3] = out_a;
} else if let Some(a_slot) = target.alpha_out.get_mut(out_idx) {
*a_slot = out_a;
}
Ok(())
}
fn accumulate_group(target: &mut AccumTarget<'_>, gx: u32, gy: u32, gw: u32, gh: u32, src: &[u8]) -> Result<(), CodecError> {
if gw == 0 || gh == 0 || gx >= target.w || gy >= target.h {
return Ok(());
}
let max_x = gx.saturating_add(gw).min(target.w);
let max_y = gy.saturating_add(gh).min(target.h);
let rx_start = gx >> target.shrink;
let rx_end = (max_x - 1) >> target.shrink;
let ry_start = gy >> target.shrink;
let ry_end = (max_y - 1) >> target.shrink;
let gw_usize = usize::try_from(gw).map_err(|_| CodecError::LimitExceeded("group width out of range".into()))?;
let px_stride = target.channels + 1;
for ry in ry_start..=ry_end {
let uy0 = ry << target.shrink;
let uy1 = uy0.saturating_add(target.step).min(target.h);
let iy0 = uy0.max(gy);
let iy1 = uy1.min(max_y);
for rx in rx_start..=rx_end {
let ux0 = rx << target.shrink;
let ux1 = ux0.saturating_add(target.step).min(target.w);
let ix0 = ux0.max(gx);
let ix1 = ux1.min(max_x);
let expected =
u16::try_from((ux1 - ux0).saturating_mul(uy1 - uy0)).map_err(|_| CodecError::InvalidImage("reduced cell area overflow".into()))?;
let seen =
u16::try_from((ix1 - ix0).saturating_mul(iy1 - iy0)).map_err(|_| CodecError::InvalidImage("reduced overlap area overflow".into()))?;
let mut sums = [0u32; 4];
let mut sum_a = 0u32;
let span_px = (ix1 - ix0) as usize;
let x_off = (ix0 - gx) as usize;
for y in iy0..iy1 {
let row_start = ((y - gy) as usize).saturating_mul(gw_usize).saturating_add(x_off).saturating_mul(px_stride);
let span_bytes = span_px.saturating_mul(px_stride);
let row = src
.get(row_start..row_start.saturating_add(span_bytes))
.ok_or_else(|| CodecError::InvalidImage("TIFF band row truncated".into()))?;
if target.channels == 3 {
for &[r, g, b, a] in row.as_chunks::<4>().0 {
let a = u32::from(a);
sum_a += a;
sums[0] += u32::from(r) * a;
sums[1] += u32::from(g) * a;
sums[2] += u32::from(b) * a;
}
} else {
for &[c, m, y_ink, k, a] in row.as_chunks::<5>().0 {
let a = u32::from(a);
sum_a += a;
sums[0] += u32::from(c) * a;
sums[1] += u32::from(m) * a;
sums[2] += u32::from(y_ink) * a;
sums[3] += u32::from(k) * a;
}
}
}
if seen == expected {
write_cell(target, rx, ry, expected, sums, sum_a)?;
continue;
}
let cur_len = target.partial.len();
let entry = match target.partial.entry((rx, ry)) {
std::collections::hash_map::Entry::Occupied(e) => e.into_mut(),
std::collections::hash_map::Entry::Vacant(e) => {
if cur_len >= target.max_partial {
return Err(CodecError::LimitExceeded("partial reduced cells exceed boundary span".into()));
}
e.insert(([0u32; 4], 0u32, 0u16))
}
};
for (dst_s, s) in entry.0.iter_mut().zip(&sums) {
*dst_s += *s;
}
entry.1 += sum_a;
entry.2 += seen;
if entry.2 >= expected {
let Some((done_sums, done_a, _)) = target.partial.remove(&(rx, ry)) else {
return Err(CodecError::InvalidImage("missing completed reduced cell".into()));
};
write_cell(target, rx, ry, expected, done_sums, done_a)?;
}
}
}
Ok(())
}
/// Decodes a TIFF at `1 / 2^shrink` (`1..=[MAX_SHRINK]`, each side rounded up) by streaming
/// strip/tile groups through [`TiffBands`] and averaging directly into the reduced display buffer,
/// applying the embedded ICC profile or active working CMYK profile just like [`crate::decode_rgba`].
pub(crate) fn decode_reduced(bytes: &[u8], shrink: u8) -> Option<RgbaImage> {
let (mut bands, rw, rh) = open_checked(bytes, shrink)?;
let (w, h) = bands.dimensions();
let step = 1u32 << shrink;
let is_cmyk = bands.layout().is_cmyk();
let is_gray = bands.layout().is_gray();
let out_px = usize::try_from(rw).ok()?.checked_mul(usize::try_from(rh).ok()?)?;
let rgba_len = out_px.checked_mul(4)?;
let mut pixels_out = Vec::new();
pixels_out.try_reserve_exact(rgba_len).ok()?;
pixels_out.resize(rgba_len, 0);
let mut alpha_out = Vec::new();
if is_cmyk {
alpha_out.try_reserve_exact(out_px).ok()?;
alpha_out.resize(out_px, 255);
}
let max_partial = max_partial_cells(rw, rh, out_px)?;
let mut partial = HashMap::new();
let target_layout = if is_cmyk { ChannelLayout::CmykA } else { ChannelLayout::Rgba };
let mut target = AccumTarget {
w,
h,
rw,
shrink,
step,
channels: if is_cmyk { 4 } else { 3 },
max_partial,
pixels_out: &mut pixels_out,
alpha_out: &mut alpha_out,
partial: &mut partial,
};
bands
.decode::<CodecError>(|gx, gy, group| {
let (gw, gh) = group.dimensions();
let converted = group.converted(target_layout, SampleType::U8);
let src = converted.as_u8().ok_or_else(|| CodecError::InvalidImage("converted TIFF band is not 8-bit".into()))?;
accumulate_group(&mut target, gx, gy, gw, gh, src)
})
.ok()?;
if !partial.is_empty() {
return None;
}
let icc = bands.icc.take();
if is_cmyk {
let alpha = alpha_out.iter().any(|&a| a < 255).then_some(alpha_out);
let raster = CmykRaster { width: rw, height: rh, cmyk: pixels_out, alpha };
return to_display_rgba(Pixels::Cmyk(raster), icc.as_deref(), &DisplayColour::active());
}
let rgba = RgbaImage::from_raw(rw, rh, pixels_out)?;
let Some(icc) = icc.as_deref() else { return Some(rgba) };
let px = if is_gray { Pixels::Gray(rgba) } else { Pixels::Rgb(rgba) };
to_display_rgba(px, Some(icc), &DisplayColour::active())
}
+1 -1
View File
@@ -77,7 +77,7 @@ pub fn decode_pixmap_page(bytes: &[u8], page: u32) -> Option<Pixmap> {
premultiplied(img.width(), img.height(), img.into_raw())
}
/// [`decode_pixmap`] of a JPEG at 1/2^`shrink` of its size, `shrink` 1 to
/// [`decode_pixmap`] of a JPEG or TIFF at 1/2^`shrink` of its size, `shrink` 1 to
/// [`designcraft_images::MAX_REDUCED_SHRINK`], without a full-size decode. `None` where only a
/// full decode applies (other formats, CMYK JPEGs).
pub(crate) fn decode_pixmap_reduced(bytes: &[u8], shrink: u8) -> Option<Pixmap> {
+159 -25
View File
@@ -324,6 +324,27 @@ pub fn decoded(data: &AssetData, page: u32) -> Option<Arc<Pixmap>> {
Some(pm)
}
/// Maximum RGBA byte size accepted for a decoded `Pixmap` (`1 GiB`).
const MAX_PIXMAP_BYTES: u64 = 1 << 30;
/// Smallest mip level of `size` whose reduced decoder dimensions (each side rounded up) fit in
/// `Pixmap` (`u16::MAX` a side) and within `1 GiB` of RGBA. Returns `0` for ordinary sizes and
/// `>= 1` for oversized rasters so zoom-in clamps to a reduced decode instead of hitting
/// [`decode_full`].
fn finest_level(size: (u32, u32)) -> u8 {
let fits = |(w, h): (u32, u32)| {
w <= u32::from(u16::MAX) && h <= u32::from(u16::MAX) && u64::from(w).saturating_mul(u64::from(h)).saturating_mul(4) <= MAX_PIXMAP_BYTES
};
let mut cur = size;
for level in 0..MAX_LEVEL {
if fits(cur) {
return level;
}
cur = (cur.0.div_ceil(2), cur.1.div_ceil(2));
}
MAX_LEVEL
}
/// The mip level of an image `size` pixels at full resolution closest above `on_screen` device
/// pixels per point when drawn `width_pt` points wide.
fn level_for(size: (u32, u32), width_pt: f64, on_screen: f64) -> u8 {
@@ -334,7 +355,7 @@ fn level_for(size: (u32, u32), width_pt: f64, on_screen: f64) -> u8 {
w = (w / 2).max(1);
h = (h / 2).max(1);
}
level
level.max(finest_level(size))
}
/// `size` halved `times` times, as [`halve`] does.
@@ -868,23 +889,18 @@ pub(crate) struct Want {
pub on_screen: f64,
}
/// Dimensions of the JPEG mip level selected for a request. `None` means the request uses the
/// full decode or is not an eligible JPEG, so warming must reserve its full decode size.
/// Dimensions of each mip level selected for a reduced-decode request. `None` means the request
/// uses the full decode or is not an eligible JPEG/TIFF, so warming must reserve its full size.
fn reduced_sizes(bytes: &[u8], page: u32, width_pt: f64, on_screen: f64) -> Option<Vec<(u32, u32)>> {
if page != 0 || !bytes.starts_with(&[0xFF, 0xD8]) {
if page != 0 || !designcraft_images::supports_reduced_decode(bytes) {
return None;
}
let (mut w, mut h) = crate::image_size(bytes)?;
let ppt = w as f64 / width_pt.max(1e-6);
let mut sizes = vec![(w, h)];
while ppt / (1u64 << sizes.len()) as f64 >= on_screen * 1.2 && w > 64 && h > 64 && sizes.len() <= 8 {
(w, h) = ((w / 2).max(1), (h / 2).max(1));
sizes.push((w, h));
}
Some(sizes)
let size = crate::image_size(bytes)?;
let level = level_for(size, width_pt, on_screen);
Some((0..=level).map(|l| halved(size, l)).collect())
}
/// Cache and peak decode estimates for a group of requests that all use reduced JPEG mips.
/// Cache and peak decode estimates for a group of requests that all use reduced mips.
fn reduced_estimates(wants: &[Want]) -> Option<(usize, usize)> {
let mut dims = None;
let mut levels = [false; 9];
@@ -897,7 +913,7 @@ fn reduced_estimates(wants: &[Want]) -> Option<(usize, usize)> {
return None;
}
let shrink = level.min(usize::from(designcraft_images::MAX_REDUCED_SHRINK));
let (_, _, decode_peak) = designcraft_images::reduced_jpeg_estimate(&bytes, shrink as u8)?;
let (_, _, decode_peak) = designcraft_images::reduced_estimate(&bytes, shrink as u8)?;
for cached in levels.iter_mut().take(level + 1).skip(shrink) {
*cached = true;
}
@@ -1011,8 +1027,8 @@ fn failed(c: &Cache, k: &(SourceKey, u32), generation: u64) -> bool {
}
/// What decoding the drawings `wanted` of the asset `key` adds to the cache and holds at its
/// peak, and whether that is their reduced JPEG estimate (else a full decode's, which keeps only
/// the levels drawn).
/// peak, and whether that is their reduced JPEG/TIFF estimate (else a full decode's, which keeps
/// only the levels drawn).
fn estimate(key: (SourceKey, u32), wanted: &[Want], levels: &DrawnLevels) -> (usize, usize, bool) {
fault(key.0);
if let Some((size, decoding)) = reduced_estimates(wanted) {
@@ -1035,9 +1051,9 @@ fn estimate(key: (SourceKey, u32), wanted: &[Want], levels: &DrawnLevels) -> (us
/// of everything else cached. An asset some drawing of which finds no level to draw is not held:
/// it is decoded again, and the cached levels its decode estimate covers count with that
/// estimate, not with everything else. The estimate of an asset in `reduced` covers the levels
/// from the one read from the JPEG blocks to each one drawn, as [`reduced_estimates`] counts
/// them; a full decode's covers the levels drawn. The levels of the assets in `index` become the
/// most recently used, so later stores evict other images first.
/// from the one read by the reduced JPEG or TIFF decoder to each one drawn, as [`reduced_estimates`]
/// counts them; a full decode's covers the levels drawn. The levels of the assets in `index` become
/// the most recently used, so later stores evict other images first.
fn held_in_view(
c: &mut Cache,
index: &HashMap<(SourceKey, u32), usize>,
@@ -1191,16 +1207,16 @@ fn waited(id: SourceKey) {
WAITED.1.notify_all();
}
/// Level `level` of a JPEG for [`mip`], decoded at 1/2, 1/4 or 1/8 of its size straight from
/// its blocks and halved from there, so a screen-sized level needs no full-size decode. `None`
/// when the full size is drawn, for other formats and for JPEGs only a full decode reads (CMYK).
/// Level `level` of a JPEG or TIFF for [`mip`], decoded at 1/2, 1/4 or 1/8 of its size without a
/// full-size decode and halved from there. `None` when the full size is drawn, for other formats,
/// and for variants only a full decode reads (CMYK JPEGs, non-CMYK separated TIFFs).
///
/// `gate`: wait at [`GATE`] for the decode's peak bytes, plus the encoded bytes it holds.
fn reduced(id: SourceKey, bytes: &[u8], page: u32, level: u8, gate: Option<usize>, colour: u64) -> Option<Arc<Pixmap>> {
if level == 0 || page != 0 {
return None;
}
if !bytes.starts_with(&[0xFF, 0xD8]) {
if !designcraft_images::supports_reduced_decode(bytes) {
return None;
}
let shrink = level.min(designcraft_images::MAX_REDUCED_SHRINK);
@@ -1210,10 +1226,10 @@ fn reduced(id: SourceKey, bytes: &[u8], page: u32, level: u8, gate: Option<usize
// Cut to `halve`'s size (it rounds down, the decoder up), as the level a full
// decode makes.
let (sw, sh) = halved(crate::image_size(bytes)?, shrink);
// Gated, a JPEG without an estimate (one only a full decode reads) isn't tried here.
// Gated, a file without an estimate (one only a full decode reads) isn't tried here.
let _reserved = match gate {
Some(held) => {
let (_, _, peak) = designcraft_images::reduced_jpeg_estimate(bytes, shrink)?;
let (_, _, peak) = designcraft_images::reduced_estimate(bytes, shrink)?;
Some(reserve((peak.saturating_mul(3) / 2).saturating_add(held), id, None))
}
None => None,
@@ -1543,6 +1559,124 @@ pub(crate) mod tests {
assert!(mip(&bytes, 0, 256.0, 0.1).is_none());
}
pub(crate) fn packbits_gray_tiff_rect(w: u32, h: u32, rows_per_strip: u32, val: u8) -> Vec<u8> {
let mut row = Vec::new();
let mut left = w as usize;
while left >= 128 {
row.push(129);
row.push(val);
left -= 128;
}
if left > 1 {
row.push((1 - left as isize) as u8);
row.push(val);
} else if left == 1 {
row.push(0);
row.push(val);
}
let n_strips = h.div_ceil(rows_per_strip) as usize;
let entries_count = 9usize;
let offsets_off = 8 + 2 + entries_count * 12 + 4;
let counts_off = offsets_off + n_strips * 4;
let mut data_off = counts_off + n_strips * 4;
let full_strip = row.repeat(rows_per_strip as usize);
let rem_rows = (h % rows_per_strip) as usize;
let rem_strip = row.repeat(rem_rows);
let mut offsets = Vec::with_capacity(n_strips);
let mut counts = Vec::with_capacity(n_strips);
for i in 0..n_strips {
let len = if rem_rows > 0 && i + 1 == n_strips { rem_strip.len() } else { full_strip.len() };
offsets.push(data_off as u32);
counts.push(len as u32);
data_off += len;
}
let tags: [(u16, u16, u32, u32); 9] = [
(256, 4, 1, w),
(257, 4, 1, h),
(258, 3, 1, 8),
(259, 3, 1, 32773),
(262, 3, 1, 1),
(273, 4, n_strips as u32, offsets_off as u32),
(277, 3, 1, 1),
(278, 4, 1, rows_per_strip),
(279, 4, n_strips as u32, counts_off as u32),
];
let mut out = Vec::with_capacity(data_off);
out.extend(b"II*\0");
out.extend(8u32.to_le_bytes());
out.extend((tags.len() as u16).to_le_bytes());
for (tag, kind, count, value) in tags {
out.extend(tag.to_le_bytes());
out.extend(kind.to_le_bytes());
out.extend(count.to_le_bytes());
out.extend(value.to_le_bytes());
}
out.extend(0u32.to_le_bytes());
for o in offsets {
out.extend(o.to_le_bytes());
}
for c in counts {
out.extend(c.to_le_bytes());
}
for i in 0..n_strips {
if rem_rows > 0 && i + 1 == n_strips {
out.extend_from_slice(&rem_strip);
} else {
out.extend_from_slice(&full_strip);
}
}
out
}
fn tiff(w: u32, h: u32) -> AssetData {
let img = image::RgbImage::from_fn(w, h, |x, y| image::Rgb([(x * 255 / w) as u8, (y * 255 / h) as u8, 128]));
let mut out = std::io::Cursor::new(Vec::new());
image::DynamicImage::ImageRgb8(img).write_to(&mut out, image::ImageFormat::Tiff).unwrap();
AssetData::embedded(out.into_inner())
}
#[test]
fn screen_levels_of_a_tiff_and_oversized_tiff_need_no_full_size_decode() {
let bytes = tiff(1024, 768);
let id = bytes.key();
let pm = mip(&bytes, 0, 1024.0, 0.1).unwrap();
assert_eq!((pm.width(), pm.height()), (128, 96));
assert!(lookup((id, 0, 0)).is_none(), "no full-size decode is kept");
let small = mip(&bytes, 0, 1024.0, 0.02).unwrap();
assert_eq!((small.width(), small.height()), (64, 48));
assert!(lookup((id, 0, 0)).is_none(), "coarser mip is halved without decoding full size");
let want = Want { bytes: bytes.clone(), page: 0, width_pt: 1024.0, on_screen: 0.1 };
let (cached, peak) = reduced_estimates(&[want]).unwrap();
let full = decode_sizes(&bytes.load().unwrap(), 0);
assert!(cached < full.0 && peak < full.1, "reduced estimates {cached}/{peak}, full {full:?}");
// Print-scale rasters past 1 GiB RGBA clamp their finest mip level above 0 so zoom-in
// stays on the reduced decode path instead of falling back to `decode_full`.
assert_eq!(finest_level((23_800, 15_840)), 1);
assert_eq!(level_for((23_800, 15_840), 1000.0, 50.0), 1);
assert_eq!(finest_level((131_070, 128)), 1);
assert_eq!(finest_level((131_071, 128)), 2);
assert_eq!(finest_level((32_768, 32_769)), 2);
let over_1gib = AssetData::embedded(packbits_gray_tiff_rect(65_536, 4_097, 256, 160));
assert!(designcraft_images::too_large_to_decode(&over_1gib.load().unwrap()));
let want_big = Want { bytes: over_1gib, page: 0, width_pt: 1000.0, on_screen: 0.5 };
assert!(reduced_estimates(&[want_big]).is_some());
let wide = AssetData::embedded(packbits_gray_tiff_rect(131_071, 32, 8, 170));
assert!(crate::decode_pixmap(&wide.load().unwrap()).is_none(), "131071px side exceeds full Pixmap limit");
let wide_pm = mip(&wide, 0, 1000.0, 50.0).expect("131071x32 TIFF clamps to level 2 instead of failing");
assert!(!is_bad(wide.key(), 0));
assert_eq!((wide_pm.width(), wide_pm.height()), (32_767, 8));
let p = wide_pm.data()[usize::from(wide_pm.width()) + 100];
assert_eq!([p.r, p.g, p.b, p.a], [170, 170, 170, 255]);
}
fn png_sized(w: u32, h: u32) -> AssetData {
let img = image::RgbaImage::from_pixel(w, h, image::Rgba([200, 100, 50, 255]));
let mut out = std::io::Cursor::new(Vec::new());
+12 -2
View File
@@ -54,9 +54,9 @@ pub fn make_proxy(bytes: &[u8], page: u32) -> Option<ProxyImage> {
None
}
/// Decode `bytes`, through the reduced JPEG decode when it still leaves at least [`MAX_SIDE_PX`].
/// Decode `bytes`, through reduced decoding when it still leaves at least [`MAX_SIDE_PX`].
fn decode_for_proxy(bytes: &[u8], page: u32) -> Option<Pixmap> {
if page == 0 && bytes.starts_with(&[0xFF, 0xD8]) {
if page == 0 && designcraft_images::supports_reduced_decode(bytes) {
let (w, h) = image_size(bytes)?;
let long = w.max(h);
let mut shrink = 0u8;
@@ -214,4 +214,14 @@ mod tests {
assert!(p.pixels.0.max(p.pixels.1) <= MAX_SIDE_PX && p.pixels.1 > p.pixels.0);
assert!(p.data.len() <= MAX_BYTES);
}
#[test]
fn oversized_tiff_builds_proxy_without_full_decode() {
let tiff = crate::images::tests::packbits_gray_tiff_rect(65_536, 64, 16, 180);
assert!(crate::decode_pixmap(&tiff).is_none(), "65536px width exceeds full Pixmap limit");
let p = make_proxy(&tiff, 0).expect("proxy generated via reduced banded TIFF decode");
assert_eq!(p.mime, "image/jpeg");
assert_eq!(p.pixels, (MAX_SIDE_PX, 1));
assert!(p.data.len() <= MAX_BYTES);
}
}