From 7906dfffdd735b3ae5c42ae7169fb76e6edf30f1 Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:54:54 +0300 Subject: [PATCH 001/520] print: shrink-to-fit refuses a sheet the written matrix cannot represent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The plan's scale had no lower bound: for a page far wider than the paper (a runaway-zoom export or hostile MediaBox, which sheets::read accepts on finite positive boxes) the scale falls below what the four-decimal, no-exponent number formatter can write, the '0' it produces is a singular CTM, and the job spooled a blank page and reported success — the same outcome the crate already refuses for a bad UserUnit 'rather than silently printing the wrong physical dimensions'. Refuse what the output cannot represent: parse back what number() would write and error when it is zero or more than one percent off. It only fires for sheets wider than ~4,300 in, so no real job is refused. Regression test: shrink_to_fit_refuses_a_sheet_the_output_cannot_represent — plan returned Ok today; with the bound it is a Spool error and nothing is sent. --- crates/print/src/sheets.rs | 15 +++++++++++++++ crates/print/src/tests/windows_native.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/crates/print/src/sheets.rs b/crates/print/src/sheets.rs index d3c989cd8..b94dca774 100644 --- a/crates/print/src/sheets.rs +++ b/crates/print/src/sheets.rs @@ -1038,6 +1038,21 @@ pub(crate) fn plan(sheets: &[Sheet], paper: (f64, f64), fit: Fit) -> Result= 1.0 && close((w, h), paper) { None } else { + // The matrix is written with at most four decimals and a PDF real has no + // exponent, so a scale that rounds away prints nothing at all and one that + // rounds up prints the wrong size. Refuse both, the way a nondefault + // UserUnit is refused: an error, never a job that spools a blank page. + let written = number(scale).parse::().unwrap_or(0.0); + if written <= 0.0 || (written - scale).abs() > scale * 0.01 { + return Err(Error::Spool(format!( + "shrink to fit cannot place sheet {} ({:.2} x {:.2} in) on the {:.2} x {:.2} in paper; nothing was sent", + i + 1, + w / 72.0, + h / 72.0, + pw / 72.0, + ph / 72.0 + ))); + } let [x0, y0, x1, y1] = sheet.bounds; let (cw, ch) = (x1 - x0, y1 - y0); // The paper in the page's unrotated space. diff --git a/crates/print/src/tests/windows_native.rs b/crates/print/src/tests/windows_native.rs index 157fe68b9..3f5014dcd 100644 --- a/crates/print/src/tests/windows_native.rs +++ b/crates/print/src/tests/windows_native.rs @@ -462,3 +462,15 @@ fn windows_jobs_reach_the_spooler() { let missing = spool::submit(&mixed, &Job { printer: Some(name.into()), ..Job::default() }); assert!(matches!(missing, Err(Error::Spool(ref m)) if m.contains(name)), "{missing:?}"); } + +#[test] +fn shrink_to_fit_refuses_a_sheet_the_output_cannot_represent() { + use crate::sheets::{Fit, plan, read}; + // A runaway-zoom export: 16 000 000 pt wide. Written with four decimals the scale would be + // "0" -- a singular matrix, a blank page, reported back as a spooled job. + let huge = TestPage { media: Some([0.0, 0.0, 16_000_000.0, 792.0]), ..LETTER }; + let pdf = test_pdf(&[huge], false); + let sheets = read(&pdf).unwrap(); + let refused = plan(&sheets.sheets, (612.0, 792.0), Fit::Shrink { margin: 18.0 }); + assert!(matches!(refused, Err(Error::Spool(ref m)) if m.contains("shrink")), "{refused:?}"); +} From d2dc637c776a6c7bd4cafa502a5f53a33154e0a5 Mon Sep 17 00:00:00 2001 From: Zach Shallbetter Date: Mon, 12 Oct 2026 21:19:01 -0700 Subject: [PATCH 002/520] Add craft-time: exact media timebase and SMPTE timecode (refs #2) Implements the shared exact media timebase crate (craft-time) for FilmCraft, SoundCraft, EffectCraft, and animation timeline consumers: - Exact Signed Timebase: Tick represents exact media time across [Tick::MIN, Tick::MAX] using TICKS_PER_SECOND = 254_016_000_000, supporting exact integer conversions for standard film, broadcast, audio, and high-framerate rates. - Total Arithmetic Contract: All operations are strictly panic-free under #![deny(clippy::panic)], providing checked_* methods and monotonic saturating division. - Timecode and Film Footage: Full SMPTE 12M drop-frame and non-drop-frame formatting, 35mm and 16mm feet+frames, broadcast zero-padded hours, and EffectCraft unpadded hours. - Differential Testing: 15 test suites incorporating full FilmCraft and EffectCraft parity vectors, boundary saturation tests, and proptest monotonicity invariants. --- Cargo.lock | 133 +++++- Cargo.toml | 1 + README.md | 2 +- crates/time/Cargo.toml | 17 + crates/time/README.md | 110 +++++ crates/time/src/lib.rs | 710 ++++++++++++++++++++++++++++++ crates/time/tests/differential.rs | 315 +++++++++++++ 7 files changed, 1280 insertions(+), 8 deletions(-) create mode 100644 crates/time/Cargo.toml create mode 100644 crates/time/README.md create mode 100644 crates/time/src/lib.rs create mode 100644 crates/time/tests/differential.rs diff --git a/Cargo.lock b/Cargo.lock index 6471411f0..89478f3cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -206,7 +206,16 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2f926cc3060f09db9ebc5b52823d85268d24bb917e472c0c4bea35780a7d" dependencies = [ - "bit-vec", + "bit-vec 0.9.1", +] + +[[package]] +name = "bit-set" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d87354e4229f54a44f7bf2435906a4656dba36026ab6eaca629a2c436a691c" +dependencies = [ + "bit-vec 0.10.1", ] [[package]] @@ -215,6 +224,16 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51" +[[package]] +name = "bit-vec" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5727b15fa97d4f4fee0a3b7c3d550ed0269f54329207b86388de918604e31269" +dependencies = [ + "borsh", + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -254,6 +273,30 @@ dependencies = [ "objc2 0.6.5", ] +[[package]] +name = "borsh" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "553c5d846a6ba5150c65e3b1b8ec073bcf1abc20f9b7220de384a4443ea4e20a" +dependencies = [ + "borsh-derive", + "bytes", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cdfe656708a01f89b451a7d36466e6fe6c414de0aa18fc54f864f6f9ca9f56" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "brotli" version = "8.0.4" @@ -668,6 +711,14 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "craft-time" +version = "0.1.0" +dependencies = [ + "proptest", + "serde", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1918,7 +1969,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "525e9ff3e1a4be2fbea1fdf0e98686a6d98b4d8f937e1bf7402245af1909e8c3" dependencies = [ "byteorder-lite", - "quick-error", + "quick-error 2.0.1", ] [[package]] @@ -2327,7 +2378,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a616d2fb8c89516ac2723a581f69d6c18576046bed761bd6b305e5618e6ae130" dependencies = [ "arrayvec", - "bit-set", + "bit-set 0.10.0", "bitflags 2.13.2", "cfg-if", "cfg_aliases", @@ -3090,12 +3141,38 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +[[package]] +name = "proptest" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8530004ccb15eae51c7e40009fbe317f341f804db54dc033eec1c50be28cfa0" +dependencies = [ + "bit-set 0.11.1", + "bit-vec 0.10.1", + "bitflags 2.13.2", + "chacha20", + "core_detect", + "num-traits", + "rand", + "rand_xorshift", + "regex-syntax", + "rusty-fork", + "tempfile", + "unarray", +] + [[package]] name = "pxfm" version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quick-error" version = "2.0.1" @@ -3215,6 +3292,15 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "rand_xorshift" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf" +dependencies = [ + "rand_core 0.10.1", +] + [[package]] name = "range-alloc" version = "0.1.5" @@ -3328,6 +3414,12 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + [[package]] name = "renderdoc-sys" version = "1.1.0" @@ -3548,6 +3640,18 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" +[[package]] +name = "rusty-fork" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2" +dependencies = [ + "fnv", + "quick-error 1.2.3", + "tempfile", + "wait-timeout", +] + [[package]] name = "rustybuzz" version = "0.20.1" @@ -4327,7 +4431,7 @@ dependencies = [ "fax", "flate2", "half", - "quick-error", + "quick-error 2.0.1", "weezl", "zune-jpeg", ] @@ -4604,6 +4708,12 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + [[package]] name = "unic-langid" version = "0.9.6" @@ -4823,6 +4933,15 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + [[package]] name = "walkdir" version = "2.5.0" @@ -5137,8 +5256,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c018fce9b6270aa203c2fdd56f3cce996713534bd757e4ea58c8560b121f14" dependencies = [ "arrayvec", - "bit-set", - "bit-vec", + "bit-set 0.10.0", + "bit-vec 0.9.1", "bitflags 2.13.2", "bytemuck", "cfg_aliases", @@ -5210,7 +5329,7 @@ dependencies = [ "android_system_properties", "arrayvec", "ash", - "bit-set", + "bit-set 0.10.0", "bitflags 2.13.2", "block2 0.6.2", "bytemuck", diff --git a/Cargo.toml b/Cargo.toml index 868e568dd..cf90e022a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ craft-i18n = { path = "crates/i18n" } craft-print = { path = "crates/print" } craft-quit = { path = "crates/quit" } craft-settings = { path = "crates/settings" } +craft-time = { path = "crates/time" } craft-ui = { path = "crates/ui" } egui = { version = "0.36", default-features = false } rayon = "1" diff --git a/README.md b/README.md index 78b243407..2bfd050a9 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,11 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " Tick(i64::MAX)`), preserving strict mathematical monotonicity across the entire signed 64-bit range without inflection. + +## Guarantees & Doctrines + +- **Zero App Code**: Zero dependencies on application crates. +- **Never Crash**: `#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]`. +- **Total Arithmetic**: Division by zero or hostile rates from damaged projects return well-defined fallback states rather than panicking. Arithmetic saturates at 64-bit limits with 128-bit intermediate checks. +- **Forbid Unsafe**: `#![forbid(unsafe_code)]`. diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs new file mode 100644 index 000000000..002eccb38 --- /dev/null +++ b/crates/time/src/lib.rs @@ -0,0 +1,710 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +//! Exact media time for the Craft suite. +//! +//! All time is an integer number of [`Tick`]s at [`TICKS_PER_SECOND`] = 254 016 000 000/s. That +//! rate divides evenly into every broadcast frame duration (23.976, 24, 25, 29.97, 30, 48, 50, +//! 59.94, 60, 119.88, 120 fps) and standard audio sample rates (44.1 kHz, 48 kHz, and their +//! multiples), so edits, frame math, audio alignment, and visual effects never drift. +//! +//! Timecode (SMPTE drop/non-drop, frames, feet+frames, audio samples) is a display representation +//! of ticks. Every conversion is total: invalid, zero, or negative rates produce well-defined defaults +//! rather than division-by-zero panics. + +use serde::{Deserialize, Serialize}; +use std::fmt; +use std::ops::{Add, AddAssign, Div, Mul, Neg, Rem, Sub, SubAssign}; + +/// Clock ticks per second: 254 016 000 000. +pub const TICKS_PER_SECOND: i64 = 254_016_000_000; + +/// A point or duration in time, in ticks. +/// Arithmetic saturates at storage limits; representable results remain exact. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Tick(pub i64); + +impl Tick { + pub const ZERO: Tick = Tick(0); + pub const MAX: Tick = Tick(i64::MAX / 4); + pub const MIN: Tick = Tick(i64::MIN / 4); + + /// Seconds to ticks, clamped to [`Tick::MIN`]..=[`Tick::MAX`]. + pub fn from_seconds_f64(s: f64) -> Tick { + let t = (s * TICKS_PER_SECOND as f64).round(); + Tick((t as i64).clamp(Tick::MIN.0, Tick::MAX.0)) + } + + pub fn seconds(self) -> f64 { + self.0 as f64 / TICKS_PER_SECOND as f64 + } + + /// Exact conversion from a count of `units` at `per_second` (e.g. samples at 48 000). + pub fn from_units(units: i64, per_second: i64) -> Tick { + Tick(clamp_i64((units as i128 * TICKS_PER_SECOND as i128).checked_div(per_second as i128).unwrap_or(0))) + } + + /// Floor conversion to a count of units at `per_second`. + pub fn to_units_floor(self, per_second: i64) -> i64 { + clamp_i64((self.0 as i128 * per_second as i128).div_euclid(TICKS_PER_SECOND as i128)) + } + + /// Conversion from a rational timestamp `pts * num / den` seconds (container timebases). + pub fn from_rational(pts: i64, num: i64, den: i64) -> Tick { + if den == 0 { + return Tick::ZERO; + } + let Some(t) = (pts as i128 * num as i128).checked_mul(TICKS_PER_SECOND as i128) else { + return Tick(if (pts < 0) ^ (num < 0) ^ (den < 0) { i64::MIN } else { i64::MAX }); + }; + Tick(clamp_i64(t.div_euclid(den as i128))) + } + + /// Inverse of [`Tick::from_rational`], floored to the timebase. + pub fn to_rational_floor(self, num: i64, den: i64) -> i64 { + clamp_i64((self.0 as i128 * den as i128).checked_div_euclid(num as i128 * TICKS_PER_SECOND as i128).unwrap_or(0)) + } + + /// Inverse of [`Tick::from_rational`], rounded to the nearest timebase unit (halves up). + pub fn to_rational_round(self, num: i64, den: i64) -> i64 { + let n = self.0 as i128 * den as i128; + let unit = num as i128 * TICKS_PER_SECOND as i128; + let (n, unit) = if unit < 0 { (-n, -unit) } else { (n, unit) }; + let (Some(q), Some(r)) = (n.checked_div_euclid(unit), n.checked_rem_euclid(unit)) else { return 0 }; + clamp_i64(if r * 2 >= unit { q + 1 } else { q }) + } + + pub fn abs(self) -> Tick { + Tick(self.0.saturating_abs()) + } + + pub fn signum(self) -> i64 { + self.0.signum() + } + + pub fn min(self, o: Tick) -> Tick { + Tick(self.0.min(o.0)) + } + + pub fn max(self, o: Tick) -> Tick { + Tick(self.0.max(o.0)) + } + + pub fn clamp(self, lo: Tick, hi: Tick) -> Tick { + Tick(self.0.max(lo.0).min(hi.0)) + } + + /// Scale by a rational factor (`num/den`), flooring. + pub fn mul_ratio(self, num: i64, den: i64) -> Tick { + Tick(clamp_i64((self.0 as i128 * num as i128).checked_div_euclid(den as i128).unwrap_or(0))) + } + + /// Divide by a rational factor (`num/den`), flooring. + pub fn div_ratio(self, num: i64, den: i64) -> Tick { + self.mul_ratio(den, num) + } + + /// Checked addition. Returns `None` if overflow occurred. + pub fn checked_add(self, rhs: Tick) -> Option { + self.0.checked_add(rhs.0).map(Tick) + } + + /// Checked subtraction. Returns `None` if overflow occurred. + pub fn checked_sub(self, rhs: Tick) -> Option { + self.0.checked_sub(rhs.0).map(Tick) + } + + /// Checked multiplication. Returns `None` if overflow occurred. + pub fn checked_mul(self, rhs: i64) -> Option { + self.0.checked_mul(rhs).map(Tick) + } + + /// Checked division. Returns `None` if `rhs == 0` or if overflow occurred (`Tick::MIN / -1`). + pub fn checked_div(self, rhs: i64) -> Option { + self.0.checked_div(rhs).map(Tick) + } + + /// Checked remainder. Returns `None` if `rhs.0 == 0` or if overflow occurred (`Tick::MIN % Tick(-1)`). + pub fn checked_rem(self, rhs: Tick) -> Option { + self.0.checked_rem(rhs.0).map(Tick) + } + + /// Saturating addition. + pub fn saturating_add(self, rhs: Tick) -> Tick { + Tick(self.0.saturating_add(rhs.0)) + } + + /// Saturating subtraction. + pub fn saturating_sub(self, rhs: Tick) -> Tick { + Tick(self.0.saturating_sub(rhs.0)) + } + + /// Saturating multiplication. + pub fn saturating_mul(self, rhs: i64) -> Tick { + Tick(self.0.saturating_mul(rhs)) + } + + /// Saturating division. Returns `None` if `rhs == 0`, and saturates on overflow (`Tick::MIN / -1 -> Tick(i64::MAX)`). + pub fn saturating_div(self, rhs: i64) -> Option { + if rhs == 0 { None } else { Some(Tick(self.0.saturating_div(rhs))) } + } +} + +impl Add for Tick { + type Output = Tick; + fn add(self, o: Tick) -> Tick { + Tick(self.0.saturating_add(o.0)) + } +} + +impl Sub for Tick { + type Output = Tick; + fn sub(self, o: Tick) -> Tick { + Tick(self.0.saturating_sub(o.0)) + } +} + +impl Neg for Tick { + type Output = Tick; + fn neg(self) -> Tick { + Tick(self.0.saturating_neg()) + } +} + +impl AddAssign for Tick { + fn add_assign(&mut self, o: Tick) { + self.0 = self.0.saturating_add(o.0); + } +} + +impl SubAssign for Tick { + fn sub_assign(&mut self, o: Tick) { + self.0 = self.0.saturating_sub(o.0); + } +} + +impl Mul for Tick { + type Output = Tick; + fn mul(self, rhs: i64) -> Tick { + Tick(self.0.saturating_mul(rhs)) + } +} + +impl Div for Tick { + type Output = Tick; + fn div(self, rhs: i64) -> Tick { + // Operator division policy: Div saturates on overflow (e.g. Tick::MIN / -1 -> Tick(i64::MAX)) + // using self.0.saturating_div(rhs) to preserve monotonicity across the full range. + // Division by zero yields Tick::ZERO. For fallible division that detects zero divisors, + // use Tick::checked_div(). + if rhs == 0 { Tick::ZERO } else { Tick(self.0.saturating_div(rhs)) } + } +} + +impl Rem for Tick { + type Output = Tick; + fn rem(self, rhs: Tick) -> Tick { + // Operator remainder policy: Rem produces exact remainder where defined, + // and Tick::ZERO for zero divisor or Tick::MIN % Tick(-1) (which is mathematically 0). + // For fallible remainder, use Tick::checked_rem(). + if rhs.0 == 0 || (self.0 == i64::MIN && rhs.0 == -1) { Tick::ZERO } else { Tick(self.0 % rhs.0) } + } +} + +impl fmt::Display for Tick { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} ticks", self.0) + } +} + +/// A half-open time range `[start, start + duration)`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct TimeRange { + pub start: Tick, + pub duration: Tick, +} + +impl TimeRange { + pub fn new(start: Tick, duration: Tick) -> Self { + Self { start, duration } + } + + pub fn from_bounds(start: Tick, end: Tick) -> Self { + Self { start, duration: end - start } + } + + pub fn end(&self) -> Tick { + self.start + self.duration + } + + pub fn contains(&self, t: Tick) -> bool { + t >= self.start && t < self.end() + } + + pub fn overlaps(&self, o: &TimeRange) -> bool { + self.start < o.end() && o.start < self.end() + } + + pub fn intersect(&self, o: &TimeRange) -> Option { + let s = self.start.max(o.start); + let e = self.end().min(o.end()); + (e > s).then(|| TimeRange::from_bounds(s, e)) + } + + pub fn is_empty(&self) -> bool { + self.duration.0 <= 0 + } +} + +/// A frame rate as an exact rational (`num / den` frames per second). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct FrameRate { + pub num: i64, + pub den: i64, +} + +impl Default for FrameRate { + fn default() -> Self { + FrameRate::FPS_23_976 + } +} + +impl FrameRate { + pub const FPS_23_976: FrameRate = FrameRate { num: 24000, den: 1001 }; + pub const FPS_24: FrameRate = FrameRate { num: 24, den: 1 }; + pub const FPS_25: FrameRate = FrameRate { num: 25, den: 1 }; + pub const FPS_29_97: FrameRate = FrameRate { num: 30000, den: 1001 }; + pub const FPS_30: FrameRate = FrameRate { num: 30, den: 1 }; + pub const FPS_48: FrameRate = FrameRate { num: 48, den: 1 }; + pub const FPS_50: FrameRate = FrameRate { num: 50, den: 1 }; + pub const FPS_59_94: FrameRate = FrameRate { num: 60000, den: 1001 }; + pub const FPS_60: FrameRate = FrameRate { num: 60, den: 1 }; + pub const FPS_119_88: FrameRate = FrameRate { num: 120000, den: 1001 }; + pub const FPS_120: FrameRate = FrameRate { num: 120, den: 1 }; + + /// Standard broadcast and cinematic rates. + pub const COMMON: [FrameRate; 11] = [ + Self::FPS_23_976, + Self::FPS_24, + Self::FPS_25, + Self::FPS_29_97, + Self::FPS_30, + Self::FPS_48, + Self::FPS_50, + Self::FPS_59_94, + Self::FPS_60, + Self::FPS_119_88, + Self::FPS_120, + ]; + + pub fn new(num: i64, den: i64) -> Self { + let g = i128::from(gcd(num.unsigned_abs(), den.unsigned_abs()).max(1)); + FrameRate { num: (i128::from(num) / g) as i64, den: (i128::from(den) / g) as i64 } + } + + /// Closest standard rate for a floating-point frame rate. + pub fn from_f64(fps: f64) -> Self { + for r in Self::COMMON { + if (r.as_f64() - fps).abs() < 0.005 { + return r; + } + } + FrameRate::new((fps * 1000.0).round() as i64, 1000) + } + + /// Returns this frame rate if positive, or the default rate ([`FrameRate::FPS_23_976`]). + /// + /// ### Application Compatibility Note + /// For non-positive or zero rates (e.g. `{0, 0}`), `craft-time` falls back to + /// [`FrameRate::FPS_23_976`] (timecode base 24), providing a standard broadcast default. + /// In contrast, EffectCraft falls back to timecode base 1. + pub fn sane(self) -> FrameRate { + if self.num > 0 && self.den > 0 { self } else { FrameRate::default() } + } + + pub fn as_f64(self) -> f64 { + self.num as f64 / self.den as f64 + } + + /// Exact duration of one frame (floored for exotic rates). + pub fn frame_duration(self) -> Tick { + let r = self.sane(); + Tick(clamp_i64((TICKS_PER_SECOND as i128 * r.den as i128) / r.num as i128)) + } + + /// Index of the frame containing `t` (floor). + pub fn frame_at(self, t: Tick) -> i64 { + clamp_i64(frame_index(self.sane(), t)) + } + + /// Start tick of frame `f` (ceiling division). + pub fn tick_of(self, f: i64) -> Tick { + Tick(clamp_i64(frame_boundary(self.sane(), i128::from(f)))) + } + + /// Snap `t` down to a frame boundary. + pub fn snap(self, t: Tick) -> Tick { + let r = self.sane(); + Tick(clamp_i64(frame_boundary(r, frame_index(r, t)))) + } + + /// Snap `t` to the nearest frame boundary. + pub fn snap_nearest(self, t: Tick) -> Tick { + let r = self.sane(); + let f = frame_index(r, t); + let a = frame_boundary(r, f); + let b = frame_boundary(r, f.saturating_add(1)); + let t = i128::from(t.0); + Tick(clamp_i64(if t - a <= b - t { a } else { b })) + } + + /// Timecode base (nominal frames per second): 30 for 29.97, 24 for 23.976. + pub fn timecode_base(self) -> i64 { + let r = self.sane(); + (r.num / r.den).saturating_add(i64::from(r.num % r.den != 0)).max(1) + } + + pub fn is_ntsc(self) -> bool { + self.den == 1001 + } + + pub fn supports_drop_frame(self) -> bool { + self.is_ntsc() && self.timecode_base() % 30 == 0 + } + + pub fn label(self) -> String { + if self.den == 1 { + format!("{}", self.num) + } else { + let v = self.as_f64(); + let s = format!("{v:.3}"); + s.trim_end_matches('0').trim_end_matches('.').to_string() + } + } +} + +impl fmt::Display for FrameRate { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} fps", self.label()) + } +} + +fn clamp_i64(value: i128) -> i64 { + value.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64 +} + +fn frame_index(rate: FrameRate, t: Tick) -> i128 { + (i128::from(t.0) * i128::from(rate.num)).div_euclid(i128::from(TICKS_PER_SECOND) * i128::from(rate.den)) +} + +fn frame_boundary(rate: FrameRate, frame: i128) -> i128 { + let Some(n) = frame.checked_mul(i128::from(TICKS_PER_SECOND)).and_then(|n| n.checked_mul(i128::from(rate.den))) else { + return if frame < 0 { i128::MIN } else { i128::MAX }; + }; + let d = i128::from(rate.num); + n.div_euclid(d) + i128::from(n.rem_euclid(d) != 0) +} + +fn gcd(mut a: u64, mut b: u64) -> u64 { + while b != 0 { + (a, b) = (b, a % b); + } + a +} + +/// How time is formatted for display. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum TimeDisplay { + #[default] + Timecode, + Frames, + Feet16, + Feet35, + AudioSamples, + Seconds, +} + +impl TimeDisplay { + pub const ALL: [TimeDisplay; 6] = + [TimeDisplay::Timecode, TimeDisplay::Feet35, TimeDisplay::Feet16, TimeDisplay::Frames, TimeDisplay::AudioSamples, TimeDisplay::Seconds]; + + pub fn label(self) -> &'static str { + match self { + TimeDisplay::Timecode => "Timecode", + TimeDisplay::Frames => "Frames", + TimeDisplay::Feet16 => "Feet + Frames 16mm", + TimeDisplay::Feet35 => "Feet + Frames 35mm", + TimeDisplay::AudioSamples => "Audio Samples", + TimeDisplay::Seconds => "Seconds", + } + } +} + +fn df_params(rate: FrameRate) -> (i64, i64) { + let base = rate.timecode_base(); + (base / 15, base) +} + +/// Convert a frame count to SMPTE fields `(negative, h, m, s, f)`. +pub fn frames_to_fields(frame: i64, rate: FrameRate, drop_frame: bool) -> (bool, i64, i64, i64, i64) { + let neg = frame < 0; + let mut n = i128::from(frame).abs(); + let (drop, base) = df_params(rate); + let (drop, base) = (i128::from(drop), i128::from(base)); + if drop_frame && rate.supports_drop_frame() { + let per_min = base * 60 - drop; + let per_10 = per_min * 10 + drop; + let d = n / per_10; + let m = n % per_10; + n = n.saturating_add(drop * 9 * d); + if m > drop { + n = n.saturating_add(drop * ((m - drop) / per_min)); + } + } + let f = n % base; + let s = (n / base) % 60; + let mi = (n / (base * 60)) % 60; + let h = n / (base * 3600); + (neg, clamp_i64(h), clamp_i64(mi), clamp_i64(s), clamp_i64(f)) +} + +/// Convert SMPTE fields back to a frame count. +pub fn fields_to_frames(h: i64, m: i64, s: i64, f: i64, rate: FrameRate, drop_frame: bool) -> i64 { + let (drop, base) = df_params(rate); + let seconds = i128::from(h) * 3600 + i128::from(m) * 60 + i128::from(s); + let mut n = seconds.saturating_mul(i128::from(base)).saturating_add(i128::from(f)); + if drop_frame && rate.supports_drop_frame() { + let total_min = i128::from(h) * 60 + i128::from(m); + n = n.saturating_sub(i128::from(drop) * (total_min - total_min / 10)); + } + clamp_i64(n) +} + +/// Format a frame count as SMPTE timecode (`HH:MM:SS:FF`, or `HH;MM;SS;FF` for drop-frame). +pub fn format_timecode_frames(frame: i64, rate: FrameRate, drop_frame: bool) -> String { + let df = drop_frame && rate.supports_drop_frame(); + let (neg, h, m, s, f) = frames_to_fields(frame, rate, df); + let sep = if df { ';' } else { ':' }; + let fw = if rate.timecode_base() >= 100 { 3 } else { 2 }; + format!("{}{h:02}{sep}{m:02}{sep}{s:02}{sep}{f:0fw$}", if neg { "-" } else { "" }) +} + +/// Format a frame count the way After Effects / EffectCraft displays the current time: hours not zero-padded +/// (`0:00:02:15`, or `0;00;02;15` for drop-frame). +pub fn format_timecode_ae(frame: i64, rate: FrameRate, drop_frame: bool) -> String { + let df = drop_frame && rate.supports_drop_frame(); + let (neg, h, m, s, f) = frames_to_fields(frame, rate, df); + let sep = if df { ';' } else { ':' }; + let fw = if rate.timecode_base() >= 100 { 3 } else { 2 }; + format!("{}{h}{sep}{m:02}{sep}{s:02}{sep}{f:0fw$}", if neg { "-" } else { "" }) +} + +/// Format a tick according to a display mode. +pub fn format_time(t: Tick, rate: FrameRate, drop_frame: bool, display: TimeDisplay, sample_rate: i64) -> String { + let frame = rate.frame_at(t); + match display { + TimeDisplay::Timecode => format_timecode_frames(frame, rate, drop_frame), + TimeDisplay::Frames => format!("{frame}"), + TimeDisplay::Feet35 | TimeDisplay::Feet16 => { + let per_ft = if display == TimeDisplay::Feet35 { 16 } else { 40 }; + let neg = frame < 0; + let a = frame.unsigned_abs(); + format!("{}{}+{:02}", if neg { "-" } else { "" }, a / per_ft, a % per_ft) + } + TimeDisplay::AudioSamples => { + let secs = t.0.div_euclid(TICKS_PER_SECOND); + let rem = Tick(t.0.rem_euclid(TICKS_PER_SECOND)).to_units_floor(sample_rate); + format!("{:02}:{:02}:{:02}:{rem:05}", secs / 3600, (secs / 60) % 60, secs % 60) + } + TimeDisplay::Seconds => format!("{:.3}", t.seconds()), + } +} + +/// Format a frame count as Feet + Frames (film footage counting). +pub fn format_feet_frames(frame: i64, per_foot: i64) -> String { + let pf = per_foot.max(1); + let sign = if frame < 0 { "-" } else { "" }; + let a = frame.unsigned_abs(); + let pf = pf as u64; + let w = if pf > 100 { 3 } else { 2 }; + format!("{sign}{:04}+{:0w$}", a / pf, a % pf) +} + +/// Error from timecode parsing. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ParseError(pub String); + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } +} +impl std::error::Error for ParseError {} + +/// Parse user-typed timecode into a frame count. +/// +/// Supports standard formats (`HH:MM:SS:FF`, `HH;MM;SS;FF`), short entry (`1000` -> `00:00:10:00`), +/// and relative offsets (`+15`, `-1.00`). +/// +/// ### Application Compatibility Note +/// For oversized timecodes whose frame calculations exceed 64-bit limits, `craft-time` +/// saturates to `i64::MAX` (or `i64::MIN`) to keep calculations total without errors. +/// In contrast, EffectCraft returns `Err(ParseError("timecode too large"))`. +pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i64) -> Result { + let s = input.trim(); + if s.is_empty() { + return Err(ParseError("empty timecode".into())); + } + let (rel, body) = match s.as_bytes()[0] { + b'+' => (Some(1), &s[1..]), + b'-' => (Some(-1), &s[1..]), + _ => (None, s), + }; + let parts: Vec<&str> = body.split([':', ';', '.', ',']).collect(); + let nums: Vec = if parts.len() == 1 { + let digits = parts[0]; + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return Err(ParseError(format!("not a timecode: `{input}`"))); + } + if rel.is_some() { + vec![digits.parse().map_err(|_| ParseError("number too large".into()))?] + } else { + let mut v = Vec::new(); + let b = digits.as_bytes(); + let mut end = b.len(); + while end > 0 { + let start = end.saturating_sub(2); + v.push(digits[start..end].parse::().unwrap_or(0)); + end = start; + } + v.reverse(); + v + } + } else { + parts + .iter() + .map(|p| if p.is_empty() { Ok(0) } else { p.parse::().map_err(|_| ParseError(format!("bad field `{p}`"))) }) + .collect::>()? + }; + if nums.len() > 4 { + return Err(ParseError("too many timecode fields".into())); + } + let mut f4 = [0i64; 4]; + let off = 4 - nums.len(); + f4[off..].copy_from_slice(&nums); + let [h, m, sec, fr] = f4; + let base = rate.timecode_base(); + let frames = if nums.len() == 1 && rel.is_some() { + fr + } else if drop_frame && rate.supports_drop_frame() && m < 60 && sec < 60 && fr < base { + fields_to_frames(h, m, sec, fr, rate, true) + } else { + h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec).saturating_mul(base).saturating_add(fr) + }; + Ok(match rel { + Some(sign) => current.saturating_add(frames.saturating_mul(sign)), + None => frames, + }) +} + +/// Parse a Feet + Frames field into a frame count. +pub fn parse_feet_frames(input: &str, per_foot: i64, current: i64) -> Result { + let s = input.trim(); + let bad = || ParseError(format!("not feet+frames: `{input}`")); + let num = |t: &str| -> Result { + let t = t.trim(); + if t.is_empty() { + return Ok(0); + } + if !t.bytes().all(|b| b.is_ascii_digit()) { + return Err(bad()); + } + t.parse::().map_err(|_| ParseError("number too large".into())) + }; + if s.is_empty() { + return Err(ParseError("empty feet+frames".into())); + } + let (sign, body) = match s.as_bytes()[0] { + b'-' => (-1, &s[1..]), + b'+' => (1, &s[1..]), + _ => (0, s), + }; + match body.rfind('+') { + Some(i) => { + let (feet, frames) = (num(&body[..i])?, num(&body[i + 1..])?); + let v = feet.checked_mul(per_foot.max(1)).and_then(|v| v.checked_add(frames)).ok_or_else(|| ParseError("number too large".into()))?; + Ok(if sign < 0 { -v } else { v }) + } + None => { + let v = num(body)?; + Ok(match sign { + 0 => v, + -1 => current.saturating_sub(v), + _ => current.saturating_add(v), + }) + } + } +} + +/// Common audio sample rates offered in sequence settings. +pub const SAMPLE_RATES: [i64; 6] = [32_000, 44_100, 48_000, 88_200, 96_000, 192_000]; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_tick_conversions() { + assert_eq!(Tick::from_seconds_f64(1.0).0, TICKS_PER_SECOND); + assert_eq!(Tick::from_units(48000, 48000).0, TICKS_PER_SECOND); + assert_eq!(Tick(TICKS_PER_SECOND).to_units_floor(48000), 48000); + assert_eq!(Tick::from_rational(1, 1, 1).0, TICKS_PER_SECOND); + assert_eq!(Tick(TICKS_PER_SECOND).to_rational_floor(1, 1), 1); + assert_eq!(Tick(TICKS_PER_SECOND).to_rational_round(1, 1), 1); + } + + #[test] + fn test_hostile_rates_never_panic() { + let zero = FrameRate { num: 0, den: 0 }; + for r in [zero, FrameRate { num: 0, den: 1 }, FrameRate { num: 25, den: 0 }, FrameRate { num: -30, den: 1 }] { + assert_eq!(r.tick_of(10), FrameRate::default().tick_of(10)); + assert_eq!(r.frame_at(Tick(TICKS_PER_SECOND)), FrameRate::default().frame_at(Tick(TICKS_PER_SECOND))); + assert!(r.frame_duration() > Tick::ZERO); + assert!(r.timecode_base() >= 1); + let _ = r.snap_nearest(Tick(12345)); + let _ = format_time(Tick(TICKS_PER_SECOND), r, true, TimeDisplay::Timecode, 48_000); + } + } + + #[test] + fn test_broadcast_frame_rates() { + for r in FrameRate::COMMON { + assert_eq!(r.frame_at(r.tick_of(100)), 100); + assert_eq!(r.snap(r.tick_of(50)), r.tick_of(50)); + assert_eq!(r.snap_nearest(r.tick_of(50)), r.tick_of(50)); + } + } + + #[test] + fn test_smpte_timecode_roundtrip() { + for r in [FrameRate::FPS_24, FrameRate::FPS_25, FrameRate::FPS_30, FrameRate::FPS_29_97] { + for f in [0, 1, 24, 25, 30, 100, 1800, 54000] { + let df = r.supports_drop_frame(); + let tc = format_timecode_frames(f, r, df); + let parsed = parse_timecode(&tc, r, df, 0); + assert_eq!(parsed, Ok(f), "rate: {}, frame: {}, tc: {}", r, f, tc); + } + } + } + + #[test] + fn test_feet_frames_roundtrip() { + for pf in [16, 40] { + for f in [0, 1, 15, 16, 95, 199, 1234] { + let s = format_feet_frames(f, pf); + let parsed = parse_feet_frames(&s, pf, 0); + assert_eq!(parsed, Ok(f)); + } + } + } +} diff --git a/crates/time/tests/differential.rs b/crates/time/tests/differential.rs new file mode 100644 index 000000000..f4634c230 --- /dev/null +++ b/crates/time/tests/differential.rs @@ -0,0 +1,315 @@ +use craft_time::{ + FrameRate, SAMPLE_RATES, TICKS_PER_SECOND, Tick, TimeDisplay, TimeRange, format_feet_frames, format_time, format_timecode_ae, + format_timecode_frames, frames_to_fields, parse_feet_frames, parse_timecode, +}; +use proptest::prelude::*; + +#[test] +fn ticks_per_second_divides_all_standard_rates_evenly() { + assert_eq!(TICKS_PER_SECOND, 254_016_000_000); + + // Divisible by all standard frame rate numerators and denominators + for r in FrameRate::COMMON { + let den = r.den as i128; + let num = r.num as i128; + let product = TICKS_PER_SECOND as i128 * den; + assert_eq!(product % num, 0, "Rate {} ({} / {}) does not divide TICKS_PER_SECOND evenly", r.label(), r.num, r.den); + } + + // Audio sample rates + for &sr in &SAMPLE_RATES { + assert_eq!(TICKS_PER_SECOND % sr, 0, "Sample rate {sr} does not divide TICKS_PER_SECOND evenly"); + } +} + +#[test] +fn frame_roundtrip_all_common_rates() { + for r in FrameRate::COMMON { + for f in -10_000..=10_000 { + let t = r.tick_of(f); + let back = r.frame_at(t); + assert_eq!(back, f, "Roundtrip failed for {} at frame {}", r.label(), f); + assert_eq!(r.snap(t), t, "Snap of exact boundary should be identity"); + assert_eq!(r.snap_nearest(t), t, "Snap nearest of exact boundary should be identity"); + } + } +} + +#[test] +fn time_range_invariants() { + let r1 = TimeRange::new(Tick(100), Tick(50)); + assert_eq!(r1.end(), Tick(150)); + assert!(r1.contains(Tick(100))); + assert!(r1.contains(Tick(149))); + assert!(!r1.contains(Tick(150))); + + let r2 = TimeRange::new(Tick(120), Tick(40)); + assert!(r1.overlaps(&r2)); + let inter = r1.intersect(&r2); + assert_eq!(inter, Some(TimeRange::new(Tick(120), Tick(30)))); + + let empty = TimeRange::new(Tick(100), Tick(0)); + assert!(empty.is_empty()); +} + +#[test] +fn tick_checked_and_saturating_arithmetic_contract() { + // 1. Division overflow: i64::MIN / -1 + let min_tick = Tick(i64::MIN); + assert_eq!(min_tick.checked_div(-1), None, "checked_div must return None on i64::MIN / -1"); + assert_eq!(min_tick.saturating_div(-1), Some(Tick(i64::MAX)), "saturating_div must saturate to Tick(i64::MAX)"); + assert_eq!(min_tick / -1, Tick(i64::MAX), "Operator Div must saturate i64::MIN / -1 to Tick(i64::MAX) without panicking"); + assert_eq!(Tick(i64::MIN + 1) / -1, Tick(i64::MAX)); + assert_eq!(Tick(i64::MIN + 2) / -1, Tick(i64::MAX - 1)); + + // 2. Division by zero + let ten = Tick(10); + assert_eq!(ten.checked_div(0), None, "checked_div must return None on division by zero"); + assert_eq!(ten.saturating_div(0), None, "saturating_div must return None on division by zero"); + assert_eq!(ten / 0, Tick::ZERO, "Operator Div fallback returns Tick::ZERO on zero divisor"); + + // 3. Remainder overflow & zero divisor + assert_eq!(min_tick.checked_rem(Tick(-1)), None, "checked_rem must return None on i64::MIN % -1"); + assert_eq!(ten.checked_rem(Tick(0)), None, "checked_rem must return None on zero divisor"); + assert_eq!(min_tick % Tick(-1), Tick::ZERO, "Operator Rem on i64::MIN % -1 produces Tick::ZERO"); + assert_eq!(ten % Tick(0), Tick::ZERO, "Operator Rem on zero divisor produces Tick::ZERO"); + + // 4. Checked Add / Sub / Mul + assert_eq!(Tick(i64::MAX).checked_add(Tick(1)), None); + assert_eq!(Tick(i64::MIN).checked_sub(Tick(1)), None); + assert_eq!(Tick(i64::MAX).checked_mul(2), None); + assert_eq!(Tick(100).checked_add(Tick(50)), Some(Tick(150))); + assert_eq!(Tick(100).checked_sub(Tick(50)), Some(Tick(50))); + assert_eq!(Tick(100).checked_mul(5), Some(Tick(500))); + + // 5. Saturating Add / Sub / Mul + assert_eq!(Tick(i64::MAX).saturating_add(Tick(1)), Tick(i64::MAX)); + assert_eq!(Tick(i64::MIN).saturating_sub(Tick(1)), Tick(i64::MIN)); + assert_eq!(Tick(i64::MAX).saturating_mul(2), Tick(i64::MAX)); +} + +#[test] +fn hostile_rates_and_timecodes_never_panic() { + let zero = FrameRate { num: 0, den: 0 }; + for r in [ + zero, + FrameRate { num: 0, den: 1 }, + FrameRate { num: 25, den: 0 }, + FrameRate { num: -30, den: 1 }, + FrameRate::from_f64(0.0), + FrameRate::from_f64(f64::NAN), + ] { + assert_eq!(r.tick_of(10), FrameRate::default().tick_of(10)); + assert_eq!(r.frame_at(Tick(TICKS_PER_SECOND)), FrameRate::default().frame_at(Tick(TICKS_PER_SECOND))); + assert!(r.frame_duration() > Tick::ZERO); + assert!(r.timecode_base() >= 1); + let _ = r.snap_nearest(Tick(12345)); + let _ = format_time(Tick(TICKS_PER_SECOND), r, true, TimeDisplay::Timecode, 48_000); + } + assert_eq!(Tick::from_rational(100, 1, 0), Tick::ZERO); + assert_eq!(Tick::from_units(100, 0), Tick::ZERO); + assert_eq!(Tick(5).to_rational_floor(0, 1), 0); + assert_eq!(Tick(5).to_rational_round(0, 1), 0); + for t in [i64::MIN, -1, 0, 1, i64::MAX] { + for (n, d) in [(i64::MIN, 1), (-1, 1000), (1, i64::MAX), (i64::MAX, i64::MIN), (1, 0)] { + let _ = Tick(t).to_rational_round(n, d); + } + } + assert_eq!(Tick(5).mul_ratio(1, 0), Tick::ZERO); + assert_eq!(Tick(5).clamp(Tick(10), Tick(0)), Tick(0)); + let _ = format_timecode_frames(i64::MIN, FrameRate::FPS_29_97, true); + for tc in ["99999999999:99:99:99", "9223372036854775807", "+9223372036854775807", "99999999999;59;59;29"] { + let _ = parse_timecode(tc, FrameRate::FPS_29_97, true, i64::MAX); + let _ = parse_timecode(tc, FrameRate::FPS_25, false, i64::MIN); + } +} + +#[test] +fn boundary_rates_saturate_without_wrapping_and_keep_representable_values_exact() { + let slow = FrameRate { num: 1, den: i64::MAX }; + assert_eq!(slow.frame_duration(), Tick(i64::MAX)); + assert_eq!(slow.tick_of(i64::MAX), Tick(i64::MAX)); + assert_eq!(slow.tick_of(i64::MIN), Tick(i64::MIN)); + assert_eq!(slow.snap_nearest(Tick(i64::MAX)), Tick::ZERO, "compare the real boundary before saturating"); + assert_eq!(slow.snap_nearest(Tick(i64::MIN)), Tick::ZERO); + + let fast = FrameRate { num: i64::MAX, den: 1 }; + assert_eq!(fast.tick_of(i64::MAX), Tick(TICKS_PER_SECOND), "large factors can cancel exactly"); + assert_eq!(fast.tick_of(-i64::MAX), Tick(-TICKS_PER_SECOND)); + assert_eq!(fast.frame_at(Tick(TICKS_PER_SECOND)), i64::MAX); + assert_eq!(fast.frame_at(Tick(i64::MAX)), i64::MAX); + assert_eq!(fast.frame_at(Tick(i64::MIN)), i64::MIN); + assert_eq!(fast.timecode_base(), i64::MAX); + + for t in [i64::MIN, -TICKS_PER_SECOND, -1, 0, 1, TICKS_PER_SECOND, i64::MAX] { + assert_eq!(fast.snap(Tick(t)), Tick(t), "sub-tick frames include every integer tick"); + assert_eq!(fast.snap_nearest(Tick(t)), Tick(t)); + } + + let balanced = FrameRate { num: i64::MAX, den: i64::MAX }; + assert_eq!(balanced.frame_duration(), Tick(TICKS_PER_SECOND)); + assert_eq!(balanced.tick_of(2), Tick(2 * TICKS_PER_SECOND)); + assert_eq!(balanced.frame_at(Tick(2 * TICKS_PER_SECOND)), 2); + assert_eq!(balanced.timecode_base(), 1); + assert_eq!(FrameRate::new(i64::MIN, i64::MIN), FrameRate { num: -1, den: -1 }); + assert_eq!(FrameRate::new(i64::MIN, 0), FrameRate { num: -1, den: 0 }); + assert_eq!(FrameRate::new(i64::MIN, 1), FrameRate { num: i64::MIN, den: 1 }); +} + +#[test] +fn scalar_time_conversions_saturate_at_storage_limits() { + assert_eq!(Tick::from_units(i64::MAX, 1), Tick(i64::MAX)); + assert_eq!(Tick::from_units(i64::MIN, 1), Tick(i64::MIN)); + assert_eq!(Tick(i64::MAX).to_units_floor(i64::MAX), i64::MAX); + assert_eq!(Tick(i64::MIN).to_units_floor(i64::MAX), i64::MIN); + assert_eq!(Tick::from_rational(i64::MAX, i64::MAX, 1), Tick(i64::MAX)); + assert_eq!(Tick::from_rational(i64::MIN, i64::MAX, 1), Tick(i64::MIN)); + assert_eq!(Tick::from_rational(i64::MIN, i64::MAX, -1), Tick(i64::MAX)); + assert_eq!(Tick::from_rational(i64::MAX, i64::MAX, 0), Tick::ZERO); + assert_eq!(Tick(i64::MAX).to_rational_floor(1, i64::MAX), i64::MAX); + assert_eq!(Tick(i64::MIN).to_rational_round(1, i64::MAX), i64::MIN); + assert_eq!(Tick(i64::MAX).mul_ratio(i64::MAX, 1), Tick(i64::MAX)); + assert_eq!(Tick(i64::MIN).abs(), Tick(i64::MAX)); +} + +#[test] +fn smpte_drop_frame_exact_counts_and_negative_boundaries() { + let r = FrameRate::FPS_29_97; + assert!(r.supports_drop_frame()); + + // 10 minutes at 29.97: 10 * 60 * 30 - 9 * 2 = 17982 frames + let ten_min_frames = 17982; + let tc = format_timecode_frames(ten_min_frames, r, true); + assert_eq!(tc, "00;10;00;00"); + assert_eq!(parse_timecode(&tc, r, true, 0), Ok(ten_min_frames)); + + // 1 minute drop-frame boundary: frame 1800 is 00;01;00;02 + let one_min_frame = 1800; + let tc_1m = format_timecode_frames(one_min_frame, r, true); + assert_eq!(tc_1m, "00;01;00;02"); + assert_eq!(parse_timecode(&tc_1m, r, true, 0), Ok(one_min_frame)); + + // Negative drop-frame boundaries + let neg_1m = -1800; + let neg_tc = format_timecode_frames(neg_1m, r, true); + assert_eq!(neg_tc, "-00;01;00;02"); + assert_eq!(parse_timecode(&neg_tc, r, true, 0), Ok(neg_1m)); + + let neg_10m = -17982; + let neg_10m_tc = format_timecode_frames(neg_10m, r, true); + assert_eq!(neg_10m_tc, "-00;10;00;00"); + assert_eq!(parse_timecode(&neg_10m_tc, r, true, 0), Ok(neg_10m)); + + let neg_1h = -107892; + let neg_1h_tc = format_timecode_frames(neg_1h, r, true); + assert_eq!(neg_1h_tc, "-01;00;00;00"); + assert_eq!(parse_timecode(&neg_1h_tc, r, true, 0), Ok(neg_1h)); +} + +#[test] +fn effectcraft_ae_differential_formatting() { + let r30 = FrameRate::FPS_30; + let r_df = FrameRate::FPS_29_97; + + // EffectCraft / After Effects formats hours without zero-padding + assert_eq!(format_timecode_ae(75, r30, false), "0:00:02:15"); + assert_eq!(format_timecode_ae(75, r_df, true), "0;00;02;15"); + + // FilmCraft / Premiere formats hours with 2 zero-padded digits + assert_eq!(format_timecode_frames(75, r30, false), "00:00:02:15"); + assert_eq!(format_timecode_frames(75, r_df, true), "00;00;02;15"); + + // Negative frames in AE style + assert_eq!(format_timecode_ae(-75, r30, false), "-0:00:02:15"); + assert_eq!(format_timecode_ae(-1800, r_df, true), "-0;01;00;02"); + + // Multi-hour parity + assert_eq!(format_timecode_ae(3600 * 30 * 12, r30, false), "12:00:00:00"); + assert_eq!(format_timecode_frames(3600 * 30 * 12, r30, false), "12:00:00:00"); +} + +#[test] +fn feet_and_frames_film_standards() { + // 35mm: 16 frames per foot + assert_eq!(format_feet_frames(0, 16), "0000+00"); + assert_eq!(format_feet_frames(199, 16), "0012+07"); + assert_eq!(parse_feet_frames("12+07", 16, 0), Ok(199)); + assert_eq!(parse_feet_frames("0012+07", 16, 0), Ok(199)); + assert_eq!(format_feet_frames(-199, 16), "-0012+07"); + assert_eq!(parse_feet_frames("-0012+07", 16, 0), Ok(-199)); + + // 16mm: 40 frames per foot + assert_eq!(format_feet_frames(95, 40), "0002+15"); + assert_eq!(parse_feet_frames("2+15", 40, 0), Ok(95)); +} + +#[test] +fn exotic_frame_rate_durations() { + let r = FrameRate::from_f64(37.516); + assert_eq!(r.frame_duration().0, 6_770_871_094); + + // Standard rates must remain exact + assert_eq!(FrameRate::FPS_23_976.frame_duration().0, 10_594_584_000); + assert_eq!(FrameRate::FPS_24.frame_duration().0, 10_584_000_000); + assert_eq!(FrameRate::FPS_25.frame_duration().0, 10_160_640_000); + assert_eq!(FrameRate::FPS_29_97.frame_duration().0, 8_475_667_200); + assert_eq!(FrameRate::FPS_30.frame_duration().0, 8_467_200_000); + assert_eq!(FrameRate::FPS_60.frame_duration().0, 4_233_600_000); +} + +proptest! { + #[test] + fn prop_monotonic_frame_math( + frame in -100_000i64..100_000, + rate_idx in 0usize..FrameRate::COMMON.len(), + ) { + let r = FrameRate::COMMON[rate_idx]; + let t1 = r.tick_of(frame); + let t2 = r.tick_of(frame + 1); + prop_assert!(t1 < t2, "tick_of must be strictly increasing"); + prop_assert_eq!(r.frame_at(t1), frame); + prop_assert_eq!(r.frame_at(t2 - Tick(1)), frame); + } + + #[test] + fn prop_feet_frames_roundtrip( + frame in -50_000i64..50_000, + per_foot in 1i64..120, + ) { + let s = format_feet_frames(frame, per_foot); + let parsed = parse_feet_frames(&s, per_foot, 0); + prop_assert_eq!(parsed, Ok(frame)); + } + + #[test] + fn prop_drop_frame_bijection(f in 0i64..(24 * 107_892)) { + let r = FrameRate::FPS_29_97; + let (_, _h, m, s, fr) = frames_to_fields(f, r, true); + // dropped labels never appear + prop_assert!(!(s == 0 && fr < 2 && m % 10 != 0)); + let txt = format_timecode_frames(f, r, true); + prop_assert_eq!(parse_timecode(&txt, r, true, 0).unwrap(), f); + } + + #[test] + fn prop_ndf_parse_roundtrip(f in -500_000i64..500_000, ri in 0usize..11) { + let r = FrameRate::COMMON[ri]; + let txt = format_timecode_frames(f, r, false); + prop_assert_eq!(parse_timecode(&txt, r, false, 0).unwrap(), f); + } + + #[test] + fn prop_tick_div_monotonicity( + a in any::(), + b in any::(), + d in any::().prop_filter("non-zero", |&x| x != 0), + ) { + if a <= b { + if d > 0 { + prop_assert!((Tick(a) / d) <= (Tick(b) / d)); + } else { + prop_assert!((Tick(a) / d) >= (Tick(b) / d)); + } + } + } +} From 5416121e09a5f88763e027dac3aa33c56094b9a2 Mon Sep 17 00:00:00 2001 From: Zach Shallbetter Date: Tue, 20 Oct 2026 17:14:16 -0700 Subject: [PATCH 003/520] Address review findings in craft-time (fixes #15) - Clarify Tick(i64::MIN) overflow documentation in division/remainder contracts. - Format negative audio sample timestamps with absolute components and leading minus sign. - Parse 4-field negative timecodes as absolute positions rather than relative offsets. - Reject signed values inside multi-field timecode components. --- crates/time/src/lib.rs | 96 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs index 002eccb38..a3fbfc355 100644 --- a/crates/time/src/lib.rs +++ b/crates/time/src/lib.rs @@ -119,12 +119,12 @@ impl Tick { self.0.checked_mul(rhs).map(Tick) } - /// Checked division. Returns `None` if `rhs == 0` or if overflow occurred (`Tick::MIN / -1`). + /// Checked division. Returns `None` if `rhs == 0` or if overflow occurred (`Tick(i64::MIN) / -1`). pub fn checked_div(self, rhs: i64) -> Option { self.0.checked_div(rhs).map(Tick) } - /// Checked remainder. Returns `None` if `rhs.0 == 0` or if overflow occurred (`Tick::MIN % Tick(-1)`). + /// Checked remainder. Returns `None` if `rhs.0 == 0` or if overflow occurred (`Tick(i64::MIN) % Tick(-1)`). pub fn checked_rem(self, rhs: Tick) -> Option { self.0.checked_rem(rhs.0).map(Tick) } @@ -144,7 +144,7 @@ impl Tick { Tick(self.0.saturating_mul(rhs)) } - /// Saturating division. Returns `None` if `rhs == 0`, and saturates on overflow (`Tick::MIN / -1 -> Tick(i64::MAX)`). + /// Saturating division. Returns `None` if `rhs == 0`, and saturates on overflow (`Tick(i64::MIN) / -1 -> Tick(i64::MAX)`). pub fn saturating_div(self, rhs: i64) -> Option { if rhs == 0 { None } else { Some(Tick(self.0.saturating_div(rhs))) } } @@ -193,7 +193,7 @@ impl Mul for Tick { impl Div for Tick { type Output = Tick; fn div(self, rhs: i64) -> Tick { - // Operator division policy: Div saturates on overflow (e.g. Tick::MIN / -1 -> Tick(i64::MAX)) + // Operator division policy: Div saturates on overflow (e.g. Tick(i64::MIN) / -1 -> Tick(i64::MAX)) // using self.0.saturating_div(rhs) to preserve monotonicity across the full range. // Division by zero yields Tick::ZERO. For fallible division that detects zero divisors, // use Tick::checked_div(). @@ -205,7 +205,7 @@ impl Rem for Tick { type Output = Tick; fn rem(self, rhs: Tick) -> Tick { // Operator remainder policy: Rem produces exact remainder where defined, - // and Tick::ZERO for zero divisor or Tick::MIN % Tick(-1) (which is mathematically 0). + // and Tick::ZERO for zero divisor or Tick(i64::MIN) % Tick(-1) (which is mathematically 0). // For fallible remainder, use Tick::checked_rem(). if rhs.0 == 0 || (self.0 == i64::MIN && rhs.0 == -1) { Tick::ZERO } else { Tick(self.0 % rhs.0) } } @@ -512,9 +512,15 @@ pub fn format_time(t: Tick, rate: FrameRate, drop_frame: bool, display: TimeDisp format!("{}{}+{:02}", if neg { "-" } else { "" }, a / per_ft, a % per_ft) } TimeDisplay::AudioSamples => { - let secs = t.0.div_euclid(TICKS_PER_SECOND); - let rem = Tick(t.0.rem_euclid(TICKS_PER_SECOND)).to_units_floor(sample_rate); - format!("{:02}:{:02}:{:02}:{rem:05}", secs / 3600, (secs / 60) % 60, secs % 60) + let neg = t.0 < 0; + let abs_ticks = t.0.unsigned_abs(); + let total_secs = abs_ticks / (TICKS_PER_SECOND as u64); + let rem_ticks = abs_ticks % (TICKS_PER_SECOND as u64); + let rem_samples = (rem_ticks as u128 * sample_rate as u128) / (TICKS_PER_SECOND as u128); + let h = total_secs / 3600; + let m = (total_secs / 60) % 60; + let s = total_secs % 60; + format!("{}{:02}:{:02}:{:02}:{:05}", if neg { "-" } else { "" }, h, m, s, rem_samples) } TimeDisplay::Seconds => format!("{:.3}", t.seconds()), } @@ -555,9 +561,9 @@ pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i if s.is_empty() { return Err(ParseError("empty timecode".into())); } - let (rel, body) = match s.as_bytes()[0] { - b'+' => (Some(1), &s[1..]), - b'-' => (Some(-1), &s[1..]), + let (sign_char, body) = match s.as_bytes()[0] { + b'+' => (Some('+'), &s[1..]), + b'-' => (Some('-'), &s[1..]), _ => (None, s), }; let parts: Vec<&str> = body.split([':', ';', '.', ',']).collect(); @@ -566,7 +572,7 @@ pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { return Err(ParseError(format!("not a timecode: `{input}`"))); } - if rel.is_some() { + if sign_char.is_some() { vec![digits.parse().map_err(|_| ParseError("number too large".into()))?] } else { let mut v = Vec::new(); @@ -583,7 +589,16 @@ pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i } else { parts .iter() - .map(|p| if p.is_empty() { Ok(0) } else { p.parse::().map_err(|_| ParseError(format!("bad field `{p}`"))) }) + .map(|p| { + if p.is_empty() { + Ok(0) + } else { + if !p.bytes().all(|b| b.is_ascii_digit()) { + return Err(ParseError(format!("bad field `{p}`"))); + } + p.parse::().map_err(|_| ParseError(format!("bad field `{p}`"))) + } + }) .collect::>()? }; if nums.len() > 4 { @@ -594,17 +609,23 @@ pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i f4[off..].copy_from_slice(&nums); let [h, m, sec, fr] = f4; let base = rate.timecode_base(); - let frames = if nums.len() == 1 && rel.is_some() { + let frames = if nums.len() == 1 && sign_char.is_some() { fr } else if drop_frame && rate.supports_drop_frame() && m < 60 && sec < 60 && fr < base { fields_to_frames(h, m, sec, fr, rate, true) } else { h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec).saturating_mul(base).saturating_add(fr) }; - Ok(match rel { - Some(sign) => current.saturating_add(frames.saturating_mul(sign)), - None => frames, - }) + + match sign_char { + Some('+') => Ok(current.saturating_add(frames)), + Some('-') => { + // A full 4-field timecode with leading '-' represents an absolute negative timecode (e.g. preroll). + // Single-number or partial relative offsets (e.g. -15 or -1.00) offset from current. + if nums.len() == 4 { Ok(-frames) } else { Ok(current.saturating_sub(frames)) } + } + None | Some(_) => Ok(frames), + } } /// Parse a Feet + Frames field into a frame count. @@ -697,6 +718,45 @@ mod tests { } } + #[test] + fn test_audio_samples_formatting_negative_ticks() { + let r = FrameRate::FPS_24; + let neg_1s = Tick(-TICKS_PER_SECOND); + assert_eq!(format_time(neg_1s, r, false, TimeDisplay::AudioSamples, 48_000), "-00:00:01:00000"); + + let pos_1s = Tick(TICKS_PER_SECOND); + assert_eq!(format_time(pos_1s, r, false, TimeDisplay::AudioSamples, 48_000), "00:00:01:00000"); + + let neg_complex = Tick(-3661 * TICKS_PER_SECOND); + assert_eq!(format_time(neg_complex, r, false, TimeDisplay::AudioSamples, 48_000), "-01:01:01:00000"); + + let zero = Tick::ZERO; + assert_eq!(format_time(zero, r, false, TimeDisplay::AudioSamples, 48_000), "00:00:00:00000"); + } + + #[test] + fn test_parse_timecode_negative_absolute_independent_of_current() { + let r = FrameRate::FPS_29_97; + // Negative full 4-field timecode must parse as absolute negative frames, not offset from current + assert_eq!(parse_timecode("-00;01;00;02", r, true, 5000), Ok(-1800)); + assert_eq!(parse_timecode("-00;01;00;02", r, true, 0), Ok(-1800)); + + let r24 = FrameRate::FPS_24; + assert_eq!(parse_timecode("-01:00:00:00", r24, false, 9999), Ok(-86400)); + + // Single-number relative entries must offset from current + assert_eq!(parse_timecode("-15", r24, false, 5000), Ok(4985)); + assert_eq!(parse_timecode("+15", r24, false, 5000), Ok(5015)); + } + + #[test] + fn test_parse_timecode_rejects_signed_inner_fields() { + let r = FrameRate::FPS_24; + assert!(parse_timecode("00:-30:00:00", r, false, 0).is_err()); + assert!(parse_timecode("01:+5:00:00", r, false, 0).is_err()); + assert!(parse_timecode("00:00:-01:00", r, false, 0).is_err()); + } + #[test] fn test_feet_frames_roundtrip() { for pf in [16, 40] { From 7a9fe3a4b2f31e64a2f609babf1c20209ac458da Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 6 Nov 2026 23:00:31 -0800 Subject: [PATCH 004/520] linesweeper: the left intersection scan in docs/sweep.typ uses alpha^(i+1) The pseudocode for the intersection scan to the left looped j = i down to 1 and compared each alpha^j with alpha^i, so its first pass compared alpha^i with itself. The lemma after it is stated for alpha^(i+1), the segment being inserted to the right of alpha^i. The loop body now uses alpha^(i+1), and w^j is the smallest event height between j and i+1. SOURCE.json records the patch in local_patches and the new sha256 of docs/sweep.typ. README.craft.md and NOTICE say one file is changed. --- NOTICE | 8 ++++---- vendor/linesweeper/README.craft.md | 16 ++++++++++------ vendor/linesweeper/SOURCE.json | 6 ++++-- vendor/linesweeper/docs/sweep.typ | 10 +++++----- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/NOTICE b/NOTICE index bd88d43e7..e830ba21f 100644 --- a/NOTICE +++ b/NOTICE @@ -218,10 +218,10 @@ applications retain their own document-font selection and licensing policy. linesweeper linesweeper 0.5.0 (https://crates.io/crates/linesweeper/0.5.0), MIT OR Apache-2.0, is stored in -vendor/linesweeper with its upstream license files and no file changed. Its manifest names Joe Neeman -as author, and its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See -vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for provenance and the files -left out. +vendor/linesweeper with its upstream license files and one local patch, to docs/sweep.typ. Its +manifest names Joe Neeman as author, and its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See +vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for provenance, local changes +and the files left out. Platform lifecycle source provenance diff --git a/vendor/linesweeper/README.craft.md b/vendor/linesweeper/README.craft.md index f683ca53d..d08f25709 100644 --- a/vendor/linesweeper/README.craft.md +++ b/vendor/linesweeper/README.craft.md @@ -1,15 +1,19 @@ # Local linesweeper 0.5.0 copy -This is the crates.io linesweeper 0.5.0 package (MIT OR Apache-2.0) with some files left out and no +This is the crates.io linesweeper 0.5.0 package (MIT OR Apache-2.0) with some files left out and one file changed. `.cargo_vcs_info.json` names the commit it was packaged from, `824b0791a750f7b27b8edcbb6c6244d0e629875e`. That commit is not in the GitHub repository the manifest names, which is archived; its README says development moved off GitHub. -Every file here except this one and `SOURCE.json` is byte for byte the file in the `.crate` archive. -`SOURCE.json` records the hash of the `.crate` download (`source_archive_sha256`) and, for every file -kept, the hash in the package (`source_sha256`) and the hash here (`sha256`). `upstream_repository` is -null because no public repository this copy can name holds `upstream_revision`. `local_patches` is -empty. +Every file here except this one, `SOURCE.json` and `docs/sweep.typ` is byte for byte the file in the +`.crate` archive. `SOURCE.json` records the hash of the `.crate` download (`source_archive_sha256`) +and, for every file kept, the hash in the package (`source_sha256`) and the hash here (`sha256`). +`upstream_repository` is null because no public repository this copy can name holds +`upstream_revision`. `local_patches` lists each change: + +- `docs/sweep.typ`: the intersection scan to the left compares each segment with `alpha^(i+1)` + instead of `alpha^i`. With `alpha^i`, its first pass compared `alpha^i` with itself, and the lemma + after it is stated for `alpha^(i+1)`. `omitted_files` lists the 43 files of the package left out here, with their hashes: diff --git a/vendor/linesweeper/SOURCE.json b/vendor/linesweeper/SOURCE.json index 9c71e51ad..5c98f9209 100644 --- a/vendor/linesweeper/SOURCE.json +++ b/vendor/linesweeper/SOURCE.json @@ -7,7 +7,9 @@ "upstream_repository": null, "upstream_revision": "824b0791a750f7b27b8edcbb6c6244d0e629875e", "upstream_directory": "", - "local_patches": [], + "local_patches": [ + "In `docs/sweep.typ`, the intersection scan to the left compares each segment with `alpha^(i+1)`, the segment the lemma after it is stated for, instead of `alpha^i`." + ], "files": [ { "path": ".cargo_vcs_info.json", "source_sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc", "source_git_blob": null, "sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc" }, { "path": ".gitignore", "source_sha256": "88b59092499ae05234330eb7d0b01e953e969caf05261530e3765eef02536cf0", "source_git_blob": null, "sha256": "88b59092499ae05234330eb7d0b01e953e969caf05261530e3765eef02536cf0" }, @@ -21,7 +23,7 @@ { "path": "benches/linebender_logo.rs", "source_sha256": "d10867f43439d37f0e1da5d30eaed9ab3bfbe655ed47212f65dc20ab4db65cb3", "source_git_blob": null, "sha256": "d10867f43439d37f0e1da5d30eaed9ab3bfbe655ed47212f65dc20ab4db65cb3" }, { "path": "benches/painted_dreams.rs", "source_sha256": "073008db93840d380710e13ed3ae38e08cb823f10c12e5f838dac1ccf37eaecf", "source_git_blob": null, "sha256": "073008db93840d380710e13ed3ae38e08cb823f10c12e5f838dac1ccf37eaecf" }, { "path": "benches/synthetic.rs", "source_sha256": "7ce59bef2e1c4f28b00cb89feb15bc71ca4b098a8d27e7b02ea699aca9bcc810", "source_git_blob": null, "sha256": "7ce59bef2e1c4f28b00cb89feb15bc71ca4b098a8d27e7b02ea699aca9bcc810" }, - { "path": "docs/sweep.typ", "source_sha256": "4766082c8672a7bb4356b5581043489c13b4e3fb8931ba3a5f0a473fab0172f3", "source_git_blob": null, "sha256": "4766082c8672a7bb4356b5581043489c13b4e3fb8931ba3a5f0a473fab0172f3" }, + { "path": "docs/sweep.typ", "source_sha256": "4766082c8672a7bb4356b5581043489c13b4e3fb8931ba3a5f0a473fab0172f3", "source_git_blob": null, "sha256": "571f227f4b43eaba8060c48344e26b639a4fea8787e5c63bd3f826836c5e4806" }, { "path": "examples/boolean_op.rs", "source_sha256": "3b38a1ccda7e736cce4b191a3a45ab4824c4c365190e3eb637b2fb39b5996c9f", "source_git_blob": null, "sha256": "3b38a1ccda7e736cce4b191a3a45ab4824c4c365190e3eb637b2fb39b5996c9f" }, { "path": "examples/bus.rs", "source_sha256": "e206a08be73899f564188052c8ecc15526b54606e52ee2935a64ce6b5723da33", "source_git_blob": null, "sha256": "e206a08be73899f564188052c8ecc15526b54606e52ee2935a64ce6b5723da33" }, { "path": "examples/curve_metrics.rs", "source_sha256": "0db6a5327fd2119a03a9a66624fea3e04b6bbb519655667ad30487aee6c787d2", "source_git_blob": null, "sha256": "0db6a5327fd2119a03a9a66624fea3e04b6bbb519655667ad30487aee6c787d2" }, diff --git a/vendor/linesweeper/docs/sweep.typ b/vendor/linesweeper/docs/sweep.typ index 0f83ac1c6..0c1df1d3d 100644 --- a/vendor/linesweeper/docs/sweep.typ +++ b/vendor/linesweeper/docs/sweep.typ @@ -368,13 +368,13 @@ of the other one. #figure( pseudocode-list([ + *for* $j = i$ down to $1$ - + let $w^j$ be the smallest height of any event between $j$ and $i$ + + let $w^j$ be the smallest height of any event between $j$ and $i+1$ - + *if* $(alpha^j_(-,epsilon), alpha^i)$ cross by $w^j$ - + choose $z$ before the crossing, such that $alpha^j approx_(z,epsilon) alpha^i$ - + insert an intersection event for $(alpha^j, alpha^i)$ at $z$ + + *if* $(alpha^j_(-,epsilon), alpha^(i+1))$ cross by $w^j$ + + choose $z$ before the crossing, such that $alpha^j approx_(z,epsilon) alpha^(i+1)$ + + insert an intersection event for $(alpha^j, alpha^(i+1))$ at $z$ - + *if* $alpha^j_+ (z) <= alpha^i (z)$ for all $z in [y, w^j]$ + + *if* $alpha^j_+ (z) <= alpha^(i+1) (z)$ for all $z in [y, w^j]$ + *break* ]), caption: "Intersection scan to the left" From 95fa868c6eb2d2feadeec578e61674c23ab6b8bf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 10 Nov 2026 22:46:50 -0800 Subject: [PATCH 005/520] linesweeper: docs/sweep.typ spells "conditions" and names q(i+1) Two typos in docs/sweep.typ are corrected: "condi0.2tions" reads "conditions", and the insertion-position argument says q(i+1) is false instead of q(i+i). SOURCE.json records the new sha256 of docs/sweep.typ and the change in local_patches, and README.craft.md lists it. --- vendor/linesweeper/README.craft.md | 2 ++ vendor/linesweeper/SOURCE.json | 5 +++-- vendor/linesweeper/docs/sweep.typ | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/vendor/linesweeper/README.craft.md b/vendor/linesweeper/README.craft.md index d08f25709..4dcc84fb6 100644 --- a/vendor/linesweeper/README.craft.md +++ b/vendor/linesweeper/README.craft.md @@ -14,6 +14,8 @@ and, for every file kept, the hash in the package (`source_sha256`) and the hash - `docs/sweep.typ`: the intersection scan to the left compares each segment with `alpha^(i+1)` instead of `alpha^i`. With `alpha^i`, its first pass compared `alpha^i` with itself, and the lemma after it is stated for `alpha^(i+1)`. +- `docs/sweep.typ`: two typos are corrected. "condi0.2tions" reads "conditions", and `q(i+i)` reads + `q(i+1)`, the first index for which `q` is false. `omitted_files` lists the 43 files of the package left out here, with their hashes: diff --git a/vendor/linesweeper/SOURCE.json b/vendor/linesweeper/SOURCE.json index 5c98f9209..c3175d996 100644 --- a/vendor/linesweeper/SOURCE.json +++ b/vendor/linesweeper/SOURCE.json @@ -8,7 +8,8 @@ "upstream_revision": "824b0791a750f7b27b8edcbb6c6244d0e629875e", "upstream_directory": "", "local_patches": [ - "In `docs/sweep.typ`, the intersection scan to the left compares each segment with `alpha^(i+1)`, the segment the lemma after it is stated for, instead of `alpha^i`." + "In `docs/sweep.typ`, the intersection scan to the left compares each segment with `alpha^(i+1)`, the segment the lemma after it is stated for, instead of `alpha^i`.", + "In `docs/sweep.typ`, the typo \"condi0.2tions\" reads \"conditions\", and `q(i+i)` reads `q(i+1)`, the first index for which `q` is false." ], "files": [ { "path": ".cargo_vcs_info.json", "source_sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc", "source_git_blob": null, "sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc" }, @@ -23,7 +24,7 @@ { "path": "benches/linebender_logo.rs", "source_sha256": "d10867f43439d37f0e1da5d30eaed9ab3bfbe655ed47212f65dc20ab4db65cb3", "source_git_blob": null, "sha256": "d10867f43439d37f0e1da5d30eaed9ab3bfbe655ed47212f65dc20ab4db65cb3" }, { "path": "benches/painted_dreams.rs", "source_sha256": "073008db93840d380710e13ed3ae38e08cb823f10c12e5f838dac1ccf37eaecf", "source_git_blob": null, "sha256": "073008db93840d380710e13ed3ae38e08cb823f10c12e5f838dac1ccf37eaecf" }, { "path": "benches/synthetic.rs", "source_sha256": "7ce59bef2e1c4f28b00cb89feb15bc71ca4b098a8d27e7b02ea699aca9bcc810", "source_git_blob": null, "sha256": "7ce59bef2e1c4f28b00cb89feb15bc71ca4b098a8d27e7b02ea699aca9bcc810" }, - { "path": "docs/sweep.typ", "source_sha256": "4766082c8672a7bb4356b5581043489c13b4e3fb8931ba3a5f0a473fab0172f3", "source_git_blob": null, "sha256": "571f227f4b43eaba8060c48344e26b639a4fea8787e5c63bd3f826836c5e4806" }, + { "path": "docs/sweep.typ", "source_sha256": "4766082c8672a7bb4356b5581043489c13b4e3fb8931ba3a5f0a473fab0172f3", "source_git_blob": null, "sha256": "b9bb12076458469869e3e7723598d35ae391307bf22ea312f1b989949e9829a1" }, { "path": "examples/boolean_op.rs", "source_sha256": "3b38a1ccda7e736cce4b191a3a45ab4824c4c365190e3eb637b2fb39b5996c9f", "source_git_blob": null, "sha256": "3b38a1ccda7e736cce4b191a3a45ab4824c4c365190e3eb637b2fb39b5996c9f" }, { "path": "examples/bus.rs", "source_sha256": "e206a08be73899f564188052c8ecc15526b54606e52ee2935a64ce6b5723da33", "source_git_blob": null, "sha256": "e206a08be73899f564188052c8ecc15526b54606e52ee2935a64ce6b5723da33" }, { "path": "examples/curve_metrics.rs", "source_sha256": "0db6a5327fd2119a03a9a66624fea3e04b6bbb519655667ad30487aee6c787d2", "source_git_blob": null, "sha256": "0db6a5327fd2119a03a9a66624fea3e04b6bbb519655667ad30487aee6c787d2" }, diff --git a/vendor/linesweeper/docs/sweep.typ b/vendor/linesweeper/docs/sweep.typ index 0c1df1d3d..78b589504 100644 --- a/vendor/linesweeper/docs/sweep.typ +++ b/vendor/linesweeper/docs/sweep.typ @@ -555,7 +555,7 @@ height. Note that because the sweep line changes at a discrete set of heights, at every important height $y$ we actually have two weakly-ordered sweep line: the "old" one from infinitesmally before $y$ height and the "new" one from $y$ onwards. Because everything is continuous and the weak ordering conditions are -closed, both the old and new sweep lines satisfy the weak ordering condi0.2tions +closed, both the old and new sweep lines satisfy the weak ordering conditions at $y$. The algorithm goes like this: at every important height we use @@ -623,7 +623,7 @@ Let's check that this insertion position is valid: for every $j <= i$ we have either $j <= i_-$ or $i_- < j <= i$. In the first case, we already observed that $alpha^j_-(y) <= beta(y)$; in the second case, $q(j)$ returned `true` and so $alpha^j_-(y) <= beta^j^+ (y)$. In either case, $alpha^j prec.tilde beta$. -On the other hand, $q(i+i)$ being `false` implies that $alpha^(i+1)_- (y) >= +On the other hand, $q(i+1)$ being `false` implies that $alpha^(i+1)_- (y) >= beta(y)$. This implies that for all $j >= i+1$, $alpha^j_+ (y) >= beta(y)$ and so $alpha^j succ.tilde_y beta$. From e314c9a4f20288c9e7e9e492eec713d2353835db Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 11 Nov 2026 21:37:35 -0800 Subject: [PATCH 006/520] linesweeper: the left-scan lemma in docs/sweep.typ takes ordering on alpha^(i+1) The lemma for the intersection scan to the left asked for the ordering invariant on (alpha^1, ..., alpha^i) and the crossing invariant on (alpha^1, ..., alpha^(i+1)). The bidirectional lemma applies it with alpha^(i+1) = beta before the extended list is known to satisfy the crossing invariant, so the premises now mirror the right scan: ordering on (alpha^1, ..., alpha^(i+1)), crossing on (alpha^1, ..., alpha^i). The argument that the order invariant holds between events cited a "third invariant"; the document defines two, and it now cites the crossing invariant. SOURCE.json gets a local_patches entry and the new sha256 of docs/sweep.typ, and README.craft.md lists the change. --- vendor/linesweeper/README.craft.md | 4 ++++ vendor/linesweeper/SOURCE.json | 5 +++-- vendor/linesweeper/docs/sweep.typ | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/vendor/linesweeper/README.craft.md b/vendor/linesweeper/README.craft.md index 4dcc84fb6..93afa58f1 100644 --- a/vendor/linesweeper/README.craft.md +++ b/vendor/linesweeper/README.craft.md @@ -16,6 +16,10 @@ and, for every file kept, the hash in the package (`source_sha256`) and the hash after it is stated for `alpha^(i+1)`. - `docs/sweep.typ`: two typos are corrected. "condi0.2tions" reads "conditions", and `q(i+i)` reads `q(i+1)`, the first index for which `q` is false. +- `docs/sweep.typ`: the lemma for the intersection scan to the left asks for the ordering invariant on + `(alpha^1, ..., alpha^(i+1))` and the crossing invariant on `(alpha^1, ..., alpha^i)`, which is how + the bidirectional lemma uses it. The argument for the order invariant cites the crossing invariant + instead of a "third invariant"; the document defines two. `omitted_files` lists the 43 files of the package left out here, with their hashes: diff --git a/vendor/linesweeper/SOURCE.json b/vendor/linesweeper/SOURCE.json index c3175d996..e88570f04 100644 --- a/vendor/linesweeper/SOURCE.json +++ b/vendor/linesweeper/SOURCE.json @@ -9,7 +9,8 @@ "upstream_directory": "", "local_patches": [ "In `docs/sweep.typ`, the intersection scan to the left compares each segment with `alpha^(i+1)`, the segment the lemma after it is stated for, instead of `alpha^i`.", - "In `docs/sweep.typ`, the typo \"condi0.2tions\" reads \"conditions\", and `q(i+i)` reads `q(i+1)`, the first index for which `q` is false." + "In `docs/sweep.typ`, the typo \"condi0.2tions\" reads \"conditions\", and `q(i+i)` reads `q(i+1)`, the first index for which `q` is false.", + "In `docs/sweep.typ`, the lemma for the intersection scan to the left asks for the ordering invariant on `(alpha^1, ..., alpha^(i+1))` and the crossing invariant on `(alpha^1, ..., alpha^i)`, as the bidirectional lemma uses it, and the argument for the order invariant cites the crossing invariant instead of a \"third invariant\"." ], "files": [ { "path": ".cargo_vcs_info.json", "source_sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc", "source_git_blob": null, "sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc" }, @@ -24,7 +25,7 @@ { "path": "benches/linebender_logo.rs", "source_sha256": "d10867f43439d37f0e1da5d30eaed9ab3bfbe655ed47212f65dc20ab4db65cb3", "source_git_blob": null, "sha256": "d10867f43439d37f0e1da5d30eaed9ab3bfbe655ed47212f65dc20ab4db65cb3" }, { "path": "benches/painted_dreams.rs", "source_sha256": "073008db93840d380710e13ed3ae38e08cb823f10c12e5f838dac1ccf37eaecf", "source_git_blob": null, "sha256": "073008db93840d380710e13ed3ae38e08cb823f10c12e5f838dac1ccf37eaecf" }, { "path": "benches/synthetic.rs", "source_sha256": "7ce59bef2e1c4f28b00cb89feb15bc71ca4b098a8d27e7b02ea699aca9bcc810", "source_git_blob": null, "sha256": "7ce59bef2e1c4f28b00cb89feb15bc71ca4b098a8d27e7b02ea699aca9bcc810" }, - { "path": "docs/sweep.typ", "source_sha256": "4766082c8672a7bb4356b5581043489c13b4e3fb8931ba3a5f0a473fab0172f3", "source_git_blob": null, "sha256": "b9bb12076458469869e3e7723598d35ae391307bf22ea312f1b989949e9829a1" }, + { "path": "docs/sweep.typ", "source_sha256": "4766082c8672a7bb4356b5581043489c13b4e3fb8931ba3a5f0a473fab0172f3", "source_git_blob": null, "sha256": "c151c2c47032eca41914139b105177d10996b6ece9c93622aaeb31a371cfe17b" }, { "path": "examples/boolean_op.rs", "source_sha256": "3b38a1ccda7e736cce4b191a3a45ab4824c4c365190e3eb637b2fb39b5996c9f", "source_git_blob": null, "sha256": "3b38a1ccda7e736cce4b191a3a45ab4824c4c365190e3eb637b2fb39b5996c9f" }, { "path": "examples/bus.rs", "source_sha256": "e206a08be73899f564188052c8ecc15526b54606e52ee2935a64ce6b5723da33", "source_git_blob": null, "sha256": "e206a08be73899f564188052c8ecc15526b54606e52ee2935a64ce6b5723da33" }, { "path": "examples/curve_metrics.rs", "source_sha256": "0db6a5327fd2119a03a9a66624fea3e04b6bbb519655667ad30487aee6c787d2", "source_git_blob": null, "sha256": "0db6a5327fd2119a03a9a66624fea3e04b6bbb519655667ad30487aee6c787d2" }, diff --git a/vendor/linesweeper/docs/sweep.typ b/vendor/linesweeper/docs/sweep.typ index 78b589504..c77f6cdc6 100644 --- a/vendor/linesweeper/docs/sweep.typ +++ b/vendor/linesweeper/docs/sweep.typ @@ -221,7 +221,7 @@ The first observation is that the sweep line invariants are maintained as we inc to the next event: + For the order invariant, first note that there is an event whenever $y$ leaves the domain of a segment, so $y$ remains in all domains until the next event. Moreover, if at any $y' > y$ the ordering breaks, two line segments must have $epsilon$-crossed one another by $y'$. - The third invariant guarantees that there's an event before this happens, so by the contra-positive until an event happens the ordering + The crossing invariant guarantees that there's an event before this happens, so by the contra-positive until an event happens the ordering constraint is maintained. + The crossing invariant is maintained because the set of things to check (i.e. the set of line segments that $epsilon$-cross one another after $y$) only shrinks as $y$ increases. @@ -383,8 +383,8 @@ of the other one. We'll skip the proof of the following lemma, because it's basically the same as the last one. #lemma[ -Suppose that $(alpha^1, ..., alpha^i)$ satisfy the ordering invariant -at $y$, and suppose also that $(alpha^1, ..., alpha^(i+1))$ +Suppose that $(alpha^1, ..., alpha^(i+1))$ satisfy the ordering invariant +at $y$, and suppose also that $(alpha^1, ..., alpha^i)$ satisfy the crossing invariant at $y$. After running an intersection scan to the left, $(alpha^1, ..., alpha^(i+1))$ satisfy a slightly stronger crossing invariant at $y$: for every $j <= i$, From 75710cfd20a11fecf1a192db43485c61a5f0c59b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 07:18:59 -0500 Subject: [PATCH 007/520] ci: lock workflow actions through an actions.lock lockfile Mutable action tags are a supply-chain risk: a moved tag runs different code without a diff. Every workflow action is now pinned through .github/workflows/actions.lock, and CI verifies the lockfile so an out-of-sync or tampered one fails instead of running. --- .github/workflows/actions.lock | 170 +++++++++++++++++++++++ .github/workflows/ai-core.yml | 6 +- .github/workflows/atomic.yml | 6 +- .github/workflows/audio.yml | 32 ++--- .github/workflows/crw.yml | 26 ++-- .github/workflows/dialogs.yml | 6 +- .github/workflows/doc.yml | 6 +- .github/workflows/docpath.yml | 6 +- .github/workflows/ecproj-format.yml | 6 +- .github/workflows/font-store.yml | 6 +- .github/workflows/formats.yml | 8 +- .github/workflows/i18n.yml | 6 +- .github/workflows/jpeg.yml | 8 +- .github/workflows/keymap.yml | 28 ++-- .github/workflows/media.yml | 16 +-- .github/workflows/pcraft-format.yml | 6 +- .github/workflows/pdf.yml | 40 +++--- .github/workflows/print.yml | 6 +- .github/workflows/proof.yml | 10 +- .github/workflows/ui.yml | 16 +-- .github/workflows/update.yml | 6 +- .github/workflows/vectorcraft-format.yml | 6 +- .github/workflows/video.yml | 16 +-- .github/workflows/web-api.yml | 8 +- .github/workflows/web-host.yml | 10 +- .github/workflows/window.yml | 6 +- sonar-project.properties | 10 +- 27 files changed, 327 insertions(+), 149 deletions(-) create mode 100644 .github/workflows/actions.lock diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock new file mode 100644 index 000000000..0d6c3b8f9 --- /dev/null +++ b/.github/workflows/actions.lock @@ -0,0 +1,170 @@ +# This file is machine-generated by `gh actions-lock`. +# Do not edit by hand; run `gh actions-lock` to update. +# Docs: https://gh.io/actions-lockfile +version: 'v0.0.3' +workflows: + '.github/workflows/ai-core.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/atomic.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/audio.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/crw.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/dialogs.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/doc.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/docpath.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/ecproj-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/font-store.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/formats.yml': + - 'actions/checkout@v7.0.1' + - 'dtolnay/rust-toolchain@stable' + - 'step-security/harden-runner@v2.22.4' + - 'swatinem/rust-cache@v2.9.3' + '.github/workflows/house-rules.yml': [] + '.github/workflows/i18n.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/jpeg.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/keymap.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/media.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/pcraft-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/pdf.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.1.0' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/print.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/proof.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/sonarcloud.yml': [] + '.github/workflows/ui.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/update.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/vectorcraft-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/video.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/web-api.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/web-host.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/window.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' +dependencies: + 'actions/cache@v6.1.0': + ref: 'v6.1.0' + commit: 'sha1-55cc8345863c7cc4c66a329aec7e433d2d1c52a9' + owner_id: 44036562 + repo_id: 215566462 + 'actions/checkout@v7.0.1': + ref: 'v7.0.1' + commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' + owner_id: 44036562 + repo_id: 197814629 + 'actions/setup-python@v7.1.0': + ref: 'v7.1.0' + commit: 'sha1-6d4fd22b20465ccc0f7354476588e8df7c9ca0e0' + owner_id: 44036562 + repo_id: 192625525 + 'dtolnay/rust-toolchain@stable': + ref: 'stable' + commit: 'sha1-b725eda525362e7cf34af9a7243cac1e3f01a7ec' + owner_id: 1940490 + repo_id: 260749683 + 'step-security/dtolnay-rust-toolchain@v1.0.0': + ref: 'v1.0.0' + commit: 'sha1-8dd607977d06adf4ed49b190381e74c17a9dd25f' + owner_id: 88700172 + repo_id: 1249865941 + 'step-security/harden-runner@v2.22.4': + ref: 'v2.22.4' + commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a' + owner_id: 88700172 + repo_id: 422287306 + 'step-security/rust-cache@v2.9.2': + ref: 'v2.9.2' + commit: 'sha1-8871c8ba063416da7c32246932ea6ae08700453e' + owner_id: 88700172 + repo_id: 822636713 + 'step-security/taiki-e-install-action@v2.87.32': + ref: 'v2.87.32' + commit: 'sha1-76747d22a7567cc4438546a84f11bdf18e670ab8' + owner_id: 88700172 + repo_id: 1382753260 + 'swatinem/rust-cache@v2.9.3': + ref: 'v2.9.3' + commit: 'sha1-d1527c8f67ef9af357290475d4af4b0fab8f12e0' + owner_id: 580492 + repo_id: 298565987 diff --git a/.github/workflows/ai-core.yml b/.github/workflows/ai-core.yml index cc8d738ac..5471bf1ec 100644 --- a/.github/workflows/ai-core.yml +++ b/.github/workflows/ai-core.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/atomic.yml b/.github/workflows/atomic.yml index 1471832d2..24f59bc2b 100644 --- a/.github/workflows/atomic.yml +++ b/.github/workflows/atomic.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/audio.yml b/.github/workflows/audio.yml index 0104f848e..3944d3591 100644 --- a/.github/workflows/audio.yml +++ b/.github/workflows/audio.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-lint save-if: ${{ github.ref == 'refs/heads/main' }} @@ -69,15 +69,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false # The workspace's rust-version. - run: rustup toolchain install 1.95.0 --profile minimal - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-msrv save-if: ${{ github.ref == 'refs/heads/main' }} @@ -88,16 +88,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-features save-if: ${{ github.ref == 'refs/heads/main' }} @@ -113,20 +113,20 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-test save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + - uses: actions/cache@v6.1.0 with: path: ${{ runner.temp }}/media-fixtures/opus/vectors # The archive's SHA-256 (scripts/fetch-opus-vectors.sh checks it). diff --git a/.github/workflows/crw.yml b/.github/workflows/crw.yml index 8fec98661..54be7ba3b 100644 --- a/.github/workflows/crw.yml +++ b/.github/workflows/crw.yml @@ -29,17 +29,17 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-${{ matrix.os }} save-if: ${{ github.ref == 'refs/heads/main' }} @@ -53,17 +53,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-wasm save-if: ${{ github.ref == 'refs/heads/main' }} @@ -75,21 +75,21 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-samples save-if: ${{ github.ref == 'refs/heads/main' }} - name: Cache the CC0 sample files - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ${{ runner.temp }}/crw-samples key: crw-samples-${{ hashFiles('crates/crw/tests/samples.json') }} diff --git a/.github/workflows/dialogs.yml b/.github/workflows/dialogs.yml index 97a931aec..c8d43d725 100644 --- a/.github/workflows/dialogs.yml +++ b/.github/workflows/dialogs.yml @@ -23,15 +23,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: clippy diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index a0c619c1e..6caa4f0ee 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/docpath.yml b/.github/workflows/docpath.yml index 571b65eca..e0115f6f5 100644 --- a/.github/workflows/docpath.yml +++ b/.github/workflows/docpath.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/ecproj-format.yml b/.github/workflows/ecproj-format.yml index 16bab05c7..24a3350af 100644 --- a/.github/workflows/ecproj-format.yml +++ b/.github/workflows/ecproj-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/font-store.yml b/.github/workflows/font-store.yml index f246f621a..0578f18ec 100644 --- a/.github/workflows/font-store.yml +++ b/.github/workflows/font-store.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/formats.yml b/.github/workflows/formats.yml index e19902e8c..84d2c0d7e 100644 --- a/.github/workflows/formats.yml +++ b/.github/workflows/formats.yml @@ -72,17 +72,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: dtolnay/rust-toolchain@stable with: components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + - uses: Swatinem/rust-cache@v2.9.3 with: key: ${{ matrix.crate }} save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/i18n.yml b/.github/workflows/i18n.yml index 5f717aeff..7e6903188 100644 --- a/.github/workflows/i18n.yml +++ b/.github/workflows/i18n.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/jpeg.yml b/.github/workflows/jpeg.yml index 900baf678..21db0ab79 100644 --- a/.github/workflows/jpeg.yml +++ b/.github/workflows/jpeg.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-jpeg save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/keymap.yml b/.github/workflows/keymap.yml index aba535265..41fcc0502 100644 --- a/.github/workflows/keymap.yml +++ b/.github/workflows/keymap.yml @@ -27,24 +27,24 @@ jobs: CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Checkout test fonts - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: repository: storytold/craft-fonts ref: b076417b5880bd4154bd22f00695b1d28c6bea83 path: .test-inputs/craft-fonts persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-linux save-if: ${{ github.ref == 'refs/heads/main' }} @@ -71,23 +71,23 @@ jobs: CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Checkout test fonts - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: repository: storytold/craft-fonts ref: b076417b5880bd4154bd22f00695b1d28c6bea83 path: .test-inputs/craft-fonts persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-${{ matrix.os }} save-if: ${{ github.ref == 'refs/heads/main' }} @@ -98,17 +98,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-wasm save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/media.yml b/.github/workflows/media.yml index cb3912d54..4814e35ca 100644 --- a/.github/workflows/media.yml +++ b/.github/workflows/media.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-media-lint save-if: ${{ github.ref == 'refs/heads/main' }} @@ -56,16 +56,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-media-test save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/pcraft-format.yml b/.github/workflows/pcraft-format.yml index 177bb4593..9c66b5a10 100644 --- a/.github/workflows/pcraft-format.yml +++ b/.github/workflows/pcraft-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml index c72ac7f72..25cf71d4e 100644 --- a/.github/workflows/pdf.yml +++ b/.github/workflows/pdf.yml @@ -31,19 +31,19 @@ jobs: RUSTFLAGS: --cfg craft_pdf_unicode_fixtures steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts @@ -61,14 +61,14 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf-backends save-if: ${{ github.ref == 'refs/heads/main' }} @@ -81,19 +81,19 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf-wasm save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts @@ -111,7 +111,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install @@ -122,19 +122,19 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-plate-test save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts diff --git a/.github/workflows/print.yml b/.github/workflows/print.yml index 3838e6c42..8e2108e09 100644 --- a/.github/workflows/print.yml +++ b/.github/workflows/print.yml @@ -24,11 +24,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/proof.yml b/.github/workflows/proof.yml index 0035b1c04..10365a230 100644 --- a/.github/workflows/proof.yml +++ b/.github/workflows/proof.yml @@ -25,27 +25,27 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false # The Language data (craft-i18n `language-data/`), embedded by crates/proof/build.rs. # Pinned to storytold/craft-i18n#114's head (#109's SCOWL dictionaries + judgment/judgement, # CP2.1); TODO(CP2.1): move to #114's merge commit once it merges. - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: repository: storytold/craft-i18n ref: c239f934ace90b7939afaceb23b472ceeafd6b8a path: craft-i18n persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-proof save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/ui.yml b/.github/workflows/ui.yml index 0892f5a2c..f15ef6ddc 100644 --- a/.github/workflows/ui.yml +++ b/.github/workflows/ui.yml @@ -30,21 +30,21 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 - name: Install software Vulkan renderer if: runner.os == 'Linux' run: | sudo apt-get update sudo apt-get install -y mesa-vulkan-drivers libvulkan1 echo 'WGPU_BACKEND=vulkan' >> "$GITHUB_ENV" - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-ui save-if: ${{ github.ref == 'refs/heads/main' }} @@ -59,15 +59,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-ui-wasm save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index f9c8e1498..9a7139026 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -28,15 +28,15 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/vectorcraft-format.yml b/.github/workflows/vectorcraft-format.yml index b2b20463a..74144f30f 100644 --- a/.github/workflows/vectorcraft-format.yml +++ b/.github/workflows/vectorcraft-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy diff --git a/.github/workflows/video.yml b/.github/workflows/video.yml index ec1c16eda..fa667bdfc 100644 --- a/.github/workflows/video.yml +++ b/.github/workflows/video.yml @@ -22,18 +22,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-video-lint - run: cargo fmt -p craft-video --check @@ -60,16 +60,16 @@ jobs: name: Video tests (${{ matrix.os }}) steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-video-${{ matrix.os }} - name: Install ffmpeg (Linux) diff --git a/.github/workflows/web-api.yml b/.github/workflows/web-api.yml index 2460fb569..4bf1a30fe 100644 --- a/.github/workflows/web-api.yml +++ b/.github/workflows/web-api.yml @@ -26,13 +26,13 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy @@ -46,7 +46,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install diff --git a/.github/workflows/web-host.yml b/.github/workflows/web-host.yml index b780ee76b..76e6701e5 100644 --- a/.github/workflows/web-host.yml +++ b/.github/workflows/web-host.yml @@ -27,18 +27,18 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-web-host save-if: ${{ github.ref == 'refs/heads/main' }} @@ -55,7 +55,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install diff --git a/.github/workflows/window.yml b/.github/workflows/window.yml index b07bce384..f14b24038 100644 --- a/.github/workflows/window.yml +++ b/.github/workflows/window.yml @@ -28,15 +28,15 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/sonar-project.properties b/sonar-project.properties index d08c71698..516e4d5cc 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -15,6 +15,14 @@ sonar.cpd.exclusions=**/*tables.rs # S4790 (weak hash): the ICC specification defines the profile ID as the MD5 of the profile, so # no other algorithm produces an ID other software accepts. It identifies, it does not protect. -sonar.issue.ignore.multicriteria=icc_profile_id +sonar.issue.ignore.multicriteria=icc_profile_id,gha-lockfile sonar.issue.ignore.multicriteria.icc_profile_id.ruleKey=rust:S4790 sonar.issue.ignore.multicriteria.icc_profile_id.resourceKey=crates/cms/src/write.rs + +# githubactions:S7637 asks for every action to be pinned to a commit SHA. A workflow covered by +# .github/workflows/actions.lock runs the commit the lockfile records, so its tag refs are pinned +# and the lockfile covers the actions those actions use too. Sonar does not read lockfiles yet, so +# the rule flags the tag refs anyway: +# https://community.sonarsource.com/t/githubactions-s7637-false-positives-for-actions-pinned-through-native-actions-lock/190814 +sonar.issue.ignore.multicriteria.gha-lockfile.ruleKey=githubactions:S7637 +sonar.issue.ignore.multicriteria.gha-lockfile.resourceKey=.github/workflows/** From 5ba5b82fad19ac0baa1058e0cb1535af656a34a8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 07:22:55 -0500 Subject: [PATCH 008/520] ci: verify the actions lockfile in CI Fails the build when .github/workflows/actions.lock stops matching the workflows, or when a locked pin no longer verifies upstream, so a stale or tampered lockfile cannot go unnoticed between dependency updates. --- .github/workflows/actions.lock | 3 +++ .github/workflows/lockfile.yml | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 .github/workflows/lockfile.yml diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index 0d6c3b8f9..c2e897943 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -63,6 +63,9 @@ workflows: - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - 'step-security/rust-cache@v2.9.2' + '.github/workflows/lockfile.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/harden-runner@v2.22.4' '.github/workflows/media.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' diff --git a/.github/workflows/lockfile.yml b/.github/workflows/lockfile.yml new file mode 100644 index 000000000..e630c9a8f --- /dev/null +++ b/.github/workflows/lockfile.yml @@ -0,0 +1,34 @@ +name: Lockfile + +# Fails when .github/workflows/actions.lock is out of sync with the workflows, or when a locked pin +# no longer verifies upstream, so a moved tag cannot slip a different commit into a run. +# Docs: https://gh.io/actions-lockfile +on: + pull_request: + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +env: + GH_TOKEN: ${{ github.token }} + +jobs: + lockfile: + name: Lockfile is in sync + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - name: Harden runner + uses: step-security/harden-runner@v2.22.4 + with: + egress-policy: audit + - uses: actions/checkout@v7.0.1 + with: + persist-credentials: false + - name: Install gh-actions-lock + run: gh extension install github/gh-actions-lock --pin v0.4.1 + - name: Verify the lockfile + run: gh actions-lock --verify From 88271c006c375551f58cce8ed773eb31e83eddfb Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:43:32 +0300 Subject: [PATCH 009/520] print: cover the 1% scale tolerance boundary in shrink-to-fit The bound had only the zero-scale side tested. Add the other edge: a scale whose four-decimal serialization is more than 1% off. A 118 750 pt wide sheet on Letter with an 18 pt margin scales by 576 / 118 750 = 0.00485053, written 0.0049 -- 1.02% off -- and the plan refuses as an error before anything is sent. Just inside the bound (118 700 pt: 0.98% off) still prints, its matrix carrying the written 0.0049. --- crates/print/src/tests/windows_native.rs | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/print/src/tests/windows_native.rs b/crates/print/src/tests/windows_native.rs index 3f5014dcd..bf4335093 100644 --- a/crates/print/src/tests/windows_native.rs +++ b/crates/print/src/tests/windows_native.rs @@ -474,3 +474,30 @@ fn shrink_to_fit_refuses_a_sheet_the_output_cannot_represent() { let refused = plan(&sheets.sheets, (612.0, 792.0), Fit::Shrink { margin: 18.0 }); assert!(matches!(refused, Err(Error::Spool(ref m)) if m.contains("shrink")), "{refused:?}"); } + +#[test] +fn shrink_to_fit_holds_the_one_percent_scale_boundary() { + use crate::sheets::{Fit, plan, read}; + // The matrix carries the scale through the four-decimal formatter, so the plan refuses when + // what would be written is more than 1% off the real scale. Letter with an 18 pt margin gives + // 576 pt of printable width: a 118 700 pt wide, 120 000 pt tall sheet (no quarter turn) scales + // by 576 / 118 700 = 0.00485257, written 0.0049 -- 0.98% short, inside the bound, so it + // prints. + let inside = TestPage { media: Some([0.0, 0.0, 118_700.0, 120_000.0]), ..LETTER }; + let pdf = test_pdf(&[inside], false); + let sheets = read(&pdf).unwrap(); + let edits = plan(&sheets.sheets, (612.0, 792.0), Fit::Shrink { margin: 18.0 }).unwrap(); + let updated = sheets.update(&edits).unwrap(); + let frame = frame_of(&updated, pdf.len()); + let written: f64 = frame.split_whitespace().nth(1).unwrap().parse().unwrap(); + let real = edits[0].frame.as_ref().unwrap().matrix[0]; + assert!(written != real && (written - real).abs() < real * 0.01, "just inside the 1% bound the written scale is accepted: {frame}"); + + // A 118 750 pt wide sheet: 576 / 118 750 = 0.00485053, written 0.0049, 1.02% off -- refused as + // an error before anything is sent, rather than printed a percent too small. + let outside = TestPage { media: Some([0.0, 0.0, 118_750.0, 120_000.0]), ..LETTER }; + let pdf = test_pdf(&[outside], false); + let sheets = read(&pdf).unwrap(); + let refused = plan(&sheets.sheets, (612.0, 792.0), Fit::Shrink { margin: 18.0 }); + assert!(matches!(refused, Err(Error::Spool(ref m)) if m.contains("shrink") && m.contains("nothing was sent")), "{refused:?}"); +} From 714b1f8b3fa9d7d3b4aea5a361a40f8ddc0f9811 Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:16:46 +0300 Subject: [PATCH 010/520] cms: index the primaries with an iterator in the 16-way sum clippy's needless_range_loop (stable) flags the loop variable indexing self.prim_n; the offset is still the bit mask. --- crates/cms/src/synth.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cms/src/synth.rs b/crates/cms/src/synth.rs index e52deb6aa..a9799195c 100644 --- a/crates/cms/src/synth.rs +++ b/crates/cms/src/synth.rs @@ -133,7 +133,7 @@ impl CmykModel { (v + 4.0 * self.tvi[i] * v * (1.0 - v)).clamp(0.0, 1.0) }); let mut s = [0.0; 3]; - for m in 0..16 { + for (m, prim) in self.prim_n.iter().enumerate().take(16) { let mut w = 1.0; for (i, ai) in a.iter().enumerate() { w *= if m & (1 << i) != 0 { *ai } else { 1.0 - ai }; @@ -141,7 +141,7 @@ impl CmykModel { if w == 0.0 { continue; } - for (acc, p) in s.iter_mut().zip(&self.prim_n[m]) { + for (acc, p) in s.iter_mut().zip(prim) { *acc += w * p; } } From 72b6005033e09b8ea2f48943493510cbcdbd8cb6 Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:23:49 +0300 Subject: [PATCH 011/520] Revert "cms: index the primaries with an iterator in the 16-way sum" This reverts commit 714b1f8b3fa9d7d3b4aea5a361a40f8ddc0f9811. --- crates/cms/src/synth.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cms/src/synth.rs b/crates/cms/src/synth.rs index a9799195c..e52deb6aa 100644 --- a/crates/cms/src/synth.rs +++ b/crates/cms/src/synth.rs @@ -133,7 +133,7 @@ impl CmykModel { (v + 4.0 * self.tvi[i] * v * (1.0 - v)).clamp(0.0, 1.0) }); let mut s = [0.0; 3]; - for (m, prim) in self.prim_n.iter().enumerate().take(16) { + for m in 0..16 { let mut w = 1.0; for (i, ai) in a.iter().enumerate() { w *= if m & (1 << i) != 0 { *ai } else { 1.0 - ai }; @@ -141,7 +141,7 @@ impl CmykModel { if w == 0.0 { continue; } - for (acc, p) in s.iter_mut().zip(prim) { + for (acc, p) in s.iter_mut().zip(&self.prim_n[m]) { *acc += w * p; } } From 341c84d1b1c75f9360023508058f42af78656940 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:03:19 +0000 Subject: [PATCH 012/520] Add borrowed PSD head resource views --- crates/psd/src/head.rs | 121 ++++++++++++++++++++++++++++++++++-- crates/psd/src/lib.rs | 4 +- crates/psd/src/resources.rs | 112 +++++++++++++++++++++++++++++---- 3 files changed, 218 insertions(+), 19 deletions(-) diff --git a/crates/psd/src/head.rs b/crates/psd/src/head.rs index ecd19735e..8aee82745 100644 --- a/crates/psd/src/head.rs +++ b/crates/psd/src/head.rs @@ -2,19 +2,67 @@ //! without the layer and mask section or any pixels. //! //! This is what a file browser or a metadata reader needs (size, depth, colour mode, ICC profile, -//! EXIF, XMP, resolution, thumbnail): a few kilobytes at the start of a file that may be -//! gigabytes long. [`Head::from_source`] reads exactly those bytes; [`Head::from_bytes`] accepts a -//! prefix of the file and reports how many more bytes it needs when the prefix is too short. +//! EXIF, XMP, resolution, thumbnail): a few bytes at the start of a file that may be gigabytes +//! long. [`Head::from_source`] reads exactly those bytes; [`Head::from_bytes`] accepts a prefix of +//! the file and reports how many more bytes it needs when the prefix is too short. Both return +//! owned data. For untrusted files with large resource sections, [`HeadView`] borrows the input +//! and parses resource records lazily. use crate::error::Result; use crate::header::Header; use crate::io::Reader; -use crate::resources::{self, ImageResource}; +use crate::resources::{self, ImageResource, ResourceIter}; use crate::source::Source; /// Length of the file header. const HEADER_LEN: u64 = 26; +/// A zero-copy view of a PSD/PSB head. +/// +/// The header and section lengths are validated eagerly. Colour-mode data and resource records +/// remain borrowed from the input; [`HeadView::resources`] validates one resource at a time and +/// stops at the first malformed record. This avoids cloning unknown or attacker-sized resource +/// payloads and lets callers keep valid records that precede a damaged one. +#[derive(Debug, Clone)] +pub struct HeadView<'a> { + /// The file header. + pub header: Header, + /// The colour mode data section (the palette of an indexed file, duotone or HDR toning data). + pub color_mode_data: &'a [u8], + resources: &'a [u8], + /// Offset in the file of the layer and mask information section (of its length field). + pub layer_section_offset: u64, +} + +impl<'a> HeadView<'a> { + /// Parses the fixed header and section bounds from `data` without copying colour data or + /// resources. Individual resource records are validated by the iterator returned from + /// [`HeadView::resources`]. + pub fn from_bytes(data: &'a [u8]) -> Result { + let header = Header::from_bytes(data)?; + let mut r = Reader::new(data); + r.skip(HEADER_LEN as usize)?; + let color_mode_len = r.u32()?; + let color_mode_data = r.bytes_u64(u64::from(color_mode_len))?; + let resource_len = r.u32()?; + let resources = r.bytes_u64(u64::from(resource_len))?; + Ok(HeadView { header, color_mode_data, resources, layer_section_offset: r.pos() as u64 }) + } + + /// Iterates over resource records without allocating. If a record is malformed, its error is + /// returned once and iteration stops; earlier records remain available to the caller. + pub fn resources(&self) -> ResourceIter<'a> { + ResourceIter::new(self.resources) + } + + /// Iterates over at most `max_records` resources. Returns one limit error if additional bytes + /// remain after that many records, then stops. Use this for untrusted files where record + /// processing itself needs a caller-defined work bound. + pub fn resources_limited(&self, max_records: usize) -> ResourceIter<'a> { + ResourceIter::with_limit(self.resources, max_records) + } +} + /// The first three sections of a file. The same values [`crate::PsdFile`] holds in its fields of /// the same names. #[derive(Debug, Clone, PartialEq, Eq)] @@ -132,6 +180,71 @@ mod tests { } } + #[test] + fn head_view_borrows_sections_and_records() { + for (file, bytes) in cases() { + let view = HeadView::from_bytes(&bytes).unwrap(); + let owned = Head::from_bytes(&bytes).unwrap(); + assert_eq!(view.header, owned.header); + assert_eq!(view.color_mode_data, owned.color_mode_data); + assert_eq!(view.layer_section_offset, owned.layer_section_offset); + let bytes_start = bytes.as_ptr() as usize; + let bytes_end = bytes_start + bytes.len(); + let color_start = view.color_mode_data.as_ptr() as usize; + assert!(bytes_start <= color_start && color_start + view.color_mode_data.len() <= bytes_end); + + let borrowed = view.resources().collect::>>().unwrap(); + assert_eq!(borrowed.iter().map(|r| (*r).into_owned()).collect::>(), file.resources); + let nonempty = borrowed.iter().find(|r| !r.data.is_empty()).expect("test resource has bytes"); + let data_start = nonempty.data.as_ptr() as usize; + assert!(bytes_start <= data_start && data_start + nonempty.data.len() <= bytes_end); + } + } + + #[test] + fn head_view_keeps_valid_resources_before_a_malformed_record() { + let (_, mut bytes) = cases().remove(0); + let color_len = u32::from_be_bytes(bytes[26..30].try_into().unwrap()) as usize; + let resource_len_at = 30 + color_len; + let resource_start = resource_len_at + 4; + let name_len = usize::from(bytes[resource_start + 6]); + let name_total = (1 + name_len + 1) & !1; + let data_len_at = resource_start + 6 + name_total; + let data_len = u32::from_be_bytes(bytes[data_len_at..data_len_at + 4].try_into().unwrap()) as usize; + let first_end = data_len_at + 4 + data_len + data_len % 2; + let resource_len = u32::from_be_bytes(bytes[resource_len_at..resource_len_at + 4].try_into().unwrap()); + let damaged_record = b"JUNK"; + bytes[resource_len_at..resource_len_at + 4].copy_from_slice(&(resource_len + damaged_record.len() as u32).to_be_bytes()); + bytes.splice(first_end..first_end, damaged_record.iter().copied()); + + let view = HeadView::from_bytes(&bytes).unwrap(); + let mut resources = view.resources(); + let first = resources.next().expect("first valid resource").unwrap(); + assert_eq!(first.id, 1005); + assert!(resources.next().expect("malformed second resource").is_err()); + assert!(resources.next().is_none(), "iteration stops after the first malformed record"); + assert!(Head::from_bytes(&bytes).is_err(), "the strict owned API still rejects malformed resources"); + } + + #[test] + fn head_view_resource_limit_is_enforced_after_valid_records() { + let (_, mut bytes) = cases().remove(0); + let color_len = u32::from_be_bytes(bytes[26..30].try_into().unwrap()) as usize; + let resource_len_at = 30 + color_len; + let resource_start = resource_len_at + 4; + let resource_len = u32::from_be_bytes(bytes[resource_len_at..resource_start].try_into().unwrap()) as usize; + let duplicate = bytes[resource_start..resource_start + resource_len].to_vec(); + bytes[resource_len_at..resource_start].copy_from_slice(&((resource_len * 2) as u32).to_be_bytes()); + bytes.splice(resource_start + resource_len..resource_start + resource_len, duplicate); + let view = HeadView::from_bytes(&bytes).unwrap(); + let all = view.resources().collect::>>().unwrap(); + assert!(all.len() > 1); + let mut limited = view.resources_limited(1); + assert!(limited.next().unwrap().is_ok()); + assert!(limited.next().unwrap().is_err()); + assert!(limited.next().is_none()); + } + #[test] fn a_prefix_ending_with_the_resources_is_enough_and_one_byte_less_is_not() { for (_, bytes) in cases() { diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 2ad36b035..93acd5f39 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -105,13 +105,13 @@ pub use descriptor::{Descriptor, VersionedDescriptor}; pub use error::{PsdError, Result}; pub use file::{GlobalLayerMask, LayerInfoPlacement, PsdFile}; pub use float::FloatImage; -pub use head::Head; +pub use head::{Head, HeadView}; pub use header::{ColorMode, Header, Version}; pub use image_data::ImageData; pub use layer::{BlendingRanges, ChannelData, LayerFlags, LayerInfo, LayerMask, LayerRecord, MaskData, MaskParameters, RealMask, Rect}; pub use pixels::{GrayImage, Layer, RgbaImage}; pub use preview::Preview; -pub use resources::{ImageResource, ResolutionInfo, ResourceData}; +pub use resources::{ImageResource, ImageResourceView, ResolutionInfo, ResourceData, ResourceIter}; pub use rows::RowDecoder; #[cfg(any(unix, windows))] pub use source::FileSource; diff --git a/crates/psd/src/resources.rs b/crates/psd/src/resources.rs index bfb5a9f72..809bee696 100644 --- a/crates/psd/src/resources.rs +++ b/crates/psd/src/resources.rs @@ -2,7 +2,7 @@ use crate::descriptor::UnicodeString; use crate::error::{PsdError, Result}; -use crate::io::{Reader, WriteExt, read_pascal, write_pascal}; +use crate::io::{Reader, WriteExt, write_pascal}; /// Well-known resource ids. pub mod ids { @@ -48,6 +48,103 @@ pub struct ImageResource { pub data: Vec, } +/// An image-resource record that borrows its name and data from the PSD/PSB input. +/// +/// Use [`ImageResourceView::into_owned`] only when the data must outlive the input bytes. Parsing a +/// view does not allocate proportional to the resource's declared size. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ImageResourceView<'a> { + /// Signature, normally `8BIM` (also `MeSa`, `AgHg`, `PHUT`, `DCSR` seen in the wild). + pub signature: [u8; 4], + /// Resource id. + pub id: u16, + /// Pascal name bytes (usually empty). + pub name: &'a [u8], + /// Resource data (without the even padding). + pub data: &'a [u8], +} + +impl ImageResourceView<'_> { + /// Copies this view into an owned resource. + pub fn into_owned(self) -> ImageResource { + ImageResource { signature: self.signature, id: self.id, name: self.name.to_vec(), data: self.data.to_vec() } + } +} + +impl<'a> ImageResourceView<'a> { + pub(crate) fn read(r: &mut Reader<'a>) -> Result { + let signature = r.array::<4>()?; + if !matches!(&signature, b"8BIM" | b"MeSa" | b"AgHg" | b"PHUT" | b"DCSR") { + return Err(PsdError::InvalidSignature { expected: "8BIM resource", found: signature }); + } + let id = r.u16()?; + let name_len = usize::from(r.u8()?); + let name = r.bytes(name_len)?; + let name_total = 1 + name_len; + let name_pad = (2 - name_total % 2) % 2; + r.skip(name_pad)?; + let len = r.u32()?; + let data = r.bytes_u64(u64::from(len))?; + if len % 2 == 1 { + r.skip(1)?; + } + Ok(ImageResourceView { signature, id, name, data }) + } +} + +/// Lazily parses image-resource records from a borrowed resource section. +/// +/// Records are yielded one at a time. If a record is malformed, the iterator yields its error +/// once and then stops; records yielded before it remain usable. +pub struct ResourceIter<'a> { + reader: Reader<'a>, + remaining_records: usize, + failed: bool, +} + +impl<'a> ResourceIter<'a> { + pub(crate) fn new(bytes: &'a [u8]) -> Self { + ResourceIter::with_limit(bytes, usize::MAX) + } + + pub(crate) fn with_limit(bytes: &'a [u8], max_records: usize) -> Self { + ResourceIter { reader: Reader::new(bytes), remaining_records: max_records, failed: false } + } +} + +impl<'a> Iterator for ResourceIter<'a> { + type Item = Result>; + + fn next(&mut self) -> Option { + if self.failed || self.reader.is_empty() { + return None; + } + if self.remaining_records == 0 { + self.failed = true; + return Some(Err(PsdError::LimitExceeded("image resource record count"))); + } + match ImageResourceView::read(&mut self.reader) { + Ok(resource) => { + self.remaining_records -= 1; + Some(Ok(resource)) + } + Err(error) => { + self.failed = true; + Some(Err(error)) + } + } + } + + fn size_hint(&self) -> (usize, Option) { + // The smallest record has a four-byte signature, two-byte id, one-byte empty name + // (plus its pad), and four-byte data length. This bounds iteration even for a section + // containing millions of empty records, without imposing an arbitrary format limit. + (0, Some(self.reader.remaining() / 12)) + } +} + +impl std::iter::FusedIterator for ResourceIter<'_> {} + /// Fixed-point resolution info (resource 1005). /// /// `h_res_fixed` and `v_res_fixed` are always pixels per inch (16.16 fixed). @@ -292,18 +389,7 @@ impl ImageResource { } pub(crate) fn read(r: &mut Reader<'_>) -> Result { - let signature = r.array::<4>()?; - if !matches!(&signature, b"8BIM" | b"MeSa" | b"AgHg" | b"PHUT" | b"DCSR") { - return Err(PsdError::InvalidSignature { expected: "8BIM resource", found: signature }); - } - let id = r.u16()?; - let name = read_pascal(r, 2)?; - let len = r.u32()?; - let data = r.bytes_u64(u64::from(len))?.to_vec(); - if len % 2 == 1 { - r.skip(1)?; - } - Ok(ImageResource { signature, id, name, data }) + Ok(ImageResourceView::read(r)?.into_owned()) } pub(crate) fn write(&self, out: &mut Vec) -> Result<()> { From cc9c3805433221e47bb6a0a3c4e318b584d271aa Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:05:37 +0000 Subject: [PATCH 013/520] Fix PSD resource iterator size hint --- crates/psd/src/head.rs | 1 + crates/psd/src/resources.rs | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/psd/src/head.rs b/crates/psd/src/head.rs index 8aee82745..ba5054c69 100644 --- a/crates/psd/src/head.rs +++ b/crates/psd/src/head.rs @@ -241,6 +241,7 @@ mod tests { assert!(all.len() > 1); let mut limited = view.resources_limited(1); assert!(limited.next().unwrap().is_ok()); + assert!(limited.size_hint().1.unwrap() >= 1, "limit error is included in the upper bound"); assert!(limited.next().unwrap().is_err()); assert!(limited.next().is_none()); } diff --git a/crates/psd/src/resources.rs b/crates/psd/src/resources.rs index 809bee696..7b12f19a6 100644 --- a/crates/psd/src/resources.rs +++ b/crates/psd/src/resources.rs @@ -136,15 +136,30 @@ impl<'a> Iterator for ResourceIter<'a> { } fn size_hint(&self) -> (usize, Option) { - // The smallest record has a four-byte signature, two-byte id, one-byte empty name - // (plus its pad), and four-byte data length. This bounds iteration even for a section - // containing millions of empty records, without imposing an arbitrary format limit. - (0, Some(self.reader.remaining() / 12)) + // There can be at most one more item than the number of bytes: valid records consume + // bytes, and a malformed record or an exhausted caller limit can yield one final error + // without consuming any. Use a conservative bound so even a short truncated record has + // room for its error item. + let upper = if self.failed || self.reader.is_empty() { 0 } else { self.reader.remaining().saturating_add(1) }; + (0, Some(upper)) } } impl std::iter::FusedIterator for ResourceIter<'_> {} +#[cfg(test)] +mod view_tests { + use super::ResourceIter; + + #[test] + fn truncated_resource_size_hint_includes_error_item() { + let mut iter = ResourceIter::new(b"JUNK"); + assert!(iter.size_hint().1.unwrap() >= 1); + assert!(iter.next().unwrap().is_err()); + assert_eq!(iter.size_hint(), (0, Some(0))); + } +} + /// Fixed-point resolution info (resource 1005). /// /// `h_res_fixed` and `v_res_fixed` are always pixels per inch (16.16 fixed). From ef01d69f7d2f05bd185431f300998ddd9b3b8557 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 19:11:55 -0500 Subject: [PATCH 014/520] Shared document bundle package IO --- Cargo.lock | 10 + Cargo.toml | 1 + crates/document-bundle/Cargo.toml | 17 ++ crates/document-bundle/src/lib.rs | 292 ++++++++++++++++++++++++++++++ crates/zip/src/archive.rs | 7 + 5 files changed, 327 insertions(+) create mode 100644 crates/document-bundle/Cargo.toml create mode 100644 crates/document-bundle/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index cf0e0bd47..174984c1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1336,6 +1336,16 @@ dependencies = [ name = "craft-docpath" version = "0.1.0" +[[package]] +name = "craft-document-bundle" +version = "0.1.0" +dependencies = [ + "craft-zip", + "serde_json", + "thiserror 2.0.21", + "zip", +] + [[package]] name = "craft-docx" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 629bab64c..4735dba39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ craft-dav = { path = "crates/dav" } craft-dialogs = { path = "crates/dialogs" } craft-doc-binary = { path = "crates/doc-binary" } craft-docx = { path = "crates/docx" } +craft-document-bundle = { path = "crates/document-bundle" } craft-dxf = { path = "crates/dxf" } craft-drawingml = { path = "crates/drawingml" } craft-eps = { path = "crates/eps" } diff --git a/crates/document-bundle/Cargo.toml b/crates/document-bundle/Cargo.toml new file mode 100644 index 000000000..705ac7db5 --- /dev/null +++ b/crates/document-bundle/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "craft-document-bundle" +description = "A reusable JSON document bundle envelope over ZIP: metadata, previews, asset entries and legacy inline asset extraction." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-zip = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +zip = { version = "9", default-features = false, features = ["deflate-flate2"] } diff --git a/crates/document-bundle/src/lib.rs b/crates/document-bundle/src/lib.rs new file mode 100644 index 000000000..97acba322 --- /dev/null +++ b/crates/document-bundle/src/lib.rs @@ -0,0 +1,292 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! A JSON document bundle envelope over ZIP. +//! +//! Applications keep their document models. This crate owns the reusable container mechanics: +//! ordered mimetype, metadata, JSON model, previews, embedded asset paths, compression policy, +//! bounded reads and extraction of legacy base64 asset maps. Entry names and MIME type are +//! supplied by the format using the bundle. + +use std::io::{Read, Seek}; + +use craft_zip::{Archive, Compression, Limits, Writer}; +use serde_json::Value; + +const MAX_ENTRIES: u64 = 100_000; +const JSON_LIMIT: u64 = 256 << 20; +const ASSET_LIMIT: u64 = 1 << 30; +const EXPANSION_LIMIT: u64 = 4 << 30; + +/// Per-entry and total-expansion limits for an untrusted document bundle. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ReadLimits { + /// Maximum expanded size of metadata and document JSON. + pub json_bytes: u64, + /// Maximum expanded size of one compressed asset. Stored assets are bounded by archive size. + pub compressed_asset_bytes: u64, + /// Maximum expansion beyond the bytes in the source archive. + pub expansion_bytes: u64, +} + +impl Default for ReadLimits { + fn default() -> Self { + Self { json_bytes: JSON_LIMIT, compressed_asset_bytes: ASSET_LIMIT, expansion_bytes: EXPANSION_LIMIT } + } +} + +/// Names and identity used by one JSON document bundle format. +#[derive(Clone, Copy, Debug)] +pub struct Spec<'a> { + /// Stored first entry and its payload. + pub mimetype_entry: &'a str, + pub mimetype: &'a str, + /// Metadata and document JSON member names. + pub metadata_entry: &'a str, + pub document_entry: &'a str, + /// Optional preview entry name. + pub preview_entry: &'a str, + /// Directory prefix for embedded assets, without a trailing slash. + pub assets_directory: &'a str, + /// Metadata value written in the `format` field. + pub format_name: &'a str, + /// Old single-file JSON field containing base64 assets keyed by decimal id. + pub legacy_assets_field: Option<&'a str>, +} + +/// An embedded asset passed to [`write`]. +#[derive(Clone, Copy, Debug)] +pub struct Asset<'a> { + /// Decimal asset id used in the archive entry path. + pub id: &'a str, + /// MIME type used to select the extension and compression policy. + pub mime: &'a str, + /// File bytes retained byte-for-byte. + pub data: &'a [u8], +} + +/// A document and embedded payloads returned by [`read`]. +#[derive(Clone, Debug, PartialEq)] +pub struct Package { + /// Parsed model JSON, after extracting legacy inline assets. + pub document: Value, + /// Format version declared by metadata, or 1 when absent. + pub version: u64, + /// Embedded asset bytes keyed by decimal asset id. + pub assets: Vec<(String, Vec)>, +} + +/// Why a document bundle could not be read or written. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The bytes are not a bundle or legacy JSON document. + #[error("not a document bundle: {0}")] + NotBundle(String), + /// The bundle requires a newer reader. + #[error("document format {0} is newer than this reader supports")] + TooNew(u64), + /// A member could not be written. + #[error("could not write document bundle: {0}")] + Write(String), + /// A member exceeded a read limit or could not be read. + #[error("could not read document bundle: {0}")] + Read(String), +} + +fn extension(mime: &str) -> &'static str { + match mime { + "image/png" => "png", + "image/jpeg" => "jpg", + "image/gif" => "gif", + "image/webp" => "webp", + "image/tiff" => "tif", + "application/pdf" => "pdf", + "application/postscript" => "eps", + "image/svg+xml" => "svg", + _ => "bin", + } +} + +/// Whether `bytes` are a ZIP bundle that contains the configured document member. +pub fn has_document(bytes: &[u8], spec: Spec<'_>) -> bool { + let Ok(archive) = Archive::open(bytes, Limits { max_entries: MAX_ENTRIES, max_total_bytes: 0 }) else { return false }; + archive.contains(spec.document_entry) +} + +/// Whether `prefix` starts with the configured stored mimetype entry. +pub fn starts_with_mimetype(prefix: &[u8], spec: Spec<'_>) -> bool { + craft_zip::stored_mimetype(prefix).is_some_and(|mime| mime == spec.mimetype.as_bytes()) +} + +/// Write a ZIP document bundle containing the serialized document, metadata, preview and assets. +pub fn write(document: &[u8], version: u32, generator: &str, preview: Option<&[u8]>, assets: &[Asset<'_>], spec: Spec<'_>) -> Result, Error> { + let mut writer = Writer::new(); + writer.add(spec.mimetype_entry, spec.mimetype.as_bytes(), Compression::Stored).map_err(|e| Error::Write(e.to_string()))?; + let metadata = serde_json::json!({"format":spec.format_name, "version":version, "generator":generator}); + let metadata = serde_json::to_vec(&metadata).map_err(|e| Error::Write(e.to_string()))?; + writer.add(spec.metadata_entry, &metadata, Compression::Deflated).map_err(|e| Error::Write(e.to_string()))?; + if let Some(preview) = preview { + writer.add(spec.preview_entry, preview, Compression::Stored).map_err(|e| Error::Write(e.to_string()))?; + } + writer.add(spec.document_entry, document, Compression::Deflated).map_err(|e| Error::Write(e.to_string()))?; + for asset in assets.iter().filter(|asset| !asset.data.is_empty()) { + let path = format!("{}/{}.{}", spec.assets_directory, asset.id, extension(asset.mime)); + let compression = + if matches!(asset.mime, "image/png" | "image/jpeg" | "image/gif" | "image/webp") { Compression::Stored } else { Compression::Deflated }; + writer.add(&path, asset.data, compression).map_err(|e| Error::Write(e.to_string()))?; + } + writer.finish().map_err(|e| Error::Write(e.to_string())) +} + +/// Read a ZIP bundle or legacy JSON document within bounded member sizes. +/// +/// Members under the configured assets directory whose id does not occur in the document's +/// `assets` object are never decompressed. Unreferenced data cannot consume the caller's memory. +pub fn read(bytes: &[u8], supported_version: u32, spec: Spec<'_>) -> Result { + read_with_limits(bytes, supported_version, spec, ReadLimits::default()) +} + +/// Like [`read`], with caller-selected bounded-read limits. +pub fn read_with_limits(bytes: &[u8], supported_version: u32, spec: Spec<'_>, limits: ReadLimits) -> Result { + if !bytes.starts_with(b"PK") { + return read_legacy(bytes, spec); + } + let source_size = u64::try_from(bytes.len()).unwrap_or(u64::MAX); + let mut archive = Archive::open(bytes, Limits { max_entries: MAX_ENTRIES, max_total_bytes: source_size.saturating_add(limits.expansion_bytes) }) + .map_err(|e| Error::NotBundle(e.to_string()))?; + let version = archive + .read(spec.metadata_entry, limits.json_bytes) + .map_err(|e| Error::Read(e.to_string()))? + .and_then(|metadata| serde_json::from_slice::(&metadata).ok()) + .and_then(|metadata| metadata.get("version").and_then(Value::as_u64)) + .unwrap_or(1); + if version > u64::from(supported_version) { + return Err(Error::TooNew(version)); + } + let document_bytes = archive + .read(spec.document_entry, limits.json_bytes) + .map_err(|e| Error::Read(e.to_string()))? + .ok_or_else(|| Error::NotBundle(format!("missing {}", spec.document_entry)))?; + let document: Value = serde_json::from_slice(&document_bytes).map_err(|e| Error::NotBundle(e.to_string()))?; + let wanted: std::collections::HashSet = + document.get("assets").and_then(Value::as_object).into_iter().flat_map(|assets| assets.keys().cloned()).collect(); + let prefix = format!("{}/", spec.assets_directory); + let mut embedded = Vec::new(); + for path in archive.names().map(|name| name.into_owned()).collect::>() { + let Some(member) = path.strip_prefix(&prefix) else { continue }; + let Some(id) = member.split('.').next() else { continue }; + if !wanted.contains(id) || id.parse::().is_err() { + continue; + } + let stored = archive.is_stored(&path).unwrap_or(false); + let limit = if stored { source_size } else { limits.compressed_asset_bytes }; + let data = archive.read(&path, limit).map_err(|e| Error::Read(e.to_string()))?; + if let Some(data) = data { + embedded.push((id.to_owned(), data)); + } + } + Ok(Package { document, version, assets: embedded }) +} + +/// Read a preview with a fixed maximum size without decompressing the full bundle. +pub fn read_preview(bytes: &[u8], preview_entry: &str) -> Option> { + const PREVIEW_LIMIT: u64 = 4 << 20; + let mut archive = Archive::open(bytes, Limits { max_entries: MAX_ENTRIES, max_total_bytes: PREVIEW_LIMIT }).ok()?; + archive.read(preview_entry, PREVIEW_LIMIT).ok().flatten() +} + +/// Read a bounded preview from a seekable file without loading the full bundle into memory. +pub fn read_preview_from(reader: R, preview_entry: &str) -> Option> { + const PREVIEW_LIMIT: u64 = 4 << 20; + let mut zip = zip::ZipArchive::new(reader).ok()?; + let entry = zip.by_name(preview_entry).ok()?; + if entry.size() > PREVIEW_LIMIT { + return None; + } + let mut preview = Vec::new(); + entry.take(PREVIEW_LIMIT.saturating_add(1)).read_to_end(&mut preview).ok()?; + (u64::try_from(preview.len()).ok()? <= PREVIEW_LIMIT).then_some(preview) +} + +fn read_legacy(bytes: &[u8], spec: Spec<'_>) -> Result { + let mut document: Value = serde_json::from_slice(bytes).map_err(|e| Error::NotBundle(e.to_string()))?; + let mut assets = Vec::new(); + if let Some(field) = spec.legacy_assets_field { + if let Some(inline) = document.get(field).and_then(Value::as_object) { + for (id, value) in inline { + if id.parse::().is_ok() + && let Some(encoded) = value.as_str() + { + assets.push((id.clone(), decode_base64(encoded))); + } + } + } + if let Some(object) = document.as_object_mut() { + object.remove(field); + } + } + Ok(Package { document, version: 1, assets }) +} + +fn decode_base64(input: &str) -> Vec { + let mut out = Vec::with_capacity(input.len().saturating_mul(3) / 4); + let (mut buffer, mut bits) = (0u32, 0u32); + for byte in input.bytes() { + let value = match byte { + b'A'..=b'Z' => byte - b'A', + b'a'..=b'z' => byte - b'a' + 26, + b'0'..=b'9' => byte - b'0' + 52, + b'+' | b'-' => 62, + b'/' | b'_' => 63, + _ => continue, + }; + buffer = (buffer << 6) | u32::from(value); + bits += 6; + if bits >= 8 { + bits -= 8; + out.push((buffer >> bits) as u8); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + const SPEC: Spec<'static> = Spec { + mimetype_entry: "mimetype", + mimetype: "application/vnd.test+zip", + metadata_entry: "meta.json", + document_entry: "document.json", + preview_entry: "preview.png", + assets_directory: "assets", + format_name: "test", + legacy_assets_field: Some("assetData"), + }; + + #[test] + fn writes_and_reads_bundle_members() { + let payload = [1u8, 2, 3]; + let bytes = + write(br#"{"assets":{"7":{}}}"#, 4, "Test 0.1", Some(b"png"), &[Asset { id: "7", mime: "image/png", data: &payload }], SPEC).unwrap(); + assert!(has_document(&bytes, SPEC)); + assert!(starts_with_mimetype(&bytes, SPEC)); + let loaded = read(&bytes, 4, SPEC).unwrap(); + assert_eq!(loaded.document, serde_json::json!({"assets":{"7":{}}})); + assert_eq!(loaded.version, 4); + assert_eq!(loaded.assets, [("7".to_owned(), payload.to_vec())]); + assert_eq!(read_preview(&bytes, SPEC.preview_entry).as_deref(), Some(&b"png"[..])); + } + + #[test] + fn extracts_legacy_inline_assets() { + let loaded = read(br#"{"assetData":{"7":"AQID"},"name":"x"}"#, 4, SPEC).unwrap(); + assert_eq!(loaded.document, serde_json::json!({"name":"x"})); + assert_eq!(loaded.assets, [("7".to_owned(), vec![1, 2, 3])]); + } + + #[test] + fn refuses_newer_versions() { + let bytes = write(b"{}", 9, "future", None, &[], SPEC).unwrap(); + assert_eq!(read(&bytes, 4, SPEC), Err(Error::TooNew(9))); + } +} diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs index bdcf64cd7..448e2220b 100644 --- a/crates/zip/src/archive.rs +++ b/crates/zip/src/archive.rs @@ -97,6 +97,13 @@ impl<'a> Archive<'a> { self.zip.by_index_raw(index).ok().map(|f| f.compressed_size()) } + /// Is `name` stored without compression? The answer lets a format with in-memory source bytes + /// apply an output-size cap to compressed entries while bounding stored reads by source size. + pub fn is_stored(&mut self, name: &str) -> Option { + let index = self.zip.index_for_name(name)?; + self.zip.by_index_raw(index).ok().map(|f| f.compression() == zip::CompressionMethod::Stored) + } + /// The first `n` bytes of entry `name` (all of it when it is shorter), or `None` when there /// is no such entry: for telling a file by its signature without inflating it. The bytes are /// charged to the total; a total with less left than the bytes wanted refuses before reading. From dc20b58f0157df05f6059bd408d6f14ab130f343 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 19:19:53 -0500 Subject: [PATCH 015/520] Bound document bundle asset reads --- crates/document-bundle/src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/crates/document-bundle/src/lib.rs b/crates/document-bundle/src/lib.rs index 97acba322..18e40a3ff 100644 --- a/crates/document-bundle/src/lib.rs +++ b/crates/document-bundle/src/lib.rs @@ -152,6 +152,9 @@ pub fn read_with_limits(bytes: &[u8], supported_version: u32, spec: Spec<'_>, li let source_size = u64::try_from(bytes.len()).unwrap_or(u64::MAX); let mut archive = Archive::open(bytes, Limits { max_entries: MAX_ENTRIES, max_total_bytes: source_size.saturating_add(limits.expansion_bytes) }) .map_err(|e| Error::NotBundle(e.to_string()))?; + if archive.has_duplicate_names() { + return Err(Error::NotBundle("the archive has duplicate member names".into())); + } let version = archive .read(spec.metadata_entry, limits.json_bytes) .map_err(|e| Error::Read(e.to_string()))? @@ -209,12 +212,21 @@ pub fn read_preview_from(reader: R, preview_entry: &str) -> Opti fn read_legacy(bytes: &[u8], spec: Spec<'_>) -> Result { let mut document: Value = serde_json::from_slice(bytes).map_err(|e| Error::NotBundle(e.to_string()))?; let mut assets = Vec::new(); + let mut total = 0u64; if let Some(field) = spec.legacy_assets_field { if let Some(inline) = document.get(field).and_then(Value::as_object) { for (id, value) in inline { if id.parse::().is_ok() && let Some(encoded) = value.as_str() { + let decoded_size = u64::try_from(encoded.len()).unwrap_or(u64::MAX).saturating_mul(3) / 4; + if decoded_size > ASSET_LIMIT { + return Err(Error::Read(format!("legacy asset {id} exceeds the {ASSET_LIMIT}-byte limit"))); + } + total = total.saturating_add(decoded_size); + if total > EXPANSION_LIMIT { + return Err(Error::Read(format!("legacy assets exceed the {EXPANSION_LIMIT}-byte total limit"))); + } assets.push((id.clone(), decode_base64(encoded))); } } @@ -289,4 +301,17 @@ mod tests { let bytes = write(b"{}", 9, "future", None, &[], SPEC).unwrap(); assert_eq!(read(&bytes, 4, SPEC), Err(Error::TooNew(9))); } + + #[test] + fn stored_assets_use_source_size_and_deflated_assets_use_their_limit() { + let bytes = vec![7u8; 128]; + let document = br#"{"assets":{"7":{}}}"#; + let limits = ReadLimits { json_bytes: 1 << 20, compressed_asset_bytes: 8, expansion_bytes: 1 << 20 }; + let stored = write(document, 4, "test", None, &[Asset { id: "7", mime: "image/png", data: &bytes }], SPEC).unwrap(); + let package = read_with_limits(&stored, 4, SPEC, limits).unwrap(); + assert_eq!(package.assets, [("7".to_owned(), bytes.clone())]); + + let compressed = write(document, 4, "test", None, &[Asset { id: "7", mime: "application/octet-stream", data: &bytes }], SPEC).unwrap(); + assert!(read_with_limits(&compressed, 4, SPEC, limits).is_err()); + } } From 61d9de7846b04bd70be576ad96427ef35c378885 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:21:00 +0000 Subject: [PATCH 016/520] psd: bound resource iterator size hint --- crates/psd/src/resources.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/psd/src/resources.rs b/crates/psd/src/resources.rs index 7b12f19a6..75b52b199 100644 --- a/crates/psd/src/resources.rs +++ b/crates/psd/src/resources.rs @@ -136,11 +136,10 @@ impl<'a> Iterator for ResourceIter<'a> { } fn size_hint(&self) -> (usize, Option) { - // There can be at most one more item than the number of bytes: valid records consume - // bytes, and a malformed record or an exhausted caller limit can yield one final error - // without consuming any. Use a conservative bound so even a short truncated record has - // room for its error item. - let upper = if self.failed || self.reader.is_empty() { 0 } else { self.reader.remaining().saturating_add(1) }; + // A valid record consumes at least 12 bytes. A truncated record or exhausted limit can + // produce one final error without consuming a full record. + let upper = + if self.failed || self.reader.is_empty() { 0 } else { (self.reader.remaining() / 12).min(self.remaining_records).saturating_add(1) }; (0, Some(upper)) } } @@ -151,6 +150,13 @@ impl std::iter::FusedIterator for ResourceIter<'_> {} mod view_tests { use super::ResourceIter; + #[test] + fn resource_size_hint_respects_the_record_limit() { + let data = vec![0; 12_000]; + let iter = ResourceIter::with_limit(&data, 3); + assert_eq!(iter.size_hint(), (0, Some(4))); + } + #[test] fn truncated_resource_size_hint_includes_error_item() { let mut iter = ResourceIter::new(b"JUNK"); From 409b0036f86f7faa5a1f2d9663eb8f692c97ea5a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:37:31 +0000 Subject: [PATCH 017/520] video: bound H.264 picture number arithmetic --- crates/video/src/h264/decode/dpb.rs | 89 +++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 5 deletions(-) diff --git a/crates/video/src/h264/decode/dpb.rs b/crates/video/src/h264/decode/dpb.rs index 67713d9d3..2615698a4 100644 --- a/crates/video/src/h264/decode/dpb.rs +++ b/crates/video/src/h264/decode/dpb.rs @@ -176,6 +176,7 @@ impl Dpb { return Ok(lists); } let cur_fn = sh.frame_num; + ensure!(max_frame_num > 0 && max_frame_num <= (1 << 16) && cur_fn < max_frame_num, "frame number out of range"); let pic_num = |e: &DpbEntry| Self::frame_num_wrap(e, cur_fn, max_frame_num); let short: Vec<&DpbEntry> = self.entries.iter().filter(|e| e.mark == RefMark::Short).collect(); let mut long: Vec<&DpbEntry> = self.entries.iter().filter(|e| e.mark == RefMark::Long).collect(); @@ -212,6 +213,7 @@ impl Dpb { let mut ref_idx = 0usize; for m in &sh.ref_pic_list_mod[l] { ensure!(ref_idx < n, "too many ref_pic_list_modification entries"); + ensure!(m.idc >= 2 || m.value < max_frame_num, "abs_diff_pic_num_minus1 out of range"); list.push(None); // temporarily one longer let (target, is_long): (Option<&DpbEntry>, bool) = match m.idc { 0 | 1 => { @@ -272,13 +274,13 @@ impl Dpb { /// Adaptive memory control (8.2.5.4). Returns true if MMCO 5 was executed. fn apply_mmcos(&mut self, mmcos: &[Mmco], cur_frame_num: u32, max_frame_num: u32, cur_long: &mut Option) -> Result { let mut had5 = false; - let curr_pic_num = cur_frame_num as i32; + let curr_pic_num = i64::from(cur_frame_num); for m in mmcos { match m.op { 1 => { - let pn = curr_pic_num - (m.difference_of_pic_nums_minus1 as i32 + 1); + let pn = curr_pic_num - (i64::from(m.difference_of_pic_nums_minus1) + 1); for e in &mut self.entries { - if e.mark == RefMark::Short && Self::frame_num_wrap(e, cur_frame_num, max_frame_num) == pn { + if e.mark == RefMark::Short && i64::from(Self::frame_num_wrap(e, cur_frame_num, max_frame_num)) == pn { e.mark = RefMark::Unused; } } @@ -295,14 +297,14 @@ impl Dpb { self.max_long_term_frame_idx.is_some_and(|max| m.long_term_frame_idx <= max), "MMCO long-term index exceeds the configured limit" ); - let pn = curr_pic_num - (m.difference_of_pic_nums_minus1 as i32 + 1); + let pn = curr_pic_num - (i64::from(m.difference_of_pic_nums_minus1) + 1); for e in &mut self.entries { if e.mark == RefMark::Long && e.long_term_frame_idx == m.long_term_frame_idx { e.mark = RefMark::Unused; } } for e in &mut self.entries { - if e.mark == RefMark::Short && Self::frame_num_wrap(e, cur_frame_num, max_frame_num) == pn { + if e.mark == RefMark::Short && i64::from(Self::frame_num_wrap(e, cur_frame_num, max_frame_num)) == pn { e.mark = RefMark::Long; e.long_term_frame_idx = m.long_term_frame_idx; } @@ -461,6 +463,83 @@ mod tests { }) } + fn reference_slice() -> SliceHeader { + SliceHeader { + nal_ref_idc: 1, + nal_unit_type: 1, + idr: false, + first_mb_in_slice: 0, + slice_type_raw: 0, + slice_type: SliceType::P, + pps_id: 0, + colour_plane_id: 0, + frame_num: 0, + field_pic: false, + bottom_field: false, + idr_pic_id: 0, + pic_order_cnt_lsb: 0, + delta_pic_order_cnt_bottom: 0, + delta_pic_order_cnt: [0; 2], + redundant_pic_cnt: 0, + direct_spatial_mv_pred: false, + num_ref_idx_active: [1, 0], + ref_pic_list_mod: [Vec::new(), Vec::new()], + pred_weight_table: None, + no_output_of_prior_pics: false, + long_term_reference: false, + adaptive_ref_pic_marking: false, + mmcos: Vec::new(), + cabac_init_idc: 0, + slice_qp_delta: 0, + sp_for_switch: false, + slice_qs_delta: 0, + disable_deblocking_filter_idc: 0, + slice_alpha_c0_offset_div2: 0, + slice_beta_offset_div2: 0, + slice_group_change_cycle: 0, + header_bits: 0, + } + } + + #[test] + fn large_picture_number_differences_do_not_overflow() { + for op in [1, 3] { + for difference in [i32::MAX as u32, 1 << 31, u32::MAX] { + let mut dpb: Dpb = Dpb::new(); + dpb.max_long_term_frame_idx = Some(0); + let op = Mmco { + op, + difference_of_pic_nums_minus1: difference, + long_term_pic_num: 0, + long_term_frame_idx: 0, + max_long_term_frame_idx_plus1: 0, + }; + assert!(dpb.apply_mmcos(&[op], 5, 16, &mut None).is_ok()); + } + } + } + + #[test] + fn invalid_reference_list_differences_are_rejected() { + use crate::h264::decode::slice::RefPicListMod; + for idc in [0, 1] { + for value in [0, 15] { + let mut sh = reference_slice(); + sh.frame_num = 5; + sh.ref_pic_list_mod[0].push(RefPicListMod { idc, value }); + let dpb: Dpb = Dpb::new(); + assert!(dpb.build_ref_lists(&sh, 0, 16, |entry| entry.frame).is_ok()); + } + for value in [16, i32::MAX as u32, 1 << 31, u32::MAX] { + let mut sh = reference_slice(); + sh.frame_num = 5; + sh.ref_pic_list_mod[0].push(RefPicListMod { idc, value }); + let dpb: Dpb = Dpb::new(); + assert!(dpb.build_ref_lists(&sh, 0, 16, |entry| entry.frame).is_err()); + } + } + } + /// A huge frame_num jump must only materialise the frames the sliding window keeps (#2779). #[test] fn frame_num_gap_materialises_at_most_max_num_ref_frames() { From 6fa8323991b54e7ee62d2d2d9d8f71e719f65a21 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:44:21 +0000 Subject: [PATCH 018/520] video: saturate extreme H.264 picture order values --- crates/video/src/h264/decode/slice.rs | 42 ++++++++++++++++----------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/crates/video/src/h264/decode/slice.rs b/crates/video/src/h264/decode/slice.rs index 75b6c4f8e..61670f059 100644 --- a/crates/video/src/h264/decode/slice.rs +++ b/crates/video/src/h264/decode/slice.rs @@ -462,21 +462,21 @@ impl PocState { let (prev_msb, prev_lsb) = if sh.idr { (0, 0) } else { (self.prev_ref_msb, self.prev_ref_lsb) }; let max_lsb = 1i32 << sps.log2_max_poc_lsb; let lsb = sh.pic_order_cnt_lsb as i32; - let msb = if lsb < prev_lsb && prev_lsb.wrapping_sub(lsb) >= max_lsb / 2 { - prev_msb.wrapping_add(max_lsb) - } else if lsb > prev_lsb && lsb.wrapping_sub(prev_lsb) > max_lsb / 2 { - prev_msb.wrapping_sub(max_lsb) + let msb = if lsb < prev_lsb && prev_lsb.saturating_sub(lsb) >= max_lsb / 2 { + prev_msb.saturating_add(max_lsb) + } else if lsb > prev_lsb && lsb.saturating_sub(prev_lsb) > max_lsb / 2 { + prev_msb.saturating_sub(max_lsb) } else { prev_msb }; - let top = msb.wrapping_add(lsb); - let bottom = top.wrapping_add(sh.delta_pic_order_cnt_bottom); + let top = msb.saturating_add(lsb); + let bottom = top.saturating_add(sh.delta_pic_order_cnt_bottom); Poc { top, bottom, msb, frame_num_offset: 0 } } 1 => { let frame_num_offset = self.frame_num_offset(sh.idr, sh.frame_num, sps); let n = sps.offset_for_ref_frame.len() as i32; - let mut abs_frame_num = if n != 0 { frame_num_offset.wrapping_add(sh.frame_num as i32) } else { 0 }; + let mut abs_frame_num = if n != 0 { frame_num_offset.saturating_add(sh.frame_num as i32) } else { 0 }; if sh.nal_ref_idc == 0 && abs_frame_num > 0 { abs_frame_num -= 1; } @@ -484,17 +484,17 @@ impl PocState { if abs_frame_num > 0 { let cycle_cnt = (abs_frame_num - 1) / n; let in_cycle = (abs_frame_num - 1) % n; - let delta_cycle = sps.offset_for_ref_frame.iter().fold(0i32, |a, &o| a.wrapping_add(o)); - expected = cycle_cnt.wrapping_mul(delta_cycle); + let delta_cycle = sps.offset_for_ref_frame.iter().fold(0i32, |a, &o| a.saturating_add(o)); + expected = cycle_cnt.saturating_mul(delta_cycle); for &o in &sps.offset_for_ref_frame[..=in_cycle as usize] { - expected = expected.wrapping_add(o); + expected = expected.saturating_add(o); } } if sh.nal_ref_idc == 0 { - expected = expected.wrapping_add(sps.offset_for_non_ref_pic); + expected = expected.saturating_add(sps.offset_for_non_ref_pic); } - let top = expected.wrapping_add(sh.delta_pic_order_cnt[0]); - let bottom = top.wrapping_add(sps.offset_for_top_to_bottom_field).wrapping_add(sh.delta_pic_order_cnt[1]); + let top = expected.saturating_add(sh.delta_pic_order_cnt[0]); + let bottom = top.saturating_add(sps.offset_for_top_to_bottom_field).saturating_add(sh.delta_pic_order_cnt[1]); Poc { top, bottom, msb: 0, frame_num_offset } } _ => { @@ -502,9 +502,9 @@ impl PocState { let temp = if sh.idr { 0 } else if sh.nal_ref_idc == 0 { - 2i32.wrapping_mul(frame_num_offset.wrapping_add(sh.frame_num as i32)).wrapping_sub(1) + 2i32.saturating_mul(frame_num_offset.saturating_add(sh.frame_num as i32)).saturating_sub(1) } else { - 2i32.wrapping_mul(frame_num_offset.wrapping_add(sh.frame_num as i32)) + 2i32.saturating_mul(frame_num_offset.saturating_add(sh.frame_num as i32)) }; Poc { top: temp, bottom: temp, msb: 0, frame_num_offset } } @@ -515,7 +515,7 @@ impl PocState { if idr { 0 } else if self.prev_frame_num > frame_num { - self.prev_frame_num_offset.wrapping_add(sps.max_frame_num() as i32) + self.prev_frame_num_offset.saturating_add(sps.max_frame_num() as i32) } else { self.prev_frame_num_offset } @@ -528,7 +528,7 @@ impl PocState { if sh.nal_ref_idc != 0 { if mmco5 { self.prev_ref_msb = 0; - self.prev_ref_lsb = poc.top.wrapping_sub(poc.frame()); + self.prev_ref_lsb = poc.top.saturating_sub(poc.frame()); } else { self.prev_ref_msb = poc.msb; self.prev_ref_lsb = sh.pic_order_cnt_lsb as i32; @@ -624,6 +624,14 @@ mod poc_tests { } } + #[test] + fn extreme_bottom_delta_saturates_instead_of_reversing_picture_order() { + let sps = sps(0); + let sh = header(0, 1, i32::MAX, [0, 0], 1); + let poc = PocState::default().compute(&sh, &sps); + assert_eq!((poc.top, poc.bottom), (1, i32::MAX)); + } + /// Offsets and deltas are se(v) values that a stream may set anywhere in the i32 range. #[test] fn poc_does_not_overflow_on_extreme_deltas() { From 4cca9be2abc7aeb2e3b55c94673f8413b905bf5e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:10:32 +0000 Subject: [PATCH 019/520] video: configure bounded decoder frame limits --- crates/video/src/h264/decode/README.md | 3 +- crates/video/src/h264/decode/decoder.rs | 13 ++++- crates/video/src/h264/decode/params.rs | 33 +++++++++++- crates/video/src/prores/README.md | 7 +++ crates/video/src/prores/decode.rs | 70 ++++++++++++++++++++++++- crates/video/src/prores/mod.rs | 4 +- 6 files changed, 123 insertions(+), 7 deletions(-) diff --git a/crates/video/src/h264/decode/README.md b/crates/video/src/h264/decode/README.md index a73b8f2f4..4c352fd00 100644 --- a/crates/video/src/h264/decode/README.md +++ b/crates/video/src/h264/decode/README.md @@ -99,7 +99,8 @@ A picture is refused when the pictures the decoder would hold do not fit 1 GiB on 32-bit targets): the DPB (the larger of its slots at the new size and what it holds now), the picture being decoded, and what is in flight or waiting in the output queue. Pictures already held count at their own size, and a buffer shared by the DPB and the queue counts once. -The per-picture size cap applies as well. +The default per-picture luma cap is 2^25 samples; applications with a different bounded frame +policy can raise it with `Decoder::set_max_picture_luma_samples`. `configure_avcc` checks every entry of the record before it handles any: the first list holds SPS NAL units and the second PPS NAL units. A record that carries a picture slice is an error and diff --git a/crates/video/src/h264/decode/decoder.rs b/crates/video/src/h264/decode/decoder.rs index 6d5fe3e5d..01a130b35 100644 --- a/crates/video/src/h264/decode/decoder.rs +++ b/crates/video/src/h264/decode/decoder.rs @@ -115,6 +115,8 @@ pub struct Decoder { /// Bytes the picture store may take ([`crate::budget`]). memory_budget: u64, threads: usize, + /// Maximum reconstructed luma samples in one picture. + max_luma_samples: u64, /// Draft mode: skip deblocking of non-reference pictures ([`Decoder::set_draft`]). draft: bool, } @@ -179,6 +181,7 @@ impl Decoder { max_in_flight: threads.max(1) + 2, memory_budget: crate::budget::DECODE_MEMORY_BUDGET, threads: workers, + max_luma_samples: super::params::MAX_LUMA_SAMPLES, draft: false, } } @@ -391,7 +394,7 @@ impl Decoder { // Before the header fields that depend on the SPS: they are validated for the streams // we decode (a 10-bit slice may have a QP below 0), so a stream we cannot decode must // fail as unsupported here, not as invalid there (#626). - sps.check_supported()?; + sps.check_supported_with_limit(self.max_luma_samples)?; found = Some((pps.clone(), sps.clone())); Ok((&**pps, &**sps)) })?; @@ -468,6 +471,14 @@ impl Decoder { self.memory_budget = bytes; } + /// Set the maximum luma sample count allowed in a single decoded picture. + /// + /// The default is 2^25 samples. This is a per-picture geometry guard; the separate + /// memory budget controls aggregate decoded picture storage. + pub fn set_max_picture_luma_samples(&mut self, samples: u64) { + self.max_luma_samples = samples; + } + /// Refuse an SPS whose pictures would not fit the memory budget, before anything is allocated. fn check_budget(&self, sps: &Sps) -> Result<()> { let chroma = sps.chroma_array_type(); diff --git a/crates/video/src/h264/decode/params.rs b/crates/video/src/h264/decode/params.rs index 62ca789eb..b919efb7d 100644 --- a/crates/video/src/h264/decode/params.rs +++ b/crates/video/src/h264/decode/params.rs @@ -6,7 +6,7 @@ use craft_media::bitstream::BitReader; /// Maximum supported reconstructed luma area. 8K UHD fits comfortably while /// hostile SPS dimensions cannot allocate full pictures and decoder work state. -const MAX_LUMA_SAMPLES: u64 = 1 << 25; +pub(super) const MAX_LUMA_SAMPLES: u64 = 1 << 25; /// Scaling matrices after applying fall-back rules, stored in raster order (weightScale). #[derive(Clone, Debug, PartialEq, Eq)] @@ -527,7 +527,11 @@ impl Sps { } pub fn check_supported(&self) -> Result<()> { - if u64::from(self.width()).saturating_mul(u64::from(self.height())) > MAX_LUMA_SAMPLES { + self.check_supported_with_limit(MAX_LUMA_SAMPLES) + } + + pub(super) fn check_supported_with_limit(&self, max_luma_samples: u64) -> Result<()> { + if u64::from(self.width()).saturating_mul(u64::from(self.height())) > max_luma_samples { return unsupported("picture dimensions exceed the decoder memory limit"); } if !self.frame_mbs_only { @@ -744,6 +748,31 @@ mod tests { assert_eq!(sps.pic_order_cnt_type, 2); assert_eq!(sps.crop_rect(), (0, 0, 176, 144)); } + + #[test] + fn caller_can_raise_single_picture_sample_limit() { + let mut w = BitWriter::new(); + w.write_bits(66, 8); + w.write_bits(0xC0, 8); + w.write_bits(30, 8); + w.write_ue(0); + w.write_ue(0); + w.write_ue(2); + w.write_ue(1); + w.write_bit(false); + w.write_ue(511); // 8192 luma samples wide + w.write_ue(511); // 8192 luma samples high + w.write_bit(true); + w.write_bit(true); + w.write_bit(false); + w.write_bit(false); + w.rbsp_trailing(); + + let sps = Sps::parse(&w.finish()).unwrap(); + assert_eq!((sps.width(), sps.height()), (8192, 8192)); + assert!(sps.check_supported().is_err()); + assert!(sps.check_supported_with_limit(1 << 26).is_ok()); + } /// Baseline SPS with the given `ue(v)` overrides for the fields hostile streams can max out. fn hostile_sps(profile: u32, bit_depth: u32, log2_frame_num: u32, w: u32, h: u32, crop: u32) -> Vec { let mut bw = BitWriter::new(); diff --git a/crates/video/src/prores/README.md b/crates/video/src/prores/README.md index e3a9ea989..9831e3282 100644 --- a/crates/video/src/prores/README.md +++ b/crates/video/src/prores/README.md @@ -16,6 +16,8 @@ use craft_video::prores::*; let frame: Frame = decode_frame(&sample)?; // 10-bit 4:2:2 / 12-bit 4:4:4 let f10 = decode_frame_with(&sample, &DecodeOptions { bit_depth: Some(10), threads: true })?; decode_frame_into(&sample, &DecodeOptions::default(), &mut frame_buf)?; // reuses planes +let limits = DecodeLimits { max_pixels: 8192 * 8192, max_frame_bytes: 512 * 1024 * 1024 }; +let bounded = decode_frame_with_limits(&sample, &DecodeOptions::default(), &limits)?; let hdr: FrameHeader = probe(&sample)?; // header only // Encode (progressive). @@ -28,6 +30,11 @@ alpha: Option>, interlace: Interlace, color: ColorInfo { primaries, tra aspect_ratio, frame_rate_code }` holds planar samples with no row padding (`chroma_width()` is `ceil(width/2)` for 4:2:2). The colour codes are H.273 code points straight from the frame header. +`DecodeLimits` bounds both frame area and aggregate decoded plane storage before allocation. +`decode_frame_with` uses an aggregate limit of 300 MiB and does not impose an additional area +limit; use `decode_frame_with_limits` when the application needs a stricter pixel policy or a +different finite memory ceiling. + `Profile` = `Proxy` (`apco`), `Lt` (`apcs`), `Standard` (`apcn`), `Hq` (`apch`), `P4444` (`ap4h`), `P4444Xq` (`ap4x`). The frame itself doesn't say which profile it is (only the container fourcc does), so the decoder doesn't need to know. diff --git a/crates/video/src/prores/decode.rs b/crates/video/src/prores/decode.rs index f4670af60..7125d4641 100644 --- a/crates/video/src/prores/decode.rs +++ b/crates/video/src/prores/decode.rs @@ -11,6 +11,21 @@ use crate::prores::{AlphaType, ChromaFormat, Error, Frame, Interlace, Result}; /// Maximum aggregate decoded plane storage for one ProRes frame (300 MiB; 8K ProRes 4444 with alpha is ~265 MiB). const MAX_FRAME_BYTES: usize = 300 * 1024 * 1024; +/// Resource limits for decoding one ProRes frame. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DecodeLimits { + /// Maximum frame width × height. Checked before any plane allocation. + pub max_pixels: u64, + /// Maximum aggregate output plane storage in bytes. + pub max_frame_bytes: usize, +} + +impl Default for DecodeLimits { + fn default() -> Self { + Self { max_pixels: u64::MAX, max_frame_bytes: MAX_FRAME_BYTES } + } +} + /// Decoder options. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct DecodeOptions { @@ -105,15 +120,28 @@ impl Scratch { /// Decode one ProRes frame. pub fn decode_frame_with(data: &[u8], opts: &DecodeOptions) -> Result { + decode_frame_with_limits(data, opts, &DecodeLimits::default()) +} + +/// Decode one ProRes frame with explicit geometry and aggregate-memory limits. +pub fn decode_frame_with_limits(data: &[u8], opts: &DecodeOptions, limits: &DecodeLimits) -> Result { let mut f = Frame::new(0, 0, ChromaFormat::Yuv422, 10, false)?; - decode_frame_into(data, opts, &mut f)?; + decode_frame_into_with_limits(data, opts, limits, &mut f)?; Ok(f) } /// Decode one ProRes frame into `out`, reusing its plane allocations (the fastest way to decode /// a sequence). On error the contents of `out` are unspecified. pub fn decode_frame_into(data: &[u8], opts: &DecodeOptions, out: &mut Frame) -> Result<()> { + decode_frame_into_with_limits(data, opts, &DecodeLimits::default(), out) +} + +/// Decode one ProRes frame into `out` under explicit resource limits. +pub fn decode_frame_into_with_limits(data: &[u8], opts: &DecodeOptions, limits: &DecodeLimits, out: &mut Frame) -> Result<()> { let (hdr, mut off, frame_size) = FrameHeader::parse(data)?; + if u64::from(hdr.width) * u64::from(hdr.height) > limits.max_pixels { + return Err(Error::Unsupported("frame size")); + } let data = &data[..frame_size]; let depth = opts.bit_depth.unwrap_or(match hdr.chroma { ChromaFormat::Yuv422 => 10, @@ -131,7 +159,7 @@ pub fn decode_frame_into(data: &[u8], opts: &DecodeOptions, out: &mut Frame) -> .checked_add(chroma.checked_mul(2).ok_or(Error::Invalid("chroma plane count overflows"))?) .and_then(|n| if hdr.alpha == AlphaType::None { Some(n) } else { n.checked_add(luma) }) .ok_or(Error::Invalid("frame plane count overflows"))?; - if samples.checked_mul(std::mem::size_of::()).is_none_or(|n| n > MAX_FRAME_BYTES) { + if samples.checked_mul(std::mem::size_of::()).is_none_or(|n| n > limits.max_frame_bytes) { return Err(Error::Invalid("frame dimensions exceed the decoder memory limit")); } let interlaced = hdr.interlace != Interlace::Progressive; @@ -564,6 +592,44 @@ mod alpha_decode_tests { assert_eq!(slice_quantiser(224).unwrap(), qscale(224)); } + #[test] + fn custom_limits_preserve_large_4444_geometry_before_allocating() { + let hdr = FrameHeader { + header_size: 20, + bitstream_version: 1, + encoder_id: *b"test", + width: 8192, + height: 8192, + chroma: ChromaFormat::Yuv444, + interlace: Interlace::Progressive, + aspect_ratio: 0, + frame_rate_code: 0, + color: crate::prores::ColorInfo::default(), + alpha: AlphaType::Bits16, + qmat_luma: [4; 64], + qmat_chroma: [4; 64], + luma_matrix_present: false, + chroma_matrix_present: false, + }; + let header = hdr.write(); + let frame_size = 8 + header.len(); + let mut data = Vec::with_capacity(frame_size); + data.extend_from_slice(&(frame_size as u32).to_be_bytes()); + data.extend_from_slice(&crate::prores::header::FRAME_ID); + data.extend_from_slice(&header); + + let defaults = DecodeLimits { max_pixels: 8192 * 8192, ..DecodeLimits::default() }; + assert!(matches!( + decode_frame_with_limits(&data, &DecodeOptions::default(), &defaults), + Err(Error::Invalid("frame dimensions exceed the decoder memory limit")) + )); + + let soundcraft = DecodeLimits { max_pixels: 8192 * 8192, max_frame_bytes: 512 * 1024 * 1024 }; + assert!(matches!(decode_frame_with_limits(&data, &DecodeOptions::default(), &soundcraft), Err(Error::Truncated))); + let too_few_pixels = DecodeLimits { max_pixels: 8192 * 8192 - 1, ..soundcraft }; + assert!(matches!(decode_frame_with_limits(&data, &DecodeOptions::default(), &too_few_pixels), Err(Error::Unsupported("frame size")))); + } + #[test] fn alpha_decoder_requires_complete_bounded_runs() { let mut out = [0u16; 1]; diff --git a/crates/video/src/prores/mod.rs b/crates/video/src/prores/mod.rs index 4af49f913..5979c19af 100644 --- a/crates/video/src/prores/mod.rs +++ b/crates/video/src/prores/mod.rs @@ -18,7 +18,9 @@ mod encode; pub mod header; mod tables; -pub use decode::{DecodeOptions, decode_frame, decode_frame_into, decode_frame_with, probe}; +pub use decode::{ + DecodeLimits, DecodeOptions, decode_frame, decode_frame_into, decode_frame_into_with_limits, decode_frame_with, decode_frame_with_limits, probe, +}; pub use encode::{Encoder, EncoderConfig}; pub use header::{FrameHeader, PictureHeader}; From 198697cfc8ef5477f9b8954572978a352010c6db Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:18:59 +0000 Subject: [PATCH 020/520] fix(cms): keep clippy compatible with workspace gate --- crates/cms/src/synth.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/cms/src/synth.rs b/crates/cms/src/synth.rs index a59058f08..b1535f78c 100644 --- a/crates/cms/src/synth.rs +++ b/crates/cms/src/synth.rs @@ -111,9 +111,9 @@ impl CmykModel { // Overprints with black: multiplicative in reflectance above a first-surface floor. let floor = 0.006; let mut prim = [[0.0; 3]; 16]; - for m in 0..16 { + for (m, slot) in prim.iter_mut().enumerate() { let p = base[m & 7]; - prim[m] = if m & 8 == 0 { + *slot = if m & 8 == 0 { p } else { std::array::from_fn(|i| { @@ -133,7 +133,7 @@ impl CmykModel { (v + 4.0 * self.tvi[i] * v * (1.0 - v)).clamp(0.0, 1.0) }); let mut s = [0.0; 3]; - for m in 0..16 { + for (m, prim) in self.prim_n.iter().enumerate() { let mut w = 1.0; for (i, ai) in a.iter().enumerate() { w *= if m & (1 << i) != 0 { *ai } else { 1.0 - ai }; @@ -141,7 +141,7 @@ impl CmykModel { if w == 0.0 { continue; } - for (acc, p) in s.iter_mut().zip(&self.prim_n[m]) { + for (acc, p) in s.iter_mut().zip(prim) { *acc += w * p; } } From 92d1bba5dea565afc0cbe15ab4915b594dec6a4f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:28:46 +0000 Subject: [PATCH 021/520] fix(proof): satisfy workspace clippy gate --- crates/proof/src/tr/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/proof/src/tr/mod.rs b/crates/proof/src/tr/mod.rs index 5472fbe21..c728d5872 100644 --- a/crates/proof/src/tr/mod.rs +++ b/crates/proof/src/tr/mod.rs @@ -100,11 +100,7 @@ fn check(d: &TrDict, word: &str, opts: &crate::Options) -> bool { } // Compound words separated by hyphens (e.g., `afyon-karahisar`) - if w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) { - return true; - } - - false + w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) } #[cfg(test)] From 2253894e2f54f8117a7292cf17552eb470e9ecd7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:39:26 +0000 Subject: [PATCH 022/520] fix: satisfy Rust 1.100 workspace clippy --- crates/latex/src/tests.rs | 12 ++++++------ crates/video/src/hevc/decode/filter.rs | 5 +---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/crates/latex/src/tests.rs b/crates/latex/src/tests.rs index 55bde2489..ccf349c80 100644 --- a/crates/latex/src/tests.rs +++ b/crates/latex/src/tests.rs @@ -353,9 +353,9 @@ fn table_borders_and_borderless_round_trip() { let mut doc = Document::new(); let mut t_borderless = Table::new(2, 2, 400.0); t_borderless.props.style = None; - for r in 0..2 { - for c in 0..2 { - t_borderless.rows[r].cells[c].blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; + for row in &mut t_borderless.rows { + for cell in &mut row.cells { + cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; } } doc.body = vec![Arc::new(Block::Table(t_borderless))]; @@ -369,9 +369,9 @@ fn table_borders_and_borderless_round_trip() { let mut doc_bordered = Document::new(); let mut t_bordered = Table::new(2, 2, 400.0); t_bordered.props.style = Some("TableGrid".to_string()); - for r in 0..2 { - for c in 0..2 { - t_bordered.rows[r].cells[c].blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; + for row in &mut t_bordered.rows { + for cell in &mut row.cells { + cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; } } doc_bordered.body = vec![Arc::new(Block::Table(t_bordered))]; diff --git a/crates/video/src/hevc/decode/filter.rs b/crates/video/src/hevc/decode/filter.rs index d5b94161d..69f936bc1 100644 --- a/crates/video/src/hevc/decode/filter.rs +++ b/crates/video/src/hevc/decode/filter.rs @@ -522,10 +522,7 @@ fn sao_edge_ctb( return false; } } - if !pic.pps.loop_filter_across_tiles && pic.layout.tile_id[pic.layout.rs_to_ts[nrs] as usize] != pic.layout.tile_id[cur_ts as usize] { - return false; - } - true + pic.pps.loop_filter_across_tiles || pic.layout.tile_id[pic.layout.rs_to_ts[nrs] as usize] == pic.layout.tile_id[cur_ts as usize] }; // avail[dy][dx]: the CTB at (rx + dx - 1, ry + dy - 1) may be used. let (rx, ry) = (rs % pic.wctb, rs / pic.wctb); From d316f0fc2d9685ca7b8fdfbaa4bbed1eb49ac496 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:02:45 +0000 Subject: [PATCH 023/520] fix(xml): walk deep KRA and ORA trees iteratively --- crates/kra/src/lib.rs | 43 ++++++++++++++++++++++++------ crates/ora/src/lib.rs | 61 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 83 insertions(+), 21 deletions(-) diff --git a/crates/kra/src/lib.rs b/crates/kra/src/lib.rs index 7485b9dd5..12ae3468b 100644 --- a/crates/kra/src/lib.rs +++ b/crates/kra/src/lib.rs @@ -161,6 +161,16 @@ fn children<'a>(el: &'a El, container: &'static str, tag: &'static str) -> impl } fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { + let mut out = node_fields(el, is_mask)?; + if !is_mask { + out.layers = layers(el, count)?; + out.masks = children(el, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; + } + Ok(out) +} + +/// A node's own attributes, without its children. +fn node_fields(el: &El, is_mask: bool) -> Result { let owned = |name: &str| attr(el, name).map(str::to_string); let node_type = attr(el, "nodetype").unwrap_or("").to_string(); // Only the kinds whose pixels are read need sound numbers; a node an application will leave @@ -168,7 +178,7 @@ fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { let read = matches!(node_type.as_str(), "paintlayer" | "grouplayer" | "transparencymask"); let lenient = |v: Result, default: f64| if read { v } else { Ok(v.unwrap_or(default)) }; let (x, y) = if is_mask && !read { (offset(el, "x").unwrap_or(0), offset(el, "y").unwrap_or(0)) } else { (offset(el, "x")?, offset(el, "y")?) }; - let mut out = Node { + let out = Node { node_type, name: owned("name"), visible: attr(el, "visible") != Some("0"), @@ -187,22 +197,39 @@ fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { layers: Vec::new(), masks: Vec::new(), }; - if !is_mask { - out.layers = layers(el, count)?; - out.masks = children(el, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; - } Ok(out) } -/// The `` of `el`. Recursion is bounded by [`MAX_XML_DEPTH`]. +/// The `` of `el`, walked with an explicit stack: groups can nest up to [`MAX_XML_DEPTH`] +/// deep, more than a small thread stack can recurse. fn layers(el: &El, count: &mut usize) -> Result> { + struct Frame<'a> { + node: Node, + rest: std::vec::IntoIter<&'a El>, + } + let mut root = children(el, "layers", "layer").collect::>().into_iter(); + let mut stack: Vec = Vec::new(); let mut out = Vec::new(); - for child in children(el, "layers", "layer") { + loop { + let next = match stack.last_mut() { + Some(frame) => frame.rest.next(), + None => root.next(), + }; + let Some(child) = next else { + let Some(frame) = stack.pop() else { break }; + match stack.last_mut() { + Some(parent) => parent.node.layers.push(frame.node), + None => out.push(frame.node), + } + continue; + }; *count += 1; if *count > MAX_NODES { return Err(invalid(format!("more than {MAX_NODES} layers"))); } - out.push(node(child, false, count)?); + let mut built = node_fields(child, false)?; + built.masks = children(child, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; + stack.push(Frame { node: built, rest: children(child, "layers", "layer").collect::>().into_iter() }); } Ok(out) } diff --git a/crates/ora/src/lib.rs b/crates/ora/src/lib.rs index b2a91cc40..69d494d0d 100644 --- a/crates/ora/src/lib.rs +++ b/crates/ora/src/lib.rs @@ -189,28 +189,63 @@ fn props(el: &El) -> Result { }) } -/// The elements of `stack`. Recursion is bounded by [`MAX_XML_DEPTH`]. +/// The elements of `stack`, walked with an explicit stack: a stack can nest up to +/// [`MAX_XML_DEPTH`] deep, more than a small thread stack can recurse. fn stack(el: &El, count: &mut usize) -> Result> { + struct Frame<'a> { + props: Props, + isolation: Option, + children: Vec, + rest: std::vec::IntoIter<&'a El>, + } + let mut root = el.elements().collect::>().into_iter(); + let mut frames: Vec = Vec::new(); let mut out = Vec::new(); - for child in el.elements() { + loop { + let next = match frames.last_mut() { + Some(frame) => frame.rest.next(), + None => root.next(), + }; + let Some(child) = next else { + let Some(frame) = frames.pop() else { break }; + let node = Node::Stack(Stack { props: frame.props, isolation: frame.isolation, children: frame.children }); + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + continue; + }; *count += 1; if *count > MAX_NODES { return Err(invalid(format!("more than {MAX_NODES} layers"))); } - out.push(match child.name.as_str() { - "layer" => Node::Layer(Layer { - props: props(child)?, - x: int(child, "x")?, - y: int(child, "y")?, - src: child.attr("src").map(|s| s.trim().to_string()), - }), - "stack" => Node::Stack(Stack { + match child.name.as_str() { + "layer" => { + let node = Node::Layer(Layer { + props: props(child)?, + x: int(child, "x")?, + y: int(child, "y")?, + src: child.attr("src").map(|s| s.trim().to_string()), + }); + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + } + "stack" => frames.push(Frame { props: props(child)?, isolation: child.attr("isolation").map(|s| s.trim().to_string()), - children: stack(child, count)?, + children: Vec::new(), + rest: child.elements().collect::>().into_iter(), }), - other => Node::Other { tag: other.to_string(), name: child.attr("name").map(str::to_string) }, - }); + other => { + let node = Node::Other { tag: other.to_string(), name: child.attr("name").map(str::to_string) }; + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + } + } } Ok(out) } From 6ef2334920b94fc91ab9f9fa37428138597c7499 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 23:58:12 +0000 Subject: [PATCH 024/520] plotter: expect registration offset command --- crates/plotter/tests/golden.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/plotter/tests/golden.rs b/crates/plotter/tests/golden.rs index 26aaae554..4d5029010 100644 --- a/crates/plotter/tests/golden.rs +++ b/crates/plotter/tests/golden.rs @@ -135,7 +135,7 @@ fn silhouette_registration_marks() { let p = silhouette::encode(&pro, &j).unwrap(); let (head, rest) = p.split_at_registration(); let text = |c: &[Command]| c.iter().map(Command::to_ascii).collect::>().join(" "); - assert!(text(head).ends_with(" FC18,1,1 TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB55,1 TB123,4600,3600,200,100")); + assert!(text(head).ends_with(" FC18,1,1 TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB54,0,0 TB55,1 TB123,4600,3600,200,100")); // The design is shifted so the first mark is the origin. assert_eq!(text(rest), "M100,100 D100,300 M100,0 SO0"); assert_eq!(p.registration_wait, Some(head.len())); @@ -148,7 +148,7 @@ fn silhouette_registration_marks() { reg.origin_right_mm = 0.0; assert!(ascii(®).ends_with(" TB124,4600,3600,0,0")); reg.search = false; - assert_eq!(ascii(®), "TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB55,1 TB23,4600,3600"); + assert_eq!(ascii(®), "TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB54,0,0 TB55,1 TB23,4600,3600"); } #[test] From c7fb7544c2cada91c7bbcfd6ddbca8ad9da77f7d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:23:04 +0000 Subject: [PATCH 025/520] craft-stencilcraft-format: add native JSON schema --- Cargo.lock | 9 + Cargo.toml | 1 + README.md | 1 + crates/stencilcraft-format/Cargo.toml | 16 ++ crates/stencilcraft-format/README.md | 23 +++ crates/stencilcraft-format/src/lib.rs | 229 ++++++++++++++++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 crates/stencilcraft-format/Cargo.toml create mode 100644 crates/stencilcraft-format/README.md create mode 100644 crates/stencilcraft-format/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index cf0e0bd47..d04aa2f0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2188,6 +2188,15 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-stencilcraft-format" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-stream-codecs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 629bab64c..cef7b2a21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,6 +110,7 @@ craft-sketch = { path = "crates/sketch" } craft-settings = { path = "crates/settings" } craft-silhouette-studio = { path = "crates/silhouette-studio" } craft-svg = { path = "crates/svg" } +craft-stencilcraft-format = { path = "crates/stencilcraft-format" } craft-svg-paths = { path = "crates/svg-paths" } craft-stream-codecs = { path = "crates/stream-codecs" } craft-text = { path = "crates/text" } diff --git a/README.md b/README.md index a45fd5870..c79fa2aff 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-sheet`](crates/sheet/README.md): spreadsheet vocabulary shared by `craft-xls`, `craft-xlsx` and `craft-ods`: cell positions and A1 text, cell values and error values, sheet-name quoting, Excel's built-in number formats and indexed colour palette. Not a file format; no dependencies. - [`craft-sf2`](crates/sf2/README.md): SoundFont 2 (`.sf2`) reader: the sample pool and, per preset, the key and velocity regions a note sounds, with tuning, loop points, pan, level and volume envelope resolved from the preset and instrument zones. Bounded on hostile input. From LoopCraft. - [`craft-silhouette-studio`](crates/silhouette-studio/src/lib.rs): bounded, fail-closed reader for Silhouette Studio `.studio` and `.studio3` documents: outlines, line colours and page size in millimetres. Protected designs are refused. +- [`craft-stencilcraft-format`](crates/stencilcraft-format/README.md): the versioned `.stencilcraft` JSON wire schema and envelope: required members, version 1 defaults, optional fields, and header checks. The app retains its document model and domain validation. - [`craft-sketch`](crates/sketch/README.md): clean-room reader for the `.sketch` container (Sketch 48 and newer): the ZIP archive, the version cut, `document.json`, the pages in document order and images, as the JSON the file holds. Bounded on hostile input. Carved from FigCraft. - [`craft-svg`](crates/svg/src/lib.rs): SVG. `sniff` recognises SVG and SVGZ data; `svgz` unpacks SVGZ within a byte limit, decodes UTF-8, UTF-16 and ISO 8859-1, and compresses; `guard` bounds diff --git a/crates/stencilcraft-format/Cargo.toml b/crates/stencilcraft-format/Cargo.toml new file mode 100644 index 000000000..fec3ad3f9 --- /dev/null +++ b/crates/stencilcraft-format/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "craft-stencilcraft-format" +description = "Reader and writer for the versioned StencilCraft JSON document format" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/stencilcraft-format/README.md b/crates/stencilcraft-format/README.md new file mode 100644 index 000000000..2391d3c56 --- /dev/null +++ b/crates/stencilcraft-format/README.md @@ -0,0 +1,23 @@ +# craft-stencilcraft-format + +Reader and writer for the versioned `.stencilcraft` JSON file. The JSON header is +`{"stencilcraft":{"name":"stencilcraft","version":1}}`; the document fields share the +top-level object with the header. `DocumentFields` owns field names, flattening, required fields, +version 1 defaults, and omitted optional members. Its values remain JSON so the consuming app +owns the domain model, geometry validation, and field-to-model mapping. + +The crate does not open files or set a file-size policy. Callers perform bounded file I/O before +calling `File::read`. + +```toml +craft-stencilcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "" } +``` + +`File::read` parses JSON, validates the name and exact supported version, reads `DocumentFields`, +and ignores unknown members for version 1 compatibility. `File::new` and `to_json_pretty` write +the current header and flattened fields. Absent `next-shape-id` defaults to 1 and absent `images` +defaults to an empty list, matching early version 1 files. + +`cargo test -p craft-stencilcraft-format` covers round-trip wire shape, version 1 defaults, +unknown members, wrong and missing headers, unsupported versions, malformed JSON, and missing +required fields. diff --git a/crates/stencilcraft-format/src/lib.rs b/crates/stencilcraft-format/src/lib.rs new file mode 100644 index 000000000..9f1e2a2c4 --- /dev/null +++ b/crates/stencilcraft-format/src/lib.rs @@ -0,0 +1,229 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! Reader and writer for the versioned JSON document format. +//! +//! The file stores a format header under `stencilcraft` and the document's wire fields at the +//! top level: +//! +//! ```json +//! { +//! "stencilcraft": { "name": "stencilcraft", "version": 1 }, +//! "board": { "width_mm": 100.0, "height_mm": 150.0 }, +//! "layers": [] +//! } +//! ``` +//! +//! [`DocumentFields`] owns the JSON field names, required and defaulted fields, and the +//! flattening rule. Its values are JSON because the consuming app owns the document model, +//! domain validation, and conversion between that model and the wire fields. The crate does not +//! open files or set a file-size policy; callers handle bounded file I/O. + +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +/// The extension, without the leading dot. +pub const EXTENSION: &str = "stencilcraft"; +/// The format name stored in the header. +pub const FORMAT_NAME: &str = "stencilcraft"; +/// The newest document format version this crate reads and writes. +pub const FORMAT_VERSION: u64 = 1; +/// The top-level member containing the format header. +pub const HEADER_KEY: &str = "stencilcraft"; + +/// Wire fields in the flattened native document object. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct DocumentFields { + /// Required millimetre artboard dimensions. + pub board: Value, + /// Required kerf settings. + pub kerf: Value, + /// Required drawing layers. + pub layers: Value, + /// Next document object id; absent in early version 1 files, where it defaults to 1. + #[serde(rename = "next-shape-id", default = "default_next_shape_id")] + pub next_shape_id: u64, + /// Embedded raster images; absent in early version 1 files, where it defaults to empty. + #[serde(default)] + pub images: Vec, + /// Optional canvas page setup, omitted when absent. + #[serde(rename = "board-setup", default, skip_serializing_if = "Option::is_none")] + pub board_setup: Option, + /// Optional pages, omitted when absent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub pages: Option, + /// Optional guides, omitted when absent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub guides: Option, + /// Optional grid settings, omitted when absent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub grid: Option, + /// Optional snap settings, omitted when absent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub snap: Option, + /// Optional groups, omitted when absent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub groups: Option, + /// Optional user metadata, omitted when absent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub meta: Option, +} + +fn default_next_shape_id() -> u64 { + 1 +} + +/// A native document and its flattened wire fields. +#[derive(Debug, Clone, PartialEq)] +pub struct File { + /// The fields after the header have been parsed, with app-domain objects still encoded as + /// JSON values. + pub document: DocumentFields, +} + +impl File { + /// Builds a native document from its wire fields. + pub fn new(document: DocumentFields) -> Self { + Self { document } + } + + /// Parses JSON, validates the format header and version, then reads the wire fields. + pub fn read(text: &str) -> Result { + let value: Value = serde_json::from_str(text).map_err(Error::Json)?; + let header = value.get(HEADER_KEY).and_then(Value::as_object).ok_or(Error::NotStencilCraft)?; + if header.get("name").and_then(Value::as_str) != Some(FORMAT_NAME) { + return Err(Error::NotStencilCraft); + } + let Some(version) = header.get("version").and_then(Value::as_u64) else { + return Err(Error::NotStencilCraft); + }; + if version != FORMAT_VERSION { + return Err(Error::Version { found: version, supported: FORMAT_VERSION }); + } + let raw: RawFile = serde_json::from_value(value).map_err(|source| Error::Document { source })?; + Ok(Self { document: raw.document }) + } + + /// Writes the current format header and flattened wire fields as indented JSON. + pub fn to_json_pretty(&self) -> Result { + let raw = RawFileRef { header: HeaderRef { name: FORMAT_NAME, version: FORMAT_VERSION }, document: &self.document }; + let value = serde_json::to_value(raw).map_err(Error::Json)?; + serde_json::to_string_pretty(&value).map_err(Error::Json) + } +} + +#[derive(Debug, Serialize, Deserialize)] +struct Header { + name: String, + version: u64, +} + +#[derive(Debug, Deserialize)] +struct RawFile { + #[serde(rename = "stencilcraft")] + _header: Header, + #[serde(flatten)] + document: DocumentFields, +} + +#[derive(Serialize)] +struct HeaderRef<'a> { + name: &'a str, + version: u64, +} + +#[derive(Serialize)] +struct RawFileRef<'a> { + #[serde(rename = "stencilcraft")] + header: HeaderRef<'a>, + #[serde(flatten)] + document: &'a DocumentFields, +} + +/// A file was malformed, belongs to another format, or uses an unsupported version. +#[derive(Debug, thiserror::Error)] +#[non_exhaustive] +pub enum Error { + /// The input is not valid JSON, or the file could not be serialized. + #[error(transparent)] + Json(#[from] serde_json::Error), + /// The input has no valid StencilCraft format header. + #[error("not a stencilcraft file")] + NotStencilCraft, + /// The input names a version other than [`FORMAT_VERSION`]. + #[error("format version {found}, this build reads {supported}")] + Version { found: u64, supported: u64 }, + /// The JSON envelope is valid, but a required or typed wire field is malformed. + #[error("{source}")] + Document { source: serde_json::Error }, +} + +#[cfg(test)] +mod tests { + use serde_json::{Value, json}; + + use super::{DocumentFields, Error, FORMAT_VERSION, File}; + + fn fields() -> DocumentFields { + DocumentFields { + board: json!({"width_mm": 100.0, "height_mm": 150.0}), + kerf: json!({"width_mm": 0.0, "apply_to_exports": false}), + layers: json!([]), + next_shape_id: 1, + images: Vec::new(), + board_setup: None, + pages: None, + guides: None, + grid: None, + snap: None, + groups: None, + meta: None, + } + } + + #[test] + fn writes_flattened_schema_with_the_current_header_and_reads_it() -> Result<(), Error> { + let file = File::new(fields()); + let text = file.to_json_pretty()?; + assert!(text.starts_with("{\n \"board\":")); + let value: Value = serde_json::from_str(&text)?; + assert_eq!(value["stencilcraft"]["name"], "stencilcraft"); + assert_eq!(value["stencilcraft"]["version"], FORMAT_VERSION); + assert_eq!(value["board"], file.document.board); + assert_eq!(value["layers"], file.document.layers); + assert!(value.get("board-setup").is_none()); + assert_eq!(File::read(&text)?, file); + Ok(()) + } + + #[test] + fn reads_version_one_defaults_and_ignores_unknown_fields() -> Result<(), Error> { + let text = r#"{"stencilcraft":{"name":"stencilcraft","version":1},"board":{},"kerf":{},"layers":[],"future-field":true}"#; + let file = File::read(text)?; + assert_eq!(file.document.next_shape_id, 1); + assert!(file.document.images.is_empty()); + Ok(()) + } + + #[test] + fn refuses_missing_or_mismatched_headers() { + for text in [ + r#"{"board":{},"kerf":{},"layers":[]}"#, + r#"{"stencilcraft":{"name":"other","version":1}}"#, + r#"{"stencilcraft":{"name":"stencilcraft","version":"1"}}"#, + ] { + assert!(matches!(File::read(text), Err(Error::NotStencilCraft))); + } + } + + #[test] + fn refuses_versions_other_than_the_current_version() { + assert!(matches!(File::read(r#"{"stencilcraft":{"name":"stencilcraft","version":2}}"#), Err(Error::Version { found: 2, supported: 1 }))); + } + + #[test] + fn separates_json_errors_from_wire_schema_errors() { + assert!(matches!(File::read("{"), Err(Error::Json(_)))); + assert!(matches!(File::read(r#"{"stencilcraft":{"name":"stencilcraft","version":1}}"#), Err(Error::Document { .. }))); + } +} From d31b04810b8b8626cec650ac6424d7cc37bace0f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:28:06 +0000 Subject: [PATCH 026/520] Fix current workspace clippy diagnostics --- crates/cms/src/synth.rs | 8 ++++---- crates/proof/src/tr/mod.rs | 6 +----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/crates/cms/src/synth.rs b/crates/cms/src/synth.rs index a59058f08..b1535f78c 100644 --- a/crates/cms/src/synth.rs +++ b/crates/cms/src/synth.rs @@ -111,9 +111,9 @@ impl CmykModel { // Overprints with black: multiplicative in reflectance above a first-surface floor. let floor = 0.006; let mut prim = [[0.0; 3]; 16]; - for m in 0..16 { + for (m, slot) in prim.iter_mut().enumerate() { let p = base[m & 7]; - prim[m] = if m & 8 == 0 { + *slot = if m & 8 == 0 { p } else { std::array::from_fn(|i| { @@ -133,7 +133,7 @@ impl CmykModel { (v + 4.0 * self.tvi[i] * v * (1.0 - v)).clamp(0.0, 1.0) }); let mut s = [0.0; 3]; - for m in 0..16 { + for (m, prim) in self.prim_n.iter().enumerate() { let mut w = 1.0; for (i, ai) in a.iter().enumerate() { w *= if m & (1 << i) != 0 { *ai } else { 1.0 - ai }; @@ -141,7 +141,7 @@ impl CmykModel { if w == 0.0 { continue; } - for (acc, p) in s.iter_mut().zip(&self.prim_n[m]) { + for (acc, p) in s.iter_mut().zip(prim) { *acc += w * p; } } diff --git a/crates/proof/src/tr/mod.rs b/crates/proof/src/tr/mod.rs index 5472fbe21..c728d5872 100644 --- a/crates/proof/src/tr/mod.rs +++ b/crates/proof/src/tr/mod.rs @@ -100,11 +100,7 @@ fn check(d: &TrDict, word: &str, opts: &crate::Options) -> bool { } // Compound words separated by hyphens (e.g., `afyon-karahisar`) - if w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) { - return true; - } - - false + w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) } #[cfg(test)] From 7a639e04a777dd96ce85683084c2cfcc08a6fecf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:35:01 +0000 Subject: [PATCH 027/520] Fix workspace clippy loop in LaTeX tests --- crates/latex/src/tests.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/latex/src/tests.rs b/crates/latex/src/tests.rs index 55bde2489..ccf349c80 100644 --- a/crates/latex/src/tests.rs +++ b/crates/latex/src/tests.rs @@ -353,9 +353,9 @@ fn table_borders_and_borderless_round_trip() { let mut doc = Document::new(); let mut t_borderless = Table::new(2, 2, 400.0); t_borderless.props.style = None; - for r in 0..2 { - for c in 0..2 { - t_borderless.rows[r].cells[c].blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; + for row in &mut t_borderless.rows { + for cell in &mut row.cells { + cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; } } doc.body = vec![Arc::new(Block::Table(t_borderless))]; @@ -369,9 +369,9 @@ fn table_borders_and_borderless_round_trip() { let mut doc_bordered = Document::new(); let mut t_bordered = Table::new(2, 2, 400.0); t_bordered.props.style = Some("TableGrid".to_string()); - for r in 0..2 { - for c in 0..2 { - t_bordered.rows[r].cells[c].blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; + for row in &mut t_bordered.rows { + for cell in &mut row.cells { + cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; } } doc_bordered.body = vec![Arc::new(Block::Table(t_bordered))]; From 1b53814a388ee7df0517a1522eb1143d5165c9a1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:37:09 +0000 Subject: [PATCH 028/520] Apply verified workspace format fixes --- crates/kra/src/lib.rs | 43 ++++++++++++++---- crates/ora/src/lib.rs | 61 ++++++++++++++++++++------ crates/plotter/tests/golden.rs | 4 +- crates/video/src/hevc/decode/filter.rs | 5 +-- 4 files changed, 86 insertions(+), 27 deletions(-) diff --git a/crates/kra/src/lib.rs b/crates/kra/src/lib.rs index 7485b9dd5..12ae3468b 100644 --- a/crates/kra/src/lib.rs +++ b/crates/kra/src/lib.rs @@ -161,6 +161,16 @@ fn children<'a>(el: &'a El, container: &'static str, tag: &'static str) -> impl } fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { + let mut out = node_fields(el, is_mask)?; + if !is_mask { + out.layers = layers(el, count)?; + out.masks = children(el, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; + } + Ok(out) +} + +/// A node's own attributes, without its children. +fn node_fields(el: &El, is_mask: bool) -> Result { let owned = |name: &str| attr(el, name).map(str::to_string); let node_type = attr(el, "nodetype").unwrap_or("").to_string(); // Only the kinds whose pixels are read need sound numbers; a node an application will leave @@ -168,7 +178,7 @@ fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { let read = matches!(node_type.as_str(), "paintlayer" | "grouplayer" | "transparencymask"); let lenient = |v: Result, default: f64| if read { v } else { Ok(v.unwrap_or(default)) }; let (x, y) = if is_mask && !read { (offset(el, "x").unwrap_or(0), offset(el, "y").unwrap_or(0)) } else { (offset(el, "x")?, offset(el, "y")?) }; - let mut out = Node { + let out = Node { node_type, name: owned("name"), visible: attr(el, "visible") != Some("0"), @@ -187,22 +197,39 @@ fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { layers: Vec::new(), masks: Vec::new(), }; - if !is_mask { - out.layers = layers(el, count)?; - out.masks = children(el, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; - } Ok(out) } -/// The `` of `el`. Recursion is bounded by [`MAX_XML_DEPTH`]. +/// The `` of `el`, walked with an explicit stack: groups can nest up to [`MAX_XML_DEPTH`] +/// deep, more than a small thread stack can recurse. fn layers(el: &El, count: &mut usize) -> Result> { + struct Frame<'a> { + node: Node, + rest: std::vec::IntoIter<&'a El>, + } + let mut root = children(el, "layers", "layer").collect::>().into_iter(); + let mut stack: Vec = Vec::new(); let mut out = Vec::new(); - for child in children(el, "layers", "layer") { + loop { + let next = match stack.last_mut() { + Some(frame) => frame.rest.next(), + None => root.next(), + }; + let Some(child) = next else { + let Some(frame) = stack.pop() else { break }; + match stack.last_mut() { + Some(parent) => parent.node.layers.push(frame.node), + None => out.push(frame.node), + } + continue; + }; *count += 1; if *count > MAX_NODES { return Err(invalid(format!("more than {MAX_NODES} layers"))); } - out.push(node(child, false, count)?); + let mut built = node_fields(child, false)?; + built.masks = children(child, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; + stack.push(Frame { node: built, rest: children(child, "layers", "layer").collect::>().into_iter() }); } Ok(out) } diff --git a/crates/ora/src/lib.rs b/crates/ora/src/lib.rs index b2a91cc40..69d494d0d 100644 --- a/crates/ora/src/lib.rs +++ b/crates/ora/src/lib.rs @@ -189,28 +189,63 @@ fn props(el: &El) -> Result { }) } -/// The elements of `stack`. Recursion is bounded by [`MAX_XML_DEPTH`]. +/// The elements of `stack`, walked with an explicit stack: a stack can nest up to +/// [`MAX_XML_DEPTH`] deep, more than a small thread stack can recurse. fn stack(el: &El, count: &mut usize) -> Result> { + struct Frame<'a> { + props: Props, + isolation: Option, + children: Vec, + rest: std::vec::IntoIter<&'a El>, + } + let mut root = el.elements().collect::>().into_iter(); + let mut frames: Vec = Vec::new(); let mut out = Vec::new(); - for child in el.elements() { + loop { + let next = match frames.last_mut() { + Some(frame) => frame.rest.next(), + None => root.next(), + }; + let Some(child) = next else { + let Some(frame) = frames.pop() else { break }; + let node = Node::Stack(Stack { props: frame.props, isolation: frame.isolation, children: frame.children }); + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + continue; + }; *count += 1; if *count > MAX_NODES { return Err(invalid(format!("more than {MAX_NODES} layers"))); } - out.push(match child.name.as_str() { - "layer" => Node::Layer(Layer { - props: props(child)?, - x: int(child, "x")?, - y: int(child, "y")?, - src: child.attr("src").map(|s| s.trim().to_string()), - }), - "stack" => Node::Stack(Stack { + match child.name.as_str() { + "layer" => { + let node = Node::Layer(Layer { + props: props(child)?, + x: int(child, "x")?, + y: int(child, "y")?, + src: child.attr("src").map(|s| s.trim().to_string()), + }); + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + } + "stack" => frames.push(Frame { props: props(child)?, isolation: child.attr("isolation").map(|s| s.trim().to_string()), - children: stack(child, count)?, + children: Vec::new(), + rest: child.elements().collect::>().into_iter(), }), - other => Node::Other { tag: other.to_string(), name: child.attr("name").map(str::to_string) }, - }); + other => { + let node = Node::Other { tag: other.to_string(), name: child.attr("name").map(str::to_string) }; + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + } + } } Ok(out) } diff --git a/crates/plotter/tests/golden.rs b/crates/plotter/tests/golden.rs index 26aaae554..4d5029010 100644 --- a/crates/plotter/tests/golden.rs +++ b/crates/plotter/tests/golden.rs @@ -135,7 +135,7 @@ fn silhouette_registration_marks() { let p = silhouette::encode(&pro, &j).unwrap(); let (head, rest) = p.split_at_registration(); let text = |c: &[Command]| c.iter().map(Command::to_ascii).collect::>().join(" "); - assert!(text(head).ends_with(" FC18,1,1 TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB55,1 TB123,4600,3600,200,100")); + assert!(text(head).ends_with(" FC18,1,1 TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB54,0,0 TB55,1 TB123,4600,3600,200,100")); // The design is shifted so the first mark is the origin. assert_eq!(text(rest), "M100,100 D100,300 M100,0 SO0"); assert_eq!(p.registration_wait, Some(head.len())); @@ -148,7 +148,7 @@ fn silhouette_registration_marks() { reg.origin_right_mm = 0.0; assert!(ascii(®).ends_with(" TB124,4600,3600,0,0")); reg.search = false; - assert_eq!(ascii(®), "TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB55,1 TB23,4600,3600"); + assert_eq!(ascii(®), "TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB54,0,0 TB55,1 TB23,4600,3600"); } #[test] diff --git a/crates/video/src/hevc/decode/filter.rs b/crates/video/src/hevc/decode/filter.rs index d5b94161d..69f936bc1 100644 --- a/crates/video/src/hevc/decode/filter.rs +++ b/crates/video/src/hevc/decode/filter.rs @@ -522,10 +522,7 @@ fn sao_edge_ctb( return false; } } - if !pic.pps.loop_filter_across_tiles && pic.layout.tile_id[pic.layout.rs_to_ts[nrs] as usize] != pic.layout.tile_id[cur_ts as usize] { - return false; - } - true + pic.pps.loop_filter_across_tiles || pic.layout.tile_id[pic.layout.rs_to_ts[nrs] as usize] == pic.layout.tile_id[cur_ts as usize] }; // avail[dy][dx]: the CTB at (rx + dx - 1, ry + dy - 1) may be used. let (rx, ry) = (rs % pic.wctb, rs / pic.wctb); From 5056c13ef7a2c66711e81baac7e591b4473209fb Mon Sep 17 00:00:00 2001 From: Nicolas Bohn Date: Fri, 13 Nov 2026 03:49:37 +0100 Subject: [PATCH 029/520] fix(dxf): split binary hex payloads into chunks of at most 127 bytes (#1147) (#1148) --- crates/dxf/src/lib.rs | 6 +++--- crates/dxf/tests/binary_write.rs | 5 ++--- crates/dxf/tests/hex_chunks.rs | 3 --- crates/dxf/tests/roundtrip.rs | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index c140ee513..0109e9040 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -768,7 +768,7 @@ fn ascii(tags: &[Tag], strings: Strings) -> Vec { /// Write tags as binary DXF (DXF Reference, "Binary DXF Files", R13+ layout): the sentinel, /// then per tag a 2-byte little-endian group code and the value in the binary form of the code's /// [`Kind`]: a zero-terminated string, an 8-byte real, a 2-, 4- or 8-byte integer, a 1-byte -/// boolean, or a binary chunk (one length byte, then up to 255 bytes; longer data is split into +/// boolean, or a binary chunk (one length byte, then up to 127 bytes; longer data is split into /// several groups of the same code). Strings are UTF-8 when `utf8` (R2007+), otherwise escaped as /// in [`write_ascii`]. A tag's value is converted to its code's kind, so a number written as text /// is stored as a number; a code outside the 16-bit range is skipped. @@ -822,8 +822,8 @@ fn binary(tags: &[Tag], strings: Strings) -> Vec { } Kind::Hex => { let bytes = hex_bytes(&t.value.as_str()); - // An empty chunk is still one group, as in ASCII. - let mut chunks: Vec<&[u8]> = bytes.chunks(255).collect(); + // At most 127 bytes per group, as in ASCII; an empty chunk is still one group. + let mut chunks: Vec<&[u8]> = bytes.chunks(127).collect(); if chunks.is_empty() { chunks.push(&[]); } diff --git a/crates/dxf/tests/binary_write.rs b/crates/dxf/tests/binary_write.rs index fb2d08210..4ecaf1709 100644 --- a/crates/dxf/tests/binary_write.rs +++ b/crates/dxf/tests/binary_write.rs @@ -44,9 +44,8 @@ fn binary_converts_values_to_their_codes_kind() { let back = parse(&write_binary(&tags, true)).unwrap(); assert_eq!(back[0], Tag::i(70, 12)); assert_eq!(back[1], Tag::i(62, i64::from(i16::MAX))); - assert_eq!(back[2].code, 310); - assert_eq!(back[3].code, 310, "300 bytes are split into chunks of at most 255"); - assert_eq!(back[2].str().len() + back[3].str().len(), 600); + let sizes: Vec = back.iter().filter(|t| t.code == 310).map(|t| t.str().len()).collect(); + assert_eq!(sizes, vec![254, 254, 92], "300 bytes are split into chunks of at most 127 (254 hex digits)"); } #[test] diff --git a/crates/dxf/tests/hex_chunks.rs b/crates/dxf/tests/hex_chunks.rs index 61100b082..80e9e9193 100644 --- a/crates/dxf/tests/hex_chunks.rs +++ b/crates/dxf/tests/hex_chunks.rs @@ -2,7 +2,6 @@ //! 1004 specify at most 127 payload bytes per chunk, even though a length byte represents 255. //! Inspect writer bytes without our parser; accept any compliant partition preserving payload, //! code and following-record framing. Oversized binary read fixtures test tolerance only. -//! Pending writer regressions extend the chunk-size finding in issue #1025. //! Public DXF Reference, Group Codes in Numerical Order and Binary DXF Files: //! //! @@ -100,7 +99,6 @@ fn binary_reader_tolerates_the_full_length_byte_domain() { } #[test] -#[ignore = "Confirmed ASCII DXF writer nonconformance: hex payloads above 127 bytes are emitted as one oversized chunk"] fn ascii_writer_splits_large_payloads_into_conforming_chunks() { for code in (310..=319).chain([1004]) { for length in [128, 254, 255, 256, 1024] { @@ -112,7 +110,6 @@ fn ascii_writer_splits_large_payloads_into_conforming_chunks() { } #[test] -#[ignore = "Confirmed binary DXF writer nonconformance: 255-byte partitioning exceeds the documented 127-byte group limit"] fn binary_writer_splits_large_payloads_into_conforming_chunks() { for code in (310..=319).chain([1004]) { for length in [128, 254, 255, 256, 1024] { diff --git a/crates/dxf/tests/roundtrip.rs b/crates/dxf/tests/roundtrip.rs index 87f1e812c..362bc725f 100644 --- a/crates/dxf/tests/roundtrip.rs +++ b/crates/dxf/tests/roundtrip.rs @@ -120,7 +120,7 @@ fn hex_chunks_keep_bytes_and_order() { // DXF groups 310–319 and 1004 permit 127 payload bytes per chunk. A binary length // byte can represent more, but that is reader tolerance, not writer conformance. // Larger payloads need chunk-size/reassembly checks rather than exact tag equality; - // see hex_chunks.rs for the independent wire checks and pending writer regressions. + // see hex_chunks.rs for the independent wire checks. for length in [0, 1, 2, 126, 127] { let hex: String = (0..length).fold(String::new(), |mut out, i| { use std::fmt::Write as _; From 94e65a460676cf11b7e080cbd207813a59e83263 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:06:38 +0000 Subject: [PATCH 030/520] ci: run workspace checks with shared gate and test inputs --- .gitattributes | 8 +++ .github/workflows/workspace.yml | 118 ++++++++++++++++---------------- 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/.gitattributes b/.gitattributes index 5d79390a2..fa2751233 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,10 @@ +# Font-build test fixtures are compared byte for byte: a checkout must not convert them. +crates/fonts-build/tests/fixture/** -text + +# The pinned font index is hashed and compiled in as committed: a checkout must not convert its +# line endings, or `Index::bundled` fails its checksum on Windows. +crates/font-store/index/sources.json -text +crates/font-store/index/indexes/*.gz binary + # Copies of the vendored krilla source diff, one per patch (see vendor/krilla/README.craft.md). vendor/krilla/patches/*.patch linguist-generated=true diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 4002900d9..1f66236e2 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -12,64 +12,66 @@ permissions: jobs: native: - name: Workspace (${{ matrix.os }}) - timeout-minutes: 30 - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, windows-latest, macos-latest] - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v4 - - name: Install software Vulkan renderer - if: runner.os == 'Linux' - run: | - sudo apt-get update - sudo apt-get install -y mesa-vulkan-drivers libvulkan1 - echo 'WGPU_BACKEND=vulkan' >> "$GITHUB_ENV" - - uses: dtolnay/rust-toolchain@stable - with: - components: rustfmt, clippy - - uses: Swatinem/rust-cache@v2 - with: - key: workspace - - name: Prepare the Windows test driver - if: runner.os == 'Windows' - shell: powershell - run: | - $ErrorActionPreference = 'Stop' - Start-Service Spooler - Add-PrinterDriver -Name 'Generic / Text Only' - - run: cargo fmt --check - - run: cargo clippy --workspace --all-targets --locked -- -D warnings - - run: cargo clippy --workspace --all-targets --all-features --locked -- -D warnings - - run: cargo test --workspace --locked - env: - CRAFT_PRINT_REQUIRE_QUEUE: ${{ runner.os == 'Windows' && '1' || '0' }} - - run: cargo test --workspace --all-features --locked - env: - CRAFT_PRINT_REQUIRE_QUEUE: ${{ runner.os == 'Windows' && '1' || '0' }} + name: Workspace + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + os: '["ubuntu-latest","windows-latest","macos-latest"]' + timeout-minutes: 60 + tools: "" + packages: mesa-vulkan-drivers libvulkan1 + craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 + linux-env: WGPU_BACKEND=vulkan + windows-env: CRAFT_PRINT_REQUIRE_QUEUE=1 + env: CRAFT_PRINT_REQUIRE_QUEUE=0 + setup-command: | + set -euo pipefail + corpus_fetcher=.test-inputs/photocraft + mkdir -p "$corpus_fetcher" + git -C "$corpus_fetcher" init + git -C "$corpus_fetcher" remote add origin https://github.com/storytold/PhotoCraft.git + git -C "$corpus_fetcher" fetch --quiet --depth=1 origin 74330157cb601573ae4523a95bb9132822fc3918 + git -C "$corpus_fetcher" checkout --quiet --detach FETCH_HEAD + (cd "$corpus_fetcher" && cargo xtask corpus --psd --psd-tools --atn) + python scripts/test-fetch-pdf-test-fonts.py + python scripts/fetch-pdf-test-fonts.py "$RUNNER_TEMP/craft-pdf-test-fonts" + printf 'CRAFT_PSD_CORPUS=%s\n' "$GITHUB_WORKSPACE/$corpus_fetcher/corpus" >> "$GITHUB_ENV" + printf 'CRAFT_PDF_TEST_FONT_DIR=%s\n' "$RUNNER_TEMP/craft-pdf-test-fonts" >> "$GITHUB_ENV" + command: | + cargo fmt --check + cargo clippy --workspace --all-targets --locked -- -D warnings + cargo clippy --workspace --all-targets --all-features --locked -- -D warnings + cargo test --workspace --locked + cargo test --workspace --all-features --locked + windows-command: | + powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"' + cargo fmt --check + cargo clippy --workspace --all-targets --locked -- -D warnings + cargo clippy --workspace --all-targets --all-features --locked -- -D warnings + cargo test --workspace --locked + cargo test --workspace --all-features --locked + msrv: - name: Rust 1.95 (Linux libraries) - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@1.95.0 - - uses: Swatinem/rust-cache@v2 - with: - key: workspace-msrv - - run: cargo check --workspace --lib --all-features --locked + name: Rust 1.95 + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + os: '["ubuntu-latest"]' + toolchain: 1.95.0 + targets: "" + tools: "" + craft-fonts-ref: "" + require-craft-fonts: false + timeout-minutes: 15 + dev-debug: "" + command: cargo check --workspace --lib --all-features --locked + wasm: name: UI and settings WASM - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@v2 - with: - key: workspace-wasm - - run: cargo check -p craft-ui -p craft-settings --all-features --locked --target wasm32-unknown-unknown + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + os: '["ubuntu-latest"]' + targets: wasm32-unknown-unknown + tools: "" + craft-fonts-ref: "" + require-craft-fonts: false + timeout-minutes: 15 + command: cargo check -p craft-ui -p craft-settings --all-features --locked --target wasm32-unknown-unknown From 7316e4e22314c2d8ce7d2cc8bc02b5e302b88e28 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:13:53 +0000 Subject: [PATCH 031/520] fix: clear workspace baseline gates on Rust 1.100 --- crates/cms/src/synth.rs | 8 ++-- crates/kra/src/lib.rs | 43 ++++++++++++++---- crates/latex/src/tests.rs | 12 ++--- crates/ora/src/lib.rs | 61 ++++++++++++++++++++------ crates/plotter/tests/golden.rs | 4 +- crates/proof/src/hunspell.rs | 16 ++++--- crates/proof/src/tr/mod.rs | 6 +-- crates/video/src/hevc/decode/filter.rs | 5 +-- 8 files changed, 108 insertions(+), 47 deletions(-) diff --git a/crates/cms/src/synth.rs b/crates/cms/src/synth.rs index a59058f08..b1535f78c 100644 --- a/crates/cms/src/synth.rs +++ b/crates/cms/src/synth.rs @@ -111,9 +111,9 @@ impl CmykModel { // Overprints with black: multiplicative in reflectance above a first-surface floor. let floor = 0.006; let mut prim = [[0.0; 3]; 16]; - for m in 0..16 { + for (m, slot) in prim.iter_mut().enumerate() { let p = base[m & 7]; - prim[m] = if m & 8 == 0 { + *slot = if m & 8 == 0 { p } else { std::array::from_fn(|i| { @@ -133,7 +133,7 @@ impl CmykModel { (v + 4.0 * self.tvi[i] * v * (1.0 - v)).clamp(0.0, 1.0) }); let mut s = [0.0; 3]; - for m in 0..16 { + for (m, prim) in self.prim_n.iter().enumerate() { let mut w = 1.0; for (i, ai) in a.iter().enumerate() { w *= if m & (1 << i) != 0 { *ai } else { 1.0 - ai }; @@ -141,7 +141,7 @@ impl CmykModel { if w == 0.0 { continue; } - for (acc, p) in s.iter_mut().zip(&self.prim_n[m]) { + for (acc, p) in s.iter_mut().zip(prim) { *acc += w * p; } } diff --git a/crates/kra/src/lib.rs b/crates/kra/src/lib.rs index 7485b9dd5..12ae3468b 100644 --- a/crates/kra/src/lib.rs +++ b/crates/kra/src/lib.rs @@ -161,6 +161,16 @@ fn children<'a>(el: &'a El, container: &'static str, tag: &'static str) -> impl } fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { + let mut out = node_fields(el, is_mask)?; + if !is_mask { + out.layers = layers(el, count)?; + out.masks = children(el, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; + } + Ok(out) +} + +/// A node's own attributes, without its children. +fn node_fields(el: &El, is_mask: bool) -> Result { let owned = |name: &str| attr(el, name).map(str::to_string); let node_type = attr(el, "nodetype").unwrap_or("").to_string(); // Only the kinds whose pixels are read need sound numbers; a node an application will leave @@ -168,7 +178,7 @@ fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { let read = matches!(node_type.as_str(), "paintlayer" | "grouplayer" | "transparencymask"); let lenient = |v: Result, default: f64| if read { v } else { Ok(v.unwrap_or(default)) }; let (x, y) = if is_mask && !read { (offset(el, "x").unwrap_or(0), offset(el, "y").unwrap_or(0)) } else { (offset(el, "x")?, offset(el, "y")?) }; - let mut out = Node { + let out = Node { node_type, name: owned("name"), visible: attr(el, "visible") != Some("0"), @@ -187,22 +197,39 @@ fn node(el: &El, is_mask: bool, count: &mut usize) -> Result { layers: Vec::new(), masks: Vec::new(), }; - if !is_mask { - out.layers = layers(el, count)?; - out.masks = children(el, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; - } Ok(out) } -/// The `` of `el`. Recursion is bounded by [`MAX_XML_DEPTH`]. +/// The `` of `el`, walked with an explicit stack: groups can nest up to [`MAX_XML_DEPTH`] +/// deep, more than a small thread stack can recurse. fn layers(el: &El, count: &mut usize) -> Result> { + struct Frame<'a> { + node: Node, + rest: std::vec::IntoIter<&'a El>, + } + let mut root = children(el, "layers", "layer").collect::>().into_iter(); + let mut stack: Vec = Vec::new(); let mut out = Vec::new(); - for child in children(el, "layers", "layer") { + loop { + let next = match stack.last_mut() { + Some(frame) => frame.rest.next(), + None => root.next(), + }; + let Some(child) = next else { + let Some(frame) = stack.pop() else { break }; + match stack.last_mut() { + Some(parent) => parent.node.layers.push(frame.node), + None => out.push(frame.node), + } + continue; + }; *count += 1; if *count > MAX_NODES { return Err(invalid(format!("more than {MAX_NODES} layers"))); } - out.push(node(child, false, count)?); + let mut built = node_fields(child, false)?; + built.masks = children(child, "masks", "mask").map(|m| node(m, true, count)).collect::>()?; + stack.push(Frame { node: built, rest: children(child, "layers", "layer").collect::>().into_iter() }); } Ok(out) } diff --git a/crates/latex/src/tests.rs b/crates/latex/src/tests.rs index 55bde2489..ccf349c80 100644 --- a/crates/latex/src/tests.rs +++ b/crates/latex/src/tests.rs @@ -353,9 +353,9 @@ fn table_borders_and_borderless_round_trip() { let mut doc = Document::new(); let mut t_borderless = Table::new(2, 2, 400.0); t_borderless.props.style = None; - for r in 0..2 { - for c in 0..2 { - t_borderless.rows[r].cells[c].blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; + for row in &mut t_borderless.rows { + for cell in &mut row.cells { + cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; } } doc.body = vec![Arc::new(Block::Table(t_borderless))]; @@ -369,9 +369,9 @@ fn table_borders_and_borderless_round_trip() { let mut doc_bordered = Document::new(); let mut t_bordered = Table::new(2, 2, 400.0); t_bordered.props.style = Some("TableGrid".to_string()); - for r in 0..2 { - for c in 0..2 { - t_bordered.rows[r].cells[c].blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; + for row in &mut t_bordered.rows { + for cell in &mut row.cells { + cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; } } doc_bordered.body = vec![Arc::new(Block::Table(t_bordered))]; diff --git a/crates/ora/src/lib.rs b/crates/ora/src/lib.rs index b2a91cc40..69d494d0d 100644 --- a/crates/ora/src/lib.rs +++ b/crates/ora/src/lib.rs @@ -189,28 +189,63 @@ fn props(el: &El) -> Result { }) } -/// The elements of `stack`. Recursion is bounded by [`MAX_XML_DEPTH`]. +/// The elements of `stack`, walked with an explicit stack: a stack can nest up to +/// [`MAX_XML_DEPTH`] deep, more than a small thread stack can recurse. fn stack(el: &El, count: &mut usize) -> Result> { + struct Frame<'a> { + props: Props, + isolation: Option, + children: Vec, + rest: std::vec::IntoIter<&'a El>, + } + let mut root = el.elements().collect::>().into_iter(); + let mut frames: Vec = Vec::new(); let mut out = Vec::new(); - for child in el.elements() { + loop { + let next = match frames.last_mut() { + Some(frame) => frame.rest.next(), + None => root.next(), + }; + let Some(child) = next else { + let Some(frame) = frames.pop() else { break }; + let node = Node::Stack(Stack { props: frame.props, isolation: frame.isolation, children: frame.children }); + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + continue; + }; *count += 1; if *count > MAX_NODES { return Err(invalid(format!("more than {MAX_NODES} layers"))); } - out.push(match child.name.as_str() { - "layer" => Node::Layer(Layer { - props: props(child)?, - x: int(child, "x")?, - y: int(child, "y")?, - src: child.attr("src").map(|s| s.trim().to_string()), - }), - "stack" => Node::Stack(Stack { + match child.name.as_str() { + "layer" => { + let node = Node::Layer(Layer { + props: props(child)?, + x: int(child, "x")?, + y: int(child, "y")?, + src: child.attr("src").map(|s| s.trim().to_string()), + }); + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + } + "stack" => frames.push(Frame { props: props(child)?, isolation: child.attr("isolation").map(|s| s.trim().to_string()), - children: stack(child, count)?, + children: Vec::new(), + rest: child.elements().collect::>().into_iter(), }), - other => Node::Other { tag: other.to_string(), name: child.attr("name").map(str::to_string) }, - }); + other => { + let node = Node::Other { tag: other.to_string(), name: child.attr("name").map(str::to_string) }; + match frames.last_mut() { + Some(parent) => parent.children.push(node), + None => out.push(node), + } + } + } } Ok(out) } diff --git a/crates/plotter/tests/golden.rs b/crates/plotter/tests/golden.rs index 26aaae554..4d5029010 100644 --- a/crates/plotter/tests/golden.rs +++ b/crates/plotter/tests/golden.rs @@ -135,7 +135,7 @@ fn silhouette_registration_marks() { let p = silhouette::encode(&pro, &j).unwrap(); let (head, rest) = p.split_at_registration(); let text = |c: &[Command]| c.iter().map(Command::to_ascii).collect::>().join(" "); - assert!(text(head).ends_with(" FC18,1,1 TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB55,1 TB123,4600,3600,200,100")); + assert!(text(head).ends_with(" FC18,1,1 TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB54,0,0 TB55,1 TB123,4600,3600,200,100")); // The design is shifted so the first mark is the origin. assert_eq!(text(rest), "M100,100 D100,300 M100,0 SO0"); assert_eq!(p.registration_wait, Some(head.len())); @@ -148,7 +148,7 @@ fn silhouette_registration_marks() { reg.origin_right_mm = 0.0; assert!(ascii(®).ends_with(" TB124,4600,3600,0,0")); reg.search = false; - assert_eq!(ascii(®), "TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB55,1 TB23,4600,3600"); + assert_eq!(ascii(®), "TB50,0 TB99 TB52,2 TB51,400 TB53,10 TB54,0,0 TB55,1 TB23,4600,3600"); } #[test] diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index 966ded72f..08629a052 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -754,11 +754,12 @@ mod tests { /// spec's default `~/.local/share` applies, and with no home there is no folder. #[test] fn xdg_data_home_follows_the_spec() { - let home = Path::new("/home/a"); - assert_eq!(xdg_data_home(Some("/data".into()), Some(home)), Some(PathBuf::from("/data"))); - assert_eq!(xdg_data_home(Some("/data".into()), None), Some(PathBuf::from("/data"))); + let home = std::env::temp_dir().join("xdg-home"); + let data = std::env::temp_dir().join("xdg-data"); + assert_eq!(xdg_data_home(Some(data.as_os_str().to_owned()), Some(&home)), Some(data.clone())); + assert_eq!(xdg_data_home(Some(data.as_os_str().to_owned()), None), Some(data)); for xdg in [None, Some(""), Some("relative/data")] { - assert_eq!(xdg_data_home(xdg.map(Into::into), Some(home)), Some(home.join(".local/share")), "{xdg:?}"); + assert_eq!(xdg_data_home(xdg.map(Into::into), Some(&home)), Some(home.join(".local/share")), "{xdg:?}"); assert_eq!(xdg_data_home(xdg.map(Into::into), None), None, "{xdg:?}"); } } @@ -1010,7 +1011,12 @@ mod tests { assert!(load(&aff, &big).unwrap_err().contains("too large")); // A directory, and on Unix an endless device, are not dictionary files. (No clock: a // reader that read `/dev/zero` would never return, so the error itself is the proof.) - assert!(load(&aff, &dir).unwrap_err().contains("not a regular file")); + let error = load(&aff, &dir).unwrap_err(); + // Windows may reject opening a directory before the reader can inspect its type. + #[cfg(unix)] + assert!(error.contains("not a regular file"), "{error}"); + #[cfg(not(unix))] + assert!(!error.is_empty()); #[cfg(unix)] assert!(load(&aff, Path::new("/dev/zero")).unwrap_err().contains("not a regular file")); // Turkish reads through the same reader. diff --git a/crates/proof/src/tr/mod.rs b/crates/proof/src/tr/mod.rs index 5472fbe21..c728d5872 100644 --- a/crates/proof/src/tr/mod.rs +++ b/crates/proof/src/tr/mod.rs @@ -100,11 +100,7 @@ fn check(d: &TrDict, word: &str, opts: &crate::Options) -> bool { } // Compound words separated by hyphens (e.g., `afyon-karahisar`) - if w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) { - return true; - } - - false + w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) } #[cfg(test)] diff --git a/crates/video/src/hevc/decode/filter.rs b/crates/video/src/hevc/decode/filter.rs index d5b94161d..69f936bc1 100644 --- a/crates/video/src/hevc/decode/filter.rs +++ b/crates/video/src/hevc/decode/filter.rs @@ -522,10 +522,7 @@ fn sao_edge_ctb( return false; } } - if !pic.pps.loop_filter_across_tiles && pic.layout.tile_id[pic.layout.rs_to_ts[nrs] as usize] != pic.layout.tile_id[cur_ts as usize] { - return false; - } - true + pic.pps.loop_filter_across_tiles || pic.layout.tile_id[pic.layout.rs_to_ts[nrs] as usize] == pic.layout.tile_id[cur_ts as usize] }; // avail[dy][dx]: the CTB at (rx + dx - 1, ry + dy - 1) may be used. let (rx, ry) = (rs % pic.wctb, rs / pic.wctb); From ae0f0488dc7a97653a26efe69991a4b2588463ac Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:44:51 +0000 Subject: [PATCH 032/520] craft-time: preserve SoundCraft time API and serialization --- Cargo.lock | 2 + crates/time/Cargo.toml | 2 + crates/time/src/lib.rs | 4 + crates/time/src/soundcraft/format.rs | 241 ++++++++ crates/time/src/soundcraft/grid.rs | 809 +++++++++++++++++++++++++ crates/time/src/soundcraft/mod.rs | 155 +++++ crates/time/src/soundcraft/tempo.rs | 622 +++++++++++++++++++ crates/time/src/soundcraft/timecode.rs | 309 ++++++++++ 8 files changed, 2144 insertions(+) create mode 100644 crates/time/src/soundcraft/format.rs create mode 100644 crates/time/src/soundcraft/grid.rs create mode 100644 crates/time/src/soundcraft/mod.rs create mode 100644 crates/time/src/soundcraft/tempo.rs create mode 100644 crates/time/src/soundcraft/timecode.rs diff --git a/Cargo.lock b/Cargo.lock index 89478f3cc..4136a81de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -717,6 +717,8 @@ version = "0.1.0" dependencies = [ "proptest", "serde", + "serde_json", + "thiserror 2.0.21", ] [[package]] diff --git a/crates/time/Cargo.toml b/crates/time/Cargo.toml index 0f63cb228..5ef614bdc 100644 --- a/crates/time/Cargo.toml +++ b/crates/time/Cargo.toml @@ -12,6 +12,8 @@ workspace = true [dependencies] serde = { workspace = true } +thiserror.workspace = true [dev-dependencies] +serde_json = { workspace = true } proptest = "1.4" diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs index a3fbfc355..0d3703060 100644 --- a/crates/time/src/lib.rs +++ b/crates/time/src/lib.rs @@ -15,6 +15,10 @@ use serde::{Deserialize, Serialize}; use std::fmt; use std::ops::{Add, AddAssign, Div, Mul, Neg, Rem, Sub, SubAssign}; +/// SoundCraft's sample-index, tempo-map, grid, and display-time API, retained verbatim for +/// session-format compatibility while sharing the broader Craft time primitives above. +pub mod soundcraft; + /// Clock ticks per second: 254 016 000 000. pub const TICKS_PER_SECOND: i64 = 254_016_000_000; diff --git a/crates/time/src/soundcraft/format.rs b/crates/time/src/soundcraft/format.rs new file mode 100644 index 000000000..11b588c3f --- /dev/null +++ b/crates/time/src/soundcraft/format.rs @@ -0,0 +1,241 @@ +//! Formatting and parsing positions in the five timebases. + +use super::{BarBeat, FrameRate, SampleRate, Samples, TempoMap, TimeError, Timecode, timecode::feet_frames}; + +/// The counters' and rulers' timebases. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum TimeFormat { + BarsBeats, + #[default] + MinSecs, + Timecode, + FeetFrames, + Samples, +} + +impl TimeFormat { + pub const ALL: [TimeFormat; 5] = [TimeFormat::BarsBeats, TimeFormat::MinSecs, TimeFormat::Timecode, TimeFormat::FeetFrames, TimeFormat::Samples]; + + pub fn label(self) -> &'static str { + match self { + TimeFormat::BarsBeats => "Bars|Beats", + TimeFormat::MinSecs => "Min:Secs", + TimeFormat::Timecode => "Timecode", + TimeFormat::FeetFrames => "Feet+Frames", + TimeFormat::Samples => "Samples", + } + } + + pub fn id(self) -> &'static str { + match self { + TimeFormat::BarsBeats => "bars_beats", + TimeFormat::MinSecs => "min_secs", + TimeFormat::Timecode => "timecode", + TimeFormat::FeetFrames => "feet_frames", + TimeFormat::Samples => "samples", + } + } + + pub fn from_id(id: &str) -> Option { + TimeFormat::ALL.into_iter().find(|f| f.id() == id || f.label().eq_ignore_ascii_case(id)) + } +} + +fn min_secs(s: Samples, sr: SampleRate) -> String { + let neg = s < 0; + // Integer maths: a float `seconds * 1000` lands just below exact millisecond positions. + let ms_total = i64::try_from(i128::from(s).abs() * 1000 / i128::from(sr.hz().max(1))).unwrap_or(i64::MAX); + let mins = ms_total / 60_000; + let secs = (ms_total / 1000) % 60; + let ms = ms_total % 1000; + format!("{}{mins}:{secs:02}.{ms:03}", if neg { "-" } else { "" }) +} + +/// Format an absolute position. `tc_start` is the timecode of sample 0 in samples, `ff_start` the +/// feet+frames reading of sample 0 in frames. +pub fn format_position(s: Samples, fmt: TimeFormat, sr: SampleRate, map: &TempoMap, rate: FrameRate, tc_start: Samples, ff_start: i64) -> String { + match fmt { + TimeFormat::Samples => s.to_string(), + TimeFormat::MinSecs => min_secs(s, sr), + TimeFormat::Timecode => Timecode::from_samples(s.saturating_add(tc_start), rate, sr).format(rate), + TimeFormat::FeetFrames => { + let (ft, fr) = feet_frames(s, rate, sr, ff_start); + format!("{ft}+{fr:02}") + } + TimeFormat::BarsBeats => { + let bb = map.bar_beat_at(s, sr); + format!("{}| {}| {:03}", bb.bar, bb.beat, bb.tick) + } + } +} + +/// Format a length starting at `start` (Bars|Beats lengths count from 0|0|000). +pub fn format_length(start: Samples, len: Samples, fmt: TimeFormat, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> String { + match fmt { + TimeFormat::BarsBeats => { + let t0 = map.samples_to_ticks(start, sr); + let t1 = map.samples_to_ticks(start.saturating_add(len), sr); + let m = map.meter_at_tick(t0); + let d = (t1 - t0).max(0); + let bars = d / m.ticks_per_bar().max(1); + let rem = d % m.ticks_per_bar().max(1); + format!("{bars}| {}| {:03}", rem / m.ticks_per_beat().max(1), rem % m.ticks_per_beat().max(1)) + } + TimeFormat::Timecode => Timecode::from_samples(len, rate, sr).format(rate), + other => format_position(len, other, sr, map, rate, 0, 0), + } +} + +/// Parse a position typed into a counter or dialog. +pub fn parse_position( + text: &str, + fmt: TimeFormat, + sr: SampleRate, + map: &TempoMap, + rate: FrameRate, + tc_start: Samples, + ff_start: i64, +) -> Result { + let t = text.trim(); + let err = || TimeError::Parse(t.to_string(), fmt.label()); + match fmt { + TimeFormat::Samples => t.replace([',', '_'], "").parse::().map_err(|_| err()), + TimeFormat::MinSecs => { + let (neg, body) = match t.strip_prefix('-') { + Some(r) => (true, r), + None => (false, t), + }; + let (m, s) = match body.split_once(':') { + Some((m, s)) => (m.trim().parse::().map_err(|_| err())?, s.trim().parse::().map_err(|_| err())?), + None => (0.0, body.parse::().map_err(|_| err())?), + }; + if !m.is_finite() || !s.is_finite() || m < 0.0 || s < 0.0 { + return Err(err()); + } + let v = sr.samples(m * 60.0 + s); + Ok(if neg { -v } else { v }) + } + TimeFormat::Timecode => { + let tc = Timecode::parse(t)?; + if tc.minutes >= 60 || tc.seconds >= 60 || tc.frames >= rate.nominal().max(1) { + return Err(err()); + } + if rate.is_drop() && tc.seconds == 0 && tc.minutes % 10 != 0 { + let dropped = if rate.nominal() >= 60 { 4 } else { 2 }; + if tc.frames < dropped { + return Err(err()); + } + } + Ok(tc.to_samples(rate, sr).saturating_sub(tc_start)) + } + TimeFormat::FeetFrames => { + let (ft, fr) = t.split_once('+').ok_or_else(err)?; + let ft: i64 = ft.trim().parse().map_err(|_| err())?; + let fr: i64 = fr.trim().parse().map_err(|_| err())?; + Ok(rate.samples_at_frame(ft.saturating_mul(16).saturating_add(fr).saturating_sub(ff_start), sr)) + } + TimeFormat::BarsBeats => { + let parts: Vec = + t.split(['|', '.', ' ']).filter(|p| !p.is_empty()).map(str::parse::).collect::>().map_err(|_| err())?; + let bb = BarBeat { + bar: *parts.first().ok_or_else(err)?, + beat: parts.get(1).copied().unwrap_or(1).max(1), + tick: parts.get(2).copied().unwrap_or(0).max(0), + }; + Ok(map.samples_at_bar_beat(bb, sr)) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ctx() -> (SampleRate, TempoMap, FrameRate) { + (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps30) + } + + #[test] + fn huge_typed_positions_parse_without_overflowing() { + let (sr, map, rate) = ctx(); + for (text, fmt) in [ + ("999999999999999999:0", TimeFormat::Timecode), + ("100000000000000:00:00:00", TimeFormat::Timecode), + ("-9223372036854775808", TimeFormat::BarsBeats), + ] { + let _ = parse_position(text, fmt, sr, &map, rate, 0, 0); + } + } + + #[test] + fn formats_like_pro_tools_counters() { + let (sr, map, rate) = ctx(); + assert_eq!(format_position(0, TimeFormat::MinSecs, sr, &map, rate, 0, 0), "0:00.000"); + assert_eq!(format_position(48_000 * 75 + 24_000, TimeFormat::MinSecs, sr, &map, rate, 0, 0), "1:15.500"); + assert_eq!(format_position(0, TimeFormat::BarsBeats, sr, &map, rate, 0, 0), "1| 1| 000"); + assert_eq!(format_position(48_000, TimeFormat::Timecode, sr, &map, rate, 0, 0), "00:00:01:00"); + assert_eq!(format_position(12345, TimeFormat::Samples, sr, &map, rate, 0, 0), "12345"); + } + + #[test] + fn minimum_bar_parses_without_overflow() { + let (sr, map, rate) = ctx(); + let p = |t: &str| parse_position(t, TimeFormat::BarsBeats, sr, &map, rate, 0, 0); + assert!(p("-9223372036854775808|01|000").is_ok_and(|s| s < 0)); + assert_eq!(p("2|3|000").ok(), Some(144_000)); + assert_eq!(p("0|1|000").ok(), Some(-96_000)); + } + + #[test] + fn parse_round_trips() { + let (sr, map, rate) = ctx(); + for fmt in TimeFormat::ALL { + for s in [0i64, 48_000, 96_000 * 3, 1_440_000] { + let text = format_position(s, fmt, sr, &map, rate, 0, 0); + let back = parse_position(&text, fmt, sr, &map, rate, 0, 0).unwrap(); + assert!((back - s).abs() <= 48, "{fmt:?} {s} → {text} → {back}"); + } + } + } + + #[test] + fn parse_round_trips_timecode_negative() { + let (sr, map, _) = ctx(); + let rate = FrameRate::Fps25; + for fmt in [TimeFormat::Timecode, TimeFormat::FeetFrames] { + for s in [-48_000i64, -96_000 * 3, -1_440_000] { + let text = format_position(s, fmt, sr, &map, rate, 0, 0); + let back = parse_position(&text, fmt, sr, &map, rate, 0, 0).unwrap(); + assert_eq!(back, s, "{fmt:?} {s} → {text} → {back}"); + } + } + } + + #[test] + fn parse_rejects_garbage() { + let (sr, map, rate) = ctx(); + assert!(parse_position("abc", TimeFormat::Samples, sr, &map, rate, 0, 0).is_err()); + assert!(parse_position("1:x", TimeFormat::MinSecs, sr, &map, rate, 0, 0).is_err()); + assert!(parse_position("", TimeFormat::BarsBeats, sr, &map, rate, 0, 0).is_err()); + } + + #[test] + fn drop_frame_skipped_labels_are_rejected() { + let (sr, map, _) = ctx(); + for (rate, skipped) in [ + (FrameRate::Fps29_97Drop, &["00:01:00;00", "00:01:00;01"][..]), + (FrameRate::Fps59_94Drop, &["00:01:00;00", "00:01:00;01", "00:01:00;02", "00:01:00;03"][..]), + ] { + for text in skipped { + assert!(parse_position(text, TimeFormat::Timecode, sr, &map, rate, 0, 0).is_err(), "accepted skipped label {text} at {rate:?}"); + } + assert!(parse_position("00:10:00;00", TimeFormat::Timecode, sr, &map, rate, 0, 0).is_ok()); + } + } + + #[test] + fn bar_lengths() { + let (sr, map, rate) = ctx(); + assert_eq!(format_length(0, 96_000 + 24_000, TimeFormat::BarsBeats, sr, &map, rate), "1| 1| 000"); + } +} diff --git a/crates/time/src/soundcraft/grid.rs b/crates/time/src/soundcraft/grid.rs new file mode 100644 index 000000000..5f32ca6d8 --- /dev/null +++ b/crates/time/src/soundcraft/grid.rs @@ -0,0 +1,809 @@ +//! Grid and Nudge values: musical or absolute step sizes, grid lines and snapping. + +use super::{FrameRate, SampleRate, Samples, TICKS_PER_QUARTER, TempoMap, Timecode, to_samples}; + +/// Note values for Bars|Beats grids. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub enum NoteValue { + Bar, + Whole, + Half, + Quarter, + Eighth, + Sixteenth, + ThirtySecond, + SixtyFourth, +} + +impl NoteValue { + /// Every musical increment in menu order, from largest to smallest. + pub const ALL: [NoteValue; 8] = [ + NoteValue::Bar, + NoteValue::Whole, + NoteValue::Half, + NoteValue::Quarter, + NoteValue::Eighth, + NoteValue::Sixteenth, + NoteValue::ThirtySecond, + NoteValue::SixtyFourth, + ]; + /// Returns the nominal tick length; bars are adjusted for the active meter when used. + pub fn ticks(self) -> i64 { + match self { + NoteValue::Bar => TICKS_PER_QUARTER * 4, + NoteValue::Whole => TICKS_PER_QUARTER * 4, + NoteValue::Half => TICKS_PER_QUARTER * 2, + NoteValue::Quarter => TICKS_PER_QUARTER, + NoteValue::Eighth => TICKS_PER_QUARTER / 2, + NoteValue::Sixteenth => TICKS_PER_QUARTER / 4, + NoteValue::ThirtySecond => TICKS_PER_QUARTER / 8, + NoteValue::SixtyFourth => TICKS_PER_QUARTER / 16, + } + } + /// Returns the menu label for this musical increment. + pub fn label(self) -> &'static str { + match self { + NoteValue::Bar => "1 bar", + NoteValue::Whole => "Whole note", + NoteValue::Half => "1/2 note", + NoteValue::Quarter => "1/4 note", + NoteValue::Eighth => "1/8 note", + NoteValue::Sixteenth => "1/16 note", + NoteValue::ThirtySecond => "1/32 note", + NoteValue::SixtyFourth => "1/64 note", + } + } +} + +/// The time scale used by a Grid or Nudge value. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub enum GridScale { + BarsBeats, + MinSecs, + Timecode, + FeetFrames, + Samples, + ClipsMarkers, +} + +impl GridScale { + /// Numeric time scales shared by Grid and Nudge. + pub const TIME: [GridScale; 5] = [GridScale::BarsBeats, GridScale::MinSecs, GridScale::Timecode, GridScale::FeetFrames, GridScale::Samples]; + + /// Returns the user-facing scale label. + pub fn label(self) -> &'static str { + match self { + GridScale::BarsBeats => "Bars|Beats", + GridScale::MinSecs => "Min:Secs", + GridScale::Timecode => "Timecode", + GridScale::FeetFrames => "Feet+Frames", + GridScale::Samples => "Samples", + GridScale::ClipsMarkers => "Clips/Markers", + } + } + + /// Returns the stable command and session identifier. + pub fn id(self) -> &'static str { + match self { + GridScale::BarsBeats => "bars_beats", + GridScale::MinSecs => "min_secs", + GridScale::Timecode => "timecode", + GridScale::FeetFrames => "feet_frames", + GridScale::Samples => "samples", + GridScale::ClipsMarkers => "clips_markers", + } + } + + /// Parses a stable identifier or user-facing label. + pub fn from_id(id: &str) -> Option { + Self::TIME.into_iter().chain([GridScale::ClipsMarkers]).find(|scale| scale.id() == id || scale.label().eq_ignore_ascii_case(id)) + } +} + +/// Timecode Grid/Nudge increments, ordered independently of the session frame rate. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub enum TimecodeValue { + Second, + /// Five physical frames, independent of skipped drop-frame labels. + FiveFrames, + Frame, + HalfFrame, + QuarterFrame, + SubFrame, +} + +impl TimecodeValue { + /// Every Timecode increment in menu order, from largest to smallest. + pub const ALL: [TimecodeValue; 6] = [ + TimecodeValue::Second, + TimecodeValue::FiveFrames, + TimecodeValue::Frame, + TimecodeValue::HalfFrame, + TimecodeValue::QuarterFrame, + TimecodeValue::SubFrame, + ]; + + /// Returns the user-facing increment label. + pub fn label(self) -> &'static str { + match self { + TimecodeValue::Second => "1 second", + TimecodeValue::FiveFrames => "5 frames", + TimecodeValue::Frame => "1 frame", + TimecodeValue::HalfFrame => "1/2 frame", + TimecodeValue::QuarterFrame => "1/4 frame", + TimecodeValue::SubFrame => "1 subframe", + } + } + + /// Returns the stable command and session identifier. + pub fn id(self) -> &'static str { + match self { + TimecodeValue::Second => "second", + TimecodeValue::FiveFrames => "5_frames", + TimecodeValue::Frame => "frame", + TimecodeValue::HalfFrame => "half_frame", + TimecodeValue::QuarterFrame => "quarter_frame", + TimecodeValue::SubFrame => "subframe", + } + } + + /// Parses a stable identifier or user-facing label. + pub fn from_id(id: &str) -> Option { + Self::ALL.into_iter().find(|value| value.id() == id || value.label().eq_ignore_ascii_case(id)) + } + + /// Returns the increment length in frames at the session rate. + fn frames(self, rate: FrameRate) -> f64 { + match self { + TimecodeValue::Second => rate.nominal() as f64, + TimecodeValue::FiveFrames => 5.0, + TimecodeValue::Frame => 1.0, + TimecodeValue::HalfFrame => 0.5, + TimecodeValue::QuarterFrame => 0.25, + TimecodeValue::SubFrame => 0.01, + } + } +} + +/// Feet+Frames Grid/Nudge increments in physical frames, independent of drop-frame label skips. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub enum FeetFramesValue { + Foot, + Frame, + QuarterFrame, + SubFrame, +} + +impl FeetFramesValue { + /// Every Feet+Frames increment in menu order, from largest to smallest. + pub const ALL: [FeetFramesValue; 4] = [FeetFramesValue::Foot, FeetFramesValue::Frame, FeetFramesValue::QuarterFrame, FeetFramesValue::SubFrame]; + + /// Returns the user-facing increment label. + pub fn label(self) -> &'static str { + match self { + FeetFramesValue::Foot => "1 foot", + FeetFramesValue::Frame => "1 frame", + FeetFramesValue::QuarterFrame => "1/4 frame", + FeetFramesValue::SubFrame => "1 subframe", + } + } + + /// Returns the stable command and session identifier. + pub fn id(self) -> &'static str { + match self { + FeetFramesValue::Foot => "foot", + FeetFramesValue::Frame => "frame", + FeetFramesValue::QuarterFrame => "quarter_frame", + FeetFramesValue::SubFrame => "subframe", + } + } + + /// Parses a stable identifier or user-facing label. + pub fn from_id(id: &str) -> Option { + Self::ALL.into_iter().find(|value| value.id() == id || value.label().eq_ignore_ascii_case(id)) + } + + /// Returns the increment length in frames. + fn frames(self) -> f64 { + let (num, den) = self.frame_ratio(); + num as f64 / den as f64 + } + + /// Exact numerator and denominator of the increment in physical frames. + fn frame_ratio(self) -> (i128, i128) { + match self { + Self::Foot => (16, 1), + Self::Frame => (1, 1), + Self::QuarterFrame => (1, 4), + Self::SubFrame => (1, 100), + } + } + + /// Apply the counter origin in frames before rounding once to the nearest sample. + fn sample_line(self, index: i128, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples { + let (num, den) = self.frame_ratio(); + let (rate_num, rate_den) = rate.rational(); + let frames = index * num - i128::from(origin) * den; + let samples = frames * i128::from(sr.hz().max(1)) * i128::from(rate_den); + let divisor = den * i128::from(rate_num); + let magnitude = (samples.abs() + divisor / 2) / divisor; + let rounded = if samples < 0 { -magnitude } else { magnitude }; + i64::try_from(rounded).unwrap_or(if rounded < 0 { i64::MIN } else { i64::MAX }) + } + + /// Find a line index using exact frame arithmetic, allowing for whole-sample rounding. + fn line_index(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> i128 { + let (num, den) = self.frame_ratio(); + let (rate_num, rate_den) = rate.rational(); + let scale = i128::from(sr.hz().max(1)) * i128::from(rate_den); + let frames = i128::from(position) * i128::from(rate_num) + i128::from(origin) * scale; + let mut index = (frames * den).div_euclid(scale * num); + if self.sample_line(index + 1, sr, rate, origin) <= position { + index += 1; + } + index + } + + /// The Feet+Frames line at or before a sample, relative to the counter's frame origin. + pub fn line_at_or_before(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples { + self.sample_line(self.line_index(position, sr, rate, origin), sr, rate, origin) + } + + /// The next distinct sample line; subframe increments may share a sample at low sample rates. + fn next_line(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples { + self.sample_line(self.line_index(position, sr, rate, origin) + 1, sr, rate, origin).max(position.saturating_add(1)) + } + + /// Snap against frame-origin lines, choosing the earlier line when distances are equal. + pub fn snap(self, position: Samples, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples { + let lo = self.line_at_or_before(position, sr, rate, origin); + let hi = self.next_line(lo, sr, rate, origin); + if i128::from(position) - i128::from(lo) <= i128::from(hi) - i128::from(position) { lo } else { hi } + } + + /// Distinct Feet+Frames sample lines within `[start, end)`, capped at `max`. + pub fn lines(self, start: Samples, end: Samples, sr: SampleRate, rate: FrameRate, origin: i64, max: usize) -> Vec { + let mut out = Vec::new(); + let mut line = self.line_at_or_before(start, sr, rate, origin); + while line < end && out.len() < max { + if line >= start { + out.push(line); + } + let next = self.next_line(line, sr, rate, origin); + if next <= line { + break; + } + line = next; + } + out + } +} + +/// A grid or nudge step. +#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] +pub enum GridValue { + /// Musical: note value, optionally dotted or triplet. + Note { value: NoteValue, dotted: bool, triplet: bool }, + /// Absolute seconds (Min:Secs grids: 0.001 … 1 s). + Seconds(f64), + /// Frames at the session timecode rate. + /// Kept for sessions and commands written before named timecode increments were added. + Frames(i64), + /// A named Timecode increment, including fractional frames and one second. + Timecode(TimecodeValue), + /// A Feet+Frames increment (one foot is 16 frames). + FeetFrames(FeetFramesValue), + /// Raw samples. + Samples(i64), +} + +impl Default for GridValue { + /// Starts on a straight sixteenth-note Bars|Beats grid. + fn default() -> Self { + GridValue::Note { value: NoteValue::Sixteenth, dotted: false, triplet: false } + } +} + +impl GridValue { + /// Returns the user-facing label for this increment. + pub fn label(&self) -> String { + match self { + GridValue::Note { value, dotted, triplet } => { + let mut s = value.label().to_string(); + if *dotted { + s.push_str(" dotted"); + } + if *triplet { + s.push_str(" triplet"); + } + s + } + GridValue::Seconds(s) => { + if *s >= 1.0 { + format!("{s} second{}", if *s == 1.0 { "" } else { "s" }) + } else { + format!("{} ms", (s * 1000.0).round() as i64) + } + } + GridValue::Frames(f) => format!("{f} frame{}", if *f == 1 { "" } else { "s" }), + GridValue::Timecode(value) => value.label().to_string(), + GridValue::FeetFrames(value) => value.label().to_string(), + GridValue::Samples(n) => format!("{n} sample{}", if *n == 1 { "" } else { "s" }), + } + } + + /// Returns the time scale to which this increment belongs. + pub fn scale(self) -> GridScale { + match self { + GridValue::Note { .. } => GridScale::BarsBeats, + GridValue::Seconds(_) => GridScale::MinSecs, + GridValue::Frames(_) | GridValue::Timecode(_) => GridScale::Timecode, + GridValue::FeetFrames(_) => GridScale::FeetFrames, + GridValue::Samples(_) => GridScale::Samples, + } + } + + /// Returns the default increment for a newly selected time scale. + pub fn for_scale(scale: GridScale) -> GridValue { + match scale { + GridScale::BarsBeats => GridValue::default(), + GridScale::MinSecs => GridValue::Seconds(1.0), + GridScale::Timecode => GridValue::Timecode(TimecodeValue::Frame), + GridScale::FeetFrames => GridValue::FeetFrames(FeetFramesValue::Frame), + GridScale::Samples => GridValue::Samples(100), + GridScale::ClipsMarkers => GridValue::default(), + } + } + + /// The next larger or smaller menu value of the same kind, or `self` at either end. Note values keep + /// their dot or triplet modifier. + pub fn stepped(self, larger: bool) -> GridValue { + /// Finds the next value in a numeric increment ladder without wrapping. + fn next(ladder: &[T], v: T, larger: bool) -> Option { + if larger { ladder.iter().copied().find(|x| *x > v) } else { ladder.iter().rev().copied().find(|x| *x < v) } + } + match self { + GridValue::Note { value, dotted, triplet } => { + let i = NoteValue::ALL.iter().position(|v| *v == value).unwrap_or(0); + let j = if larger { i.checked_sub(1) } else { i.checked_add(1) }; + j.and_then(|j| NoteValue::ALL.get(j)).map_or(self, |&value| GridValue::Note { value, dotted, triplet }) + } + GridValue::Seconds(s) => next(&[0.001, 0.01, 0.1, 0.5, 1.0], s, larger).map_or(self, GridValue::Seconds), + GridValue::Frames(f) => next(&[1, 10, 100], f, larger).map_or(self, GridValue::Frames), + GridValue::Timecode(value) => step_enum(&TimecodeValue::ALL, value, larger).map_or(self, GridValue::Timecode), + GridValue::FeetFrames(value) => step_enum(&FeetFramesValue::ALL, value, larger).map_or(self, GridValue::FeetFrames), + GridValue::Samples(n) => next(&[1, 2, 10, 100, 1_000, 10_000], n, larger).map_or(self, GridValue::Samples), + } + } + + fn note_ticks(value: NoteValue, dotted: bool, triplet: bool, map: &TempoMap, at_tick: i64) -> i64 { + let base = if value == NoteValue::Bar { map.meter_at_tick(at_tick).ticks_per_bar() } else { value.ticks() }; + let mut t = base; + if dotted { + t = t * 3 / 2; + } + if triplet { + t = t * 2 / 3; + } + t.max(1) + } + + /// The step in samples at position `at` (musical steps depend on tempo). + pub fn step_samples(&self, at: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples { + match *self { + GridValue::Note { value, dotted, triplet } => { + let t0 = map.samples_to_ticks(at, sr); + let dt = Self::note_ticks(value, dotted, triplet, map, t0); + (map.tick_to_samples(t0.saturating_add(dt), sr).saturating_sub(map.tick_to_samples(t0, sr))).max(1) + } + GridValue::Seconds(s) => sr.samples(s.clamp(0.000_01, 3600.0)).max(1), + GridValue::Frames(f) => to_samples(rate.samples_per_frame(sr) * f.clamp(1, 1_000_000) as f64).max(1), + GridValue::Timecode(value) => to_samples(rate.samples_per_frame(sr) * value.frames(rate)).max(1), + GridValue::FeetFrames(value) => to_samples(rate.samples_per_frame(sr) * value.frames()).max(1), + GridValue::Samples(n) => n.max(1), + } + } + + /// Nearest grid line to `pos`. + pub fn snap(&self, pos: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples { + if let Self::FeetFrames(value) = *self { + return value.snap(pos, sr, rate, 0); + } + let lo = self.line_at_or_before(pos, sr, map, rate); + let hi = self.next_line(lo, sr, map, rate); + if (pos - lo) <= (hi - pos) { lo } else { hi } + } + + /// The grid line at or before `pos`. + pub fn line_at_or_before(&self, pos: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples { + match *self { + Self::FeetFrames(value) => value.line_at_or_before(pos, sr, rate, 0), + GridValue::Note { value, dotted, triplet } => { + // Float ticks can land a hair either side of a line's own tick. + let mut t = map.samples_to_ticks_f(pos, sr).floor() as i64; + if map.tick_to_samples(t, sr) > pos { + t = t.saturating_sub(1); + } else if map.tick_to_samples(t.saturating_add(1), sr) <= pos { + t = t.saturating_add(1); + } + // Sub-bar values restart at each bar so odd meters stay aligned. A whole note is + // always four quarter notes and therefore keeps a continuous tick origin. + let bb = map.bar_beat_at_tick(t); + let bar_tick = map.bar_start_tick(bb.bar); + let origin = if value == NoteValue::Whole { 0 } else { bar_tick }; + let step = Self::note_ticks(value, dotted, triplet, map, origin); + let k = (t - origin).div_euclid(step); + map.tick_to_samples(origin + k * step, sr) + } + _ => self.abs_line(self.abs_index_at_or_before(pos, sr, rate), sr, rate), + } + } + + /// Position of absolute grid line `k` (Seconds, Frames, Samples), at an exact multiple of the step + /// rounded once, so rounding never accumulates from line to line. + fn abs_line(&self, k: i64, sr: SampleRate, rate: FrameRate) -> Samples { + if let Self::FeetFrames(value) = *self { + return value.sample_line(i128::from(k), sr, rate, 0); + } + if matches!(self, GridValue::Timecode(TimecodeValue::Second)) && rate.is_drop() { + return rate.samples_at_frame(Self::drop_second_frame(k, rate), sr); + } + if let Some(frames) = self.frame_step(rate) { + let whole = frames.round(); + if frames == whole { + return rate.samples_at_frame(k.saturating_mul(whole as i64), sr); + } + let step = (rate.samples_per_frame(sr) * frames).max(1.0); + return to_samples(k as f64 * step); + } + match *self { + GridValue::Seconds(s) => to_samples(k as f64 * Self::seconds_step(s, sr)), + GridValue::Samples(n) => k.saturating_mul(n.max(1)), + GridValue::Note { .. } => 0, + GridValue::Frames(_) | GridValue::Timecode(_) | GridValue::FeetFrames(_) => 0, + } + } + + /// Index of the absolute grid line at or before `pos`. + fn abs_index_at_or_before(&self, pos: Samples, sr: SampleRate, rate: FrameRate) -> i64 { + if matches!(self, GridValue::Timecode(TimecodeValue::Second)) && rate.is_drop() { + let tc = Timecode::from_frames(rate.frames_at(pos, sr), rate); + let magnitude = tc.hours.saturating_mul(3600).saturating_add(tc.minutes.saturating_mul(60)).saturating_add(tc.seconds); + let mut k = if tc.negative { magnitude.saturating_neg() } else { magnitude }; + while self.abs_line(k, sr, rate) > pos { + let previous = k.saturating_sub(1); + if previous == k { + break; + } + k = previous; + } + while self.abs_line(k.saturating_add(1), sr, rate) <= pos { + let next = k.saturating_add(1); + if next == k { + break; + } + k = next; + } + return k; + } + let mut k = if let Some(frames) = self.frame_step(rate) { + let step = (rate.samples_per_frame(sr) * frames).max(1.0); + (pos as f64 / step).floor() as i64 + } else { + match *self { + GridValue::Seconds(s) => (pos as f64 / Self::seconds_step(s, sr)).floor() as i64, + GridValue::Samples(n) => return pos.div_euclid(n.max(1)), + GridValue::Note { .. } => return 0, + GridValue::Frames(_) | GridValue::Timecode(_) | GridValue::FeetFrames(_) => return 0, + } + }; + // Lines are rounded to whole samples, so the floored index can be one off either way. + if self.abs_line(k, sr, rate) > pos { + k = k.saturating_sub(1); + } else if self.abs_line(k.saturating_add(1), sr, rate) <= pos { + k = k.saturating_add(1); + } + k + } + + /// The exact (fractional) Seconds step in samples, at least one sample. + fn seconds_step(s: f64, sr: SampleRate) -> f64 { + (s.clamp(0.000_01, 3600.0) * sr.as_f64()).max(1.0) + } + + /// Maps a signed whole-second timecode label to its physical frame at a drop-frame rate. + fn drop_second_frame(index: i64, rate: FrameRate) -> i64 { + let seconds = index.unsigned_abs(); + let total_minutes = seconds / 60; + let frames = if seconds.is_multiple_of(60) && !total_minutes.is_multiple_of(10) { if rate.nominal() >= 60 { 4 } else { 2 } } else { 0 }; + Timecode { + negative: index < 0, + hours: i64::try_from(seconds / 3600).unwrap_or(i64::MAX), + minutes: i64::try_from((seconds / 60) % 60).unwrap_or(0), + seconds: i64::try_from(seconds % 60).unwrap_or(0), + frames, + } + .to_frames(rate) + } + + /// A frame-based increment as a possibly fractional number of frames. + fn frame_step(&self, rate: FrameRate) -> Option { + match *self { + GridValue::Frames(frames) => Some(frames.clamp(1, 1_000_000) as f64), + GridValue::Timecode(value) => Some(value.frames(rate)), + GridValue::FeetFrames(value) => Some(value.frames()), + _ => None, + } + } + + /// The first grid line strictly after `line`. + pub fn next_line(&self, line: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate) -> Samples { + if let Self::FeetFrames(value) = *self { + return value.next_line(line, sr, rate, 0); + } + if !matches!(self, GridValue::Note { .. }) { + let next = self.abs_line(self.abs_index_at_or_before(line, sr, rate).saturating_add(1), sr, rate); + return if next > line { next } else { line.saturating_add(1) }; + } + let step = self.step_samples(line, sr, map, rate); + let candidate = self.line_at_or_before(line.saturating_add(step), sr, map, rate); + if candidate > line { candidate } else { line.saturating_add(step) } + } + + /// All grid lines within `[start, end)`, capped at `max` lines. + pub fn lines(&self, start: Samples, end: Samples, sr: SampleRate, map: &TempoMap, rate: FrameRate, max: usize) -> Vec { + if let Self::FeetFrames(value) = *self { + return value.lines(start, end, sr, rate, 0, max); + } + let mut out = Vec::new(); + let mut p = self.line_at_or_before(start, sr, map, rate); + while p < end && out.len() < max { + if p >= start { + out.push(p); + } + let n = self.next_line(p, sr, map, rate); + if n <= p { + break; + } + p = n; + } + out + } +} + +/// Moves one position through an ordered increment menu without wrapping. +fn step_enum(values: &[T], value: T, larger: bool) -> Option { + let index = values.iter().position(|candidate| *candidate == value)?; + let next = if larger { index.checked_sub(1) } else { index.checked_add(1) }?; + values.get(next).copied() +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Frame origins are subtracted before rounding whole frames, quarter frames and subframes. + #[test] + fn feet_frames_origins_round_only_after_applying_the_frame_offset() { + let (sr, rate) = (SampleRate::HZ_48000, FrameRate::Fps29_97); + for at in [49_650, 100_901, 177_778] { + assert_eq!(FeetFramesValue::Foot.line_at_or_before(at, sr, rate, 1), at); + assert_eq!(FeetFramesValue::Foot.snap(at - 1, sr, rate, 1), at); + } + assert_eq!(FeetFramesValue::QuarterFrame.snap(50_049, sr, rate, 1), 50_050); + assert_eq!(FeetFramesValue::SubFrame.lines(49_665, 49_667, sr, rate, 1, 4), [49_666]); + } + + /// Dense subframes retain distinct sample lines and remain bounded with extreme signed origins. + #[test] + fn feet_frames_subframes_handle_dense_steps_and_extreme_origins() { + let sr = SampleRate::new(8_000).unwrap(); + let rate = FrameRate::Fps120; + for origin in [i64::MIN, -1, 0, 1, i64::MAX] { + assert_eq!(FeetFramesValue::SubFrame.lines(-3, 4, sr, rate, origin, 20), [-3, -2, -1, 0, 1, 2, 3]); + for at in [i64::MIN, -1, 0, 1, i64::MAX] { + assert_eq!(FeetFramesValue::SubFrame.line_at_or_before(at, sr, rate, origin), at); + assert_eq!(FeetFramesValue::SubFrame.snap(at, sr, rate, origin), at); + } + } + } + + #[test] + fn grid_lines_are_their_own_line_at_odd_tempos() { + let (g, sr, rate) = (GridValue::default(), SampleRate::HZ_48000, FrameRate::Fps30); + for bpm in [90.0, 95.0, 110.0, 130.0, 140.0] { + let map = TempoMap::new(bpm, 4, 4).unwrap(); + let mut line = 0; + for _ in 0..64 { + assert_eq!(g.line_at_or_before(line, sr, &map, rate), line, "{bpm} bpm"); + line = g.next_line(line, sr, &map, rate); + } + } + } + + /// Musical boundaries stay exact across a tempo change while absolute scales remain + /// independent of the tempo map. + #[test] + fn grids_stay_aligned_across_a_tempo_change() { + let (sr, rate) = (SampleRate::HZ_48000, FrameRate::Fps29_97Drop); + let mut map = TempoMap::default(); + map.set_tempo(TICKS_PER_QUARTER * 4, 90.0).unwrap(); + let musical = GridValue::Note { value: NoteValue::Quarter, dotted: false, triplet: false }; + for tick in [0, TICKS_PER_QUARTER * 3, TICKS_PER_QUARTER * 4, TICKS_PER_QUARTER * 5] { + let line = map.tick_to_samples(tick, sr); + assert_eq!(musical.line_at_or_before(line, sr, &map, rate), line); + } + for absolute in [ + GridValue::Seconds(0.5), + GridValue::Timecode(TimecodeValue::Second), + GridValue::FeetFrames(FeetFramesValue::Foot), + GridValue::Samples(1_000), + ] { + let line = absolute.next_line(absolute.line_at_or_before(100_000, sr, &map, rate), sr, &map, rate); + assert_eq!(absolute.line_at_or_before(line, sr, &map, rate), line, "{absolute:?}"); + } + } + + #[test] + fn sixteenth_grid_at_120() { + let g = GridValue::default(); + let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps30); + assert_eq!(g.step_samples(0, sr, &map, rate), 6000); + assert_eq!(g.snap(2900, sr, &map, rate), 0); + assert_eq!(g.snap(3100, sr, &map, rate), 6000); + assert_eq!(g.lines(0, 24_000, sr, &map, rate, 100), vec![0, 6000, 12000, 18000]); + } + + /// Absolute increments report their expected sample lengths at a fixed rate. + #[test] + fn absolute_grids() { + let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps25); + assert_eq!(GridValue::Seconds(1.0).snap(70_000, sr, &map, rate), 48_000); + assert_eq!(GridValue::Frames(1).step_samples(0, sr, &map, rate), 1920); + assert_eq!(GridValue::Timecode(TimecodeValue::Second).step_samples(0, sr, &map, rate), 48_000); + assert_eq!(GridValue::Timecode(TimecodeValue::FiveFrames).step_samples(0, sr, &map, rate), 9_600); + assert_eq!(GridValue::Timecode(TimecodeValue::HalfFrame).step_samples(0, sr, &map, rate), 960); + assert_eq!(GridValue::Timecode(TimecodeValue::QuarterFrame).step_samples(0, sr, &map, rate), 480); + assert_eq!(GridValue::Timecode(TimecodeValue::SubFrame).step_samples(0, sr, &map, rate), 19); + assert_eq!(GridValue::FeetFrames(FeetFramesValue::Foot).step_samples(0, sr, &map, rate), 30_720); + assert_eq!(GridValue::Samples(0).step_samples(0, sr, &map, rate), 1); + } + + /// Bar length follows the meter while a whole note always spans four quarter notes. + #[test] + fn a_bar_and_a_whole_note_differ_in_three_four() { + let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::new(120.0, 3, 4).unwrap(), FrameRate::Fps25); + let note = |value| GridValue::Note { value, dotted: false, triplet: false }; + assert_eq!(note(NoteValue::Bar).step_samples(0, sr, &map, rate), 72_000); + assert_eq!(note(NoteValue::Whole).step_samples(0, sr, &map, rate), 96_000); + assert_eq!(note(NoteValue::Bar).line_at_or_before(96_000, sr, &map, rate), 72_000); + assert_eq!(note(NoteValue::Whole).line_at_or_before(96_000, sr, &map, rate), 96_000); + assert_eq!(note(NoteValue::Half).lines(0, 150_000, sr, &map, rate, 10), [0, 48_000, 72_000, 120_000, 144_000]); + } + + #[test] + fn seconds_grid_lines_land_on_whole_seconds() { + let (sr, map, rate) = (SampleRate::HZ_44100, TempoMap::default(), FrameRate::Fps30); + for g in [GridValue::Seconds(0.001), GridValue::Seconds(0.01)] { + for n in 0..20 { + let s = n * 44_100; + assert_eq!(g.snap(s, sr, &map, rate), s, "{g:?} at {n} s"); + assert_eq!(g.line_at_or_before(s, sr, &map, rate), s, "{g:?} at {n} s"); + } + } + } + + /// Legacy frame grids stay aligned to exact frame starts at fractional rates. + #[test] + fn frame_grid_lines_land_on_frame_starts() { + let sr = SampleRate::HZ_48000; + let map = TempoMap::default(); + for rate in [FrameRate::Fps29_97, FrameRate::Fps23_976] { + for f in [1, 10] { + let g = GridValue::Frames(f); + for k in (0..5000).step_by(37) { + let s = rate.samples_at_frame(k, sr); + let line = rate.samples_at_frame(k.div_euclid(f) * f, sr); + assert_eq!(g.line_at_or_before(s, sr, &map, rate), line, "{rate:?} {f} frames at frame {k}"); + if k % f == 0 { + assert_eq!(g.snap(s, sr, &map, rate), s, "{rate:?} {f} frames at frame {k}"); + } + } + let start = rate.samples_at_frame(4000, sr); + let end = rate.samples_at_frame(4000 + 20 * f, sr); + let want: Vec<_> = (0..20).map(|i| rate.samples_at_frame(4000 + i * f, sr)).collect(); + assert_eq!(g.lines(start, end, sr, &map, rate, 100), want, "{rate:?} {f} frames"); + } + } + } + + /// Named whole- and sub-frame grids retain the exact-multiple calculation used by legacy frame grids. + #[test] + fn named_frame_grids_land_on_exact_subdivisions() { + let sr = SampleRate::HZ_48000; + let map = TempoMap::default(); + let grids = TimecodeValue::ALL.into_iter().map(GridValue::Timecode).chain(FeetFramesValue::ALL.into_iter().map(GridValue::FeetFrames)); + for rate in [FrameRate::Fps29_97, FrameRate::Fps23_976] { + for grid in grids.clone() { + for index in 0..200 { + let line = grid.abs_line(index, sr, rate); + assert_eq!(grid.line_at_or_before(line, sr, &map, rate), line, "{grid:?} {rate:?} line {index}"); + assert_eq!(grid.next_line(line, sr, &map, rate), grid.abs_line(index + 1, sr, rate), "{grid:?} {rate:?} line {index}"); + } + } + } + } + + /// Drop-frame second grids follow labelled second boundaries in both timeline directions. + #[test] + fn drop_frame_second_grids_follow_timecode_labels() { + let sr = SampleRate::HZ_48000; + let map = TempoMap::default(); + let grid = GridValue::Timecode(TimecodeValue::Second); + for rate in [FrameRate::Fps29_97Drop, FrameRate::Fps59_94Drop] { + for seconds in [0, 59, 60, 61, 600, 601, 3600] { + let frame = GridValue::drop_second_frame(seconds, rate); + let line = rate.samples_at_frame(frame, sr); + assert_eq!(grid.line_at_or_before(line, sr, &map, rate), line, "{rate:?} second {seconds}"); + assert_eq!(grid.line_at_or_before(-line, sr, &map, rate), -line, "{rate:?} second -{seconds}"); + for offset in -2..=rate.nominal() + 2 { + let position = rate.samples_at_frame(frame.saturating_add(offset), sr); + let before = grid.line_at_or_before(position, sr, &map, rate); + assert!(before <= position, "{rate:?} second {seconds}, frame offset {offset}"); + assert!(grid.next_line(before, sr, &map, rate) > position, "{rate:?} second {seconds}, frame offset {offset}"); + } + } + let labelled = Timecode { negative: false, hours: 0, minutes: 1, seconds: 1, frames: 0 }.to_samples(rate, sr); + assert_eq!(grid.snap(labelled, sr, &map, rate), labelled); + } + + for (rate, minute_frame) in [(FrameRate::Fps29_97Drop, 1800), (FrameRate::Fps59_94Drop, 3600)] { + let boundary = rate.samples_at_frame(minute_frame, sr); + assert_eq!(grid.line_at_or_before(boundary, sr, &map, rate), boundary, "{rate:?} positive minute boundary"); + assert_eq!(grid.snap(boundary, sr, &map, rate), boundary, "{rate:?} positive minute snap"); + assert_eq!(grid.line_at_or_before(-boundary, sr, &map, rate), -boundary, "{rate:?} negative minute boundary"); + assert_eq!(grid.snap(-boundary, sr, &map, rate), -boundary, "{rate:?} negative minute snap"); + } + } + + #[test] + fn lines_are_capped() { + let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps25); + assert_eq!(GridValue::Samples(1).lines(0, 1_000_000, sr, &map, rate, 50).len(), 50); + } + + #[test] + fn triplets_and_dots() { + let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps25); + let t = GridValue::Note { value: NoteValue::Quarter, dotted: false, triplet: true }; + assert_eq!(t.step_samples(0, sr, &map, rate), 16_000); + let d = GridValue::Note { value: NoteValue::Quarter, dotted: true, triplet: false }; + assert_eq!(d.step_samples(0, sr, &map, rate), 36_000); + } + + /// Stepping follows each menu ladder and stops at its endpoints. + #[test] + fn values_step_to_the_next_larger_or_smaller() { + let note = |value, triplet| GridValue::Note { value, dotted: false, triplet }; + assert_eq!(note(NoteValue::Sixteenth, false).stepped(true), note(NoteValue::Eighth, false)); + assert_eq!(note(NoteValue::Eighth, true).stepped(false), note(NoteValue::Sixteenth, true)); + assert_eq!(note(NoteValue::Bar, false).stepped(true), note(NoteValue::Bar, false)); + assert_eq!(note(NoteValue::SixtyFourth, false).stepped(false), note(NoteValue::SixtyFourth, false)); + assert_eq!(GridValue::Seconds(0.01).stepped(true), GridValue::Seconds(0.1)); + assert_eq!(GridValue::Seconds(0.5).stepped(false), GridValue::Seconds(0.1)); + assert_eq!(GridValue::Seconds(1.0).stepped(true), GridValue::Seconds(1.0)); + assert_eq!(GridValue::Frames(1).stepped(true), GridValue::Frames(10)); + assert_eq!(GridValue::Timecode(TimecodeValue::Frame).stepped(false), GridValue::Timecode(TimecodeValue::HalfFrame)); + assert_eq!(GridValue::FeetFrames(FeetFramesValue::Frame).stepped(true), GridValue::FeetFrames(FeetFramesValue::Foot)); + assert_eq!(GridValue::Samples(100).stepped(false), GridValue::Samples(10)); + assert_eq!(GridValue::Samples(1).stepped(true), GridValue::Samples(2)); + assert_eq!(GridValue::Samples(1).stepped(false), GridValue::Samples(1)); + } + + #[test] + fn snap_at_extreme_position_does_not_overflow() { + let sr = SampleRate::new(8000).unwrap(); + let map = TempoMap::new(1000.0, 4, 4).unwrap(); + let _ = GridValue::default().snap(i64::MAX, sr, &map, FrameRate::Fps30); + } +} diff --git a/crates/time/src/soundcraft/mod.rs b/crates/time/src/soundcraft/mod.rs new file mode 100644 index 000000000..bd4df3aa9 --- /dev/null +++ b/crates/time/src/soundcraft/mod.rs @@ -0,0 +1,155 @@ +//! SoundCraft timebases. +//! +//! Every position in a session is an absolute sample index (`i64`) at the session sample rate. +//! This crate converts those positions to and from the five Pro Tools-style timebases +//! (Bars|Beats, Min:Secs, Timecode, Feet+Frames, Samples) through a [`TempoMap`], and computes +//! grid lines and snapping for the Grid / Nudge values. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +mod format; +mod grid; +mod tempo; +mod timecode; + +pub use format::{TimeFormat, format_length, format_position, parse_position}; +pub use grid::{FeetFramesValue, GridScale, GridValue, NoteValue, TimecodeValue}; +pub use tempo::{BarBeat, MeterEvent, TICKS_PER_QUARTER, TempoEvent, TempoMap}; +pub use timecode::{FrameRate, Timecode}; + +/// A position or length in samples. +pub type Samples = i64; + +/// Largest position accepted from parameters and files: 2^40 samples (about 265 days at 48 kHz). +/// Clamping there keeps every later addition and subtraction on positions far away from overflow. +pub const MAX_POSITION: Samples = 1 << 40; + +/// Errors from parsing or converting time values. +#[derive(Debug, Clone, PartialEq, thiserror::Error)] +pub enum TimeError { + #[error("cannot parse `{0}` as a {1} value")] + Parse(String, &'static str), + #[error("invalid sample rate {0}")] + SampleRate(u32), + #[error("invalid tempo {0}")] + Tempo(f64), + #[error("invalid meter {0}/{1}")] + Meter(u32, u32), +} + +/// Sample rates supported by sessions (Pro Tools offers 44.1 k – 192 k; we accept 8 k – 768 k). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub struct SampleRate(u32); + +impl SampleRate { + pub const HZ_44100: SampleRate = SampleRate(44_100); + pub const HZ_48000: SampleRate = SampleRate(48_000); + pub const COMMON: [u32; 8] = [44_100, 48_000, 88_200, 96_000, 176_400, 192_000, 352_800, 384_000]; + + pub fn new(hz: u32) -> Result { + if (8_000..=768_000).contains(&hz) { Ok(SampleRate(hz)) } else { Err(TimeError::SampleRate(hz)) } + } + pub fn hz(self) -> u32 { + self.0 + } + pub fn as_f64(self) -> f64 { + f64::from(self.0) + } + /// Samples → seconds. + pub fn seconds(self, s: Samples) -> f64 { + s as f64 / self.as_f64() + } + /// Seconds → samples, rounded to nearest and saturated. + pub fn samples(self, seconds: f64) -> Samples { + to_samples(seconds * self.as_f64()) + } +} + +impl Default for SampleRate { + fn default() -> Self { + SampleRate::HZ_48000 + } +} + +/// Round a float sample count to `i64`, mapping NaN to 0 and saturating infinities. +pub fn to_samples(v: f64) -> Samples { + if v.is_nan() { + 0 + } else if v >= i64::MAX as f64 { + i64::MAX + } else if v <= i64::MIN as f64 { + i64::MIN + } else { + v.round() as i64 + } +} + +/// A half-open sample range `[start, end)`. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub struct Range { + pub start: Samples, + pub end: Samples, +} + +impl Range { + pub fn new(a: Samples, b: Samples) -> Self { + if a <= b { Range { start: a, end: b } } else { Range { start: b, end: a } } + } + pub fn point(at: Samples) -> Self { + Range { start: at, end: at } + } + pub fn len(&self) -> Samples { + self.end.saturating_sub(self.start) + } + pub fn is_empty(&self) -> bool { + self.end <= self.start + } + pub fn contains(&self, s: Samples) -> bool { + s >= self.start && s < self.end + } + pub fn overlaps(&self, o: &Range) -> bool { + self.start < o.end && o.start < self.end + } + pub fn intersect(&self, o: &Range) -> Option { + let s = self.start.max(o.start); + let e = self.end.min(o.end); + (s < e).then_some(Range { start: s, end: e }) + } + pub fn shifted(&self, by: Samples) -> Range { + Range { start: self.start.saturating_add(by), end: self.end.saturating_add(by) } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sample_rate_bounds() { + assert!(SampleRate::new(48_000).is_ok()); + assert!(SampleRate::new(0).is_err()); + assert!(SampleRate::new(10_000_000).is_err()); + } + + #[test] + fn seconds_round_trip() { + let sr = SampleRate::HZ_44100; + assert_eq!(sr.samples(1.0), 44_100); + assert!((sr.seconds(22_050) - 0.5).abs() < 1e-12); + } + + #[test] + fn to_samples_is_total() { + assert_eq!(to_samples(f64::NAN), 0); + assert_eq!(to_samples(f64::INFINITY), i64::MAX); + assert_eq!(to_samples(f64::NEG_INFINITY), i64::MIN); + } + + #[test] + fn range_ops() { + let a = Range::new(10, 0); + assert_eq!(a, Range { start: 0, end: 10 }); + assert_eq!(a.intersect(&Range::new(5, 20)), Some(Range::new(5, 10))); + assert_eq!(a.intersect(&Range::new(10, 20)), None); + assert!(a.contains(9) && !a.contains(10)); + } +} diff --git a/crates/time/src/soundcraft/tempo.rs b/crates/time/src/soundcraft/tempo.rs new file mode 100644 index 000000000..10f44b8dc --- /dev/null +++ b/crates/time/src/soundcraft/tempo.rs @@ -0,0 +1,622 @@ +//! Tempo and meter maps: the conversion between musical time (ticks) and samples. + +use super::{MAX_POSITION, SampleRate, Samples, TimeError, to_samples}; + +/// Ticks per quarter note (Pro Tools' resolution). +pub const TICKS_PER_QUARTER: i64 = 960; + +/// A tempo change at a tick position. `bpm` is quarter notes per minute. +#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct TempoEvent { + pub tick: i64, + pub bpm: f64, +} + +/// A meter (time signature) change at a tick position, which must fall on a bar line. +#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct MeterEvent { + pub tick: i64, + pub numerator: u32, + pub denominator: u32, +} + +impl MeterEvent { + pub fn ticks_per_beat(&self) -> i64 { + let d = i64::from(self.denominator.max(1)); + (TICKS_PER_QUARTER * 4 / d).max(1) + } + pub fn ticks_per_bar(&self) -> i64 { + self.ticks_per_beat().saturating_mul(i64::from(self.numerator.max(1))) + } +} + +/// A musical position: 1-based bar and beat plus ticks within the beat. +#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct BarBeat { + pub bar: i64, + pub beat: i64, + pub tick: i64, +} + +/// Tempo and meter changes for a session. Always has at least one of each, at tick 0. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +#[serde(from = "TempoMapData")] +pub struct TempoMap { + tempos: Vec, + meters: Vec, + /// For each tempo event, the elapsed time at its tick in quarter-note minutes (ticks / bpm, + /// summed over the earlier segments). Lets tick/sample conversions binary-search the segment + /// instead of walking every earlier tempo change, so converting many positions against a large + /// tempo map stays O(log n) each. Rebuilt whenever `tempos` changes. + #[serde(skip)] + elapsed: Vec, +} + +/// The serialized form of [`TempoMap`], without the derived lookup table. +#[derive(serde::Deserialize)] +struct TempoMapData { + tempos: Vec, + meters: Vec, +} + +impl From for TempoMap { + fn from(d: TempoMapData) -> Self { + TempoMap::from_parts(d.tempos, d.meters) + } +} + +impl Default for TempoMap { + fn default() -> Self { + TempoMap::from_parts(vec![TempoEvent { tick: 0, bpm: 120.0 }], vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }]) + } +} + +fn valid_bpm(bpm: f64) -> Result { + if bpm.is_finite() && (5.0..=1000.0).contains(&bpm) { Ok(bpm) } else { Err(TimeError::Tempo(bpm)) } +} + +fn valid_meter(numerator: u32, denominator: u32) -> bool { + (1..=64).contains(&numerator) && [1, 2, 4, 8, 16, 32, 64].contains(&denominator) +} + +impl TempoMap { + fn from_parts(tempos: Vec, meters: Vec) -> Self { + let mut m = TempoMap { tempos, meters, elapsed: Vec::new() }; + m.reindex(); + m + } + + /// Rebuild `elapsed` from `tempos`. The first segment runs from tick 0 at the first tempo. + fn reindex(&mut self) { + let mut elapsed = Vec::with_capacity(self.tempos.len()); + let mut acc = 0.0; + let mut seg_tick = 0i64; + let mut bpm = self.tempos.first().map_or(120.0, |e| e.bpm); + for (i, e) in self.tempos.iter().enumerate() { + if i > 0 { + // Saturating: a file's ticks are indexed before `sanitize` orders them. + acc += e.tick.saturating_sub(seg_tick) as f64 / bpm; + seg_tick = e.tick; + bpm = e.bpm; + } + elapsed.push(acc); + } + self.elapsed = elapsed; + } + + /// Samples per quarter-note minute (the unit of `elapsed`) at `sr`. + fn samples_per_elapsed(sr: SampleRate) -> f64 { + sr.as_f64() * 60.0 / TICKS_PER_QUARTER as f64 + } + + /// The tempo segment `i` (an index into `tempos`, 0 when it is empty): its start tick (0 for + /// the first), bpm and elapsed time at its start. + fn segment(&self, i: usize) -> (i64, f64, f64) { + let bpm = self.tempos.get(i).or(self.tempos.first()).map_or(120.0, |e| e.bpm); + let tick = if i == 0 { 0 } else { self.tempos.get(i).map_or(0, |e| e.tick) }; + (tick, bpm, self.elapsed.get(i).copied().unwrap_or(0.0)) + } + + pub fn new(bpm: f64, numerator: u32, denominator: u32) -> Result { + let mut m = TempoMap::default(); + m.set_tempo(0, bpm)?; + m.set_meter(0, numerator, denominator)?; + Ok(m) + } + + pub fn tempos(&self) -> &[TempoEvent] { + &self.tempos + } + pub fn meters(&self) -> &[MeterEvent] { + &self.meters + } + + /// Repair a map read from a file: only valid tempos and meters within `0..=MAX_POSITION` ticks, in + /// tick order, with one of each at tick 0. + pub fn sanitize(&mut self) { + self.tempos.retain(|e| (0..=MAX_POSITION).contains(&e.tick) && valid_bpm(e.bpm).is_ok()); + self.tempos.sort_by_key(|e| e.tick); + self.tempos.dedup_by_key(|e| e.tick); + if self.tempos.first().is_none_or(|e| e.tick > 0) { + let bpm = self.tempos.first().map_or(120.0, |e| e.bpm); + self.tempos.insert(0, TempoEvent { tick: 0, bpm }); + } + self.meters.retain(|m| (0..=MAX_POSITION).contains(&m.tick) && valid_meter(m.numerator, m.denominator)); + self.meters.sort_by_key(|m| m.tick); + self.meters.dedup_by_key(|m| m.tick); + if self.meters.first().is_none_or(|m| m.tick > 0) { + let (numerator, denominator) = self.meters.first().map_or((4, 4), |m| (m.numerator, m.denominator)); + self.meters.insert(0, MeterEvent { tick: 0, numerator, denominator }); + } + self.reindex(); + } + + /// Insert or replace the tempo at `tick`. + pub fn set_tempo(&mut self, tick: i64, bpm: f64) -> Result<(), TimeError> { + let bpm = valid_bpm(bpm)?; + let tick = tick.max(0); + match self.tempos.binary_search_by(|e| e.tick.cmp(&tick)) { + Ok(i) => { + if let Some(e) = self.tempos.get_mut(i) { + e.bpm = bpm; + } + } + Err(i) => self.tempos.insert(i, TempoEvent { tick, bpm }), + } + self.reindex(); + Ok(()) + } + + /// Remove a tempo event (the one at tick 0 is kept). + pub fn remove_tempo(&mut self, tick: i64) -> bool { + if tick == 0 { + return false; + } + let before = self.tempos.len(); + self.tempos.retain(|e| e.tick != tick); + self.reindex(); + before != self.tempos.len() + } + + /// Insert or replace the meter at `tick` (snapped to the bar line at or before it). + /// Later meter changes keep their bar numbers, so they stay on bar lines. + pub fn set_meter(&mut self, tick: i64, numerator: u32, denominator: u32) -> Result<(), TimeError> { + if !valid_meter(numerator, denominator) { + return Err(TimeError::Meter(numerator, denominator)); + } + let tick = if tick <= 0 { 0 } else { self.bar_start_tick(self.bar_beat_at_tick(tick).bar) }; + let later = self.take_meters_after(tick); + self.put_meter(MeterEvent { tick, numerator, denominator }); + self.restore_meters_at_bars(later); + Ok(()) + } + + /// Remove a meter event (the one at tick 0 is kept). Later meter changes keep their bar numbers. + pub fn remove_meter(&mut self, tick: i64) -> bool { + if tick == 0 || !self.meters.iter().any(|e| e.tick == tick) { + return false; + } + let later = self.take_meters_after(tick); + self.meters.retain(|e| e.tick != tick); + self.restore_meters_at_bars(later); + true + } + + fn put_meter(&mut self, ev: MeterEvent) { + match self.meters.binary_search_by(|e| e.tick.cmp(&ev.tick)) { + Ok(i) => { + if let Some(e) = self.meters.get_mut(i) { + *e = ev; + } + } + Err(i) => self.meters.insert(i, ev), + } + } + + /// Remove the meters after `tick`, returning each with the bar it starts: the bar + /// [`Self::bar_beat_at_tick`] gives its tick, counted in one pass over the map rather than one + /// walk per meter, so a session with many meter changes edits in linear time. + fn take_meters_after(&mut self, tick: i64) -> Vec<(i64, MeterEvent)> { + let mut later = Vec::new(); + let (mut bar, mut seg_tick) = (1i64, 0i64); + for (i, m) in self.meters.iter().enumerate() { + let tpb = m.ticks_per_bar(); + if m.tick > tick { + later.push((bar.saturating_add(m.tick.saturating_sub(seg_tick) / tpb), *m)); + } + if let Some(n) = self.meters.get(i + 1) { + bar = bar.saturating_add(ceil_div(n.tick.saturating_sub(seg_tick), tpb)); + seg_tick = n.tick; + } + } + self.meters.retain(|e| e.tick <= tick); + later + } + + /// The bar and tick of the last meter's segment, where [`Self::bar_start_tick`]'s walk ends. + fn last_segment(&self) -> (i64, i64) { + let (mut cur_bar, mut cur_tick) = (1i64, 0i64); + for (i, m) in self.meters.iter().enumerate() { + if let Some(n) = self.meters.get(i + 1) { + cur_bar = cur_bar.saturating_add(ceil_div(n.tick.saturating_sub(cur_tick), m.ticks_per_bar())); + cur_tick = n.tick; + } + } + (cur_bar, cur_tick) + } + + /// Put meters back at the start of their bars under the current earlier meters, in bar order. + /// Each lands at or past the last meter, so the walk to its bar resumes from there; a bar + /// before it (meters off their bar lines) takes the full walk. + fn restore_meters_at_bars(&mut self, later: Vec<(i64, MeterEvent)>) { + let (mut cur_bar, mut cur_tick) = self.last_segment(); + for (bar, ev) in later { + let tick = if bar >= cur_bar { + let tpb = self.meters.last().map_or(MeterEvent { tick: 0, numerator: 4, denominator: 4 }, |m| *m).ticks_per_bar(); + cur_tick.saturating_add(bar.saturating_sub(cur_bar).saturating_mul(tpb)) + } else { + self.bar_start_tick(bar) + }; + self.put_meter(MeterEvent { tick, ..ev }); + (cur_bar, cur_tick) = if bar >= cur_bar { (bar, tick) } else { self.last_segment() }; + } + } + + /// Tempo in effect at `tick`. + pub fn tempo_at_tick(&self, tick: i64) -> f64 { + let mut bpm = self.tempos.first().map_or(120.0, |e| e.bpm); + for e in &self.tempos { + if e.tick <= tick { + bpm = e.bpm; + } else { + break; + } + } + bpm + } + + /// Meter in effect at `tick`. + pub fn meter_at_tick(&self, tick: i64) -> MeterEvent { + let mut m = self.meters.first().copied().unwrap_or(MeterEvent { tick: 0, numerator: 4, denominator: 4 }); + for e in &self.meters { + if e.tick <= tick { + m = *e; + } else { + break; + } + } + m + } + + fn samples_per_tick(bpm: f64, sr: SampleRate) -> f64 { + sr.as_f64() * 60.0 / (bpm * TICKS_PER_QUARTER as f64) + } + + /// Ticks → samples (exact for constant-tempo segments). + pub fn tick_to_samples(&self, tick: i64, sr: SampleRate) -> Samples { + to_samples(self.tick_to_samples_f(tick, sr)) + } + + fn tick_to_samples_f(&self, tick: i64, sr: SampleRate) -> f64 { + // Tempo events after the first that start before `tick` (the segments it has passed). + let passed = self.tempos.get(1..).map_or(0, |rest| rest.partition_point(|e| e.tick < tick)); + let (seg_tick, bpm, elapsed) = self.segment(passed); + elapsed * Self::samples_per_elapsed(sr) + (tick - seg_tick) as f64 * Self::samples_per_tick(bpm, sr) + } + + /// Samples → ticks (fractional). + pub fn samples_to_ticks_f(&self, s: Samples, sr: SampleRate) -> f64 { + let s = s as f64; + let scale = Self::samples_per_elapsed(sr); + // Tempo events after the first that start at or before `s`. + let passed = self.elapsed.get(1..).map_or(0, |rest| rest.partition_point(|e| e * scale <= s)); + let (seg_tick, bpm, elapsed) = self.segment(passed); + seg_tick as f64 + (s - elapsed * scale) / Self::samples_per_tick(bpm, sr) + } + + pub fn samples_to_ticks(&self, s: Samples, sr: SampleRate) -> i64 { + to_samples(self.samples_to_ticks_f(s, sr)) + } + + /// Tick of the first beat of `bar` (1-based; bars before 1 extend the first meter backwards). + pub fn bar_start_tick(&self, bar: i64) -> i64 { + let mut cur_bar = 1i64; + let mut cur_tick = 0i64; + let first = self.meter_at_tick(0); + if bar < 1 { + return bar.saturating_sub(1).saturating_mul(first.ticks_per_bar()); + } + for (i, m) in self.meters.iter().enumerate() { + let next_tick = self.meters.get(i + 1).map(|n| n.tick); + let tpb = m.ticks_per_bar(); + let bars_in_seg = next_tick.map(|nt| ceil_div(nt.saturating_sub(cur_tick), tpb)); + match bars_in_seg { + Some(n) if bar >= cur_bar.saturating_add(n) => { + cur_bar = cur_bar.saturating_add(n); + cur_tick = next_tick.unwrap_or(cur_tick); + } + _ => return cur_tick.saturating_add((bar - cur_bar).saturating_mul(tpb)), + } + } + cur_tick + } + + /// Bar|beat|tick at `tick`. + pub fn bar_beat_at_tick(&self, tick: i64) -> BarBeat { + let first = self.meter_at_tick(0); + if tick < 0 { + let tpb = first.ticks_per_bar(); + let abs = tick.unsigned_abs(); + let tpb_u = tpb.unsigned_abs(); + let bars_back_u = abs / tpb_u + u64::from(!abs.is_multiple_of(tpb_u)); + let bars_back = i64::try_from(bars_back_u).unwrap_or(i64::MAX); + // Offset of `tick` inside its bar, without negating `i64::MIN`. + let within = ((tpb_u - abs % tpb_u) % tpb_u) as i64; + let beat_t = first.ticks_per_beat(); + return BarBeat { bar: 1 - bars_back, beat: within / beat_t + 1, tick: within % beat_t }; + } + let mut bar = 1i64; + let mut seg_tick = 0i64; + for (i, m) in self.meters.iter().enumerate() { + let tpb = m.ticks_per_bar(); + if let Some(n) = self.meters.get(i + 1) + && tick >= n.tick + { + bar = bar.saturating_add(ceil_div(n.tick.saturating_sub(seg_tick), tpb)); + seg_tick = n.tick; + continue; + } + let rel = tick - seg_tick; + let bars = rel / tpb; + let within = rel % tpb; + let beat_t = m.ticks_per_beat(); + return BarBeat { bar: bar + bars, beat: within / beat_t + 1, tick: within % beat_t }; + } + BarBeat { bar, beat: 1, tick: 0 } + } + + /// Bar|beat|tick → tick. + pub fn tick_at_bar_beat(&self, bb: BarBeat) -> i64 { + let start = self.bar_start_tick(bb.bar); + let m = self.meter_at_tick(start); + start.saturating_add(bb.beat.saturating_sub(1).max(0).saturating_mul(m.ticks_per_beat())).saturating_add(bb.tick) + } + + pub fn bar_beat_at(&self, s: Samples, sr: SampleRate) -> BarBeat { + self.bar_beat_at_tick(self.samples_to_ticks(s, sr)) + } + + pub fn samples_at_bar_beat(&self, bb: BarBeat, sr: SampleRate) -> Samples { + self.tick_to_samples(self.tick_at_bar_beat(bb), sr) + } + + /// Scale every tempo by `factor` (Tempo Operations › Scale). + pub fn scale_tempos(&mut self, factor: f64) -> Result<(), TimeError> { + let mut out = self.tempos.clone(); + for e in &mut out { + e.bpm = valid_bpm(e.bpm * factor)?; + } + self.tempos = out; + self.reindex(); + Ok(()) + } +} + +/// Ceiling division for a non-negative `num` and positive `den`, without overflow. +fn ceil_div(num: i64, den: i64) -> i64 { + num / den + i64::from(num % den != 0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn default_map_is_120_4_4() { + let m = TempoMap::default(); + let sr = SampleRate::HZ_48000; + // One bar of 4/4 at 120 bpm = 2 seconds. + assert_eq!(m.samples_at_bar_beat(BarBeat { bar: 2, beat: 1, tick: 0 }, sr), 96_000); + assert_eq!(m.bar_beat_at(96_000, sr), BarBeat { bar: 2, beat: 1, tick: 0 }); + assert_eq!(m.bar_beat_at(24_000, sr), BarBeat { bar: 1, beat: 2, tick: 0 }); + } + + #[test] + fn meter_near_max_tick_does_not_overflow() { + let mut m = TempoMap::default(); + m.set_meter(i64::MAX, 7, 8).unwrap(); + let bb = m.bar_beat_at_tick(i64::MAX); + assert!(bb.bar > 1); + assert!(m.bar_start_tick(bb.bar) > 0); + assert!(m.bar_start_tick(i64::MAX) > 0); + } + + #[test] + fn minimum_bar_saturates_instead_of_overflowing() { + let m = TempoMap::default(); + assert_eq!(m.bar_start_tick(i64::MIN), i64::MIN); + assert_eq!(m.bar_start_tick(0), -TICKS_PER_QUARTER * 4); + } + + #[test] + fn minimum_beat_is_clamped_without_overflow() { + let m = TempoMap::default(); + assert_eq!(m.tick_at_bar_beat(BarBeat { bar: 1, beat: i64::MIN, tick: 0 }), 0); + } + + #[test] + fn bar_beat_at_min_tick_does_not_overflow() { + let m = TempoMap::default(); + let bb = m.bar_beat_at_tick(i64::MIN); + assert!(bb.bar < 1); + assert!(bb.beat >= 1 && bb.tick >= 0); + assert_eq!(m.bar_beat_at_tick(-1).bar, 0); + } + + #[test] + fn tempo_change_is_integrated() { + let mut m = TempoMap::default(); + let sr = SampleRate::HZ_48000; + m.set_tempo(TICKS_PER_QUARTER * 4, 60.0).unwrap(); + // Bar 1 at 120 = 96000 samples; bar 2 at 60 = 192000 samples. + assert_eq!(m.tick_to_samples(TICKS_PER_QUARTER * 8, sr), 96_000 + 192_000); + assert_eq!(m.samples_to_ticks(96_000 + 192_000, sr), TICKS_PER_QUARTER * 8); + } + + #[test] + fn meter_change_counts_bars() { + let mut m = TempoMap::default(); + // 2 bars of 4/4 then 3/4. + m.set_meter(TICKS_PER_QUARTER * 8, 3, 4).unwrap(); + assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER * 8), BarBeat { bar: 3, beat: 1, tick: 0 }); + assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER * 11), BarBeat { bar: 4, beat: 1, tick: 0 }); + assert_eq!(m.bar_start_tick(4), TICKS_PER_QUARTER * 11); + let bb = BarBeat { bar: 4, beat: 2, tick: 10 }; + assert_eq!(m.bar_beat_at_tick(m.tick_at_bar_beat(bb)), bb); + } + + #[test] + fn editing_an_early_meter_keeps_later_bars_and_scales_linearly() { + // 200 000 meter changes, one per bar, built directly: a walk per meter would take + // minutes to edit here, and even to build through `set_meter`. + let mut m = TempoMap::new(120.0, 4, 4).unwrap(); + let mut tick = 0; + for bar in 1..200_000i64 { + let numerator = 3 + (bar % 2) as u32; + tick += m.meters.last().map_or(0, MeterEvent::ticks_per_bar); + m.meters.push(MeterEvent { tick, numerator, denominator: 4 }); + } + let sample = [1usize, 1_000, 199_998]; + let bars = |m: &TempoMap| sample.map(|i| m.bar_beat_at_tick(m.meters[i].tick).bar); + assert_eq!(bars(&m), [2, 1_001, 199_999]); + m.set_meter(0, 7, 8).unwrap(); + assert_eq!(bars(&m), [2, 1_001, 199_999], "later meters keep their bar numbers"); + assert_eq!(m.meters.len(), 200_000); + assert!(m.meters.windows(2).all(|w| w[0].tick < w[1].tick)); + assert!(m.remove_meter(m.meters[1].tick)); + assert_eq!([1usize, 999, 199_997].map(|i| m.bar_beat_at_tick(m.meters[i].tick).bar), [3, 1_001, 199_999]); + // Meters off their bar lines (counted as bars 2 and 4) land where they always did: on + // bar 2 of the new meter, then two bars of their own later. + let mut off = TempoMap::new(120.0, 4, 4).unwrap(); + off.meters.push(MeterEvent { tick: 100, numerator: 3, denominator: 4 }); + off.meters.push(MeterEvent { tick: 5_000, numerator: 5, denominator: 4 }); + off.set_meter(0, 2, 4).unwrap(); + let placed: Vec<(i64, u32)> = off.meters.iter().map(|e| (e.tick, e.numerator)).collect(); + assert_eq!(placed, vec![(0, 2), (2 * TICKS_PER_QUARTER, 3), (8 * TICKS_PER_QUARTER, 5)]); + } + + #[test] + fn eighth_note_meters() { + let m = TempoMap::new(120.0, 6, 8).unwrap(); + assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER * 3), BarBeat { bar: 2, beat: 1, tick: 0 }); + assert_eq!(m.bar_beat_at_tick(TICKS_PER_QUARTER / 2), BarBeat { bar: 1, beat: 2, tick: 0 }); + } + + #[test] + fn sanitize_keeps_what_the_setters_accept() { + let tempo = |tick, bpm| TempoEvent { tick, bpm }; + let meter = |tick, numerator, denominator| MeterEvent { tick, numerator, denominator }; + let mut m = TempoMap::from_parts( + vec![tempo(960, 4e9), tempo(3840, 90.0), tempo(-5, 100.0), tempo(1920, 60.0), tempo(1920, 70.0)], + vec![meter(0, 0, 4), meter(3840, 3, 4)], + ); + m.sanitize(); + assert_eq!(m.tempos(), [tempo(0, 60.0), tempo(1920, 60.0), tempo(3840, 90.0)]); + assert_eq!(m.meters(), [meter(0, 3, 4), meter(3840, 3, 4)]); + let mut empty = TempoMap::from_parts(Vec::new(), Vec::new()); + empty.sanitize(); + assert_eq!(empty, TempoMap::default()); + } + + #[test] + fn sanitize_drops_events_beyond_the_position_range() { + let mut m = TempoMap::from_parts( + vec![TempoEvent { tick: 0, bpm: 120.0 }, TempoEvent { tick: i64::MAX, bpm: 90.0 }], + vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }, MeterEvent { tick: i64::MAX, numerator: 3, denominator: 4 }], + ); + m.sanitize(); + assert_eq!(m, TempoMap::default()); + assert_eq!(m.bar_start_tick(2), TICKS_PER_QUARTER * 4); + } + + #[test] + fn rejects_bad_values() { + let mut m = TempoMap::default(); + assert!(m.set_tempo(0, f64::NAN).is_err()); + assert!(m.set_tempo(0, 0.0).is_err()); + assert!(m.set_meter(0, 0, 4).is_err()); + assert!(m.set_meter(0, 4, 3).is_err()); + assert!(!m.remove_tempo(0)); + } + + #[test] + fn negative_positions() { + let m = TempoMap::default(); + assert_eq!(m.bar_beat_at_tick(-TICKS_PER_QUARTER * 4), BarBeat { bar: 0, beat: 1, tick: 0 }); + assert_eq!(m.bar_beat_at_tick(-TICKS_PER_QUARTER), BarBeat { bar: 0, beat: 4, tick: 0 }); + } + + /// The segment walk the indexed conversions replaced, kept as the reference they must match. + fn linear_tick_to_samples(m: &TempoMap, tick: i64, sr: SampleRate) -> i64 { + let mut acc = 0.0; + let mut seg_tick = 0i64; + let mut bpm = m.tempos[0].bpm; + for e in m.tempos.iter().skip(1) { + if e.tick >= tick { + break; + } + acc += (e.tick - seg_tick) as f64 * TempoMap::samples_per_tick(bpm, sr); + seg_tick = e.tick; + bpm = e.bpm; + } + to_samples(acc + (tick - seg_tick) as f64 * TempoMap::samples_per_tick(bpm, sr)) + } + + #[test] + fn indexed_conversions_match_the_segment_walk() { + let sr = SampleRate::HZ_48000; + let mut m = TempoMap::default(); + for i in 1..200i64 { + m.set_tempo(i * 487, 40.0 + ((i * 37) % 300) as f64).unwrap(); + } + assert!(m.remove_tempo(487 * 50)); + for tick in (-1000..200 * 487 + 5000).step_by(113) { + let s = m.tick_to_samples(tick, sr); + assert_eq!(s, linear_tick_to_samples(&m, tick, sr), "tick {tick}"); + assert!((m.samples_to_ticks(s, sr) - tick).abs() <= 1, "tick {tick}"); + } + let json = serde_json::to_string(&m).unwrap(); + assert!(!json.contains("elapsed")); + let back: TempoMap = serde_json::from_str(&json).unwrap(); + assert_eq!(back, m); + assert_eq!(back.tick_to_samples(150 * 487 + 3, sr), m.tick_to_samples(150 * 487 + 3, sr)); + } + + #[test] + fn a_file_with_wild_ticks_is_indexed_without_overflow() { + let json = r#"{"tempos":[{"tick":0,"bpm":120.0},{"tick":-9223372036854775808,"bpm":100.0},{"tick":9223372036854775807,"bpm":90.0}],"meters":[{"tick":0,"numerator":4,"denominator":4}]}"#; + let mut m: TempoMap = serde_json::from_str(json).unwrap(); + m.sanitize(); + assert_eq!(m.tempos().first().map(|e| e.tick), Some(0)); + assert_eq!(m.elapsed.len(), m.tempos().len()); + } + + #[test] + fn conversions_against_a_large_tempo_map_stay_fast() { + // 200k tempo changes, then 200k conversions each way: quadratic if every conversion walks + // the earlier segments, well under a second when it binary-searches them. + let sr = SampleRate::HZ_48000; + let n = 200_000i64; + let tempos = (0..n).map(|i| TempoEvent { tick: i * 10, bpm: 60.0 + (i % 7) as f64 }).collect(); + let mut m = TempoMap::from_parts(tempos, vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }]); + m.sanitize(); + let start = std::time::Instant::now(); + let mut sum = 0i64; + for i in 0..n { + let s = m.tick_to_samples(i * 10 + 5, sr); + sum = sum.wrapping_add(s).wrapping_add(m.samples_to_ticks(s, sr)); + } + assert!(sum != 0); + assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed()); + } +} diff --git a/crates/time/src/soundcraft/timecode.rs b/crates/time/src/soundcraft/timecode.rs new file mode 100644 index 000000000..03eb1cc94 --- /dev/null +++ b/crates/time/src/soundcraft/timecode.rs @@ -0,0 +1,309 @@ +//! SMPTE timecode and Feet+Frames. + +use super::{SampleRate, Samples, TimeError}; + +/// Session timecode rates. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum FrameRate { + Fps23_976, + #[default] + Fps24, + Fps25, + Fps29_97, + Fps29_97Drop, + Fps30, + Fps47_952, + Fps48, + Fps50, + Fps59_94, + Fps59_94Drop, + Fps60, + Fps100, + Fps119_88, + Fps120, +} + +impl FrameRate { + pub const ALL: [FrameRate; 15] = [ + FrameRate::Fps23_976, + FrameRate::Fps24, + FrameRate::Fps25, + FrameRate::Fps29_97, + FrameRate::Fps29_97Drop, + FrameRate::Fps30, + FrameRate::Fps47_952, + FrameRate::Fps48, + FrameRate::Fps50, + FrameRate::Fps59_94, + FrameRate::Fps59_94Drop, + FrameRate::Fps60, + FrameRate::Fps100, + FrameRate::Fps119_88, + FrameRate::Fps120, + ]; + + /// Real frames per second as a rational (num, den). + pub fn rational(self) -> (i64, i64) { + match self { + FrameRate::Fps23_976 => (24_000, 1001), + FrameRate::Fps24 => (24, 1), + FrameRate::Fps25 => (25, 1), + FrameRate::Fps29_97 | FrameRate::Fps29_97Drop => (30_000, 1001), + FrameRate::Fps30 => (30, 1), + FrameRate::Fps47_952 => (48_000, 1001), + FrameRate::Fps48 => (48, 1), + FrameRate::Fps50 => (50, 1), + FrameRate::Fps59_94 | FrameRate::Fps59_94Drop => (60_000, 1001), + FrameRate::Fps60 => (60, 1), + FrameRate::Fps100 => (100, 1), + FrameRate::Fps119_88 => (120_000, 1001), + FrameRate::Fps120 => (120, 1), + } + } + + /// Frames per labelled second (the counting base). + pub fn nominal(self) -> i64 { + let (n, d) = self.rational(); + (n + d / 2) / d + } + + pub fn is_drop(self) -> bool { + matches!(self, FrameRate::Fps29_97Drop | FrameRate::Fps59_94Drop) + } + + pub fn label(self) -> &'static str { + match self { + FrameRate::Fps23_976 => "23.976", + FrameRate::Fps24 => "24", + FrameRate::Fps25 => "25", + FrameRate::Fps29_97 => "29.97", + FrameRate::Fps29_97Drop => "29.97 Drop", + FrameRate::Fps30 => "30", + FrameRate::Fps47_952 => "47.952", + FrameRate::Fps48 => "48", + FrameRate::Fps50 => "50", + FrameRate::Fps59_94 => "59.94", + FrameRate::Fps59_94Drop => "59.94 Drop", + FrameRate::Fps60 => "60", + FrameRate::Fps100 => "100", + FrameRate::Fps119_88 => "119.88", + FrameRate::Fps120 => "120", + } + } + + /// Whole frame index at a sample position (floored). + pub fn frames_at(self, s: Samples, sr: SampleRate) -> i64 { + let (n, d) = self.rational(); + let num = i128::from(s) * i128::from(n); + let den = i128::from(sr.hz()) * i128::from(d); + if den == 0 { + return 0; + } + i64::try_from(num.div_euclid(den)).unwrap_or(0) + } + + /// Sample position of the start of frame `f`. + pub fn samples_at_frame(self, f: i64, sr: SampleRate) -> Samples { + let (n, d) = self.rational(); + if n == 0 { + return 0; + } + // Round half away from zero so negative frames mirror positive ones. + let mag = (i128::from(f.unsigned_abs()) * i128::from(sr.hz()) * i128::from(d) + i128::from(n) / 2) / i128::from(n); + let v = if f < 0 { -mag } else { mag }; + i64::try_from(v).unwrap_or(if v < 0 { i64::MIN } else { i64::MAX }) + } + + /// Samples per frame (fractional). + pub fn samples_per_frame(self, sr: SampleRate) -> f64 { + let (n, d) = self.rational(); + sr.as_f64() * d as f64 / n as f64 + } +} + +/// A timecode label. +#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct Timecode { + pub negative: bool, + pub hours: i64, + pub minutes: i64, + pub seconds: i64, + pub frames: i64, +} + +impl Timecode { + /// Label for an absolute frame count. + pub fn from_frames(frames: i64, rate: FrameRate) -> Timecode { + let negative = frames < 0; + // Drop-frame labels can exceed i64::MAX even when the frame count fits. Widen before + // taking the magnitude so i64::MIN is represented exactly too. + let mut f = i128::from(frames).abs(); + let base = i128::from(rate.nominal().max(1)); + if rate.is_drop() { + // Drop 2 (or 4) labels each minute except every tenth. + let drop = if base >= 60 { 4 } else { 2 }; + let per_10min = base * 600 - drop * 9; + let per_min = base * 60 - drop; + let tens = f / per_10min; + let rem = f % per_10min; + let extra = if rem > drop { drop * 9 * tens + drop * ((rem - drop) / per_min) } else { drop * 9 * tens }; + f += extra; + } + let frames_l = f % base; + let total_secs = f / base; + Timecode { + negative, + hours: i64::try_from(total_secs / 3600).unwrap_or(i64::MAX), + minutes: i64::try_from((total_secs / 60) % 60).unwrap_or(0), + seconds: i64::try_from(total_secs % 60).unwrap_or(0), + frames: i64::try_from(frames_l).unwrap_or(0), + } + } + + /// Absolute frame count of this label. + pub fn to_frames(&self, rate: FrameRate) -> i64 { + let base = rate.nominal().max(1); + let total_min = i128::from(self.hours) * 60 + i128::from(self.minutes); + let mut f = (total_min * 60 + i128::from(self.seconds)) * i128::from(base) + i128::from(self.frames); + if rate.is_drop() { + let drop = if base >= 60 { 4 } else { 2 }; + f -= drop * (total_min - total_min / 10); + } + let f = if self.negative { -f } else { f }; + i64::try_from(f).unwrap_or(if f < 0 { i64::MIN } else { i64::MAX }) + } + + pub fn from_samples(s: Samples, rate: FrameRate, sr: SampleRate) -> Timecode { + Timecode::from_frames(rate.frames_at(s, sr), rate) + } + + pub fn to_samples(&self, rate: FrameRate, sr: SampleRate) -> Samples { + rate.samples_at_frame(self.to_frames(rate), sr) + } + + pub fn format(&self, rate: FrameRate) -> String { + let sep = if rate.is_drop() { ';' } else { ':' }; + let sign = if self.negative { "-" } else { "" }; + format!("{sign}{:02}:{:02}:{:02}{sep}{:02}", self.hours, self.minutes, self.seconds, self.frames) + } + + /// Parse `HH:MM:SS:FF` (also `;` / `.` separators and fewer fields, right-aligned). + pub fn parse(text: &str) -> Result { + let t = text.trim(); + let (negative, t) = match t.strip_prefix('-') { + Some(r) => (true, r), + None => (false, t), + }; + let parts: Vec<&str> = t.split([':', ';', '.']).collect(); + if parts.is_empty() || parts.len() > 4 { + return Err(TimeError::Parse(text.to_string(), "timecode")); + } + let mut nums = [0i64; 4]; + let offset = 4 - parts.len(); + for (i, p) in parts.iter().enumerate() { + let v: i64 = p.trim().parse().map_err(|_| TimeError::Parse(text.to_string(), "timecode"))?; + if v < 0 { + return Err(TimeError::Parse(text.to_string(), "timecode")); + } + if let Some(slot) = nums.get_mut(offset + i) { + *slot = v; + } + } + Ok(Timecode { negative, hours: nums[0], minutes: nums[1], seconds: nums[2], frames: nums[3] }) + } +} + +/// Feet+Frames for 35 mm film (16 frames per foot), `start` frames in at sample 0. +pub fn feet_frames(s: Samples, rate: FrameRate, sr: SampleRate, start: i64) -> (i64, i64) { + let f = rate.frames_at(s, sr).saturating_add(start); + (f.div_euclid(16), f.rem_euclid(16)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn non_drop_round_trip() { + let sr = SampleRate::HZ_48000; + let tc = Timecode::from_samples(48_000 * 3661, FrameRate::Fps25, sr); + assert_eq!(tc.format(FrameRate::Fps25), "01:01:01:00"); + assert_eq!(tc.to_samples(FrameRate::Fps25, sr), 48_000 * 3661); + } + + #[test] + fn negative_timecode_rounds_symmetrically() { + let sr = SampleRate::HZ_48000; + let tc = Timecode::parse("-00:00:01:00").unwrap(); + assert_eq!(tc.to_samples(FrameRate::Fps25, sr), -48_000); + for r in [FrameRate::Fps25, FrameRate::Fps29_97, FrameRate::Fps29_97Drop, FrameRate::Fps23_976] { + for f in [1i64, 7, 25, 1799, 107_892] { + assert_eq!(r.samples_at_frame(-f, sr), -r.samples_at_frame(f, sr), "{r:?} frame {f}"); + } + } + } + + #[test] + fn drop_frame_labels() { + let r = FrameRate::Fps29_97Drop; + // Frame 1800 is the first frame of minute 1 → labelled 00:01:00;02. + assert_eq!(Timecode::from_frames(1800, r).format(r), "00:01:00;02"); + assert_eq!(Timecode::from_frames(1799, r).format(r), "00:00:59;29"); + // Minute 10 keeps its labels. + assert_eq!(Timecode::from_frames(17_982, r).format(r), "00:10:00;00"); + for f in [0, 1, 1799, 1800, 1801, 17_981, 17_982, 107_892, 500_000] { + assert_eq!(Timecode::from_frames(f, r).to_frames(r), f, "frame {f}"); + } + } + + #[test] + fn huge_timecode_saturates_instead_of_overflowing() { + let sr = SampleRate::HZ_48000; + for r in [FrameRate::Fps30, FrameRate::Fps29_97Drop] { + let tc = Timecode::parse("9223372036854775807:00:00:00").unwrap(); + assert_eq!(tc.to_samples(r, sr), i64::MAX); + let tc = Timecode::parse("-9223372036854775807:00:00:00").unwrap(); + assert_eq!(tc.to_samples(r, sr), i64::MIN); + } + let tc = Timecode::parse("01:02:03:04").unwrap(); + assert_eq!(tc.to_samples(FrameRate::Fps30, sr), 178_710_400); + } + + #[test] + fn extreme_frame_counts_round_trip_at_every_rate() { + for rate in FrameRate::ALL { + for frames in [i64::MIN, i64::MIN + 1, -1, 0, 1, i64::MAX - 1, i64::MAX] { + let tc = Timecode::from_frames(frames, rate); + assert_eq!(tc.to_frames(rate), frames, "{rate:?}: {frames}"); + assert_eq!(tc.negative, frames < 0); + assert!((0..60).contains(&tc.minutes)); + assert!((0..60).contains(&tc.seconds)); + assert!((0..rate.nominal()).contains(&tc.frames)); + assert_eq!(Timecode::parse(&tc.format(rate)).unwrap(), tc); + } + } + } + + #[test] + fn extreme_drop_frame_labels_do_not_overflow() { + for rate in [FrameRate::Fps29_97Drop, FrameRate::Fps59_94Drop] { + assert_eq!(Timecode::from_frames(i64::MAX, rate).to_frames(rate), i64::MAX); + } + } + + #[test] + fn parse_variants() { + let tc = Timecode::parse("01:02:03:04").unwrap(); + assert_eq!((tc.hours, tc.minutes, tc.seconds, tc.frames), (1, 2, 3, 4)); + let tc = Timecode::parse("3:04").unwrap(); + assert_eq!((tc.seconds, tc.frames), (3, 4)); + assert!(Timecode::parse("a:b").is_err()); + assert!(Timecode::parse("1:2:3:4:5").is_err()); + } + + #[test] + fn feet_and_frames() { + let sr = SampleRate::HZ_48000; + assert_eq!(feet_frames(48_000, FrameRate::Fps24, sr, 0), (1, 8)); + } +} From 4c251094737d9931a79cc8a68fdf5eb39d25b585 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:47:36 +0000 Subject: [PATCH 033/520] test(openpgp): align signature time with generated key --- crates/openpgp/src/tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/openpgp/src/tests.rs b/crates/openpgp/src/tests.rs index 4dc30a0d3..f78519ba0 100644 --- a/crates/openpgp/src/tests.rs +++ b/crates/openpgp/src/tests.rs @@ -443,7 +443,8 @@ fn the_unhashed_area_never_decides_who_is_blamed() { // An issuer only in the unhashed area finds a key, which counts because it fits. let alice = secret(ALICE, Make::default()); - let mut config = config(&alice.primary_key, pgp::packet::SignatureType::Binary, t, vec![]); + let alice_created = made(&alice); + let mut config = config(&alice.primary_key, pgp::packet::SignatureType::Binary, alice_created, vec![]); config.hashed_subpackets.retain(|p| matches!(p.data, SubpacketData::SignatureCreationTime(_))); config.unhashed_subpackets = vec![Subpacket::regular(SubpacketData::IssuerFingerprint(alice.primary_key.fingerprint())).unwrap()]; let sig = config.sign(&alice.primary_key, &Password::empty(), SIGNED_PART.as_bytes()).unwrap(); From 88aa0bed6a315c10203a5d0ea51652a4c7dc82d2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:07:08 +0000 Subject: [PATCH 034/520] Add shared SoundCraft session format --- Cargo.lock | 14 + Cargo.toml | 1 + README.md | 1 + crates/scraft-format/Cargo.toml | 24 + crates/scraft-format/README.md | 15 + crates/scraft-format/src/automation.rs | 394 +++++++ crates/scraft-format/src/b64.rs | 114 ++ crates/scraft-format/src/bundle.rs | 250 +++++ crates/scraft-format/src/clip.rs | 544 ++++++++++ crates/scraft-format/src/ids.rs | 21 + crates/scraft-format/src/lib.rs | 53 + crates/scraft-format/src/markers.rs | 58 ++ crates/scraft-format/src/mixer.rs | 714 +++++++++++++ crates/scraft-format/src/session.rs | 1319 ++++++++++++++++++++++++ crates/scraft-format/src/source.rs | 204 ++++ crates/scraft-format/src/track.rs | 477 +++++++++ crates/zip/src/archive.rs | 6 + crates/zip/src/lib.rs | 16 + 18 files changed, 4225 insertions(+) create mode 100644 crates/scraft-format/Cargo.toml create mode 100644 crates/scraft-format/README.md create mode 100644 crates/scraft-format/src/automation.rs create mode 100644 crates/scraft-format/src/b64.rs create mode 100644 crates/scraft-format/src/bundle.rs create mode 100644 crates/scraft-format/src/clip.rs create mode 100644 crates/scraft-format/src/ids.rs create mode 100644 crates/scraft-format/src/lib.rs create mode 100644 crates/scraft-format/src/markers.rs create mode 100644 crates/scraft-format/src/mixer.rs create mode 100644 crates/scraft-format/src/session.rs create mode 100644 crates/scraft-format/src/source.rs create mode 100644 crates/scraft-format/src/track.rs diff --git a/Cargo.lock b/Cargo.lock index 2dbe34bdd..ae32f8b7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2145,6 +2145,20 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-scraft-format" +version = "0.1.0" +dependencies = [ + "craft-audio", + "craft-midi", + "craft-time", + "craft-zip", + "proptest", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-settings" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 23e3e5d46..65909c1cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,6 +108,7 @@ craft-raw = { path = "crates/raw" } craft-sheet = { path = "crates/sheet" } craft-sketch = { path = "crates/sketch" } craft-settings = { path = "crates/settings" } +craft-scraft-format = { path = "crates/scraft-format" } craft-time = { path = "crates/time" } craft-silhouette-studio = { path = "crates/silhouette-studio" } craft-svg = { path = "crates/svg" } diff --git a/README.md b/README.md index c56ce2bf0..ed2a8bddd 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit is built on it in `craft-pdf`. - [`craft-timeline`](crates/timeline/README.md): editorial timeline interchange: exact media time and SMPTE timecode, import/export reports, media path and `file://` URL handling, Avid Log Exchange (ALE) and CMX 3600 EDL documents, format detection. From FilmCraft. - [`craft-time`](crates/time/README.md): exact rational ticks and SMPTE time across media, with a `soundcraft` compatibility module retaining SoundCraft's sample-index timebase, tempo maps, grids and session serialization semantics. +- [`craft-scraft-format`](crates/scraft-format/README.md): SoundCraft's typed `.scraft` session schema, versioned reader/writer, hostile-input validation, and `.scraftz` bundle reader/writer. Engine and UI mapping stays in SoundCraft. - [`craft-metadata`](crates/metadata/README.md): photo metadata, from LightCraft: EXIF (read and write, maker-note lens and focus point, every tag by name), IPTC-IIM, XMP packets (read, write, and a merge that keeps what another program wrote), GPX track logs, and the blocks that carry them in JPEG, PNG, diff --git a/crates/scraft-format/Cargo.toml b/crates/scraft-format/Cargo.toml new file mode 100644 index 000000000..9ec17cf9f --- /dev/null +++ b/crates/scraft-format/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "craft-scraft-format" +description = "SoundCraft's versioned native session document model and JSON schema" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-audio = { workspace = true, features = ["lossless-encode", "peaks"] } +craft-midi = { workspace = true, features = ["serde"] } +craft-time.workspace = true +craft-zip.workspace = true +serde.workspace = true +serde_json.workspace = true +thiserror.workspace = true + +[dev-dependencies] +craft-audio = { workspace = true, features = ["decode", "serde"] } +proptest.workspace = true diff --git a/crates/scraft-format/README.md b/crates/scraft-format/README.md new file mode 100644 index 000000000..c2df56206 --- /dev/null +++ b/crates/scraft-format/README.md @@ -0,0 +1,15 @@ +# craft-scraft-format + +The native SoundCraft session document, `.scraft` JSON schema and typed reader/writer. `Session` +and its nested types are the wire representation; the library checks `format_version` before +parsing, deserializes the document directly (without building a `serde_json::Value` tree), then +sanitizes and validates the resulting session. `Session::to_json` writes the same pretty JSON +representation. + +The crate retains SoundCraft's serialized enum names, defaults, compatibility behavior for older +fields, 256 MiB session byte limit, character limits, and duplicate/exhausted ID checks. Its time +fields use `craft_time::soundcraft`, the exact sample-index time API retained by `craft-time`. +MIDI sequence values use `craft-midi`'s serde representation. + +The application still maps sessions to UI state, engine state, media paths and audio pools. The +session schema and its format-specific parsing, writing, sanitization and validation live here. diff --git a/crates/scraft-format/src/automation.rs b/crates/scraft-format/src/automation.rs new file mode 100644 index 000000000..6d597682e --- /dev/null +++ b/crates/scraft-format/src/automation.rs @@ -0,0 +1,394 @@ +//! Automation lanes and modes. + +use crate::soundcraft_time::Samples; + +/// Automation modes (per track). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum AutomationMode { + Off, + #[default] + Read, + Touch, + Latch, + TouchLatch, + Write, + Trim, +} + +impl AutomationMode { + pub const ALL: [AutomationMode; 7] = [ + AutomationMode::Off, + AutomationMode::Read, + AutomationMode::Touch, + AutomationMode::Latch, + AutomationMode::TouchLatch, + AutomationMode::Write, + AutomationMode::Trim, + ]; + pub fn label(self) -> &'static str { + match self { + AutomationMode::Off => "off", + AutomationMode::Read => "read", + AutomationMode::Touch => "touch", + AutomationMode::Latch => "latch", + AutomationMode::TouchLatch => "touch/latch", + AutomationMode::Write => "write", + AutomationMode::Trim => "trim", + } + } + pub fn from_id(s: &str) -> Option { + let s = s.to_ascii_lowercase(); + AutomationMode::ALL.into_iter().find(|m| m.label() == s || m.label().replace('/', "_") == s || format!("{m:?}").to_ascii_lowercase() == s) + } + pub fn reads(self) -> bool { + !matches!(self, AutomationMode::Off) + } +} + +/// Which parameter an automation lane controls. +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize)] +pub enum AutoParam { + /// Fader level, dB. + Volume, + /// Pan, -1..1 (index = which panner of a stereo track: 0 left, 1 right). + Pan(u8), + /// Mute (0/1). + Mute, + /// Send level, dB. + SendLevel(u8), + SendPan(u8), + SendMute(u8), + /// Insert slot + parameter id. + Plugin { + slot: u8, + param: String, + }, +} + +impl AutoParam { + pub fn label(&self) -> String { + match self { + AutoParam::Volume => "volume".into(), + AutoParam::Pan(0) => "pan".into(), + AutoParam::Pan(i) => format!("pan {}", u16::from(*i) + 1), + AutoParam::Mute => "mute".into(), + AutoParam::SendLevel(i) => format!("send {} level", slot_letter(*i)), + AutoParam::SendPan(i) => format!("send {} pan", slot_letter(*i)), + AutoParam::SendMute(i) => format!("send {} mute", slot_letter(*i)), + AutoParam::Plugin { slot, param } => format!("insert {} {param}", slot_letter(*slot)), + } + } + /// Parse ids like `volume`, `pan`, `pan2`, `mute`, `send_a_level`, `plugin:a:threshold`. + pub fn parse(s: &str) -> Option { + let orig = s.trim(); + let s = orig.to_ascii_lowercase(); + match s.as_str() { + "volume" | "vol" => return Some(AutoParam::Volume), + "pan" | "pan1" => return Some(AutoParam::Pan(0)), + "pan2" => return Some(AutoParam::Pan(1)), + "mute" => return Some(AutoParam::Mute), + _ => {} + } + if let Some(rest) = s.strip_prefix("send_") { + let mut it = rest.splitn(2, '_'); + let slot = letter_slot(it.next()?)?; + return match it.next()? { + "level" => Some(AutoParam::SendLevel(slot)), + "pan" => Some(AutoParam::SendPan(slot)), + "mute" => Some(AutoParam::SendMute(slot)), + _ => None, + }; + } + if let Some(rest) = s.strip_prefix("plugin:") { + let (slot, param) = rest.split_once(':')?; + // ASCII lowercasing keeps byte offsets, so take the param from the original. + let param = orig.get(orig.len() - param.len()..)?; + return Some(AutoParam::Plugin { slot: letter_slot(slot)?, param: param.to_string() }); + } + None + } + /// Value range and default. + pub fn range(&self) -> (f32, f32, f32) { + match self { + AutoParam::Volume | AutoParam::SendLevel(_) => (-144.0, 12.0, 0.0), + AutoParam::Pan(_) | AutoParam::SendPan(_) => (-1.0, 1.0, 0.0), + AutoParam::Mute | AutoParam::SendMute(_) => (0.0, 1.0, 0.0), + AutoParam::Plugin { .. } => (f32::MIN, f32::MAX, 0.0), + } + } +} + +pub fn slot_letter(i: u8) -> char { + char::from(b'A'.saturating_add(i.min(25))) +} + +fn letter_slot(s: &str) -> Option { + let c = s.chars().next()?.to_ascii_uppercase(); + if s.len() == 1 && c.is_ascii_uppercase() { Some(c as u8 - b'A') } else { s.parse().ok() } +} + +#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct AutomationPoint { + pub at: Samples, + pub value: f32, +} + +/// A breakpoint automation lane. Points are kept sorted by time. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct AutomationLane { + pub param: AutoParam, + pub points: Vec, + #[serde(default)] + pub enabled: bool, +} + +impl AutomationLane { + pub fn new(param: AutoParam) -> Self { + AutomationLane { param, points: Vec::new(), enabled: true } + } + + /// Value at `at` (linear interpolation; `default` when empty). + pub fn value_at(&self, at: Samples, default: f32) -> f32 { + let pts = &self.points; + let Some(first) = pts.first() else { return default }; + if at <= first.at { + return first.value; + } + let idx = pts.partition_point(|p| p.at <= at); + match (pts.get(idx.wrapping_sub(1)), pts.get(idx)) { + (Some(a), Some(b)) if b.at > a.at => { + // Mute-like lanes step instead of ramping. + if matches!(self.param, AutoParam::Mute | AutoParam::SendMute(_)) { + return a.value; + } + // Widen so far-apart breakpoints cannot overflow i64. + let span = i128::from(b.at) - i128::from(a.at); + let off = i128::from(at) - i128::from(a.at); + // Interpolate in f64: the f32 difference overflows for + // opposite-sign values near f32::MAX. + let (av, bv) = (f64::from(a.value), f64::from(b.value)); + (av + (bv - av) * (off as f64 / span as f64)) as f32 + } + (Some(a), _) => a.value, + _ => default, + } + } + + /// Insert or replace a breakpoint. + pub fn set_point(&mut self, at: Samples, value: f32) { + let (lo, hi, _) = self.param.range(); + let value = if value.is_nan() { 0.0 } else { value.clamp(lo, hi) }; + match self.points.binary_search_by(|p| p.at.cmp(&at)) { + Ok(i) => { + if let Some(p) = self.points.get_mut(i) { + p.value = value; + } + } + Err(i) => self.points.insert(i, AutomationPoint { at, value }), + } + } + + /// Remove points in `[start, end)`. + pub fn clear_range(&mut self, start: Samples, end: Samples) -> usize { + let before = self.points.len(); + self.points.retain(|p| p.at < start || p.at >= end); + before - self.points.len() + } + + /// Write a constant value over a range, keeping the surrounding curve intact with anchor points. + pub fn write_range(&mut self, start: Samples, end: Samples, value: f32, default: f32) { + if end <= start { + return; + } + let before = self.value_at(start.saturating_sub(1), default); + let after = self.value_at(end, default); + self.clear_range(start, end.saturating_add(1)); + if start > 0 { + self.set_point(start - 1, before); + } + self.set_point(start, value); + self.set_point(end, value); + let anchor = end.saturating_add(1); + // A real breakpoint already at `end + 1` keeps its own value. + if self.points.binary_search_by(|p| p.at.cmp(&anchor)).is_err() { + self.set_point(anchor, after); + } + } + + /// Thin: remove points whose removal changes the curve by less than `tolerance`. + pub fn thin(&mut self, tolerance: f32) -> usize { + // Linear thinning would move the transitions of step lanes. + if matches!(self.param, AutoParam::Mute | AutoParam::SendMute(_)) || self.points.len() < 3 || tolerance.is_nan() { + return 0; + } + let tolerance = f64::from(tolerance); + let mut keep = vec![true; self.points.len()]; + let mut last = 0usize; + // Slopes from the last kept point that keep every point removed since then within `tolerance`. + let (mut lo, mut hi) = (f64::NEG_INFINITY, f64::INFINITY); + for i in 1..self.points.len() - 1 { + let (Some(a), Some(b), Some(c)) = (self.points.get(last), self.points.get(i), self.points.get(i + 1)) else { continue }; + let dt = b.at.saturating_sub(a.at) as f64; + let rise = f64::from(b.value) - f64::from(a.value); + lo = lo.max((rise - tolerance) / dt); + hi = hi.min((rise + tolerance) / dt); + let slope = (f64::from(c.value) - f64::from(a.value)) / c.at.saturating_sub(a.at) as f64; + if (lo..=hi).contains(&slope) { + if let Some(k) = keep.get_mut(i) { + *k = false; + } + } else { + last = i; + (lo, hi) = (f64::NEG_INFINITY, f64::INFINITY); + } + } + let before = self.points.len(); + let mut it = keep.iter(); + self.points.retain(|_| it.next().copied().unwrap_or(true)); + before - self.points.len() + } + + /// Shift points at or after `from` by `delta` (Shuffle edits, Insert Time). + pub fn shift_from(&mut self, from: Samples, delta: Samples) { + for p in &mut self.points { + if p.at >= from { + p.at = p.at.saturating_add(delta).max(0); + } + } + self.points.sort_by_key(|p| p.at); + self.points.dedup_by_key(|p| p.at); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_keeps_plugin_param_case() { + let p = AutoParam::Plugin { slot: 0, param: "Cutoff".into() }; + assert_eq!(AutoParam::parse("plugin:a:Cutoff"), Some(p)); + assert_eq!(AutoParam::parse("Plugin:B:Mix"), Some(AutoParam::Plugin { slot: 1, param: "Mix".into() })); + assert_eq!(AutoParam::parse("Volume"), Some(AutoParam::Volume)); + assert_eq!(AutoParam::parse("SEND_B_LEVEL"), Some(AutoParam::SendLevel(1))); + } + + #[test] + fn value_at_far_apart_points_does_not_overflow() { + let mut l = AutomationLane::new(AutoParam::Volume); + l.set_point(-1, 0.0); + l.set_point(i64::MAX, -10.0); + let v = l.value_at(5, 0.0); + assert!(v.is_finite() && (-10.0..=0.0).contains(&v)); + let w = l.value_at(i64::MAX - 1, 0.0); + assert!(w.is_finite() && (-10.0..=0.0).contains(&w)); + } + + #[test] + fn value_at_opposite_sign_extremes_stays_finite() { + let mut l = AutomationLane::new(AutoParam::Plugin { slot: 0, param: "Cutoff".into() }); + l.set_point(0, -3.0e38); + l.set_point(1000, 3.0e38); + let v = l.value_at(500, 0.0); + assert!(v.is_finite() && v.abs() < 1.0e32, "mid value {v}"); + let w = l.value_at(1, 0.0); + assert!(w.is_finite() && w < -2.9e38, "early value {w}"); + } + + #[test] + fn pan_label_max_does_not_overflow() { + assert_eq!(AutoParam::Pan(255).label(), "pan 256"); + } + + #[test] + fn interpolation_and_steps() { + let mut l = AutomationLane::new(AutoParam::Volume); + assert_eq!(l.value_at(10, -3.0), -3.0); + l.set_point(0, 0.0); + l.set_point(100, -10.0); + assert!((l.value_at(50, 0.0) + 5.0).abs() < 1e-6); + assert_eq!(l.value_at(1000, 0.0), -10.0); + let mut m = AutomationLane::new(AutoParam::Mute); + m.set_point(0, 0.0); + m.set_point(100, 1.0); + assert_eq!(m.value_at(99, 0.0), 0.0); + assert_eq!(m.value_at(100, 0.0), 1.0); + } + + #[test] + fn write_range_keeps_point_right_after_end() { + let mut l = AutomationLane::new(AutoParam::Volume); + l.set_point(0, 0.0); + l.set_point(11, -100.0); + l.write_range(2, 10, -20.0, 0.0); + assert_eq!(l.value_at(11, 0.0), -100.0); + } + + #[test] + fn write_range_preserves_outside() { + let mut l = AutomationLane::new(AutoParam::Volume); + l.set_point(0, 0.0); + l.set_point(1000, 0.0); + l.write_range(200, 400, -20.0, 0.0); + assert_eq!(l.value_at(100, 0.0), 0.0); + assert_eq!(l.value_at(300, 0.0), -20.0); + assert_eq!(l.value_at(600, 0.0), 0.0); + } + + #[test] + fn thin_removes_collinear() { + let mut l = AutomationLane::new(AutoParam::Volume); + for i in 0..=10 { + l.set_point(i * 10, -(i as f32)); + } + assert_eq!(l.thin(0.01), 9); + assert_eq!(l.points.len(), 2); + } + + #[test] + fn thin_keeps_removed_points_within_tolerance() { + for direction in [1.0, -1.0] { + let mut lane = AutomationLane::new(AutoParam::Volume); + for (at, value) in [(0, 0.0), (100, 0.0), (200, 0.0), (300, 0.12), (400, 0.26)] { + lane.set_point(at, value * direction); + } + let original = lane.points.clone(); + let tolerance = 0.1; + assert!(lane.thin(tolerance) > 0); + for point in original { + let error = (lane.value_at(point.at, 0.0) - point.value).abs(); + assert!(error <= tolerance, "at {}: error {error} exceeds {tolerance}", point.at); + } + } + } + + #[test] + fn thin_keeps_mute_changes_in_place() { + for param in [AutoParam::Mute, AutoParam::SendMute(0)] { + let mut lane = AutomationLane::new(param); + for (at, value) in [(0, 0.0), (9000, 1.0), (10000, 1.0)] { + lane.set_point(at, value); + } + lane.thin(0.1); + assert_eq!(lane.value_at(8999, 0.0), 0.0); + assert_eq!(lane.value_at(9000, 0.0), 1.0); + } + } + + #[test] + fn param_ids() { + assert_eq!(AutoParam::parse("send_b_level"), Some(AutoParam::SendLevel(1))); + assert_eq!(AutoParam::parse("plugin:a:threshold"), Some(AutoParam::Plugin { slot: 0, param: "threshold".into() })); + assert_eq!(AutoParam::parse("nope"), None); + assert_eq!(AutomationMode::from_id("touch/latch"), Some(AutomationMode::TouchLatch)); + } + + #[test] + fn set_point_clamps_nan() { + let mut l = AutomationLane::new(AutoParam::Pan(0)); + l.set_point(0, f32::NAN); + l.set_point(1, 5.0); + assert_eq!(l.points[0].value, 0.0); + assert_eq!(l.points[1].value, 1.0); + } +} diff --git a/crates/scraft-format/src/b64.rs b/crates/scraft-format/src/b64.rs new file mode 100644 index 000000000..100a9a08f --- /dev/null +++ b/crates/scraft-format/src/b64.rs @@ -0,0 +1,114 @@ +//! Standard base64 (RFC 4648, `+/` alphabet, `=` padding), used to keep opaque plugin state +//! blobs in the JSON session file. Decoding treats its input as hostile: it rejects bad +//! characters and bad padding, ignores ASCII whitespace, and caps the output size. + +/// Largest blob [`decode`] will produce (64 MiB): bigger session fields are rejected. +pub const MAX_DECODED_BYTES: usize = 64 << 20; + +const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/// Encodes bytes as padded standard base64. +pub fn encode(data: &[u8]) -> String { + let mut out = String::with_capacity(data.len().div_ceil(3).saturating_mul(4)); + for chunk in data.chunks(3) { + let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)]; + let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); + let sym = |shift: u32| char::from(ALPHABET.get(((n >> shift) & 63) as usize).copied().unwrap_or(b'A')); + out.push(sym(18)); + out.push(sym(12)); + out.push(if chunk.len() > 1 { sym(6) } else { '=' }); + out.push(if chunk.len() > 2 { sym(0) } else { '=' }); + } + out +} + +fn value(c: u8) -> Option { + let v = match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' => 62, + b'/' => 63, + _ => return None, + }; + Some(u32::from(v)) +} + +/// Decodes padded (or unpadded) standard base64. `None` for malformed input or a result larger +/// than [`MAX_DECODED_BYTES`]. +pub fn decode(text: &str) -> Option> { + if text.bytes().filter(|c| !c.is_ascii_whitespace()).count() > MAX_DECODED_BYTES.div_ceil(3).saturating_mul(4) { + return None; + } + let syms: Vec = text.bytes().filter(|c| !c.is_ascii_whitespace()).collect(); + let body = syms.strip_suffix(b"==").or_else(|| syms.strip_suffix(b"=")).unwrap_or(&syms); + if body.contains(&b'=') || (syms.len() != body.len() && !syms.len().is_multiple_of(4)) || body.len() % 4 == 1 { + return None; + } + let decoded_len = body.len() / 4 * 3 + body.len() % 4 * 3 / 4; + if decoded_len > MAX_DECODED_BYTES { + return None; + } + let mut out = Vec::with_capacity(decoded_len); + for chunk in body.chunks(4) { + let mut n = 0u32; + for (i, &c) in chunk.iter().enumerate() { + n |= value(c)? << (18 - 6 * i as u32); + } + let bytes = [(n >> 16) as u8, (n >> 8) as u8, n as u8]; + out.extend_from_slice(bytes.get(..chunk.len() - 1)?); + } + Some(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rfc4648_vectors_round_trip() { + for (plain, enc) in + [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] + { + assert_eq!(encode(plain.as_bytes()), enc); + assert_eq!(decode(enc).unwrap(), plain.as_bytes()); + } + let all: Vec = (0..=255).collect(); + assert_eq!(decode(&encode(&all)).unwrap(), all); + assert_eq!(decode("Zm9v\nYmE=").unwrap(), b"fooba", "whitespace is ignored"); + assert_eq!(decode("Zm9vYmE").unwrap(), b"fooba", "padding is optional"); + } + + #[test] + fn hostile_input_is_rejected() { + for bad in ["Z", "Zm9=v", "Zm9v!", "====", "Zg=", "é", "Zg===", "Z==="] { + assert!(decode(bad).is_none(), "{bad}"); + } + } + + #[test] + fn size_cap_counts_the_partial_quartet() { + fn encoded_zero_bytes(len: usize) -> String { + let groups = len / 3; + let mut encoded = "A".repeat(groups * 4); + match len % 3 { + 1 => encoded.push_str("AA=="), + 2 => encoded.push_str("AAA="), + _ => {} + } + encoded + } + + // Build valid base64 directly instead of encoding a 64 MiB allocation on every case. + // This keeps the same decoded-size boundary checks while making the workspace gate much + // faster and using less temporary memory. + let exact = encoded_zero_bytes(MAX_DECODED_BYTES); + assert_eq!(decode(&exact).map(|bytes| bytes.len()), Some(MAX_DECODED_BYTES), "padded boundary"); + assert_eq!(decode(exact.trim_end_matches('=')).map(|bytes| bytes.len()), Some(MAX_DECODED_BYTES), "unpadded boundary"); + + let too_large = encoded_zero_bytes(MAX_DECODED_BYTES + 1); + assert!(decode(&too_large).is_none(), "padded over boundary"); + let spaced = format!("{}\n {}", &too_large[..8], &too_large[8..]); + assert!(decode(&spaced).is_none(), "whitespace over boundary"); + } +} diff --git a/crates/scraft-format/src/bundle.rs b/crates/scraft-format/src/bundle.rs new file mode 100644 index 000000000..52d6d33be --- /dev/null +++ b/crates/scraft-format/src/bundle.rs @@ -0,0 +1,250 @@ +//! The optional `.scraftz` ZIP container for a `.scraft` session and its embedded audio files. + +use crate::{FileFormat, MAX_SESSION_BYTES, SESSION_EXTENSION, Session}; +use craft_audio::{BitDepth, EncodeFormat, EncodeOptions, encode}; +use craft_zip::{Archive, Compression, Limits, Writer}; +use std::collections::{HashMap, HashSet}; +use std::ffi::OsStr; +use std::path::Path; + +/// Extension of a ZIP bundle containing a `.scraft` session and its audio files. +pub const BUNDLE_EXTENSION: &str = "scraftz"; + +/// The limits applied to a bundle in both directions. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct BundleLimits { + /// Maximum ZIP entries, including directory entries. + pub entries: usize, + /// Maximum combined uncompressed size of entries. + pub bytes: u64, + /// Maximum size of the contained session JSON. + pub session_bytes: u64, +} + +/// SoundCraft's current bundle defaults: 10,000 entries, 1 GiB expanded and 256 MiB per session. +pub const DEFAULT_BUNDLE_LIMITS: BundleLimits = BundleLimits { entries: 10_000, bytes: 1 << 30, session_bytes: MAX_SESSION_BYTES }; + +/// Errors while reading or writing a session bundle. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum BundleError { + /// The archive is malformed, exceeds a configured limit, or does not contain a session. + #[error("bundle: {0}")] + Invalid(String), + /// The session JSON cannot be serialized. + #[error("{0}")] + Session(String), + /// The embedded audio cannot be encoded as WAV. + #[error("{0}")] + Audio(String), +} + +/// Write a session and its decoded audio as a deterministic `.scraftz` ZIP bundle. +/// +/// Audio entries use the same float WAV representation as SoundCraft's desktop bundle writer. +/// The returned session has each included source's path rewritten to `Audio Files/.wav`, +/// its format set to `Wav`, and `unsaved` cleared. +pub fn write_bundle(session: &Session, limits: BundleLimits) -> Result<(Vec, Session), BundleError> { + let mut fresh = session.clone(); + let mut zip = Writer::new(); + let mut names = HashSet::new(); + let mut count = 0usize; + let mut total = 0u64; + + for source in &mut fresh.sources { + let Some(audio) = fresh.pool.get(source.id) else { continue }; + let bytes = encode(&audio.buffer, &EncodeOptions::new(EncodeFormat::Wav).with_bit_depth(BitDepth::Float32)) + .map_err(|e| BundleError::Audio(e.to_string()))?; + let base = sanitize_name(&source.name); + let mut target = format!("Audio Files/{base}.wav"); + let mut suffix = 1usize; + while names.contains(&target) && suffix <= 9_999 { + target = format!("Audio Files/{base}_{suffix:02}.wav"); + suffix += 1; + } + account(&mut count, &mut total, bytes.len(), limits)?; + zip.add(&target, &bytes, Compression::Stored).map_err(|e| BundleError::Invalid(e.to_string()))?; + names.insert(target.clone()); + source.path = target; + source.unsaved = false; + source.format = FileFormat::Wav; + } + + let text = fresh.to_json().map_err(|e| BundleError::Session(e.to_string()))?; + check_session_size("the session", text.len() as u64, limits.session_bytes, true)?; + account(&mut count, &mut total, text.len(), limits)?; + let session_name = format!("{}.{}", sanitize_name(&fresh.name), SESSION_EXTENSION); + zip.add(&session_name, text.as_bytes(), Compression::Stored).map_err(|e| BundleError::Invalid(e.to_string()))?; + let bytes = zip.finish().map_err(|e| BundleError::Invalid(e.to_string()))?; + Ok((bytes, fresh)) +} + +/// Read the first root-level `.scraft` and retain other files by their archive path. +/// +/// Duplicate entry names, non-text names and entries whose file data overlaps another entry are +/// refused before payloads are copied. The session byte cap is checked against the ZIP's declared +/// size before decompressing its JSON. +pub fn read_bundle(bytes: &[u8], limits: BundleLimits) -> Result<(String, HashMap>), BundleError> { + let max_entries = u64::try_from(limits.entries).unwrap_or(u64::MAX); + let mut archive = Archive::open(bytes, Limits { max_entries, max_total_bytes: limits.bytes }).map_err(zip_error)?; + if archive.has_duplicate_names() { + return Err(BundleError::Invalid("files share a name".into())); + } + if archive.has_overlapping_files().map_err(zip_error)? { + return Err(BundleError::Invalid("files share data".into())); + } + let names: Vec = archive.names().map(|n| n.into_owned()).collect(); + if names.len() != archive.len() { + return Err(BundleError::Invalid("an entry name is not text".into())); + } + let mut session = None; + let mut files = HashMap::new(); + for name in names { + if name.ends_with('/') { + continue; + } + let is_session = !name.contains('/') && Path::new(&name).extension().and_then(OsStr::to_str) == Some(SESSION_EXTENSION); + let size = archive.declared_size(&name).ok_or_else(|| BundleError::Invalid(format!("{name}: missing entry")))?; + if is_session && session.is_none() { + check_session_size(&name, size, limits.session_bytes, false).map_err(bundle_context)?; + } + let data = archive.read(&name, limits.bytes).map_err(zip_error)?.ok_or_else(|| BundleError::Invalid(format!("{name}: missing entry")))?; + if is_session && session.is_none() { + session = Some(String::from_utf8(data).map_err(|_| BundleError::Invalid(format!("{name} is not text")))?); + } else { + files.insert(name, data); + } + } + let session = session.ok_or_else(|| BundleError::Invalid("no .scraft session inside".into()))?; + Ok((session, files)) +} + +/// Refuse a session that cannot be opened again under the same byte cap. +pub fn check_session_size(name: &str, len: u64, limit: u64, saving: bool) -> Result<(), BundleError> { + if len <= limit { + return Ok(()); + } + let most = if limit >= 1 << 20 { format!("{} MiB", limit >> 20) } else { format!("{limit} bytes") }; + let message = if saving { + format!("{name} is too large to save: over {most}, more than SoundCraft opens") + } else { + format!("{name} is too large to open (over {most})") + }; + Err(BundleError::Session(message)) +} + +fn bundle_context(error: BundleError) -> BundleError { + match error { + BundleError::Session(message) => BundleError::Invalid(message), + other => other, + } +} + +fn account(count: &mut usize, total: &mut u64, len: usize, limits: BundleLimits) -> Result<(), BundleError> { + *count = count.saturating_add(1); + *total = total.saturating_add(u64::try_from(len).unwrap_or(u64::MAX)); + if *count > limits.entries || *total > limits.bytes { + return Err(BundleError::Invalid(format!( + "too large to save (a bundle holds at most {} files and {} MiB, or it can't be opened again)", + limits.entries, + limits.bytes >> 20 + ))); + } + Ok(()) +} + +fn zip_error(error: craft_zip::Error) -> BundleError { + match error { + craft_zip::Error::NotZip(message) => BundleError::Invalid(message), + craft_zip::Error::TooManyEntries { .. } => BundleError::Invalid("too many files".into()), + craft_zip::Error::EntryTooLarge { .. } | craft_zip::Error::BudgetSpent { .. } => BundleError::Invalid("too large".into()), + craft_zip::Error::Entry { name, msg } => BundleError::Invalid(format!("{name}: {msg}")), + other => BundleError::Invalid(other.to_string()), + } +} + +fn sanitize_name(name: &str) -> String { + let text: String = name.chars().map(|c| if c.is_alphanumeric() || matches!(c, '-' | '_' | ' ' | '.') { c } else { '_' }).collect(); + let text = text.trim().trim_start_matches('.').to_string(); + if text.is_empty() { "untitled".into() } else { text.chars().take(100).collect() } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Session; + + #[test] + fn bare_session_round_trips_in_a_bundle() { + let session = Session::default(); + let (bytes, fresh) = write_bundle(&session, DEFAULT_BUNDLE_LIMITS).unwrap(); + assert!(bytes.starts_with(b"PK\x03\x04")); + assert_eq!(fresh, session); + assert_eq!(write_bundle(&session, DEFAULT_BUNDLE_LIMITS).unwrap().0, bytes, "same input writes the same ZIP bytes"); + let (text, files) = read_bundle(&bytes, DEFAULT_BUNDLE_LIMITS).unwrap(); + assert!(files.is_empty()); + assert_eq!(Session::from_json(&text).unwrap(), session); + } + + #[test] + fn audio_is_stored_as_float_wav_and_the_session_path_is_rewritten() { + use crate::{FileFormat, Source, SourceAudio, SourceId}; + use craft_audio::AudioBuffer; + use std::sync::Arc; + + let id = SourceId(1); + let samples = [-2.0, -0.25, 0.5, 1.5]; + let mut audio = AudioBuffer::new(48_000, 1, samples.len()); + audio.channels[0].copy_from_slice(&samples); + let mut session = Session::default(); + session.sources.push(Source { + id, + name: "Lead / take?".into(), + path: "elsewhere.wav".into(), + channels: 1, + frames: samples.len() as u64, + sample_rate: 48_000, + format: FileFormat::Other, + time_reference: 0, + unsaved: true, + }); + session.pool.insert(id, Arc::new(SourceAudio::new(audio))); + + let (bytes, fresh) = write_bundle(&session, DEFAULT_BUNDLE_LIMITS).unwrap(); + let source = fresh.sources.first().unwrap(); + assert_eq!(source.path, "Audio Files/Lead _ take_.wav"); + assert_eq!(source.format, FileFormat::Wav); + assert!(!source.unsaved); + + let (text, files) = read_bundle(&bytes, DEFAULT_BUNDLE_LIMITS).unwrap(); + let parsed = Session::from_json(&text).unwrap(); + assert_eq!(parsed.sources[0].path, source.path); + let wav = files.get(&source.path).unwrap(); + let (_, decoded) = craft_audio::decode(wav, Some("wav")).unwrap(); + assert_eq!(decoded.channels[0], samples); + } + + #[test] + fn session_size_is_checked_in_both_directions() { + let too_big = check_session_size("a session", 11, 10, true).unwrap_err(); + assert_eq!(too_big.to_string(), "a session is too large to save: over 10 bytes, more than SoundCraft opens"); + let too_big = check_session_size("a session", 11, 10, false).unwrap_err(); + assert_eq!(too_big.to_string(), "a session is too large to open (over 10 bytes)"); + } + + #[test] + fn a_bundle_without_a_session_is_refused() { + let mut zip = Writer::new(); + zip.add("Audio Files/a.wav", b"audio", Compression::Stored).unwrap(); + let bytes = zip.finish().unwrap(); + assert_eq!(read_bundle(&bytes, DEFAULT_BUNDLE_LIMITS).unwrap_err().to_string(), "bundle: no .scraft session inside"); + } + + #[test] + fn every_truncation_of_a_bundle_is_an_error_not_a_panic() { + let (bytes, _) = write_bundle(&Session::default(), DEFAULT_BUNDLE_LIMITS).unwrap(); + for cut in 0..bytes.len() { + assert!(read_bundle(&bytes[..cut], DEFAULT_BUNDLE_LIMITS).is_err(), "accepted truncation at {cut}"); + } + assert!(read_bundle(&bytes, DEFAULT_BUNDLE_LIMITS).is_ok()); + } +} diff --git a/crates/scraft-format/src/clip.rs b/crates/scraft-format/src/clip.rs new file mode 100644 index 000000000..28e60b6d9 --- /dev/null +++ b/crates/scraft-format/src/clip.rs @@ -0,0 +1,544 @@ +//! Clips: windows onto audio sources or MIDI sequences, placed on a playlist. + +use crate::soundcraft_midi::Sequence; +use crate::soundcraft_time::{MAX_POSITION, Range, Samples}; +use crate::{ClipId, MAX_NAME_CHARS, SourceId, truncate_chars}; + +/// Fade curve shapes (Pro Tools offers standard, equal-power, S-curve and custom presets). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum FadeShape { + Linear, + #[default] + EqualPower, + SCurve, + Exponential, + Logarithmic, +} + +impl FadeShape { + pub const ALL: [FadeShape; 5] = [FadeShape::Linear, FadeShape::EqualPower, FadeShape::SCurve, FadeShape::Exponential, FadeShape::Logarithmic]; + + /// Fade-in gain at `t` ∈ [0,1] (fade-outs use `gain(1 - t)`). + pub fn gain(self, t: f32) -> f32 { + let t = if t.is_nan() { 0.0 } else { t.clamp(0.0, 1.0) }; + match self { + FadeShape::Linear => t, + FadeShape::EqualPower => (t * std::f32::consts::FRAC_PI_2).sin(), + FadeShape::SCurve => 0.5 - 0.5 * (t * std::f32::consts::PI).cos(), + FadeShape::Exponential => t * t * t, + FadeShape::Logarithmic => 1.0 - (1.0 - t) * (1.0 - t) * (1.0 - t), + } + } + + pub fn label(self) -> &'static str { + match self { + FadeShape::Linear => "Linear", + FadeShape::EqualPower => "Equal Power", + FadeShape::SCurve => "S-Curve", + FadeShape::Exponential => "Exponential", + FadeShape::Logarithmic => "Logarithmic", + } + } + + pub fn from_id(s: &str) -> Option { + let s = s.to_ascii_lowercase().replace(['-', '_', ' '], ""); + FadeShape::ALL.into_iter().find(|f| f.label().to_ascii_lowercase().replace(['-', ' '], "") == s) + } +} + +/// A fade at a clip boundary, `len` samples long. +#[derive(Debug, Clone, Copy, PartialEq, Default, serde::Serialize, serde::Deserialize)] +pub struct Fade { + pub len: Samples, + pub shape: FadeShape, + /// Continuous bend (-1..1); zero preserves the original shape exactly. + #[serde(default)] + pub curvature: f32, +} + +impl Fade { + /// The envelope used by both rendering and audio, with fixed zero/unity endpoints. + pub fn gain(self, t: f32) -> f32 { + let base = self.shape.gain(t); + let curvature = if self.curvature.is_finite() { self.curvature.clamp(-1.0, 1.0) } else { 0.0 }; + if curvature == 0.0 || base <= 0.0 || base >= 1.0 { + return base; + } + let weighted = base * (curvature * 4.0).exp2(); + weighted / (weighted + 1.0 - base) + } + + /// Invert the bend at the midpoint so a vertical drag follows the pointer. + pub fn curvature_for_midpoint_gain(self, gain: f32) -> f32 { + let gain = if gain.is_finite() { gain.clamp(0.001, 0.999) } else { self.gain(0.5) }; + let base = self.shape.gain(0.5); + (((gain / (1.0 - gain)).log2() - (base / (1.0 - base)).log2()) / 4.0).clamp(-1.0, 1.0) + } + + pub fn sanitize(&mut self) { + self.curvature = if self.curvature.is_finite() { self.curvature.clamp(-1.0, 1.0) } else { 0.0 }; + } +} + +/// What a clip plays. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub enum ClipContent { + /// Audio from `source`, starting `offset` samples into the file. + Audio { source: SourceId, offset: Samples }, + /// MIDI notes, with ticks relative to the clip start. + Midi { sequence: Sequence }, + /// Picture from the movie `source` ([`crate::VideoSource`], `Session::videos`), starting + /// `offset` samples into the movie. Video clips carry no audio. + Video { source: SourceId, offset: Samples }, +} + +/// A clip on a playlist. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Clip { + pub id: ClipId, + pub name: String, + /// Timeline start (samples). + pub start: Samples, + /// Length (samples, > 0). + pub length: Samples, + pub content: ClipContent, + /// Static clip gain in dB. + #[serde(default)] + pub gain_db: f32, + /// Clip gain envelope breakpoints: (offset from clip start, dB). Empty = flat `gain_db`. + #[serde(default)] + pub gain_env: Vec<(Samples, f32)>, + #[serde(default)] + pub fade_in: Fade, + #[serde(default)] + pub fade_out: Fade, + #[serde(default)] + pub muted: bool, + #[serde(default)] + pub edit_locked: bool, + #[serde(default)] + pub time_locked: bool, + #[serde(default)] + pub rating: u8, + /// Sync point offset from clip start. + #[serde(default)] + pub sync_point: Samples, + /// Optional colour override (RGB). + #[serde(default)] + pub color: Option<[u8; 3]>, + /// The key in [`crate::Session::auto_names`] of the names that editing gives the clips it + /// separates from this one (Clip List › Auto Rename). `None` keeps the clip's name. + #[serde(default)] + pub auto_name: Option, + /// Elastic / Vari-speed ratio (1.0 = none) — applied by rendered processing. + #[serde(default = "one")] + pub stretch: f64, + /// Reversed playback (AudioSuite Reverse renders into a new source; this is for previews). + #[serde(default)] + pub group: Option, + /// Marks the clips made by `clip.loop` (the origin and its copies share one id), so + /// `clip.unloop` still recognises the loop after the source's content is edited. + #[serde(default)] + pub loop_id: Option, +} + +fn one() -> f64 { + 1.0 +} + +impl Clip { + pub fn audio(id: ClipId, name: impl Into, source: SourceId, offset: Samples, start: Samples, length: Samples) -> Clip { + Clip { + id, + name: name.into(), + start, + length: length.max(1), + content: ClipContent::Audio { source, offset }, + gain_db: 0.0, + gain_env: Vec::new(), + fade_in: Fade::default(), + fade_out: Fade::default(), + muted: false, + edit_locked: false, + time_locked: false, + rating: 0, + sync_point: 0, + color: None, + auto_name: None, + stretch: 1.0, + group: None, + loop_id: None, + } + } + + pub fn midi(id: ClipId, name: impl Into, start: Samples, length: Samples, sequence: Sequence) -> Clip { + let mut c = Clip::audio(id, name, SourceId(0), 0, start, length); + c.content = ClipContent::Midi { sequence }; + c + } + + /// A clip showing the movie `source` from `offset` samples into it. + pub fn video(id: ClipId, name: impl Into, source: SourceId, offset: Samples, start: Samples, length: Samples) -> Clip { + let mut c = Clip::audio(id, name, SourceId(0), 0, start, length); + c.content = ClipContent::Video { source, offset }; + c + } + + pub fn end(&self) -> Samples { + self.start.saturating_add(self.length) + } + + pub fn range(&self) -> Range { + Range { start: self.start, end: self.end() } + } + + pub fn is_audio(&self) -> bool { + matches!(self.content, ClipContent::Audio { .. }) + } + + pub fn is_video(&self) -> bool { + matches!(self.content, ClipContent::Video { .. }) + } + + /// The audio source this clip plays (None for MIDI and video clips). + pub fn source(&self) -> Option { + match self.content { + ClipContent::Audio { source, .. } => Some(source), + ClipContent::Midi { .. } | ClipContent::Video { .. } => None, + } + } + + /// The movie this clip shows (video clips only). + pub fn video_source(&self) -> Option { + match self.content { + ClipContent::Video { source, .. } => Some(source), + ClipContent::Audio { .. } | ClipContent::Midi { .. } => None, + } + } + + /// Offset into the audio file or movie (0 for MIDI). + pub fn source_offset(&self) -> Samples { + match self.content { + ClipContent::Audio { offset, .. } | ClipContent::Video { offset, .. } => offset, + ClipContent::Midi { .. } => 0, + } + } + + /// Gain (linear) at `rel` samples from the clip start: clip gain × envelope × fades. + pub fn gain_at(&self, rel: Samples) -> f32 { + self.gain_at_with_fades(rel, self.fade_in, self.fade_out) + } + + /// Evaluate temporary fades while retaining clip gain and its envelope. + /// UI previews can use this without modifying the clip or rebuilding media. + pub fn gain_at_with_fades(&self, rel: Samples, fade_in: Fade, fade_out: Fade) -> f32 { + let mut db = self.gain_db; + if !self.gain_env.is_empty() { + db += env_value(&self.gain_env, rel); + } + let mut g = db_to_gain(db); + if fade_in.len > 0 && rel < fade_in.len { + g *= fade_in.gain(rel as f32 / fade_in.len as f32); + } + let from_end = self.length.saturating_sub(rel); + if fade_out.len > 0 && from_end <= fade_out.len { + g *= fade_out.gain(from_end as f32 / fade_out.len as f32); + } + g + } + + /// Gain envelope value in dB at `rel` samples from the clip start (0 without an envelope). + pub fn env_db_at(&self, rel: Samples) -> f32 { + env_value(&self.gain_env, rel) + } + + /// Move the start edge to `new_start` keeping material anchored (trim start). + pub fn trim_start_to(&mut self, new_start: Samples) { + // #907: these are public APIs, so they have to survive arguments a command would never + // pass (commands clamp to `MAX_POSITION` first). `Samples` is `i64`, so an extreme + // `new_start` against an extreme `start` wrapped in release and panicked in debug. + // Never so far back that the length would pass `MAX_POSITION`: trimming the start keeps the end. + let end = self.end(); + let new_start = new_start.max(end.saturating_sub(MAX_POSITION)).clamp(-MAX_POSITION, MAX_POSITION).min(end.saturating_sub(1)); + let delta = new_start.saturating_sub(self.start); + // The mixer plays a stretched audio clip from `offset + rel / stretch`. + let stretch = if self.stretch.is_finite() && self.stretch > 0.0 { self.stretch } else { 1.0 }; + match &mut self.content { + ClipContent::Audio { offset, .. } if (stretch - 1.0).abs() >= 1e-9 => { + *offset = offset.saturating_add((delta as f64 / stretch).round() as Samples); + } + ClipContent::Audio { offset, .. } | ClipContent::Video { offset, .. } => { + *offset = offset.saturating_add(delta); + } + ClipContent::Midi { .. } => {} + } + // MIDI note ticks are re-anchored by the engine, which knows the tempo map. + self.length = self.length.saturating_sub(delta).clamp(1, MAX_POSITION); + self.start = new_start; + if self.sync_point != 0 { + self.sync_point = self.sync_point.saturating_sub(delta); + } + self.drop_stray_sync(); + self.clamp_fades(); + // Keep the gain at the cut when earlier breakpoints are dropped. + if self.gain_env.iter().any(|&(t, _)| t < delta) && self.gain_env.iter().all(|&(t, _)| t != delta) { + self.gain_env.insert(0, (delta, env_value(&self.gain_env, delta))); + } + self.gain_env.retain(|&(t, _)| t >= delta); + self.gain_env.iter_mut().for_each(|(t, _)| *t = t.saturating_sub(delta)); + } + + /// Move the end edge to `new_end`. + pub fn trim_end_to(&mut self, new_end: Samples) { + // #907: saturating, like `trim_start_to`. + let new_end = new_end.clamp(-MAX_POSITION, MAX_POSITION).max(self.start.saturating_add(1)); + self.length = new_end.saturating_sub(self.start).clamp(1, MAX_POSITION); + self.drop_stray_sync(); + self.clamp_fades(); + } + + /// Keep both fades within the clip. + pub fn clamp_fades(&mut self) { + self.fade_in.len = self.fade_in.len.clamp(0, self.length); + self.fade_out.len = self.fade_out.len.clamp(0, self.length); + } + + /// A sync point (0 means none) that a trim left outside the clip no longer marks anything. + fn drop_stray_sync(&mut self) { + if !(0..self.length).contains(&self.sync_point) { + self.sync_point = 0; + } + } + + /// Repair a clip read from a file: positions and lengths within ±[`MAX_POSITION`], as commands + /// keep theirs, fades within the clip, a name that can be drawn, and a usable gain and stretch. + pub fn sanitize(&mut self) { + bound(&mut self.start); + self.length = self.length.clamp(1, MAX_POSITION); + bound(&mut self.sync_point); + self.gain_env.iter_mut().for_each(|(at, _)| bound(at)); + match &mut self.content { + ClipContent::Audio { offset, .. } | ClipContent::Video { offset, .. } => bound(offset), + ClipContent::Midi { sequence } => bound_sequence(sequence), + } + self.clamp_fades(); + self.fade_in.sanitize(); + self.fade_out.sanitize(); + truncate_chars(&mut self.name, MAX_NAME_CHARS); + if !self.gain_db.is_finite() { + self.gain_db = 0.0; + } + if !self.stretch.is_finite() || self.stretch <= 0.0 { + self.stretch = 1.0; + } + } +} + +pub(crate) fn bound(at: &mut Samples) { + *at = (*at).clamp(-MAX_POSITION, MAX_POSITION); +} + +pub(crate) fn bound_sequence(s: &mut Sequence) { + for n in &mut s.notes { + bound(&mut n.start); + n.length = n.length.clamp(1, MAX_POSITION); + } + s.ctrls.iter_mut().for_each(|c| bound(&mut c.tick)); +} + +fn env_value(env: &[(Samples, f32)], at: Samples) -> f32 { + let mut prev: Option<(Samples, f32)> = None; + for &(t, v) in env { + if t >= at { + return match prev { + Some((pt, pv)) if t > pt => { + let elapsed = i128::from(at).saturating_sub(i128::from(pt)); + let span = i128::from(t).saturating_sub(i128::from(pt)); + pv + (v - pv) * (elapsed as f32 / span as f32) + } + _ => v, + }; + } + prev = Some((t, v)); + } + prev.map_or(0.0, |(_, v)| v) +} + +pub(crate) fn db_to_gain(db: f32) -> f32 { + if db.is_nan() || db <= -144.0 { 0.0 } else { 10f32.powf(db.min(48.0) / 20.0) } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fade_shapes_hit_endpoints() { + for s in FadeShape::ALL { + assert!(s.gain(0.0).abs() < 1e-6, "{s:?}"); + assert!((s.gain(1.0) - 1.0).abs() < 1e-6, "{s:?}"); + assert_eq!(s.gain(f32::NAN), 0.0); + assert_eq!(FadeShape::from_id(s.label()), Some(s)); + } + } + + #[test] + fn fade_curvature_is_bounded_monotonic_and_inverts_at_the_midpoint() { + for shape in FadeShape::ALL { + let original = Fade { len: 1000, shape, curvature: 0.0 }; + for i in 0..=128 { + let t = i as f32 / 128.0; + assert_eq!(original.gain(t), shape.gain(t), "legacy curves must be unchanged"); + } + for curvature in [-1.0, -0.5, 0.0, 0.5, 1.0] { + let fade = Fade { curvature, ..original }; + assert_eq!(fade.gain(0.0), 0.0); + assert_eq!(fade.gain(1.0), 1.0); + let mut previous = 0.0; + for i in 0..=128 { + let gain = fade.gain(i as f32 / 128.0); + assert!(gain.is_finite() && (0.0..=1.0).contains(&gain)); + assert!(gain >= previous, "{shape:?}, {curvature}"); + previous = gain; + } + assert!((fade.curvature_for_midpoint_gain(fade.gain(0.5)) - curvature).abs() < 1e-5); + } + assert!((Fade { curvature: 0.5, ..original }).gain(0.5) > original.gain(0.5)); + assert!((Fade { curvature: -0.5, ..original }).gain(0.5) < original.gain(0.5)); + } + } + + #[test] + fn fade_curvature_loads_legacy_data_and_sanitizes_hostile_values() { + let fade: Fade = serde_json::from_str(r#"{"len":100,"shape":"SCurve"}"#).unwrap(); + assert_eq!(fade.curvature, 0.0); + for curvature in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -10.0, 10.0] { + let mut fade = Fade { curvature, ..fade }; + assert!(fade.gain(0.5).is_finite()); + fade.sanitize(); + assert!(fade.curvature.is_finite() && (-1.0..=1.0).contains(&fade.curvature)); + let roundtrip: Fade = serde_json::from_str(&serde_json::to_string(&fade).unwrap()).unwrap(); + assert_eq!(roundtrip, fade); + } + } + + #[test] + fn temporary_fades_keep_clip_gain_and_envelope_without_mutating_the_clip() { + let mut clip = Clip::audio(ClipId(1), "preview", SourceId(1), 0, 0, 1000); + clip.gain_db = -6.0; + clip.gain_env = vec![(0, 0.0), (1000, 6.0)]; + clip.fade_in = Fade { len: 100, shape: FadeShape::SCurve, curvature: 0.25 }; + clip.fade_out = Fade { len: 100, shape: FadeShape::Logarithmic, curvature: -0.1 }; + let before = clip.clone(); + let fade_in = Fade { len: 1000, shape: FadeShape::Linear, curvature: 0.5 }; + let fade_out = Fade { len: 1000, shape: FadeShape::Linear, curvature: -0.5 }; + assert!((clip.gain_at_with_fades(500, fade_in, fade_out) - db_to_gain(-3.0) * 0.16).abs() < 1e-6); + let mut committed = clip.clone(); + committed.fade_in = fade_in; + committed.fade_out = fade_out; + for rel in [0, 50, 500, 999, 1000] { + assert_eq!(clip.gain_at_with_fades(rel, fade_in, fade_out), committed.gain_at(rel)); + } + for rel in [i64::MIN, i64::MAX] { + assert!(clip.gain_at(rel).is_finite()); + assert!(clip.gain_at_with_fades(rel, fade_in, fade_out).is_finite()); + } + assert_eq!(clip, before); + clip.gain_env = vec![(i64::MIN, 0.0), (i64::MAX, 6.0)]; + assert!((clip.gain_at(500) - db_to_gain(-3.0)).abs() < 1e-6); + assert!((clip.gain_at_with_fades(500, fade_in, fade_out) - db_to_gain(-3.0) * 0.16).abs() < 1e-6); + } + + #[test] + fn gain_with_fades() { + let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 0, 100, 1000); + c.fade_in = Fade { len: 100, shape: FadeShape::Linear, curvature: 0.0 }; + c.fade_out = Fade { len: 100, shape: FadeShape::Linear, curvature: 0.0 }; + assert!(c.gain_at(0).abs() < 1e-6); + assert!((c.gain_at(50) - 0.5).abs() < 1e-6); + assert!((c.gain_at(500) - 1.0).abs() < 1e-6); + assert!((c.gain_at(950) - 0.5).abs() < 1e-6); + } + + #[test] + fn gain_envelope_interpolates() { + let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 0, 0, 1000); + c.gain_env = vec![(0, 0.0), (1000, -20.0)]; + assert!((c.gain_at(500) - db_to_gain(-10.0)).abs() < 1e-4); + } + + #[test] + fn trim_start_keeps_gain_envelope_at_cut() { + for (env, cut, expected) in [ + (vec![(0, 0.0), (1000, -20.0)], 500, vec![(0, -10.0), (500, -20.0)]), + (vec![(0, 0.0), (100, -20.0)], 500, vec![(0, -20.0)]), + (vec![(0, 0.0), (500, -10.0), (1000, -20.0)], 500, vec![(0, -10.0), (500, -20.0)]), + (vec![], 500, vec![]), + (vec![(100, -3.0)], 0, vec![(100, -3.0)]), + (vec![(100, -3.0)], -100, vec![(200, -3.0)]), + ] { + let mut clip = Clip::audio(ClipId(1), "a", SourceId(1), 100, 0, 1000); + clip.gain_env = env; + let gain_at_cut = clip.gain_at(cut); + clip.trim_start_to(cut); + assert_eq!(clip.gain_env, expected); + if cut > 0 { + assert!((clip.gain_at(0) - gain_at_cut).abs() < 1e-6); + } + } + } + + #[test] + fn trims_keep_material_anchored() { + let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 1000, 100, 1000); + c.trim_start_to(300); + assert_eq!((c.start, c.length, c.source_offset()), (300, 800, 1200)); + c.trim_end_to(500); + assert_eq!(c.length, 200); + c.trim_end_to(-50); + assert_eq!(c.length, 1); + // Regression: trimming the start must not leave a fade-out longer than the clip. + let mut c = Clip::audio(ClipId(2), "b", SourceId(1), 0, 0, 1000); + c.fade_out = Fade { len: 800, shape: FadeShape::Linear, curvature: 0.0 }; + c.trim_start_to(900); + assert!(c.fade_out.len <= c.length); + } + + /// #907: `trim_start_to` / `trim_end_to` are public APIs, so they have to survive arguments a + /// command would never pass. `Samples` is `i64`, so an extreme `new_start` against an extreme + /// `start` wrapped in release and panicked in debug. Every position stays within + /// `±MAX_POSITION` and every length within `1..=MAX_POSITION`. + #[test] + fn trims_saturate_on_extreme_arguments() { + for &new_start in &[Samples::MIN, -(MAX_POSITION * 2), -1, 0, MAX_POSITION, MAX_POSITION * 2, Samples::MAX] { + let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 1000, MAX_POSITION / 2, 1000); + c.trim_start_to(new_start); + assert!((-(MAX_POSITION as i128)..=(MAX_POSITION as i128)).contains(&(c.start as i128)), "start {new_start}: {}", c.start); + assert!((1..=MAX_POSITION).contains(&c.length), "start {new_start}: length {}", c.length); + assert_eq!(c.end(), MAX_POSITION / 2 + 1000, "start {new_start} moved the end"); + } + for &new_end in &[Samples::MIN, -(MAX_POSITION * 2), -1, 0, MAX_POSITION, MAX_POSITION * 2, Samples::MAX] { + let mut c = Clip::audio(ClipId(2), "b", SourceId(1), 1000, -(MAX_POSITION / 2), 1000); + c.trim_end_to(new_end); + assert!((1..=MAX_POSITION).contains(&c.length), "end {new_end}: length {}", c.length); + assert!(c.end() - c.start == c.length, "end {new_end}: start {} end {}", c.start, c.end()); + } + } + + #[test] + fn trim_start_of_stretched_audio_advances_offset_by_source_time() { + // The mixer plays source `offset + rel / stretch`, so a split at timeline 1000 of a 2x clip + // must start the right half at source sample 500. + let mut c = Clip::audio(ClipId(1), "a", SourceId(1), 0, 0, 2000); + c.stretch = 2.0; + c.trim_start_to(1000); + assert_eq!(c.source_offset(), 500); + let mut c = Clip::audio(ClipId(2), "b", SourceId(1), 0, 0, 2000); + c.stretch = 0.5; + c.trim_start_to(1000); + assert_eq!(c.source_offset(), 2000); + let mut c = Clip::audio(ClipId(3), "c", SourceId(1), 0, 0, 2000); + c.trim_start_to(1000); + assert_eq!(c.source_offset(), 1000); + } +} diff --git a/crates/scraft-format/src/ids.rs b/crates/scraft-format/src/ids.rs new file mode 100644 index 000000000..441ced1ae --- /dev/null +++ b/crates/scraft-format/src/ids.rs @@ -0,0 +1,21 @@ +//! Typed ids. All ids come from one session-wide counter so they never collide across kinds. + +macro_rules! id_type { + ($name:ident) => { + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default, serde::Serialize, serde::Deserialize)] + #[serde(transparent)] + pub struct $name(pub u64); + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } + } + }; +} + +id_type!(TrackId); +id_type!(ClipId); +id_type!(SourceId); +id_type!(BusId); +id_type!(GroupId); +id_type!(MarkerId); diff --git a/crates/scraft-format/src/lib.rs b/crates/scraft-format/src/lib.rs new file mode 100644 index 000000000..d26bca759 --- /dev/null +++ b/crates/scraft-format/src/lib.rs @@ -0,0 +1,53 @@ +//! The SoundCraft session document. +//! +//! A [`Session`] is pure data: tracks with playlists of clips, the mixer (inserts, sends, routing, +//! automation), tempo/meter, memory locations and groups. Audio sample data lives in +//! [`SourcePool`] behind `Arc`s, so cloning a session for undo is cheap. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +mod automation; +pub mod b64; +pub mod bundle; +mod clip; +mod ids; +mod markers; +mod mixer; +mod session; +mod source; +mod track; + +pub use craft_midi as soundcraft_midi; +pub use craft_time::soundcraft as soundcraft_time; + +pub use automation::{AutoParam, AutomationLane, AutomationMode, AutomationPoint}; +pub use bundle::{BUNDLE_EXTENSION, BundleError, BundleLimits, DEFAULT_BUNDLE_LIMITS, check_session_size, read_bundle, write_bundle}; +pub use clip::{Clip, ClipContent, Fade, FadeShape}; +pub use ids::{BusId, ClipId, GroupId, MarkerId, SourceId, TrackId}; +pub use markers::{Group, MarkerKind, MemoryLocation}; +pub use mixer::{ + Bus, ChannelFormat, INSERT_SLOTS, Insert, Mixer, OutputPath, Route, SEND_SLOTS, SendSlot, Speaker, SurroundPan, fader_db_to_pos, fader_pos_to_db, +}; +pub use session::{ + AutoName, BitDepthSetting, EditMode, EditState, FEET_FRAMES_START, MAX_COMMENT_CHARS, MAX_NAME_CHARS, SESSION_EXTENSION, Session, SessionError, + Tool, TrackView, ZoomState, ZoomToggle, truncate_chars, +}; +pub use source::{FileFormat, Source, SourceAudio, SourcePool, VideoSource}; +pub use track::{Playlist, TRACK_COLORS, Track, TrackHeight, TrackKind}; + +pub use soundcraft_time as time; + +/// Largest `.scraft` session opened or written by SoundCraft. +pub const MAX_SESSION_BYTES: u64 = 256 << 20; + +/// Read a bare `.scraft` file after enforcing the caller's byte limit. +pub fn read_session_bytes(name: &str, bytes: &[u8], limit: u64) -> Result { + check_session_size(name, u64::try_from(bytes.len()).unwrap_or(u64::MAX), limit, false)?; + String::from_utf8(bytes.to_vec()).map_err(|_| BundleError::Session(format!("{name}: not a SoundCraft session"))) +} + +/// Serialize a `.scraft` session and refuse output larger than the caller's read limit. +pub fn write_session(session: &Session, limit: u64) -> Result { + let text = session.to_json().map_err(|error| BundleError::Session(error.to_string()))?; + check_session_size("the session", u64::try_from(text.len()).unwrap_or(u64::MAX), limit, true)?; + Ok(text) +} diff --git a/crates/scraft-format/src/markers.rs b/crates/scraft-format/src/markers.rs new file mode 100644 index 000000000..eaa517dd2 --- /dev/null +++ b/crates/scraft-format/src/markers.rs @@ -0,0 +1,58 @@ +//! Memory locations (markers and stored selections) and mix/edit groups. + +use crate::soundcraft_time::Samples; +use crate::{GroupId, MarkerId, TrackId}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum MarkerKind { + /// A point marker on a ruler. + #[default] + Marker, + /// A stored selection range. + Selection, + /// A location that only recalls view settings. + None, +} + +/// A memory location (Window › Memory Locations). +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct MemoryLocation { + pub id: MarkerId, + /// User-visible number (1-based, unique). + pub number: u32, + pub name: String, + pub kind: MarkerKind, + pub start: Samples, + pub end: Samples, + #[serde(default)] + pub comments: String, + #[serde(default)] + pub color: Option<[u8; 3]>, + /// Marker ruler lane 1..5. + #[serde(default = "one")] + pub ruler: u8, + /// Track-lane marker on this track, if any. + #[serde(default)] + pub track: Option, +} + +fn one() -> u8 { + 1 +} + +/// A track group (Track › Group…). +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Group { + pub id: GroupId, + pub name: String, + /// Group letter id shown in the Groups list ("a", "b", …). + pub letter: char, + pub members: Vec, + pub edit: bool, + pub mix: bool, + pub active: bool, + pub color: [u8; 3], + /// Shared attributes linked by the group (volume, mute, solo, rec, etc.). + #[serde(default)] + pub attributes: Vec, +} diff --git a/crates/scraft-format/src/mixer.rs b/crates/scraft-format/src/mixer.rs new file mode 100644 index 000000000..efdaf2d17 --- /dev/null +++ b/crates/scraft-format/src/mixer.rs @@ -0,0 +1,714 @@ +//! Per-track mixer state: inserts, sends, routing, fader, pan, mute/solo. + +use crate::{AutomationMode, BusId, SourceId}; +use std::collections::BTreeMap; + +pub const INSERT_SLOTS: usize = 10; +pub const SEND_SLOTS: usize = 10; + +/// Channel formats (track widths). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum ChannelFormat { + #[default] + Mono, + Stereo, + Lcr, + Quad, + Lcrs, + Surround50, + Surround51, + Surround60, + Surround61, + Sdds70, + Sdds71, + Surround70, + Surround71, + Atmos702, + Atmos712, + Atmos502, + Atmos512, + Atmos504, + Atmos514, + Atmos704, + Atmos714, + Atmos706, + Atmos716, + Atmos904, + Atmos914, + Atmos906, + Atmos916, + /// Ambisonics of order 1..=7 ((n+1)² channels). + Ambisonics(u8), +} + +impl ChannelFormat { + pub const ALL: [ChannelFormat; 34] = [ + ChannelFormat::Mono, + ChannelFormat::Stereo, + ChannelFormat::Lcr, + ChannelFormat::Quad, + ChannelFormat::Lcrs, + ChannelFormat::Surround50, + ChannelFormat::Surround51, + ChannelFormat::Surround60, + ChannelFormat::Surround61, + ChannelFormat::Sdds70, + ChannelFormat::Sdds71, + ChannelFormat::Surround70, + ChannelFormat::Surround71, + ChannelFormat::Atmos702, + ChannelFormat::Atmos712, + ChannelFormat::Ambisonics(1), + ChannelFormat::Ambisonics(2), + ChannelFormat::Ambisonics(3), + ChannelFormat::Ambisonics(4), + ChannelFormat::Ambisonics(5), + ChannelFormat::Ambisonics(6), + ChannelFormat::Ambisonics(7), + ChannelFormat::Atmos502, + ChannelFormat::Atmos512, + ChannelFormat::Atmos504, + ChannelFormat::Atmos514, + ChannelFormat::Atmos704, + ChannelFormat::Atmos714, + ChannelFormat::Atmos706, + ChannelFormat::Atmos716, + ChannelFormat::Atmos904, + ChannelFormat::Atmos914, + ChannelFormat::Atmos906, + ChannelFormat::Atmos916, + ]; + pub fn channels(self) -> usize { + match self { + ChannelFormat::Mono => 1, + ChannelFormat::Stereo => 2, + ChannelFormat::Lcr => 3, + ChannelFormat::Quad | ChannelFormat::Lcrs => 4, + ChannelFormat::Surround50 => 5, + ChannelFormat::Surround51 | ChannelFormat::Surround60 => 6, + ChannelFormat::Surround61 | ChannelFormat::Sdds70 | ChannelFormat::Surround70 | ChannelFormat::Atmos502 => 7, + ChannelFormat::Sdds71 | ChannelFormat::Surround71 | ChannelFormat::Atmos512 => 8, + ChannelFormat::Atmos702 | ChannelFormat::Atmos504 => 9, + ChannelFormat::Atmos712 | ChannelFormat::Atmos514 => 10, + ChannelFormat::Atmos704 => 11, + ChannelFormat::Atmos714 => 12, + ChannelFormat::Atmos706 | ChannelFormat::Atmos904 => 13, + ChannelFormat::Atmos716 | ChannelFormat::Atmos914 => 14, + ChannelFormat::Atmos906 => 15, + ChannelFormat::Atmos916 => 16, + ChannelFormat::Ambisonics(n) => { + let n = usize::from(n.clamp(1, 7)) + 1; + n * n + } + } + } + pub fn label(self) -> &'static str { + match self { + ChannelFormat::Mono => "Mono", + ChannelFormat::Stereo => "Stereo", + ChannelFormat::Lcr => "LCR", + ChannelFormat::Quad => "Quad", + ChannelFormat::Lcrs => "LCRS", + ChannelFormat::Surround50 => "5.0", + ChannelFormat::Surround51 => "5.1", + ChannelFormat::Surround60 => "6.0", + ChannelFormat::Surround61 => "6.1", + ChannelFormat::Sdds70 => "7.0 SDDS", + ChannelFormat::Sdds71 => "7.1 SDDS", + ChannelFormat::Surround70 => "7.0", + ChannelFormat::Surround71 => "7.1", + ChannelFormat::Atmos702 => "7.0.2", + ChannelFormat::Atmos712 => "7.1.2", + ChannelFormat::Atmos502 => "5.0.2", + ChannelFormat::Atmos512 => "5.1.2", + ChannelFormat::Atmos504 => "5.0.4", + ChannelFormat::Atmos514 => "5.1.4", + ChannelFormat::Atmos704 => "7.0.4", + ChannelFormat::Atmos714 => "7.1.4", + ChannelFormat::Atmos706 => "7.0.6", + ChannelFormat::Atmos716 => "7.1.6", + ChannelFormat::Atmos904 => "9.0.4", + ChannelFormat::Atmos914 => "9.1.4", + ChannelFormat::Atmos906 => "9.0.6", + ChannelFormat::Atmos916 => "9.1.6", + ChannelFormat::Ambisonics(1) => "1st Order Ambisonics", + ChannelFormat::Ambisonics(2) => "2nd Order Ambisonics", + ChannelFormat::Ambisonics(3) => "3rd Order Ambisonics", + ChannelFormat::Ambisonics(4) => "4th Order Ambisonics", + ChannelFormat::Ambisonics(5) => "5th Order Ambisonics", + ChannelFormat::Ambisonics(6) => "6th Order Ambisonics", + ChannelFormat::Ambisonics(_) => "7th Order Ambisonics", + } + } + pub fn from_id(s: &str) -> Option { + ChannelFormat::ALL.into_iter().find(|f| f.label().eq_ignore_ascii_case(s.trim())) + } + /// Speakers in channel order, or empty for Ambisonics (ACN order, SN3D). + /// + /// Channel order is SMPTE / WAV order: speakers with a WAVE_FORMAT_EXTENSIBLE mask bit come in + /// ascending bit order (L R C LFE Ls Rs Lc Rc Cs Lss Rss Ltf Rtf Ltr Rtr; 7.x puts the rear + /// surrounds Lrs/Rrs on the "back" bits before the sides Lss/Rss), followed by speakers WAV + /// cannot name (the top-middle pair of .6 layouts). E.g. 5.1 = L R C LFE Ls Rs, + /// 7.1 = L R C LFE Lrs Rrs Lss Rss, 7.1.4 = L R C LFE Lrs Rrs Lss Rss Ltf Rtf Ltr Rtr. + pub fn speakers(self) -> &'static [Speaker] { + use Speaker::*; + match self { + ChannelFormat::Mono => &[C], + ChannelFormat::Stereo => &[L, R], + ChannelFormat::Lcr => &[L, R, C], + ChannelFormat::Quad => &[L, R, Ls, Rs], + ChannelFormat::Lcrs => &[L, R, C, Cs], + ChannelFormat::Surround50 => &[L, R, C, Ls, Rs], + ChannelFormat::Surround51 => &[L, R, C, Lfe, Ls, Rs], + ChannelFormat::Surround60 => &[L, R, C, Ls, Rs, Cs], + ChannelFormat::Surround61 => &[L, R, C, Lfe, Ls, Rs, Cs], + ChannelFormat::Sdds70 => &[L, R, C, Ls, Rs, Lc, Rc], + ChannelFormat::Sdds71 => &[L, R, C, Lfe, Ls, Rs, Lc, Rc], + ChannelFormat::Surround70 => &[L, R, C, Lrs, Rrs, Lss, Rss], + ChannelFormat::Surround71 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss], + ChannelFormat::Atmos702 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltm, Rtm], + ChannelFormat::Atmos712 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltm, Rtm], + ChannelFormat::Atmos502 => &[L, R, C, Ls, Rs, Ltm, Rtm], + ChannelFormat::Atmos512 => &[L, R, C, Lfe, Ls, Rs, Ltm, Rtm], + ChannelFormat::Atmos504 => &[L, R, C, Ls, Rs, Ltf, Rtf, Ltr, Rtr], + ChannelFormat::Atmos514 => &[L, R, C, Lfe, Ls, Rs, Ltf, Rtf, Ltr, Rtr], + ChannelFormat::Atmos704 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr], + ChannelFormat::Atmos714 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr], + ChannelFormat::Atmos706 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm], + ChannelFormat::Atmos716 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm], + ChannelFormat::Atmos904 => &[L, R, C, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr], + ChannelFormat::Atmos914 => &[L, R, C, Lfe, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr], + ChannelFormat::Atmos906 => &[L, R, C, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm], + ChannelFormat::Atmos916 => &[L, R, C, Lfe, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm], + ChannelFormat::Ambisonics(_) => &[], + } + } + pub fn is_ambisonic(self) -> bool { + matches!(self, ChannelFormat::Ambisonics(_)) + } + pub fn has_lfe(self) -> bool { + self.speakers().contains(&Speaker::Lfe) + } + pub fn has_height(self) -> bool { + self.speakers().iter().any(|s| s.is_height()) + } + /// Short per-channel labels for meters (`W Y Z X 5…` for Ambisonics). + pub fn channel_label(self, ch: usize) -> String { + match self.speakers().get(ch) { + Some(s) => s.label().to_string(), + None if self.is_ambisonic() => ["W", "Y", "Z", "X"].get(ch).map_or_else(|| format!("{}", ch + 1), ToString::to_string), + None => format!("{}", ch + 1), + } + } + /// WAVE_FORMAT_EXTENSIBLE channel mask (0 = no positions, e.g. Ambisonics). Speakers that WAV + /// cannot name (top middle) borrow the top-front bits when the layout has no top fronts. + pub fn channel_mask(self) -> u32 { + let sp = self.speakers(); + let has_tf = sp.contains(&Speaker::Ltf); + sp.iter().fold(0u32, |m, s| match s { + Speaker::Ltm if !has_tf => m | 0x1000, + Speaker::Rtm if !has_tf => m | 0x4000, + s => m | s.wave_bit(), + }) + } + /// Best format for a channel count. + pub fn for_channels(n: usize) -> ChannelFormat { + [ + ChannelFormat::Mono, + ChannelFormat::Stereo, + ChannelFormat::Lcr, + ChannelFormat::Quad, + ChannelFormat::Surround50, + ChannelFormat::Surround51, + ChannelFormat::Surround70, + ChannelFormat::Surround71, + ] + .into_iter() + .find(|f| f.channels() == n) + .unwrap_or(if n <= 1 { ChannelFormat::Mono } else { ChannelFormat::Stereo }) + } +} + +/// A loudspeaker position in a channel format. +/// +/// Angles follow ITU-R BS.775 / Dolby conventions: azimuth in degrees, 0 = front centre, positive +/// = to the right; elevation in degrees above the listener plane. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub enum Speaker { + L, + R, + C, + Lfe, + /// 5.x surrounds (±110°). + Ls, + Rs, + /// SDDS inner fronts (±15°). + Lc, + Rc, + /// Centre surround (180°). + Cs, + /// 7.x side surrounds (±90°). + Lss, + Rss, + /// 7.x rear surrounds (±150°). + Lrs, + Rrs, + /// 9.x front wides (±60°). + Lw, + Rw, + /// Top front (±45°, elevation 45°). + Ltf, + Rtf, + /// Top middle (±90°, elevation 45°). + Ltm, + Rtm, + /// Top rear (±135°, elevation 45°). + Ltr, + Rtr, +} + +impl Speaker { + pub fn azimuth(self) -> f32 { + match self { + Speaker::L => -30.0, + Speaker::R => 30.0, + Speaker::C | Speaker::Lfe => 0.0, + Speaker::Ls => -110.0, + Speaker::Rs => 110.0, + Speaker::Lc => -15.0, + Speaker::Rc => 15.0, + Speaker::Cs => 180.0, + Speaker::Lss => -90.0, + Speaker::Rss => 90.0, + Speaker::Lrs => -150.0, + Speaker::Rrs => 150.0, + Speaker::Lw => -60.0, + Speaker::Rw => 60.0, + Speaker::Ltf => -45.0, + Speaker::Rtf => 45.0, + Speaker::Ltm => -90.0, + Speaker::Rtm => 90.0, + Speaker::Ltr => -135.0, + Speaker::Rtr => 135.0, + } + } + pub fn elevation(self) -> f32 { + if self.is_height() { 45.0 } else { 0.0 } + } + pub fn is_height(self) -> bool { + matches!(self, Speaker::Ltf | Speaker::Rtf | Speaker::Ltm | Speaker::Rtm | Speaker::Ltr | Speaker::Rtr) + } + pub fn is_lfe(self) -> bool { + self == Speaker::Lfe + } + pub fn label(self) -> &'static str { + match self { + Speaker::L => "L", + Speaker::R => "R", + Speaker::C => "C", + Speaker::Lfe => "LFE", + Speaker::Ls => "Ls", + Speaker::Rs => "Rs", + Speaker::Lc => "Lc", + Speaker::Rc => "Rc", + Speaker::Cs => "Cs", + Speaker::Lss => "Lss", + Speaker::Rss => "Rss", + Speaker::Lrs => "Lrs", + Speaker::Rrs => "Rrs", + Speaker::Lw => "Lw", + Speaker::Rw => "Rw", + Speaker::Ltf => "Ltf", + Speaker::Rtf => "Rtf", + Speaker::Ltm => "Ltm", + Speaker::Rtm => "Rtm", + Speaker::Ltr => "Ltr", + Speaker::Rtr => "Rtr", + } + } + /// WAVE_FORMAT_EXTENSIBLE `dwChannelMask` bit (0 for speakers WAV cannot name). + pub fn wave_bit(self) -> u32 { + match self { + Speaker::L => 0x1, + Speaker::R => 0x2, + Speaker::C => 0x4, + Speaker::Lfe => 0x8, + Speaker::Ls | Speaker::Lrs => 0x10, + Speaker::Rs | Speaker::Rrs => 0x20, + Speaker::Lc | Speaker::Lw => 0x40, + Speaker::Rc | Speaker::Rw => 0x80, + Speaker::Cs => 0x100, + Speaker::Lss => 0x200, + Speaker::Rss => 0x400, + Speaker::Ltf => 0x1000, + Speaker::Rtf => 0x4000, + Speaker::Ltr => 0x8000, + Speaker::Rtr => 0x2_0000, + Speaker::Ltm | Speaker::Rtm => 0, + } + } +} + +/// Surround panner position (Pro Tools-style X/Y puck). `None` on a mixer means "use the stereo +/// pan", which keeps sessions made before surround mixing sounding exactly as they did. +#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] +#[serde(default)] +pub struct SurroundPan { + /// -1 (left) ..= 1 (right). + pub x: f32, + /// -1 (back) ..= 1 (front). + pub y: f32, + /// Elevation 0 (ear level) ..= 1 (overhead), used by height formats. + pub z: f32, + /// 0 = point source ..= 1 = spread over every speaker. + pub divergence: f32, + /// Centre speaker share of front-centre images, 0..=100 % (0 = phantom centre on L/R). + pub center: f32, + /// LFE send level, dB (-144 = off). + pub lfe_db: f32, +} + +impl Default for SurroundPan { + fn default() -> Self { + SurroundPan { x: 0.0, y: 1.0, z: 0.0, divergence: 0.0, center: 100.0, lfe_db: -144.0 } + } +} + +impl SurroundPan { + /// Puck at the stereo pan position (front edge). + pub fn from_stereo(pan: f32) -> Self { + SurroundPan { x: finite_or(pan, 0.0).clamp(-1.0, 1.0), ..SurroundPan::default() } + } + /// Clamp into range (NaN → defaults). + pub fn sanitize(&mut self) { + let d = SurroundPan::default(); + self.x = finite_or(self.x, d.x).clamp(-1.0, 1.0); + self.y = finite_or(self.y, d.y).clamp(-1.0, 1.0); + self.z = finite_or(self.z, d.z).clamp(0.0, 1.0); + self.divergence = finite_or(self.divergence, d.divergence).clamp(0.0, 1.0); + self.center = finite_or(self.center, d.center).clamp(0.0, 100.0); + self.lfe_db = finite_or(self.lfe_db, d.lfe_db).clamp(-144.0, 12.0); + } +} + +/// An internal mix bus (Pro Tools "bus" paths). +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Bus { + pub id: BusId, + pub name: String, + pub format: ChannelFormat, +} + +/// Physical output paths (from the I/O setup). +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct OutputPath { + pub name: String, + /// First hardware channel (0-based). + pub first_channel: u16, + pub format: ChannelFormat, +} + +/// Where a signal goes or comes from. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum Route { + #[default] + None, + /// The main output (hardware outputs 1-2, or the bounce source). + Main, + Bus(BusId), + /// Hardware input/output by path name. + Hardware(String), +} + +/// A plugin instance on an insert slot. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Insert { + /// Registry id from `soundcraft-dsp` (e.g. `eq7`, `comp`). + pub plugin: String, + #[serde(default, deserialize_with = "finite_params")] + pub params: BTreeMap, + #[serde(default)] + pub bypass: bool, + #[serde(default = "yes")] + pub active: bool, + /// Preset name shown in the plugin window header. + #[serde(default)] + pub preset: String, + /// Opaque plugin state (third-party plugins: CLAP state / VST3 component + controller + /// state), standard base64. Restored into a new instance before it first processes. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub state: Option, + /// The session audio source a sample-playing instrument (the built-in Sampler) plays. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub sample: Option, +} + +fn yes() -> bool { + true +} + +impl Insert { + pub fn new(plugin: impl Into) -> Self { + Insert { + plugin: plugin.into(), + params: BTreeMap::new(), + bypass: false, + active: true, + preset: String::from(""), + state: None, + sample: None, + } + } + + /// The decoded plugin state (`None` when absent or malformed). + pub fn state_bytes(&self) -> Option> { + self.state.as_deref().and_then(crate::b64::decode) + } + + /// Stores a plugin state blob (base64-encoded); an empty blob clears it. + pub fn set_state_bytes(&mut self, data: &[u8]) { + self.state = (!data.is_empty()).then(|| crate::b64::encode(data)); + } +} + +/// A send to a bus. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct SendSlot { + pub target: Route, + pub level_db: f32, + pub pan: f32, + pub mute: bool, + pub pre_fader: bool, + #[serde(default = "yes")] + pub follow_main_pan: bool, + /// Surround position into a multichannel bus (None = follow the track / stereo pan). + #[serde(default)] + pub surround: Option, +} + +impl SendSlot { + pub fn new(target: Route) -> Self { + SendSlot { target, level_db: -144.0, pan: 0.0, mute: false, pre_fader: false, follow_main_pan: true, surround: None } + } +} + +/// The channel strip. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Mixer { + pub volume_db: f32, + /// One panner per source channel for mono/stereo (stereo tracks: two panners, default hard L/R). + pub pan: Vec, + pub mute: bool, + pub solo: bool, + #[serde(default)] + pub solo_safe: bool, + /// Live record-enable state. Runtime-only: opening a session must never arm an input. + #[serde(skip)] + pub record_arm: bool, + /// Live input-monitor state. Runtime-only: opening a session must never enable monitoring. + #[serde(skip)] + pub input_monitor: bool, + #[serde(default)] + pub phase_invert: bool, + pub input: Route, + pub output: Route, + pub inserts: Vec>, + pub sends: Vec>, + pub automation_mode: AutomationMode, + /// Input gain / trim, dB. + #[serde(default)] + pub trim_db: f32, + /// VCA master that controls this track, if any (track id of the VCA). + #[serde(default)] + pub vca: Option, + /// Surround panner, used when the output is multichannel (None = the stereo pan to L/R). + #[serde(default)] + pub surround: Option, +} + +impl Mixer { + pub fn new(format: ChannelFormat) -> Self { + let pan = match format { + ChannelFormat::Mono => vec![0.0], + ChannelFormat::Stereo => vec![-1.0, 1.0], + _ => Vec::new(), + }; + Mixer { + volume_db: 0.0, + pan, + mute: false, + solo: false, + solo_safe: false, + record_arm: false, + input_monitor: false, + phase_invert: false, + input: Route::None, + output: Route::Main, + inserts: vec![None; INSERT_SLOTS], + sends: vec![None; SEND_SLOTS], + automation_mode: AutomationMode::Read, + trim_db: 0.0, + vca: None, + surround: None, + } + } + + /// Clamp everything into range (after deserialising untrusted files). + pub fn sanitize(&mut self) { + self.volume_db = finite_or(self.volume_db, 0.0).clamp(-144.0, 12.0); + for p in &mut self.pan { + *p = finite_or(*p, 0.0).clamp(-1.0, 1.0); + } + self.inserts.resize(INSERT_SLOTS, None); + self.sends.resize(SEND_SLOTS, None); + for s in self.sends.iter_mut().flatten() { + s.level_db = finite_or(s.level_db, -144.0).clamp(-144.0, 12.0); + s.pan = finite_or(s.pan, 0.0).clamp(-1.0, 1.0); + if let Some(sp) = &mut s.surround { + sp.sanitize(); + } + } + if let Some(sp) = &mut self.surround { + sp.sanitize(); + } + self.trim_db = finite_or(self.trim_db, 0.0).clamp(-144.0, 24.0); + } +} + +impl crate::Session { + /// The main mix format: the format of the first (main) output path, at least stereo and at + /// most 16 channels (wider formats fall back to stereo). + pub fn main_format(&self) -> ChannelFormat { + match self.outputs.first().map(|o| o.format) { + Some(f) if (2..=16).contains(&f.channels()) => f, + _ => ChannelFormat::Stereo, + } + } +} + +pub(crate) fn finite_or(v: f32, d: f32) -> f32 { + if v.is_finite() { v } else { d } +} + +/// Parameters saved as `null` (how JSON writes NaN and infinity) or beyond `f32` are dropped, so +/// they keep their defaults instead of failing the whole session. +fn finite_params<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { + let params: BTreeMap> = serde::Deserialize::deserialize(d)?; + Ok(params.into_iter().filter_map(|(k, v)| Some((k, v.map(|v| v as f32).filter(|v| v.is_finite())?))).collect()) +} + +/// Fader taper used by Pro Tools-style faders: position 0..1 ↔ dB (-inf..+12). +pub fn fader_pos_to_db(pos: f32) -> f32 { + let p = finite_or(pos, 0.0).clamp(0.0, 1.0); + if p <= 0.0 { + return -144.0; + } + // Piecewise: top 75 % covers -24..+12 linearly-ish, the bottom compresses to -inf. + if p >= 0.25 { -24.0 + (p - 0.25) / 0.75 * 36.0 } else { -24.0 - (1.0 - p / 0.25).powf(1.5) * 120.0 } +} + +pub fn fader_db_to_pos(db: f32) -> f32 { + let db = finite_or(db, -144.0); + if db <= -144.0 { + return 0.0; + } + if db >= -24.0 { + (0.25 + (db.min(12.0) + 24.0) / 36.0 * 0.75).min(1.0) + } else { + let x = ((-24.0 - db) / 120.0).clamp(0.0, 1.0).powf(1.0 / 1.5); + ((1.0 - x) * 0.25).max(0.0) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn insert_state_round_trips_through_json() { + let mut i = Insert::new("vst3:00"); + assert_eq!(i.state_bytes(), None); + let blob: Vec = (0..=255u8).cycle().take(1000).collect(); + i.set_state_bytes(&blob); + let json = serde_json::to_string(&i).unwrap(); + let back: Insert = serde_json::from_str(&json).unwrap(); + assert_eq!(back.state_bytes().unwrap(), blob); + i.set_state_bytes(&[]); + assert_eq!(i.state, None); + // Old sessions have no `state`; stateless inserts don't write one. + let old: Insert = serde_json::from_str(r#"{"plugin":"eq_7band"}"#).unwrap(); + assert_eq!(old.state, None); + assert!(!serde_json::to_string(&old).unwrap().contains("state")); + // A hostile state string decodes to nothing rather than failing the load. + let bad: Insert = serde_json::from_str(r#"{"plugin":"x","state":"!!!"}"#).unwrap(); + assert_eq!(bad.state_bytes(), None); + } + + #[test] + fn params_saved_as_null_or_out_of_range_are_dropped() { + let mut i = Insert::new("eq_7band"); + i.params.extend([("gain".to_string(), f32::NAN), ("q".to_string(), 0.7)]); + let back: Insert = serde_json::from_str(&serde_json::to_string(&i).unwrap()).unwrap(); + assert_eq!(back.params, BTreeMap::from([("q".to_string(), 0.7)])); + let big: Insert = serde_json::from_str(r#"{"plugin":"x","params":{"gain":1e39,"q":0.5}}"#).unwrap(); + assert_eq!(big.params, BTreeMap::from([("q".to_string(), 0.5)])); + } + + #[test] + fn fader_taper_round_trips() { + for db in [-100.0f32, -60.0, -24.0, -10.0, 0.0, 6.0, 12.0] { + let p = fader_db_to_pos(db); + assert!((fader_pos_to_db(p) - db).abs() < 0.05, "{db} → {p}"); + } + assert_eq!(fader_pos_to_db(0.0), -144.0); + assert_eq!(fader_db_to_pos(f32::NAN), 0.0); + } + + #[test] + fn mixer_defaults_and_sanitize() { + let mut m = Mixer::new(ChannelFormat::Stereo); + assert_eq!(m.pan, vec![-1.0, 1.0]); + m.volume_db = f32::INFINITY; + m.inserts.truncate(2); + m.sanitize(); + assert_eq!(m.volume_db, 0.0); + assert_eq!(m.inserts.len(), INSERT_SLOTS); + } + + #[test] + fn speaker_layouts_match_channel_counts_and_masks() { + for f in ChannelFormat::ALL { + if !f.is_ambisonic() { + assert_eq!(f.speakers().len(), f.channels(), "{f:?}"); + // Mask bits are unique and in ascending channel order. + let bits: Vec = f.speakers().iter().map(|s| s.wave_bit()).filter(|b| *b != 0).collect(); + assert!(bits.windows(2).all(|w| w[0] < w[1]), "{f:?} not in WAV order"); + } + } + assert_eq!(ChannelFormat::Surround51.channel_mask(), 0x3F); + assert_eq!(ChannelFormat::Surround71.channel_mask(), 0x63F); + assert_eq!(ChannelFormat::Atmos714.channel_mask(), 0x2_D63F); + assert_eq!(ChannelFormat::Ambisonics(1).channel_mask(), 0); + } + + #[test] + fn surround_pan_defaults_and_old_sessions() { + let m = Mixer::new(ChannelFormat::Mono); + assert_eq!(m.surround, None); + // A mixer saved before surround existed still loads. + let mut v = serde_json::to_value(&m).unwrap(); + v.as_object_mut().unwrap().remove("surround"); + let back: Mixer = serde_json::from_value(v).unwrap(); + assert_eq!(back.surround, None); + let mut sp: SurroundPan = serde_json::from_str(r#"{"x": 5.0}"#).unwrap(); + sp.sanitize(); + assert_eq!(sp.x, 1.0); + assert_eq!(sp.center, 100.0); + } + + #[test] + fn formats() { + assert_eq!(ChannelFormat::for_channels(6), ChannelFormat::Surround51); + assert_eq!(ChannelFormat::from_id("stereo"), Some(ChannelFormat::Stereo)); + } +} diff --git a/crates/scraft-format/src/session.rs b/crates/scraft-format/src/session.rs new file mode 100644 index 000000000..5f73f940e --- /dev/null +++ b/crates/scraft-format/src/session.rs @@ -0,0 +1,1319 @@ +//! The session: the whole document. + +use crate::clip::{bound, bound_sequence}; +use crate::soundcraft_time::{FrameRate, GridScale, GridValue, Range, SampleRate, Samples, TempoMap, TimeFormat}; +use crate::{ + Bus, BusId, ChannelFormat, Clip, ClipId, Group, GroupId, MarkerId, MarkerKind, MemoryLocation, OutputPath, Source, SourceId, SourcePool, + TRACK_COLORS, Track, TrackId, TrackKind, VideoSource, +}; + +/// Native session file extension. +pub const SESSION_EXTENSION: &str = "scraft"; + +/// `EditState::values` key holding the feet+frames reading of sample 0, in frames. +pub const FEET_FRAMES_START: &str = "feet_frames.start"; + +#[derive(Debug, Clone, PartialEq, thiserror::Error)] +pub enum SessionError { + #[error("not a SoundCraft session: {0}")] + Format(String), + #[error("session file version {0} is newer than this SoundCraft supports")] + Version(u32), + #[error("{0}")] + Invalid(String), +} + +/// Session bit depth for recording/rendering. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum BitDepthSetting { + Int16, + #[default] + Int24, + Float32, +} + +/// Edit modes. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum EditMode { + Shuffle, + #[default] + Slip, + Spot, + Grid, + /// Relative grid: moves keep their offset from the grid. + GridRelative, +} + +impl EditMode { + pub fn label(self) -> &'static str { + match self { + EditMode::Shuffle => "SHUFFLE", + EditMode::Slip => "SLIP", + EditMode::Spot => "SPOT", + EditMode::Grid => "GRID", + EditMode::GridRelative => "REL GRID", + } + } + pub fn from_id(s: &str) -> Option { + match s.to_ascii_lowercase().as_str() { + "shuffle" => Some(EditMode::Shuffle), + "slip" => Some(EditMode::Slip), + "spot" => Some(EditMode::Spot), + "grid" | "absolute_grid" => Some(EditMode::Grid), + "relative_grid" | "rel_grid" | "grid_relative" => Some(EditMode::GridRelative), + _ => None, + } + } + pub fn is_grid(self) -> bool { + matches!(self, EditMode::Grid | EditMode::GridRelative) + } +} + +/// Edit tools. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum Tool { + Zoom, + Trim, + #[default] + Selector, + Grabber, + Scrubber, + Pencil, + Smart, +} + +impl Tool { + pub const ALL: [Tool; 7] = [Tool::Zoom, Tool::Trim, Tool::Selector, Tool::Grabber, Tool::Scrubber, Tool::Pencil, Tool::Smart]; + pub fn id(self) -> &'static str { + match self { + Tool::Zoom => "zoom", + Tool::Trim => "trim", + Tool::Selector => "selector", + Tool::Grabber => "grabber", + Tool::Scrubber => "scrubber", + Tool::Pencil => "pencil", + Tool::Smart => "smart", + } + } + pub fn label(self) -> &'static str { + match self { + Tool::Zoom => "Zoomer", + Tool::Trim => "Trim", + Tool::Selector => "Selector", + Tool::Grabber => "Grabber", + Tool::Scrubber => "Scrubber", + Tool::Pencil => "Pencil", + Tool::Smart => "Smart Tool", + } + } + pub fn from_id(s: &str) -> Option { + Tool::ALL.into_iter().find(|t| t.id() == s.to_ascii_lowercase()) + } + /// F-key shortcut (F5..F10) like the incumbent. + pub fn fkey(self) -> Option<&'static str> { + match self { + Tool::Zoom => Some("F5"), + Tool::Trim => Some("F6"), + Tool::Selector => Some("F7"), + Tool::Grabber => Some("F8"), + Tool::Scrubber => Some("F9"), + Tool::Pencil => Some("F10"), + Tool::Smart => None, + } + } +} + +/// Track view lanes. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub enum TrackView { + Waveform, + Blocks, + Volume, + Pan, + Mute, +} + +/// Horizontal zoom + scroll. +#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct ZoomState { + /// Samples per pixel (point). + pub samples_per_px: f64, + /// Left edge of the visible timeline. + pub scroll: Samples, + /// Waveform vertical zoom factor. + pub waveform_zoom: f32, + /// Zoom preset memories 1..5. + pub presets: [f64; 5], +} + +impl Default for ZoomState { + /// Starts with a practical overview zoom and five reusable preset levels. + fn default() -> Self { + ZoomState { samples_per_px: 1024.0, scroll: 0, waveform_zoom: 1.0, presets: [64.0, 256.0, 1024.0, 4096.0, 16384.0] } + } +} + +/// The view Zoom Toggle goes back to. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct ZoomToggle { + pub samples_per_px: f64, + pub scroll: Samples, + pub heights: Vec<(TrackId, crate::TrackHeight)>, +} + +/// Edit-window state saved with the session (selection, tools, modes, view settings). +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct EditState { + pub tool: Tool, + pub edit_mode: EditMode, + /// The edit selection (a point = insertion position). + pub selection: Range, + /// Tracks in the edit selection. + pub selected_tracks: Vec, + /// Clips explicitly selected (Grabber selection). + #[serde(default)] + pub selected_clips: Vec, + /// Audio files selected in the Clip List. + #[serde(default)] + pub selected_sources: Vec, + /// Playhead position when stopped. + pub playhead: Samples, + pub grid: GridValue, + pub nudge: GridValue, + /// Grid/Nudge time scales follow the main counter when enabled. + #[serde(default)] + pub grid_follow_main: bool, + #[serde(default)] + pub nudge_follow_main: bool, + /// Grid lines and snapping use visible clip boundaries and memory locations while the numeric + /// Grid value is retained for when this mode is left. + #[serde(default)] + pub grid_clips_markers: bool, + #[serde(default = "yes")] + pub grid_lines: bool, + pub main_counter: TimeFormat, + pub sub_counter: Option, + pub zoom: ZoomState, + /// Set while Zoom Toggle is on. + pub zoom_toggle: Option, + /// Visible rulers. + pub rulers: Vec, + pub link_timeline_edit: bool, + pub link_track_edit: bool, + pub insertion_follows_playback: bool, + pub loop_playback: bool, + pub pre_post_roll: bool, + pub pre_roll: Samples, + pub post_roll: Samples, + pub tab_to_transient: bool, + pub automation_follows_edit: bool, + pub markers_follow_edit: bool, + pub layered_editing: bool, + pub mirrored_midi: bool, + pub click: bool, + pub countoff: bool, + pub countoff_bars: u32, + pub midi_merge: bool, + pub record_mode: String, + pub loop_record: bool, + pub quickpunch: bool, + pub scrolling: String, + pub solo_mode: String, + pub pre_fader_metering: bool, + pub delay_compensation: bool, + pub keyboard_focus: String, + /// Timeline selection when it is unlinked from the edit selection. + pub timeline_selection: Range, + /// Generic on/off view and option flags keyed by id (e.g. `view.clip.name`, `waveform.rectified`, + /// `options.midi_thru`). Saved with the session so every view setting persists. + #[serde(default)] + pub flags: std::collections::BTreeSet, + /// Numeric settings keyed by id (e.g. `click.volume_db`, `engine.buffer_size`). + #[serde(default)] + pub values: std::collections::BTreeMap, +} + +impl EditState { + pub fn flag(&self, id: &str) -> bool { + self.flags.contains(id) + } + pub fn set_flag(&mut self, id: &str, on: bool) { + if on { + self.flags.insert(id.to_string()); + } else { + self.flags.remove(id); + } + } + pub fn value(&self, id: &str, default: f64) -> f64 { + self.values.get(id).copied().filter(|v| v.is_finite()).unwrap_or(default) + } +} + +fn yes() -> bool { + true +} + +impl Default for EditState { + /// Starts the Edit window in Slip mode with independent Grid and Nudge scales. + fn default() -> Self { + EditState { + tool: Tool::Smart, + edit_mode: EditMode::Slip, + selection: Range::default(), + selected_tracks: Vec::new(), + selected_clips: Vec::new(), + selected_sources: Vec::new(), + playhead: 0, + grid: GridValue::default(), + nudge: GridValue::Seconds(1.0), + grid_follow_main: false, + nudge_follow_main: false, + grid_clips_markers: false, + grid_lines: true, + main_counter: TimeFormat::MinSecs, + sub_counter: Some(TimeFormat::Samples), + zoom: ZoomState::default(), + zoom_toggle: None, + rulers: vec!["bars_beats".into(), "min_secs".into(), "tempo".into(), "meter".into(), "markers".into()], + link_timeline_edit: true, + link_track_edit: false, + insertion_follows_playback: false, + loop_playback: false, + pre_post_roll: false, + pre_roll: 96_000, + post_roll: 96_000, + tab_to_transient: false, + automation_follows_edit: true, + markers_follow_edit: false, + layered_editing: false, + mirrored_midi: false, + click: false, + countoff: false, + countoff_bars: 2, + midi_merge: false, + record_mode: "normal".into(), + loop_record: false, + quickpunch: false, + scrolling: "page".into(), + solo_mode: "sip".into(), + pre_fader_metering: false, + delay_compensation: true, + keyboard_focus: "commands".into(), + timeline_selection: Range::default(), + flags: ["view.clip.name", "view.clip.gain_line", "waveform.peak", "view.clip.overlap_shadows", "view.marker.ruler_lines"] + .iter() + .map(ToString::to_string) + .collect(), + values: std::collections::BTreeMap::new(), + } + } +} + +/// Clip List › Auto Rename: the names that editing gives the clips it separates from a clip. +/// Each new clip is named `name`, then `zeros` zeros, then `number`, then `suffix`, and `number` +/// counts up by one for the next clip. +#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)] +pub struct AutoName { + pub name: String, + /// The number the next new clip gets. + pub number: u64, + pub zeros: usize, + pub suffix: String, + /// Tells this scheme from another one under the same key after an undo or in another session, + /// for pastes from the clipboard. 0 matches no scheme. + #[serde(default)] + pub id: u64, +} + +impl AutoName { + /// The largest starting number. + pub const MAX_NUMBER: u64 = 999_999_999; + /// The most zeros before the number. + pub const MAX_ZEROS: usize = 9; + + /// Room for `name` and `suffix` together: 29 characters stay free for 9 zeros and the 20 + /// digits of a u64, so a clip name never loses its number to [`MAX_NAME_CHARS`]. + pub const MAX_TEXT_CHARS: usize = MAX_NAME_CHARS - 29; + + /// Repair a scheme read from a file: names that can be drawn and at most [`Self::MAX_ZEROS`] zeros. + pub fn sanitize(&mut self) { + truncate_chars(&mut self.name, Self::MAX_TEXT_CHARS); + truncate_chars(&mut self.suffix, Self::MAX_TEXT_CHARS - self.name.chars().count()); + self.zeros = self.zeros.min(Self::MAX_ZEROS); + } +} + +/// The document. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Session { + pub format_version: u32, + pub name: String, + pub sample_rate: SampleRate, + pub bit_depth: BitDepthSetting, + pub frame_rate: FrameRate, + /// Timecode of sample 0 (session start), as a sample count. + pub timecode_start: Samples, + pub tempo: TempoMap, + #[serde(default)] + pub key_signatures: Vec<(Samples, String)>, + pub tracks: Vec, + pub sources: Vec, + /// Movies referenced by Video track clips. + #[serde(default)] + pub videos: Vec, + pub busses: Vec, + pub outputs: Vec, + pub markers: Vec, + pub groups: Vec, + /// Clip List › Auto Rename schemes, keyed by the ids in [`Clip::auto_name`]. The clips separated + /// from one clip share its scheme, so they count up one number sequence. + #[serde(default)] + pub auto_names: std::collections::BTreeMap, + pub edit: EditState, + pub next_id: u64, + #[serde(default)] + pub comments: String, + /// Original MIDI performances keyed by clip id (Event › MIDI Operations › Restore/Flatten + /// Performance). Independent of undo so a restore survives later edits. + #[serde(default)] + pub midi_originals: std::collections::BTreeMap, + #[serde(skip)] + pub pool: SourcePool, + /// The folder relative source paths point into: where the session was opened or last saved. + #[serde(skip)] + pub folder: Option, +} + +impl Default for Session { + fn default() -> Self { + Session::new("Untitled", SampleRate::HZ_48000) + } +} + +impl Session { + pub const FORMAT_VERSION: u32 = 1; + + pub fn new(name: impl Into, sample_rate: SampleRate) -> Self { + let mut s = Session { + format_version: Self::FORMAT_VERSION, + name: name.into(), + sample_rate, + bit_depth: BitDepthSetting::Int24, + frame_rate: FrameRate::Fps30, + timecode_start: 0, + tempo: TempoMap::default(), + key_signatures: Vec::new(), + tracks: Vec::new(), + sources: Vec::new(), + videos: Vec::new(), + busses: Vec::new(), + outputs: vec![OutputPath { name: "Out 1-2".into(), first_channel: 0, format: ChannelFormat::Stereo }], + markers: Vec::new(), + groups: Vec::new(), + auto_names: std::collections::BTreeMap::new(), + edit: EditState::default(), + next_id: 1, + comments: String::new(), + midi_originals: std::collections::BTreeMap::new(), + pool: SourcePool::default(), + folder: None, + }; + s.edit.pre_roll = sample_rate.samples(2.0); + s.edit.post_roll = sample_rate.samples(2.0); + s + } + + /// Allocate a fresh id. + pub fn alloc(&mut self) -> u64 { + let id = self.next_id.max(1); + self.next_id = id.saturating_add(1); + id + } + + // ---- lookups ------------------------------------------------------------------------- + + pub fn track(&self, id: TrackId) -> Option<&Track> { + self.tracks.iter().find(|t| t.id == id) + } + pub fn track_mut(&mut self, id: TrackId) -> Option<&mut Track> { + self.tracks.iter_mut().find(|t| t.id == id) + } + pub fn track_index(&self, id: TrackId) -> Option { + self.tracks.iter().position(|t| t.id == id) + } + pub fn track_by_name(&self, name: &str) -> Option<&Track> { + self.tracks.iter().find(|t| t.name == name).or_else(|| self.tracks.iter().find(|t| t.name.eq_ignore_ascii_case(name))) + } + pub fn master(&self) -> Option<&Track> { + self.tracks.iter().find(|t| t.kind == TrackKind::Master) + } + pub fn source(&self, id: SourceId) -> Option<&Source> { + self.sources.iter().find(|s| s.id == id) + } + pub fn video(&self, id: SourceId) -> Option<&VideoSource> { + self.videos.iter().find(|v| v.id == id) + } + pub fn bus(&self, id: BusId) -> Option<&Bus> { + self.busses.iter().find(|b| b.id == id) + } + pub fn bus_by_name(&self, name: &str) -> Option<&Bus> { + self.busses.iter().find(|b| b.name.eq_ignore_ascii_case(name)) + } + pub fn group(&self, id: GroupId) -> Option<&Group> { + self.groups.iter().find(|g| g.id == id) + } + pub fn marker(&self, id: MarkerId) -> Option<&MemoryLocation> { + self.markers.iter().find(|m| m.id == id) + } + + /// Find a clip anywhere (active playlists). + pub fn find_clip(&self, id: ClipId) -> Option<(TrackId, &Clip)> { + self.tracks.iter().find_map(|t| t.clips().iter().find(|c| c.id == id).map(|c| (t.id, c))) + } + + pub fn find_clip_mut(&mut self, id: ClipId) -> Option<&mut Clip> { + self.tracks.iter_mut().find_map(|t| t.playlist_mut().and_then(|p| p.clip_mut(id))) + } + + /// Clips on every playlist, alternates included. + pub fn all_clips(&self) -> impl Iterator { + self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| &p.clips)) + } + + pub fn clips_using(&self, src: SourceId) -> impl Iterator { + self.all_clips().filter(move |c| c.source() == Some(src)) + } + + /// End of the last clip on any track (session length). + pub fn content_end(&self) -> Samples { + self.tracks.iter().filter_map(|t| t.playlist().map(|p| p.end())).max().unwrap_or(0) + } + + /// Feet+frames reading of sample 0, in frames (Setup › Current Feet+Frames Position). + pub fn feet_frames_start(&self) -> i64 { + self.edit.value(FEET_FRAMES_START, 0.0) as i64 + } + + /// Format a position against this session's timecode and feet+frames references. + pub fn format_position(&self, at: Samples, fmt: TimeFormat) -> String { + crate::soundcraft_time::format_position( + at, + fmt, + self.sample_rate, + &self.tempo, + self.frame_rate, + self.timecode_start, + self.feet_frames_start(), + ) + } + + /// Parse a position typed in `fmt` against this session's references. + pub fn parse_position(&self, text: &str, fmt: TimeFormat) -> Result { + crate::soundcraft_time::parse_position( + text, + fmt, + self.sample_rate, + &self.tempo, + self.frame_rate, + self.timecode_start, + self.feet_frames_start(), + ) + } + + /// Whether the Edit window shows a track: unhidden, with every parent folder open. + pub fn track_shown(&self, track: &Track) -> bool { + if track.hidden { + return false; + } + let mut folder = track.folder; + for _ in 0..64 { + let Some(parent) = folder.and_then(|id| self.track(id)) else { return true }; + if !parent.folder_open { + return false; + } + folder = parent.folder; + } + false + } + + /// Collects unique visible active-playlist clip boundaries and memory-location boundaries. + fn clip_marker_grid_points(&self, keep: impl Fn(Samples) -> bool, excluded: &[ClipId], include_zero: bool) -> Vec { + let mut points = Vec::new(); + let excluded: std::collections::HashSet = excluded.iter().copied().collect(); + if include_zero && keep(0) { + points.push(0); + } + for clip in + self.tracks.iter().filter(|track| self.track_shown(track)).flat_map(|track| track.clips()).filter(|clip| !excluded.contains(&clip.id)) + { + points.extend([clip.start, clip.end()].into_iter().filter(|point| keep(*point))); + } + for marker in &self.markers { + if keep(marker.start) { + points.push(marker.start); + } + if marker.end != marker.start && keep(marker.end) { + points.push(marker.end); + } + } + points.sort_unstable(); + points.dedup(); + points + } + + /// Sample-domain Timecode reference; Feet+Frames applies its origin in frames separately. + fn grid_origin(&self) -> Samples { + match self.edit.grid.scale() { + GridScale::Timecode => self.timecode_start, + _ => 0, + } + } + + /// Grid lines for the current Grid value, including clip and marker boundaries. + pub fn grid_lines(&self, start: Samples, end: Samples, max: usize) -> Vec { + if !self.edit.grid_clips_markers { + if let GridValue::FeetFrames(value) = self.edit.grid { + return value.lines(start, end, self.sample_rate, self.frame_rate, self.feet_frames_start(), max); + } + let origin = self.grid_origin(); + return self + .edit + .grid + .lines(start.saturating_add(origin), end.saturating_add(origin), self.sample_rate, &self.tempo, self.frame_rate, max) + .into_iter() + .map(|point| point.saturating_sub(origin)) + .collect(); + } + let points = self.clip_marker_grid_points(|point| point >= start && point < end, &[], true); + if points.len() <= max { + return points; + } + match max { + 0 => Vec::new(), + 1 => points.first().copied().into_iter().collect(), + _ => { + let span = i128::from(end).saturating_sub(i128::from(start)).max(1); + let mut bucket = None; + points + .into_iter() + .filter(|point| { + let next = i128::from(*point).saturating_sub(i128::from(start)).saturating_mul(max as i128) / span; + if bucket == Some(next) { + false + } else { + bucket = Some(next); + true + } + }) + .collect() + } + } + } + + /// The current Grid line at or before `position`. + pub fn grid_line_at_or_before(&self, position: Samples) -> Samples { + self.grid_line_at_or_before_excluding(position, &[]) + } + + /// Find the current Grid line at or before `position` without boundaries of `excluded` clips. + pub fn grid_line_at_or_before_excluding(&self, position: Samples, excluded: &[ClipId]) -> Samples { + if !self.edit.grid_clips_markers { + if let GridValue::FeetFrames(value) = self.edit.grid { + return value.line_at_or_before(position, self.sample_rate, self.frame_rate, self.feet_frames_start()); + } + let origin = self.grid_origin(); + return self + .edit + .grid + .line_at_or_before(position.saturating_add(origin), self.sample_rate, &self.tempo, self.frame_rate) + .saturating_sub(origin); + } + self.clip_marker_grid_points(|_| true, excluded, true).into_iter().take_while(|point| *point <= position).last().unwrap_or(position) + } + + /// Snap to the nearest line in the current Grid. + pub fn snap_to_grid(&self, position: Samples) -> Samples { + self.snap_to_grid_excluding(position, &[]) + } + + /// Snap to the nearest current Grid line without using boundaries of `excluded` clips. + pub fn snap_to_grid_excluding(&self, position: Samples, excluded: &[ClipId]) -> Samples { + if !self.edit.grid_clips_markers { + if let GridValue::FeetFrames(value) = self.edit.grid { + return value.snap(position, self.sample_rate, self.frame_rate, self.feet_frames_start()); + } + let origin = self.grid_origin(); + return self.edit.grid.snap(position.saturating_add(origin), self.sample_rate, &self.tempo, self.frame_rate).saturating_sub(origin); + } + let mut points = self.clip_marker_grid_points(|_| true, excluded, false); + if !points.is_empty() { + points.push(0); + } + points.into_iter().min_by_key(|point| i128::from(*point).saturating_sub(i128::from(position)).abs()).unwrap_or(position) + } + + pub fn next_track_color(&self) -> [u8; 3] { + let n = self.tracks.iter().filter(|t| t.kind != TrackKind::Master).count(); + TRACK_COLORS.get(n % TRACK_COLORS.len()).copied().unwrap_or([128, 128, 128]) + } + + /// A unique track name: `base`, or `base 1`, `base 2` … like the incumbent ("Audio 1"). + pub fn unique_track_name(&self, base: &str) -> String { + let taken = |n: &str| self.tracks.iter().any(|t| t.name == n); + if !base.is_empty() && !taken(base) && !matches!(base, "Audio" | "Aux" | "MIDI" | "Inst" | "VCA" | "Folder" | "Master") { + return base.to_string(); + } + let prefix = format!("{base} "); + let used: std::collections::HashSet = self + .tracks + .iter() + .filter_map(|t| t.name.strip_prefix(&prefix)) + .filter_map(|n| n.parse().ok().filter(|i: &u64| i.to_string() == n)) + .collect(); + let i = (1..).find(|i| !used.contains(i)).unwrap_or(1); + format!("{base} {i}") + } + + /// Add a track with defaults; returns its id. + pub fn add_track(&mut self, kind: TrackKind, format: ChannelFormat, name: Option<&str>) -> TrackId { + let id = TrackId(self.alloc()); + let name = self.unique_track_name(name.unwrap_or(kind.default_name())); + let color = if kind == TrackKind::Master { [140, 140, 140] } else { self.next_track_color() }; + let mut t = Track::new(id, name, kind, format, color); + if kind == TrackKind::Master { + t.mixer.output = crate::Route::Main; + t.mixer.input = crate::Route::Main; + } + // Master faders sit at the bottom like the incumbent; others go before any master. + let pos = if kind == TrackKind::Master { + self.tracks.len() + } else { + self.tracks.iter().position(|t| t.kind == TrackKind::Master).unwrap_or(self.tracks.len()) + }; + self.tracks.insert(pos, t); + id + } + + pub fn add_bus(&mut self, name: &str, format: ChannelFormat) -> BusId { + if let Some(b) = self.bus_by_name(name) { + return b.id; + } + let id = BusId(self.alloc()); + self.busses.push(Bus { id, name: name.to_string(), format }); + id + } + + /// Add a memory location; returns its id. + pub fn add_marker(&mut self, name: &str, kind: MarkerKind, start: Samples, end: Samples) -> MarkerId { + let id = MarkerId(self.alloc()); + let number = self.markers.iter().map(|m| m.number).max().unwrap_or(0).saturating_add(1); + self.markers.push(MemoryLocation { + id, + number, + name: name.to_string(), + kind, + start, + end: end.max(start), + comments: String::new(), + color: None, + ruler: 1, + track: None, + }); + self.markers.sort_by_key(|m| m.start); + id + } + + pub fn new_clip_id(&mut self) -> ClipId { + ClipId(self.alloc()) + } + + /// Drop the Auto Rename schemes that no clip on any playlist uses. + pub fn prune_auto_names(&mut self) { + let used: std::collections::BTreeSet = self.all_clips().filter_map(|c| c.auto_name).collect(); + self.auto_names.retain(|k, _| used.contains(k)); + } + + // ---- persistence --------------------------------------------------------------------- + + pub fn to_json(&self) -> Result { + serde_json::to_string_pretty(self).map_err(|e| SessionError::Format(e.to_string())) + } + + /// Parse a session; decoded audio must be attached separately (`pool`). + /// + /// The text is parsed straight into the document, never into a `serde_json::Value` tree + /// first (which takes many times the file's size). Only the version is read before that. + /// A field of the document given twice in one object is refused (`duplicate field`), as no + /// writer repeats one. Keys of maps (plugin params, edit values, Auto Rename schemes, MIDI + /// originals) and unknown keys keep the last value given, as before. + pub fn from_json(text: &str) -> Result { + /// The version alone: every other field is skipped, not kept. + #[derive(serde::Deserialize)] + struct Head { + format_version: Option, + } + // `Head` would also take a JSON array as its fields in order: only an object has a version. + let object = text.trim_start_matches([' ', '\t', '\n', '\r']).starts_with('{'); + let version = match object.then(|| serde_json::from_str::(text)) { + Some(Ok(head)) => head.format_version, + // A version given twice is that error, not a missing version. + Some(Err(e)) if e.to_string().starts_with("duplicate field") => return Err(SessionError::Format(e.to_string())), + // Valid JSON of another shape (not an object, or a version that is not a number) + // has no version; anything else is not JSON. + _ => { + serde_json::from_str::(text).map_err(|e| SessionError::Format(e.to_string()))?; + None + } + }; + let ver = version.and_then(|v| v.as_u64()).ok_or_else(|| SessionError::Format("missing format_version".into()))?; + if ver > u64::from(Self::FORMAT_VERSION) { + return Err(SessionError::Version(u32::try_from(ver).unwrap_or(u32::MAX))); + } + let mut s: Session = serde_json::from_str(text).map_err(|e| SessionError::Format(e.to_string()))?; + s.sanitize(); + s.validate()?; + Ok(s) + } + + /// Repair invariants after loading untrusted data. + pub fn sanitize(&mut self) { + let max_id = self + .tracks + .iter() + .map(|t| t.id.0) + .chain(self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| p.clips.iter().map(|c| c.id.0)))) + .chain(self.sources.iter().map(|s| s.id.0)) + .chain(self.auto_names.keys().copied()) + .chain(self.videos.iter().map(|v| v.id.0)) + .chain(self.busses.iter().map(|b| b.id.0)) + .chain(self.markers.iter().map(|m| m.id.0)) + .chain(self.groups.iter().map(|g| g.id.0)) + .max() + .unwrap_or(0); + self.next_id = self.next_id.max(max_id.saturating_add(1)); + truncate_chars(&mut self.name, MAX_NAME_CHARS); + truncate_chars(&mut self.comments, MAX_COMMENT_CHARS); + for t in &mut self.tracks { + t.mixer.sanitize(); + truncate_chars(&mut t.name, MAX_NAME_CHARS); + truncate_chars(&mut t.comments, MAX_COMMENT_CHARS); + if t.playlists.is_empty() { + t.playlists.push(crate::Playlist::new(t.name.clone())); + } + if t.active_playlist >= t.playlists.len() { + t.active_playlist = 0; + } + for p in &mut t.playlists { + truncate_chars(&mut p.name, MAX_NAME_CHARS); + p.clips.retain(|c| c.length > 0); + p.clips.iter_mut().for_each(Clip::sanitize); + p.sort(); + } + for l in &mut t.automation { + l.points.retain(|p| p.value.is_finite()); + let (lo, hi, _) = l.param.range(); + l.points.iter_mut().for_each(|p| p.value = p.value.clamp(lo, hi)); + l.points.iter_mut().for_each(|p| bound(&mut p.at)); + l.points.sort_by_key(|p| p.at); + } + } + for m in &mut self.markers { + bound(&mut m.start); + bound(&mut m.end); + truncate_chars(&mut m.name, MAX_NAME_CHARS); + truncate_chars(&mut m.comments, MAX_COMMENT_CHARS); + } + for (at, key) in &mut self.key_signatures { + bound(at); + truncate_chars(key, MAX_NAME_CHARS); + } + self.midi_originals.values_mut().for_each(bound_sequence); + let e = &mut self.edit; + for at in [ + &mut e.selection.start, + &mut e.selection.end, + &mut e.timeline_selection.start, + &mut e.timeline_selection.end, + &mut e.playhead, + &mut e.pre_roll, + &mut e.post_roll, + &mut e.zoom.scroll, + &mut self.timecode_start, + ] { + bound(at); + } + for v in &mut self.videos { + if !v.frame_rate.is_finite() || v.frame_rate < 0.0 { + v.frame_rate = 0.0; + } + if !v.duration.is_finite() || v.duration < 0.0 { + v.duration = 0.0; + } + truncate_chars(&mut v.name, MAX_NAME_CHARS); + } + self.busses.iter_mut().for_each(|b| truncate_chars(&mut b.name, MAX_NAME_CHARS)); + self.outputs.iter_mut().for_each(|o| truncate_chars(&mut o.name, MAX_NAME_CHARS)); + self.groups.iter_mut().for_each(|g| truncate_chars(&mut g.name, MAX_NAME_CHARS)); + self.sources.iter_mut().for_each(|s| truncate_chars(&mut s.name, MAX_NAME_CHARS)); + // Drop performances whose clip no longer exists anywhere (any playlist). + let clip_ids: std::collections::BTreeSet = + self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| p.clips.iter().map(|c| c.id.0))).collect(); + self.midi_originals.retain(|id, _| clip_ids.contains(id)); + self.prune_auto_names(); + self.auto_names.values_mut().for_each(crate::AutoName::sanitize); + if !self.edit.zoom.samples_per_px.is_finite() || self.edit.zoom.samples_per_px <= 0.0 { + self.edit.zoom.samples_per_px = 1024.0; + } + if SampleRate::new(self.sample_rate.hz()).is_err() { + self.sample_rate = SampleRate::default(); + } + self.tempo.sanitize(); + } + + /// Reject a document whose ids are ambiguous or exhausted, run after [`Session::sanitize`] on + /// untrusted data. Duplicate ids in a namespace make commands, selections and source + /// associations act on an arbitrary one of the objects; a maximal `next_id` -- or a maximal + /// existing id, which `sanitize` folds into `next_id` -- makes every later allocation return + /// `u64::MAX` and repeat itself. + fn validate(&self) -> Result<(), SessionError> { + if self.next_id == u64::MAX { + return Err(SessionError::Invalid("the session id space is exhausted".into())); + } + distinct("track", self.tracks.iter().map(|t| t.id.0))?; + distinct("clip", self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| p.clips.iter().map(|c| c.id.0))))?; + distinct("source", self.sources.iter().map(|s| s.id.0))?; + distinct("video", self.videos.iter().map(|v| v.id.0))?; + distinct("bus", self.busses.iter().map(|b| b.id.0))?; + distinct("marker", self.markers.iter().map(|m| m.id.0))?; + distinct("group", self.groups.iter().map(|g| g.id.0))?; + Ok(()) + } +} + +/// The most characters kept of a name read from a file: longer ones can't be shown, and drawing +/// a name of a million characters overflows the renderer's buffers. +pub const MAX_NAME_CHARS: usize = 256; +/// The most characters kept of a comment read from a file. +pub const MAX_COMMENT_CHARS: usize = 4096; + +/// Cut `s` to at most `max` characters. +pub fn truncate_chars(s: &mut String, max: usize) { + if let Some((end, _)) = s.char_indices().nth(max) { + s.truncate(end); + } +} + +/// Every id in `ids` distinct, or an error naming the namespace and the repeated id. +fn distinct(kind: &str, ids: impl Iterator) -> Result<(), SessionError> { + let mut seen = std::collections::HashSet::new(); + for id in ids { + if !seen.insert(id) { + return Err(SessionError::Invalid(format!("duplicate {kind} id {id}"))); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn add_tracks_names_and_order() { + let mut s = Session::default(); + let m = s.add_track(TrackKind::Master, ChannelFormat::Stereo, None); + let a = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + let b = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + let v = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some("Vocal")); + assert_eq!(s.track(a).map(|t| t.name.as_str()), Some("Audio 1")); + assert_eq!(s.track(b).map(|t| t.name.as_str()), Some("Audio 2")); + assert_eq!(s.track(v).map(|t| t.name.as_str()), Some("Vocal")); + assert_eq!(s.tracks.last().map(|t| t.id), Some(m)); + let v2 = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, Some("Vocal")); + assert_eq!(s.track(v2).map(|t| t.name.as_str()), Some("Vocal 1")); + } + + #[test] + fn json_round_trip() { + let mut s = Session::default(); + let t = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, None); + let cid = s.new_clip_id(); + if let Some(p) = s.track_mut(t).and_then(Track::playlist_mut) { + p.clips.push(Clip::audio(cid, "c", SourceId(99), 0, 10, 100)); + } + s.add_marker("Verse", MarkerKind::Marker, 48_000, 48_000); + let text = s.to_json().unwrap(); + let back = Session::from_json(&text).unwrap(); + assert_eq!(back, s); + } + + /// Clips/Markers mode snaps and draws at clip and memory-location boundaries. + #[test] + fn clips_markers_grid_uses_visible_clip_edges_and_memory_locations() { + let mut s = Session::default(); + let track = s.add_track(TrackKind::Midi, ChannelFormat::Mono, None); + let clip = Clip::midi(s.new_clip_id(), "Notes", 1_000, 2_000, crate::soundcraft_midi::Sequence::default()); + let clip_id = clip.id; + s.track_mut(track).and_then(Track::playlist_mut).unwrap().clips.push(clip); + let hidden = s.add_track(TrackKind::Midi, ChannelFormat::Mono, None); + let hidden_clip = Clip::midi(s.new_clip_id(), "Hidden", 3_200, 100, crate::soundcraft_midi::Sequence::default()); + if let Some(track) = s.track_mut(hidden) { + track.hidden = true; + track.playlist_mut().unwrap().clips.push(hidden_clip); + } + s.add_marker("Cue", MarkerKind::Marker, 2_500, 2_500); + s.add_marker("Selection", MarkerKind::Selection, 4_000, 4_500); + s.edit.grid_clips_markers = true; + + assert_eq!(s.grid_lines(0, 5_000, 20), [0, 1_000, 2_500, 3_000, 4_000, 4_500]); + assert_eq!(s.grid_line_at_or_before(2_800), 2_500); + assert_eq!(s.snap_to_grid(2_800), 3_000); + assert_eq!(s.snap_to_grid(3_700), 4_000); + assert_eq!(s.grid_lines(2_400, 4_100, 2), [2_500, 4_000]); + assert_eq!(s.snap_to_grid(3_200), 3_000); + assert_eq!(s.snap_to_grid_excluding(2_800, &[clip_id]), 2_500); + + let folder = s.add_track(TrackKind::Folder, ChannelFormat::Stereo, None); + s.track_mut(track).unwrap().folder = Some(folder); + s.track_mut(folder).unwrap().folder_open = false; + assert_eq!(s.grid_lines(0, 5_000, 20), [0, 2_500, 4_000, 4_500]); + } + + /// Open hidden folders keep their visible descendants eligible for lines and snapping. + #[test] + fn clips_markers_grid_keeps_visible_children_of_hidden_open_folders() { + let mut s = Session::default(); + let outer = s.add_track(TrackKind::Folder, ChannelFormat::Stereo, None); + let inner = s.add_track(TrackKind::Folder, ChannelFormat::Stereo, None); + let child = s.add_track(TrackKind::Midi, ChannelFormat::Mono, None); + let clip = Clip::midi(s.new_clip_id(), "Visible", 1_000, 2_000, crate::soundcraft_midi::Sequence::default()); + s.track_mut(inner).unwrap().folder = Some(outer); + s.track_mut(inner).unwrap().hidden = true; + s.track_mut(child).unwrap().folder = Some(inner); + s.track_mut(child).unwrap().playlist_mut().unwrap().clips.push(clip); + s.edit.grid_clips_markers = true; + + assert_eq!(s.grid_lines(0, 5_000, 20), [0, 1_000, 3_000]); + assert_eq!(s.snap_to_grid(2_800), 3_000); + assert_eq!(s.grid_line_at_or_before(2_800), 1_000); + + s.track_mut(child).unwrap().hidden = true; + assert_eq!(s.grid_lines(0, 5_000, 20), [0]); + assert_eq!(s.snap_to_grid(2_800), 2_800); + s.track_mut(child).unwrap().hidden = false; + s.track_mut(inner).unwrap().folder_open = false; + assert_eq!(s.grid_lines(0, 5_000, 20), [0]); + s.track_mut(inner).unwrap().folder_open = true; + s.track_mut(outer).unwrap().folder_open = false; + assert_eq!(s.grid_lines(0, 5_000, 20), [0]); + } + + /// With no real Clips/Markers boundaries, snapping leaves the position alone while drawing + /// retains the session-start line. + #[test] + fn empty_clips_markers_grid_does_not_snap_everything_to_zero() { + let mut s = Session::default(); + s.edit.grid_clips_markers = true; + assert_eq!(s.grid_lines(0, 1_000, 10), [0]); + assert_eq!(s.snap_to_grid(678), 678); + } + + /// Frame-based grids share the same references as their displayed counters. + #[test] + fn frame_grids_apply_session_counter_origins() { + let mut s = Session { sample_rate: SampleRate::HZ_48000, frame_rate: FrameRate::Fps30, timecode_start: 1_600, ..Session::default() }; + s.edit.grid = GridValue::Timecode(crate::soundcraft_time::TimecodeValue::Second); + assert_eq!(s.grid_line_at_or_before(47_000), 46_400); + assert_eq!(s.snap_to_grid(46_400), 46_400); + + s.edit.grid = GridValue::FeetFrames(crate::soundcraft_time::FeetFramesValue::Foot); + s.edit.values.insert(FEET_FRAMES_START.into(), 1.0); + assert_eq!(s.grid_line_at_or_before(25_000), 24_000); + assert_eq!(s.snap_to_grid(24_000), 24_000); + } + + /// Every Feet+Frames increment shares the parser's single rounding at fractional frame rates. + #[test] + fn feet_frames_grid_matches_parsed_positions_with_signed_frame_origins() { + use crate::soundcraft_time::FeetFramesValue; + for rate in [FrameRate::Fps23_976, FrameRate::Fps29_97, FrameRate::Fps29_97Drop, FrameRate::Fps59_94Drop] { + let mut s = Session { sample_rate: SampleRate::HZ_48000, frame_rate: rate, ..Session::default() }; + for origin in [-17, -1, 1, 17] { + s.edit.values.insert(FEET_FRAMES_START.into(), origin as f64); + for value in FeetFramesValue::ALL { + s.edit.grid = GridValue::FeetFrames(value); + for feet in [-64, -2, 0, 1, 2, 4, 7, 12, 128] { + let at = s.parse_position(&format!("{feet}+00"), TimeFormat::FeetFrames).unwrap(); + assert_eq!(s.grid_line_at_or_before(at), at, "{rate:?} {value:?} origin {origin}, foot {feet}"); + assert_eq!(s.snap_to_grid(at), at); + assert_eq!(s.grid_lines(at - 1, at + 2, 4), [at]); + } + } + } + } + let mut s = Session { sample_rate: SampleRate::HZ_48000, frame_rate: FrameRate::Fps29_97, ..Session::default() }; + s.edit.values.insert(FEET_FRAMES_START.into(), 1.0); + s.edit.grid = GridValue::FeetFrames(FeetFramesValue::Foot); + assert_eq!(s.snap_to_grid(49_649), 49_650); + assert_eq!(s.format_position(49_650, TimeFormat::FeetFrames), "2+00"); + } + + /// Drop-frame minute boundaries keep their counter alignment with signed session origins. + #[test] + fn drop_frame_grids_apply_session_origins_at_minute_boundaries() { + for (rate, minute_frame) in [(FrameRate::Fps29_97Drop, 1_800), (FrameRate::Fps59_94Drop, 3_600)] { + for origin_frame in [-1, 1] { + let sr = SampleRate::HZ_48000; + let origin = rate.samples_at_frame(origin_frame, sr); + let mut s = Session { sample_rate: sr, frame_rate: rate, timecode_start: origin, ..Session::default() }; + s.edit.grid = GridValue::Timecode(crate::soundcraft_time::TimecodeValue::Second); + for boundary in [-minute_frame, minute_frame] { + let at = rate.samples_at_frame(boundary, sr) - origin; + assert_eq!(s.grid_line_at_or_before(at), at, "{rate:?}, origin {origin_frame}"); + assert_eq!(s.snap_to_grid(at + 1), at, "{rate:?}, origin {origin_frame}"); + assert_eq!(s.grid_lines(at - 1, at + 2, 10), [at]); + } + } + } + } + + #[test] + fn session_with_legacy_fit_height_loads() { + let mut s = Session::default(); + let t = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, None); + if let Some(track) = s.track_mut(t) { + track.height = crate::TrackHeight::Large; + } + let text = s.to_json().unwrap().replace("\"height\": \"Large\"", "\"height\": \"Fit\""); + assert!(text.contains("\"height\": \"Fit\"")); + let back = Session::from_json(&text).unwrap(); + assert_eq!(back.track(t).map(|tr| tr.height), Some(crate::TrackHeight::Medium)); + } + + /// Session JSON omits live flags and ignores their values in legacy documents. + #[test] + fn live_input_states_are_never_saved_or_restored() { + let mut s = Session::default(); + let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + let track = s.track_mut(t).unwrap(); + track.mixer.mute = true; + track.mixer.record_arm = true; + track.mixer.input_monitor = true; + + let mut value = serde_json::to_value(&s).unwrap(); + let mixer = value["tracks"][0]["mixer"].as_object_mut().unwrap(); + assert!(!mixer.contains_key("record_arm")); + assert!(!mixer.contains_key("input_monitor")); + + // Files written by older versions may still contain these keys; ignore them on input. + mixer.insert("record_arm".into(), serde_json::Value::Bool(true)); + mixer.insert("input_monitor".into(), serde_json::Value::Bool(true)); + let loaded = Session::from_json(&value.to_string()).unwrap(); + let track = loaded.track(t).unwrap(); + assert!(track.mixer.mute, "persistent mixer state still loads"); + assert!(!track.mixer.record_arm); + assert!(!track.mixer.input_monitor); + } + + #[test] + fn video_clip_round_trips_and_old_sessions_load() { + let mut s = Session::default(); + let t = s.add_track(TrackKind::Video, ChannelFormat::Mono, None); + let vid = SourceId(s.alloc()); + s.videos.push(VideoSource { + id: vid, + name: "cut.mov".into(), + path: "/films/cut.mov".into(), + width: 1920, + height: 1080, + frame_rate: 23.976, + duration: 12.5, + codec: "prores".into(), + offline: None, + }); + let cid = s.new_clip_id(); + if let Some(p) = s.track_mut(t).and_then(Track::playlist_mut) { + p.clips.push(Clip::video(cid, "cut", vid, 480, 0, 600_000)); + } + let back = Session::from_json(&s.to_json().unwrap()).unwrap(); + assert_eq!(back, s); + let c = &back.track(t).unwrap().clips()[0]; + assert_eq!((c.video_source(), c.source(), c.source_offset(), c.is_audio()), (Some(vid), None, 480, false)); + assert_eq!(back.track(t).unwrap().kind.label(), "Video Track"); + assert!(back.video(vid).is_some()); + // A session written before video support has no `videos` key. + let mut v = serde_json::to_value(Session::default()).unwrap(); + v.as_object_mut().unwrap().remove("videos"); + assert!(Session::from_json(&v.to_string()).unwrap().videos.is_empty()); + // Trimming the start keeps the picture anchored. + let mut c = c.clone(); + c.trim_start_to(100); + assert_eq!(c.source_offset(), 580); + } + + #[test] + fn unique_track_names_take_the_first_free_number() { + let mut s = Session::default(); + for name in ["Piano", "Piano 1", "Piano 3", "Piano 02", "Pianos 2"] { + s.add_track(TrackKind::Audio, ChannelFormat::Mono, Some(name)); + } + assert_eq!(s.unique_track_name("Piano"), "Piano 2"); + assert_eq!(s.unique_track_name("Organ"), "Organ"); + assert_eq!(s.unique_track_name("Audio"), "Audio 1"); + } + + #[test] + fn names_from_a_file_are_cut_to_a_length_that_can_be_drawn() { + let mut s = Session::default(); + let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + if let Some(tr) = s.track_mut(t) { + tr.name = "é".repeat(1_000); + } + s.comments = "c".repeat(10_000); + let back = Session::from_json(&s.to_json().unwrap()).unwrap(); + assert_eq!(back.track(t).map(|tr| tr.name.chars().count()), Some(MAX_NAME_CHARS)); + assert_eq!(back.comments.len(), MAX_COMMENT_CHARS); + } + + #[test] + fn a_bad_sample_rate_or_tempo_is_repaired_on_load() { + let mut v = serde_json::to_value(Session::default()).unwrap(); + v["sample_rate"] = serde_json::json!(0); + v["tempo"]["tempos"] = serde_json::json!([{"tick": 0, "bpm": 4e9}]); + let s = Session::from_json(&v.to_string()).unwrap(); + assert_eq!((s.sample_rate, s.tempo), (SampleRate::HZ_48000, TempoMap::default())); + } + + #[test] + fn a_meter_change_far_beyond_the_position_range_is_dropped_on_load() { + let mut v = serde_json::to_value(Session::default()).unwrap(); + v["tempo"]["meters"] = serde_json::json!([ + {"tick": 0, "numerator": 4, "denominator": 4}, + {"tick": i64::MAX, "numerator": 3, "denominator": 4} + ]); + let s = Session::from_json(&v.to_string()).unwrap(); + assert_eq!(s.parse_position("2|1|000", TimeFormat::BarsBeats), Ok(96_000)); + } + + #[test] + fn rejects_garbage_and_future_versions() { + assert!(Session::from_json("not json").is_err()); + assert!(Session::from_json("{}").is_err()); + let mut v = serde_json::to_value(Session::default()).unwrap(); + v["format_version"] = serde_json::json!(999); + assert!(matches!(Session::from_json(&v.to_string()), Err(SessionError::Version(999)))); + } + + #[test] + fn the_version_is_checked_as_before() { + // #892: the version is read without building the whole document as a tree; the errors + // stay what they were. + let format = |t: &str| match Session::from_json(t) { + Err(SessionError::Format(m)) => m, + other => panic!("{t}: {other:?}"), + }; + for t in [ + "{}", + "[]", + "[5]", + " \n[1, {}]", + "\"x\"", + "5", + "null", + r#"{"format_version":"1"}"#, + r#"{"format_version":-1}"#, + r#"{"format_version":1.5}"#, + r#"{"format_version":null}"#, + r#"{"format_version":[0,0,0]}"#, + r#"{"name":"x","other":{"format_version":1}}"#, + ] { + assert_eq!(format(t), "missing format_version", "{t}"); + } + for t in ["not json", r#"{"format_version":1"#, r#"{"format_version":1} x"#, r#"{"format_version":"1", ]"#] { + let want = serde_json::from_str::(t).unwrap_err().to_string(); + assert_eq!(format(t), want, "{t}"); + } + let mut v = serde_json::to_value(Session::default()).unwrap(); + for (ver, want) in [(serde_json::json!(2), 2), (serde_json::json!(1u64 << 40), u32::MAX)] { + v["format_version"] = ver; + assert_eq!(Session::from_json(&v.to_string()), Err(SessionError::Version(want))); + } + // A field given twice is refused by name, the version too. + let twice = |key: &str| format(&format!(r#"{{"format_version":1,"{key}":"a","{key}":"b"}}"#)); + assert!(twice("name").starts_with("duplicate field `name` at line 1 column "), "{}", twice("name")); + assert!(twice("format_version").starts_with("duplicate field `format_version` at line 1 column "), "{}", twice("format_version")); + // A newer version is refused even when the rest would not parse. + assert_eq!(Session::from_json(r#"{"format_version":7,"tracks":5}"#), Err(SessionError::Version(7))); + // An older or the current version loads as it did. + for ver in [0, 1] { + v["format_version"] = serde_json::json!(ver); + assert_eq!(Session::from_json(&v.to_string()).map(|s| s.format_version), Ok(ver)); + } + } + + #[test] + fn duplicate_ids_are_rejected_on_load() { + let mut s = Session::default(); + s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + let mut v = serde_json::to_value(&s).unwrap(); + v["tracks"][1]["id"] = v["tracks"][0]["id"].clone(); + let err = Session::from_json(&v.to_string()).unwrap_err(); + assert!(matches!(&err, SessionError::Invalid(m) if m.contains("duplicate track id")), "{err}"); + } + + #[test] + fn an_exhausted_id_space_is_rejected_on_load() { + let mut s = Session::default(); + s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + let mut v = serde_json::to_value(&s).unwrap(); + v["next_id"] = serde_json::json!(u64::MAX); + let err = Session::from_json(&v.to_string()).unwrap_err(); + assert!(matches!(&err, SessionError::Invalid(m) if m.contains("exhausted")), "{err}"); + // A maximal existing id is exhausted too, however `next_id` was written: every later + // allocation would return `u64::MAX` again. + let mut v = serde_json::to_value(&s).unwrap(); + v["tracks"][0]["id"] = serde_json::json!(u64::MAX); + let err = Session::from_json(&v.to_string()).unwrap_err(); + assert!(matches!(&err, SessionError::Invalid(m) if m.contains("exhausted")), "{err}"); + } + + #[test] + fn sanitize_repairs() { + let mut s = Session::default(); + let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + if let Some(tr) = s.track_mut(t) { + tr.active_playlist = 7; + tr.mixer.volume_db = f32::NAN; + } + s.next_id = 0; + s.sanitize(); + let tr = s.track(t).unwrap(); + assert_eq!(tr.active_playlist, 0); + assert_eq!(tr.mixer.volume_db, 0.0); + assert!(s.next_id > t.0); + } + + #[test] + fn loaded_automation_is_clamped_to_its_range() { + // #884: a session file can carry points outside a lane's range; load clamps them. + let mut s = Session::default(); + let t = s.add_track(TrackKind::Audio, ChannelFormat::Mono, None); + let tr = s.track_mut(t).unwrap(); + for p in [crate::AutoParam::Volume, crate::AutoParam::SendLevel(0)] { + tr.lane_mut(&p).points.push(crate::AutomationPoint { at: 0, value: 500.0 }); + } + let s = Session::from_json(&serde_json::to_string(&s).unwrap()).unwrap(); + let tr = s.track(t).unwrap(); + for p in [crate::AutoParam::Volume, crate::AutoParam::SendLevel(0)] { + assert_eq!(tr.lane(&p).unwrap().points[0].value, 12.0, "{p:?}"); + } + } + + #[test] + fn a_loaded_auto_name_scheme_leaves_room_for_its_number() { + let mut scheme = crate::AutoName { name: "n".repeat(300), suffix: "s".repeat(300), ..Default::default() }; + scheme.sanitize(); + assert_eq!(scheme.name.chars().count() + scheme.suffix.chars().count(), crate::AutoName::MAX_TEXT_CHARS); + } +} diff --git a/crates/scraft-format/src/source.rs b/crates/scraft-format/src/source.rs new file mode 100644 index 000000000..7013d0031 --- /dev/null +++ b/crates/scraft-format/src/source.rs @@ -0,0 +1,204 @@ +//! Audio sources (files in the session's Audio Files folder) and their decoded data, and the +//! movies video clips reference. + +use crate::SourceId; +use craft_audio::{AudioBuffer, AudioInfo, CodecId, Container, peaks::Peaks}; +use std::collections::BTreeMap; +use std::sync::Arc; + +/// The kind of file a source came from, as session files store it in [`Source::format`]. The +/// variant names are part of the session format: craft-audio's `Container` and `CodecId` name +/// more formats under other names, so this stays here and maps from them. +#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub enum FileFormat { + Wav, + Aiff, + Flac, + Mp3, + Ogg, + Aac, + Alac, + Caf, + /// Anything else that decodes: Matroska, WebM, raw AC-3. + Other, +} + +impl FileFormat { + /// The kind of the file `info` describes. + pub fn of(info: &AudioInfo) -> FileFormat { + match info.container { + Container::Wav => FileFormat::Wav, + Container::Aiff => FileFormat::Aiff, + Container::Flac => FileFormat::Flac, + Container::MpegAudio => FileFormat::Mp3, + Container::Ogg => FileFormat::Ogg, + Container::Caf => FileFormat::Caf, + Container::Adts => FileFormat::Aac, + // MP4 and QuickTime hold many codecs: the codec names the kind. + Container::Mp4 | Container::QuickTime => match info.codec { + CodecId::Alac => FileFormat::Alac, + CodecId::Aac | CodecId::HeAac => FileFormat::Aac, + _ => FileFormat::Other, + }, + _ => FileFormat::Other, + } + } +} + +/// Metadata for an audio file used by the session (serialised). +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Source { + pub id: SourceId, + pub name: String, + /// Path relative to the session folder (or absolute for referenced media). + pub path: String, + pub channels: u16, + pub frames: u64, + pub sample_rate: u32, + pub format: FileFormat, + /// Original BWF time stamp (samples), if any. + #[serde(default)] + pub time_reference: u64, + /// True when the file has not been written to disk yet (recorded/rendered this session). + #[serde(default)] + pub unsaved: bool, +} + +/// A movie used by Video track clips (serialised). The pictures themselves are never stored in +/// the session: the UI opens the movie from `path` and decodes frames on demand. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct VideoSource { + pub id: SourceId, + pub name: String, + /// Absolute path of the movie (referenced, never copied into the session folder). + pub path: String, + #[serde(default)] + pub width: u32, + #[serde(default)] + pub height: u32, + /// Frames per second (0 = unknown). + #[serde(default)] + pub frame_rate: f64, + /// Picture length in seconds. + #[serde(default)] + pub duration: f64, + /// Codec id (`h264`, `prores`, `mjpeg`, `hevc`, …). + #[serde(default)] + pub codec: String, + /// Why the movie is not opened, when its stored path was refused on opening the session. + #[serde(skip)] + pub offline: Option, +} + +impl VideoSource { + /// Display aspect ratio (width / height), 16:9 when unknown. + pub fn aspect(&self) -> f64 { + if self.width == 0 || self.height == 0 { 16.0 / 9.0 } else { f64::from(self.width) / f64::from(self.height) } + } +} + +/// Decoded audio plus waveform overviews (not serialised). +#[derive(Debug)] +pub struct SourceAudio { + pub buffer: AudioBuffer, + pub peaks: Vec, +} + +impl SourceAudio { + pub fn new(buffer: AudioBuffer) -> Self { + let peaks = buffer.channels.iter().map(|c| Peaks::build(c)).collect(); + SourceAudio { buffer, peaks } + } + pub fn frames(&self) -> usize { + self.buffer.frames() + } + pub fn channel(&self, ch: usize) -> Option<&[f32]> { + self.buffer.channels.get(ch).map(Vec::as_slice) + } +} + +/// Shared decoded audio, keyed by source id. +#[derive(Debug, Clone, Default)] +pub struct SourcePool { + map: BTreeMap>, +} + +impl SourcePool { + pub fn insert(&mut self, id: SourceId, audio: Arc) { + self.map.insert(id, audio); + } + pub fn get(&self, id: SourceId) -> Option<&Arc> { + self.map.get(&id) + } + pub fn remove(&mut self, id: SourceId) -> Option> { + self.map.remove(&id) + } + pub fn contains(&self, id: SourceId) -> bool { + self.map.contains_key(&id) + } + pub fn len(&self) -> usize { + self.map.len() + } + pub fn is_empty(&self) -> bool { + self.map.is_empty() + } + pub fn ids(&self) -> impl Iterator + '_ { + self.map.keys().copied() + } +} + +impl PartialEq for SourcePool { + fn eq(&self, other: &Self) -> bool { + self.map.len() == other.map.len() && self.map.iter().zip(other.map.iter()).all(|((a, x), (b, y))| a == b && Arc::ptr_eq(x, y)) + } +} + +#[cfg(test)] +mod tests { + use super::FileFormat; + use craft_audio::{AudioBuffer, EncodeFormat, EncodeOptions, encode, probe}; + + /// Sessions written before the format code moved to craft-audio name a source's format with + /// these words; they must keep reading, and new sessions must keep writing them. + #[test] + fn the_format_names_in_session_files_do_not_change() { + use FileFormat::{Aac, Aiff, Alac, Caf, Flac, Mp3, Ogg, Other, Wav}; + let names = ["Wav", "Aiff", "Flac", "Mp3", "Ogg", "Aac", "Alac", "Caf", "Other"]; + for (format, name) in [Wav, Aiff, Flac, Mp3, Ogg, Aac, Alac, Caf, Other].into_iter().zip(names) { + assert_eq!(serde_json::to_string(&format).unwrap(), format!("\"{name}\"")); + assert_eq!(serde_json::from_str::(&format!("\"{name}\"")).unwrap(), format); + } + } + + /// An MP4 is AAC only when its audio is: Opus, AC-3 or PCM in MP4 is not. + #[test] + fn an_mp4_is_named_by_its_codec() { + let wav = encode(&AudioBuffer::new(48_000, 1, 480), &EncodeOptions::default()).unwrap(); + let mut info = serde_json::to_value(probe(&wav, None).unwrap()).unwrap(); + for (container, codec, format) in [ + ("Mp4", "Aac", FileFormat::Aac), + ("Mp4", "HeAac", FileFormat::Aac), + ("QuickTime", "Alac", FileFormat::Alac), + ("Mp4", "Opus", FileFormat::Other), + ("Mp4", "Ac3", FileFormat::Other), + ("QuickTime", "Pcm", FileFormat::Other), + ("Adts", "Aac", FileFormat::Aac), + ] { + info["container"] = container.into(); + info["codec"] = codec.into(); + let info: craft_audio::AudioInfo = serde_json::from_value(info.clone()).unwrap(); + assert_eq!(FileFormat::of(&info), format, "{codec} in {container}"); + } + } + + #[test] + fn a_probed_file_maps_to_its_format() { + let buf = AudioBuffer::new(48_000, 1, 480); + for (written, format) in + [(EncodeFormat::Wav, FileFormat::Wav), (EncodeFormat::Aiff, FileFormat::Aiff), (EncodeFormat::Flac, FileFormat::Flac)] + { + let info = probe(&encode(&buf, &EncodeOptions::new(written)).unwrap(), None).unwrap(); + assert_eq!(FileFormat::of(&info), format); + } + } +} diff --git a/crates/scraft-format/src/track.rs b/crates/scraft-format/src/track.rs new file mode 100644 index 000000000..d31bc5912 --- /dev/null +++ b/crates/scraft-format/src/track.rs @@ -0,0 +1,477 @@ +//! Tracks and playlists. + +use crate::soundcraft_time::{Range, Samples}; +use crate::{AutoParam, AutomationLane, ChannelFormat, Clip, ClipId, Mixer, TrackId}; + +/// Track types (Track › New). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)] +pub enum TrackKind { + #[default] + Audio, + Aux, + Master, + Midi, + Instrument, + Vca, + Folder, + /// Picture: clips reference a movie ([`crate::VideoSource`]); no audio path. + Video, +} + +impl TrackKind { + pub const ALL: [TrackKind; 8] = [ + TrackKind::Audio, + TrackKind::Aux, + TrackKind::Master, + TrackKind::Midi, + TrackKind::Instrument, + TrackKind::Vca, + TrackKind::Folder, + TrackKind::Video, + ]; + pub fn label(self) -> &'static str { + match self { + TrackKind::Audio => "Audio Track", + TrackKind::Aux => "Aux Input", + TrackKind::Master => "Master Fader", + TrackKind::Midi => "MIDI Track", + TrackKind::Instrument => "Instrument Track", + TrackKind::Vca => "VCA Master", + TrackKind::Folder => "Routing Folder Track", + TrackKind::Video => "Video Track", + } + } + pub fn default_name(self) -> &'static str { + match self { + TrackKind::Audio => "Audio", + TrackKind::Aux => "Aux", + TrackKind::Master => "Master", + TrackKind::Midi => "MIDI", + TrackKind::Instrument => "Inst", + TrackKind::Vca => "VCA", + TrackKind::Folder => "Folder", + TrackKind::Video => "Video", + } + } + pub fn id(self) -> &'static str { + match self { + TrackKind::Audio => "audio", + TrackKind::Aux => "aux", + TrackKind::Master => "master", + TrackKind::Midi => "midi", + TrackKind::Instrument => "instrument", + TrackKind::Vca => "vca", + TrackKind::Folder => "folder", + TrackKind::Video => "video", + } + } + pub fn from_id(s: &str) -> Option { + let s = s.to_ascii_lowercase(); + TrackKind::ALL.into_iter().find(|k| k.id() == s || k.label().eq_ignore_ascii_case(&s)) + } + /// Track kinds that hold clips on a timeline. + pub fn has_playlist(self) -> bool { + matches!(self, TrackKind::Audio | TrackKind::Midi | TrackKind::Instrument | TrackKind::Video) + } + pub fn is_midi(self) -> bool { + matches!(self, TrackKind::Midi | TrackKind::Instrument) + } + /// Kinds that pass audio through the mixer. + pub fn is_audio_path(self) -> bool { + matches!(self, TrackKind::Audio | TrackKind::Aux | TrackKind::Master | TrackKind::Instrument | TrackKind::Folder) + } +} + +/// Track heights (View › Track Height presets). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize)] +pub enum TrackHeight { + /// Any height from Micro to Extreme, in points. + Custom(u16), + Micro, + Mini, + Small, + #[default] + Medium, + Large, + Jumbo, + Extreme, + /// Fit To Window: this many points. + Fit(u16), +} + +/// A saved track height: `"Micro"` … `"Extreme"` (or `{"Micro": null}` …), `{"Custom": points}`, +/// `{"Fit": points}`, or the bare `"Fit"` older sessions used. Read as it comes, never buffered (as an +/// untagged enum would), so anything else in a session file is refused at its first token. +impl<'de> serde::Deserialize<'de> for TrackHeight { + fn deserialize>(d: D) -> Result { + d.deserialize_any(HeightVisitor) + } +} + +const HEIGHT_VARIANTS: &[&str] = &["Custom", "Micro", "Mini", "Small", "Medium", "Large", "Jumbo", "Extreme", "Fit"]; + +struct HeightVisitor; + +impl HeightVisitor { + /// The height a variant without a size names. + fn preset(name: &str) -> Option { + Some(match name { + "Micro" => TrackHeight::Micro, + "Mini" => TrackHeight::Mini, + "Small" => TrackHeight::Small, + "Medium" => TrackHeight::Medium, + "Large" => TrackHeight::Large, + "Jumbo" => TrackHeight::Jumbo, + "Extreme" => TrackHeight::Extreme, + _ => return None, + }) + } +} + +impl<'de> serde::de::Visitor<'de> for HeightVisitor { + type Value = TrackHeight; + + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("a track height") + } + + fn visit_str(self, v: &str) -> Result { + match v { + // Older sessions stored Fit with no size, drawn 88 points tall: the default Medium height. + "Fit" => Ok(TrackHeight::Medium), + "Custom" => Err(E::invalid_type(serde::de::Unexpected::UnitVariant, &"a height in points")), + other => Self::preset(other).ok_or_else(|| E::unknown_variant(other, HEIGHT_VARIANTS)), + } + } + + fn visit_map>(self, mut map: A) -> Result { + use serde::de::Error as _; + let Some(key) = map.next_key::()? else { return Err(A::Error::invalid_length(0, &"one variant")) }; + let h = match key.as_str() { + "Custom" => TrackHeight::Custom(map.next_value()?), + // `{"Fit": null}` is the old size-less Fit too, as above. + "Fit" => map.next_value::>()?.map_or(TrackHeight::Medium, TrackHeight::Fit), + other => { + let h = Self::preset(other).ok_or_else(|| A::Error::unknown_variant(other, HEIGHT_VARIANTS))?; + map.next_value::<()>()?; + h + } + }; + if map.next_key::()?.is_some() { + return Err(A::Error::invalid_length(2, &"one variant")); + } + Ok(h) + } +} + +impl TrackHeight { + pub const ALL: [TrackHeight; 7] = [ + TrackHeight::Micro, + TrackHeight::Mini, + TrackHeight::Small, + TrackHeight::Medium, + TrackHeight::Large, + TrackHeight::Jumbo, + TrackHeight::Extreme, + ]; + pub fn points(self) -> f32 { + match self { + TrackHeight::Custom(points) => f32::from(points).clamp(TrackHeight::Micro.points(), TrackHeight::Extreme.points()), + TrackHeight::Micro => 16.0, + TrackHeight::Mini => 24.0, + TrackHeight::Small => 46.0, + TrackHeight::Medium => 88.0, + TrackHeight::Large => 132.0, + TrackHeight::Jumbo => 220.0, + TrackHeight::Extreme => 440.0, + TrackHeight::Fit(points) => f32::from(points.max(16)), + } + } + pub fn label(self) -> &'static str { + match self { + TrackHeight::Custom(_) => "custom", + TrackHeight::Micro => "micro", + TrackHeight::Mini => "mini", + TrackHeight::Small => "small", + TrackHeight::Medium => "medium", + TrackHeight::Large => "large", + TrackHeight::Jumbo => "jumbo", + TrackHeight::Extreme => "extreme", + TrackHeight::Fit(_) => "fit to window", + } + } + pub fn from_id(s: &str) -> Option { + TrackHeight::ALL.into_iter().find(|h| h.label().eq_ignore_ascii_case(s)) + } + /// The preset this many points tall, else a custom height clamped to Micro..Extreme. + pub fn from_points(points: f32) -> TrackHeight { + let p = points.max(TrackHeight::Micro.points()).min(TrackHeight::Extreme.points()).round() as u16; + TrackHeight::ALL.into_iter().find(|h| h.points() == f32::from(p)).unwrap_or(TrackHeight::Custom(p)) + } + /// The next preset taller (or shorter) than this height; Extreme (or Micro) stays. + pub fn step(self, taller: bool) -> TrackHeight { + let p = self.points(); + let next = + if taller { TrackHeight::ALL.into_iter().find(|h| h.points() > p) } else { TrackHeight::ALL.into_iter().rev().find(|h| h.points() < p) }; + next.unwrap_or(self) + } +} + +/// The default track colour palette (original, chosen for SoundCraft). +pub const TRACK_COLORS: [[u8; 3]; 16] = [ + [86, 132, 214], + [94, 170, 214], + [80, 190, 168], + [104, 186, 98], + [168, 196, 76], + [222, 196, 72], + [230, 152, 62], + [222, 104, 72], + [214, 82, 110], + [196, 86, 168], + [150, 98, 206], + [110, 104, 214], + [130, 150, 170], + [178, 140, 110], + [120, 170, 140], + [190, 120, 150], +]; + +/// A playlist: an alternative take of a track's clips. +#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)] +pub struct Playlist { + pub name: String, + /// Clips sorted by start; non-overlapping except at crossfades. + pub clips: Vec, +} + +impl Playlist { + pub fn new(name: impl Into) -> Self { + Playlist { name: name.into(), clips: Vec::new() } + } + pub fn sort(&mut self) { + self.clips.sort_by_key(|c| (c.start, c.id.0)); + } + pub fn clip(&self, id: ClipId) -> Option<&Clip> { + self.clips.iter().find(|c| c.id == id) + } + pub fn clip_mut(&mut self, id: ClipId) -> Option<&mut Clip> { + self.clips.iter_mut().find(|c| c.id == id) + } + pub fn clips_in(&self, r: Range) -> impl Iterator { + self.clips.iter().filter(move |c| c.range().overlaps(&r)) + } + pub fn clip_at(&self, at: Samples) -> Option<&Clip> { + self.clips.iter().rev().find(|c| c.range().contains(at)) + } + pub fn end(&self) -> Samples { + self.clips.iter().map(Clip::end).max().unwrap_or(0) + } +} + +/// A track. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Track { + pub id: TrackId, + pub name: String, + pub kind: TrackKind, + pub format: ChannelFormat, + pub color: [u8; 3], + pub height: TrackHeight, + pub playlists: Vec, + pub active_playlist: usize, + pub mixer: Mixer, + pub automation: Vec, + /// Which lane the track shows: `waveform`/`blocks`/`notes`/`regions` or an automation param id. + pub view: String, + #[serde(default)] + pub hidden: bool, + #[serde(default)] + pub inactive: bool, + #[serde(default)] + pub frozen: bool, + #[serde(default)] + pub comments: String, + /// Folder that contains this track. + #[serde(default)] + pub folder: Option, + #[serde(default = "yes")] + pub folder_open: bool, + /// Instrument plugin (instrument tracks) — registry id. + #[serde(default)] + pub instrument: Option, + /// Ticks (musical) or samples timebase for clips. + #[serde(default)] + pub ticks_timebase: bool, + /// Elastic Audio algorithm, if enabled. + #[serde(default)] + pub elastic: Option, + /// Pinned to top in the Edit window. + #[serde(default)] + pub pinned: bool, +} + +fn yes() -> bool { + true +} + +impl Track { + pub fn new(id: TrackId, name: impl Into, kind: TrackKind, format: ChannelFormat, color: [u8; 3]) -> Self { + let name = name.into(); + let format = if kind.is_midi() && kind == TrackKind::Midi { ChannelFormat::Mono } else { format }; + let view = if kind.is_midi() { + "notes" + } else if kind.has_playlist() { + "waveform" + } else { + "volume" + }; + Track { + id, + playlists: vec![Playlist::new(name.clone())], + name, + kind, + format, + color, + height: TrackHeight::Medium, + active_playlist: 0, + mixer: Mixer::new(format), + automation: Vec::new(), + view: view.into(), + hidden: false, + inactive: false, + frozen: false, + comments: String::new(), + folder: None, + folder_open: true, + instrument: None, + ticks_timebase: kind.is_midi(), + elastic: None, + pinned: false, + } + } + + pub fn channels(&self) -> usize { + self.format.channels() + } + + pub fn playlist(&self) -> Option<&Playlist> { + self.playlists.get(self.active_playlist) + } + + pub fn playlist_mut(&mut self) -> Option<&mut Playlist> { + self.playlists.get_mut(self.active_playlist) + } + + pub fn clips(&self) -> &[Clip] { + self.playlist().map_or(&[], |p| p.clips.as_slice()) + } + + pub fn lane(&self, p: &AutoParam) -> Option<&AutomationLane> { + self.automation.iter().find(|l| &l.param == p) + } + + pub fn lane_mut(&mut self, p: &AutoParam) -> &mut AutomationLane { + let idx = match self.automation.iter().position(|l| &l.param == p) { + Some(i) => i, + None => { + self.automation.push(AutomationLane::new(p.clone())); + self.automation.len() - 1 + } + }; + let len = self.automation.len(); + // `idx` is in range: it came from `position` or is the index of the element just pushed. + &mut self.automation[idx.min(len - 1)] + } + + pub fn is_folder(&self) -> bool { + self.kind == TrackKind::Folder + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn legacy_fit_string_loads_as_default_height() { + assert_eq!(serde_json::from_str::("\"Fit\"").unwrap(), TrackHeight::Medium); + assert_eq!(serde_json::from_str::("{\"Fit\":120}").unwrap(), TrackHeight::Fit(120)); + assert_eq!(serde_json::to_string(&TrackHeight::Fit(120)).unwrap(), "{\"Fit\":120}"); + assert_eq!(serde_json::from_str::("\"Large\"").unwrap(), TrackHeight::Large); + assert_eq!(serde_json::from_str::("{\"Custom\":50}").unwrap(), TrackHeight::Custom(50)); + } + + #[test] + fn every_saved_height_form_loads_and_nothing_else() { + for h in TrackHeight::ALL.into_iter().chain([TrackHeight::Custom(50), TrackHeight::Fit(120)]) { + assert_eq!(serde_json::from_str::(&serde_json::to_string(&h).unwrap()).unwrap(), h); + assert_eq!(serde_json::from_value::(serde_json::to_value(h).unwrap()).unwrap(), h); + } + for h in TrackHeight::ALL { + let null = format!("{{{}:null}}", serde_json::to_string(&h).unwrap()); + assert_eq!(serde_json::from_str::(&null).unwrap(), h, "{null}"); + } + assert_eq!(serde_json::from_str::(r#"{"Fit":null}"#).unwrap(), TrackHeight::Medium); + assert!(serde_json::from_str::(r#"{"Custom":null}"#).is_err()); + for bad in + [r#""Custom""#, r#""Huge""#, "7", "[]", "null", "{}", r#"{"Fit":"x"}"#, r#"{"Fit":70000}"#, r#"{"Large":1}"#, r#"{"Fit":1,"Fit":2}"#] + { + assert!(serde_json::from_str::(bad).is_err(), "{bad}"); + } + } + + #[test] + fn a_huge_height_is_refused_at_its_first_token() { + // #892: the height was read through an untagged enum, which buffers the whole value + // first; a session could hold a huge array there. It is refused where it starts. + for (open, rest) in [("[", "0,".repeat(100_000) + "0]"), (r#"{"Fit":["#, "0,".repeat(100_000) + "0]}")] { + let text = format!("{open}{rest}"); + let err = serde_json::from_str::(&text).unwrap_err(); + assert!(err.is_data() && err.line() == 1 && err.column() <= open.len() + 1, "{open}: {err}"); + } + } + + #[test] + fn new_track_defaults() { + let t = Track::new(TrackId(1), "Gtr", TrackKind::Audio, ChannelFormat::Stereo, TRACK_COLORS[0]); + assert_eq!(t.channels(), 2); + assert_eq!(t.view, "waveform"); + assert_eq!(t.playlists.len(), 1); + let m = Track::new(TrackId(2), "Keys", TrackKind::Midi, ChannelFormat::Stereo, TRACK_COLORS[0]); + assert_eq!(m.format, ChannelFormat::Mono); + assert_eq!(m.view, "notes"); + } + + #[test] + fn lane_mut_creates_once() { + let mut t = Track::new(TrackId(1), "Gtr", TrackKind::Audio, ChannelFormat::Mono, TRACK_COLORS[0]); + t.lane_mut(&AutoParam::Volume).set_point(0, -3.0); + t.lane_mut(&AutoParam::Volume).set_point(10, -6.0); + assert_eq!(t.automation.len(), 1); + assert_eq!(t.lane(&AutoParam::Volume).map(|l| l.points.len()), Some(2)); + } + + #[test] + fn kind_ids() { + for k in TrackKind::ALL { + assert_eq!(TrackKind::from_id(k.id()), Some(k)); + } + } + + #[test] + fn custom_heights_round_to_presets_and_step_through_them() { + assert_eq!(TrackHeight::from_points(88.0), TrackHeight::Medium); + assert_eq!(TrackHeight::from_points(57.4), TrackHeight::Custom(57)); + assert_eq!(TrackHeight::from_points(9999.0), TrackHeight::Extreme); + assert_eq!(TrackHeight::from_points(f32::NAN), TrackHeight::Micro); + assert_eq!(TrackHeight::Custom(u16::MAX).points(), 440.0); + assert_eq!(TrackHeight::Custom(57).step(true), TrackHeight::Medium); + assert_eq!(TrackHeight::Custom(57).step(false), TrackHeight::Small); + assert_eq!(TrackHeight::Extreme.step(true), TrackHeight::Extreme); + assert_eq!(TrackHeight::Micro.step(false), TrackHeight::Micro); + let json = serde_json::to_string(&TrackHeight::Custom(57)).unwrap(); + assert_eq!(serde_json::from_str::(&json).unwrap(), TrackHeight::Custom(57)); + assert_eq!(serde_json::from_str::("\"Large\"").unwrap(), TrackHeight::Large); + } +} diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs index bdcf64cd7..1a4045c61 100644 --- a/crates/zip/src/archive.rs +++ b/crates/zip/src/archive.rs @@ -73,6 +73,12 @@ impl<'a> Archive<'a> { self.duplicates } + /// Do two entries' compressed data ranges overlap? A format that stores many files in one + /// archive can refuse aliases so one payload cannot be expanded and retained repeatedly. + pub fn has_overlapping_files(&mut self) -> Result { + self.zip.has_overlapping_files().map_err(|e| Error::NotZip(e.to_string())) + } + /// The entry names in the archive's order, folders included. A name that is not text is /// left out. pub fn names(&self) -> impl Iterator> { diff --git a/crates/zip/src/lib.rs b/crates/zip/src/lib.rs index 4e802d795..1ee218dcf 100644 --- a/crates/zip/src/lib.rs +++ b/crates/zip/src/lib.rs @@ -203,4 +203,20 @@ mod tests { assert_eq!(budget.remaining(), 0); assert!(budget.charge(u64::MAX).is_err()); } + + #[test] + #[allow(clippy::unwrap_used, clippy::expect_used)] + fn archive_reports_entries_that_alias_the_same_payload() { + use std::io::Write as _; + + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + zip.start_file("first", opts).unwrap(); + zip.write_all(b"payload").unwrap(); + zip.shallow_copy_file("first", "alias").unwrap(); + let bytes = zip.finish().unwrap().into_inner(); + + let mut archive = Archive::open(&bytes, Limits { max_entries: 2, max_total_bytes: 100 }).unwrap(); + assert!(archive.has_overlapping_files().unwrap()); + } } From a2e0f586323131ce54a8abae0603f0a26f00f23e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:20:55 +0000 Subject: [PATCH 035/520] Bound shared SoundCraft session IO --- crates/scraft-format/src/bundle.rs | 11 ++-- crates/scraft-format/src/lib.rs | 80 ++++++++++++++++++++++++++++-- crates/zip/src/archive.rs | 11 ++++ crates/zip/src/lib.rs | 19 +++++++ 4 files changed, 109 insertions(+), 12 deletions(-) diff --git a/crates/scraft-format/src/bundle.rs b/crates/scraft-format/src/bundle.rs index 52d6d33be..4421472a2 100644 --- a/crates/scraft-format/src/bundle.rs +++ b/crates/scraft-format/src/bundle.rs @@ -69,8 +69,7 @@ pub fn write_bundle(session: &Session, limits: BundleLimits) -> Result<(Vec, source.format = FileFormat::Wav; } - let text = fresh.to_json().map_err(|e| BundleError::Session(e.to_string()))?; - check_session_size("the session", text.len() as u64, limits.session_bytes, true)?; + let text = crate::write_session(&fresh, limits.session_bytes)?; account(&mut count, &mut total, text.len(), limits)?; let session_name = format!("{}.{}", sanitize_name(&fresh.name), SESSION_EXTENSION); zip.add(&session_name, text.as_bytes(), Compression::Stored).map_err(|e| BundleError::Invalid(e.to_string()))?; @@ -92,13 +91,11 @@ pub fn read_bundle(bytes: &[u8], limits: BundleLimits) -> Result<(String, HashMa if archive.has_overlapping_files().map_err(zip_error)? { return Err(BundleError::Invalid("files share data".into())); } - let names: Vec = archive.names().map(|n| n.into_owned()).collect(); - if names.len() != archive.len() { - return Err(BundleError::Invalid("an entry name is not text".into())); - } + let len = archive.len(); let mut session = None; let mut files = HashMap::new(); - for name in names { + for index in 0..len { + let name = archive.entry_name(index).map_err(zip_error)?.ok_or_else(|| BundleError::Invalid("missing entry".into()))?; if name.ends_with('/') { continue; } diff --git a/crates/scraft-format/src/lib.rs b/crates/scraft-format/src/lib.rs index d26bca759..c54d7f934 100644 --- a/crates/scraft-format/src/lib.rs +++ b/crates/scraft-format/src/lib.rs @@ -42,12 +42,82 @@ pub const MAX_SESSION_BYTES: u64 = 256 << 20; /// Read a bare `.scraft` file after enforcing the caller's byte limit. pub fn read_session_bytes(name: &str, bytes: &[u8], limit: u64) -> Result { check_session_size(name, u64::try_from(bytes.len()).unwrap_or(u64::MAX), limit, false)?; - String::from_utf8(bytes.to_vec()).map_err(|_| BundleError::Session(format!("{name}: not a SoundCraft session"))) + read_session_data(name, bytes.to_vec(), limit) } -/// Serialize a `.scraft` session and refuse output larger than the caller's read limit. +/// Turn already-read `.scraft` bytes into text without making another copy. +pub fn read_session_data(name: &str, bytes: Vec, limit: u64) -> Result { + check_session_size(name, u64::try_from(bytes.len()).unwrap_or(u64::MAX), limit, false)?; + String::from_utf8(bytes).map_err(|_| BundleError::Session(format!("{name}: not a SoundCraft session"))) +} + +/// The size of pretty JSON for `session`, measured without retaining the serialized document. +pub fn session_json_len(session: &Session) -> Result { + struct Count(u64); + impl std::io::Write for Count { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + self.0 = self.0.saturating_add(u64::try_from(bytes.len()).unwrap_or(u64::MAX)); + Ok(bytes.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let mut count = Count(0); + serde_json::to_writer_pretty(&mut count, session).map_err(|error| BundleError::Session(error.to_string()))?; + Ok(count.0) +} + +/// Serialize a `.scraft` session, refusing output larger than the caller's read limit while +/// keeping the temporary allocation within that limit. pub fn write_session(session: &Session, limit: u64) -> Result { - let text = session.to_json().map_err(|error| BundleError::Session(error.to_string()))?; - check_session_size("the session", u64::try_from(text.len()).unwrap_or(u64::MAX), limit, true)?; - Ok(text) + write_session_named(session, "the session", limit) +} + +/// [`write_session`] with the path or label used for the session-size error. +pub fn write_session_named(session: &Session, name: &str, limit: u64) -> Result { + struct Bounded(Vec, u64, bool); + impl std::io::Write for Bounded { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + let remaining = self.1.saturating_sub(u64::try_from(self.0.len()).unwrap_or(u64::MAX)); + let count = usize::try_from(remaining).unwrap_or(usize::MAX).min(bytes.len()); + self.0.extend_from_slice(&bytes[..count]); + self.2 |= count < bytes.len(); + Ok(count) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let capacity = usize::try_from(limit).unwrap_or(usize::MAX); + let mut output = Bounded(Vec::with_capacity(capacity.min(1024 * 1024)), limit, false); + let serialized = serde_json::to_writer_pretty(&mut output, session); + if output.2 { + check_session_size(name, limit.saturating_add(1), limit, true)?; + } + serialized.map_err(|error| BundleError::Session(error.to_string()))?; + String::from_utf8(output.0).map_err(|error| BundleError::Session(error.to_string())) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used)] +mod session_io_tests { + use super::*; + + #[test] + fn bounded_writer_matches_canonical_session_json_and_refuses_one_byte_under() { + let session = Session::default(); + let canonical = session.to_json().unwrap(); + let len = u64::try_from(canonical.len()).unwrap(); + assert_eq!(session_json_len(&session).unwrap(), len); + assert_eq!(write_session(&session, len).unwrap(), canonical); + assert!(write_session(&session, len - 1).is_err()); + } + + #[test] + fn bare_session_reader_checks_size_and_utf8() { + assert!(read_session_bytes("song.scraft", b"{}", 1).is_err()); + assert!(read_session_bytes("song.scraft", &[0xff], 10).is_err()); + assert_eq!(read_session_bytes("song.scraft", b"{}", 2).unwrap(), "{}"); + } } diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs index 1a4045c61..1783b3e06 100644 --- a/crates/zip/src/archive.rs +++ b/crates/zip/src/archive.rs @@ -67,6 +67,17 @@ impl<'a> Archive<'a> { self.zip.is_empty() } + /// The name at `index`, copied on demand so callers can process hostile directories without + /// retaining a second copy of every potentially large name. + pub fn entry_name(&mut self, index: usize) -> Result, Error> { + if index >= self.zip.len() { + return Ok(None); + } + let file = self.zip.by_index_raw(index).map_err(|error| Error::NotZip(error.to_string()))?; + let name = file.name().map_err(|error| Error::NotZip(error.to_string()))?; + Ok(Some(name.into_owned())) + } + /// Do two entries share a name? `zip` then keeps one of them, and which one a writer meant /// is not known: a format that must not guess refuses the archive. pub fn has_duplicate_names(&self) -> bool { diff --git a/crates/zip/src/lib.rs b/crates/zip/src/lib.rs index 1ee218dcf..ef3264294 100644 --- a/crates/zip/src/lib.rs +++ b/crates/zip/src/lib.rs @@ -219,4 +219,23 @@ mod tests { let mut archive = Archive::open(&bytes, Limits { max_entries: 2, max_total_bytes: 100 }).unwrap(); assert!(archive.has_overlapping_files().unwrap()); } + + #[test] + #[allow(clippy::unwrap_used, clippy::expect_used)] + fn archive_can_copy_one_name_at_a_time_by_index() { + use std::io::Write as _; + + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default(); + zip.start_file("first", opts).unwrap(); + zip.write_all(b"1").unwrap(); + zip.start_file("second", opts).unwrap(); + zip.write_all(b"2").unwrap(); + let bytes = zip.finish().unwrap().into_inner(); + + let mut archive = Archive::open(&bytes, Limits { max_entries: 2, max_total_bytes: 10 }).unwrap(); + assert_eq!(archive.entry_name(0).unwrap().as_deref(), Some("first")); + assert_eq!(archive.entry_name(1).unwrap().as_deref(), Some("second")); + assert_eq!(archive.entry_name(2).unwrap(), None); + } } From 7c108201239d2504bb61e941828dc9f388187a94 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:26:31 +0000 Subject: [PATCH 036/520] Preserve ZIP directory semantics in bundle reader --- crates/scraft-format/src/bundle.rs | 5 +++-- crates/zip/src/archive.rs | 9 +++++++++ crates/zip/src/lib.rs | 7 +++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/scraft-format/src/bundle.rs b/crates/scraft-format/src/bundle.rs index 4421472a2..6b4885003 100644 --- a/crates/scraft-format/src/bundle.rs +++ b/crates/scraft-format/src/bundle.rs @@ -95,10 +95,11 @@ pub fn read_bundle(bytes: &[u8], limits: BundleLimits) -> Result<(String, HashMa let mut session = None; let mut files = HashMap::new(); for index in 0..len { - let name = archive.entry_name(index).map_err(zip_error)?.ok_or_else(|| BundleError::Invalid("missing entry".into()))?; - if name.ends_with('/') { + let is_dir = archive.entry_is_dir(index).map_err(zip_error)?.ok_or_else(|| BundleError::Invalid("missing entry".into()))?; + if is_dir { continue; } + let name = archive.entry_name(index).map_err(zip_error)?.ok_or_else(|| BundleError::Invalid("missing entry".into()))?; let is_session = !name.contains('/') && Path::new(&name).extension().and_then(OsStr::to_str) == Some(SESSION_EXTENSION); let size = archive.declared_size(&name).ok_or_else(|| BundleError::Invalid(format!("{name}: missing entry")))?; if is_session && session.is_none() { diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs index 1783b3e06..83d7b603d 100644 --- a/crates/zip/src/archive.rs +++ b/crates/zip/src/archive.rs @@ -78,6 +78,15 @@ impl<'a> Archive<'a> { Ok(Some(name.into_owned())) } + /// Whether the entry at `index` is a directory according to ZIP filename conventions. + pub fn entry_is_dir(&mut self, index: usize) -> Result, Error> { + if index >= self.zip.len() { + return Ok(None); + } + let file = self.zip.by_index_raw(index).map_err(|error| Error::NotZip(error.to_string()))?; + Ok(Some(file.is_dir())) + } + /// Do two entries share a name? `zip` then keeps one of them, and which one a writer meant /// is not known: a format that must not guess refuses the archive. pub fn has_duplicate_names(&self) -> bool { diff --git a/crates/zip/src/lib.rs b/crates/zip/src/lib.rs index ef3264294..452351ee3 100644 --- a/crates/zip/src/lib.rs +++ b/crates/zip/src/lib.rs @@ -231,11 +231,14 @@ mod tests { zip.write_all(b"1").unwrap(); zip.start_file("second", opts).unwrap(); zip.write_all(b"2").unwrap(); + zip.start_file("folder\\", opts).unwrap(); let bytes = zip.finish().unwrap().into_inner(); - let mut archive = Archive::open(&bytes, Limits { max_entries: 2, max_total_bytes: 10 }).unwrap(); + let mut archive = Archive::open(&bytes, Limits { max_entries: 3, max_total_bytes: 10 }).unwrap(); assert_eq!(archive.entry_name(0).unwrap().as_deref(), Some("first")); assert_eq!(archive.entry_name(1).unwrap().as_deref(), Some("second")); - assert_eq!(archive.entry_name(2).unwrap(), None); + assert_eq!(archive.entry_is_dir(2).unwrap(), Some(true)); + assert_eq!(archive.entry_name(2).unwrap().as_deref(), Some("folder\\")); + assert_eq!(archive.entry_name(3).unwrap(), None); } } From ca7a73bd2c812c6c37f0b86827eba6d0749e4912 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:50:46 +0000 Subject: [PATCH 037/520] ci: allow longer Windows workspace gate --- .github/workflows/workspace.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 1f66236e2..cf9499419 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -16,7 +16,7 @@ jobs: uses: storytold/craft-common-actions/.github/workflows/gate.yml@main with: os: '["ubuntu-latest","windows-latest","macos-latest"]' - timeout-minutes: 60 + timeout-minutes: 120 tools: "" packages: mesa-vulkan-drivers libvulkan1 craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 From 8c4113936910c0441acea94068fa8a5b6751a6ab Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:00:37 -0500 Subject: [PATCH 038/520] Add shared raster codec facade and versioned JSON headers --- Cargo.lock | 9 +++ Cargo.toml | 1 + crates/raster-codecs/Cargo.toml | 1 + crates/raster-codecs/src/common.rs | 92 ++++++++++++++++++++++++++++++ crates/versioned-json/Cargo.toml | 15 +++++ crates/versioned-json/src/lib.rs | 63 ++++++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 crates/raster-codecs/src/common.rs create mode 100644 crates/versioned-json/Cargo.toml create mode 100644 crates/versioned-json/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index cf0e0bd47..81cc73b2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2106,6 +2106,7 @@ dependencies = [ name = "craft-raster-codecs" version = "0.1.0" dependencies = [ + "image", "rayon", "thiserror 2.0.21", ] @@ -2343,6 +2344,14 @@ dependencies = [ "kurbo 0.13.1", ] +[[package]] +name = "craft-versioned-json" +version = "0.1.0" +dependencies = [ + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-video" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 629bab64c..5519f216b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ craft-aep = { path = "crates/aep" } craft-affinity = { path = "crates/affinity" } craft-ai-core = { path = "crates/ai-core" } craft-pdf = { path = "crates/pdf", version = "=0.1.0" } +craft-versioned-json = { path = "crates/versioned-json" } craft-pdn = { path = "crates/pdn" } craft-plate-test = { path = "crates/plate-test" } krilla = { version = "=0.8.2", default-features = false, features = ["raster-images"] } diff --git a/crates/raster-codecs/Cargo.toml b/crates/raster-codecs/Cargo.toml index 6fd3ab858..cadf9cbd0 100644 --- a/crates/raster-codecs/Cargo.toml +++ b/crates/raster-codecs/Cargo.toml @@ -12,6 +12,7 @@ workspace = true [dependencies] thiserror = { workspace = true } +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "bmp", "gif", "tiff", "webp"] } # The DDS encoder compresses rows of blocks in parallel on native; wasm stays single-threaded. [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs new file mode 100644 index 000000000..b12fb8b0a --- /dev/null +++ b/crates/raster-codecs/src/common.rs @@ -0,0 +1,92 @@ +//! Bounded decoding and PNG encoding for common photographic and web image formats. +//! +//! This facade is for applications that need a single RGBA pixel buffer regardless of whether +//! the source was PNG, JPEG, BMP, GIF, TIFF, or WebP. Format sniffing and codec dispatch stay +//! inside this crate. Callers choose their own stricter file and image limits. + +use std::io::Cursor; + +use image::{ImageReader, Limits as ImageLimits, RgbaImage}; + +use crate::CodecError; + +pub use image::RgbaImage as Pixels; + +/// Limits applied before allocating the decoded image. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DecodeLimits { + pub max_file_bytes: usize, + pub max_width: u32, + pub max_height: u32, + pub max_pixels: u64, + pub max_alloc_bytes: u64, +} + +impl Default for DecodeLimits { + fn default() -> Self { + Self { max_file_bytes: 64 * 1024 * 1024, max_width: 10_000, max_height: 10_000, + max_pixels: 16_000_000, max_alloc_bytes: 256 * 1024 * 1024 } + } +} + +/// Decode one frame into straight-alpha RGBA pixels. Animated images use the first frame. +pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result { + if bytes.len() > limits.max_file_bytes { + return Err(CodecError::LimitExceeded(format!("{} encoded bytes exceed max {}", bytes.len(), limits.max_file_bytes))); + } + if is_heif(bytes) { + return Err(CodecError::unsupported("image", "HEIC/HEIF is not supported by the enabled codecs")); + } + let reader = || -> Result>, CodecError> { + let mut reader = ImageReader::new(Cursor::new(bytes)).with_guessed_format() + .map_err(|e| CodecError::malformed("image", e))?; + if reader.format().is_none() { + return Err(CodecError::unsupported("image", "unrecognised image format")); + } + let mut image_limits = ImageLimits::default(); + image_limits.max_image_width = Some(limits.max_width); + image_limits.max_image_height = Some(limits.max_height); + image_limits.max_alloc = Some(limits.max_alloc_bytes); + reader.limits(image_limits); + Ok(reader) + }; + let (width, height) = reader()?.into_dimensions().map_err(|e| CodecError::malformed("image", e))?; + check_dimensions(width, height, limits)?; + let decoded = reader()?.decode().map_err(|e| CodecError::malformed("image", e))?; + check_dimensions(decoded.width(), decoded.height(), limits)?; + Ok(decoded.to_rgba8()) +} + +/// Encode an RGBA image as PNG. +pub fn encode_png(pixels: &RgbaImage) -> Result, CodecError> { + let mut out = Cursor::new(Vec::new()); + pixels.write_to(&mut out, image::ImageFormat::Png).map_err(|e| CodecError::encode("PNG", e))?; + Ok(out.into_inner()) +} + +/// Encode RGBA pixels as JPEG at the requested quality (1–100). +pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecError> { + if !(1..=100).contains(&quality) { + return Err(CodecError::InvalidImage(format!("JPEG quality {quality} is outside 1–100"))); + } + let rgb = image::DynamicImage::ImageRgba8(pixels.clone()).to_rgb8(); + let mut out = Cursor::new(Vec::new()); + image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, quality) + .encode_image(&rgb).map_err(|e| CodecError::encode("JPEG", e))?; + Ok(out.into_inner()) +} + +fn is_heif(bytes: &[u8]) -> bool { + bytes.get(4..8) == Some(b"ftyp") + && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) +} + +fn check_dimensions(width: u32, height: u32, limits: DecodeLimits) -> Result<(), CodecError> { + let pixels = u64::from(width).checked_mul(u64::from(height)) + .ok_or_else(|| CodecError::LimitExceeded("image dimensions overflow".into()))?; + if width == 0 || height == 0 || width > limits.max_width || height > limits.max_height || pixels > limits.max_pixels { + return Err(CodecError::LimitExceeded(format!("image dimensions {width}x{height} exceed configured limits"))); + } + Ok(()) +} + diff --git a/crates/versioned-json/Cargo.toml b/crates/versioned-json/Cargo.toml new file mode 100644 index 000000000..1b1bbd354 --- /dev/null +++ b/crates/versioned-json/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "craft-versioned-json" +description = "Reusable named and versioned JSON document envelopes" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[dependencies] +serde_json = { workspace = true } +thiserror = { workspace = true } + +[lints] +workspace = true diff --git a/crates/versioned-json/src/lib.rs b/crates/versioned-json/src/lib.rs new file mode 100644 index 000000000..f100920fb --- /dev/null +++ b/crates/versioned-json/src/lib.rs @@ -0,0 +1,63 @@ +//! Helpers for named, versioned JSON documents whose payload is an object. +//! +//! The library checks and writes the common envelope. Each application owns its payload schema, +//! migrations and validation. + +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use serde_json::{Map, Value}; + +/// A recognized document header. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Header { + pub name: String, + pub version: u64, +} + +/// Header errors, kept independent of an application's schema and error type. +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum Error { + #[error("missing or invalid `{0}` header")] + Missing(String), + #[error("expected format `{expected}`, found `{found}`")] + WrongName { expected: String, found: String }, + #[error("format version {found} is unsupported; this build reads {expected}")] + UnsupportedVersion { found: u64, expected: u64 }, +} + +/// Checks `root[header_key]` for the expected name and exact supported version. +pub fn read_header(root: &Value, header_key: &str, expected_name: &str, expected_version: u64) -> Result { + let header = root.get(header_key).ok_or_else(|| Error::Missing(header_key.to_owned()))?; + let name = header.get("name").and_then(Value::as_str).ok_or_else(|| Error::Missing(header_key.to_owned()))?; + let version = header.get("version").and_then(Value::as_u64).ok_or_else(|| Error::Missing(header_key.to_owned()))?; + if name != expected_name { + return Err(Error::WrongName { expected: expected_name.to_owned(), found: name.to_owned() }); + } + if version != expected_version { + return Err(Error::UnsupportedVersion { found: version, expected: expected_version }); + } + Ok(Header { name: name.to_owned(), version }) +} + +/// Adds a named/versioned header to a JSON object, creating the object if needed. +pub fn write_header(root: &mut Value, header_key: &str, name: &str, version: u64) -> Result<(), Error> { + if !root.is_object() { + *root = Value::Object(Map::new()); + } + let object = root.as_object_mut().ok_or_else(|| Error::Missing(header_key.to_owned()))?; + object.insert(header_key.to_owned(), serde_json::json!({ "name": name, "version": version })); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_and_writes_headers() { + let mut root = serde_json::json!({ "payload": true }); + write_header(&mut root, "format", "example", 3).unwrap(); + assert_eq!(read_header(&root, "format", "example", 3).unwrap().version, 3); + assert_eq!(read_header(&root, "format", "example", 4), Err(Error::UnsupportedVersion { found: 3, expected: 4 })); + } +} From 4ecdcd78dace41e6098512a663cdf62d20e372e1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:03:41 -0500 Subject: [PATCH 039/520] Export common raster codec facade --- Cargo.lock | 8 ---- Cargo.toml | 1 - crates/raster-codecs/src/common.rs | 16 +++----- crates/raster-codecs/src/lib.rs | 2 + crates/versioned-json/Cargo.toml | 15 ------- crates/versioned-json/src/lib.rs | 63 ------------------------------ 6 files changed, 7 insertions(+), 98 deletions(-) delete mode 100644 crates/versioned-json/Cargo.toml delete mode 100644 crates/versioned-json/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 81cc73b2c..4b671fb2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2344,14 +2344,6 @@ dependencies = [ "kurbo 0.13.1", ] -[[package]] -name = "craft-versioned-json" -version = "0.1.0" -dependencies = [ - "serde_json", - "thiserror 2.0.21", -] - [[package]] name = "craft-video" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 5519f216b..629bab64c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ craft-aep = { path = "crates/aep" } craft-affinity = { path = "crates/affinity" } craft-ai-core = { path = "crates/ai-core" } craft-pdf = { path = "crates/pdf", version = "=0.1.0" } -craft-versioned-json = { path = "crates/versioned-json" } craft-pdn = { path = "crates/pdn" } craft-plate-test = { path = "crates/plate-test" } krilla = { version = "=0.8.2", default-features = false, features = ["raster-images"] } diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index b12fb8b0a..b653db687 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -24,8 +24,7 @@ pub struct DecodeLimits { impl Default for DecodeLimits { fn default() -> Self { - Self { max_file_bytes: 64 * 1024 * 1024, max_width: 10_000, max_height: 10_000, - max_pixels: 16_000_000, max_alloc_bytes: 256 * 1024 * 1024 } + Self { max_file_bytes: 64 * 1024 * 1024, max_width: 10_000, max_height: 10_000, max_pixels: 16_000_000, max_alloc_bytes: 256 * 1024 * 1024 } } } @@ -38,8 +37,7 @@ pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result Result>, CodecError> { - let mut reader = ImageReader::new(Cursor::new(bytes)).with_guessed_format() - .map_err(|e| CodecError::malformed("image", e))?; + let mut reader = ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(|e| CodecError::malformed("image", e))?; if reader.format().is_none() { return Err(CodecError::unsupported("image", "unrecognised image format")); } @@ -71,22 +69,18 @@ pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecErro } let rgb = image::DynamicImage::ImageRgba8(pixels.clone()).to_rgb8(); let mut out = Cursor::new(Vec::new()); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, quality) - .encode_image(&rgb).map_err(|e| CodecError::encode("JPEG", e))?; + image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, quality).encode_image(&rgb).map_err(|e| CodecError::encode("JPEG", e))?; Ok(out.into_inner()) } fn is_heif(bytes: &[u8]) -> bool { - bytes.get(4..8) == Some(b"ftyp") - && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) + bytes.get(4..8) == Some(b"ftyp") && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) } fn check_dimensions(width: u32, height: u32, limits: DecodeLimits) -> Result<(), CodecError> { - let pixels = u64::from(width).checked_mul(u64::from(height)) - .ok_or_else(|| CodecError::LimitExceeded("image dimensions overflow".into()))?; + let pixels = u64::from(width).checked_mul(u64::from(height)).ok_or_else(|| CodecError::LimitExceeded("image dimensions overflow".into()))?; if width == 0 || height == 0 || width > limits.max_width || height > limits.max_height || pixels > limits.max_pixels { return Err(CodecError::LimitExceeded(format!("image dimensions {width}x{height} exceed configured limits"))); } Ok(()) } - diff --git a/crates/raster-codecs/src/lib.rs b/crates/raster-codecs/src/lib.rs index 880a32c6c..5b335a30c 100644 --- a/crates/raster-codecs/src/lib.rs +++ b/crates/raster-codecs/src/lib.rs @@ -18,6 +18,8 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +/// Common bounded decoding and PNG/JPEG encoding for common image formats. +pub mod common; pub mod dds; mod dds_bc; pub mod dicom; diff --git a/crates/versioned-json/Cargo.toml b/crates/versioned-json/Cargo.toml deleted file mode 100644 index 1b1bbd354..000000000 --- a/crates/versioned-json/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "craft-versioned-json" -description = "Reusable named and versioned JSON document envelopes" -version.workspace = true -edition.workspace = true -license.workspace = true -rust-version.workspace = true -repository.workspace = true - -[dependencies] -serde_json = { workspace = true } -thiserror = { workspace = true } - -[lints] -workspace = true diff --git a/crates/versioned-json/src/lib.rs b/crates/versioned-json/src/lib.rs deleted file mode 100644 index f100920fb..000000000 --- a/crates/versioned-json/src/lib.rs +++ /dev/null @@ -1,63 +0,0 @@ -//! Helpers for named, versioned JSON documents whose payload is an object. -//! -//! The library checks and writes the common envelope. Each application owns its payload schema, -//! migrations and validation. - -#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] - -use serde_json::{Map, Value}; - -/// A recognized document header. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Header { - pub name: String, - pub version: u64, -} - -/// Header errors, kept independent of an application's schema and error type. -#[derive(Debug, thiserror::Error, PartialEq, Eq)] -pub enum Error { - #[error("missing or invalid `{0}` header")] - Missing(String), - #[error("expected format `{expected}`, found `{found}`")] - WrongName { expected: String, found: String }, - #[error("format version {found} is unsupported; this build reads {expected}")] - UnsupportedVersion { found: u64, expected: u64 }, -} - -/// Checks `root[header_key]` for the expected name and exact supported version. -pub fn read_header(root: &Value, header_key: &str, expected_name: &str, expected_version: u64) -> Result { - let header = root.get(header_key).ok_or_else(|| Error::Missing(header_key.to_owned()))?; - let name = header.get("name").and_then(Value::as_str).ok_or_else(|| Error::Missing(header_key.to_owned()))?; - let version = header.get("version").and_then(Value::as_u64).ok_or_else(|| Error::Missing(header_key.to_owned()))?; - if name != expected_name { - return Err(Error::WrongName { expected: expected_name.to_owned(), found: name.to_owned() }); - } - if version != expected_version { - return Err(Error::UnsupportedVersion { found: version, expected: expected_version }); - } - Ok(Header { name: name.to_owned(), version }) -} - -/// Adds a named/versioned header to a JSON object, creating the object if needed. -pub fn write_header(root: &mut Value, header_key: &str, name: &str, version: u64) -> Result<(), Error> { - if !root.is_object() { - *root = Value::Object(Map::new()); - } - let object = root.as_object_mut().ok_or_else(|| Error::Missing(header_key.to_owned()))?; - object.insert(header_key.to_owned(), serde_json::json!({ "name": name, "version": version })); - Ok(()) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn reads_and_writes_headers() { - let mut root = serde_json::json!({ "payload": true }); - write_header(&mut root, "format", "example", 3).unwrap(); - assert_eq!(read_header(&root, "format", "example", 3).unwrap().version, 3); - assert_eq!(read_header(&root, "format", "example", 4), Err(Error::UnsupportedVersion { found: 3, expected: 4 })); - } -} From 5407ea8220a350b1b34f6fe41fa45cd4c71566a6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:04:29 -0500 Subject: [PATCH 040/520] Document required kerf field in native format example --- crates/stencilcraft-format/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/stencilcraft-format/src/lib.rs b/crates/stencilcraft-format/src/lib.rs index 9f1e2a2c4..81881f6a9 100644 --- a/crates/stencilcraft-format/src/lib.rs +++ b/crates/stencilcraft-format/src/lib.rs @@ -10,6 +10,7 @@ //! { //! "stencilcraft": { "name": "stencilcraft", "version": 1 }, //! "board": { "width_mm": 100.0, "height_mm": 150.0 }, +//! "kerf": { "width_mm": 0.0, "apply_to_exports": false }, //! "layers": [] //! } //! ``` From 1dbfad04fd9f010e355b8658e7defb97e756493a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:08:05 -0500 Subject: [PATCH 041/520] Support extension-based raster encoding --- crates/raster-codecs/src/common.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index b653db687..da3c1c39d 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -73,6 +73,16 @@ pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecErro Ok(out.into_inner()) } +/// Encode pixels according to a destination filename's extension (`png`, `jpg`, or `jpeg`). +pub fn encode_for_path(path: impl AsRef, pixels: &RgbaImage) -> Result, CodecError> { + let extension = path.as_ref().extension().and_then(std::ffi::OsStr::to_str).unwrap_or_default(); + match extension.to_ascii_lowercase().as_str() { + "png" => encode_png(pixels), + "jpg" | "jpeg" => encode_jpeg(pixels, 90), + other => Err(CodecError::unsupported("image", format!("cannot encode files with the .{other} extension"))), + } +} + fn is_heif(bytes: &[u8]) -> bool { bytes.get(4..8) == Some(b"ftyp") && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) } From dc705aa3647ef658b1367995796d20458d0b4882 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:09:00 -0500 Subject: [PATCH 042/520] Re-export common RGBA pixel types --- crates/raster-codecs/src/common.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index da3c1c39d..868e375d1 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -10,7 +10,7 @@ use image::{ImageReader, Limits as ImageLimits, RgbaImage}; use crate::CodecError; -pub use image::RgbaImage as Pixels; +pub use image::{Rgba as Pixel, RgbaImage as Pixels}; /// Limits applied before allocating the decoded image. #[derive(Debug, Clone, Copy, PartialEq, Eq)] From e95c99e7bcba0f70bae630596355c3ebd372fff6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:10:01 -0500 Subject: [PATCH 043/520] Support common raster output formats by path --- crates/raster-codecs/src/common.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index 868e375d1..fd8c1be60 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -73,16 +73,26 @@ pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecErro Ok(out.into_inner()) } -/// Encode pixels according to a destination filename's extension (`png`, `jpg`, or `jpeg`). +/// Encode pixels according to a destination filename's extension. pub fn encode_for_path(path: impl AsRef, pixels: &RgbaImage) -> Result, CodecError> { let extension = path.as_ref().extension().and_then(std::ffi::OsStr::to_str).unwrap_or_default(); match extension.to_ascii_lowercase().as_str() { "png" => encode_png(pixels), "jpg" | "jpeg" => encode_jpeg(pixels, 90), + "bmp" => encode_dynamic(pixels, image::ImageFormat::Bmp), + "gif" => encode_dynamic(pixels, image::ImageFormat::Gif), + "tif" | "tiff" => encode_dynamic(pixels, image::ImageFormat::Tiff), + "webp" => encode_dynamic(pixels, image::ImageFormat::WebP), other => Err(CodecError::unsupported("image", format!("cannot encode files with the .{other} extension"))), } } +fn encode_dynamic(pixels: &RgbaImage, format: image::ImageFormat) -> Result, CodecError> { + let mut out = Cursor::new(Vec::new()); + image::DynamicImage::ImageRgba8(pixels.clone()).write_to(&mut out, format).map_err(|e| CodecError::encode("image", e))?; + Ok(out.into_inner()) +} + fn is_heif(bytes: &[u8]) -> bool { bytes.get(4..8) == Some(b"ftyp") && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) } From 3afade51630fcdb02662c761e0805bc6147bb0ac Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:13:31 -0500 Subject: [PATCH 044/520] Expose renderer-neutral SVG preflight --- crates/svg/src/lib.rs | 1 + crates/svg/src/preflight.rs | 91 +++++++++++++++++++++++++++++++++++++ crates/svg/src/tree.rs | 34 +++++++------- 3 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 crates/svg/src/preflight.rs diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index fd6ca685e..982e4f82b 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -20,6 +20,7 @@ pub mod color; pub mod guard; pub mod paths; +pub mod preflight; pub mod sniff; pub mod svgz; #[cfg(feature = "tree")] diff --git a/crates/svg/src/preflight.rs b/crates/svg/src/preflight.rs new file mode 100644 index 000000000..3ab381ff3 --- /dev/null +++ b/crates/svg/src/preflight.rs @@ -0,0 +1,91 @@ +//! Decode and validate SVG/SVGZ text before a caller hands it to a renderer. +//! +//! This boundary has no dependency on an SVG renderer. It decodes the file's text encoding and +//! bounded gzip container, then checks entity expansion, attribute work, XML structure, node +//! count and nesting depth. + +use std::borrow::Cow; + +/// Limits for decoding and validating one SVG document. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// Bytes of source text, also the largest unpacked SVGZ. + pub bytes: usize, + /// Deepest XML element nesting (the root alone is 1). + pub depth: usize, + /// Total bytes entities may expand to. + pub entity_bytes: usize, + /// Refuse a doctype entity containing markup. + pub refuse_entity_markup: bool, + /// Attributes on one element. + pub attributes: usize, + /// Sum of squared attribute counts across the document. + pub attribute_work: usize, + /// Maximum XML nodes after entity expansion. + pub xml_nodes: u32, +} + +impl Default for Limits { + fn default() -> Self { + Limits { + bytes: 64 << 20, + depth: 256, + entity_bytes: crate::guard::MAX_ENTITY_EXPANSION, + refuse_entity_markup: false, + attributes: crate::guard::MAX_ATTRIBUTES, + attribute_work: crate::guard::MAX_ATTRIBUTE_WORK, + xml_nodes: 4_000_000, + } + } +} + +/// Why an SVG's text or XML could not be read within `Limits`. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The bytes are too large, corrupt SVGZ, nested SVGZ, or invalid text encoding. + #[error(transparent)] + Text(#[from] crate::svgz::Error), + /// Entity references expand past the configured limit. + #[error("the SVG's entities expand to too much text")] + Entities, + /// A doctype entity contains markup and is refused by the caller's policy. + #[error("the SVG's document type declares an entity holding markup, which isn't supported")] + EntityMarkup, + /// XML element nesting exceeds the configured limit. + #[error("the SVG's elements are nested more than {0} levels deep")] + TooDeep(usize), + /// Attribute count or total attribute work exceeds the configured limit. + #[error("an element of the SVG has more than {0} attributes, or its elements have too many in all")] + TooWide(usize), + /// The XML is malformed or exceeds the node limit. + #[error("invalid SVG XML: {0}")] + Xml(String), +} + +/// Decodes SVG or SVGZ text and checks the XML against `limits`. +pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result, Error> { + let text = crate::svgz::text(bytes, limits.bytes)?; + parse_text(&text, limits)?; + Ok(text) +} + +/// Parses already-decoded SVG text and checks it against `limits`. +pub fn parse_text<'a>(svg: &'a str, limits: &Limits) -> Result, Error> { + if svg.len() > limits.bytes { + return Err(Error::Text(crate::svgz::Error::TooLarge(limits.bytes))); + } + if limits.refuse_entity_markup && crate::guard::entity_holds_markup(svg) { + return Err(Error::EntityMarkup); + } + if crate::guard::entities_expand_past(svg, limits.entity_bytes) { + return Err(Error::Entities); + } + if crate::guard::too_wide(svg, limits.attributes, limits.attribute_work) { + return Err(Error::TooWide(limits.attributes)); + } + if crate::guard::deeper_than(svg, limits.depth) { + return Err(Error::TooDeep(limits.depth)); + } + let options = roxmltree::ParsingOptions { allow_dtd: true, nodes_limit: limits.xml_nodes, ..Default::default() }; + roxmltree::Document::parse_with_options(svg, options).map_err(|e| Error::Xml(e.to_string())) +} diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index 6c79c33a7..dd14be619 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -125,23 +125,23 @@ pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Re /// Every check on SVG text, cheapest first; the XML it parses to on the way. fn checked<'s>(svg: &'s str, limits: &Limits) -> Result, Error> { - if svg.len() > limits.bytes { - return Err(Error::Text(svgz::Error::TooLarge(limits.bytes))); - } - if limits.refuse_entity_markup && guard::entity_holds_markup(svg) { - return Err(Error::EntityMarkup); - } - if guard::entities_expand_past(svg, limits.entity_bytes) { - return Err(Error::Entities); - } - if guard::too_wide(svg, limits.attributes, limits.attribute_work) { - return Err(Error::TooWide(limits.attributes)); - } - if guard::deeper_than(svg, limits.depth) { - return Err(Error::TooDeep(limits.depth)); - } - let options = roxmltree::ParsingOptions { allow_dtd: true, nodes_limit: limits.xml_nodes, ..Default::default() }; - let xml = roxmltree::Document::parse_with_options(svg, options).map_err(|e| Error::Xml(e.to_string()))?; + let preflight_limits = crate::preflight::Limits { + bytes: limits.bytes, + depth: limits.depth, + entity_bytes: limits.entity_bytes, + refuse_entity_markup: limits.refuse_entity_markup, + attributes: limits.attributes, + attribute_work: limits.attribute_work, + xml_nodes: limits.xml_nodes, + }; + let xml = crate::preflight::parse_text(svg, &preflight_limits).map_err(|e| match e { + crate::preflight::Error::Text(e) => Error::Text(e), + crate::preflight::Error::Entities => Error::Entities, + crate::preflight::Error::EntityMarkup => Error::EntityMarkup, + crate::preflight::Error::TooDeep(depth) => Error::TooDeep(depth), + crate::preflight::Error::TooWide(attributes) => Error::TooWide(attributes), + crate::preflight::Error::Xml(msg) => Error::Xml(msg), + })?; if height(&xml, limits.depth)?.is_none() { return Err(Error::TooDeep(limits.depth)); } From 9430f2807e1c5f1cf852bad369feb10774759965 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:17:04 -0500 Subject: [PATCH 045/520] Add shared image codec facade --- ATTRIBUTION.md | 1 + Cargo.lock | 404 +++- Cargo.toml | 1 + NOTICE | 8 + crates/image-codecs/Cargo.toml | 45 + crates/image-codecs/README.md | 323 +++ crates/image-codecs/fuzz/.gitignore | 5 + crates/image-codecs/fuzz/Cargo.toml | 78 + .../fuzz/fuzz_targets/decode_any.rs | 18 + .../fuzz/fuzz_targets/decode_exr.rs | 12 + .../fuzz/fuzz_targets/decode_gif.rs | 13 + .../fuzz/fuzz_targets/decode_heif.rs | 15 + .../fuzz/fuzz_targets/decode_jpeg.rs | 12 + .../fuzz/fuzz_targets/decode_png.rs | 12 + .../fuzz/fuzz_targets/decode_tiff.rs | 20 + .../fuzz/fuzz_targets/decode_webp.rs | 13 + crates/image-codecs/src/cicp.rs | 182 ++ crates/image-codecs/src/codecs.rs | 25 + crates/image-codecs/src/codecs/dds.rs | 556 +++++ crates/image-codecs/src/codecs/dds_bc.rs | 368 +++ crates/image-codecs/src/codecs/deep_exr.rs | 786 ++++++ crates/image-codecs/src/codecs/dicom.rs | 517 ++++ crates/image-codecs/src/codecs/dpx.rs | 886 +++++++ crates/image-codecs/src/codecs/exr.rs | 441 ++++ .../src/codecs/exr_cryptomatte.rs | 266 ++ crates/image-codecs/src/codecs/fits.rs | 855 +++++++ crates/image-codecs/src/codecs/gif_frames.rs | 82 + crates/image-codecs/src/codecs/gif_palette.rs | 126 + crates/image-codecs/src/codecs/heif.rs | 126 + crates/image-codecs/src/codecs/iff.rs | 544 +++++ crates/image-codecs/src/codecs/jpeg.rs | 931 +++++++ crates/image-codecs/src/codecs/jxl.rs | 159 ++ crates/image-codecs/src/codecs/pcx.rs | 139 ++ crates/image-codecs/src/codecs/png.rs | 2139 +++++++++++++++++ crates/image-codecs/src/codecs/pnm.rs | 355 +++ crates/image-codecs/src/codecs/tga.rs | 175 ++ crates/image-codecs/src/codecs/tiff.rs | 220 ++ crates/image-codecs/src/codecs/tiff/bands.rs | 509 ++++ crates/image-codecs/src/codecs/tiff/decode.rs | 932 +++++++ crates/image-codecs/src/codecs/tiff/encode.rs | 659 +++++ crates/image-codecs/src/codecs/tiff/tags.rs | 99 + crates/image-codecs/src/codecs/tiff/ycbcr.rs | 222 ++ crates/image-codecs/src/codecs/tiff_exif.rs | 238 ++ crates/image-codecs/src/codecs/tiff_ifd.rs | 502 ++++ crates/image-codecs/src/codecs/via_image.rs | 455 ++++ crates/image-codecs/src/codecs/vp8.rs | 848 +++++++ .../src/codecs/vp8/bool_encoder.rs | 273 +++ crates/image-codecs/src/codecs/vp8/predict.rs | 382 +++ crates/image-codecs/src/codecs/vp8/tables.rs | 593 +++++ .../image-codecs/src/codecs/vp8/transform.rs | 183 ++ crates/image-codecs/src/codecs/webp.rs | 254 ++ crates/image-codecs/src/codecs/xpm.rs | 226 ++ .../image-codecs/src/codecs/xpm/x11_colors.rs | 666 +++++ crates/image-codecs/src/error.rs | 31 + crates/image-codecs/src/exif_color.rs | 138 ++ crates/image-codecs/src/exr_stream.rs | 294 +++ crates/image-codecs/src/fidelity.rs | 282 +++ crates/image-codecs/src/format.rs | 425 ++++ crates/image-codecs/src/image.rs | 1028 ++++++++ crates/image-codecs/src/lab.rs | 56 + crates/image-codecs/src/lib.rs | 421 ++++ crates/image-codecs/src/options.rs | 364 +++ crates/image-codecs/src/orientation.rs | 476 ++++ crates/image-codecs/src/resolution.rs | 575 +++++ crates/image-codecs/src/source_settings.rs | 355 +++ crates/image-codecs/src/web.rs | 525 ++++ crates/image-codecs/tests/it/camera_raw.rs | 134 ++ crates/image-codecs/tests/it/common/mod.rs | 122 + .../image-codecs/tests/it/common/tiffgen.rs | 327 +++ crates/image-codecs/tests/it/dds.rs | 410 ++++ crates/image-codecs/tests/it/dds_write.rs | 375 +++ .../image-codecs/tests/it/decode_warnings.rs | 511 ++++ crates/image-codecs/tests/it/deep_exr.rs | 992 ++++++++ crates/image-codecs/tests/it/detect_caps.rs | 309 +++ crates/image-codecs/tests/it/dpx.rs | 521 ++++ .../image-codecs/tests/it/exr_cryptomatte.rs | 271 +++ crates/image-codecs/tests/it/exr_multipart.rs | 334 +++ crates/image-codecs/tests/it/exr_stream.rs | 161 ++ crates/image-codecs/tests/it/fidelity.rs | 243 ++ crates/image-codecs/tests/it/gif_frames.rs | 168 ++ crates/image-codecs/tests/it/heif.rs | 194 ++ crates/image-codecs/tests/it/limits.rs | 670 ++++++ crates/image-codecs/tests/it/main.rs | 39 + crates/image-codecs/tests/it/malformed.rs | 270 +++ crates/image-codecs/tests/it/metadata.rs | 340 +++ crates/image-codecs/tests/it/orientation.rs | 670 ++++++ crates/image-codecs/tests/it/pcx.rs | 137 ++ crates/image-codecs/tests/it/png_jpeg.rs | 646 +++++ crates/image-codecs/tests/it/pnm_headers.rs | 82 + crates/image-codecs/tests/it/resolution.rs | 546 +++++ crates/image-codecs/tests/it/roundtrip.rs | 348 +++ .../image-codecs/tests/it/source_settings.rs | 111 + crates/image-codecs/tests/it/tga.rs | 91 + crates/image-codecs/tests/it/tiff_bands.rs | 367 +++ crates/image-codecs/tests/it/tiff_bench.rs | 124 + crates/image-codecs/tests/it/tiff_exif.rs | 262 ++ crates/image-codecs/tests/it/tiff_lab.rs | 109 + crates/image-codecs/tests/it/tiff_pages.rs | 841 +++++++ .../image-codecs/tests/it/tiff_photoshop.rs | 436 ++++ crates/image-codecs/tests/it/tiff_pyramid.rs | 184 ++ .../image-codecs/tests/it/tiff_regressions.rs | 176 ++ crates/image-codecs/tests/it/tiff_ycbcr.rs | 259 ++ crates/image-codecs/tests/it/webp_frames.rs | 166 ++ crates/image-codecs/tests/it/webp_lossy.rs | 140 ++ crates/image-codecs/tests/it/xpm.rs | 123 + crates/image-codecs/tests/jxl_export.rs | 110 + crates/image-codecs/tests/pnm_peak_memory.rs | 36 + crates/raster-codecs/Cargo.toml | 3 +- crates/raster-codecs/README.md | 6 +- crates/raster-codecs/src/common.rs | 75 +- 110 files changed, 35260 insertions(+), 48 deletions(-) create mode 100644 crates/image-codecs/Cargo.toml create mode 100644 crates/image-codecs/README.md create mode 100644 crates/image-codecs/fuzz/.gitignore create mode 100644 crates/image-codecs/fuzz/Cargo.toml create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_any.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_png.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs create mode 100644 crates/image-codecs/src/cicp.rs create mode 100644 crates/image-codecs/src/codecs.rs create mode 100644 crates/image-codecs/src/codecs/dds.rs create mode 100644 crates/image-codecs/src/codecs/dds_bc.rs create mode 100644 crates/image-codecs/src/codecs/deep_exr.rs create mode 100644 crates/image-codecs/src/codecs/dicom.rs create mode 100644 crates/image-codecs/src/codecs/dpx.rs create mode 100644 crates/image-codecs/src/codecs/exr.rs create mode 100644 crates/image-codecs/src/codecs/exr_cryptomatte.rs create mode 100644 crates/image-codecs/src/codecs/fits.rs create mode 100644 crates/image-codecs/src/codecs/gif_frames.rs create mode 100644 crates/image-codecs/src/codecs/gif_palette.rs create mode 100644 crates/image-codecs/src/codecs/heif.rs create mode 100644 crates/image-codecs/src/codecs/iff.rs create mode 100644 crates/image-codecs/src/codecs/jpeg.rs create mode 100644 crates/image-codecs/src/codecs/jxl.rs create mode 100644 crates/image-codecs/src/codecs/pcx.rs create mode 100644 crates/image-codecs/src/codecs/png.rs create mode 100644 crates/image-codecs/src/codecs/pnm.rs create mode 100644 crates/image-codecs/src/codecs/tga.rs create mode 100644 crates/image-codecs/src/codecs/tiff.rs create mode 100644 crates/image-codecs/src/codecs/tiff/bands.rs create mode 100644 crates/image-codecs/src/codecs/tiff/decode.rs create mode 100644 crates/image-codecs/src/codecs/tiff/encode.rs create mode 100644 crates/image-codecs/src/codecs/tiff/tags.rs create mode 100644 crates/image-codecs/src/codecs/tiff/ycbcr.rs create mode 100644 crates/image-codecs/src/codecs/tiff_exif.rs create mode 100644 crates/image-codecs/src/codecs/tiff_ifd.rs create mode 100644 crates/image-codecs/src/codecs/via_image.rs create mode 100644 crates/image-codecs/src/codecs/vp8.rs create mode 100644 crates/image-codecs/src/codecs/vp8/bool_encoder.rs create mode 100644 crates/image-codecs/src/codecs/vp8/predict.rs create mode 100644 crates/image-codecs/src/codecs/vp8/tables.rs create mode 100644 crates/image-codecs/src/codecs/vp8/transform.rs create mode 100644 crates/image-codecs/src/codecs/webp.rs create mode 100644 crates/image-codecs/src/codecs/xpm.rs create mode 100644 crates/image-codecs/src/codecs/xpm/x11_colors.rs create mode 100644 crates/image-codecs/src/error.rs create mode 100644 crates/image-codecs/src/exif_color.rs create mode 100644 crates/image-codecs/src/exr_stream.rs create mode 100644 crates/image-codecs/src/fidelity.rs create mode 100644 crates/image-codecs/src/format.rs create mode 100644 crates/image-codecs/src/image.rs create mode 100644 crates/image-codecs/src/lab.rs create mode 100644 crates/image-codecs/src/lib.rs create mode 100644 crates/image-codecs/src/options.rs create mode 100644 crates/image-codecs/src/orientation.rs create mode 100644 crates/image-codecs/src/resolution.rs create mode 100644 crates/image-codecs/src/source_settings.rs create mode 100644 crates/image-codecs/src/web.rs create mode 100644 crates/image-codecs/tests/it/camera_raw.rs create mode 100644 crates/image-codecs/tests/it/common/mod.rs create mode 100644 crates/image-codecs/tests/it/common/tiffgen.rs create mode 100644 crates/image-codecs/tests/it/dds.rs create mode 100644 crates/image-codecs/tests/it/dds_write.rs create mode 100644 crates/image-codecs/tests/it/decode_warnings.rs create mode 100644 crates/image-codecs/tests/it/deep_exr.rs create mode 100644 crates/image-codecs/tests/it/detect_caps.rs create mode 100644 crates/image-codecs/tests/it/dpx.rs create mode 100644 crates/image-codecs/tests/it/exr_cryptomatte.rs create mode 100644 crates/image-codecs/tests/it/exr_multipart.rs create mode 100644 crates/image-codecs/tests/it/exr_stream.rs create mode 100644 crates/image-codecs/tests/it/fidelity.rs create mode 100644 crates/image-codecs/tests/it/gif_frames.rs create mode 100644 crates/image-codecs/tests/it/heif.rs create mode 100644 crates/image-codecs/tests/it/limits.rs create mode 100644 crates/image-codecs/tests/it/main.rs create mode 100644 crates/image-codecs/tests/it/malformed.rs create mode 100644 crates/image-codecs/tests/it/metadata.rs create mode 100644 crates/image-codecs/tests/it/orientation.rs create mode 100644 crates/image-codecs/tests/it/pcx.rs create mode 100644 crates/image-codecs/tests/it/png_jpeg.rs create mode 100644 crates/image-codecs/tests/it/pnm_headers.rs create mode 100644 crates/image-codecs/tests/it/resolution.rs create mode 100644 crates/image-codecs/tests/it/roundtrip.rs create mode 100644 crates/image-codecs/tests/it/source_settings.rs create mode 100644 crates/image-codecs/tests/it/tga.rs create mode 100644 crates/image-codecs/tests/it/tiff_bands.rs create mode 100644 crates/image-codecs/tests/it/tiff_bench.rs create mode 100644 crates/image-codecs/tests/it/tiff_exif.rs create mode 100644 crates/image-codecs/tests/it/tiff_lab.rs create mode 100644 crates/image-codecs/tests/it/tiff_pages.rs create mode 100644 crates/image-codecs/tests/it/tiff_photoshop.rs create mode 100644 crates/image-codecs/tests/it/tiff_pyramid.rs create mode 100644 crates/image-codecs/tests/it/tiff_regressions.rs create mode 100644 crates/image-codecs/tests/it/tiff_ycbcr.rs create mode 100644 crates/image-codecs/tests/it/webp_frames.rs create mode 100644 crates/image-codecs/tests/it/webp_lossy.rs create mode 100644 crates/image-codecs/tests/it/xpm.rs create mode 100644 crates/image-codecs/tests/jxl_export.rs create mode 100644 crates/image-codecs/tests/pnm_peak_memory.rs diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 965b348e4..19d38973d 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -82,6 +82,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso | `hayro-interpret 0.7.0 / src/font/generated/metrics.rs` | Adobe (Core 14 AFM metrics, PDF specification encodings); tables by The Hayro Authors | https://github.com/storytold/pdfcraft/blob/7437bde378f43856eada40af25b983812e615e8a/vendor/hayro-interpret/src/font/generated/metrics.rs | MIT OR Apache-2.0 | `2ec9e608573eda459de1e1d9bda3bb494315e36bee885acc88912a11fe0caa41` | Glyph widths and encodings for non-embedded standard fonts. Non-visual dependency data; existing embedding unchanged | | `crates/audio/src/ac3/golden_frames.bin` | FilmCraft contributors (original work: 12 AC-3 and E-AC-3 syncframes that ffmpeg's `ac3` and `eac3` encoders made from tones and seeded noise synthesised by ffmpeg's lavfi generators; no third-party or recorded audio) | [FilmCraft `crates/ac3`](https://github.com/storytold/filmcraft/tree/1c9c2e82/crates/ac3/src) | MIT OR Apache-2.0 | | `crates/audio/tests/fixtures/` | SoundCraft and craft-libs contributors (original work: 0.1 s test tones from ffmpeg's `sine` generator and hand-built silent MPEG frames; no third-party or recorded audio; commands in `crates/audio/tests/fixtures/README.md`) | [SoundCraft `crates/audio-io/tests/fixtures`](https://github.com/storytold/soundcraft/tree/e86d09a/crates/audio-io/tests/fixtures); `tone.mov`, `tone.mka`, `tone.opus`, `tone.ac3` and `tone.eac3` made in this repository | MIT OR Apache-2.0 | +| `crates/image-codecs/src/codecs/xpm/x11_colors.rs` | X.Org Foundation and X11 contributors (X11 `rgb.txt` colour names and values, normalised into a lookup table) | , via [PhotoCraft `crates/codecs`](https://github.com/storytold/photocraft/tree/5000240c6/crates/codecs/src/codecs/xpm) | MIT (X.Org app/rgb) | | `crates/raster-codecs/src/xpm/x11_colors.rs` | X.Org Foundation and X11 contributors (X11 `rgb.txt` colour names and values, normalised into a lookup table) | , via [PhotoCraft `crates/codecs`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/codecs/src/codecs/xpm) | MIT (X.Org app/rgb) | | `crates/psd/tests/fixtures/atn/*.atn` (seven synthetic action sets, encoded field by field; `crates/psd/tests/fixtures/atn/README.md`) | PhotoCraft contributors | [PhotoCraft `crates/psd/tests/fixtures/atn`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/psd/tests/fixtures/atn) | MIT OR Apache-2.0 | | `crates/lottie/tests/fixtures/` | EffectCraft contributors (original work: four hand-written Lottie JSON documents, written from the public Lottie format description; not exported from any Adobe product or third-party file) | [EffectCraft `crates/lottie/tests/fixtures`](https://github.com/storytold/effectcraft/tree/e18f48fb/crates/lottie/tests/fixtures) | MIT OR Apache-2.0 | diff --git a/Cargo.lock b/Cargo.lock index 4b671fb2e..c5ff17d87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,24 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aligned" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee4508988c62edf04abd8d92897fca0c2995d907ce1dfeaf369dac3716a40685" +dependencies = [ + "as-slice", +] + +[[package]] +name = "aligned-vec" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc890384c8602f339876ded803c97ad529f3842aba97f6392b3dba0dd171769b" +dependencies = [ + "equator", +] + [[package]] name = "alloc-no-stdlib" version = "2.0.4" @@ -180,6 +198,23 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + +[[package]] +name = "arg_enum_proc_macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "argon2" version = "0.5.3" @@ -214,6 +249,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "ash" version = "0.38.0+1.3.281" @@ -366,6 +410,49 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "av-scenechange" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f321d77c20e19b92c39e7471cf986812cbb46659d2af674adc4331ef3f18394" +dependencies = [ + "aligned", + "anyhow", + "arg_enum_proc_macro", + "arrayvec", + "log", + "num-rational", + "num-traits", + "pastey", + "rayon", + "thiserror 2.0.21", + "v_frame", + "y4m", +] + +[[package]] +name = "av1-grain" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cfddb07216410377231960af4fcab838eaa12e013417781b78bd95ee22077f8" +dependencies = [ + "anyhow", + "arrayvec", + "log", + "nom", + "num-rational", + "v_frame", +] + +[[package]] +name = "avif-serialize" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7178fe5f7d460b13895ebb9dcb28a3a6216d2df2574a0806cb51b555d297f38" +dependencies = [ + "arrayvec", +] + [[package]] name = "aws-lc-rs" version = "1.18.1" @@ -493,6 +580,15 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "bitstream-io" +version = "4.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eff00be299a18769011411c9def0d827e8f2d7bf0c3dbf53633147a8867fd1f" +dependencies = [ + "no_std_io2", +] + [[package]] name = "bitvec" version = "1.1.1" @@ -703,6 +799,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "built" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0e531d93d39c34eef561e929e8a7f86d77a5af08aac4f6d6e39976c51858e9" + [[package]] name = "bumpalo" version = "3.20.3" @@ -1628,6 +1730,30 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +dependencies = [ + "craft-heif", + "craft-jpeg", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder 0.6.1", + "jxl-oxide", + "png 0.18.1", + "proptest", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + [[package]] name = "craft-jmap" version = "0.1.0" @@ -2106,6 +2232,7 @@ dependencies = [ name = "craft-raster-codecs" version = "0.1.0" dependencies = [ + "craft-image-codecs", "image", "rayon", "thiserror 2.0.21", @@ -3135,7 +3262,7 @@ dependencies = [ "bitflags 2.13.2", "emath", "epaint", - "itertools", + "itertools 0.15.0", "log", "nohash-hasher", "profiling", @@ -3192,7 +3319,7 @@ dependencies = [ "ahash", "egui", "enum-map", - "itertools", + "itertools 0.15.0", "log", "profiling", "resvg", @@ -3421,6 +3548,26 @@ name = "epaint_default_fonts" version = "0.37.0" source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" +[[package]] +name = "equator" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4711b213838dfee0117e3be6ac926007d7f433d7bbe33595975d4190cb07e6fc" +dependencies = [ + "equator-macro", +] + +[[package]] +name = "equator-macro" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44f23cf4b44bfce11a86ace86f8a73ffdec849c9fd00a386a53d278bd9e81fb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -3477,6 +3624,7 @@ dependencies = [ "lebe", "miniz_oxide 0.8.9", "multiversion", + "rayon-core", "smallvec", "zune-inflate", ] @@ -4696,6 +4844,9 @@ dependencies = [ "moxcms", "num-traits", "png 0.18.1", + "qoi", + "ravif", + "rgb", "tiff", "zune-core", "zune-jpeg", @@ -4729,6 +4880,12 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" +[[package]] +name = "imgref" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e44b0a4eaa4c82f441d50a963f2d5f05a787240aeee097597033e72accfd22f" + [[package]] name = "indexmap" version = "2.14.2" @@ -4758,6 +4915,17 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "interpolate_name" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "io-extras" version = "0.19.0" @@ -4805,6 +4973,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.15.0" @@ -5069,6 +5246,8 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25f15eb830aa77a7f21148d72e153562a26bfe570139bd4922eab1908dd499d3" dependencies = [ + "rayon", + "rayon-core", "tracing", ] @@ -5229,6 +5408,16 @@ version = "0.2.190" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" +[[package]] +name = "libfuzzer-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2" +dependencies = [ + "arbitrary", + "cc", +] + [[package]] name = "libloading" version = "0.8.9" @@ -5316,6 +5505,15 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" +[[package]] +name = "loop9" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fae87c125b03c1d2c0150c90365d7d6bcc53fb73a9acaef207d2d065860f062" +dependencies = [ + "imgref", +] + [[package]] name = "lru-slab" version = "0.1.3" @@ -5328,6 +5526,15 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if", +] + [[package]] name = "md-5" version = "0.10.6" @@ -5543,6 +5750,12 @@ dependencies = [ "jni-sys 0.3.1", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "nix" version = "0.31.3" @@ -5555,6 +5768,15 @@ dependencies = [ "libc", ] +[[package]] +name = "no_std_io2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003" +dependencies = [ + "memchr", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -5570,6 +5792,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "noop_proc_macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -5579,6 +5807,16 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-bigint-dig" version = "0.8.6" @@ -5611,6 +5849,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "num-integer" version = "0.1.47" @@ -5630,6 +5879,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -6187,6 +6447,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pastey" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec" + [[package]] name = "pbkdf2" version = "0.13.0" @@ -6713,6 +6985,19 @@ name = "profiling" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +dependencies = [ + "profiling-procmacros", +] + +[[package]] +name = "profiling-procmacros" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb" +dependencies = [ + "quote", + "syn 2.0.119", +] [[package]] name = "proptest" @@ -6740,6 +7025,15 @@ version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -6861,10 +7155,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" dependencies = [ "libc", - "rand_chacha", + "rand_chacha 0.3.1", "rand_core 0.6.4", ] +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + [[package]] name = "rand" version = "0.10.3" @@ -6886,6 +7190,16 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + [[package]] name = "rand_core" version = "0.6.4" @@ -6895,6 +7209,15 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + [[package]] name = "rand_core" version = "0.10.1" @@ -6925,6 +7248,55 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" +[[package]] +name = "rav1e" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b6dd56e85d9483277cde964fd1bdb0428de4fec5ebba7540995639a21cb32b" +dependencies = [ + "aligned-vec", + "arbitrary", + "arg_enum_proc_macro", + "arrayvec", + "av-scenechange", + "av1-grain", + "bitstream-io", + "built", + "cfg-if", + "interpolate_name", + "itertools 0.14.0", + "libc", + "libfuzzer-sys", + "log", + "maybe-rayon", + "new_debug_unreachable", + "noop_proc_macro", + "num-derive", + "num-traits", + "paste", + "profiling", + "rand 0.9.5", + "rand_chacha 0.9.0", + "simd_helpers", + "thiserror 2.0.21", + "v_frame", + "wasm-bindgen", +] + +[[package]] +name = "ravif" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e52310197d971b0f5be7fe6b57530dcd27beb35c1b013f29d66c1ad73fbbcc45" +dependencies = [ + "avif-serialize", + "imgref", + "loop9", + "quick-error 2.0.1", + "rav1e", + "rgb", +] + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -7962,6 +8334,15 @@ dependencies = [ "simdutf8", ] +[[package]] +name = "simd_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" +dependencies = [ + "quote", +] + [[package]] name = "simdutf8" version = "0.1.5" @@ -9176,6 +9557,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "v_frame" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "666b7727c8875d6ab5db9533418d7c764233ac9c0cff1d469aec8fa127597be2" +dependencies = [ + "aligned-vec", + "num-traits", + "wasm-bindgen", +] + [[package]] name = "vello_common" version = "0.0.8" @@ -10198,6 +10590,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" +[[package]] +name = "y4m" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448" + [[package]] name = "yeslogic-fontconfig-sys" version = "6.0.1" diff --git a/Cargo.toml b/Cargo.toml index 629bab64c..53395992e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,6 +100,7 @@ craft-prepress = { path = "crates/prepress" } craft-print = { path = "crates/print" } craft-proof = { path = "crates/proof" } craft-raster-codecs = { path = "crates/raster-codecs" } +craft-image-codecs = { path = "crates/image-codecs" } craft-psd = { path = "crates/psd" } craft-pst = { path = "crates/pst" } craft-quit = { path = "crates/quit" } diff --git a/NOTICE b/NOTICE index c6583cc70..10e0fcc19 100644 --- a/NOTICE +++ b/NOTICE @@ -89,6 +89,14 @@ The optional PDF writer bundles four CC0 ICC profiles by Clinton Ingram. See ATT Source provenance +crates/image-codecs (craft-image-codecs) is moved from PhotoCraft's standalone `photocraft-codecs` +crate, including its bounded decoders, encoders, format registry, image model, metadata handling, +orientation, fidelity warnings, and tests, from revision 5000240c6: +https://github.com/storytold/photocraft +Copyright (c) 2026 ArtCraft Team and the PhotoCraft contributors. The format implementations are +clean-room work based on their public specifications; dependency licences and X.Org's `rgb.txt` +attribution are listed in ATTRIBUTION.md. + crates/raw (craft-raw) is moved from LightCraft, with behaviour ported from two other readers: - LightCraft, crates/raw (src/lib.rs, dng.rs, dngwrite.rs, tiffraw.rs, unpack.rs, ljpeg.rs, jxl.rs, opcodes.rs, profile.rs, gaintable.rs, semantic.rs, preview.rs, vendor/crw.rs, vendor/hasselblad.rs, diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml new file mode 100644 index 000000000..30f7f7046 --- /dev/null +++ b/crates/image-codecs/Cargo.toml @@ -0,0 +1,45 @@ +[package] +name = "craft-image-codecs" +description = "Bounded, depth- and metadata-preserving codecs for flat raster image formats" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[features] +corpus = [] +default = [] +avif = ["image/avif"] +heif = ["dep:craft-heif"] + +[dependencies] +thiserror = { workspace = true } +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff", "gif", "bmp", "tga", "ico", "qoi", "hdr"] } +png = "0.18" +tiff = { version = "0.11", default-features = false, features = ["deflate", "fax", "jpeg", "lzw"] } +exr = { version = "1.73", default-features = false } +half = "2" +serde_json = { workspace = true } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } +image-webp = "0.2" +zune-jpeg = "0.5" +zune-core = "0.5" +jpeg-encoder = "0.6" +craft-jpeg = { workspace = true } +zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } +craft-heif = { workspace = true, optional = true } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +rayon = { workspace = true } +exr = { version = "1.73", default-features = false, features = ["rayon"] } + +[dev-dependencies] +proptest = { workspace = true } +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff"] } + +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +jxl-oxide = { version = "=0.12.6", default-features = false, features = ["rayon"] } diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md new file mode 100644 index 000000000..0fdcd6ae4 --- /dev/null +++ b/crates/image-codecs/README.md @@ -0,0 +1,323 @@ +# craft-image-codecs + +Codecs for flat raster image formats, written in pure Rust. This crate defines its own `Image` +type and shares its baseline and extended JPEG decoders through `craft-jpeg`, and its optional +HEIF decoder through `craft-heif`. It builds for `wasm32-unknown-unknown` and does no filesystem +I/O: decoders take `&[u8]` and encoders return `Vec`. + +It follows the "Avoiding GIMP's hole" rules in `docs/architecture.md` §1.1: + +* **Symmetric.** Every format we write, we can also read. The only exceptions are listed in + `ASYMMETRIC_EXCEPTIONS`, each with a reason. The test suite checks this. +* **Depth-preserving.** An image can hold U8, U16, F16 or F32 samples, and each format keeps + what it can hold. +* **Metadata-preserving.** ICC, EXIF, XMP, DPI and text are kept wherever the format supports + them. +* **Honest.** `caps(format)` says what each format holds, and + `fidelity_warnings(&image, format)` lists what an export will lose before you write it. Both + functions use the same encode plan as `encode`, so a warning appears exactly when the file + changes. On the way in, `Image::warnings` lists what the decoded image doesn't show: frames or + pages left out, or data that ended early. + +```rust +use craft_image_codecs::*; + +let img = decode(&bytes)?; // detect + decode (default Limits) +for w in fidelity_warnings(&img, Format::Jpeg) { // e.g. "16-bit will be reduced to 8-bit" + println!("{w}"); +} +let out = encode(&img, Format::Tiff, &EncodeOptions::default())?; +``` + +```toml +craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "" } +``` + +## Capability matrix (default build) + +| Format | Read | Write | Depths (native) | Layouts (native) | Alpha | ICC | EXIF | XMP | DPI | Text | Lossy write | Backend | +|---|---|---|---|---|---|---|---|---|---|---|---|---| +| PNG | yes | yes | U8, U16 | Gray, GrayA, RGB, RGBA | yes | yes (iCCP) | yes (eXIf) | yes (iTXt `XML:com.adobe.xmp`) | yes (pHYs) | yes (tEXt/zTXt/iTXt) | no | `png` | +| JPEG | yes | yes | U8 | Gray, RGB, CMYK | no | yes (multi-segment APP2) | yes (APP1) | yes (APP1) | yes (JFIF) | no | yes | `zune-jpeg` / `jpeg-encoder` | +| TIFF | yes | yes | U8, U16, F32 | all six (CMYK, CMYK+A included) | yes | yes (tag 34675) | no | yes (tag 700) | yes | yes (Description, Make, Model, Software, DateTime, Artist, Copyright) | no | `tiff` | +| WebP | yes | yes | U8 | RGB, RGBA | yes | yes | yes | yes | no | no | yes (VP8, when `webp_lossless` is off) | `image-webp` / built-in VP8 | +| GIF | yes | yes | U8 | RGBA | 1-bit | no | no | no | no | no | yes (256-colour palette) | `image` | +| BMP | yes | yes | U8 | RGB, RGBA | yes | no | no | no | no | no | no | `image` | +| TGA | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | no | `image` (read) / built in (RLE write, one scanline per packet) | +| ICO | yes | yes | U8 | RGBA (at most 256×256) | yes | no | no | no | no | no | no | `image` | +| DDS | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | yes (BC1–BC3, when `EncodeOptions::dds` asks for one) | built in | +| Netpbm (PBM/PGM/PPM/PAM/PFM) | yes | yes | U8, U16, F32 (float: PFM gray/RGB only) | all six (PAM `CMYK`/`CMYK_ALPHA`) | yes (PAM) | no | no | no | no | no | no | built in | +| QOI | yes | yes | U8 | RGB, RGBA | yes | no | no | no | no | no | no | `image` | +| OpenEXR | yes | yes | F16, F32 | Y, YA, RGB, RGBA | yes | no | no | no | no | no | no | `exr` | +| Radiance HDR | yes | yes | F32 | RGB | no | no | no | no | no | no | yes (RGBE) | `image` | +| FITS | yes | yes | U8, U16, F32 | Gray, RGB | no | no | no | no | no | yes (header cards) | no | built in | +| JPEG XL (.jxl) | **no** (see #2868) | yes | U8, U16 | Gray, RGB, RGBA (GrayA expands to RGBA) | yes | no (bare codestream) | no | no | no | no | no | `zune-jpegxl` | +| DPX | yes | yes | U8, U16 (10/12-bit code values read and written exactly through U16) | Gray, RGB, RGBA | yes | no | no | no | no | DPX header fields only (creator, project, copyright, creation time, transfer, colorimetric, frame position, frame rate, time code) | no | built in | +| Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | +| DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | +| Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | +| HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | + +"Native" means the data is stored and read back without conversion. Anything else is converted by +the encode plan, and `fidelity_warnings` reports the conversion when it loses information: + +* A float image written to an integer format goes to U16 where the format supports it. Values + outside 0..1 raise `RangeClipped`. +* Integer data written to a float-only format (EXR, HDR) becomes F32 without loss. +* CMYK is converted to RGB with a naive formula that is not colour-managed. `CmykConverted` is + raised and the ICC profile is dropped, because a CMYK profile does not describe RGB data. +* Gray written to an RGB-only format is expanded to RGB without loss, so no warning is raised. + +`Image::convert` applies the same ICC rule: it keeps the profile only when the colour model stays +the same. + +Palette BMP and PNG pixels decode as RGB(A) in `Image`; `bmp_palette` and `png_palette` expose the +embedded table (and a PNG's transparent entry) so `photocraft-io` can open them in Indexed Color mode. +`bmp_palette` returns the colours the header declares, not the decoder's 256-entry padding. +`encode_bmp_indexed` writes the inverse: indices plus a table as an 8-bit palette BMP. + +## Documented asymmetries and limitations + +* **JPEG XL export.** Writes lossless 8/16-bit bare codestreams with `zune-jpegxl`. + Native layouts are Gray, RGB and RGBA. GrayA is expanded to RGBA at the + original depth. For 16-bit RGBA (including expanded GrayA), we correct + zune-jpegxl 0.5.2's extra-channel header, which otherwise describes the + 16-bit alpha samples as 8-bit. This preserves alpha precision; the independent + jxl-oxide decoder test checks the actual gray and alpha samples. The expansion + is not a native GrayA layout. No ICC, EXIF, XMP or DPI container boxes are written; the + document pipeline converts tagged images to sRGB and reports metadata loss. + Reading remains in the separate #2868 PR. The `ASYMMETRIC_EXCEPTIONS` + entry stays until import is merged. + +* **DDS.** The top mip level of a 2D texture: uncompressed pixels described by channel masks + (8, 16, 24 or 32 bits, RGB or luminance, with or without alpha) or BC1/BC2/BC3 (DXT1/DXT3/DXT5), + with the legacy header or the DX10 one. Uncompressed row padding follows the header's + `DDSD_PITCH` stride; without that flag, rows are tightly packed. Everything opens as U8; the + other mip levels are ignored. Cubemaps, texture arrays, volume textures, premultiplied DXT2/DXT4 and other formats + (BC4–BC7, float) are refused with `CodecError::Unsupported`. Writing (`EncodeOptions::dds`) makes + a 2D texture with the legacy header: uncompressed by default (BGRA, BGRX, or luminance with or + without alpha, so it reads back exactly), or BC1/BC2/BC3. The block encoder is our own, from + Microsoft's public block-compression documentation: RGB565 endpoints fitted along each block's + principal axis and refined by least squares, indices picked against the palette the reader + decodes; BC1 turns texels under half opacity into its transparent black, and BC3 picks + whichever alpha mode fits. `mipmaps` adds the full chain down to 1×1, each level a box filter + of the one above (the last column and row of an odd-sized level average three pixels, so none + is dropped). Cubemaps, arrays, BC4–BC7 and sRGB (DX10-only) formats aren't written. + +* **FITS.** The image in the primary HDU, or the first `IMAGE` extension, is read; `NAXIS3 = 3` + opens as RGB and any other plane count opens the first plane with `DecodeWarning::MorePages`. + Rows follow `ROWORDER` (bottom-up by default, as Siril and DS9 do). Physical values that fit + stay exact as U8/U16 (a camera's `BITPIX = 16`, `BZERO = 32768`); the rest become F32, taken as + normalised when at most 2 and scaled by 65535 (or the maximum) otherwise. Header cards are + kept as text (`HIERARCH` for long names). Tile-compressed (fpack) files are refused with + `CodecError::Unsupported`; Bayer mosaics open undebayered. + +* **DPX.** SMPTE 268M frames in either byte order (`SDPX`, `XPDS`): the first image element, + descriptor 6 (luma), 50 (RGB) or 51 (RGBA), unsigned and unencoded, at 8, 10, 12 or 16 bits. + 10-bit data may be packed (0) or filled three to a word (1, method A; 2, method B), 12-bit data + packed (0) or one to a 16-bit word (1, method A). 10- and 12-bit code values open as U16 + `round(c × 65535 / (2^b − 1))`, so a frame written back at its depth keeps them exactly. No + colour transform is applied: `DecodeWarning::DpxEncoding` names the transfer characteristic and + colorimetric specification, so a log frame is reported as log. More elements than the first + give `DecodeWarning::MoreElements`; the image orientation is applied on import. Writing is + big-endian, one unencoded element, at `EncodeOptions::dpx_bits` (8/10/12/16; by default 8 for + U8 and 16 otherwise), packing 1 for 10 and 12 bits; gray with alpha is written as luma. + `FidelityWarning::BitsReduced` reports a 10- or 12-bit save (and every Cineon save) that drops + precision: always for float samples, for U16 ones unless they are already code values at that + depth. + Other descriptors, signed or run-length-encoded data and other bit depths return + `CodecError::Unsupported` naming the field. + +* **Cineon.** Kodak's 10-bit logarithmic film-scan format (the one DPX grew from), + read from the same Kodak header family: the generic 1024-byte header, big-endian, one (Gray) + or three (RGB) channels, three 10-bit samples packed into each 32-bit word, lines on a word + boundary. The 10-bit code values open as U16 `round(c × 65535 / 1023)`; the file's black/white + reference points are a log-conversion choice and are not applied. A depth other than 10 bits or + a channel count other than 1/3 returns `CodecError::Unsupported`; truncation errors rather than + panicking. Writing follows the Cineon 4.5 layout: a 1024-byte generic header (version `V4.5`, + one or three channels designated as universal-metric gray or R, G, B, 10 bits, data 0–1023 over + densities 0–2.048) and a 1024-byte film header left undefined, then the samples three to a + big-endian 32-bit word, left justified (packing 5), lines on a word boundary. Samples are written + as `round(v × 1023 / 65535)`, so a frame opened from Cineon keeps its code values; any other + image takes the encode plan (alpha dropped, CMYK and Lab to RGB) and is quantized to 10 bits. + +* **DICOM (read-only).** Single-frame medical images over the uncompressed transfer syntaxes + (implicit VR little-endian, explicit VR little/big-endian): MONOCHROME1/2 gray (with the + 1 = white inversion) and RGB/YBR_FULL, 8- and 16-bit allocated samples, interleaved or planar, + scaled from `BitsStored`/`HighBit` and mapped up when `PixelRepresentation` is signed. + Undefined-length sequences outside the pixel data are skipped, as clinical files contain them. + Compressed transfer syntaxes, sequences over the pixel data and other photometric + interpretations return `Unsupported`/`Malformed`. There is no DICOM writer + (`ASYMMETRIC_EXCEPTIONS`). + +* **Amiga IFF.** `FORM ... ILBM` stores interleaved 1–8 bitplanes; + `FORM ... PBM ` stores 8-bit chunky palette indices with even-padded rows. Both support + stored and ByteRun1 compression, a `CMAP` palette (grey ramp when absent, black for missing + entries), and transparent-color masking. ILBM also supports a separate mask plane and + 24-bitplane RGB ("deep" ILBM: 8 planes each of red, green and blue, least significant bit + first, no palette). HAM/EHB viewport modes, PBM mask planes, a transparent color in a + 24-bitplane file, other bit depths and compressions return `CodecError::Unsupported`. + Writing produces a 24-bitplane ILBM, ByteRun1 compressed one plane row at a time, at most + 65535 × 65535 pixels. An image with alpha gets a mask plane (masking 1): alpha of 128 or more + is opaque, and `AlphaBinarized` is raised when any pixel is not fully opaque. Gray is + expanded to RGB, and 16-bit and float images are reduced to 8 bits. + +* **Camera raw files** (DNG, CR2, NEF, ARW… which are TIFF-structured) are recognised and refused + with `CodecError::Unsupported`: they are sensor data, not flat images. `photocraft-raw` decodes + and develops them, and `photocraft-io` routes them there. + +* **HEIF/HEIC (read-only, feature `heif`).** Decoding lives in the optional `craft-heif` crate + behind the non-default `heif` feature (official PhotoCraft builds enable it; distributors choose). + Without it, HEIF is still detected and decoding returns `CodecError::Unsupported` ("HEIC/HEIF + support isn't included in this build"). iPhone and Mac photos decode with `heic-rs` (pure Rust, no `unsafe`): + single pictures and grid-tiled photos, 8 to 12 bits, auxiliary alpha, ICC, EXIF and XMP. The + container's `irot`/`imir`/`clap` are applied and EXIF Orientation is rewritten to 1 (HEIF's EXIF + tag only repeats the container's, and applying both would turn the photo twice). Grayscale + decodes to RGB. Image sequences, overlays and identity derivations, multilayer HEVC and some + 4:2:2/4:4:4 streams return `CodecError::Unsupported` or `Malformed`, never wrong pixels. Writing + needs an HEVC encoder and every mature one is C, so HEIF is listed in `ASYMMETRIC_EXCEPTIONS`. +* **AVIF.** Encoding uses `ravif`, which is pure Rust. Decoding + needs `dav1d`, which is C. AVIF is therefore read-unsupported, and write support is gated + behind the non-default `avif` feature. In a default build it is neither readable nor writable, + so the symmetric guarantee holds. It is listed in `ASYMMETRIC_EXCEPTIONS`. +* **Lossy WebP.** Lossless (VP8L, via `image-webp`) is the default. `webp_lossless: false` + writes a lossy file with our own clean-room VP8 key-frame encoder (`codecs::vp8`, from + RFC 6386; `webp_quality` 0–100 on libwebp's scale sets the quantizer). Alpha travels losslessly + in an `ALPH` chunk and ICC/EXIF/XMP in their own chunks of the extended (`VP8X`) container. The + encoder makes no rate-distortion decisions yet, and a side longer than 16383 px (the VP8 frame + header's limit) is refused with `CodecError::Encode`. We read both lossy and lossless files. +* **Animation and multi-page files** (APNG, animated GIF/WebP, multi-page TIFF): only the first + frame or page is decoded, and a single frame is written. `FormatCaps::animation` marks + containers that can hold more frames. The decoded image then carries a + `DecodeWarning::MoreFrames` / `MorePages` in `Image::warnings`, with the total when the file + states it (frames and TIFF directories are counted without decoding them; reduced-resolution + TIFF directories and transparency masks are not pages). Any TIFF page can still be opened + with `decode_tiff_page` (and `photocraft_io::import_tiff_page`). Every frame of a GIF comes + from `decode_gif_frames`: each frame composited onto the logical screen with the disposal + methods applied, as RGBA8 with its delay in milliseconds, within `Limits` and + `MAX_GIF_FRAMES`. Every frame of an animated WebP comes from `decode_webp_frames` the same + way, composited onto its canvas, with its duration in milliseconds and the file's ICC profile. + The video-layer commands use both (`photocraft_io::import_gif_frames`, + `photocraft_io::import_webp_frames`). +* **EXIF in TIFF** is stored as a sub-IFD rather than a blob, so it is not preserved yet. + `caps.exif = false` for TIFF, and a warning is raised. +* **Orientation** (EXIF tag 274 in JPEG, PNG `eXIf` and WebP; the decoded page's tag in TIFF and + BigTIFF) is applied on + decode, like Photoshop: the pixels come back upright and the EXIF/XMP orientation is rewritten + to 1 (`DecodeOptions::keep_orientation` opts out). Encoders always write Orientation = 1 + (`upright_exif`, `upright_xmp`, which change only the tag's value, kept in its own type), so upright pixels + are never rotated twice. Parsing is strict: an IFD0 inside the 8-byte header or cut off before + its next-IFD pointer, an Orientation that is not exactly one SHORT or LONG, or an out-of-range value + reads as 1; with duplicate entries the first wins. `Limits` are checked again on the upright + size (5–8 swap width and height), and the rotated buffer is allocated fallibly. + `Image::oriented` turns any layout and depth in parallel bands (about 10 ms for 24 MP RGB8 in + release). +* **Resolution** (#1691, module `resolution`): a file without a resolution of its own format + (no PNG `pHYs`, a WebP, HEIF, …) takes it from its XMP `tiff:XResolution` / `YResolution` / + `ResolutionUnit`, else from EXIF IFD0 (RATIONAL X/YResolution, unit 2 inch — the default when + absent — or 3 cm; unit 1 or anything malformed counts as no resolution). A JPEG uses, like + Photoshop, the first usable of: APP13 Photoshop ResolutionInfo (0x03ED), XMP, EXIF, and only + then the JFIF density (cameras write EXIF only, many tools write a default JFIF 72). On export + the EXIF and XMP resolution are rewritten to the image's (`export_exif`, `export_xmp`): the + RATIONAL values in place, ResolutionUnit set to 2, an entry that can't be rewritten removed + from IFD0 without moving any other data. IFD1 (the thumbnail's) is left as it is. +* **JPEG** + * 8-bit only. Neither decoder backend supports 12-bit. + * A file cut off inside its image data decodes leniently (a baseline JPEG's missing rows come + out grey) but never silently: the image carries `DecodeWarning::Truncated`. A file that ends + before any scan data is an error. + * CMYK is always written 4:4:4 (subsampled CMYK is not portable) as Adobe-inverted CMYK with an + APP14 marker. + * Text is not written because there is no COM-segment support. + * EXIF and XMP must each fit in one APP1 segment (65 533 bytes with its header). Metadata that + doesn't fit is dropped and the export still succeeds, with a + `MetadataTooLarge` warning ("EXIF (… bytes) is too large for the format; it will be dropped"). + Before dropping XMP, the encoder first removes the layered-document properties + (`DocumentAncestors`, `TextLayers`) that mean nothing in a flat JPEG. Extended XMP is not + written. +* **TIFF decode** + * Classic TIFF and BigTIFF (version 43, 8-byte offsets), both byte orders, strips or tiles, + planar configuration 1 or 2. + * Supports 1, 2 and 4-bit gray, 8/16/32/64-bit integers (32 and 64-bit are reduced to U16), + F16, F32 and F64 (converted to F32), WhiteIsZero (#691: the tones are no longer inverted), + palette images (opened as RGB) and associated alpha (made straight). + * Extra samples: the first alpha sample (ExtraSamples 1 or 2, wherever it sits) is the image's + alpha. Every other extra sample, such as a spot colour or a further alpha channel + (ExtraSamples 0), comes back in `Metadata::extra_samples`: one plane per sample in the + image's sample type, with its ExtraSamples value as `kind` (a reserved value above 2 reads as + 0). At most `MAX_EXTRA_SAMPLES` (16) are kept, and they count against `Limits::max_alloc` with + the image: when the image fits but its extra samples don't, it opens without them and carries + `DecodeWarning::ExtraSamplesDropped`. + * Every directory is reachable: `tiff_info` lists the main IFD chain and its SubIFDs (tag 330) + with each page's size, kind (page, reduced-resolution copy, mask) and storage, walked with a + seen-set, a depth limit and a 10 000-directory cap; `decode_tiff_page` opens any of them. + `decode` opens the first full-resolution page, like Photoshop (thumbnails are skipped). + * Banded decoding: the image is allocated once at its final size and each strip or row of + tiles is decoded straight into it, in parallel on native targets, with at most one + strip/tile-sized scratch buffer per worker (30 MP RGB8: Deflate 345 → 32 ms, LZW 428 → 43 + ms in release). Image data cut off by the end of the file decodes with + `DecodeWarning::Truncated`; a file far too small for its declared size (more than any + compression could expand) is refused before allocating. + * YCbCr with three 8- or 16-bit samples decodes to RGB. YCbCr with other sample counts or + bit depths returns `Unsupported`. JPEG-compressed YCbCr uses the full-range JFIF conversion. + * 8/16-bit CIELab and ICCLab decode to the Lab channel layout. + * Signed integer samples and unsupported compression methods return `Unsupported`. +* **TIFF encode** writes classic TIFF, or BigTIFF when `EncodeOptions::tiff_bigtiff` is set or + the file could pass 4 GiB. `Metadata::extra_samples` (up to 16) are written after the colour + and alpha samples, with their `kind` in the ExtraSamples tag (a reserved `kind` above 2 is + written as 0). Each plane must be exactly + `width × height × sample.bytes()` bytes; a plane of any other length, and more than 16 planes, + are encode errors rather than silently truncated or reinterpreted. +* **EXR** + * Multi-part files (a Maya/Arnold render writes one part per AOV) open the part that + looks most like a colour image — RGBA beats RGB beats YA beats a single channel, so a + depth-only part no longer fails the file — with `DecodeWarning::MoreParts` naming how + many parts exist. `exr_info` lists every part (name, view, size, channels, deep/tiled) + without decoding pixels, and `decode_exr_part` decodes a chosen one. Deep parts stay + with the deep decoder: a file that has any is refused here with a pointer to it. + * Reads at full resolution, from the data window. + * Channel names are matched by suffix, so `layer.R` counts as `R`. + * Subsampled channels are unsupported. + * Cryptomatte layers (specification 1.2, as written by Arnold, V-Ray, Redshift, Mantra/Karma + and Cycles) are listed and decoded structurally: `cryptomatte_layers` reads the header + attributes, channels and embedded manifest without pixels, `decode_cryptomatte` returns the + per-pixel (ID, coverage) pairs, and `cryptomatte_id`/`cryptomatte_key`/`cryptomatte_preview_color` + hash names to IDs the way the reference implementation does (official test vectors in the + test suite). + * Deep data (scanlines or single-level tiles, compressed NONE, RLE or ZIPS) opens as a flat + image: each pixel's samples are composited into one, with + `DecodeWarning::DeepFlattened` because per-pixel depth is not kept. Deep ZIP (16-line + blocks) and deep mip-map or rip-map tiles return `Unsupported`. + * Only lossless compressions are offered for writing: None, RLE, ZIP1, ZIP16 and PIZ. +* **Netpbm writing** picks the subtype from the image: float gives PFM, gray gives P5, RGB gives + P6, and anything with alpha or CMYK gives P7 (PAM). Writing a `.pbm` file therefore produces + whichever of these fits the data. +* **Colour conversions** (CMYK↔RGB, RGB→gray) are naive. Colour-managed conversion belongs in + `photocraft-color`. + +## Limits (decompression bombs) + +`DecodeOptions { limits: Limits { max_width, max_height, max_pixels, max_alloc }, .. }` guards +decoding. Header dimensions are checked before the pixel buffer is allocated, and the budget is +also passed to the underlying decoders. A violation returns `CodecError::LimitExceeded`. The +defaults are 262144 px per side, 2^30 pixels and 8 GiB (2 GiB on 32-bit targets such as wasm). +PNG also bounds the aggregate decoded text and XMP (UTF-8 keywords and values) by `max_alloc`, +including chunks after IDAT. This budget is separate from pixels, not a total process-memory cap. + +## Tests + +`cargo test -p craft-image-codecs` covers: + +* the full round-trip matrix (every writable format × 4 sample types × 6 layouts), exact for + lossless formats and above a PSNR threshold for lossy ones; +* metadata preservation per format, checked against `caps`; +* magic-number detection, including negatives; +* fidelity warnings, and a cross-check that the warnings match the actual round-trip result; +* the symmetric-capability guarantee; +* malformed input: truncation, bit flips and proptest random bytes; +* Adam7 PNG against progressive PNG and against the `image` crate as an oracle; +* limit enforcement. + +If `corpus/pngsuite/*.png` exists at the repo root, every file in it is compared against the +`image` crate's decoder. Without it, that test is skipped. diff --git a/crates/image-codecs/fuzz/.gitignore b/crates/image-codecs/fuzz/.gitignore new file mode 100644 index 000000000..ab0eaa1a4 --- /dev/null +++ b/crates/image-codecs/fuzz/.gitignore @@ -0,0 +1,5 @@ +target/ +corpus/ +artifacts/ +coverage/ +Cargo.lock diff --git a/crates/image-codecs/fuzz/Cargo.toml b/crates/image-codecs/fuzz/Cargo.toml new file mode 100644 index 000000000..3257a3953 --- /dev/null +++ b/crates/image-codecs/fuzz/Cargo.toml @@ -0,0 +1,78 @@ +[package] +name = "craft-image-codecs-fuzz" +version = "0.0.0" +publish = false +edition = "2024" +license = "MIT OR Apache-2.0" + +[package.metadata] +cargo-fuzz = true + +# Standalone workspace so the main workspace's `crates/*` glob and lints do +# not apply (cargo-fuzz needs nightly + sanitizer flags). +[workspace] + +[dependencies] +libfuzzer-sys = "0.4" +craft-image-codecs = { path = "..", features = ["heif"] } +# Called directly too: the codec turns its panics into errors (see decode_heif.rs). +heic-rs = "=0.1.4" + +[profile.release] +debug = 1 + +[[bin]] +name = "decode_any" +path = "fuzz_targets/decode_any.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_png" +path = "fuzz_targets/decode_png.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_jpeg" +path = "fuzz_targets/decode_jpeg.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_tiff" +path = "fuzz_targets/decode_tiff.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_exr" +path = "fuzz_targets/decode_exr.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_heif" +path = "fuzz_targets/decode_heif.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_gif" +path = "fuzz_targets/decode_gif.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_webp" +path = "fuzz_targets/decode_webp.rs" +test = false +doc = false +bench = false diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_any.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_any.rs new file mode 100644 index 000000000..aa261efaf --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_any.rs @@ -0,0 +1,18 @@ +#![no_main] +//! Detect + decode arbitrary bytes, then re-encode what decoded (symmetry +//! path) — neither may panic. + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_with, encode, DecodeOptions, EncodeOptions, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + if let Ok(img) = decode_with(data, &opts) + && let Some(f) = craft_image_codecs::detect(data) + { + let _ = encode(&img, f, &EncodeOptions::default()); + } +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs new file mode 100644 index 000000000..1e6abbf81 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs @@ -0,0 +1,12 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::OpenExr, data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs new file mode 100644 index 000000000..4e0c556c7 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs @@ -0,0 +1,13 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, decode_gif_frames, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Gif, data, &opts); + let _ = decode_gif_frames(data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs new file mode 100644 index 000000000..447c52a0d --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs @@ -0,0 +1,15 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +// heic-rs is young and parses untrusted HEIF/HEVC: run with `-timeout=10` so a hang is reported, +// not just a panic. The codec turns a heic-rs panic into an error, so heic-rs is also called +// directly, where its panics still reach the fuzzer. Both orientation paths are covered. +fuzz_target!(|data: &[u8]| { + let keep_orientation = data.first().is_some_and(|b| b & 1 == 1); + let raw = heic_rs::DecodeOptions::default().with_max_pixels(Some(1 << 22)).with_transforms(!keep_orientation).with_alpha(true); + let _ = heic_rs::decode(data, &raw); + let limits = Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }; + let _ = decode_as_with(Format::Heif, data, &DecodeOptions { limits, keep_orientation }); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs new file mode 100644 index 000000000..96616edc8 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs @@ -0,0 +1,12 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Jpeg, data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_png.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_png.rs new file mode 100644 index 000000000..54b23f399 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_png.rs @@ -0,0 +1,12 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Png, data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs new file mode 100644 index 000000000..277dac778 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs @@ -0,0 +1,20 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, decode_tiff_page, exif_orientation, tiff_info, tiff_orientation, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Tiff, data, &opts); + let _ = exif_orientation(data); + let _ = tiff_orientation(data); + // Every directory of the chain and its SubIFDs (BigTIFF too), not only the default page. + if let Ok(info) = tiff_info(data) { + for page in 0..info.pages.len().min(16) { + let _ = decode_tiff_page(data, page, &opts); + } + } +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs new file mode 100644 index 000000000..139777a81 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs @@ -0,0 +1,13 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, decode_webp_frames, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::WebP, data, &opts); + let _ = decode_webp_frames(data, &opts); +}); diff --git a/crates/image-codecs/src/cicp.rs b/crates/image-codecs/src/cicp.rs new file mode 100644 index 000000000..fc148837c --- /dev/null +++ b/crates/image-codecs/src/cicp.rs @@ -0,0 +1,182 @@ +//! ITU-T H.273 colour descriptions (CICP: coding-independent code points), as HEIF `colr` +//! `nclx` boxes and video streams declare them, and the HDR transfer functions of ITU-R BT.2100. +//! +//! PQ and HLG can't be written as an ICC tone curve, so an image using them is decoded here to +//! linear light (32-bit float) instead: diffuse white (BT.2408's 203 cd/m²) becomes 1.0 and the +//! highlights above it are kept as values over 1.0, as Photoshop holds HDR in a 32-bit document. +//! The caller tags the result with a linear profile on the declared primaries. + +use crate::error::CodecError; +use crate::image::{Image, SampleType}; + +/// A colour description: the four H.273 code points as written in the file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Cicp { + /// Colour primaries: 1 BT.709, 9 BT.2020, 12 P3-D65… + pub primaries: u16, + /// Transfer characteristics: 13 sRGB, 1/6/14/15 BT.709, 8 linear, 16 PQ, 18 HLG… + pub transfer: u16, + /// Matrix coefficients. + pub matrix: u16, + /// Full-range samples rather than studio swing. + pub full_range: bool, +} + +impl Cicp { + /// SMPTE ST 2084 / BT.2100 perceptual quantizer. + pub const TRANSFER_PQ: u16 = 16; + /// ARIB STD-B67 / BT.2100 hybrid log-gamma. + pub const TRANSFER_HLG: u16 = 18; + + /// PQ or HLG: samples that [`linearize`] decodes to linear light. + pub fn is_hdr(self) -> bool { + matches!(self.transfer, Cicp::TRANSFER_PQ | Cicp::TRANSFER_HLG) + } +} + +/// The luminance that becomes 1.0: BT.2408's HDR reference (diffuse) white. +pub const REFERENCE_WHITE_NITS: f32 = 203.0; +/// The nominal display peak HLG is rendered for (BT.2100's reference display). +pub const HLG_PEAK_NITS: f32 = 1000.0; +/// PQ's absolute peak. +pub const PQ_PEAK_NITS: f32 = 10_000.0; + +/// BT.2100 HLG inverse OETF: non-linear signal `E'` (0..1) to normalised scene light `E` (0..1). +pub fn hlg_inverse_oetf(e: f32) -> f32 { + const A: f32 = 0.178_832_77; + const B: f32 = 1.0 - 4.0 * A; + const C: f32 = 0.559_910_7; + let e = if e.is_finite() { e.clamp(0.0, 1.0) } else { 0.0 }; + if e <= 0.5 { e * e / 3.0 } else { (((e - C) / A).exp() + B) / 12.0 } +} + +/// BT.2100 HLG signal to display light relative to [`REFERENCE_WHITE_NITS`]: the inverse OETF, +/// then the OOTF for a [`HLG_PEAK_NITS`] display (system gamma 1.2, zero black level), whose +/// luminance uses the weights of the `luma` primaries. +pub fn hlg_to_linear(rgb: [f32; 3], luma: [f32; 3]) -> [f32; 3] { + let s = rgb.map(hlg_inverse_oetf); + let ys = luma[0] * s[0] + luma[1] * s[1] + luma[2] * s[2]; + // γ = 1.2 + 0.42·log10(Lw / 1000) is exactly 1.2 at 1000 cd/m². + let gain = if ys > 0.0 { HLG_PEAK_NITS * ys.powf(0.2) / REFERENCE_WHITE_NITS } else { 0.0 }; + s.map(|v| v * gain) +} + +/// SMPTE ST 2084 (PQ) signal `E'` (0..1) to display light relative to [`REFERENCE_WHITE_NITS`]. +pub fn pq_to_linear(e: f32) -> f32 { + const M1: f32 = 2610.0 / 16384.0; + const M2: f32 = 2523.0 / 4096.0 * 128.0; + const C1: f32 = 3424.0 / 4096.0; + const C2: f32 = 2413.0 / 4096.0 * 32.0; + const C3: f32 = 2392.0 / 4096.0 * 32.0; + let e = if e.is_finite() { e.clamp(0.0, 1.0) } else { 0.0 }; + let p = e.powf(1.0 / M2); + let y = ((p - C1).max(0.0) / (C2 - C3 * p)).powf(1.0 / M1); + y * PQ_PEAK_NITS / REFERENCE_WHITE_NITS +} + +/// Relative luminance weights of the RGB primaries `code` (BT.2020's for anything else, the +/// space BT.2100 defines HLG in). +fn luma_weights(code: u16) -> [f32; 3] { + match code { + 1 => [0.2126, 0.7152, 0.0722], + 12 => [0.2290, 0.6917, 0.0793], + _ => [0.2627, 0.6780, 0.0593], + } +} + +/// `img` (RGB or RGBA, decoded with `cicp`'s PQ or HLG transfer) as linear light in 32-bit float, +/// alpha unchanged. Its ICC profile is dropped: whatever it said described the encoded samples, +/// not linear light. Anything else comes back unchanged. +pub fn linearize(img: Image, cicp: Cicp) -> Result { + let layout = img.layout(); + if !cicp.is_hdr() || !layout.is_rgb() { + return Ok(img); + } + let (w, h) = img.dimensions(); + let ch = layout.channels(); + let mut v = img.to_normalized(); + let luma = luma_weights(cicp.primaries); + for px in v.chunks_exact_mut(ch) { + let [r, g, b, ..] = *px else { continue }; + let out = if cicp.transfer == Cicp::TRANSFER_HLG { hlg_to_linear([r, g, b], luma) } else { [r, g, b].map(pq_to_linear) }; + px[..3].copy_from_slice(&out); + } + let mut out = Image::from_raw(w, h, layout, SampleType::F32, v.iter().flat_map(|x| x.to_ne_bytes()).collect())?; + out.meta = img.meta; + out.warnings = img.warnings; + out.cicp = Some(cicp); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::image::ChannelLayout; + + const BT2020: [f32; 3] = [0.2627, 0.6780, 0.0593]; + + fn close(a: f32, b: f32, tol: f32) -> bool { + (a - b).abs() <= tol + } + + #[test] + fn hlg_inverse_oetf_matches_bt2100() { + assert_eq!(hlg_inverse_oetf(0.0), 0.0); + // The two segments meet at E' = 0.5, E = 1/12. + assert!(close(hlg_inverse_oetf(0.5), 1.0 / 12.0, 1e-6)); + assert!(close(hlg_inverse_oetf(0.500_001), 1.0 / 12.0, 1e-5)); + assert!(close(hlg_inverse_oetf(1.0), 1.0, 1e-4)); + // BT.2408's HLG reference white, 75 % signal, is E ≈ 0.265. + assert!(close(hlg_inverse_oetf(0.75), 0.2650, 1e-3)); + for bad in [f32::NAN, f32::INFINITY, -1.0] { + assert!(hlg_inverse_oetf(bad).is_finite()); + } + } + + #[test] + fn hlg_reference_white_is_one_and_highlights_are_kept() { + // BT.2408: a 75 % HLG grey on a 1000 cd/m² display is 203 cd/m², our 1.0. + let white = hlg_to_linear([0.75; 3], BT2020); + assert!(white.iter().all(|&v| close(v, 1.0, 0.005)), "{white:?}"); + // Peak signal is the display peak: 1000 / 203. + let peak = hlg_to_linear([1.0; 3], BT2020); + assert!(peak.iter().all(|&v| close(v, 1000.0 / 203.0, 0.01)), "{peak:?}"); + // 50 % grey: 1000 · (1/12)^1.2 ≈ 50.7 cd/m². + let mid = hlg_to_linear([0.5; 3], BT2020); + assert!(mid.iter().all(|&v| close(v * 203.0, 50.71, 0.1)), "{mid:?}"); + assert_eq!(hlg_to_linear([0.0; 3], BT2020), [0.0; 3]); + // A saturated colour keeps its hue: only red lit stays only red. + let red = hlg_to_linear([0.75, 0.0, 0.0], BT2020); + assert!(red[0] > 0.0 && red[1] == 0.0 && red[2] == 0.0, "{red:?}"); + } + + #[test] + fn pq_matches_st2084() { + assert_eq!(pq_to_linear(0.0), 0.0); + assert!(close(pq_to_linear(1.0), 10_000.0 / 203.0, 0.05)); + // 203 cd/m² is PQ signal ≈ 0.5807 (BT.2408). + assert!(close(pq_to_linear(0.5807), 1.0, 0.005), "{}", pq_to_linear(0.5807)); + assert!(pq_to_linear(f32::NAN).is_finite()); + } + + #[test] + fn linearize_decodes_hlg_to_float_and_keeps_alpha() { + let hlg = Cicp { primaries: 9, transfer: Cicp::TRANSFER_HLG, matrix: 9, full_range: true }; + let three_quarters = (0.75f32 * 65535.0).round() as u16; + let img = Image::from_u16(2, 1, ChannelLayout::Rgba, &[three_quarters, three_quarters, three_quarters, 65535, 0, 0, 0, 32768]) + .unwrap() + .with_icc(Some(b"encoded-profile".to_vec())); + let out = linearize(img, hlg).unwrap(); + assert_eq!((out.sample_type(), out.layout(), out.icc.as_deref(), out.cicp), (SampleType::F32, ChannelLayout::Rgba, None, Some(hlg))); + let v = out.to_normalized(); + assert!(v[..3].iter().all(|&x| close(x, 1.0, 0.005)), "{v:?}"); + assert!(close(v[3], 1.0, 1e-6) && v[4..7] == [0.0; 3] && close(v[7], 0.5, 1e-3), "{v:?}"); + } + + #[test] + fn linearize_leaves_sdr_untouched() { + let srgb = Cicp { primaries: 1, transfer: 13, matrix: 6, full_range: false }; + let img = Image::from_u16(1, 1, ChannelLayout::Rgb, &[1, 2, 3]).unwrap(); + assert_eq!(linearize(img.clone(), srgb).unwrap(), img); + } +} diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs new file mode 100644 index 000000000..4c4c22388 --- /dev/null +++ b/crates/image-codecs/src/codecs.rs @@ -0,0 +1,25 @@ +pub(crate) mod dds; +pub(crate) mod dds_bc; +pub(crate) mod deep_exr; +pub(crate) mod dicom; +pub(crate) mod dpx; +pub(crate) mod exr; +pub(crate) mod exr_cryptomatte; +pub(crate) mod fits; +pub(crate) mod gif_frames; +pub(crate) mod gif_palette; +pub(crate) mod heif; +pub(crate) mod iff; +pub(crate) mod jpeg; +pub(crate) mod jxl; +pub(crate) mod pcx; +pub(crate) mod png; +pub(crate) mod pnm; +pub(crate) mod tga; +pub(crate) mod tiff; +pub(crate) mod tiff_exif; +pub(crate) mod tiff_ifd; +pub(crate) mod via_image; +pub(crate) mod vp8; +pub(crate) mod webp; +pub(crate) mod xpm; diff --git a/crates/image-codecs/src/codecs/dds.rs b/crates/image-codecs/src/codecs/dds.rs new file mode 100644 index 000000000..1d220e6ac --- /dev/null +++ b/crates/image-codecs/src/codecs/dds.rs @@ -0,0 +1,556 @@ +//! DirectDraw Surface (DDS) textures, after Microsoft's public DDS documentation ("DDS", +//! "DDS_HEADER", "DDS_PIXELFORMAT", "DDS_HEADER_DXT10"). +//! +//! Reading: the top mip level of a 2D texture, either uncompressed (8 to 32 bits per pixel +//! described by channel masks, RGB or luminance) or block-compressed BC1/BC2/BC3 +//! (DXT1/DXT3/DXT5), with or without the DX10 header. Cubemaps, texture arrays and volume +//! textures are refused rather than flattened to one face or slice. +//! +//! Writing: a 2D texture, uncompressed (BGRA or BGRX, luminance with or without alpha) or +//! BC1/BC2/BC3, optionally with its full mip chain. The legacy header describes all of these, +//! so no DX10 header is written; formats only DXGI can name (BC4–BC7, sRGB) aren't written. + +use std::borrow::Cow; + +use super::dds_bc::{self, Block, Mask}; +use crate::SampleType; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image}; +use crate::options::{DdsCompression, EncodeOptions, Limits}; + +/// `DDS ` plus the 124-byte DDS_HEADER. +const HEADER: usize = 128; +/// DDS_HEADER_DXT10, present when the FourCC is `DX10`. +const DX10_HEADER: usize = 20; + +const DDSD_DEPTH: u32 = 0x80_0000; +const DDPF_ALPHAPIXELS: u32 = 0x1; +const DDPF_FOURCC: u32 = 0x4; +const DDPF_RGB: u32 = 0x40; +const DDPF_LUMINANCE: u32 = 0x2_0000; +const DDSCAPS2_CUBEMAP: u32 = 0x200; +const DDSCAPS2_CUBEMAP_FACES: u32 = 0xfc00; +const DDSCAPS2_VOLUME: u32 = 0x20_0000; +const DIMENSION_TEXTURE2D: u32 = 3; +const MISC_TEXTURECUBE: u32 = 0x4; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::Dds, reason) +} +fn unsupported(reason: impl Into) -> CodecError { + CodecError::unsupported(Format::Dds, reason) +} +fn le32(b: &[u8], at: usize) -> Result { + let bytes = b.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).ok_or_else(|| bad("truncated header"))?; + Ok(u32::from_le_bytes(bytes)) +} + +/// Header-declared extra subresources cannot survive a single-image rewrite, even without presence flags. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + let depth = le32(bytes, 24)?; + let mips = le32(bytes, 28)?; + let faces = (le32(bytes, 112)? & DDSCAPS2_CUBEMAP_FACES).count_ones(); + let dx10 = le32(bytes, 80)? & DDPF_FOURCC != 0 && bytes.get(84..88) == Some(b"DX10"); + let (cube, array) = if dx10 { (le32(bytes, 136)? & MISC_TEXTURECUBE != 0, le32(bytes, 140)?) } else { (false, 0) }; + Ok(depth > 1 || mips > 1 || faces > 1 || cube || array > 1) +} + +/// A FourCC as text when it is printable, else as the number legacy writers store there. +fn fourcc_name(fourcc: u32) -> String { + let bytes = fourcc.to_le_bytes(); + if bytes.iter().all(|c| c.is_ascii_graphic() || *c == b' ') { bytes.iter().map(|&c| char::from(c)).collect() } else { fourcc.to_string() } +} + +/// One channel of an uncompressed pixel: where it sits and its largest value. +#[derive(Clone, Copy)] +struct Field { + shift: u32, + max: u32, +} + +impl Field { + /// `None` for an empty mask; an error for one with gaps or wider than the pixel. + fn new(mask: u32, bits: u32) -> Result, CodecError> { + if mask == 0 { + return Ok(None); + } + let shift = mask.trailing_zeros(); + let max = mask >> shift; + if max & max.wrapping_add(1) != 0 || (bits < 32 && mask >> bits != 0) { + return Err(bad(format!("channel mask {mask:#x} is not a contiguous field of a {bits}-bit pixel"))); + } + Ok(Some(Field { shift, max })) + } + + fn get(self, pixel: u32) -> u8 { + let v = u64::from((pixel >> self.shift) & self.max); + let max = u64::from(self.max); + ((v * 255 + max / 2) / max) as u8 + } +} + +enum Encoding { + /// Uncompressed: bytes per pixel, then red (or luminance), green, blue and alpha. + Masks { + bytes: usize, + gray: bool, + fields: [Option; 4], + }, + Bc1, + Bc2, + Bc3, +} + +impl Encoding { + fn masks(bits: u32, masks: [u32; 4], gray: bool) -> Result { + if !matches!(bits, 8 | 16 | 24 | 32) { + return Err(bad(format!("{bits} bits per pixel is not 8, 16, 24 or 32"))); + } + let mut fields = [None; 4]; + for (field, mask) in fields.iter_mut().zip(masks) { + *field = Field::new(mask, bits)?; + } + let colour = if gray { fields[0].is_some() } else { fields[..3].iter().any(Option::is_some) }; + if !colour { + return Err(bad("the pixel format has no colour channel masks")); + } + Ok(Encoding::Masks { bytes: bits as usize / 8, gray, fields }) + } + + fn layout(&self) -> ChannelLayout { + match self { + Encoding::Masks { gray: true, fields, .. } if fields[3].is_some() => ChannelLayout::GrayA, + Encoding::Masks { gray: true, .. } => ChannelLayout::Gray, + Encoding::Masks { fields, .. } if fields[3].is_none() => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + } + } + + /// Stored row stride and byte size of the top mip level (block rows for BC). + fn level_layout(&self, width: u32, height: u32, pitch: Option) -> Result<(usize, usize), CodecError> { + let overflow = || bad("pixel data size overflow"); + let (w, h) = (usize::try_from(width).map_err(|_| overflow())?, usize::try_from(height).map_err(|_| overflow())?); + let (row_bytes, rows) = match self { + Encoding::Masks { bytes, .. } => { + let packed = w.checked_mul(*bytes).ok_or_else(overflow)?; + let row_bytes = match pitch { + Some(pitch) => usize::try_from(pitch).map_err(|_| overflow())?, + None => packed, + }; + if row_bytes < packed { + return Err(bad(format!("row pitch {row_bytes} is smaller than the {packed}-byte pixel row"))); + } + (row_bytes, h) + } + Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { + let block = if matches!(self, Encoding::Bc1) { 8 } else { 16 }; + (w.div_ceil(4).checked_mul(block).ok_or_else(overflow)?, h.div_ceil(4)) + } + }; + let size = row_bytes.checked_mul(rows).ok_or_else(overflow)?; + Ok((row_bytes, size)) + } +} + +/// The legacy pixel format: a FourCC, or channel masks. +fn legacy_encoding(b: &[u8]) -> Result { + let (flags, fourcc, bits) = (le32(b, 80)?, le32(b, 84)?, le32(b, 88)?); + let alpha = if flags & DDPF_ALPHAPIXELS != 0 { le32(b, 104)? } else { 0 }; + if flags & DDPF_FOURCC != 0 { + return match &fourcc.to_le_bytes() { + b"DXT1" => Ok(Encoding::Bc1), + b"DXT3" => Ok(Encoding::Bc2), + b"DXT5" => Ok(Encoding::Bc3), + b"DXT2" | b"DXT4" => Err(unsupported("premultiplied-alpha DXT2/DXT4 textures are not supported")), + _ => Err(unsupported(format!("compression `{}` is not supported (DXT1, DXT3 and DXT5 are)", fourcc_name(fourcc)))), + }; + } + if flags & DDPF_RGB != 0 { + return Encoding::masks(bits, [le32(b, 92)?, le32(b, 96)?, le32(b, 100)?, alpha], false); + } + if flags & DDPF_LUMINANCE != 0 { + return Encoding::masks(bits, [le32(b, 92)?, 0, 0, alpha], true); + } + Err(unsupported(format!("pixel format flags {flags:#x} are not RGB, luminance or a supported compression"))) +} + +/// The DX10 header's DXGI format, refusing anything but a single 2D texture. +fn dx10_encoding(b: &[u8]) -> Result { + let (dxgi, dimension, misc, array) = (le32(b, HEADER)?, le32(b, HEADER + 4)?, le32(b, HEADER + 8)?, le32(b, HEADER + 12)?); + if misc & MISC_TEXTURECUBE != 0 { + return Err(unsupported("cubemaps are not supported")); + } + if dimension != DIMENSION_TEXTURE2D { + return Err(unsupported(format!("only 2D textures are supported (resource dimension {dimension})"))); + } + if array > 1 { + return Err(unsupported(format!("texture arrays are not supported ({array} textures)"))); + } + const RGBA: [u32; 4] = [0xff, 0xff00, 0xff_0000, 0xff00_0000]; + const BGRA: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0xff00_0000]; + const BGRX: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0]; + match dxgi { + 27..=29 => Encoding::masks(32, RGBA, false), + 87 | 90 | 91 => Encoding::masks(32, BGRA, false), + 88 | 92 | 93 => Encoding::masks(32, BGRX, false), + 70..=72 => Ok(Encoding::Bc1), + 73..=75 => Ok(Encoding::Bc2), + 76..=78 => Ok(Encoding::Bc3), + _ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1, BC2, BC3 and 8-bit RGBA or BGRA are)"))), + } +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !b.starts_with(b"DDS ") { + return Err(bad("missing the `DDS ` signature")); + } + if le32(b, 4)? != 124 { + return Err(bad("header size is not 124")); + } + let (flags, height, width, depth, caps2) = (le32(b, 8)?, le32(b, 12)?, le32(b, 16)?, le32(b, 24)?, le32(b, 112)?); + if caps2 & DDSCAPS2_CUBEMAP != 0 { + return Err(unsupported("cubemaps are not supported")); + } + if caps2 & DDSCAPS2_VOLUME != 0 || (flags & DDSD_DEPTH != 0 && depth > 1) { + return Err(unsupported("volume textures are not supported")); + } + let dx10 = le32(b, 80)? & DDPF_FOURCC != 0 && &le32(b, 84)?.to_le_bytes() == b"DX10"; + let (encoding, start) = if dx10 { (dx10_encoding(b)?, HEADER + DX10_HEADER) } else { (legacy_encoding(b)?, HEADER) }; + let layout = encoding.layout(); + limits.check(width, height, layout, SampleType::U8)?; + // The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set. + let pitch = if flags & DDSD_PITCH != 0 { Some(le32(b, 20)?) } else { None }; + let (row_pitch, size) = encoding.level_layout(width, height, pitch)?; + let data = b.get(start..).and_then(|d| d.get(..size)).ok_or_else(|| bad(format!("truncated pixel data: {size} bytes needed")))?; + let (w, h) = (width as usize, height as usize); + let channels = layout.channels(); + let row_size = w.checked_mul(channels).ok_or_else(|| bad("pixel size overflow"))?; + let total = row_size.checked_mul(h).ok_or_else(|| bad("pixel size overflow"))?; + let mut pixels = vec![0u8; total]; + match encoding { + Encoding::Masks { bytes, gray, fields } => { + for (src_row, dst_row) in data.chunks_exact(row_pitch).zip(pixels.chunks_exact_mut(row_size)) { + // Stop at the output width, leaving any stored row padding unread. + for (src, dst) in src_row.chunks_exact(bytes).zip(dst_row.chunks_exact_mut(channels)) { + let pixel = src.iter().rev().fold(0u32, |v, &byte| v << 8 | u32::from(byte)); + let channel = |i: usize| fields[i].map_or(0, |f| f.get(pixel)); + let values = if gray { [channel(0), channel(3), 0, 0] } else { [channel(0), channel(1), channel(2), channel(3)] }; + dst.copy_from_slice(&values[..channels]); + } + } + } + Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { + let block_size = if matches!(encoding, Encoding::Bc1) { 8 } else { 16 }; + let blocks_x = w.div_ceil(4); + for (i, block) in data.chunks_exact(block_size).enumerate() { + let texels = match encoding { + Encoding::Bc1 => bc1(block, true), + Encoding::Bc2 => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc2_alpha(block)), + _ => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc3_alpha(block)), + }; + let (bx, by) = ((i % blocks_x) * 4, (i / blocks_x) * 4); + for (t, texel) in texels.iter().enumerate() { + let (x, y) = (bx + t % 4, by + t / 4); + if x < w && y < h { + let at = (y * w + x) * 4; + if let Some(dst) = pixels.get_mut(at..at + 4) { + dst.copy_from_slice(texel); + } + } + } + } + } + } + Image::from_u8(width, height, layout, pixels) +} + +/// A BC1 colour block. BC2 and BC3 colour blocks always have four colours; only BC1's +/// `c0 <= c1` order selects three colours and transparent black. +fn bc1(block: &[u8], one_bit_alpha: bool) -> [[u8; 4]; 16] { + let byte = |i: usize| block.get(i).copied().unwrap_or(0); + let (c0, c1) = (u16::from_le_bytes([byte(0), byte(1)]), u16::from_le_bytes([byte(2), byte(3)])); + let palette = dds_bc::colour_palette(c0, c1, c0 > c1 || !one_bit_alpha); + let indices = u32::from_le_bytes([byte(4), byte(5), byte(6), byte(7)]); + std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3]) +} + +/// BC2: four explicit bits of alpha per texel. +fn bc2_alpha(block: &[u8]) -> [u8; 16] { + let bits = u64::from_le_bytes(std::array::from_fn(|i| block.get(i).copied().unwrap_or(0))); + std::array::from_fn(|t| ((bits >> (4 * t)) & 0xf) as u8 * 17) +} + +/// BC3: two alpha endpoints and a 3-bit index per texel into eight values, or into six plus +/// 0 and 255 when `a0 <= a1`. +fn bc3_alpha(block: &[u8]) -> [u8; 16] { + let byte = |i: usize| block.get(i).copied().unwrap_or(0); + let palette = dds_bc::alpha_palette(byte(0), byte(1)); + let bits = u64::from_le_bytes([byte(2), byte(3), byte(4), byte(5), byte(6), byte(7), 0, 0]); + std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize]) +} + +fn with_alpha(mut texels: [[u8; 4]; 16], alpha: [u8; 16]) -> [[u8; 4]; 16] { + for (texel, a) in texels.iter_mut().zip(alpha) { + texel[3] = a; + } + texels +} + +const DDSD_CAPS: u32 = 0x1; +const DDSD_HEIGHT: u32 = 0x2; +const DDSD_WIDTH: u32 = 0x4; +const DDSD_PITCH: u32 = 0x8; +const DDSD_PIXELFORMAT: u32 = 0x1000; +const DDSD_MIPMAPCOUNT: u32 = 0x2_0000; +const DDSD_LINEARSIZE: u32 = 0x8_0000; +const DDSCAPS_COMPLEX: u32 = 0x8; +const DDSCAPS_TEXTURE: u32 = 0x1000; +const DDSCAPS_MIPMAP: u32 = 0x40_0000; + +fn encode_err(reason: impl std::fmt::Display) -> CodecError { + CodecError::encode(Format::Dds, reason) +} + +/// How a written texture stores its pixels. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Store { + /// 8-bit luminance. + Luminance, + /// 8-bit luminance, then 8-bit alpha. + LuminanceAlpha, + /// Blue, green, red and an unused byte (written as 255, so a reader that takes it for + /// alpha still sees an opaque texture). + Bgrx, + Bgra, + Bc1, + Bc2, + Bc3, +} + +impl Store { + /// The storage for an image of `layout`, as [`crate::fidelity::plan`] chose it. + fn new(compression: DdsCompression, layout: ChannelLayout) -> Result { + Ok(match (compression, layout) { + (DdsCompression::None, ChannelLayout::Gray) => Store::Luminance, + (DdsCompression::None, ChannelLayout::GrayA) => Store::LuminanceAlpha, + (DdsCompression::None, ChannelLayout::Rgb) => Store::Bgrx, + (DdsCompression::None, ChannelLayout::Rgba) => Store::Bgra, + (DdsCompression::Bc1, ChannelLayout::Rgba) => Store::Bc1, + (DdsCompression::Bc2, ChannelLayout::Rgba) => Store::Bc2, + (DdsCompression::Bc3, ChannelLayout::Rgba) => Store::Bc3, + (_, layout) => return Err(encode_err(format!("{layout:?} pixels can't be written as `{}`", compression.name()))), + }) + } + + /// Bytes per pixel, or per 4×4 block for a block compression. + fn unit(self) -> usize { + match self { + Store::Luminance => 1, + Store::LuminanceAlpha => 2, + Store::Bgrx | Store::Bgra => 4, + Store::Bc1 => 8, + Store::Bc2 | Store::Bc3 => 16, + } + } + + fn is_block(self) -> bool { + matches!(self, Store::Bc1 | Store::Bc2 | Store::Bc3) + } + + /// Bytes of one row of pixels (or of 4×4 blocks) of a level `width` wide. + fn row_bytes(self, width: usize) -> Option { + if self.is_block() { width.div_ceil(4).checked_mul(self.unit()) } else { width.checked_mul(self.unit()) } + } + + fn level_bytes(self, width: usize, height: usize) -> Option { + let rows = if self.is_block() { height.div_ceil(4) } else { height }; + self.row_bytes(width)?.checked_mul(rows) + } + + /// DDS_PIXELFORMAT: flags, FourCC, bits per pixel and the red, green, blue and alpha masks. + fn pixel_format(self) -> (u32, [u8; 4], u32, [u32; 4]) { + match self { + Store::Luminance => (DDPF_LUMINANCE, [0; 4], 8, [0xff, 0, 0, 0]), + Store::LuminanceAlpha => (DDPF_LUMINANCE | DDPF_ALPHAPIXELS, [0; 4], 16, [0xff, 0, 0, 0xff00]), + Store::Bgrx => (DDPF_RGB, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0]), + Store::Bgra => (DDPF_RGB | DDPF_ALPHAPIXELS, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0xff00_0000]), + Store::Bc1 => (DDPF_FOURCC, *b"DXT1", 0, [0; 4]), + Store::Bc2 => (DDPF_FOURCC, *b"DXT3", 0, [0; 4]), + Store::Bc3 => (DDPF_FOURCC, *b"DXT5", 0, [0; 4]), + } + } +} + +/// Levels in a full mip chain: the larger side halves down to 1. +fn mip_levels(width: u32, height: u32) -> u32 { + 32 - width.max(height).max(1).leading_zeros() +} + +fn half((w, h): (usize, usize)) -> (usize, usize) { + ((w / 2).max(1), (h / 2).max(1)) +} + +pub(crate) fn encode(image: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = image.converted(plan.layout, SampleType::U8); + let pixels = img.as_u8().ok_or_else(|| encode_err("the image was not converted to 8 bits per channel"))?; + let store = Store::new(opts.dds.compression, img.layout())?; + let (width, height) = img.dimensions(); + let levels = if opts.dds.mipmaps { mip_levels(width, height) } else { 1 }; + let top = (width as usize, height as usize); + // Every level's size, checked before anything is allocated. + let mut sizes = Vec::new(); + let mut size = top; + for _ in 0..levels { + sizes.push(store.level_bytes(size.0, size.1).ok_or_else(|| encode_err("texture size overflow"))?); + size = half(size); + } + let total = sizes.iter().try_fold(HEADER, |total, &s| total.checked_add(s)).ok_or_else(|| encode_err("texture size overflow"))?; + let header = header(width, height, store, levels, sizes.first().copied().unwrap_or(0))?; + let mut out = crate::options::alloc_zeroed::(total)?; + out.get_mut(..HEADER).ok_or_else(|| encode_err("output buffer too small"))?.copy_from_slice(&header); + + let channels = img.layout().channels(); + let mut level = Cow::Borrowed(pixels); + let (mut size, mut at) = (top, HEADER); + for (i, &bytes) in sizes.iter().enumerate() { + let dst = out.get_mut(at..at + bytes).ok_or_else(|| encode_err("output buffer too small"))?; + write_level(store, &level, size, dst)?; + at += bytes; + if i + 1 < sizes.len() { + let next = half(size); + level = Cow::Owned(downsample(&level, size, next, channels)?); + size = next; + } + } + Ok(out) +} + +/// The `DDS ` signature and DDS_HEADER. `top` is the top level's size in bytes. +fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Result<[u8; HEADER], CodecError> { + let mut b = [0u8; HEADER]; + let mut put = |at: usize, v: u32| { + if let Some(d) = b.get_mut(at..at + 4) { + d.copy_from_slice(&v.to_le_bytes()); + } + }; + // Uncompressed: the pitch of one row. Compressed: the whole top level (its "linear size"). + let (size_flag, pitch_or_size) = if store.is_block() { (DDSD_LINEARSIZE, Some(top)) } else { (DDSD_PITCH, store.row_bytes(width as usize)) }; + let pitch_or_size = + pitch_or_size.and_then(|v| u32::try_from(v).ok()).ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?; + let mipmapped = levels > 1; + put(4, 124); + put(8, DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | size_flag | if mipmapped { DDSD_MIPMAPCOUNT } else { 0 }); + put(12, height); + put(16, width); + put(20, pitch_or_size); + put(28, levels); + let (flags, fourcc, bits, masks) = store.pixel_format(); + put(76, 32); + put(80, flags); + put(84, u32::from_le_bytes(fourcc)); + put(88, bits); + for (i, mask) in masks.into_iter().enumerate() { + put(92 + 4 * i, mask); + } + put(108, DDSCAPS_TEXTURE | if mipmapped { DDSCAPS_COMPLEX | DDSCAPS_MIPMAP } else { 0 }); + b[..4].copy_from_slice(b"DDS "); + Ok(b) +} + +/// Runs `f` on each `len`-byte row of `out` with its index, in parallel on native targets. +fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) { + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(len.max(1)).enumerate().for_each(|(i, row)| f(i, row)); + } + #[cfg(target_arch = "wasm32")] + for (i, row) in out.chunks_mut(len.max(1)).enumerate() { + f(i, row); + } +} + +/// Writes one level of `width` × `height` pixels (in the image's layout) into `dst`. +fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), CodecError> { + let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?; + match store { + Store::Luminance | Store::LuminanceAlpha => { + // The image's gray (and alpha) samples, in the same order. + let src = pixels.get(..dst.len()).ok_or_else(|| encode_err("the level is smaller than its size"))?; + dst.copy_from_slice(src); + } + Store::Bgrx | Store::Bgra => { + let channels = if store == Store::Bgra { 4 } else { 3 }; + rows(dst, row_bytes, |y, row| { + let src = pixels.get(y * width * channels..).unwrap_or_default(); + for (d, s) in row.as_chunks_mut::<4>().0.iter_mut().zip(src.chunks_exact(channels)) { + let byte = |i: usize| s.get(i).copied().unwrap_or(255); + *d = [byte(2), byte(1), byte(0), byte(3)]; + } + }); + } + Store::Bc1 | Store::Bc2 | Store::Bc3 => { + let (texels, _) = pixels.as_chunks::<4>(); + rows(dst, row_bytes, |by, row| { + for (bx, d) in row.chunks_exact_mut(store.unit()).enumerate() { + let (block, inside) = gather(texels, (width, height), bx, by); + let encoded: &[u8] = match store { + Store::Bc1 => &dds_bc::encode_bc1(&block, inside), + Store::Bc2 => &dds_bc::encode_bc2(&block, inside), + _ => &dds_bc::encode_bc3(&block, inside), + }; + for (d, s) in d.iter_mut().zip(encoded) { + *d = *s; + } + } + }); + } + } + Ok(()) +} + +/// Block (`bx`, `by`)'s texels, and which of them lie inside the image. +fn gather(texels: &[[u8; 4]], (width, height): (usize, usize), bx: usize, by: usize) -> (Block, Mask) { + let mut block = [[0u8; 4]; 16]; + let mut inside: Mask = 0; + for (t, texel) in block.iter_mut().enumerate() { + let (x, y) = (bx * 4 + t % 4, by * 4 + t / 4); + if x < width + && y < height + && let Some(p) = texels.get(y * width + x) + { + *texel = *p; + inside |= 1 << t; + } + } + (block, inside) +} + +/// The next mip level: each pixel the average of the 2×2 pixels it covers, or of 2×3, 3×2 or +/// 3×3 along the last column and row of an odd-sized level, so no pixel is dropped. +fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result, CodecError> { + let len = nw.checked_mul(nh).and_then(|n| n.checked_mul(channels)).ok_or_else(|| encode_err("mip level size overflow"))?; + let mut dst = crate::options::alloc_zeroed::(len)?; + let span = |i: usize, n: usize, size: usize| 2 * i..=if i + 1 == n { size.saturating_sub(1) } else { 2 * i + 1 }; + rows(&mut dst, nw * channels, |y, row| { + let ys = span(y, nh, h); + for (x, px) in row.chunks_exact_mut(channels.max(1)).enumerate() { + let xs = span(x, nw, w); + for (c, v) in px.iter_mut().enumerate() { + let (mut sum, mut n) = (0u32, 0u32); + for sy in ys.clone() { + for sx in xs.clone() { + if let Some(&s) = src.get((sy * w + sx) * channels + c) { + sum += u32::from(s); + n += 1; + } + } + } + *v = ((sum + n / 2) / n.max(1)) as u8; + } + } + }); + Ok(dst) +} diff --git a/crates/image-codecs/src/codecs/dds_bc.rs b/crates/image-codecs/src/codecs/dds_bc.rs new file mode 100644 index 000000000..bbd55b4bf --- /dev/null +++ b/crates/image-codecs/src/codecs/dds_bc.rs @@ -0,0 +1,368 @@ +//! BC1, BC2 and BC3 (DXT1, DXT3, DXT5) blocks, after Microsoft's public block compression +//! documentation ("Block Compression (Direct3D 10)" and "Texture Block Compression in Direct3D +//! 11"): the palettes the DDS reader decodes with, and the block encoder the DDS writer uses. +//! +//! The encoder fits a block's colours with two RGB565 endpoints along their principal axis, +//! refines them by least squares, and scores every candidate against the exact palette the +//! reader rebuilds, so the indices it picks are the best ones for what a decoder shows. + +/// The 16 texels of a 4×4 block, row by row, as RGBA. +pub(crate) type Block = [[u8; 4]; 16]; + +/// Bit `t` set for each texel `t` of a block that counts: edge blocks of a texture whose size is +/// not a multiple of four have texels outside the image. +pub(crate) type Mask = u16; + +pub(crate) fn rgb565(c: u16) -> [u8; 3] { + let scale = |v: u16, max: u16| ((u32::from(v) * 255 + u32::from(max) / 2) / u32::from(max)) as u8; + [scale(c >> 11, 31), scale((c >> 5) & 63, 63), scale(c & 31, 31)] +} + +/// The colours a colour block's indices select. Four-colour mode (BC1 with `c0 > c1`, and every +/// BC2 and BC3 block) adds two colours at thirds; three-colour mode adds the midpoint and +/// transparent black. +pub(crate) fn colour_palette(c0: u16, c1: u16, four: bool) -> [[u8; 4]; 4] { + let (a, b) = (rgb565(c0), rgb565(c1)); + let mix = |wa: u32, wb: u32| { + let d = wa + wb; + let at = |i: usize| ((wa * u32::from(a[i]) + wb * u32::from(b[i]) + d / 2) / d) as u8; + [at(0), at(1), at(2), 255] + }; + if four { + [[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(2, 1), mix(1, 2)] + } else { + [[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(1, 1), [0; 4]] + } +} + +/// A BC3 alpha block's eight values: six interpolated at sevenths when `a0 > a1`, else four +/// interpolated at fifths followed by 0 and 255. +pub(crate) fn alpha_palette(a0: u8, a1: u8) -> [u8; 8] { + let (a0, a1) = (u32::from(a0), u32::from(a1)); + let mut palette = [a0, a1, 0, 0, 0, 0, 0, 255]; + if a0 > a1 { + for (k, v) in palette.iter_mut().enumerate().skip(2) { + let k = k as u32; + *v = ((8 - k) * a0 + (k - 1) * a1 + 3) / 7; + } + } else { + for (k, v) in palette.iter_mut().enumerate().take(6).skip(2) { + let k = k as u32; + *v = ((6 - k) * a0 + (k - 1) * a1 + 2) / 5; + } + } + palette.map(|v| v as u8) +} + +/// A BC1 block. Texels under half opacity become BC1's transparent black, which needs the +/// three-colour mode; an opaque block takes whichever mode fits its colours better. +pub(crate) fn encode_bc1(block: &Block, inside: Mask) -> [u8; 8] { + let transparent = select(block, inside, |t| t[3] < 128); + colour_block(block, inside & !transparent, transparent, true) +} + +/// A BC2 block: four bits of alpha per texel, then a four-colour block. +pub(crate) fn encode_bc2(block: &Block, inside: Mask) -> [u8; 16] { + let bits = block.iter().enumerate().fold(0u64, |bits, (t, texel)| bits | ((u64::from(texel[3]) * 15 + 127) / 255) << (4 * t)); + join(bits.to_le_bytes(), colour_block(block, inside, 0, false)) +} + +/// A BC3 block: an interpolated alpha block, then a four-colour block. +pub(crate) fn encode_bc3(block: &Block, inside: Mask) -> [u8; 16] { + join(alpha_block(block, inside), colour_block(block, inside, 0, false)) +} + +fn join(a: [u8; 8], b: [u8; 8]) -> [u8; 16] { + std::array::from_fn(|i| if i < 8 { a.get(i) } else { b.get(i - 8) }.copied().unwrap_or(0)) +} + +/// The texels of `inside` for which `f` holds. +fn select(block: &Block, inside: Mask, f: impl Fn(&[u8; 4]) -> bool) -> Mask { + block.iter().enumerate().filter(|(t, texel)| inside >> t & 1 != 0 && f(texel)).fold(0, |m, (t, _)| m | 1 << t) +} + +fn rgb_of(texel: &[u8; 4]) -> [f32; 3] { + [f32::from(texel[0]), f32::from(texel[1]), f32::from(texel[2])] +} + +/// RGB565 of a colour, rounding each channel to the nearest level. +fn to565(c: [f32; 3]) -> u16 { + let q = |v: f32, max: f32| (v.clamp(0.0, 255.0) * max / 255.0).round() as u16; + q(c[0], 31.0) << 11 | q(c[1], 63.0) << 5 | q(c[2], 31.0) +} + +fn distance(a: &[u8; 4], b: &[u8; 4]) -> u32 { + (0..3).map(|i| u32::from(a[i].abs_diff(b[i])).pow(2)).sum() +} + +/// A colour block (endpoints, then 2-bit indices) for the `opaque` texels' colours, with the +/// `transparent` ones on BC1's transparent index. `bc1` allows the three-colour mode; BC2 and +/// BC3 always decode four colours. +fn colour_block(block: &Block, opaque: Mask, transparent: Mask, bc1: bool) -> [u8; 8] { + let colours: Vec<[f32; 3]> = block.iter().enumerate().filter(|(t, _)| opaque >> t & 1 != 0).map(|(_, texel)| rgb_of(texel)).collect(); + if colours.is_empty() { + // Nothing visible: every texel on the transparent index (or index 0 of black). + let indices = if transparent != 0 { u32::MAX } else { 0 }; + return pack(0, 0, indices); + } + let (lo, hi) = principal_endpoints(&colours); + let mut candidates = vec![(to565(lo), to565(hi))]; + let modes: &[bool] = if transparent != 0 { + &[false] + } else if bc1 { + &[true, false] + } else { + &[true] + }; + let mut best: Option<(u32, [u8; 8])> = None; + // Each refinement starts from the best block so far: its indices give the least-squares + // endpoints, which become one more candidate. + for round in 0..3 { + for &(e0, e1) in &candidates { + for &four in modes { + let (bytes, error) = fit(block, opaque, transparent, e0, e1, four); + if best.is_none_or(|(e, _)| error < e) { + best = Some((error, bytes)); + } + } + } + let Some((error, bytes)) = best else { break }; + if error == 0 || round == 2 { + break; + } + candidates = least_squares(block, opaque, &bytes).into_iter().collect(); + if candidates.is_empty() { + break; + } + } + best.map_or([0; 8], |(_, bytes)| bytes) +} + +/// Orders endpoints `e0`, `e1` for the mode (`c0 > c1` for four colours, `c0 <= c1` for three) +/// and picks each texel's nearest palette colour. Returns the block and its squared error. +fn fit(block: &Block, opaque: Mask, transparent: Mask, e0: u16, e1: u16, four: bool) -> ([u8; 8], u32) { + let (c0, c1) = if four == (e0 >= e1) { (e0, e1) } else { (e1, e0) }; + // Equal endpoints can't be in four-colour order; the palette is then one colour either way. + let palette = colour_palette(c0, c1, four && c0 > c1); + let choices = if four && c0 > c1 { 4 } else { 3 }; + let mut indices = 0u32; + let mut error = 0u32; + for (t, texel) in block.iter().enumerate() { + let index = if transparent >> t & 1 != 0 { + 3 + } else if opaque >> t & 1 != 0 { + let (i, d) = palette.iter().take(choices).enumerate().map(|(i, p)| (i, distance(texel, p))).min_by_key(|&(_, d)| d).unwrap_or((0, 0)); + error += d; + i as u32 + } else { + 0 + }; + indices |= index << (2 * t); + } + (pack(c0, c1, indices), error) +} + +fn pack(c0: u16, c1: u16, indices: u32) -> [u8; 8] { + let (c0, c1, i) = (c0.to_le_bytes(), c1.to_le_bytes(), indices.to_le_bytes()); + [c0[0], c0[1], c1[0], c1[1], i[0], i[1], i[2], i[3]] +} + +/// The ends of the colours' spread along their principal axis (the covariance's dominant +/// eigenvector, by power iteration), clamped to the RGB cube. +fn principal_endpoints(colours: &[[f32; 3]]) -> ([f32; 3], [f32; 3]) { + let n = colours.len().max(1) as f32; + let mean = [0, 1, 2].map(|i| colours.iter().map(|c| c[i]).sum::() / n); + let mut cov = [[0f32; 3]; 3]; + for c in colours { + let d = [c[0] - mean[0], c[1] - mean[1], c[2] - mean[2]]; + for (i, row) in cov.iter_mut().enumerate() { + for (j, v) in row.iter_mut().enumerate() { + *v += d[i] * d[j]; + } + } + } + let times = |a: [f32; 3]| [0, 1, 2].map(|i| cov[i][0] * a[0] + cov[i][1] * a[1] + cov[i][2] * a[2]); + let iterate = |start: usize| { + let mut axis = [0f32; 3]; + axis[start] = 1.0; + for _ in 0..8 { + let next = times(axis); + let len = next.iter().map(|v| v * v).sum::().sqrt(); + if !len.is_normal() { + break; + } + axis = next.map(|v| v / len); + } + axis + }; + // From each channel's axis, keeping the one the colours spread along most: a channel that + // varies on its own is already an eigenvector, so a single start there would never leave it + // even when the others vary more together. + let spread = |a: [f32; 3]| times(a).iter().zip(a).map(|(x, y)| x * y).sum::(); + let axis = [0, 1, 2].map(iterate).into_iter().max_by(|&a, &b| spread(a).total_cmp(&spread(b))).unwrap_or([1.0, 0.0, 0.0]); + let along = |c: &[f32; 3]| (c[0] - mean[0]) * axis[0] + (c[1] - mean[1]) * axis[1] + (c[2] - mean[2]) * axis[2]; + let (lo, hi) = colours.iter().map(along).fold((f32::MAX, f32::MIN), |(lo, hi), t| (lo.min(t), hi.max(t))); + let at = |t: f32| [0, 1, 2].map(|i| (mean[i] + t * axis[i]).clamp(0.0, 255.0)); + if lo > hi { (mean, mean) } else { (at(lo), at(hi)) } +} + +/// The endpoints that minimise the squared error for the indices `bytes` already chose: each +/// texel's index fixes its weights between the endpoints, giving 2×2 normal equations per +/// channel. `None` when the indices don't constrain both endpoints. +fn least_squares(block: &Block, opaque: Mask, bytes: &[u8; 8]) -> Option<(u16, u16)> { + let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); + let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); + let four = c0 > c1; + // How much of c0 each index holds. + let weight = |i: u32| match (four, i) { + (_, 0) => Some(1.0f32), + (_, 1) => Some(0.0), + (true, 2) => Some(2.0 / 3.0), + (true, _) => Some(1.0 / 3.0), + (false, 2) => Some(0.5), + (false, _) => None, + }; + let (mut aa, mut ab, mut bb) = (0f32, 0f32, 0f32); + let (mut ax, mut bx) = ([0f32; 3], [0f32; 3]); + for (t, texel) in block.iter().enumerate() { + if opaque >> t & 1 == 0 { + continue; + } + let Some(a) = weight(indices >> (2 * t) & 3) else { continue }; + let b = 1.0 - a; + let c = rgb_of(texel); + aa += a * a; + ab += a * b; + bb += b * b; + for ((x0, x1), v) in ax.iter_mut().zip(bx.iter_mut()).zip(c) { + *x0 += a * v; + *x1 += b * v; + } + } + let det = aa * bb - ab * ab; + if det.abs() < 1e-3 { + return None; + } + let e0 = [0, 1, 2].map(|i| (ax[i] * bb - bx[i] * ab) / det); + let e1 = [0, 1, 2].map(|i| (bx[i] * aa - ax[i] * ab) / det); + Some((to565(e0), to565(e1))) +} + +/// A BC3 alpha block: whichever of the two modes (eight values between the extremes, or six +/// between the extremes other than 0 and 255, plus exact 0 and 255) fits better. +fn alpha_block(block: &Block, inside: Mask) -> [u8; 8] { + let alphas = || block.iter().enumerate().filter(move |(t, _)| inside >> t & 1 != 0).map(|(_, texel)| texel[3]); + let range = |values: &mut dyn Iterator| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))); + let (lo, hi) = range(&mut alphas()).unwrap_or((0, 0)); + // Eight values need a0 > a1; equal ends fall to the six-value mode, which starts with a0 too. + let eight = alpha_fit(block, inside, hi, lo); + let (mid_lo, mid_hi) = range(&mut alphas().filter(|a| !matches!(a, 0 | 255))).unwrap_or((0, 0)); + let six = alpha_fit(block, inside, mid_lo, mid_hi); + if six.1 < eight.1 { six.0 } else { eight.0 } +} + +fn alpha_fit(block: &Block, inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u32) { + let palette = alpha_palette(a0, a1); + let mut bits = 0u64; + let mut error = 0u32; + for (t, texel) in block.iter().enumerate() { + if inside >> t & 1 == 0 { + continue; + } + let (i, d) = palette.iter().enumerate().map(|(i, &p)| (i, u32::from(p.abs_diff(texel[3])).pow(2))).min_by_key(|&(_, d)| d).unwrap_or((0, 0)); + error += d; + bits |= (i as u64) << (3 * t); + } + let b = bits.to_le_bytes(); + ([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn decode_colour(bytes: &[u8; 8], bc1: bool) -> Block { + let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); + let palette = colour_palette(c0, c1, c0 > c1 || !bc1); + let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); + std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3]) + } + + #[test] + fn solid_blocks_land_on_the_nearest_565_colour() { + for colour in [[0, 0, 0, 255], [255, 255, 255, 255], [12, 200, 77, 255]] { + let block = [colour; 16]; + let out = decode_colour(&encode_bc1(&block, u16::MAX), true); + for texel in out { + assert!(texel.iter().zip(colour).all(|(a, b)| a.abs_diff(b) <= 4), "{texel:?} for {colour:?}"); + } + } + } + + #[test] + fn two_colour_blocks_are_exact_when_the_colours_are_565() { + let (a, b) = ([255, 0, 0, 255], [0, 0, 255, 255]); + let block: Block = std::array::from_fn(|t| if t % 3 == 0 { a } else { b }); + for bc1 in [true, false] { + let bytes = if bc1 { encode_bc1(&block, u16::MAX) } else { colour_block(&block, u16::MAX, 0, false) }; + assert_eq!(decode_colour(&bytes, bc1), block); + } + } + + #[test] + fn bc1_punches_through_texels_under_half_alpha() { + let block: Block = std::array::from_fn(|t| if t < 8 { [200, 100, 50, 255] } else { [9, 9, 9, 127] }); + let bytes = encode_bc1(&block, u16::MAX); + let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); + assert!(c0 <= c1, "three-colour mode"); + let out = decode_colour(&bytes, true); + assert!(out[..8].iter().all(|t| t[3] == 255) && out[8..].iter().all(|t| *t == [0; 4]), "{out:?}"); + } + + #[test] + fn four_colour_blocks_keep_c0_above_c1() { + // A ramp needs all four colours; BC2 and BC3 blocks must not rely on the order, but + // older decoders read c0 <= c1 as three colours, so the encoder never writes that. + let block: Block = std::array::from_fn(|t| [(t * 16) as u8, 0, 0, 255]); + let bytes = colour_block(&block, u16::MAX, 0, false); + assert!(u16::from_le_bytes([bytes[0], bytes[1]]) > u16::from_le_bytes([bytes[2], bytes[3]])); + } + + #[test] + fn alpha_blocks_pick_the_mode_that_keeps_0_and_255() { + // 0, 255 and a cluster in between: the six-value mode stores 0 and 255 exactly. + let block: Block = std::array::from_fn(|t| [0, 0, 0, [0, 255, 100, 110][t % 4]]); + let bytes = alpha_block(&block, u16::MAX); + assert!(bytes[0] <= bytes[1], "six-value mode"); + let palette = alpha_palette(bytes[0], bytes[1]); + let bits = u64::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], 0, 0]); + for (t, texel) in block.iter().enumerate() { + let got = palette[(bits >> (3 * t)) as usize & 7]; + assert!(got.abs_diff(texel[3]) <= 2, "texel {t}: {got} for {}", texel[3]); + } + } + + #[test] + fn the_principal_axis_is_found_when_one_channel_varies_on_its_own() { + // Red alternates on its own (variance 64); green and blue alternate together (variance 49 + // each, 98 along their shared axis), uncorrelated with red. Starting on red alone would + // keep red's axis and flatten green and blue; the block must do better than its bounding box. + let block: Block = std::array::from_fn(|t| { + let gb = if (t / 2) % 2 == 0 { 121 } else { 135 }; + [if t % 2 == 0 { 120 } else { 136 }, gb, gb, 255] + }); + let error = |bytes: &[u8; 8]| decode_colour(bytes, false).iter().zip(&block).map(|(a, b)| distance(a, b)).sum::(); + let (_, bounding_box) = fit(&block, u16::MAX, 0, to565([120.0, 121.0, 121.0]), to565([136.0, 135.0, 135.0]), true); + let ours = error(&colour_block(&block, u16::MAX, 0, false)); + assert!(ours < bounding_box, "{ours} vs the bounding box's {bounding_box}"); + } + + #[test] + fn texels_outside_the_image_are_ignored() { + // Only the top-left texel is inside: its colour alone decides the endpoints. + let mut block: Block = [[0, 255, 0, 255]; 16]; + block[0] = [255, 0, 255, 255]; + let out = decode_colour(&encode_bc1(&block, 1), true); + assert_eq!(out[0], [255, 0, 255, 255]); + } +} diff --git a/crates/image-codecs/src/codecs/deep_exr.rs b/crates/image-codecs/src/codecs/deep_exr.rs new file mode 100644 index 000000000..0652112bf --- /dev/null +++ b/crates/image-codecs/src/codecs/deep_exr.rs @@ -0,0 +1,786 @@ +//! Deep OpenEXR (`deepscanline` / `deeptile`): a variable-length list of samples per pixel, +//! each with its own colour, alpha and depth. The `exr` crate rejects deep headers, so the +//! chunk stream is walked directly from the bytes, per the OpenEXR file-layout specification: +//! a deep chunk carries its coordinates, the packed size of the sample-count table, the +//! packed and unpacked sizes of the sample data, then the compressed count table (cumulative +//! within each row of the block) and the compressed sample data (row by row, each row channel +//! by channel). The layout was checked against files written by the OpenEXR library. Deep data permits only NONE, RLE, ZIPS and ZIP +//! compression; we support the first three (one scan line per block, which keeps the count +//! table unambiguous) and decline ZIP with `unsupported`. +//! +//! [`decode_deep`] returns the structured samples ([`DeepImage`]); [`flatten`] composites +//! them into a flat `Image` — samples sorted by Z, front-to-back `over` with premultiplied +//! colour, un-premultiplied into straight alpha (HDR values are kept, not clamped) — because the flat document model has no +//! per-pixel depth. + +use std::io::{Cursor, Read, Seek, SeekFrom}; + +use exr::meta::attribute::{LevelMode, SampleType as ExrSample}; +use exr::meta::{BlockDescription, MetaData}; +use exr::prelude::Compression; +use half::f16; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, DecodeWarning, DeepChannel, DeepImage, Image, SampleType}; +use crate::options::{Limits, alloc_zeroed}; + +const F: Format = Format::OpenExr; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +fn unsup(reason: impl Into) -> CodecError { + CodecError::unsupported(F, reason) +} + +// --- checked little-endian reads ------------------------------------------------------------- +// Every length and offset comes from the file, so every read is bounds-checked and a short +// read is a malformed file, never a panic. + +fn read_exact<'a>(cur: &mut Cursor<&'a [u8]>, n: usize, what: &str) -> Result<&'a [u8], CodecError> { + // Borrow packed payloads from the input. Copying them here allocates before count-table + // admission and needlessly duplicates the table even when the second pass just skips it. + let truncated = || err(format!("truncated file while reading {what}")); + let start = usize::try_from(cur.position()).map_err(|_| truncated())?; + let end = start.checked_add(n).ok_or_else(truncated)?; + let bytes = *cur.get_ref(); + let v = bytes.get(start..end).ok_or_else(truncated)?; + cur.set_position(end as u64); + Ok(v) +} + +fn i32(cur: &mut Cursor<&[u8]>) -> Result { + let b = read_exact(cur, 4, "an i32")?; + Ok(i32::from_le_bytes([b.first().copied().unwrap_or(0), b.get(1).copied().unwrap_or(0), b.get(2).copied().unwrap_or(0), b.get(3).copied().unwrap_or(0)])) +} + +fn u64(cur: &mut Cursor<&[u8]>) -> Result { + let b = read_exact(cur, 8, "a u64")?; + let a: [u8; 8] = b.try_into().unwrap_or([0; 8]); + Ok(u64::from_le_bytes(a)) +} + +fn seek_to(cur: &mut Cursor<&[u8]>, pos: u64) -> Result<(), CodecError> { + if pos > cur.get_ref().len() as u64 { + return Err(err(format!("chunk offset {pos} points past the end of the file"))); + } + cur.seek(SeekFrom::Start(pos)).map_err(err)?; + Ok(()) +} + +// --- decompression ---------------------------------------------------------------------------- + +/// Undo the ZIP predictor: each byte was stored as its difference to the previous byte plus +/// 128 (so a zero difference is 0x80), modulo 256. Applies to the sample-count table and the +/// sample data alike. +fn differences_to_samples(buf: &mut [u8]) { + if let Some(&first) = buf.first() { + let mut prev = i16::from(first); + for b in buf.iter_mut().skip(1) { + let s = (prev + i16::from(*b) - 128) as u8; + prev = i16::from(s); + *b = s; + } + } +} + +/// Undo the ZIP half-split: the stream is `[first half][second half]` with the second half +/// holding every second byte of the original; weave them back together. The scratch copy is +/// allocated fallibly, so a buffer that passed `max_alloc` but not the allocator is an error. +fn interleave_byte_blocks(buf: &mut [u8]) -> Result<(), CodecError> { + let half = buf.len().div_ceil(2); + let mut out = alloc_zeroed::(buf.len())?; + let mut k = 0usize; + for i in 0..buf.len() / 2 { + let (a, b) = (buf.get(i).copied(), buf.get(half + i).copied()); + if let (Some(a), Some(b), Some(pair)) = (a, b, out.get_mut(k..k + 2)) { + if let Some(byte) = pair.first_mut() { + *byte = a; + } + if let Some(byte) = pair.get_mut(1) { + *byte = b; + } + } + k += 2; + } + if buf.len() % 2 == 1 && half > 0 { + let last = buf.get(half - 1).copied(); + if let (Some(last), Some(slot)) = (last, out.get_mut(k)) { + *slot = last; + } + } + buf.copy_from_slice(&out); + Ok(()) +} + +/// Make room in `out` for `extra` more bytes, `limit` in all. Grows by doubling like `Vec`, +/// but never reserves past `limit`: callers admit exactly that size against `max_alloc`, and +/// plain doubling reserved up to twice as much. Fallible, so a failed allocation is an error. +fn reserve_up_to(out: &mut Vec, extra: usize, limit: usize, what: &str) -> Result<(), CodecError> { + let need = out.len().saturating_add(extra); + if need > limit { + return Err(err(format!("{what}: data expands past {limit} bytes"))); + } + if need > out.capacity() { + let target = out.capacity().saturating_mul(2).max(need).min(limit); + out.try_reserve_exact(target.saturating_sub(out.len())).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {target} bytes for {what}")))?; + } + Ok(()) +} + +/// OpenEXR RLE tokens: a negative byte `n` means `-n` literal bytes follow, a non-negative +/// byte `n` means the next byte repeats `n + 1` times (as `ImfRle.cpp` and the `exr` crate). +fn rle_decode(src: &[u8], expected: usize, what: &str) -> Result, CodecError> { + let mut out = Vec::new(); + reserve_up_to(&mut out, expected.min(1 << 20), expected, what)?; + let mut i = 0; + while i < src.len() { + let count = src[i] as i8; + i += 1; + let n = if count < 0 { (-(count as i32)) as usize } else { count as usize + 1 }; + if out.len().saturating_add(n) > expected { + return Err(err(format!("{what}: RLE data expands past {expected} bytes"))); + } + reserve_up_to(&mut out, n, expected, what)?; + if count < 0 { + let end = i.saturating_add(n); + let run = src.get(i..end.min(src.len())).ok_or_else(|| err(format!("{what}: RLE run points past the compressed data")))?; + if run.len() != n { + return Err(err(format!("{what}: RLE run points past the compressed data"))); + } + out.extend_from_slice(run); + i = end; + } else { + let b = src.get(i).copied().ok_or_else(|| err(format!("{what}: RLE run points past the compressed data")))?; + i += 1; + out.resize(out.len() + n, b); + } + } + if out.len() != expected { + return Err(err(format!("{what}: RLE data expands to {} bytes, expected {expected}", out.len()))); + } + Ok(out) +} + +/// Decompress `what` to exactly `expected` bytes. Like flat blocks, a payload that already +/// has the uncompressed length is stored raw whatever the declared compression. +fn decompress(compression: Compression, packed: &[u8], expected: usize, what: &str) -> Result, CodecError> { + if packed.len() == expected { + let mut out = Vec::new(); + reserve_up_to(&mut out, expected, expected, what)?; + out.extend_from_slice(packed); + return Ok(out); + } + match compression { + Compression::Uncompressed => Err(err(format!("{what}: expected {expected} uncompressed bytes, found {}", packed.len()))), + Compression::RLE => { + // Like ZIPS, RLE runs over the predicted, byte-split stream. + let mut out = rle_decode(packed, expected, what)?; + differences_to_samples(&mut out); + interleave_byte_blocks(&mut out)?; + Ok(out) + } + Compression::ZIP1 => { + // Inflate straight into `out`, grown with `reserve_up_to` rather than by + // `read_to_end`, whose doubling reserves past `expected`. Once `expected` bytes are + // in, a one-byte read checks that the stream ends there. + let malformed = || err(format!("{what}: zlib data is malformed")); + let mut zlib = flate2::bufread::ZlibDecoder::new(packed); + let mut out = Vec::new(); + let mut filled = 0; + while filled < expected { + if filled == out.len() { + let grow = out.len().max(1 << 13).min(expected - filled); + reserve_up_to(&mut out, grow, expected, what)?; + out.resize(filled + grow, 0); + } + let n = zlib.read(out.get_mut(filled..).unwrap_or_default()).map_err(|_| malformed())?; + if n == 0 { + break; + } + filled += n; + } + out.truncate(filled); + if filled == expected && zlib.read(&mut [0u8; 1]).map_err(|_| malformed())? != 0 { + return Err(err(format!("{what}: zlib data expands past {expected} bytes"))); + } + if out.len() != expected { + return Err(err(format!("{what}: zlib data expands to {} bytes, expected {expected}", out.len()))); + } + differences_to_samples(&mut out); + interleave_byte_blocks(&mut out)?; + Ok(out) + } + other => Err(unsup(format!("deep data with {other:?} compression (NONE, RLE and ZIPS are supported)"))), + } +} + +// --- the deep chunk stream --------------------------------------------------------------------- + +/// One parsed chunk: where its pixels go and its cumulative sample counts, plus where to +/// find the compressed sample data in the second pass. +struct DeepChunk { + /// Global pixel indices of the block's pixels, in block order (row-major within the block). + pixels: Vec, + /// Pixels per row of the block (the data window width for a scan line, the clipped tile + /// width for a tile). + row_w: usize, + /// Cumulative sample count table of the block: `len == pixels.len() + 1`. + prefix: Vec, + /// File offset of the chunk start. + pos: u64, + /// Size of the compressed sample data in the file. + packed_data: u64, + /// The decompressed sample-data size the file declares (validated against the table). + unpacked_data: u64, +} + +fn sample_bytes(t: ExrSample) -> usize { + match t { + ExrSample::U32 | ExrSample::F32 => 4, + ExrSample::F16 => 2, + } +} + +fn our_sample_type(t: ExrSample) -> SampleType { + match t { + ExrSample::F16 => SampleType::F16, + ExrSample::U32 | ExrSample::F32 => SampleType::F32, + } +} + +/// Read the deep samples of the first deep part. `meta` must come from an unvalidated +/// [`MetaData::read_from_buffered`] of the same bytes (deep headers fail validation). +pub(crate) fn decode_deep(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Result { + let header_index = meta.headers.iter().position(|h| h.deep).ok_or_else(|| unsup("the file has no deep part"))?; + let header = meta.headers.get(header_index).ok_or_else(|| unsup("the file has no deep part"))?; + let compression = header.compression; + if !matches!(compression, Compression::Uncompressed | Compression::RLE | Compression::ZIP1) { + return Err(unsup(format!("deep data with {compression:?} compression (NONE, RLE and ZIPS are supported)"))); + } + + let dw = header.data_window(); + let (w, h) = (dw.size.0, dw.size.1); + if w == 0 || h == 0 { + return Err(err("the data window is empty")); + } + if w > u32::MAX as usize || h > u32::MAX as usize { + return Err(err("the data window is too large")); + } + let y0 = dw.position.1; + let npixels = w.checked_mul(h).ok_or_else(|| err("the data window is too large"))?; + limits.check_bytes(w as u32, h as u32, (header.channels.list.len().max(1) as u64) * 4)?; + + // The display window is the full stage the renderer composed onto (a render pass's data + // window is often its crop). Open on the stage when it plausibly holds the data region: + // sane size for the limits, and the data region inside it. Otherwise fall back to the + // data window alone, as before. + let display = header.shared_attributes.display_window; + let display_size = (display.size.0.max(1) as u32, display.size.1.max(1) as u32); + let (stage, origin) = match display_size { + (sw, sh) + if sw <= 1 << 15 + && sh <= 1 << 15 + && limits.check_bytes(sw, sh, 4).is_ok() + && dw.position.0 >= display.position.0 + && dw.position.1 >= display.position.1 + && dw.position.0.saturating_add(w as i32) <= display.position.0.saturating_add(sw as i32) + && dw.position.1.saturating_add(h as i32) <= display.position.1.saturating_add(sh as i32) => + { + (Some((sw, sh)), (dw.position.0 - display.position.0, dw.position.1 - display.position.1)) + } + _ => (None, (0, 0)), + }; + + let exr_types: Vec = header.channels.list.iter().map(|c| c.sample_type).collect(); + let bytes_per_sample: usize = exr_types.iter().map(|t| sample_bytes(*t)).sum(); + let multipart = meta.requirements.has_multiple_layers; + + // The offset tables follow the headers: `chunk_count` u64 per header, in header order. + // Our part's table starts after the tables of all earlier parts. + let mut table_pos = header_end(bytes)?; + for h_ in meta.headers.iter().take(header_index) { + table_pos = table_pos.saturating_add((h_.chunk_count.min(1 << 24) as u64).saturating_mul(8)); + } + let mut cur = Cursor::new(bytes); + seek_to(&mut cur, table_pos)?; + let mut offsets = Vec::with_capacity(header.chunk_count.min(1 << 20)); + for _ in 0..header.chunk_count { + offsets.push(u64(&mut cur)?); + } + let tables_end = cur.position(); + + // Block geometry. + let (tiles_x, tile_w, tile_h, slots) = match &header.blocks { + BlockDescription::ScanLines => (0usize, w, 1usize, h), // one line per block for NONE/RLE/ZIPS + BlockDescription::Tiles(t) => { + if t.level_mode != LevelMode::Singular { + return Err(unsup("deep mip-map or rip-map tiles (single-level tiles are supported)")); + } + let (tw, th) = (t.tile_size.0.max(1), t.tile_size.1.max(1)); + let tx = w.saturating_add(tw - 1) / tw; + let ty = h.saturating_add(th - 1) / th; + (tx, tw, th, tx.checked_mul(ty).ok_or_else(|| err("tile count overflows"))?) + } + }; + + let mut counts = vec![0u32; npixels]; + let mut seen = vec![false; slots]; + let mut chunks: Vec = Vec::with_capacity(offsets.len()); + + for &off in &offsets { + if off < tables_end || off >= bytes.len() as u64 { + return Err(err(format!("chunk offset {off} is outside the chunk area of the file"))); + } + seek_to(&mut cur, off)?; + if multipart { + let part = i32(&mut cur)?; + if part < 0 || part as usize != header_index { + return Err(err(format!("chunk declares part {part}, expected {header_index}"))); + } + } + // Block coordinates → the block's pixel bounds and its slot. The per-pixel index is + // built only after the chunk's declared payload sizes are validated below, so a + // truncated or lying chunk cannot drive a large index allocation first. + let (block_rows, block_cols, slot, row_w, scanline_row): (std::ops::Range, std::ops::Range, usize, usize, bool) = match &header.blocks { + BlockDescription::ScanLines => { + let y = i32(&mut cur)?; + let row = match y.checked_sub(y0) { + Some(d) if d >= 0 && (d as usize) < h => d as usize, + _ => return Err(err(format!("scan line block y={y} is outside the data window"))), + }; + (row..row + 1, 0..w, row, w, true) + } + BlockDescription::Tiles(_) => { + let (tx, ty, lx, ly) = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); + if (lx, ly) != (0, 0) { + return Err(err(format!("deep tile at level ({lx},{ly}); only level 0 exists in a single-level file"))); + } + if tx < 0 || ty < 0 || (tx as usize) >= tiles_x || (ty as usize) * tile_h >= h { + return Err(err(format!("tile ({tx},{ty}) is outside the data window"))); + } + let (bx, by) = ((tx as usize) * tile_w, (ty as usize) * tile_h); + let rows = (by + tile_h).min(h); + let cols = (bx + tile_w).min(w); + let slot = (ty as usize).saturating_mul(tiles_x) + tx as usize; + (by..rows, bx..cols, slot, cols - bx, false) + } + }; + let block_len = block_rows.len().saturating_mul(block_cols.len()); + if seen.get(slot).copied().unwrap_or(true) { + return Err(err(format!("two chunks cover block {slot}"))); + } + if let Some(s) = seen.get_mut(slot) { + *s = true; + } + + let packed_table = u64(&mut cur)?; + let packed_data = u64(&mut cur)?; + let unpacked_data = u64(&mut cur)?; + let remaining = (bytes.len() as u64).saturating_sub(cur.position()); + if packed_table > remaining || packed_data > remaining.saturating_sub(packed_table) { + return Err(err("deep chunk sizes point past the end of the file")); + } + // The chunk's payload is present; now materialize the block's global pixel index. For + // scan lines this is one row; for tiles the clipped tile rectangle. + let block_pixels: Vec = if scanline_row { + let row = block_rows.start; + (0..w).map(|x| row * w + x).collect() + } else { + let mut px = Vec::with_capacity(block_len); + for yy in block_rows.clone() { + for xx in block_cols.clone() { + px.push(yy.saturating_mul(w).saturating_add(xx)); + } + } + px + }; + let what = "the sample-count table"; + let table_bytes = read_exact(&mut cur, packed_table as usize, what)?; + let expected_table = block_pixels.len().checked_mul(4).ok_or_else(|| err("count table overflows"))?; + // A compressed clipped table also needs its scratch copy. The data-window check only + // budgets four bytes per channel per pixel, insufficient for a one-channel image. + // An equal-sized payload takes decompress's raw path, which allocates no scratch. + let buffers = if table_bytes.len() == expected_table { 1 } else { 2 }; + let clipped_need = (expected_table as u64).saturating_mul(buffers); + if clipped_need > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!( + "{what} needs {clipped_need} bytes including decompression scratch, exceeding max_alloc {}", + limits.max_alloc + ))); + } + // An edge tile's table may still be stored at the full tile size (its pixels first, in + // rows of the clipped width, then unused entries), as the OpenEXR library writes it. + // That size comes from the header's tile size alone, which nothing ties to the data + // window, so it is tried only when `max_alloc` has room for the table and for the copy + // that undoes its byte split. Otherwise only the clipped size is read. + let full_table = tile_w.checked_mul(tile_h).and_then(|n| n.checked_mul(4)); + let raw = match full_table { + // Scan lines and interior tiles: the full size is the clipped size. + Some(full) if full <= expected_table => decompress(compression, table_bytes, expected_table, what)?, + Some(full) if (full as u64).saturating_mul(2) <= limits.max_alloc => { + decompress(compression, table_bytes, full, what).or_else(|_| decompress(compression, table_bytes, expected_table, what))? + } + _ => decompress(compression, table_bytes, expected_table, what).map_err(|_| { + // Four bytes per pixel of the tile, twice: the table and its scratch copy. + let need = (tile_w as u64).saturating_mul(tile_h as u64).saturating_mul(8); + CodecError::LimitExceeded(format!( + "{what} of a {tile_w}x{tile_h} deep tile does not decode at the clipped size, and at the full tile size it needs {need} bytes with its scratch copy, exceeding max_alloc {}", + limits.max_alloc + )) + })?, + }; + + // The table is cumulative within each row of the block (a tile restarts at every row); + // `prefix` is cumulative over the whole block, and each pixel's own count is the difference. + let mut prefix = Vec::with_capacity(block_pixels.len() + 1); + prefix.push(0u32); + let (mut row_base, mut prev) = (0u32, 0u32); + for (i, p) in block_pixels.iter().enumerate() { + let b: [u8; 4] = raw.get(i * 4..i * 4 + 4).and_then(|s| s.try_into().ok()).ok_or_else(|| err("the count table is short"))?; + let v = u32::from_le_bytes(b); + if row_w > 0 && i % row_w == 0 { + row_base = row_base.checked_add(prev).ok_or_else(|| err("the block's sample count overflows"))?; + prev = 0; + } + if v < prev { + return Err(err("the sample-count table decreases (malformed cumulative counts)")); + } + if let Some(c) = counts.get_mut(*p) { + *c = v - prev; + } + prev = v; + prefix.push(row_base.checked_add(v).ok_or_else(|| err("the block's sample count overflows"))?); + } + let total = prefix.last().copied().unwrap_or(0) as u64; + let expected_data = total.checked_mul(bytes_per_sample as u64).ok_or_else(|| err("the sample data size overflows"))?; + if expected_data != unpacked_data { + return Err(err(format!("the chunk declares {unpacked_data} bytes of sample data, the count table implies {expected_data}"))); + } + chunks.push(DeepChunk { pixels: block_pixels, row_w, prefix, pos: off, packed_data, unpacked_data }); + } + if seen.iter().any(|s| !s) { + let missing = seen.iter().filter(|s| !**s).count(); + return Err(err(format!("{missing} of {slots} chunks are missing from the offset table"))); + } + + // Global cumulative counts in raster order. + let mut deep_counts = Vec::with_capacity(npixels + 1); + deep_counts.push(0u64); + let mut acc = 0u64; + for &c in &counts { + acc = acc.checked_add(c as u64).ok_or_else(|| err("the total sample count overflows"))?; + deep_counts.push(acc); + } + let total_samples = acc; + let sample_bytes_total = total_samples.checked_mul(exr_types.len() as u64 * 4).ok_or_else(|| err("the sample buffer size overflows"))?; + if sample_bytes_total > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("deep samples need {sample_bytes_total} bytes, exceeding max_alloc {}", limits.max_alloc))); + } + + let mut channels: Vec = header + .channels + .list + .iter() + .map(|c| DeepChannel { name: c.name.to_string(), sample: our_sample_type(c.sample_type), samples: vec![0f32; total_samples as usize] }) + .collect(); + + // Second pass: the compressed sample data, scattered per pixel (a block's channel data + // is contiguous in the file but the pixels' samples interleave with other blocks'). + for chunk in &chunks { + seek_to(&mut cur, chunk.pos)?; + if multipart { + let _part = i32(&mut cur)?; + } + match &header.blocks { + BlockDescription::ScanLines => { + let _y = i32(&mut cur)?; + } + BlockDescription::Tiles(_) => { + let _coords = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); + } + } + let packed_table = u64(&mut cur)?; + let _packed_data = u64(&mut cur)?; + let _unpacked_data = u64(&mut cur)?; + // The count table sits between the sizes and the sample data; skip its bytes. + read_exact(&mut cur, packed_table as usize, "the sample-count table")?; + let data_bytes = read_exact(&mut cur, chunk.packed_data as usize, "the deep sample data")?; + let raw = decompress(compression, data_bytes, chunk.unpacked_data as usize, "the deep sample data")?; + // Row by row (a scan line block is one row), and within a row channel by channel, as + // flat tiles are laid out: each pixel's samples are contiguous within its channel. + let mut in_off = 0usize; + let row_w = chunk.row_w.max(1); + for (r, row) in chunk.pixels.chunks(row_w).enumerate() { + let i0 = r * row_w; + let row_from = chunk.prefix.get(i0).copied().unwrap_or(0) as usize; + let row_to = chunk.prefix.get(i0 + row.len()).copied().unwrap_or(0) as usize; + let row_total = row_to.saturating_sub(row_from); + for (ci, t) in exr_types.iter().enumerate() { + let sb = sample_bytes(*t); + let ch_bytes = row_total.checked_mul(sb).ok_or_else(|| err("channel size overflows"))?; + let end = in_off.checked_add(ch_bytes).ok_or_else(|| err("channel size overflows"))?; + let src = raw.get(in_off..end).ok_or_else(|| err("the sample data is short"))?; + in_off = end; + let out = channels.get_mut(ci).ok_or_else(|| err("channel index out of range"))?; + for (j, p) in row.iter().enumerate() { + let from = (chunk.prefix.get(i0 + j).copied().unwrap_or(0) as usize).saturating_sub(row_from); + let n = (chunk.prefix.get(i0 + j + 1).copied().unwrap_or(0) as usize).saturating_sub(row_from).saturating_sub(from); + let to = deep_counts.get(*p).copied().unwrap_or(0) as usize; + for k in 0..n { + let base = (from + k) * sb; + let v = match t { + ExrSample::F16 => src.get(base..base + 2).map(|b| f16::from_le_bytes([b[0], b[1]]).to_f32()), + ExrSample::F32 => src.get(base..base + 4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])), + ExrSample::U32 => src.get(base..base + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32), + }; + let (Some(v), Some(slot)) = (v, out.samples.get_mut(to + k)) else { + return Err(err("the sample data is short")); + }; + *slot = v; + } + } + } + } + } + + Ok(DeepImage { width: w as u32, height: h as u32, stage, origin, channels, counts: deep_counts }) +} + +/// Where the headers end and the offset tables begin. [`MetaData::read_from_buffered`] +/// consumes exactly the version field and the headers, so re-reading with a fresh cursor +/// and taking its end position yields it. +fn header_end(bytes: &[u8]) -> Result { + let mut cur = Cursor::new(bytes); + MetaData::read_from_buffered(&mut cur, false).map_err(err)?; + Ok(cur.position()) +} + +/// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the +/// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied +/// into straight alpha at the end. Without any alpha channel the front-most sample wins, +/// opaque. Z, ZBack and other deep channels influence the flat result only through the +/// ordering; volume samples (with ZBack) are approximated as point samples. +pub(crate) fn flatten(deep: &DeepImage, limits: &Limits) -> Result { + let npx = deep.width as usize * deep.height as usize; + let has = |n: &str| deep.channel(n).is_some(); + let rgb = has("R") && has("G") && has("B"); + let gray = !rgb && (has("Y") || deep.channels.len() == 1); + let layout = if rgb || gray { + if has("A") || has("AR") || has("AG") || has("AB") { + if rgb { ChannelLayout::Rgba } else { ChannelLayout::GrayA } + } else if rgb { + ChannelLayout::Rgb + } else { + ChannelLayout::Gray + } + } else { + return Err(CodecError::unsupported(F, "no R/G/B or Y channels in the deep part")); + }; + let nc = layout.channels(); + // Admit the flattened output at its true size: nc f32 samples per pixel. The deep decode + // admitted the data window at four bytes per pixel, before the flattened channel layout + // was known, so re-check here with the actual layout and f32 sample size. + limits.check_bytes(deep.width, deep.height, nc as u64 * 4)?; + let samples = |n: &str| -> &[f32] { deep.channel(n).map(|c| c.samples.as_slice()).unwrap_or(&[]) }; + let (r, g, b) = (samples("R"), samples("G"), samples("B")); + let (ar, ag, ab) = (samples("AR"), samples("AG"), samples("AB")); + let (a, z) = (samples("A"), samples("Z")); + let y = samples("Y"); + let lum = if y.is_empty() { deep.channels.first().map(|c| c.samples.as_slice()).unwrap_or(&[]) } else { y }; + + let mut out = vec![0f32; npx * nc]; + let mut order: Vec = Vec::new(); + let mut max_samples = 0u32; + for p in 0..npx { + let range = deep.sample_range(p); + let (s0, n) = (range.start as usize, (range.end - range.start) as usize); + max_samples = max_samples.max(u32::try_from(n).unwrap_or(u32::MAX)); + if n == 0 { + continue; // no samples: transparent black + } + order.clear(); + order.extend(0u32..n as u32); + if !z.is_empty() { + // Stable sort by Z; a NaN depth (invalid) sorts last, deterministically. + order.sort_by(|&i, &j| { + let zi = z.get(s0 + i as usize).copied().unwrap_or(f32::NAN); + let zj = z.get(s0 + j as usize).copied().unwrap_or(f32::NAN); + zi.total_cmp(&zj) + }); + } + let at = |ch: &[f32], i: u32| ch.get(s0 + i as usize).copied().unwrap_or(0.0); + // Per-colour-channel alpha: AR/AG/AB when present, else A, else opaque. + let alpha = |i: u32| -> [f32; 3] { + let s = at(a, i); + [ + if !a.is_empty() && ar.is_empty() { + s + } else if !ar.is_empty() { + at(ar, i) + } else { + 1.0 + }, + if !a.is_empty() && ag.is_empty() { + s + } else if !ag.is_empty() { + at(ag, i) + } else { + 1.0 + }, + if !a.is_empty() && ab.is_empty() { + s + } else if !ab.is_empty() { + at(ab, i) + } else { + 1.0 + }, + ] + }; + let mut cacc = [0f32; 3]; // premultiplied colour + let mut aacc = [0f32; 3]; // accumulated alpha + for &i in &order { + let ai = alpha(i); + let src = if rgb { [at(r, i), at(g, i), at(b, i)] } else { [at(lum, i); 3] }; + let mut covered = true; + for k in 0..3 { + let t = (1.0 - aacc[k]).max(0.0); + cacc[k] += src[k] * t; + aacc[k] += t * ai[k]; + covered &= aacc[k] >= 1.0; + } + if covered { + break; // everything behind is hidden + } + } + let a_out = if a.is_empty() && ar.is_empty() && ag.is_empty() && ab.is_empty() { 1.0 } else { (aacc[0] + aacc[1] + aacc[2]) / 3.0 }; + let a_clamped = a_out.clamp(0.0, 1.0); + // Un-premultiply into straight alpha (the deep colours are premultiplied). + let straight = |c: f32| if a_clamped > 1e-6 { c / a_clamped } else { 0.0 }; + let base = p * nc; + // Written by layout: RGB(A), gray(+A) — a fixed RGBA quadruple would put a luminance + // copy into a GrayA image's alpha slot. + // A fixed array plus a length: this runs once per pixel, so no allocation. + let (vals, len) = match (rgb, nc) { + (true, 4) => ([straight(cacc[0]), straight(cacc[1]), straight(cacc[2]), a_clamped], 4), + (true, _) => ([straight(cacc[0]), straight(cacc[1]), straight(cacc[2]), 0.0], 3), + (false, 2) => ([straight(cacc[0]), a_clamped, 0.0, 0.0], 2), + (false, _) => ([straight(cacc[0]), 0.0, 0.0, 0.0], 1), + }; + for (k, v) in vals.into_iter().take(len).enumerate() { + if let Some(slot) = out.get_mut(base + k) { + *slot = v; + } + } + } + // Open on the full display-window stage when the file declares one: the renderer's data + // window is its crop, and the composition lives on the whole stage. The composed region + // pads to the stage at its offset, transparent elsewhere (limits validated the size). + let mut img = match deep.stage { + Some((sw, sh)) => { + // The display stage can be larger than the data window; admit it at the real f32 + // layout size before allocating, since the earlier deep-decode check used four + // bytes per pixel and did not know the flattened channel count. + limits.check_bytes(sw, sh, nc as u64 * 4)?; + let mut padded = vec![0f32; sw as usize * sh as usize * nc]; + let row_len = deep.width as usize * nc; + let dx = deep.origin.0.max(0) as usize; + for row in 0..deep.height as usize { + let src = row * row_len; + let dst = (row + deep.origin.1.max(0) as usize) * sw as usize + dx; + if let (Some(d), Some(r)) = (padded.get_mut(dst * nc..dst * nc + row_len), out.get(src..src + row_len)) { + d.copy_from_slice(r); + } + } + Image::from_f32(sw, sh, layout, &padded)? + } + None => Image::from_f32(deep.width, deep.height, layout, &out)?, + }; + if let Some((sw, sh)) = deep.stage { + img.warnings.push(DecodeWarning::DisplayWindowStage { stage: (sw, sh), origin: deep.origin }); + } + img.warnings.push(DecodeWarning::DeepFlattened { format: F, max_samples_per_pixel: max_samples }); + Ok(img) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write as _; + + #[test] + fn packed_reads_borrow_input_and_reject_bad_sizes_before_allocation() { + let bytes = [1u8, 2, 3, 4]; + let mut cur = Cursor::new(bytes.as_slice()); + let prefix = read_exact(&mut cur, 3, "x").expect("three input bytes"); + assert_eq!(prefix, &[1, 2, 3]); + assert_eq!(prefix.as_ptr(), bytes.as_ptr()); + assert_eq!(cur.position(), 3); + assert!(matches!(read_exact(&mut cur, usize::MAX, "x"), Err(CodecError::Malformed { .. }))); + assert_eq!(cur.position(), 3); + assert_eq!(read_exact(&mut cur, 1, "x").expect("last byte"), &[4]); + assert!(matches!(read_exact(&mut cur, 1, "x"), Err(CodecError::Malformed { .. }))); + cur.set_position(u64::MAX); + assert!(matches!(read_exact(&mut cur, 1, "x"), Err(CodecError::Malformed { .. }))); + } + + /// What the writer stores before compressing: the byte split, then the +128 predictor. + fn predicted(raw: &[u8]) -> Vec { + let half = raw.len().div_ceil(2); + let mut out = vec![0u8; raw.len()]; + for (i, &v) in raw.iter().enumerate() { + out[if i % 2 == 0 { i / 2 } else { half + i / 2 }] = v; + } + let mut prev = 0i16; + for (i, b) in out.iter_mut().enumerate() { + let v = i16::from(*b); + if i > 0 { + *b = (v - prev + 128) as u8; + } + prev = v; + } + out + } + + /// #4958: callers admit `expected` bytes for a decompressed buffer (twice that for a count + /// table, with the copy that undoes its byte split), so decompression must not reserve + /// more. `Vec`'s doubling gave a 4 MiB ZIPS table 8 MiB of capacity and a 3 MiB RLE table + /// 4 MiB, so the "admitted" table and its copy took up to three times the table size. + #[test] + fn decompressed_buffers_reserve_no_more_than_expected() { + for n in [1usize << 20, 2 << 20, (3 << 20) + 1, 4 << 20, 5 << 20] { + let raw = vec![0u8; n]; + let p = predicted(&raw); + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + z.write_all(&p).expect("write to a Vec"); + let zips = z.finish().expect("finish a Vec"); + assert!(decompress(Compression::ZIP1, &zips[..zips.len() - 1], n, "the table").is_err()); + // RLE: the first byte as a literal, then runs of up to 128 copies of the next. + let mut rle = vec![0xFF, p[0]]; + for run in p[1..].chunks(128) { + rle.extend_from_slice(&[(run.len() - 1) as u8, run[0]]); + } + for (compression, packed) in [(Compression::ZIP1, &zips), (Compression::RLE, &rle)] { + let out = decompress(compression, packed, n, "the table").expect("decompresses"); + assert_eq!(out, raw, "{compression:?}, {n} bytes"); + assert!(out.capacity() <= n, "{compression:?}: a {n} byte buffer reserved {} bytes", out.capacity()); + } + // One byte short, the same data expands past the expected size and is refused. + for (compression, packed) in [(Compression::ZIP1, &zips), (Compression::RLE, &rle)] { + assert!(decompress(compression, packed, n - 1, "the table").is_err(), "{compression:?}, {n} bytes"); + } + } + } + + /// A size past the limit is malformed data, and a reservation the allocator refuses is an + /// error rather than an abort. + #[test] + fn reservations_past_the_limit_or_the_allocator_are_errors() { + let mut v = Vec::new(); + assert!(matches!(reserve_up_to(&mut v, 2, 1, "x"), Err(CodecError::Malformed { .. }))); + assert!(matches!(reserve_up_to(&mut v, isize::MAX as usize + 1, usize::MAX, "x"), Err(CodecError::LimitExceeded(_)))); + assert_eq!(v.capacity(), 0); + } +} diff --git a/crates/image-codecs/src/codecs/dicom.rs b/crates/image-codecs/src/codecs/dicom.rs new file mode 100644 index 000000000..ea2674dae --- /dev/null +++ b/crates/image-codecs/src/codecs/dicom.rs @@ -0,0 +1,517 @@ +//! DICOM (medical imaging) reader, uncompressed transfer syntaxes only. +//! +//! Supports what a viewer needs from a single-frame image: MONOCHROME1/2 (gray) and RGB or +//! YBR_FULL, 8- and 16-bit samples, interleaved or planar, both byte orders and explicit or +//! implicit VR. Undefined-length sequences outside the pixel data are skipped (clinical files +//! contain them); compressed transfer syntaxes, encapsulated pixel data and other photometric +//! interpretations are refused with a clear error. Read-only in this crate. + +use std::collections::HashMap; + +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Dicom; +const PREAMBLE: usize = 132; // 128 zero bytes + "DICM" +const UNDEFINED: u32 = 0xFFFF_FFFF; + +fn bad(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +/// A DICOM file begins with a 128-byte preamble and the `DICM` magic. +pub(crate) fn is_dicom(b: &[u8]) -> bool { + b.get(128..132) == Some(b"DICM") +} + +#[derive(Clone, Copy)] +struct Endian { + big: bool, +} + +/// The transfer syntax: byte order and whether value representations are explicit. +#[derive(Clone, Copy)] +struct Syntax { + endian: Endian, + explicit: bool, +} + +/// The value representations whose length field is 2 reserved bytes + `u32` in explicit VR. +fn long_vr(vr: &[u8]) -> bool { + matches!(vr, b"OB" | b"OW" | b"OF" | b"SQ" | b"UT" | b"UN" | b"OD" | b"OL" | b"OV" | b"UC" | b"UR") +} + +/// A cursor over the byte stream. +struct Cur<'a> { + b: &'a [u8], + pos: usize, + big: bool, +} + +impl<'a> Cur<'a> { + fn take(&mut self, n: usize) -> Option<&'a [u8]> { + let s = self.b.get(self.pos..self.pos.checked_add(n)?)?; + self.pos += n; + Some(s) + } + fn u16(&mut self) -> Option { + let c = self.take(2)?; + Some(if self.big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }) + } + fn u32(&mut self) -> Option { + let c = self.take(4)?; + Some(if self.big { u32::from_be_bytes([c[0], c[1], c[2], c[3]]) } else { u32::from_le_bytes([c[0], c[1], c[2], c[3]]) }) + } + /// Skip `len` bytes, refusing to run past the buffer. + fn skip(&mut self, len: usize) -> Result<(), CodecError> { + self.pos = self.pos.checked_add(len).filter(|p| *p <= self.b.len()).ok_or_else(|| bad("a value runs past the end of the file"))?; + Ok(()) + } +} + +/// Read one element's tag and value length, leaving the cursor at the value. Item and delimiter +/// tags (group `FFFE`) carry a `u32` length with no VR even under explicit VR. +fn read_header(c: &mut Cur, syn: &Syntax) -> Result<(u16, u16, [u8; 2], u32), CodecError> { + let group = c.u16().ok_or_else(|| bad("truncated element tag"))?; + let elem = c.u16().ok_or_else(|| bad("truncated element tag"))?; + let (vr, len) = if group == 0xFFFE { + ([0, 0], c.u32().ok_or_else(|| bad("truncated item length"))?) + } else if syn.explicit { + let vr = c.take(2).ok_or_else(|| bad("truncated VR"))?; + let v = [vr[0], vr[1]]; + let len = if long_vr(&v) { + c.take(2).ok_or_else(|| bad("truncated VR"))?; // reserved + c.u32().ok_or_else(|| bad("truncated length"))? + } else { + u32::from(c.u16().ok_or_else(|| bad("truncated length"))?) + }; + (v, len) + } else { + ([0, 0], c.u32().ok_or_else(|| bad("truncated length"))?) + }; + Ok((group, elem, vr, len)) +} + +/// Skip an undefined-length sequence, ending just after its `FFFE,E0DD` delimiter. Nesting is +/// tracked through undefined-length items and nested sequences; everything else is skipped by +/// its length. Returns the position after the delimiter. +fn skip_undefined(c: &mut Cur, syn: &Syntax) -> Result<(), CodecError> { + let mut depth = 1u32; + while depth > 0 { + let (group, elem, _, len) = read_header(c, syn)?; + // Item (E00D) and sequence (E0DD) delimiters close one undefined length; they carry no + // content of their own. + if group == 0xFFFE && matches!(elem, 0xE00D | 0xE0DD) { + depth -= 1; + continue; + } + if len == UNDEFINED { + depth += 1; + } else { + c.skip(len as usize)?; + } + } + Ok(()) +} + +/// Walk the elements from `start`, calling `f` with each `(group, element, value)`. An +/// undefined-length sequence outside the pixel data is skipped; encapsulated (undefined-length) +/// pixel data is refused. Returns the number of bytes walked. +fn elements(b: &[u8], start: usize, syn: &Syntax, mut f: impl FnMut(u16, u16, &Elem) -> Result<(), CodecError>) -> Result { + let mut c = Cur { b, pos: start, big: syn.endian.big }; + loop { + if c.pos >= b.len() { + return Ok(c.pos - start); + } + let (group, elem, vr, len) = read_header(&mut c, syn)?; + if len == UNDEFINED { + if (group, elem) == (0x7FE0, 0x0010) { + return Err(CodecError::unsupported(F, "encapsulated (compressed) pixel data")); + } + skip_undefined(&mut c, syn)?; + continue; + } + let len = len as usize; + let start = c.pos; + if b.len() < start.saturating_add(len) { + return Err(bad("an element value runs past the end of the file")); + } + f(group, elem, &Elem { vr, start, len })?; + c.pos = start + len; + } +} + +/// A parsed element: its VR and the byte range of its value. +struct Elem { + vr: [u8; 2], + start: usize, + len: usize, +} + +fn u16_val(b: &[u8], e: &Elem, big: bool) -> Option { + let c = b.get(e.start..e.start.checked_add(2)?)?; + Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }) +} + +/// The transfer syntax (byte order and explicit/implicit VR) and the data-set start. The file +/// meta group is always explicit VR little-endian; the data set follows its `(0002,0010)` +/// TransferSyntaxUID. +fn transfer_syntax(b: &[u8]) -> Result<(Syntax, usize), CodecError> { + let meta = Syntax { endian: Endian { big: false }, explicit: true }; + let mut ts: Option = None; + let mut data_start = b.len(); + let mut c = Cur { b, pos: PREAMBLE, big: false }; + while c.pos + 8 <= b.len() { + let save = c.pos; + let (group, elem, _, len) = read_header(&mut c, &meta)?; + if group != 0x0002 { + // A non-meta group begins the data set. + data_start = save; + break; + } + if len == UNDEFINED { + return Err(CodecError::unsupported(F, "undefined-length meta element")); + } + let start = c.pos; + if b.len() < start.saturating_add(len as usize) { + return Err(bad("a meta element runs past the end of the file")); + } + if elem == 0x0010 { + ts = Some(String::from_utf8_lossy(&b[start..start + len as usize]).trim_end_matches(['\0', ' ']).to_string()); + } + c.pos = start + len as usize; + } + let syn = match ts.as_deref() { + Some("1.2.840.10008.1.2") => Syntax { endian: Endian { big: false }, explicit: false }, + Some("1.2.840.10008.1.2.1") => Syntax { endian: Endian { big: false }, explicit: true }, + Some("1.2.840.10008.1.2.2") => Syntax { endian: Endian { big: true }, explicit: true }, + Some(other) => return Err(CodecError::unsupported(F, format!("transfer syntax {other} (only uncompressed is read)"))), + // No meta: the historical default is implicit VR little-endian. + None => Syntax { endian: Endian { big: false }, explicit: false }, + }; + Ok((syn, data_start)) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !is_dicom(b) { + return Err(bad("not a DICOM file (no DICM magic)")); + } + let (syn, start) = transfer_syntax(b)?; + let mut tags: HashMap<(u16, u16), Elem> = HashMap::new(); + elements(b, start, &syn, |g, e, el| { + // Later duplicates must not silently override the first (DICOM forbids duplicates). + tags.entry((g, e)).or_insert_with(|| Elem { vr: el.vr, start: el.start, len: el.len }); + Ok(()) + })?; + let big = syn.endian.big; + let get_u16 = |tag: (u16, u16)| tags.get(&tag).and_then(|e| u16_val(b, e, big)); + let rows = u32::from(get_u16((0x0028, 0x0010)).ok_or_else(|| bad("missing Rows (0028,0010)"))?); + let cols = u32::from(get_u16((0x0028, 0x0011)).ok_or_else(|| bad("missing Columns (0028,0011)"))?); + let samples = u32::from(get_u16((0x0028, 0x0002)).unwrap_or(1)); + let bits = get_u16((0x0028, 0x0100)).ok_or_else(|| bad("missing BitsAllocated (0028,0100)"))?; + let bits_stored = get_u16((0x0028, 0x0101)).unwrap_or(bits); + let high_bit = get_u16((0x0028, 0x0102)).unwrap_or(bits_stored.saturating_sub(1)); + if u32::from(high_bit) >= u32::from(bits) { + return Err(bad("HighBit is outside the allocated bits")); + } + let signed = get_u16((0x0028, 0x0103)).unwrap_or(0) == 1; + let planar = get_u16((0x0028, 0x0006)).unwrap_or(0); + let photometric = tags + .get(&(0x0028, 0x0004)) + .and_then(|e| b.get(e.start..e.start.checked_add(e.len)?)) + .map(|v| String::from_utf8_lossy(v).trim_end_matches(['\0', ' ']).to_string()) + .ok_or_else(|| bad("missing PhotometricInterpretation (0028,0004)"))?; + let pixel = tags.get(&(0x7FE0, 0x0010)).ok_or_else(|| CodecError::unsupported(F, "no pixel data (7FE0,0010)"))?; + if !matches!(bits, 8 | 16) || !matches!(bits_stored, 1..=16) { + return Err(CodecError::unsupported(F, format!("{bits}-bit allocated / {bits_stored}-bit stored samples"))); + } + let mono = matches!(photometric.as_str(), "MONOCHROME1" | "MONOCHROME2"); + let rgb = photometric == "RGB"; + let ybr = photometric == "YBR_FULL"; + if !(mono || rgb || ybr) { + return Err(CodecError::unsupported(F, format!("photometric interpretation {photometric}"))); + } + // Mono stores one sample; RGB and YBR_FULL store three. Any other count would index past the + // pixel data, so it is refused rather than misread. + let want_samples = if mono { 1 } else { 3 }; + if samples != want_samples { + return Err(CodecError::unsupported(F, format!("{samples} samples per pixel with {photometric}"))); + } + if ybr && bits != 8 { + return Err(CodecError::unsupported(F, format!("{bits}-bit {photometric} (8-bit only)"))); + } + let layout = if mono { ChannelLayout::Gray } else { ChannelLayout::Rgb }; + let out_sample = if bits == 8 { SampleType::U8 } else { SampleType::U16 }; + limits.check(cols, rows, layout, out_sample)?; + + let pixels = (cols as usize).checked_mul(rows as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let n = pixels.checked_mul(samples as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let bytes_per = if bits == 16 { 2usize } else { 1 }; + let need = n.checked_mul(bytes_per).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?; + let data = b + .get(pixel.start..pixel.start.checked_add(need).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?) + .ok_or_else(|| bad("pixel data is truncated"))?; + let out_channels = layout.channels(); + let out_len = pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let mut out = Vec::new(); + out.try_reserve_exact(out_len).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; + // Raw sample of channel `ch` at pixel `p`, honouring the planar configuration. + let sample_at = |p: usize, ch: usize| -> Option { + let idx = if planar == 1 { ch.checked_mul(pixels)?.checked_add(p)? } else { p.checked_mul(samples as usize)?.checked_add(ch)? }; + let off = idx.checked_mul(bytes_per)?; + if bits == 16 { + let c = data.get(off..off.checked_add(2)?)?; + Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }.into()) + } else { + Some(u32::from(*data.get(off)?)) + } + }; + let stored_max = f64::from((1u32 << bits_stored) - 1); + // Extract the `bits_stored` samples (the least significant one at `high_bit`) and scale to + // [0, 1]; signed values are shifted up by half a range first. `high_bit` is 1-based here. + let shift = (u32::from(high_bit) + 1).saturating_sub(u32::from(bits_stored)); + let mask = (1u32 << bits_stored) - 1; + let half = 1u32 << (bits_stored - 1); + let norm = move |raw: u32| -> f64 { + let v = (raw >> shift) & mask; + // Signed: flip the sign bit to map [-2^(b-1), 2^(b-1)-1] onto [0, 2^b-1]. + let v = if signed { v ^ half } else { v }; + (f64::from(v) / stored_max).clamp(0.0, 1.0) + }; + let q = |x: f64| -> u16 { (x * 65535.0).round() as u16 }; + let push = |out: &mut Vec, x: f64| { + if bits == 8 { + out.push((x * 255.0).round() as u8); + } else { + out.extend_from_slice(&q(x).to_ne_bytes()); + } + }; + for p in 0..pixels { + let s = |ch: usize| sample_at(p, ch).ok_or_else(|| bad("pixel data is shorter than the image")); + if mono { + let x = norm(s(0)?); + push(&mut out, if photometric == "MONOCHROME1" { 1.0 - x } else { x }); + } else if rgb { + for ch in 0..3 { + push(&mut out, norm(s(ch)?)); + } + } else { + // YBR_FULL (8-bit): BT.601 full-range to RGB. + let y = f64::from(s(0)?); + let cb = f64::from(s(1)?) - 128.0; + let cr = f64::from(s(2)?) - 128.0; + let r = (y + 1.402 * cr).clamp(0.0, 255.0); + let g = (y - 0.344_136 * cb - 0.714_136 * cr).clamp(0.0, 255.0); + let bl = (y + 1.772 * cb).clamp(0.0, 255.0); + for v in [r, g, bl] { + push(&mut out, v / 255.0); + } + } + } + Image::from_raw(cols, rows, layout, out_sample, out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Build the file meta group (explicit VR little-endian), including the given extra + /// `(element, ascii)` pairs before the TransferSyntaxUID. + fn meta(b: &mut Vec, ts: &str, extra: &[(u16, &str)]) { + let ui = |e: u16, v: &str, b: &mut Vec| { + let mut s = v.as_bytes().to_vec(); + if s.len() % 2 == 1 { + s.push(0); + } + b.extend_from_slice(&0x0002u16.to_le_bytes()); + b.extend_from_slice(&e.to_le_bytes()); + b.extend_from_slice(b"UI"); + b.extend_from_slice(&(s.len() as u16).to_le_bytes()); + b.extend_from_slice(&s); + }; + for (e, v) in extra { + ui(*e, v, b); + } + ui(0x0010, ts, b); + } + + fn us(b: &mut Vec, g: u16, e: u16, v: u16, explicit: bool) { + b.extend_from_slice(&g.to_le_bytes()); + b.extend_from_slice(&e.to_le_bytes()); + if explicit { + b.extend_from_slice(b"US"); + b.extend_from_slice(&2u16.to_le_bytes()); + } else { + b.extend_from_slice(&2u32.to_le_bytes()); + } + b.extend_from_slice(&v.to_le_bytes()); + } + + fn cs(b: &mut Vec, g: u16, e: u16, v: &str, explicit: bool) { + let mut s = v.as_bytes().to_vec(); + if s.len() % 2 == 1 { + s.push(0); + } + b.extend_from_slice(&g.to_le_bytes()); + b.extend_from_slice(&e.to_le_bytes()); + if explicit { + b.extend_from_slice(b"CS"); + b.extend_from_slice(&(s.len() as u16).to_le_bytes()); + } else { + b.extend_from_slice(&(s.len() as u32).to_le_bytes()); + } + b.extend_from_slice(&s); + } + + /// A DICOM with the given transfer syntax UID and data-set encoding. + #[allow(clippy::too_many_arguments)] + fn synth(ts: &str, explicit: bool, big: bool, planar: u16, rows: u16, cols: u16, samples: u16, bits: u16, photometric: &str, pixels: &[u8]) -> Vec { + let mut b = vec![0u8; 128]; + b.extend_from_slice(b"DICM"); + // One extra meta element before the syntax, so a reader that only looks at the last + // group-0002 element takes the wrong one. + meta(&mut b, ts, &[(0x0002, "1.2.840.10008.5.1.4.1.1.7")]); + let _ = big; + us(&mut b, 0x0028, 0x0010, rows, explicit); + us(&mut b, 0x0028, 0x0011, cols, explicit); + us(&mut b, 0x0028, 0x0002, samples, explicit); + us(&mut b, 0x0028, 0x0006, planar, explicit); + cs(&mut b, 0x0028, 0x0004, photometric, explicit); + us(&mut b, 0x0028, 0x0100, bits, explicit); + us(&mut b, 0x0028, 0x0101, bits, explicit); + us(&mut b, 0x0028, 0x0102, bits - 1, explicit); + us(&mut b, 0x0028, 0x0103, 0, explicit); + b.extend_from_slice(&0x7FE0u16.to_le_bytes()); + b.extend_from_slice(&0x0010u16.to_le_bytes()); + if explicit { + b.extend_from_slice(b"OB"); + b.extend_from_slice(&0u16.to_le_bytes()); + } + b.extend_from_slice(&(pixels.len() as u32).to_le_bytes()); + b.extend_from_slice(pixels); + b + } + + #[test] + fn decodes_8bit_monochrome() { + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (2, 2, ChannelLayout::Gray, SampleType::U8)); + assert_eq!(img.data(), &[0, 51, 102, 255]); + } + + #[test] + fn decodes_16bit_monochrome_and_scales() { + let px: Vec = [0u16, 32768, 65535].iter().flat_map(|v| v.to_le_bytes()).collect(); + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 3, 1, 1, 16, "MONOCHROME2", &px); + let img = decode(&b, &Limits::default()).unwrap(); + let v = img.to_u16_samples().unwrap(); + assert_eq!(v[0], 0); + assert!((i32::from(v[1]) - 32768).abs() <= 1); + assert_eq!(v[2], 65535); + } + + #[test] + fn monochrome1_is_inverted() { + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 1, 1, 8, "MONOCHROME1", &[0, 255]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[255, 0]); + } + + #[test] + fn decodes_rgb_interleaved_and_planar() { + // 2 pixels: (10,20,30) and (40,50,60). + let interleaved = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 2, 3, 8, "RGB", &[10, 20, 30, 40, 50, 60]); + let img = decode(&interleaved, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10u8, 20, 30, 40, 50, 60][..])); + // Planar 0: R plane, then G, then B. + let planar = synth("1.2.840.10008.1.2.1", true, false, 1, 1, 2, 3, 8, "RGB", &[10, 40, 20, 50, 30, 60]); + let img = decode(&planar, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[10, 20, 30, 40, 50, 60]); + } + + #[test] + fn decodes_ybr_full_to_rgb() { + // White (Y=128, Cb=Cr=128) stays neutral grey. + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 3, 8, "YBR_FULL", &[128, 128, 128]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgb); + let d = img.data(); + assert!(d[0].abs_diff(d[1]) <= 1 && d[1].abs_diff(d[2]) <= 1, "{d:?}"); + } + + #[test] + fn decodes_implicit_vr() { + let b = synth("1.2.840.10008.1.2", false, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &[0, 51, 102, 255]); + } + + #[test] + fn skips_an_undefined_length_sequence_outside_the_pixel_data() { + // Insert an undefined-length SQ (0008,2218) with one item, then a defined-length one, + // before the pixel data: the reader must skip both and still decode the image. + let mut b = vec![0u8; 128]; + b.extend_from_slice(b"DICM"); + meta(&mut b, "1.2.840.10008.1.2.1", &[]); + for (g, e, v) in [ + (0x0028, 0x0010, 1u16), + (0x0028, 0x0011, 1), + (0x0028, 0x0002, 1), + (0x0028, 0x0100, 8), + (0x0028, 0x0101, 8), + (0x0028, 0x0102, 7), + (0x0028, 0x0103, 0), + ] { + us(&mut b, g, e, v, true); + } + cs(&mut b, 0x0028, 0x0004, "MONOCHROME2", true); + // Undefined-length sequence (0008,2211), one undefined-length item containing a nested + // defined-length sequence, then the delimiters. + b.extend_from_slice(&[0x08, 0x00, 0x11, 0x22, b'S', b'Q', 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF]); + b.extend_from_slice(&[0xFE, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF]); // item, undefined + b.extend_from_slice(&[0x08, 0x00, 0x55, 0x22, b'S', b'Q', 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 1, 2, 3, 4]); // nested, defined + b.extend_from_slice(&[0xFE, 0xFF, 0x0D, 0xE0, 0x00, 0x00, 0x00, 0x00]); // item delimitation + b.extend_from_slice(&[0xFE, 0xFF, 0xDD, 0xE0, 0x00, 0x00, 0x00, 0x00]); // sequence delimitation + // Pixel data (defined length). + b.extend_from_slice(&0x7FE0u16.to_le_bytes()); + b.extend_from_slice(&0x0010u16.to_le_bytes()); + b.extend_from_slice(b"OB"); + b.extend_from_slice(&0u16.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.push(200); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[200]); + } + + #[test] + fn truncated_and_bad_is_error_not_panic() { + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 1, 2, 3]); + for cut in [0, 4, 128, 132, 140, b.len() - 1] { + assert!(decode(&b[..cut.min(b.len())], &Limits::default()).is_err(), "cut {cut}"); + } + assert!(decode(b"not dicom", &Limits::default()).is_err()); + } + + #[test] + fn high_bit_outside_the_allocated_bits_is_an_error_not_a_panic() { + let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[7]); + let tag = [0x28, 0x00, 0x02, 0x01, b'U', b'S', 0x02, 0x00]; + let at = b.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len(); + b[at..at + 2].copy_from_slice(&200u16.to_le_bytes()); + assert!(decode(&b, &Limits::default()).is_err()); + } + + #[test] + fn rejects_compressed_transfer_syntax() { + let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[0]); + let uncompressed = b"1.2.840.10008.1.2.1\0"; + let compressed = b"1.2.840.10008.1.2.4\0"; // same length, a compressed family + let at = b.windows(uncompressed.len()).position(|w| w == uncompressed).expect("transfer syntax in the fixture"); + b[at..at + compressed.len()].copy_from_slice(compressed); + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/codecs/dpx.rs b/crates/image-codecs/src/codecs/dpx.rs new file mode 100644 index 000000000..86e4938b0 --- /dev/null +++ b/crates/image-codecs/src/codecs/dpx.rs @@ -0,0 +1,886 @@ +//! DPX (SMPTE 268M Digital Picture Exchange), implemented here from the published layout: the +//! 1664-byte generic header (file and image information, image source), the 384-byte industry +//! header (film and television), then the image data of each element. +//! +//! Reading: +//! +//! * Both byte orders: `SDPX` (big-endian) and `XPDS` (little-endian). +//! * The first image element, descriptor 6 (luma), 50 (RGB) or 51 (RGBA), unsigned, without +//! run-length encoding, at 8, 10, 12 or 16 bits. 10-bit samples are packed into 32-bit words +//! (packing 0), or three to a word with the padding bits at the bottom (1, method A) or the +//! top (2, method B); 12-bit samples are packed (0) or one to a 16-bit word in its top bits +//! (1, method A). Lines start on a 32-bit word boundary. +//! * 8- and 16-bit code values become U8 and U16 samples unchanged; 10- and 12-bit ones become U16 +//! as `round(c × 65535 / (2^b − 1))`, so they come back exactly when written at their depth. +//! * No colour transform: the transfer characteristic and colorimetric specification are reported +//! ([`DecodeWarning::DpxEncoding`]), so a log frame is labelled log, not shown as display-referred. +//! * The image orientation is applied on import (the caller turns the image upright). +//! * Header fields a pipeline relies on travel as text metadata under the [`KEYS`] names, and a +//! DPX export writes them back. +//! +//! Writing: one element, big-endian, unencoded, descriptor 6, 50 or 51; 8 and 16 bits with +//! packing 0, 10 and 12 bits with packing 1 (method A). The bit depth follows the image +//! (8 → 8, 16 → 16) unless [`EncodeOptions::dpx_bits`] picks one. +//! +//! Kodak Cineon (`.cin`), the format DPX grew from, is read and written here too (see its section). + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType}; +use crate::options::{DpxBits, EncodeOptions, Limits}; + +const F: Format = Format::Dpx; +/// Generic plus industry header: where our image data starts. +const HEADER_LEN: usize = 2048; +const GENERIC_LEN: u32 = 1664; +const INDUSTRY_LEN: u32 = 384; +/// "Undefined" for 32-bit fields (also the bit pattern of an undefined float). +const UNDEF32: u32 = u32::MAX; +const UNDEF8: u8 = u8::MAX; +/// Offset of the first image element and the size of one. +const ELEMENT0: usize = 780; +const ELEMENT_LEN: usize = 72; +const MAX_ELEMENTS: u16 = 8; + +// Header field offsets (generic file header, image information, film and TV headers). +const CREATION_TIME: (usize, usize) = (136, 24); +const CREATOR: (usize, usize) = (160, 100); +const PROJECT: (usize, usize) = (260, 200); +const COPYRIGHT: (usize, usize) = (460, 200); +const ORIENTATION: usize = 768; +const FRAME_POSITION: usize = 1712; +const FRAME_RATE: usize = 1724; +const TIME_CODE: usize = 1920; + +/// Text metadata keys of the header fields kept through a DPX → document → DPX round trip. +pub(crate) const KEY_CREATOR: &str = "DPX:Creator"; +pub(crate) const KEY_PROJECT: &str = "DPX:Project"; +pub(crate) const KEY_COPYRIGHT: &str = "DPX:Copyright"; +pub(crate) const KEY_CREATION_TIME: &str = "DPX:CreationTime"; +pub(crate) const KEY_TRANSFER: &str = "DPX:Transfer"; +pub(crate) const KEY_COLORIMETRIC: &str = "DPX:Colorimetric"; +pub(crate) const KEY_FRAME_POSITION: &str = "DPX:FramePosition"; +pub(crate) const KEY_FRAME_RATE: &str = "DPX:FrameRate"; +pub(crate) const KEY_TIME_CODE: &str = "DPX:TimeCode"; +pub(crate) const KEYS: [&str; 9] = + [KEY_CREATOR, KEY_PROJECT, KEY_COPYRIGHT, KEY_CREATION_TIME, KEY_TRANSFER, KEY_COLORIMETRIC, KEY_FRAME_POSITION, KEY_FRAME_RATE, KEY_TIME_CODE]; + +fn err(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +fn unsupported(msg: impl Into) -> CodecError { + CodecError::unsupported(F, msg) +} + +/// The file's byte order. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Order { + Big, + Little, +} + +impl Order { + fn bytes(b: &[u8], at: usize) -> Option<[u8; N]> { + b.get(at..at.checked_add(N)?)?.try_into().ok() + } + + fn u16(self, b: &[u8], at: usize) -> Option { + let s = Self::bytes::<2>(b, at)?; + Some(if self == Order::Big { u16::from_be_bytes(s) } else { u16::from_le_bytes(s) }) + } + + fn u32(self, b: &[u8], at: usize) -> Option { + let s = Self::bytes::<4>(b, at)?; + Some(if self == Order::Big { u32::from_be_bytes(s) } else { u32::from_le_bytes(s) }) + } + + /// A 32-bit field that may be undefined (all ones). + fn defined_u32(self, b: &[u8], at: usize) -> Option { + self.u32(b, at).filter(|&v| v != UNDEF32) + } + + fn f32(self, b: &[u8], at: usize) -> Option { + self.defined_u32(b, at).map(f32::from_bits).filter(|v| v.is_finite()) + } +} + +/// The byte order from the magic number, `None` when it is not DPX. +fn order(b: &[u8]) -> Option { + match b.get(..4)? { + b"SDPX" => Some(Order::Big), + b"XPDS" => Some(Order::Little), + _ => None, + } +} + +/// An ASCII header string: up to the first NUL, printable characters only, trimmed. `None` when +/// empty or undefined (all ones). +fn string(b: &[u8], (at, len): (usize, usize)) -> Option { + let raw = b.get(at..at.checked_add(len)?)?; + let end = raw.iter().position(|&c| c == 0).unwrap_or(raw.len()); + let s: String = raw.get(..end)?.iter().filter(|c| (32..=126).contains(*c)).map(|&c| c as char).collect(); + Some(s.trim().to_string()).filter(|s| !s.is_empty()) +} + +/// The descriptive fields of image element 0 that decoding needs. +#[derive(Debug, Clone, Copy)] +struct Element { + signed: bool, + descriptor: u8, + transfer: u8, + colorimetric: u8, + bits: u8, + packing: u16, + encoding: u16, + data_offset: usize, + eol_padding: usize, +} + +struct Header { + order: Order, + width: u32, + height: u32, + elements: u16, + element: Element, +} + +/// The decoder opens only element zero; protect the other declared elements from flat saves. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + Ok(parse(bytes)?.elements > 1) +} + +fn parse(b: &[u8]) -> Result { + let o = order(b).ok_or_else(|| err("missing SDPX/XPDS magic number"))?; + let missing = |what: &str| err(format!("the header ends before {what}")); + let image_offset = o.u32(b, 4).ok_or_else(|| missing("the image data offset"))?; + o.u16(b, ORIENTATION).ok_or_else(|| missing("the orientation"))?; + let elements = o.u16(b, ORIENTATION + 2).ok_or_else(|| missing("the element count"))?; + if !(1..=MAX_ELEMENTS).contains(&elements) { + return Err(err(format!("invalid number of image elements {elements}"))); + } + let width = o.u32(b, 772).ok_or_else(|| missing("the line length"))?; + let height = o.u32(b, 776).ok_or_else(|| missing("the line count"))?; + let e = ELEMENT0; + let field = |at: usize| o.u32(b, e + at).ok_or_else(|| missing("image element 1")); + let byte = |at: usize| b.get(e + at).copied().ok_or_else(|| missing("image element 1")); + let half = |at: usize| o.u16(b, e + at).ok_or_else(|| missing("image element 1")); + let data_offset = match field(28)? { + 0 | UNDEF32 => image_offset, + v => v, + }; + let eol_padding = match field(32)? { + UNDEF32 => 0, + v => v, + }; + let element = Element { + signed: field(0)? == 1, + descriptor: byte(20)?, + transfer: byte(21)?, + colorimetric: byte(22)?, + bits: byte(23)?, + packing: half(24)?, + encoding: half(26)?, + data_offset: usize::try_from(data_offset).map_err(|_| err("image data offset out of range"))?, + eol_padding: usize::try_from(eol_padding).map_err(|_| err("line padding out of range"))?, + }; + Ok(Header { order: o, width, height, elements, element }) +} + +/// The EXIF orientation (1–8) that turns the stored lines upright: the DPX orientation codes 0–7 +/// in SMPTE 268M order (left-to-right top-to-bottom, mirrored, flipped, rotated, transposed…). +/// Anything else, undefined included, is taken as the default (1). +pub(crate) fn orientation(b: &[u8]) -> u16 { + let Some(o) = order(b).and_then(|o| o.u16(b, ORIENTATION)) else { return 1 }; + match o { + 1 => 2, + 2 => 4, + 3 => 3, + 4 => 5, + 5 => 6, + 6 => 8, + 7 => 7, + _ => 1, + } +} + +/// Bytes in one stored line of `samples` samples, padded to a 32-bit word. +fn line_bytes(samples: usize, bits: u8, packing: u16) -> Option { + let words = match (bits, packing) { + // Three samples to a word, or one to a 16-bit half word. + (10, 1 | 2) => samples.div_ceil(3), + (12, 1 | 2) => samples.div_ceil(2), + _ => samples.checked_mul(usize::from(bits))?.div_ceil(32), + }; + words.checked_mul(4) +} + +/// Code values of one stored line. +fn unpack_line(line: &[u8], o: Order, bits: u8, packing: u16, out: &mut [u16]) -> Result<(), CodecError> { + let short = || err("truncated image data"); + match (bits, packing) { + (8, _) => { + let src = line.get(..out.len()).ok_or_else(short)?; + for (v, &c) in out.iter_mut().zip(src) { + *v = u16::from(c); + } + } + (16, _) => { + for (i, v) in out.iter_mut().enumerate() { + *v = o.u16(line, i * 2).ok_or_else(short)?; + } + } + (10, 1 | 2) => { + let base = if packing == 1 { 22 } else { 20 }; + for (i, v) in out.iter_mut().enumerate() { + let word = o.u32(line, i / 3 * 4).ok_or_else(short)?; + *v = ((word >> (base - 10 * (i % 3) as u32)) & 0x3FF) as u16; + } + } + (12, 1 | 2) => { + let shift = if packing == 1 { 4 } else { 0 }; + for (i, v) in out.iter_mut().enumerate() { + *v = (o.u16(line, i * 2).ok_or_else(short)? >> shift) & 0xFFF; + } + } + // Packing 0: one bit stream through the line's 32-bit words, high bits first. + _ => { + let mask = (1u64 << bits) - 1; + let (mut acc, mut have, mut word) = (0u64, 0u32, 0usize); + for v in out.iter_mut() { + while have < u32::from(bits) { + acc = (acc << 32) | u64::from(o.u32(line, word * 4).ok_or_else(short)?); + word += 1; + have += 32; + } + have -= u32::from(bits); + *v = ((acc >> have) & mask) as u16; + } + } + } + Ok(()) +} + +/// A 10- or 12-bit code value as a 16-bit sample: `round(c × 65535 / max)`. +fn widen(c: u16, max: u64) -> u16 { + ((u64::from(c) * 2 * 65535 + max) / (2 * max)).min(65535) as u16 +} + +/// The inverse of [`widen`]: `round(v × max / 65535)`. +fn narrow(v: u16, max: u64) -> u16 { + ((u64::from(v) * 2 * max + 65535) / (2 * 65535)) as u16 +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let h = parse(bytes)?; + let e = h.element; + let layout = match e.descriptor { + 6 => ChannelLayout::Gray, + 50 => ChannelLayout::Rgb, + 51 => ChannelLayout::Rgba, + d => return Err(unsupported(format!("descriptor {d} is not supported (only 6 luma, 50 RGB and 51 RGBA)"))), + }; + if e.signed { + return Err(unsupported("data sign 1 (signed samples) is not supported")); + } + if e.encoding != 0 { + return Err(unsupported(format!("encoding {} (run-length encoded data) is not supported", e.encoding))); + } + let packing_ok = match e.bits { + 8 | 16 => e.packing == 0, + 10 => e.packing <= 2, + 12 => e.packing <= 1, + b => return Err(unsupported(format!("bit size {b} is not supported (only 8, 10, 12 and 16)"))), + }; + if !packing_ok { + return Err(unsupported(format!("packing {} is not supported at {} bits", e.packing, e.bits))); + } + let sample = if e.bits == 8 { SampleType::U8 } else { SampleType::U16 }; + limits.check(h.width, h.height, layout, sample)?; + + let nc = layout.channels(); + let per_line = (h.width as usize).checked_mul(nc).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?; + let height = h.height as usize; + let data = bytes.get(e.data_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?; + let too_large = || CodecError::LimitExceeded("image too large".into()); + let mut line_len = line_bytes(per_line, e.bits, e.packing).ok_or_else(too_large)?; + let mut stride = line_len.checked_add(e.eol_padding).ok_or_else(too_large)?; + // Lines run `stride` apart; the last one needs no padding after it. + let span = |stride: usize, line_len: usize| stride.checked_mul(height - 1)?.checked_add(line_len); + // Some writers pack 8- and 16-bit lines without padding them to a word; take that layout + // when the file is too short for padded lines but holds the unpadded ones. + let tight = per_line * usize::from(e.bits / 8); + if matches!(e.bits, 8 | 16) && e.eol_padding == 0 && line_len != tight && span(stride, line_len).is_none_or(|need| data.len() < need) { + (line_len, stride) = (tight, tight); + } + if span(stride, line_len).ok_or_else(too_large)? > data.len() { + return Err(err("truncated image data")); + } + + let mut codes = vec![0u16; per_line.checked_mul(height).ok_or_else(too_large)?]; + for (y, row) in codes.chunks_exact_mut(per_line.max(1)).enumerate() { + let start = y * stride; + let line = data.get(start..).ok_or_else(|| err("truncated image data"))?; + unpack_line(line, h.order, e.bits, e.packing, row)?; + } + + let mut img = match e.bits { + 8 => Image::from_u8(h.width, h.height, layout, codes.iter().map(|&c| c as u8).collect())?, + 16 => Image::from_u16(h.width, h.height, layout, &codes)?, + b => { + let max = (1u64 << b) - 1; + let wide: Vec = codes.iter().map(|&c| widen(c, max)).collect(); + Image::from_u16(h.width, h.height, layout, &wide)? + } + }; + if h.elements > 1 { + img.warnings.push(DecodeWarning::MoreElements { total: u32::from(h.elements) }); + } + if e.transfer != UNDEF8 || e.colorimetric != UNDEF8 { + img.warnings.push(DecodeWarning::DpxEncoding { transfer: e.transfer, colorimetric: e.colorimetric }); + } + img.meta.text = header_text(bytes, h.order, e); + Ok(img) +} + +// ---------------------------------------------------------------- Cineon (Kodak .cin) +// +// Cineon is the 10-bit film-scan format DPX grew from (same Kodak header family, big-endian). +// The generic header is 1024 bytes: the file information fields, then at offset 192 the image +// orientation (1 byte), the channel count (1 byte) and up to eight 28-byte channel descriptors +// (the first carries the bits per pixel and the line/image dimensions). Image data is three +// 10-bit samples packed into each 32-bit word, lines on a word boundary — the same layout as +// DPX's 10-bit packing method A, so the unpacking and packing are shared. + +/// The Cineon magic number (`802A5FD7` hex), big-endian. +pub(crate) fn is_cineon(b: &[u8]) -> bool { + b.get(..4) == Some(&[0x80, 0x2A, 0x5F, 0xD7]) +} + +/// The EXIF orientation (1–8) for a Cineon header's orientation code (the same 0–7 order as DPX). +pub(crate) fn cineon_orientation(b: &[u8]) -> u16 { + let Some(code) = b.get(192).copied() else { return 1 }; + match code { + 1 => 2, + 2 => 4, + 3 => 3, + 4 => 5, + 5 => 6, + 6 => 8, + 7 => 7, + _ => 1, + } +} + +/// Decode a Cineon (`.cin`) frame: 10-bit logarithmic film scans, big-endian, one (gray) or +/// three (RGB) channels. The 10-bit code values become U16 as `round(c × 65535 / 1023)`; the +/// file's black/white reference points are not applied (a log conversion is a display choice). +pub(crate) fn decode_cineon(bytes: &[u8], limits: &Limits) -> Result { + if !is_cineon(bytes) { + return Err(err("missing the Cineon magic number (802A5FD7)")); + } + let o = Order::Big; + let missing = |what: &str| err(format!("the header ends before {what}")); + let image_offset = usize::try_from(o.u32(bytes, 4).ok_or_else(|| missing("the image data offset"))?).map_err(|_| err("image data offset out of range"))?; + let channels = *bytes.get(193).ok_or_else(|| missing("the channel count"))?; + let bits = *bytes.get(198).ok_or_else(|| missing("the bits per pixel"))?; + if bits != 10 { + return Err(unsupported(format!("{bits}-bit samples (Cineon frames are 10-bit)"))); + } + let layout = match channels { + 1 => ChannelLayout::Gray, + 3 => ChannelLayout::Rgb, + n => return Err(unsupported(format!("{n} channels (Cineon frames have 1 or 3)"))), + }; + let width = o.u32(bytes, 200).ok_or_else(|| missing("the pixels per line"))?; + let height = o.u32(bytes, 204).ok_or_else(|| missing("the lines per image"))?; + limits.check(width, height, layout, SampleType::U16)?; + + let (w, h) = (width as usize, height as usize); + let per_line = w.checked_mul(layout.channels()).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?; + // Three 10-bit samples per 32-bit word, lines on a word boundary (DPX packing 1). + let line_len = line_bytes(per_line, 10, 1).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?; + let data = bytes.get(image_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?; + let need = line_len.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + if data.len() < need { + return Err(err("truncated image data")); + } + let mut codes = vec![0u16; per_line.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?]; + for (y, row) in codes.chunks_exact_mut(per_line).enumerate() { + let start = y * line_len; + let line = data.get(start..start + line_len).ok_or_else(|| err("truncated image data"))?; + unpack_line(line, o, 10, 1, row)?; + } + let wide: Vec = codes.iter().map(|&c| widen(c, 1023)).collect(); + Image::from_u16(width, height, layout, &wide) +} + +/// Cineon's generic plus film header: where our image data starts. +const CINEON_HEADER_LEN: usize = 2048; +const CINEON_GENERIC_LEN: u32 = 1024; +/// Offset of the first of the eight 28-byte channel descriptors. +const CINEON_CHANNEL0: usize = 196; + +/// Encode a Cineon 4.5 frame: the generic header, an undefined film header, then the samples as +/// 10-bit code values `round(v × 1023 / 65535)`, three to a big-endian word, left justified +/// (packing 5), lines on a word boundary. The plan has already made the image U16 gray or RGB. +pub(crate) fn encode_cineon(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(Format::Cineon, msg); + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + // Channel designators: 0 for black and white, 1–3 for red, green and blue. + let designators: &[u8] = match img.layout() { + ChannelLayout::Gray => &[0], + ChannelLayout::Rgb => &[1, 2, 3], + l => return Err(enc(format!("Cineon cannot store {l:?}"))), + }; + let samples = img.to_u16_samples().ok_or_else(|| enc(format!("cannot write {:?} samples at 10 bits", img.sample_type())))?; + let codes: Vec = samples.into_iter().map(|v| narrow(v, 1023)).collect(); + let per_line = (w as usize).checked_mul(designators.len()).ok_or_else(|| enc("image too large".into()))?; + if codes.len() != per_line.checked_mul(h as usize).ok_or_else(|| enc("image too large".into()))? { + return Err(enc("the image has the wrong number of samples".into())); + } + let data_len = line_bytes(per_line, 10, 1).and_then(|l| l.checked_mul(h as usize)).ok_or_else(|| enc("image too large".into()))?; + let total = + CINEON_HEADER_LEN.checked_add(data_len).and_then(|t| u32::try_from(t).ok()).ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?; + + let mut wr = Writer { h: vec![0; CINEON_HEADER_LEN] }; + // File information. + wr.u32(0, 0x802A_5FD7); + wr.u32(4, CINEON_HEADER_LEN as u32); + wr.u32(8, CINEON_GENERIC_LEN); + wr.u32(12, CINEON_HEADER_LEN as u32 - CINEON_GENERIC_LEN); + wr.u32(16, 0); + wr.u32(20, total); + wr.string((24, 8), "V4.5"); + // Image information: default orientation, then each channel's descriptor; the unused ones, + // the white point and the primaries are undefined. + wr.u8(192, 0); + wr.u8(193, designators.len() as u8); + wr.undefined(CINEON_CHANNEL0, 420); + for (i, &designator) in designators.iter().enumerate() { + let c = CINEON_CHANNEL0 + 28 * i; + wr.u8(c, 0); + wr.u8(c + 1, designator); + wr.u8(c + 2, 10); + wr.u8(c + 3, 0); + wr.u32(c + 4, w); + wr.u32(c + 8, h); + // Code values 0–1023 stand for printing densities 0–2.048, the Cineon convention. All + // four are 32-bit floats here (DPX stores the data values as integers). + wr.u32(c + 12, 0f32.to_bits()); + wr.u32(c + 16, 0f32.to_bits()); + wr.u32(c + 20, 1023f32.to_bits()); + wr.u32(c + 24, 2.048f32.to_bits()); + } + // Pixel-interleaved, unsigned, positive, no line or channel padding. + wr.u8(681, 5); + // Image origination: pixel pitches and gamma unknown. + wr.undefined(972, 984); + // Film header: manufacturer, type, perforation offset, prefix, count, frame position and rate + // unknown; its strings stay empty. + wr.undefined(1024, 1036); + wr.undefined(1068, 1076); + + let mut out = wr.h; + out.try_reserve_exact(data_len).map_err(|_| enc("out of memory".into()))?; + for row in codes.chunks_exact(per_line.max(1)) { + pack_line(&mut out, row, 10); + } + Ok(out) +} + +/// The kept header fields as text metadata, in [`KEYS`] order; undefined ones are left out. +fn header_text(b: &[u8], o: Order, e: Element) -> Vec<(String, String)> { + let mut text = Vec::new(); + let mut put = |k: &str, v: Option| { + if let Some(v) = v { + text.push((k.to_string(), v)); + } + }; + put(KEY_CREATOR, string(b, CREATOR)); + put(KEY_PROJECT, string(b, PROJECT)); + put(KEY_COPYRIGHT, string(b, COPYRIGHT)); + put(KEY_CREATION_TIME, string(b, CREATION_TIME)); + put(KEY_TRANSFER, (e.transfer != UNDEF8).then(|| e.transfer.to_string())); + put(KEY_COLORIMETRIC, (e.colorimetric != UNDEF8).then(|| e.colorimetric.to_string())); + // The industry header is there when the generic header says so and the file holds it. + let industry = o.u32(b, 28).is_some_and(|l| l != 0 && l != UNDEF32) && b.len() >= HEADER_LEN; + if industry { + put(KEY_FRAME_POSITION, o.defined_u32(b, FRAME_POSITION).map(|v| v.to_string())); + put(KEY_FRAME_RATE, o.f32(b, FRAME_RATE).map(|v| v.to_string())); + put(KEY_TIME_CODE, o.defined_u32(b, TIME_CODE).map(format_time_code)); + } + text +} + +/// A SMPTE time code (BCD `HHMMSSFF`) as `HH:MM:SS:FF`, or `0x…` when it is not valid BCD. +fn format_time_code(v: u32) -> String { + let d: [u32; 8] = std::array::from_fn(|i| (v >> ((7 - i) * 4)) & 0xF); + if d.iter().all(|&d| d <= 9) { format!("{}{}:{}{}:{}{}:{}{}", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]) } else { format!("{v:#010x}") } +} + +/// The inverse of [`format_time_code`]; `None` for anything else. +fn parse_time_code(s: &str) -> Option { + if let Some(hex) = s.strip_prefix("0x") { + return u32::from_str_radix(hex, 16).ok(); + } + let parts: Vec<&str> = s.split(':').collect(); + if parts.len() != 4 || parts.iter().any(|p| p.len() != 2 || !p.bytes().all(|c| c.is_ascii_digit())) { + return None; + } + parts.iter().flat_map(|p| p.bytes()).try_fold(0u32, |acc, c| Some((acc << 4) | u32::from(c - b'0'))) +} + +/// Bits written for `sample` when the options pick none: 8 for U8, 16 otherwise. +pub(crate) fn bits_for(opt: Option, sample: SampleType) -> u8 { + match opt { + Some(b) => b.bits(), + None if sample == SampleType::U8 => 8, + None => 16, + } +} + +/// The bits `format` stores below its U16 plan: DPX at 10 or 12 bits, Cineon's 10. `None` when +/// the stored depth is the plan's sample type. +pub(crate) fn packed_bits(format: Format, opts: &EncodeOptions) -> Option { + match format { + Format::Dpx => opts.dpx_bits.filter(|b| matches!(b, DpxBits::Bits10 | DpxBits::Bits12)).map(DpxBits::bits), + Format::Cineon => Some(10), + _ => None, + } +} + +/// `true` when every sample of a U16 image is already a `bits`-bit code value, so writing it at +/// `bits` and reading it back gives the same samples (a frame opened from such a file). +pub(crate) fn keeps_u16_samples(img: &Image, bits: u8) -> bool { + if img.sample_type() != SampleType::U16 || !(1..16).contains(&bits) { + return false; + } + let max = (1u64 << bits) - 1; + img.data().as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes(*c)).all(|v| widen(narrow(v, max), max) == v) +} + +/// The plan's sample type for the chosen bits: U8 for 8 bits, U16 for 10, 12 and 16. +pub(crate) fn plan_sample(opt: Option, src: SampleType) -> SampleType { + match opt { + Some(DpxBits::Bits8) => SampleType::U8, + Some(_) => SampleType::U16, + None if src == SampleType::U8 => SampleType::U8, + None => SampleType::U16, + } +} + +struct Writer { + h: Vec, +} + +impl Writer { + fn u8(&mut self, at: usize, v: u8) { + if let Some(b) = self.h.get_mut(at) { + *b = v; + } + } + + fn u16(&mut self, at: usize, v: u16) { + if let Some(s) = self.h.get_mut(at..at + 2) { + s.copy_from_slice(&v.to_be_bytes()); + } + } + + fn u32(&mut self, at: usize, v: u32) { + if let Some(s) = self.h.get_mut(at..at + 4) { + s.copy_from_slice(&v.to_be_bytes()); + } + } + + /// Marks a run of numeric fields undefined (all ones). + fn undefined(&mut self, from: usize, to: usize) { + if let Some(s) = self.h.get_mut(from..to) { + s.fill(0xFF); + } + } + + /// An ASCII string field, cut to leave room for a terminating NUL. + fn string(&mut self, (at, len): (usize, usize), v: &str) { + let ascii: Vec = v.chars().map(|c| if (' '..='~').contains(&c) { c as u8 } else { b'?' }).take(len.saturating_sub(1)).collect(); + if let Some(s) = self.h.get_mut(at..at + ascii.len()) { + s.copy_from_slice(&ascii); + } + } +} + +fn text<'a>(img: &'a Image, key: &str) -> Option<&'a str> { + img.meta.text.iter().find(|(k, _)| k == key).map(|(_, v)| v.trim()).filter(|v| !v.is_empty()) +} + +/// Appends one line's code values, padded to a 32-bit word. +fn pack_line(out: &mut Vec, codes: &[u16], bits: u8) { + let start = out.len(); + match bits { + 8 => out.extend(codes.iter().map(|&c| c as u8)), + 10 => { + for chunk in codes.chunks(3) { + let word = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c & 0x3FF) << (22 - 10 * i as u32))); + out.extend_from_slice(&word.to_be_bytes()); + } + } + 12 => out.extend(codes.iter().flat_map(|&c| ((c & 0xFFF) << 4).to_be_bytes())), + _ => out.extend(codes.iter().flat_map(|&c| c.to_be_bytes())), + } + let written = out.len() - start; + out.resize(start + written.div_ceil(4) * 4, 0); +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let (descriptor, layout) = match img.layout() { + ChannelLayout::Gray => (6u8, ChannelLayout::Gray), + ChannelLayout::Rgb => (50, ChannelLayout::Rgb), + ChannelLayout::Rgba => (51, ChannelLayout::Rgba), + l => return Err(CodecError::encode(F, format!("DPX cannot store {l:?}"))), + }; + let bits = bits_for(opts.dpx_bits, img.sample_type()); + let codes: Vec = match (img.sample_type(), bits) { + (SampleType::U8, 8) => img.data().iter().map(|&v| u16::from(v)).collect(), + (SampleType::U16, 16) => img.to_u16_samples().unwrap_or_default(), + (SampleType::U16, 10 | 12) => { + let max = (1u64 << bits) - 1; + img.to_u16_samples().unwrap_or_default().into_iter().map(|v| narrow(v, max)).collect() + } + (s, b) => return Err(CodecError::encode(F, format!("cannot write {s:?} samples at {b} bits"))), + }; + let per_line = w as usize * layout.channels(); + if codes.len() != per_line * h as usize { + return Err(CodecError::encode(F, "the image has the wrong number of samples")); + } + let data_len = line_bytes(per_line, bits, if matches!(bits, 10 | 12) { 1 } else { 0 }) + .and_then(|l| l.checked_mul(h as usize)) + .ok_or_else(|| CodecError::encode(F, "image too large"))?; + let total = u32::try_from(HEADER_LEN + data_len).map_err(|_| CodecError::encode(F, "image too large for a DPX file (4 GiB)"))?; + + let mut wr = Writer { h: vec![0; HEADER_LEN] }; + // Generic file header. + wr.h[..4].copy_from_slice(b"SDPX"); + wr.u32(4, HEADER_LEN as u32); + wr.string((8, 8), "V2.0"); + wr.u32(16, total); + wr.u32(20, 1); + wr.u32(24, GENERIC_LEN); + wr.u32(28, INDUSTRY_LEN); + wr.u32(32, 0); + let keep = |key| if opts.embed_metadata { text(&img, key) } else { None }; + wr.string(CREATION_TIME, keep(KEY_CREATION_TIME).unwrap_or("")); + wr.string(CREATOR, keep(KEY_CREATOR).unwrap_or("PhotoCraft")); + wr.string(PROJECT, keep(KEY_PROJECT).unwrap_or("")); + wr.string(COPYRIGHT, keep(KEY_COPYRIGHT).unwrap_or("")); + wr.u32(660, UNDEF32); + // Image information: default orientation, one element. + wr.u16(ORIENTATION, 0); + wr.u16(ORIENTATION + 2, 1); + wr.u32(772, w); + wr.u32(776, h); + let e = ELEMENT0; + wr.u32(e, 0); + wr.u32(e + 4, 0); + wr.u32(e + 8, UNDEF32); + wr.u32(e + 12, ((1u32 << bits) - 1).min(65535)); + wr.u32(e + 16, UNDEF32); + wr.u8(e + 20, descriptor); + // No claim about the encoding unless the document came with one. + let code = |key| keep(key).and_then(|v| v.parse::().ok()).unwrap_or(UNDEF8); + wr.u8(e + 21, code(KEY_TRANSFER)); + wr.u8(e + 22, code(KEY_COLORIMETRIC)); + wr.u8(e + 23, bits); + wr.u16(e + 24, if matches!(bits, 10 | 12) { 1 } else { 0 }); + wr.u16(e + 26, 0); + wr.u32(e + 28, HEADER_LEN as u32); + wr.u32(e + 32, 0); + wr.u32(e + 36, 0); + // The seven unused elements, the image source header's numbers and the industry header's + // numbers are undefined; their strings stay empty. + wr.undefined(e + ELEMENT_LEN, e + ELEMENT_LEN * usize::from(MAX_ELEMENTS)); + wr.undefined(1408, 1432); + wr.undefined(1620, 1644); + wr.undefined(FRAME_POSITION, 1732); + wr.undefined(TIME_CODE, 1972); + if let Some(v) = keep(KEY_FRAME_POSITION).and_then(|v| v.parse::().ok()) { + wr.u32(FRAME_POSITION, v); + } + if let Some(v) = keep(KEY_FRAME_RATE).and_then(|v| v.parse::().ok()).filter(|v| v.is_finite()) { + wr.u32(FRAME_RATE, v.to_bits()); + } + if let Some(v) = keep(KEY_TIME_CODE).and_then(parse_time_code) { + wr.u32(TIME_CODE, v); + } + + let mut out = wr.h; + out.try_reserve_exact(data_len).map_err(|_| CodecError::encode(F, "out of memory"))?; + for row in codes.chunks_exact(per_line.max(1)) { + pack_line(&mut out, row, bits); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A minimal header for `w`×`h` with element 0 set; image data at 2048. + fn header(o: Order, w: u32, h: u32, descriptor: u8, bits: u8, packing: u16) -> Vec { + let mut b = vec![0u8; HEADER_LEN]; + let put32 = |b: &mut Vec, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + let put16 = |b: &mut Vec, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + b[..4].copy_from_slice(if o == Order::Big { b"SDPX" } else { b"XPDS" }); + put32(&mut b, 4, HEADER_LEN as u32); + put32(&mut b, 28, INDUSTRY_LEN); + put16(&mut b, ORIENTATION, 0); + put16(&mut b, ORIENTATION + 2, 1); + put32(&mut b, 772, w); + put32(&mut b, 776, h); + b[ELEMENT0 + 20] = descriptor; + b[ELEMENT0 + 21] = UNDEF8; + b[ELEMENT0 + 22] = UNDEF8; + b[ELEMENT0 + 23] = bits; + put16(&mut b, ELEMENT0 + 24, packing); + put32(&mut b, ELEMENT0 + 28, HEADER_LEN as u32); + b[FRAME_POSITION..1732].fill(0xFF); + b[TIME_CODE..1972].fill(0xFF); + b + } + + fn words(o: Order, ws: &[u32]) -> Vec { + ws.iter().flat_map(|w| if o == Order::Big { w.to_be_bytes() } else { w.to_le_bytes() }).collect() + } + + #[test] + fn ten_bit_method_a_and_b_unpack_three_to_a_word() { + for o in [Order::Big, Order::Little] { + for (packing, base) in [(1u16, 22u32), (2, 20)] { + let mut b = header(o, 1, 1, 50, 10, packing); + b.extend(words(o, &[(1023 << base) | (512 << (base - 10)) | (1 << (base - 20))])); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(512, 1023), widen(1, 1023)], "{o:?} packing {packing}"); + } + } + } + + #[test] + fn ten_bit_packing_0_is_one_bit_stream_per_line() { + // Four luma samples: 40 bits over two words, the line padded to the second word. + let samples = [1u64, 2, 1022, 1023]; + let stream = samples.iter().fold(0u64, |acc, &s| (acc << 10) | s) << 24; + for o in [Order::Big, Order::Little] { + let mut b = header(o, 4, 2, 6, 10, 0); + let line = words(o, &[(stream >> 32) as u32, stream as u32]); + b.extend(&line); + b.extend(&line); + let img = decode(&b, &Limits::default()).unwrap(); + let want: Vec = samples.iter().map(|&s| widen(s as u16, 1023)).collect(); + assert_eq!(img.to_u16_samples().unwrap(), [want.clone(), want].concat(), "{o:?}"); + } + } + + #[test] + fn twelve_bit_method_a_keeps_the_top_bits() { + let mut b = header(Order::Big, 2, 1, 6, 12, 1); + b.extend_from_slice(&[0xFF, 0xF0, 0x00, 0x10]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(1, 4095)]); + } + + #[test] + fn widen_and_narrow_round_trip_every_code() { + for bits in [10u8, 12] { + let max = (1u64 << bits) - 1; + for c in 0..=max as u16 { + assert_eq!(narrow(widen(c, max), max), c, "{bits}-bit {c}"); + } + } + assert_eq!(widen(512, 1023), ((512.0f64 * 65535.0 / 1023.0).round()) as u16); + } + + #[test] + fn orientation_codes_map_to_exif() { + let mut b = header(Order::Big, 1, 1, 6, 8, 0); + for (dpx, exif) in [(0u16, 1u16), (1, 2), (2, 4), (3, 3), (4, 5), (5, 6), (6, 8), (7, 7), (0xFFFF, 1)] { + b[ORIENTATION..ORIENTATION + 2].copy_from_slice(&dpx.to_be_bytes()); + assert_eq!(orientation(&b), exif, "DPX {dpx}"); + } + } + + #[test] + fn time_codes_round_trip() { + assert_eq!(format_time_code(0x0102_0304), "01:02:03:04"); + assert_eq!(parse_time_code("01:02:03:04"), Some(0x0102_0304)); + assert_eq!(parse_time_code(&format_time_code(0xABCD_0000)), Some(0xABCD_0000)); + assert_eq!(parse_time_code("1:2:3:4"), None); + } + + #[test] + fn eight_bit_lines_without_word_padding_still_open() { + // 1 × 2 RGB: 3 bytes a line, 6 in all; padded lines would need 8. + let mut b = header(Order::Big, 1, 2, 50, 8, 0); + b.extend_from_slice(&[1, 2, 3, 4, 5, 6]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[1, 2, 3, 4, 5, 6]); + } + + /// A minimal Cineon: a 1024-byte generic header, then `w × h` 10-bit samples packed three + /// to a 32-bit word (RGB in word order, or gray three pixels per word). + fn cineon(w: u32, h: u32, channels: u8, px: &[[u16; 3]]) -> Vec { + let mut b = vec![0u8; 1024]; + b[0..4].copy_from_slice(&[0x80, 0x2A, 0x5F, 0xD7]); // magic (802A5FD7) + b[4..8].copy_from_slice(&1024u32.to_be_bytes()); // image data offset + b[192] = 0; // orientation: left-to-right, top-to-bottom + b[193] = channels; + b[198] = 10; // bits per pixel (channel 1) + b[200..204].copy_from_slice(&w.to_be_bytes()); // pixels per line + b[204..208].copy_from_slice(&h.to_be_bytes()); // lines per image + let word = |a: u16, c: u16, e: u16| ((u32::from(a) << 22) | (u32::from(c) << 12) | (u32::from(e) << 2)).to_be_bytes(); + if channels == 3 { + for p in px { + b.extend_from_slice(&word(p[0], p[1], p[2])); + } + } else { + let g = |i: usize| px.get(i).map_or(0, |p| p[0]); + for i in (0..px.len()).step_by(3) { + b.extend_from_slice(&word(g(i), g(i + 1), g(i + 2))); + } + } + b + } + + #[test] + fn cineon_reads_ten_bit_rgb() { + let px = [[0, 512, 1023], [1023, 0, 256]]; + let b = cineon(2, 1, 3, &px); + assert!(is_cineon(&b)); + assert_eq!(order(&b), None, "Cineon is not DPX"); + let img = decode_cineon(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); + assert_eq!( + img.to_u16_samples().unwrap(), + vec![widen(0, 1023), widen(512, 1023), widen(1023, 1023), widen(1023, 1023), widen(0, 1023), widen(256, 1023)] + ); + } + + #[test] + fn cineon_reads_grayscale_and_rejects_other_depths() { + let px = [[10, 0, 0], [20, 0, 0], [30, 0, 0], [40, 0, 0]]; + let b = cineon(4, 1, 1, &px); + let img = decode_cineon(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.to_u16_samples().unwrap(), vec![widen(10, 1023), widen(20, 1023), widen(30, 1023), widen(40, 1023)]); + // A depth other than 10 is unsupported, and truncation errors rather than panicking. + let mut bad = cineon(1, 1, 3, &[[0, 0, 0]]); + bad[198] = 8; + assert!(matches!(decode_cineon(&bad, &Limits::default()), Err(CodecError::Unsupported { .. }))); + let full = cineon(4, 4, 3, &[[0; 3]; 16]); + for cut in [0, 4, 200, 1024, full.len() - 1] { + assert!(decode_cineon(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}"); + } + } +} diff --git a/crates/image-codecs/src/codecs/exr.rs b/crates/image-codecs/src/codecs/exr.rs new file mode 100644 index 000000000..0d68d20ca --- /dev/null +++ b/crates/image-codecs/src/codecs/exr.rs @@ -0,0 +1,441 @@ +//! OpenEXR via the pure-Rust `exr` crate: F16/F32, Y/YA/RGB/RGBA channels, +//! lossless compression. Multi-part files (a Maya/Arnold render writes one part +//! per AOV) open the part that looks most like a colour image, with a warning +//! naming how many parts exist; [`info`] lists every part and [`decode_part`] +//! decodes a chosen one. Pixel data is taken from the data window. + +use std::io::Cursor; + +use exr::meta::MetaData; +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, ReadChannels, ReadLayers, WritableImage, read, +}; +use half::f16; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, ExrChannelInfo, ExrPartInfo, Image, SampleType, base_channel_name, exr_color_channels}; +use crate::options::{EncodeOptions, ExrCompression, Limits}; + +const F: Format = Format::OpenExr; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +/// An `exr`-crate error, with its `NotSupported` kind kept distinct from malformed data +/// (the same split the TIFF codec makes), so unsupported features say so. +fn map_exr(e: exr::error::Error) -> CodecError { + match e { + exr::error::Error::NotSupported(message) => CodecError::unsupported(F, format!("not supported: {message}")), + e => err(e), + } +} + +/// Refuses headers whose `preview` attributes declare more pixels than their value holds. +/// +/// The `exr` crate sizes a preview's pixel buffer from its declared dimensions (up to 4 MiB at +/// once, zero-filled) before it finds the attribute too short, and in the non-pedantic mode +/// every reader here uses it then skips the attribute and goes on: thousands of tiny malformed +/// previews would each cost that allocation. This walks the raw headers (name, type, size, +/// value per attribute; an empty name ends a header, an empty header ends a multi-part list) +/// and checks each preview against its own size, so the crate only reads consistent ones. +/// Anything else odd is left to the crate to report. +pub(crate) fn check_previews(b: &[u8]) -> Result<(), CodecError> { + if b.get(..4) != Some(&[0x76, 0x2f, 0x31, 0x01][..]) { + return Ok(()); + } + let Some(flags) = b.get(4..8).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes) else { return Ok(()) }; + let multipart = flags & 0x1000 != 0; + let cstr = |at: usize| -> Option<(&[u8], usize)> { + let rest = b.get(at..)?; + let n = rest.iter().position(|&c| c == 0)?; + Some((rest.get(..n)?, at + n + 1)) + }; + let u32_at = |at: usize| b.get(at..at.checked_add(4)?).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes); + let mut at = 8; + loop { + // One header's attributes. + loop { + let Some((name, next)) = cstr(at) else { return Ok(()) }; + at = next; + if name.is_empty() { + break; + } + let Some((kind, next)) = cstr(at) else { return Ok(()) }; + let Some(size) = u32_at(next).and_then(|n| i32::try_from(n).ok()).and_then(|n| usize::try_from(n).ok()) else { return Ok(()) }; + let value = next + 4; + let end = match value.checked_add(size) { + Some(end) if end <= b.len() => end, + _ => return Ok(()), + }; + if kind == b"preview" { + let declared = + u32_at(value).zip(u32_at(value + 4)).and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)).filter(|_| size >= 8); + if declared.is_none_or(|n| n > (size - 8) as u64) { + return Err(err("a preview attribute declares more pixels than it holds")); + } + } + at = end; + } + // A multi-part list ends with an empty header (a lone NUL). + if !multipart || b.get(at) == Some(&0) { + return Ok(()); + } + } +} + +/// Read the headers without the `exr` crate's validation (it rejects deep parts outright), +/// after checking every part's declared size against the limits, before any pixel allocation. +pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result { + check_previews(bytes)?; + let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(err)?; + // Flat decoding reads every part at once, so the parts' sizes are also capped together. + let mut total: u64 = 0; + // DWAA/DWAB parts are counted apart: they go past 2^16:1 (a flat 8192x4320 frame with 12 + // alpha-scheme mattes is about 30 KB, over 75000:1). + let mut dwa: u64 = 0; + for header in meta.headers.iter() { + let size = header.layer_size; + let (w, h) = (u32::try_from(size.0).unwrap_or(u32::MAX), u32::try_from(size.1).unwrap_or(u32::MAX)); + let bpp = header.channels.list.len().max(1) as u64 * 4; + limits.check_bytes(w, h, bpp)?; + let bytes = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp); + total = total.saturating_add(bytes); + if matches!(header.compression, Compression::DWAA(_) | Compression::DWAB(_)) { + dwa = dwa.saturating_add(bytes); + } + } + if total > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("{total} bytes across {} parts exceed max_alloc {}", meta.headers.len(), limits.max_alloc))); + } + // The `exr` crate allocates a flat part's samples before reading its chunks, so the file + // must be able to hold them (#8597). The other compressions stay far below 2^16:1 even + // against this f32 budget. DWA's run-length channels are zlib-packed on top and reach about + // 132000:1, so DWA parts get 2^18:1. Deep parts size their samples from the file's own counts. + if !meta.requirements.has_deep_data { + Limits::check_plausible(total.saturating_sub(dwa).saturating_add(dwa / 4), bytes.len(), 1 << 16)?; + } + Ok(meta) +} + +/// Header facts of one part, in file order. +fn part_info(index: usize, header: &exr::meta::header::Header) -> ExrPartInfo { + let (w, h) = (u32::try_from(header.layer_size.0).unwrap_or(u32::MAX), u32::try_from(header.layer_size.1).unwrap_or(u32::MAX)); + let sample = |t: exr::meta::attribute::SampleType| match t { + exr::meta::attribute::SampleType::F16 => SampleType::F16, + // U32 channels (Arnold ID buffers) are listed as F32: exact up to 2^24. + exr::meta::attribute::SampleType::F32 | exr::meta::attribute::SampleType::U32 => SampleType::F32, + }; + let display = &header.shared_attributes.display_window; + ExrPartInfo { + index, + name: header.own_attributes.layer_name.as_ref().map(|n| n.to_string()), + view: header.own_attributes.view_name.as_ref().map(|v| v.to_string()), + width: w, + height: h, + data_window: (header.own_attributes.layer_position.0, header.own_attributes.layer_position.1, w, h), + display_window: ( + display.position.0, + display.position.1, + u32::try_from(display.size.0).unwrap_or(u32::MAX), + u32::try_from(display.size.1).unwrap_or(u32::MAX), + ), + deep: header.deep, + tiled: matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)), + channels: header.channels.list.iter().map(|c| ExrChannelInfo { name: c.name.to_string(), sample: sample(c.sample_type) }).collect(), + } +} + +/// Lists every part of the file (header facts only, no pixel data): what a multi-part +/// chooser offers, and what [`decode_part`] takes an index into. +pub(crate) fn info(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + let meta = read_meta(bytes, limits)?; + Ok(meta.headers.iter().enumerate().map(|(i, h)| part_info(i, h)).collect()) +} + +/// The part plain [`decode`] opens: the highest [`ExrPartInfo::color_rank`], earliest wins. +fn pick_part(meta: &MetaData) -> usize { + let mut best = 0; + let mut best_rank = meta.headers.first().map_or(0, |h| part_info(0, h).color_rank()); + for (i, header) in meta.headers.iter().enumerate().skip(1) { + let rank = part_info(i, header).color_rank(); + if rank > best_rank { + best = i; + best_rank = rank; + } + } + best +} + +/// Selects one part for the `exr` crate's first-valid-layer reader: `create_channels_reader` +/// is asked about the headers in file order and the first `Ok` wins, so every header before +/// `index` (and, harmlessly, those after it) is declined. Only the chosen part's chunks are +/// then decompressed and only its planes allocated; the other parts are skipped unread. +struct OnlyPart { + inner: C, + index: usize, + seen: std::cell::Cell, +} + +impl<'s, C: ReadChannels<'s>> ReadChannels<'s> for OnlyPart { + type Reader = C::Reader; + + fn create_channels_reader(&'s self, header: &exr::meta::header::Header) -> exr::error::Result { + let position = self.seen.get(); + self.seen.set(position.saturating_add(1)); + if position == self.index { self.inner.create_channels_reader(header) } else { Err(exr::error::Error::Invalid("part not requested".into())) } + } +} + +/// Decode one part's flat layer via the `exr` crate (it handles every flat compression). +/// Other parts are not decompressed, so an AOV file costs one part per request, not the file. +pub(super) fn read_layer(bytes: &[u8], index: usize) -> Result>, CodecError> { + check_previews(bytes)?; + let only = OnlyPart { inner: read().no_deep_data().largest_resolution_level().all_channels(), index, seen: std::cell::Cell::new(0) }; + only.first_valid_layer().all_attributes().from_buffered(Cursor::new(bytes)).map(|image| image.layer_data).map_err(map_exr) +} + +fn layer_to_image(layer: &Layer>) -> Result { + let (w, h) = (layer.size.0, layer.size.1); + let channels = &layer.channel_data.list; + let names: Vec<_> = channels.iter().map(|c| c.name.to_string()).collect(); + let (layout, selected) = exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| CodecError::unsupported(F, "no R/G/B or Y channels"))?; + let color: Vec<_> = selected.iter().map(|i| channels.get(*i).ok_or_else(|| err("selected channel is missing"))).collect::>()?; + let all_f16 = color.iter().all(|c| matches!(c.sample_data, FlatSamples::F16(_))); + let sample = if all_f16 { SampleType::F16 } else { SampleType::F32 }; + let npx = w * h; + if color.iter().any(|c| c.sample_data.len() != npx) { + return Err(err("channel sample count mismatch (subsampled channels are unsupported)")); + } + let nc = color.len(); + let (w32, h32) = (w as u32, h as u32); + let img = if all_f16 { + let mut out = vec![f16::ZERO; npx * nc]; + for (ci, c) in color.iter().enumerate() { + if let FlatSamples::F16(v) = &c.sample_data { + for (i, s) in v.iter().enumerate() { + out[i * nc + ci] = *s; + } + } + } + Image::from_f16(w32, h32, layout, &out)? + } else { + let mut out = vec![0f32; npx * nc]; + for (ci, c) in color.iter().enumerate() { + for (i, s) in c.sample_data.values_as_f32().enumerate() { + out[i * nc + ci] = s; + } + } + Image::from_f32(w32, h32, layout, &out)? + }; + debug_assert_eq!(img.sample_type(), sample); + Ok(img) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let meta = read_meta(bytes, limits)?; + + // Deep parts are rejected by the `exr` crate's reader; walk their chunks ourselves and + // composite the samples into the flat image (a warning says what was lost). + if meta.requirements.has_deep_data { + let deep = super::deep_exr::decode_deep(&meta, bytes, limits)?; + return super::deep_exr::flatten(&deep, limits); + } + + let pick = pick_part(&meta); + let mut img = layer_to_image(&read_layer(bytes, pick)?)?; + if meta.headers.len() > 1 { + let total = u32::try_from(meta.headers.len()).ok(); + img.warnings.push(DecodeWarning::MoreParts { total }); + } + Ok(img) +} + +/// The size of the part [`decode`] opens, from the headers alone. Deep files are refused (their +/// flattened size comes from the deep decoder), as is a part [`decode`] cannot show. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32), CodecError> { + let meta = read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "no header-only size probe for deep data")); + } + let pick = pick_part(&meta); + let info = meta.headers.get(pick).map(|h| part_info(pick, h)).ok_or_else(|| err("no parts"))?; + if info.color_rank() == 0 { + return Err(CodecError::unsupported(F, "no R/G/B or Y channels")); + } + Ok((info.width, info.height)) +} + +/// Decode one part of a multi-part file by its index into [`info`]'s list. A file with +/// deep parts is not mixed: the deep decoder composites those instead. +pub(crate) fn decode_part(bytes: &[u8], index: usize, limits: &Limits) -> Result { + let meta = read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + } + if index >= meta.headers.len() { + return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); + } + layer_to_image(&read_layer(bytes, index)?) +} + +/// Decodes the named channels of one flat part (exact names as the file stores them, e.g. +/// `diffuse.R`) into an image: R/G/B (plus A when named) give Rgba, a lone Y gives Gray. This +/// is how a channel group renders without the part's other channels in the way. +pub(crate) fn decode_channels(bytes: &[u8], index: usize, names: &[&str], limits: &Limits) -> Result { + let meta = read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + } + if index >= meta.headers.len() { + return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); + } + let layer = read_layer(bytes, index)?; + let (w, h) = (layer.size.0, layer.size.1); + let channels = &layer.channel_data.list; + let npx = w * h; + let mut picked: Vec<&AnyChannel> = Vec::with_capacity(names.len()); + for name in names { + let c = channels.iter().find(|c| c.name.to_string() == *name).ok_or_else(|| err(format!("the part has no channel {name}")))?; + if c.sample_data.len() != npx { + return Err(err("channel sample count mismatch (subsampled channels are unsupported)")); + } + picked.push(c); + } + let base = |c: &AnyChannel| -> String { base_channel_name(&c.name.to_string()).to_string() }; + let has = |n: &str| picked.iter().any(|c| base(c) == n); + let (layout, order): (ChannelLayout, Vec>) = if has("R") && has("G") && has("B") { + let at = |n: &str| picked.iter().position(|c| base(c) == n); + match has("A").then(|| at("A")) { + Some(a) => (ChannelLayout::Rgba, vec![at("R"), at("G"), at("B"), a]), + None => (ChannelLayout::Rgb, vec![at("R"), at("G"), at("B"), None]), + } + } else if has("Y") { + let y = picked.iter().position(|c| base(c) == "Y"); + match picked.iter().position(|c| base(c) == "A") { + Some(a) => (ChannelLayout::GrayA, vec![y, Some(a)]), + None => (ChannelLayout::Gray, vec![y]), + } + } else if picked.len() == 1 { + (ChannelLayout::Gray, vec![Some(0)]) + } else { + return Err(CodecError::unsupported(F, "the named channels are no R/G/B or Y set")); + }; + let all_f16 = picked.iter().all(|c| matches!(c.sample_data, FlatSamples::F16(_))); + let nc = layout.channels(); + let (w32, h32) = (w as u32, h as u32); + // The None slots sit past the layout's own channels (the absent A of an RGB pick) and + // are skipped, so the buffer keeps its pixel stride. + let img = if all_f16 { + let mut out = vec![f16::ZERO; npx * nc]; + for (ci, pick) in order.iter().enumerate() { + let Some(idx) = pick else { continue }; + let Some(FlatSamples::F16(v)) = picked.get(*idx).map(|c| &c.sample_data) else { continue }; + for (i, s) in v.iter().enumerate() { + out[i * nc + ci] = *s; + } + } + Image::from_f16(w32, h32, layout, &out) + } else { + let mut out = vec![0f32; npx * nc]; + for (ci, pick) in order.iter().enumerate() { + let Some(idx) = pick else { continue }; + let Some(c) = picked.get(*idx) else { continue }; + for (i, s) in c.sample_data.values_as_f32().enumerate() { + out[i * nc + ci] = s; + } + } + Image::from_f32(w32, h32, layout, &out) + }; + img.map_err(|e| CodecError::encode(F, e)) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = (img.width() as usize, img.height() as usize); + let layout = img.layout(); + let names: &[&str] = match layout { + ChannelLayout::Gray => &["Y"], + ChannelLayout::GrayA => &["Y", "A"], + ChannelLayout::Rgb => &["R", "G", "B"], + ChannelLayout::Rgba => &["R", "G", "B", "A"], + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let nc = names.len(); + let mut list: Vec> = Vec::with_capacity(nc); + match img.sample_type() { + SampleType::F16 => { + let s = img.to_f16_samples().unwrap_or_default(); + for (ci, n) in names.iter().enumerate() { + let v: Vec = s.iter().skip(ci).step_by(nc).copied().collect(); + list.push(AnyChannel::new(*n, FlatSamples::F16(v))); + } + } + SampleType::F32 => { + let s = img.to_f32_samples().unwrap_or_default(); + for (ci, n) in names.iter().enumerate() { + let v: Vec = s.iter().skip(ci).step_by(nc).copied().collect(); + list.push(AnyChannel::new(*n, FlatSamples::F32(v))); + } + } + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + } + let compression = match opts.exr_compression { + ExrCompression::None => Compression::Uncompressed, + ExrCompression::Rle => Compression::RLE, + ExrCompression::Zip1 => Compression::ZIP1, + ExrCompression::Zip16 => Compression::ZIP16, + ExrCompression::Piz => Compression::PIZ, + }; + let encoding = Encoding { compression, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }; + let channels = AnyChannels::sort(list.into_iter().collect()); + let layer = Layer::new((w, h), LayerAttributes::default(), encoding, channels); + let image = exr::image::Image::from_layer(layer); + let mut cursor = Cursor::new(Vec::new()); + image.write().to_buffered(&mut cursor).map_err(|e| CodecError::encode(F, e))?; + Ok(cursor.into_inner()) +} + +#[cfg(test)] +mod preview_tests { + use super::*; + use exr::meta::attribute::Preview; + use exr::prelude::{ImageAttributes, IntegerBounds}; + + /// A 2x2 single-part file whose layer carries a 2x1 preview. + fn with_preview() -> Vec { + let mut attrs = LayerAttributes::named("rgb"); + attrs.preview = Some(Preview { size: exr::math::Vec2(2, 1), pixel_data: vec![7; 8] }); + let list = ["R", "G", "B"].into_iter().map(|n| AnyChannel::new(n, FlatSamples::F32(vec![0.5; 4]))).collect(); + let layer = Layer::new( + (2, 2), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (2, 2))), vec![layer]); + let mut out = Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).unwrap(); + out.into_inner() + } + + #[test] + fn a_preview_declaring_more_pixels_than_it_holds_is_refused_before_the_crate_reads_it() { + let good = with_preview(); + check_previews(&good).unwrap(); + assert_eq!(decode(&good, &Limits::default()).unwrap().dimensions(), (2, 2)); + // Patch the preview to 1024x1024 (4 MiB) while its value still holds 8 bytes. + let tag = b"preview\0preview\0"; + let at = good.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len() + 4; + let mut bad = good.clone(); + bad[at..at + 4].copy_from_slice(&1024u32.to_le_bytes()); + bad[at + 4..at + 8].copy_from_slice(&1024u32.to_le_bytes()); + for r in [check_previews(&bad), decode(&bad, &Limits::default()).map(|_| ()), read_layer(&bad, 0).map(|_| ())] { + assert!(matches!(&r, Err(CodecError::Malformed { message, .. }) if message.contains("preview")), "{r:?}"); + } + // A header cut off mid-attribute is left to the crate. + check_previews(&good[..at]).unwrap(); + } +} diff --git a/crates/image-codecs/src/codecs/exr_cryptomatte.rs b/crates/image-codecs/src/codecs/exr_cryptomatte.rs new file mode 100644 index 000000000..62fd2e1e0 --- /dev/null +++ b/crates/image-codecs/src/codecs/exr_cryptomatte.rs @@ -0,0 +1,266 @@ +//! Cryptomatte (specification 1.2): per-pixel object/material IDs with coverage in EXR +//! channels, as written by Arnold, V-Ray, Redshift, Mantra/Karma and Cycles. Layers are +//! announced by `cryptomatte//name|hash|conversion|manifest` header attributes; the +//! samples live in `NN` RGBA channel sets, each holding two (ID, coverage) ranks: +//! `.red`/`.green` (rank 2·NN) and `.blue`/`.alpha` (rank 2·NN+1). +//! +//! IDs are the float *values*: MurmurHash3 x86-32 (seed 0) of the object name, its +//! exponent fixed away from 0/255 so no ID is denormal, infinite or NaN, read as an f32 +//! bit pattern. Everything here compares IDs by value, never by bit pattern. + +use std::io::Cursor; + +use exr::meta::MetaData; +use exr::meta::attribute::AttributeValue; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{CryptomatteBuffer, CryptomatteLayer}; +use crate::options::Limits; + +const F: Format = Format::OpenExr; + +/// A Cryptomatte stream channel (`crypto_asset00.red`) splits into its stream name +/// (`crypto_asset00`), the stream index, whether it is a coverage channel (green/alpha) +/// instead of an ID (red/blue), and its rank within the set (0 for red/green, 1 for +/// blue/alpha). +pub(crate) struct StreamChannel { + pub stream: String, + pub index: u32, + pub coverage: bool, + pub rank: usize, +} + +/// `NN` where NN is one or more digits (the reference readers match `\d+`), with a +/// `.red`/`.r` or `.blue`/`.b` (ID) or `.green`/`.g` or `.alpha`/`.a` (coverage) suffix, +/// in any case (Blender writes `.R`/`.G`/`.B`/`.A`). +pub(crate) fn split_stream(channel: &str) -> Option { + let (stream, suffix) = channel.rsplit_once('.')?; + let (coverage, rank) = match suffix.to_ascii_lowercase().as_str() { + "red" | "r" => (false, 0), + "green" | "g" => (true, 0), + "blue" | "b" => (false, 1), + "alpha" | "a" => (true, 1), + _ => return None, + }; + let digits = stream.chars().rev().take_while(|c| c.is_ascii_digit()).count(); + if digits == 0 || digits == stream.len() { + return None; + } + let split = stream.len() - digits; + let index = stream[split..].parse().ok()?; + Some(StreamChannel { stream: stream.to_string(), index, coverage, rank }) +} + +/// MurmurHash3, x86-32 bit variant, seed 0 (public-domain algorithm, Austin Appleby). +fn murmur3_32(data: &[u8]) -> u32 { + const C1: u32 = 0xcc9e_2d51; + const C2: u32 = 0x1b87_3593; + let mut h = 0u32; + let (chunks, rest) = data.as_chunks::<4>(); + for c in chunks { + let mut k = u32::from_le_bytes(*c); + k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); + h ^= k; + h = h.rotate_left(13).wrapping_mul(5).wrapping_add(0xe654_6b64); + } + if !rest.is_empty() { + let mut k = [0u8; 4]; + k[..rest.len()].copy_from_slice(rest); + let mut k = u32::from_le_bytes(k); + k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); + h ^= k; + } + h ^= data.len() as u32; + h ^= h >> 16; + h = h.wrapping_mul(0x85eb_ca6b); + h ^= h >> 13; + h = h.wrapping_mul(0xc2b2_ae35); + h ^ (h >> 16) +} + +/// The Cryptomatte ID of a name: the MurmurHash3 bits with the exponent fixed away from +/// 0 and 255, as an f32 value. +pub fn cryptomatte_id(name: &str) -> f32 { + let bits = murmur3_32(name.as_bytes()); + let exp = (bits >> 23) & 0xff; + let bits = if exp == 0 || exp == 255 { bits ^ (1 << 23) } else { bits }; + f32::from_bits(bits) +} + +/// The 7-hex-digit metadata key of a layer name (the ID bits without their last digit). +pub fn cryptomatte_key(name: &str) -> String { + format!("{:08x}", cryptomatte_id(name).to_bits())[..7].to_string() +} + +/// The preview colour of an ID (the reference formula): green and blue channels shifted +/// out of the ID's bit pattern, red unused. +pub fn cryptomatte_preview_color(id: f32) -> [f32; 3] { + let bits = id.to_bits(); + [0.0, f32::from_bits(bits << 8), f32::from_bits(bits << 16)] +} + +/// The `cryptomatte//` text attribute of a header, if present. +fn attribute(other: &std::collections::HashMap, key: &str, field: &str) -> Option { + let full = exr::meta::attribute::Text::new_or_none(format!("cryptomatte/{key}/{field}").as_str())?; + match other.get(&full)? { + AttributeValue::Text(t) => Some(t.to_string()), + _ => None, + } +} + +/// Parse a manifest JSON (`{"object name": "<8-hex ID bits>"}`) into (ID value, name) +/// pairs. Entries that are not strings of up to eight hex digits are skipped, like the +/// reference readers tolerate a partially broken manifest; a broken document yields none. +fn parse_manifest(json: &str) -> Vec<(f32, String)> { + let Ok(value) = serde_json::from_str::(json) else { return Vec::new() }; + let Some(map) = value.as_object() else { return Vec::new() }; + let mut out = Vec::with_capacity(map.len().min(1 << 20)); + for (name, hex) in map { + let Some(hex) = hex.as_str() else { continue }; + let Some(bits) = u32::from_str_radix(hex.trim_start_matches("0x"), 16).ok() else { continue }; + out.push((f32::from_bits(bits), name.clone())); + } + out +} + +/// The stream channel names of a layer (`crypto_asset00`, `crypto_asset01`, …, exactly as +/// the file spells them), matched leniently: the channel prefix and the `name` attribute +/// differ in case or `_`/`-` between renderers (`CryptoAsset` vs `crypto_asset`). +fn layer_streams(header: &exr::meta::header::Header, name: &str) -> Vec { + let normalize = |s: &str| s.to_ascii_lowercase().replace(['_', '-'], ""); + let want = normalize(name); + let mut found: Vec<(u32, String)> = header + .channels + .list + .iter() + .filter_map(|c| { + let s = split_stream(&c.name.to_string())?; + (!s.coverage).then_some((s.index, s.stream)) + }) + .filter(|(_, stream)| normalize(stream.trim_end_matches(|c: char| c.is_ascii_digit())) == want) + .collect(); + found.sort(); + found.dedup(); + found.into_iter().map(|(_, stream)| stream).collect() +} + +/// Lists every Cryptomatte layer of the file from the headers alone: what a Cryptomatte +/// picker offers. The embedded manifest (capped, JSON) is parsed into (ID, name) pairs. +pub(crate) fn layers(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + super::exr::check_previews(bytes)?; + let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(|e| CodecError::malformed(F, e))?; + // Manifests can be large; bound the embedded text before parsing anything. + const MANIFEST_CAP: usize = 16 << 20; + let mut out = Vec::new(); + for (part, header) in meta.headers.iter().enumerate() { + let size = header.layer_size; + let (w, h) = (u32::try_from(size.0).unwrap_or(u32::MAX), u32::try_from(size.1).unwrap_or(u32::MAX)); + limits.check_bytes(w, h, 8)?; + // Cache stream lists by resolved layer name: layer_streams() rescans every channel, + // and distinct file-controlled keys can all name the same layer, which would turn + // channel discovery into O(keys * channels) work and allocate a fresh list each time. + let mut streams_for: std::collections::HashMap> = std::collections::HashMap::new(); + for key in cryptomatte_keys(&header.own_attributes.other) { + let Some(name) = attribute(&header.own_attributes.other, &key, "name") else { continue }; + let channels = streams_for.entry(name.clone()).or_insert_with(|| layer_streams(header, name.as_str())).clone(); + if channels.is_empty() { + continue; + } + let manifest = attribute(&header.own_attributes.other, &key, "manifest") + .filter(|m| m.len() <= MANIFEST_CAP) + .map(|m| parse_manifest(m.as_str())) + .unwrap_or_default(); + out.push(CryptomatteLayer { + part, + name, + key: key.clone(), + conversion: attribute(&header.own_attributes.other, &key, "conversion"), + manifest, + sidecar_manifest: attribute(&header.own_attributes.other, &key, "manif_file"), + channels, + }); + } + } + out.sort_by(|a, b| (&a.part, &a.name).cmp(&(&b.part, &b.name))); + Ok(out) +} + +/// Every `cryptomatte//` key of a header, in file order, without duplicates. +fn cryptomatte_keys(other: &std::collections::HashMap) -> Vec { + let mut keys = Vec::new(); + // Track seen keys in a set: the attribute names are file-controlled, so membership with + // keys.iter().any() is O(n^2) in the number of distinct `cryptomatte//` + // groups. The Vec still records file order; the set only answers "seen before". + let mut seen = std::collections::HashSet::new(); + for name in other.keys() { + let full = name.to_string(); + let Some(rest) = full.strip_prefix("cryptomatte/") else { continue }; + let Some((key, _field)) = rest.split_once('/') else { continue }; + if !key.is_empty() && seen.insert(key.to_string()) { + keys.push(key.to_string()); + } + } + keys +} + +/// Decodes the samples of the Cryptomatte layer called `layer_name` (a +/// [`CryptomatteLayer::name`]): per pixel its (ID, coverage) pairs, coverage above zero, +/// sorted by descending coverage. Refuses files that also contain deep parts, like the +/// other flat part decoders. +pub(crate) fn decode(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result { + let found = layers(bytes, limits)?; + let layer = found + .iter() + .find(|l| l.name.eq_ignore_ascii_case(layer_name)) + .ok_or_else(|| CodecError::unsupported(F, format!("no Cryptomatte layer named {layer_name}")))?; + // Also caps all parts' declared sizes together, like every other flat decode. + let meta = super::exr::read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "this file has deep parts; Cryptomatte decoding stays with flat files")); + } + let header = meta.headers.get(layer.part).ok_or_else(|| CodecError::malformed(F, "the Cryptomatte part is missing"))?; + let size = header.layer_size; + let (w, h) = (size.0, size.1); + let npx = w.checked_mul(h).ok_or_else(|| CodecError::malformed(F, "the layer is too large"))?; + let too_large = || CodecError::malformed(F, "the layer is too large"); + let (w32, h32) = (u32::try_from(w).map_err(|_| too_large())?, u32::try_from(h).map_err(|_| too_large())?); + // Each pixel also gets a pair list, so count its header with the channel samples (four + // f32 channels per set: two ranks of ID and coverage). Include the heap capacity the pair + // list can reach: up to one (id, coverage) pair per rank, 8 bytes each. Without this the + // returned per-pixel vectors could exceed the caller's max_alloc budget. + let ranks = layer.channels.len().saturating_mul(2); + let pair_capacity = (ranks as u64).saturating_mul(std::mem::size_of::<(f32, f32)>() as u64); + let per_pixel = + (layer.channels.len().max(1) as u64).saturating_mul(16).saturating_add(std::mem::size_of::>() as u64).saturating_add(pair_capacity); + limits.check_bytes(w32, h32, per_pixel)?; + + let ex_layer = super::exr::read_layer(bytes, layer.part)?; + + // Two (ID, coverage) ranks per set: rank `slot * 2 + stream.rank`. + let mut ids: Vec> = vec![Vec::new(); ranks]; + let mut coverages: Vec> = vec![Vec::new(); ranks]; + for c in &ex_layer.channel_data.list { + let Some(stream) = split_stream(&c.name.to_string()) else { continue }; + let Some(slot) = layer.channels.iter().position(|s| *s == stream.stream) else { continue }; + let values: Vec = c.sample_data.values_as_f32().collect(); + if values.len() != npx { + return Err(CodecError::malformed(F, "a Cryptomatte channel is smaller than its layer")); + } + let bucket = if stream.coverage { &mut coverages } else { &mut ids }; + let Some(dst) = slot.checked_mul(2).and_then(|r| r.checked_add(stream.rank)).and_then(|r| bucket.get_mut(r)) else { continue }; + *dst = values; + } + + let mut pixels = vec![Vec::new(); npx]; + for (p, out) in pixels.iter_mut().enumerate() { + for (id, cov) in ids.iter().zip(&coverages) { + let (id, cov) = (id.get(p).copied().unwrap_or(0.0), cov.get(p).copied().unwrap_or(0.0)); + if cov > 0.0 && id.is_finite() { + out.push((id, cov)); + } + } + out.sort_by(|a, b| b.1.total_cmp(&a.1)); + } + Ok(CryptomatteBuffer { width: w32, height: h32, pixels }) +} diff --git a/crates/image-codecs/src/codecs/fits.rs b/crates/image-codecs/src/codecs/fits.rs new file mode 100644 index 000000000..ae6e94505 --- /dev/null +++ b/crates/image-codecs/src/codecs/fits.rs @@ -0,0 +1,855 @@ +//! FITS (Flexible Image Transport System, FITS Standard 4.0), implemented here: the image in the +//! primary HDU, or the first `IMAGE` extension when the primary HDU holds no data. +//! +//! Reading: +//! +//! * `BITPIX` 8, 16, 32, 64, -32 and -64, with `BSCALE`/`BZERO` and integer `BLANK`. +//! * `NAXIS3 = 3` opens as RGB (the planes are R, G, B); any other plane count opens the first +//! plane as gray with [`DecodeWarning::MorePages`]. +//! * Rows are stored bottom-to-top, as the standard's convention has it (Siril, DS9, Astropy), +//! unless `ROWORDER = 'TOP-DOWN'`. +//! * Physical values that fit (unsigned 8-bit, or integers in 0..=65535 such as a camera's +//! `BITPIX = 16`, `BZERO = 32768`) are kept exactly as U8/U16. Everything else becomes F32: +//! values up to 2 are taken as already normalised (Siril, PixInsight), larger ones are scaled by +//! 65535 when they fit in it (DeepSkyStacker, most stackers) and by the maximum otherwise. NaN +//! and blank pixels read as 0. +//! * The other header cards are kept as text: `KEY` → value (strings unquoted), `COMMENT` and +//! `HISTORY` by their text, `HIERARCH` keywords by their long name. Long strings continued +//! on `CONTINUE` cards are joined. +//! +//! Writing: gray or RGB, U8 (`BITPIX = 8`), U16 (`BITPIX = 16`, `BZERO = 32768`) or F32 +//! (`BITPIX = -32`, 0..1), bottom-to-top with `ROWORDER = 'BOTTOM-UP'`, plus the text cards +//! (strings longer than one card on `CONTINUE` cards). +//! Tile-compressed files (fpack `ZIMAGE` tables) are recognised and refused. + +use std::collections::HashMap; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType}; +use crate::options::{EncodeOptions, Limits}; + +const F: Format = Format::Fits; +const BLOCK: usize = 2880; +const CARD: usize = 80; +/// Most HDUs walked looking for an image; FITS files from cameras and stackers hold a handful. +const MAX_HDUS: usize = 1000; + +/// Keywords that describe the data layout. They are written from the image, never copied from +/// (or kept as) text metadata: after decoding they no longer describe the pixels. +const STRUCTURAL: &[&str] = &[ + "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", "DATAMIN", + "DATAMAX", "ZIMAGE", +]; + +fn err(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +#[derive(Debug, Clone, PartialEq)] +enum Value { + /// A quoted string, unescaped, trailing blanks removed. + Str(String), + /// Anything else (number, logical, complex) as written. + Raw(String), +} + +#[derive(Debug, Clone)] +struct Card { + key: String, + /// `None` for commentary cards (`COMMENT`, `HISTORY`, blank keyword). + value: Option, + /// The text of a commentary card. + text: String, + /// A `HIERARCH` keyword (`key` holds its long name). + hierarch: bool, +} + +fn parse_value(field: &str) -> Value { + let t = field.trim_start(); + if let Some(rest) = t.strip_prefix('\'') { + let mut s = String::new(); + let mut chars = rest.chars().peekable(); + while let Some(c) = chars.next() { + if c == '\'' { + if chars.peek() == Some(&'\'') { + chars.next(); + s.push('\''); + } else { + break; + } + } else { + s.push(c); + } + } + Value::Str(s.trim_end().to_string()) + } else { + Value::Raw(t.split('/').next().unwrap_or("").trim().to_string()) + } +} + +fn parse_card(raw: &[u8]) -> Card { + // Header bytes are ASCII 32–126 by the standard; anything else is replaced, never trusted. + let s: String = raw.iter().map(|&b| if (32..=126).contains(&b) { b as char } else { '?' }).collect(); + let key_field = s.get(..8).unwrap_or(&s).trim_end().to_string(); + if key_field == "HIERARCH" { + let rest = s.get(9..).unwrap_or(""); + if let Some((k, v)) = rest.split_once('=') { + return Card { key: k.trim().to_string(), value: Some(parse_value(v)), text: String::new(), hierarch: true }; + } + } + if s.get(8..10) == Some("= ") { + let v = parse_value(s.get(10..).unwrap_or("")); + return Card { key: key_field, value: Some(v), text: String::new(), hierarch: false }; + } + Card { key: key_field, value: None, text: s.get(8..).unwrap_or("").trim_end().to_string(), hierarch: false } +} + +struct Header { + cards: Vec, + /// First-occurrence index of each non-HIERARCH keyword, so `get` is O(1). The card + /// count and the number of looked-up keywords (NAXIS1..NAXISn) are both file-controlled, + /// so a linear scan per lookup lets a crafted header multiply commentary cards by axis + /// count into quadratic work. + index: HashMap, + /// Offset of the data unit. + data_start: usize, +} + +impl Header { + fn get(&self, key: &str) -> Option<&Value> { + let c = &self.cards[*self.index.get(key)?]; + c.value.as_ref() + } + + fn int(&self, key: &str) -> Option { + match self.get(key)? { + Value::Raw(r) => r.parse::().ok().or_else(|| self.float(key).filter(|f| f.fract() == 0.0 && f.abs() < 9.0e15).map(|f| f as i64)), + Value::Str(_) => None, + } + } + + fn float(&self, key: &str) -> Option { + match self.get(key)? { + // Fortran-style `D` exponents are allowed in FITS. + Value::Raw(r) => r.replace(['D', 'd'], "E").parse::().ok().filter(|f| f.is_finite()), + Value::Str(_) => None, + } + } + + fn logical(&self, key: &str) -> bool { + matches!(self.get(key), Some(Value::Raw(r)) if r == "T") + } + + fn string(&self, key: &str) -> Option<&str> { + match self.get(key)? { + Value::Str(s) => Some(s.trim()), + Value::Raw(_) => None, + } + } +} + +fn parse_header(b: &[u8], start: usize) -> Result { + let mut cards = Vec::new(); + let mut pos = start; + loop { + let raw = b.get(pos..pos.checked_add(CARD).ok_or_else(|| err("header too long"))?).ok_or_else(|| err("header ends before END"))?; + pos += CARD; + if raw.starts_with(b"END") && raw.get(3..).is_some_and(|r| r.iter().all(|&c| c == b' ')) { + break; + } + let card = parse_card(raw); + // Long strings (FITS 4.0 section 4.2.1.2): a string ending in '&' goes on in the next + // `CONTINUE` record. An orphaned `CONTINUE` stays a card of its own (commentary). + if let Some(Card { value: Some(Value::Str(s)), .. }) = cards.last_mut() + && s.ends_with('&') + && card.key == "CONTINUE" + && card.text.starts_with(" ") + && let Value::Str(more) = parse_value(&card.text) + { + s.pop(); + s.push_str(&more); + continue; + } + cards.push(card); + } + let data_start = pos.div_ceil(BLOCK).checked_mul(BLOCK).ok_or_else(|| err("header too long"))?; + // Index each non-HIERARCH keyword to its first occurrence, matching the previous + // `find(|c| !c.hierarch && c.key == key)` lookup while making `get` O(1). + let mut index = HashMap::with_capacity(cards.len()); + for (i, c) in cards.iter().enumerate() { + if !c.hierarch { + index.entry(c.key.clone()).or_insert(i); + } + } + Ok(Header { cards, index, data_start }) +} + +/// The axis lengths (`NAXIS1`, `NAXIS2`, …). +fn axes(h: &Header) -> Result, CodecError> { + let n = h.int("NAXIS").ok_or_else(|| err("missing NAXIS"))?; + if !(0..=999).contains(&n) { + return Err(err(format!("invalid NAXIS {n}"))); + } + (1..=n).map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))).collect() +} + +/// Bytes in the data unit (before padding to a block). +fn data_len(h: &Header, bitpix: i64) -> Result { + let ax = axes(h)?; + if ax.is_empty() { + return Ok(0); + } + let pcount = u64::try_from(h.int("PCOUNT").unwrap_or(0)).map_err(|_| err("negative PCOUNT"))?; + let gcount = u64::try_from(h.int("GCOUNT").unwrap_or(1)).map_err(|_| err("negative GCOUNT"))?; + let elems = ax.iter().try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("data size overflows"))?; + elems + .checked_add(pcount) + .and_then(|v| v.checked_mul(gcount)) + .and_then(|v| v.checked_mul(bitpix.unsigned_abs() / 8)) + .ok_or_else(|| err("data size overflows")) +} + +/// Inspect every HDU without decoding pixels: one gray plane or three RGB planes are retained, +/// while other cubes, image extensions, tables and other data-bearing HDUs cannot survive a flat save. +/// Incomplete or over-limit walks fail closed rather than treating an unchecked suffix as safe. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + let mut start = 0; + let mut image_seen = false; + for index in 0..MAX_HDUS { + let h = parse_header(bytes, start)?; + if index == 0 && h.get("SIMPLE").is_none() { + return Err(err("missing SIMPLE")); + } + let ax = axes(&h)?; + let size = data_len(&h, h.int("BITPIX").unwrap_or(8))?; + let image_hdu = index == 0 || h.string("XTENSION") == Some("IMAGE"); + if h.logical("ZIMAGE") { + return Ok(true); + } + if !image_hdu && size > 0 { + return Ok(true); + } + // An image HDU with a zero axis has no data (FITS 4.0, NAXISn) and holds no image, like + // the empty primary: `find_image` skips it too. + if image_hdu && size > 0 && ax.len() >= 2 && ax[..2].iter().all(|n| *n > 0) { + let planes = ax.iter().skip(2).try_fold(1u64, |n, axis| n.checked_mul(*axis)).ok_or_else(|| err("plane count overflows"))?; + let retained = planes == 1 || (ax.get(2) == Some(&3) && planes == 3); + if image_seen || !retained || h.int("GCOUNT").unwrap_or(1) > 1 || h.int("PCOUNT").unwrap_or(0) > 0 { + return Ok(true); + } + image_seen = true; + } else if size > 0 { + return Ok(true); + } + let padded = size.div_ceil(BLOCK as u64).checked_mul(BLOCK as u64).ok_or_else(|| err("data size overflows"))?; + let next = usize::try_from(padded).ok().and_then(|size| h.data_start.checked_add(size)).ok_or_else(|| err("data size overflows"))?; + if next > bytes.len() { + return Err(err("truncated HDU data")); + } + if next == bytes.len() { + return Ok(false); + } + start = next; + } + Err(err("too many HDUs to inspect every source image")) +} + +/// Whether an HDU with these axes stores pixels: at least two axes and none of them zero (FITS 4.0, +/// NAXISn: a zero axis means no data follow the header). +fn holds_pixels(ax: &[u64]) -> bool { + ax.len() >= 2 && ax.iter().all(|n| *n > 0) +} + +/// Finds the image HDU: the primary one when it holds pixels, else the first `IMAGE` extension +/// that does. Returns its header and the primary header (for its descriptive cards). +fn find_image(b: &[u8]) -> Result<(Header, Option
), CodecError> { + let primary = parse_header(b, 0)?; + if primary.get("SIMPLE").is_none() { + return Err(err("missing SIMPLE")); + } + if holds_pixels(&axes(&primary)?) { + return Ok((primary, None)); + } + let mut compressed = false; + let mut cur_start = primary.data_start; + let mut cur_len = data_len(&primary, primary.int("BITPIX").unwrap_or(8))?; + for _ in 0..MAX_HDUS { + let next = usize::try_from(cur_len.div_ceil(BLOCK as u64).saturating_mul(BLOCK as u64)) + .ok() + .and_then(|l| cur_start.checked_add(l)) + .ok_or_else(|| err("data size overflows"))?; + if next >= b.len() { + break; + } + let h = parse_header(b, next)?; + let kind = h.string("XTENSION").unwrap_or("").to_string(); + if kind == "IMAGE" && holds_pixels(&axes(&h)?) { + return Ok((h, Some(primary))); + } + if kind == "BINTABLE" && h.logical("ZIMAGE") { + compressed = true; + } + cur_len = data_len(&h, h.int("BITPIX").unwrap_or(8))?; + cur_start = h.data_start; + } + if compressed { + return Err(CodecError::unsupported(F, "tile-compressed FITS (fpack) is not supported; decompress it with funpack first")); + } + Err(err("the file holds no image")) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let (h, primary) = find_image(bytes)?; + let bitpix = h.int("BITPIX").ok_or_else(|| err("missing BITPIX"))?; + if !matches!(bitpix, 8 | 16 | 32 | 64 | -32 | -64) { + return Err(err(format!("invalid BITPIX {bitpix}"))); + } + let ax = axes(&h)?; + let (Some(&w), Some(&hgt)) = (ax.first(), ax.get(1)) else { return Err(err("the image has fewer than two axes")) }; + let w = u32::try_from(w).map_err(|_| CodecError::LimitExceeded(format!("width {w}")))?; + let hgt = u32::try_from(hgt).map_err(|_| CodecError::LimitExceeded(format!("height {hgt}")))?; + let planes = ax.iter().skip(2).try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("too many planes"))?; + let (layout, nplanes) = if ax.get(2) == Some(&3) && planes == 3 { (ChannelLayout::Rgb, 3usize) } else { (ChannelLayout::Gray, 1) }; + + let bscale = h.float("BSCALE").unwrap_or(1.0); + let bzero = h.float("BZERO").unwrap_or(0.0); + let blank = if bitpix > 0 { h.int("BLANK") } else { None }; + let integral = bscale == 1.0 && bzero.fract() == 0.0; + let candidate = match bitpix { + 8 if integral && bzero == 0.0 => SampleType::U8, + 8 | 16 if integral => SampleType::U16, + _ => SampleType::F32, + }; + limits.check(w, hgt, layout, candidate)?; + + let bps = (bitpix.unsigned_abs() / 8) as usize; + let plane_len = (w as usize).checked_mul(hgt as usize).ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{hgt} does not fit in memory")))?; + let n = plane_len.checked_mul(nplanes).ok_or_else(|| CodecError::LimitExceeded("image does not fit in memory".into()))?; + let need = n.checked_mul(bps).and_then(|l| l.checked_add(h.data_start)).ok_or_else(|| err("data size overflows"))?; + let data = bytes.get(h.data_start..need).ok_or_else(|| err("truncated data"))?; + + // Physical value of sample `i` (planar order); `None` for a blank or NaN pixel. + let physical = |i: usize| -> Option { + let s = data.get(i * bps..(i + 1) * bps)?; + let raw = match bitpix { + 8 => s.first().map(|&v| v as i64)?, + 16 => i16::from_be_bytes([s[0], s[1]]) as i64, + 32 => i32::from_be_bytes([s[0], s[1], s[2], s[3]]) as i64, + 64 => i64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]), + -32 => return Some(f32::from_be_bytes([s[0], s[1], s[2], s[3]]) as f64 * bscale + bzero).filter(|v| v.is_finite()), + _ => return Some(f64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]) * bscale + bzero).filter(|v| v.is_finite()), + }; + if blank == Some(raw) { + return None; + } + Some(raw as f64 * bscale + bzero) + }; + + let top_down = h.string("ROWORDER").is_some_and(|r| r.eq_ignore_ascii_case("TOP-DOWN")); + let (wu, hu) = (w as usize, hgt as usize); + // Planar, bottom-up (by default) → interleaved, top-down. + let dest = |i: usize| -> usize { + let (p, rem) = (i / plane_len, i % plane_len); + let (y, x) = (rem / wu, rem % wu); + let y = if top_down { y } else { hu - 1 - y }; + (y * wu + x) * nplanes + p + }; + + let mut img = match candidate { + SampleType::U8 => { + let mut out = vec![0u8; n]; + for i in 0..n { + out[dest(i)] = physical(i).unwrap_or(0.0) as u8; + } + Image::from_u8(w, hgt, layout, out)? + } + _ => { + let mut out16 = if candidate == SampleType::U16 { Some(vec![0u16; n]) } else { None }; + if let Some(out) = out16.as_mut() { + for i in 0..n { + let v = physical(i).unwrap_or(0.0); + if !(0.0..=65535.0).contains(&v) { + // Signed data with negative values: fall back to float. + out16 = None; + break; + } + out[dest(i)] = v as u16; + } + } + match out16 { + Some(out) => Image::from_u16(w, hgt, layout, &out)?, + None => { + let max = (0..n).filter_map(physical).fold(0.0f64, f64::max); + let white = if max <= 2.0 { + 1.0 + } else if max <= 65535.0 { + 65535.0 + } else { + max + }; + let mut out = vec![0f32; n]; + for i in 0..n { + out[dest(i)] = (physical(i).unwrap_or(0.0) / white) as f32; + } + Image::from_f32(w, hgt, layout, &out)? + } + } + } + }; + + if planes > nplanes as u64 { + img.warnings.push(DecodeWarning::MorePages { total: u32::try_from(planes).ok() }); + } + let mut text = Vec::new(); + for c in primary.iter().flat_map(|p| p.cards.iter()).chain(h.cards.iter()) { + if !c.hierarch && (STRUCTURAL.contains(&c.key.as_str()) || is_naxis(&c.key)) { + continue; + } + let entry = match &c.value { + Some(Value::Str(s) | Value::Raw(s)) => (c.key.clone(), s.clone()), + None if c.key.is_empty() && c.text.trim().is_empty() => continue, + None => (if c.key.is_empty() { "COMMENT".to_string() } else { c.key.clone() }, c.text.trim().to_string()), + }; + text.push(entry); + } + img.meta.text = text; + Ok(img) +} + +fn is_naxis(key: &str) -> bool { + key.strip_prefix("NAXIS").is_some_and(|d| d.chars().all(|c| c.is_ascii_digit())) +} + +/// A standard keyword: 1–8 characters from `A-Z 0-9 _ -`. +fn standard_key(key: &str) -> bool { + (1..=8).contains(&key.len()) && key.bytes().all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_' || b == b'-') +} + +fn ascii(s: &str) -> String { + s.chars().map(|c| if (' '..='~').contains(&c) { c } else { '?' }).collect() +} + +/// A FITS literal (number or logical) that can be written unquoted. +fn literal(v: &str) -> bool { + v == "T" || v == "F" || (v.replace(['D', 'd'], "E").parse::().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b))) +} + +fn quoted(v: &str, room: usize) -> String { + let mut s = String::from("'"); + for c in ascii(v).chars() { + let add = if c == '\'' { 2 } else { 1 }; + // Keep room for the closing quote. + if s.len() + add + 1 > room { + break; + } + s.push(c); + if c == '\'' { + s.push('\''); + } + } + // Strings are at least eight characters inside the quotes. + while s.len() < 9 { + s.push(' '); + } + s.push('\''); + s +} + +struct Writer { + out: Vec, +} + +impl Writer { + fn card(&mut self, text: &str) { + let mut c: Vec = ascii(text).into_bytes(); + c.truncate(CARD); + c.resize(CARD, b' '); + self.out.extend_from_slice(&c); + } + + fn value(&mut self, key: &str, v: &str) { + if literal(v) { + self.card(&format!("{key:<8}= {v:>20}")); + return; + } + // A string too long for one card goes on in `CONTINUE` cards, every piece but the last + // ending in '&' (FITS 4.0 section 4.2.1.2). A doubled quote is never split, and trailing + // blanks (not significant) are dropped so none are kept before an '&'. + let escaped = ascii(v).trim_end().replace('\'', "''"); + let mut rest = escaped.as_str(); + let mut head = format!("{key:<8}= "); + while rest.len() > CARD - 12 { + let cut = CARD - 13 - rest.get(..CARD - 13).unwrap_or("").bytes().rev().take_while(|&b| b == b'\'').count() % 2; + let (piece, tail) = rest.split_at_checked(cut).unwrap_or((rest, "")); + self.card(&format!("{head}'{piece}&'")); + head = "CONTINUE ".to_string(); + rest = tail; + } + self.card(&format!("{head}'{rest:<8}'")); + } + + fn commentary(&mut self, key: &str, text: &str) { + let chars: Vec = ascii(text).chars().collect(); + if chars.is_empty() { + self.card(key); + } + for chunk in chars.chunks(CARD - 8) { + self.card(&format!("{key:<8}{}", chunk.iter().collect::())); + } + } + + fn text(&mut self, key: &str, v: &str) { + let upper = key.to_ascii_uppercase(); + if matches!(upper.as_str(), "COMMENT" | "HISTORY") { + self.commentary(&upper, v); + } else if standard_key(key) { + // `CONTINUE` only ever carries a string's continuation, written by `value`. + if !STRUCTURAL.contains(&key) && !is_naxis(key) && key != "CONTINUE" { + self.value(key, v); + } + } else { + // Long or mixed-case names (`Description`, `Software`): the HIERARCH convention, + // when the card fits, else a comment. + let name = ascii(key); + let head = format!("HIERARCH {name} = "); + let fits_key = !name.contains('=') && !name.trim().is_empty(); + let body = if literal(v) { v.to_string() } else { quoted(v, CARD.saturating_sub(head.len())) }; + if fits_key && head.len() + body.len() <= CARD && (literal(v) || body.ends_with('\'')) && quoted_whole(v, &body) { + self.card(&format!("{head}{body}")); + } else { + self.commentary("COMMENT", &format!("{key} = {v}")); + } + } + } +} + +/// `body` holds all of `v` (it was not cut short to fit the card). +fn quoted_whole(v: &str, body: &str) -> bool { + literal(v) || body.trim_matches('\'').trim_end().replace("''", "'") == ascii(v).trim_end() +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let layout = img.layout(); + let nc = match layout { + ChannelLayout::Gray => 1, + ChannelLayout::Rgb => 3, + l => return Err(CodecError::encode(F, format!("FITS cannot store {l:?}"))), + }; + let bitpix = match img.sample_type() { + SampleType::U8 => 8, + SampleType::U16 => 16, + SampleType::F32 => -32, + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + }; + let mut wr = Writer { out: Vec::new() }; + wr.value("SIMPLE", "T"); + wr.value("BITPIX", &bitpix.to_string()); + wr.value("NAXIS", if nc == 3 { "3" } else { "2" }); + wr.value("NAXIS1", &w.to_string()); + wr.value("NAXIS2", &h.to_string()); + if nc == 3 { + wr.value("NAXIS3", "3"); + } + if bitpix == 16 { + wr.value("BZERO", "32768"); + wr.value("BSCALE", "1"); + } + wr.value("ROWORDER", "BOTTOM-UP"); + if opts.embed_metadata { + for (k, v) in &img.meta.text { + wr.text(k, v); + } + } + wr.card("END"); + let mut out = wr.out; + out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' '); + + // Planar, bottom row first. + let (wu, hu) = (w as usize, h as usize); + let idx = |p: usize, y: usize, x: usize| ((hu - 1 - y) * wu + x) * nc + p; + match bitpix { + 8 => { + let d = img.data(); + for p in 0..nc { + for y in 0..hu { + for x in 0..wu { + out.push(d[idx(p, y, x)]); + } + } + } + } + 16 => { + let d = img.to_u16_samples().unwrap_or_default(); + for p in 0..nc { + for y in 0..hu { + for x in 0..wu { + out.extend_from_slice(&((d[idx(p, y, x)] as i32 - 32768) as i16).to_be_bytes()); + } + } + } + } + _ => { + let d = img.to_f32_samples().unwrap_or_default(); + for p in 0..nc { + for y in 0..hu { + for x in 0..wu { + // 0..1 as documented: a value above 2 would make readers (ours too) + // take the data as ADU and scale it by 65535. NaN is written as 0. + let v = d[idx(p, y, x)]; + let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; + out.extend_from_slice(&v.to_be_bytes()); + } + } + } + } + } + out.resize(out.len().div_ceil(BLOCK) * BLOCK, 0); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn header(cards: &[&str]) -> Vec { + let mut w = Writer { out: Vec::new() }; + for c in cards { + w.card(c); + } + w.card("END"); + let mut out = w.out; + out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' '); + out + } + + #[test] + fn continue_strings_decode() { + let mut b = header(&[ + "SIMPLE = T", + "BITPIX = 8", + "NAXIS = 2", + "NAXIS1 = 1", + "NAXIS2 = 1", + "OBJECT = 'aaa&'", + "CONTINUE 'bbb&' / a comment", + "CONTINUE '&'", + "CONTINUE ''", + "OBSERVER= 'R&'", + "COMMENT hi", + "CONTINUE 'orphan'", + ]); + b.push(0); + let img = decode(&b, &Limits::default()).unwrap(); + let text = [("OBJECT", "aaabbb"), ("OBSERVER", "R&"), ("COMMENT", "hi"), ("CONTINUE", "'orphan'")]; + assert_eq!(img.meta.text, text.map(|(k, v)| (k.to_string(), v.to_string()))); + } + + #[test] + fn continue_strings_round_trip() { + for value in ["a".repeat(100), format!("{}' {}", "a".repeat(65), "b".repeat(100)), "'".repeat(100), "a".repeat(68)] { + let mut img = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap(); + img.meta.text = vec![("OBJECT".into(), value.clone()), ("CONTINUE".into(), "orphan".into())]; + let b = crate::encode(&img, F, &EncodeOptions::default()).unwrap(); + assert!(!b.chunks(CARD).any(|c| c.starts_with(b"CONTINUE="))); + let decoded = decode(&b, &Limits::default()).unwrap(); + assert_eq!(decoded.meta.text, vec![("OBJECT".into(), value)]); + } + } + + #[test] + fn camera_u16_with_bzero_reads_exactly_and_bottom_up() { + let mut b = header(&[ + "SIMPLE = T", + "BITPIX = 16", + "NAXIS = 2", + "NAXIS1 = 2", + "NAXIS2 = 2", + "BZERO = 32768", + "OBJECT = 'M 31 '", + "EXPTIME = 300.0 / seconds", + ]); + for v in [0u16, 1000, 40000, 65535] { + b.extend_from_slice(&((v as i32 - 32768) as i16).to_be_bytes()); + } + b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.sample_type(), SampleType::U16); + // The first stored row is the bottom one. + assert_eq!(img.to_u16_samples().unwrap(), vec![40000, 65535, 0, 1000]); + assert!(img.meta.text.contains(&("OBJECT".into(), "M 31".into()))); + assert!(img.meta.text.contains(&("EXPTIME".into(), "300.0".into()))); + assert!(!img.meta.text.iter().any(|(k, _)| k == "BZERO" || k == "NAXIS1")); + } + + #[test] + fn roworder_top_down_is_honoured() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 2", "ROWORDER= 'TOP-DOWN'"]); + b.extend_from_slice(&[10, 20]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[10, 20]); + } + + #[test] + fn stacked_float_in_adu_is_scaled_by_65535() { + let mut b = header(&["SIMPLE = T", "BITPIX = -32", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]); + for v in [65535.0f32, f32::NAN] { + b.extend_from_slice(&v.to_be_bytes()); + } + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.to_f32_samples().unwrap(), vec![1.0, 0.0]); + } + + #[test] + fn hdr_float_saves_clamped_and_reopens_as_saved() { + // One value above 2 used to make the reader scale everything by 65535 (all black). + let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap(); + let opts = EncodeOptions::default(); + let bytes = encode(&img, crate::fidelity::plan(&img, F, &opts), &opts).unwrap(); + let back = decode(&bytes, &Limits::default()).unwrap().to_f32_samples().unwrap(); + assert!((back[0] - 0.5).abs() < 1e-6, "{back:?}"); + assert!((back[1] - 1.0).abs() < 1e-6, "{back:?}"); + } + + #[test] + fn signed_negative_data_falls_back_to_float() { + let mut b = header(&["SIMPLE = T", "BITPIX = 16", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]); + for v in [-100i16, 1000] { + b.extend_from_slice(&v.to_be_bytes()); + } + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.sample_type(), SampleType::F32); + } + + #[test] + fn image_extension_after_empty_primary() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T", "TELESCOP= 'Seestar S50'"]); + b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"])); + b.push(42); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[42]); + assert!(img.meta.text.contains(&("TELESCOP".into(), "Seestar S50".into()))); + } + + /// A zero axis (`NAXIS3 = 0`) means the HDU has no data: it is neither the opened image nor + /// an additional one that Batch Save and Close would discard. + #[test] + fn zero_axis_image_hdus_are_empty() { + let empty = |primary: bool| { + let first = if primary { "SIMPLE = T" } else { "XTENSION= 'IMAGE '" }; + header(&[first, "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 0", "PCOUNT = 0", "GCOUNT = 1"]) + }; + let image = || { + let mut b = header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"]); + b.push(42); + b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0); + b + }; + let mut after_empty_primary = empty(true); + after_empty_primary.extend(image()); + let mut after_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]); + after_empty_extension.extend(empty(false)); + after_empty_extension.extend(image()); + let mut before_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]); + before_empty_extension.extend(image()); + before_empty_extension.extend(empty(false)); + for b in [&after_empty_primary, &after_empty_extension, &before_empty_extension] { + assert!(!crate::has_unretained_source_data(b).unwrap()); + assert_eq!(decode(b, &Limits::default()).unwrap().data(), &[42]); + } + // Only empty HDUs: nothing to open, nothing to lose. + assert!(!crate::has_unretained_source_data(&empty(true)).unwrap()); + assert!(decode(&empty(true), &Limits::default()).is_err()); + // A second non-empty image is still additional data. + let mut two = after_empty_primary.clone(); + two.extend(image()); + assert!(crate::has_unretained_source_data(&two).unwrap()); + } + + #[test] + fn fpack_is_refused_as_unsupported() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); + b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0", "ZIMAGE = T"])); + b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 1", "NAXIS1 = 0"])); + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } + + /// Hostile headers: absurd or negative axes, bad BITPIX, overflowing sizes, missing END, + /// a long chain of empty HDUs and declared data past the end all error, never panic or + /// allocate the declared size. + #[test] + fn hostile_headers_error_without_panicking() { + let base = ["SIMPLE = T", "BITPIX = 16"]; + let cases: &[&[&str]] = &[ + &["NAXIS = 2", "NAXIS1 = 4294967296", "NAXIS2 = 1"], + &["NAXIS = 2", "NAXIS1 = -5", "NAXIS2 = 1"], + &["NAXIS = 2", "NAXIS1 = 2000000", "NAXIS2 = 2000000"], + &["NAXIS = 3", "NAXIS1 = 65536", "NAXIS2 = 65536", "NAXIS3 = 9223372036854775807"], + &["NAXIS = 1000"], + &["NAXIS = -1"], + &["NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0"], + &["NAXIS = 2", "NAXIS1 = 1.5D300", "NAXIS2 = 1"], + &["NAXIS = 2"], + &["NAXIS = 2", "NAXIS1 = 100", "NAXIS2 = 100"], + ]; + for extra in cases { + let cards: Vec<&str> = base.iter().chain(extra.iter()).copied().collect(); + let b = header(&cards); + assert!(decode(&b, &Limits::default()).is_err(), "{extra:?}"); + } + for bitpix in ["0", "7", "-16", "128", "'X'"] { + let card = format!("BITPIX = {bitpix}"); + let b = header(&["SIMPLE = T", &card, "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1"]); + assert!(decode(&b, &Limits::default()).is_err(), "BITPIX {bitpix}"); + } + // No END card: the header runs off the end of the file. + let mut w = Writer { out: Vec::new() }; + for c in ["SIMPLE = T", "BITPIX = 8", "NAXIS = 2"] { + w.card(c); + } + assert!(decode(&w.out, &Limits::default()).is_err()); + // An empty primary followed by many empty extensions: bounded walk, then "no image". + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); + for _ in 0..(MAX_HDUS + 5) { + b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 0"])); + } + assert!(decode(&b, &Limits::default()).is_err()); + // An extension whose declared data size overflows. + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); + b.extend(header(&[ + "XTENSION= 'BINTABLE'", + "BITPIX = 64", + "NAXIS = 2", + "NAXIS1 = 9223372036854775807", + "NAXIS2 = 4", + "PCOUNT = 0", + "GCOUNT = 1", + ])); + assert!(decode(&b, &Limits::default()).is_err()); + } + + #[test] + fn extra_planes_warn() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 4"]); + b.extend_from_slice(&[1, 2, 3, 4]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.warnings, vec![DecodeWarning::MorePages { total: Some(4) }]); + } + + #[test] + fn strings_with_quotes_and_hierarch_round_trip() { + let mut w = Writer { out: Vec::new() }; + w.text("OBSERVER", "O'Brien"); + w.text("Software", "photocraft 1.0"); + w.text("HISTORY", "stacked"); + let cards: Vec = w.out.chunks(CARD).map(parse_card).collect(); + assert_eq!(cards[0].value, Some(Value::Str("O'Brien".into()))); + assert!(cards[1].hierarch && cards[1].key == "Software"); + assert_eq!(cards[1].value, Some(Value::Str("photocraft 1.0".into()))); + assert_eq!((cards[2].key.as_str(), cards[2].text.as_str()), ("HISTORY", "stacked")); + } +} diff --git a/crates/image-codecs/src/codecs/gif_frames.rs b/crates/image-codecs/src/codecs/gif_frames.rs new file mode 100644 index 000000000..b5439f6ef --- /dev/null +++ b/crates/image-codecs/src/codecs/gif_frames.rs @@ -0,0 +1,82 @@ +//! Every frame of an animated GIF. [`crate::decode`] keeps the first frame only; this walks the +//! whole animation for callers that can hold it (video layers). +//! +//! The `image` crate composites each frame onto the logical screen and applies the disposal +//! methods, so every returned frame is a full, self-contained picture. + +use std::io::Cursor; + +use image::codecs::gif::GifDecoder; +use image::{AnimationDecoder, ImageDecoder}; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, DecodeWarning, Image}; +use crate::options::DecodeOptions; + +/// Most frames one GIF (or animated WebP) may yield. Frames are held in memory, and a tiny +/// canvas could otherwise ask for millions of them while staying inside the byte budget. +pub const MAX_GIF_FRAMES: usize = 100_000; + +/// One composited frame of an animation and how long it stays on screen. +#[derive(Debug, Clone, PartialEq)] +pub struct AnimationFrame { + pub image: Image, + /// On-screen time in milliseconds. GIF counts in hundredths of a second, so there this is a + /// multiple of 10 (0 when the file gives none); WebP counts in milliseconds. + pub delay_ms: u32, +} + +/// Decodes every frame of a GIF, each as a full RGBA8 image of the logical screen size. +/// +/// The first frame must decode. If a later frame is damaged the frames before it are returned +/// and the first one carries [`DecodeWarning::Truncated`]. Dimensions, the frame count +/// ([`MAX_GIF_FRAMES`]) and the total decoded bytes are checked against `opts.limits` before +/// each frame is kept. +pub fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result, CodecError> { + let limits = &opts.limits; + let map = |e: image::ImageError| match e { + image::ImageError::Limits(l) => CodecError::LimitExceeded(l.to_string()), + image::ImageError::Unsupported(u) => CodecError::unsupported(Format::Gif, u.to_string()), + e => CodecError::malformed(Format::Gif, e), + }; + let mut decoder = GifDecoder::new(Cursor::new(bytes)).map_err(map)?; + let mut il = image::Limits::default(); + il.max_image_width = Some(limits.max_width); + il.max_image_height = Some(limits.max_height); + il.max_alloc = Some(limits.max_alloc); + decoder.set_limits(il).map_err(map)?; + let (w, h) = decoder.dimensions(); + limits.check_bytes(w, h, 4)?; + // The backend initializes the whole logical screen before it reads the first frame, so the + // same file-size plausibility as the first-frame decode applies before iterating (#8597). + super::via_image::gif_plausible(bytes, w, h, 4)?; + let frame_bytes = u64::from(w) * u64::from(h) * 4; + + let mut out: Vec = Vec::new(); + let mut damaged = false; + for frame in decoder.into_frames() { + let frame = match frame { + Ok(f) => f, + Err(e) if out.is_empty() => return Err(map(e)), + Err(_) => { + damaged = true; + break; + } + }; + if out.len() >= MAX_GIF_FRAMES { + return Err(CodecError::LimitExceeded(format!("more than {MAX_GIF_FRAMES} frames"))); + } + if frame_bytes.saturating_mul(out.len() as u64 + 1) > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("{} frames of {w}x{h} exceed max_alloc {}", out.len() + 1, limits.max_alloc))); + } + let (numer, denom) = frame.delay().numer_denom_ms(); + let delay_ms = numer.checked_div(denom).unwrap_or(0); + let rgba = frame.into_buffer(); + out.push(AnimationFrame { image: Image::from_u8(w, h, ChannelLayout::Rgba, rgba.into_raw())?, delay_ms }); + } + if damaged && let Some(first) = out.first_mut() { + first.image.warnings.push(DecodeWarning::Truncated { format: Format::Gif }); + } + Ok(out) +} diff --git a/crates/image-codecs/src/codecs/gif_palette.rs b/crates/image-codecs/src/codecs/gif_palette.rs new file mode 100644 index 000000000..eb0a0e218 --- /dev/null +++ b/crates/image-codecs/src/codecs/gif_palette.rs @@ -0,0 +1,126 @@ +//! The colour table of a GIF, read from its blocks without decoding pixels (GIF89a +//! specification: Logical Screen Descriptor, Global and Local Color Tables, Graphic Control +//! Extension). +//! +//! A GIF is always palette-based, so it opens in Indexed Color with the table of the frame that +//! [`crate::decode`] keeps, the first: its Local Color Table when it has one, else the Global +//! one, and the transparent entry that frame's Graphic Control Extension names. + +/// Colours of the table the first frame uses, and its transparent entry. `None` for anything +/// that isn't a well-formed GIF up to its first image, or a first frame without a table. +pub(crate) fn palette(b: &[u8]) -> Option<(Vec<[u8; 3]>, Option)> { + if !(b.starts_with(b"GIF87a") || b.starts_with(b"GIF89a")) { + return None; + } + // Logical Screen Descriptor: width, height, packed fields, background, aspect. + let packed = *b.get(10)?; + let mut at = 13; + let table = |at: usize, packed: u8| -> Option> { + let n = 2usize << (packed & 0x07); + let bytes = b.get(at..at.checked_add(n.checked_mul(3)?)?)?; + Some(bytes.as_chunks::<3>().0.to_vec()) + }; + let global = if packed & 0x80 != 0 { + let t = table(at, packed)?; + at += t.len() * 3; + Some(t) + } else { + None + }; + let mut transparent = None; + loop { + match *b.get(at)? { + // Extension: label, then data sub-blocks up to a zero-length one. + 0x21 => { + let label = *b.get(at + 1)?; + at += 2; + // Graphic Control Extension: block size 4, packed (bit 0: transparent colour + // given), delay (2 bytes), transparent colour index. + if label == 0xF9 && *b.get(at)? == 4 { + let flags = *b.get(at + 1)?; + transparent = (flags & 0x01 != 0).then_some(*b.get(at + 4)?); + } + loop { + let size = usize::from(*b.get(at)?); + at = at.checked_add(1 + size)?; + if size == 0 { + break; + } + } + } + // Image Descriptor: position and size (8 bytes), packed fields, then the Local Color + // Table if bit 7 is set. This is the first frame: its table is the one used. + 0x2C => { + let packed = *b.get(at + 9)?; + let colors = if packed & 0x80 != 0 { table(at + 10, packed)? } else { global? }; + let transparent = transparent.filter(|i| usize::from(*i) < colors.len()); + return Some((colors, transparent)); + } + // Trailer, or anything else: no image. + _ => return None, + } + } +} + +#[cfg(test)] +mod tests { + use super::palette; + + /// A GIF up to its first image: `global` table entries, optional GCE transparency, optional + /// local table. + fn gif(global: &[[u8; 3]], transparent: Option, local: Option<&[[u8; 3]]>) -> Vec { + let size_bits = |n: usize| (n.max(2).next_power_of_two().trailing_zeros() - 1) as u8; + let mut b = b"GIF89a".to_vec(); + b.extend([4, 0, 4, 0]); + b.push(if global.is_empty() { 0 } else { 0x80 | size_bits(global.len()) }); + b.extend([0, 0]); + let pad = |b: &mut Vec, t: &[[u8; 3]]| { + let n = 2usize << size_bits(t.len()); + for k in 0..n { + b.extend(t.get(k).copied().unwrap_or([0, 0, 0])); + } + }; + if !global.is_empty() { + pad(&mut b, global); + } + // A comment extension first, to skip. + b.extend([0x21, 0xFE, 3, b'h', b'i', b'!', 0]); + if let Some(i) = transparent { + b.extend([0x21, 0xF9, 4, 0x01, 0, 0, i, 0]); + } + b.extend([0x2C, 0, 0, 0, 0, 4, 0, 4, 0]); + match local { + Some(t) => { + b.push(0x80 | size_bits(t.len())); + pad(&mut b, t); + } + None => b.push(0), + } + b.extend([2, 2, 0x4C, 0x01, 0, 0x3B]); + b + } + + #[test] + fn the_first_frames_table_and_transparent_entry() { + let g = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]]; + assert_eq!(palette(&gif(&g, None, None)), Some((g.to_vec(), None))); + assert_eq!(palette(&gif(&g, Some(2), None)), Some((g.to_vec(), Some(2)))); + // A local table wins over the global one. + let l = [[1, 2, 3], [4, 5, 6]]; + assert_eq!(palette(&gif(&g, Some(1), Some(&l))), Some((l.to_vec(), Some(1)))); + // A transparent index past the table is no transparent entry. + assert_eq!(palette(&gif(&l, Some(9), None)), Some((l.to_vec(), None))); + } + + #[test] + fn no_table_or_not_a_gif_is_none() { + assert_eq!(palette(&gif(&[], None, None)), None, "no table at all"); + assert_eq!(palette(b"\x89PNG\r\n\x1a\n"), None); + assert_eq!(palette(b""), None); + let whole = gif(&[[9, 9, 9], [8, 8, 8]], Some(0), None); + for cut in 0..whole.len() - 6 { + // Cut short anywhere before the image descriptor's fields: never a panic. + let _ = palette(&whole[..cut]); + } + } +} diff --git a/crates/image-codecs/src/codecs/heif.rs b/crates/image-codecs/src/codecs/heif.rs new file mode 100644 index 000000000..4fde27cf1 --- /dev/null +++ b/crates/image-codecs/src/codecs/heif.rs @@ -0,0 +1,126 @@ +//! HEIF / HEIC (the iPhone and Mac photo format), read-only: see [`crate::ASYMMETRIC_EXCEPTIONS`]. +//! +//! Decoding lives in the optional `craft-heif` crate (heic-rs, a pure-Rust HEVC +//! still-picture decoder), enabled by this crate's `heif` feature. Without it, HEIF files are +//! still detected and opening one is an [`CodecError::Unsupported`] error, never a panic. +//! +//! HEIF records orientation in the container (`irot`/`imir`, plus a `clap` crop), not in EXIF, +//! so those are applied by the decoder and the EXIF Orientation is rewritten to 1. Its EXIF tag +//! only mirrors what the container already says, and applying it as well would turn the photo +//! twice. + +use crate::Format; +use crate::error::CodecError; +use crate::image::Image; +use crate::options::Limits; + +const F: Format = Format::Heif; + +/// The reason given when this build has no HEIF decoder. +#[cfg(not(feature = "heif"))] +pub(crate) const NOT_IN_BUILD: &str = "HEIC/HEIF support isn't included in this build"; + +#[cfg(not(feature = "heif"))] +pub(crate) fn decode(_bytes: &[u8], _limits: &Limits, _keep_orientation: bool) -> Result { + Err(CodecError::unsupported(F, NOT_IN_BUILD)) +} + +#[cfg(feature = "heif")] +fn err(e: craft_heif::Error) -> CodecError { + match e { + craft_heif::Error::Unsupported(what) => CodecError::unsupported(F, what), + craft_heif::Error::Limit(what) => CodecError::LimitExceeded(what), + craft_heif::Error::Malformed(what) => CodecError::malformed(F, what), + } +} + +#[cfg(feature = "heif")] +pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> Result { + use crate::cicp::Cicp; + use crate::image::{ChannelLayout, Metadata, SampleType}; + + // The container alone: the declared size is checked before any pixel is decoded. + let info = craft_heif::probe(bytes).map_err(err)?; + let layout = if info.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + let sample = if info.bit_depth > 8 { SampleType::U16 } else { SampleType::U8 }; + let (w, h) = if keep_orientation { (info.coded_width, info.coded_height) } else { (info.width, info.height) }; + limits.check(w, h, layout, sample)?; + // craft-heif returns 16-bit samples for every coded depth. Bound that intermediate, including + // the coded canvas before any clean-aperture crop or orientation transform. + limits.check(info.coded_width, info.coded_height, layout, SampleType::U16)?; + // The delegated decoder allocates the full coded image before applying any clean-aperture + // crop or orientation transform, so the coded dimensions must pass the limit policy even + // when the displayed (transformed) size is small. Checking only the transformed size let a + // tiny visible image hide a large coded allocation. + if !keep_orientation && (info.coded_width, info.coded_height) != (w, h) { + limits.check(info.coded_width, info.coded_height, layout, sample)?; + } + let options = craft_heif::Options { max_pixels: limits.max_pixels, apply_transforms: !keep_orientation, ..Default::default() }; + let decoded = craft_heif::decode(bytes, &options).map_err(err)?; + // The decoder re-reads the container; trust its output's shape, not the probe's. + let layout = if decoded.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + let sample = if decoded.bit_depth > 8 { SampleType::U16 } else { SampleType::U8 }; + let image = Image::from_u16(decoded.width, decoded.height, layout, &decoded.data)?; + let mut img = if sample == SampleType::U16 { image } else { image.convert(layout, SampleType::U8) }; + img.icc = decoded.icc; + let colour = decoded.colour; + img.cicp = Some(Cicp { primaries: colour.primaries, transfer: colour.transfer, matrix: colour.matrix, full_range: colour.full_range }); + let (exif, xmp) = craft_heif::read_metadata(bytes).map_err(err)?; + img.meta = Metadata { + exif: exif.map(|e| if keep_orientation { e } else { crate::orientation::upright_exif(&e).into_owned() }), + xmp: xmp.map(|x| if keep_orientation { x } else { crate::orientation::upright_xmp(&x).into_owned() }), + ..Default::default() + }; + // A 10-bit HDR HEIC (BT.2100 HLG or PQ) opens as linear light in 32-bit float (#6752): no + // ICC curve can describe those transfers, and an 8/16-bit document would clip the highlights. + match img.cicp.filter(|c| c.is_hdr()) { + Some(c) => { + limits.check(decoded.width, decoded.height, layout, SampleType::F32)?; + crate::cicp::linearize(img, c) + } + None => Ok(img), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[cfg(not(feature = "heif"))] + #[test] + fn without_the_feature_heif_is_a_clear_unsupported_error() { + let header = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"; + assert_eq!(crate::detect(header), Some(F), "still recognised"); + let r = crate::decode(header); + assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("isn't included in this build")), "{r:?}"); + assert!(!crate::caps(F).read); + } + + #[cfg(feature = "heif")] + #[test] + fn with_the_feature_heif_is_readable_and_errors_map() { + assert!(crate::caps(F).read); + let r = crate::decode(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"); + assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("sequence")), "{r:?}"); + assert!(matches!(crate::decode_as(F, b""), Err(CodecError::Malformed { .. } | CodecError::Unsupported { .. }))); + } + + /// Found by the `decode_heif` fuzz target: a malformed box makes heic-rs 0.1.1 slice out of range + /// (`boxes.rs:130`, "slice index starts at 24 but ends at 16"). It must be an error, not a crash. + #[cfg(feature = "heif")] + const HEIC_RS_BOX_PANIC: [u8; 72] = [ + 0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50, 0x72, 0x6d, + 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70, 0x00, 0xa8, 0x00, + ]; + + #[cfg(feature = "heif")] + #[test] + fn a_heic_rs_panic_is_a_malformed_file_error() { + assert_eq!(crate::detect(&HEIC_RS_BOX_PANIC), Some(Format::Heif)); + for opts in [crate::DecodeOptions::default(), crate::DecodeOptions { keep_orientation: true, ..Default::default() }] { + let r = crate::decode_with(&HEIC_RS_BOX_PANIC, &opts); + assert!(matches!(r, Err(CodecError::Malformed { format: Format::Heif, .. })), "{r:?}"); + } + } +} diff --git a/crates/image-codecs/src/codecs/iff.rs b/crates/image-codecs/src/codecs/iff.rs new file mode 100644 index 000000000..4997817ca --- /dev/null +++ b/crates/image-codecs/src/codecs/iff.rs @@ -0,0 +1,544 @@ +//! Amiga IFF/ILBM reader and writer (EA "Interchange File Format", "InterLeaved BitMap" and the +//! chunky "PBM " variant). Written from the published IFF/ILBM layout: a `FORM` chunk of four-byte +//! chunks (`BMHD` bitmap header, `CMAP` palette, `CAMG` Amiga viewport mode, `BODY` pixel data). +//! +//! Read: 1–8 bitplanes with a palette (or a gray ramp when there is none) with masking 0/1/2, and +//! 24-bitplane "deep" ILBMs (8 planes each of red, green and blue, least significant bit first, +//! no palette) with masking 0/1, both stored (0) and ByteRun1/PackBits (1) compressed. Anything +//! else is a clear `Unsupported`/`Malformed` error, never a panic. +//! +//! Write: a 24-bitplane ILBM, ByteRun1 compressed one plane row at a time, with a mask plane +//! (masking 1) when the image has alpha. The mask plane is one bit per pixel: alpha of 128 or +//! more is opaque. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Iff; + +fn bad(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +fn unsupported(msg: impl Into) -> CodecError { + CodecError::unsupported(F, msg) +} + +fn be32(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(4)?)?.try_into().ok()?; + Some(u32::from_be_bytes(s)) +} + +fn be16(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(2)?)?.try_into().ok()?; + Some(u16::from_be_bytes(s)) +} + +/// `FORM` + a bitmap type (`ILBM` interleaved, `PBM ` chunky) at offset 8. +pub(crate) fn looks_like_iff(b: &[u8]) -> bool { + b.get(0..4) == Some(b"FORM") && matches!(b.get(8..12), Some(b"ILBM" | b"PBM ")) +} + +/// Bitplanes of a "deep" ILBM: red, green and blue, 8 each, least significant bit first. +const DEEP_PLANES: u8 = 24; + +/// The bitmap header fields decoding needs. +struct Header { + width: u32, + height: u32, + planes: u8, + masking: u8, + compression: u8, + transparent: u16, + /// `PBM ` stores one palette index per byte, padded to even row lengths. + chunky: bool, +} + +/// One four-byte chunk's id and byte range. +struct Chunk<'a> { + id: [u8; 4], + data: &'a [u8], +} + +/// Walk the `FORM`'s chunks from offset 12 to `end` (the form length), stopping at the first +/// malformed length so a hostile file can't run past the buffer. +fn chunks(b: &[u8]) -> impl Iterator> { + let end = be32(b, 4).map_or(b.len(), |n| (n as usize).saturating_add(8).min(b.len())); + let mut pos = 12usize; + std::iter::from_fn(move || { + if pos + 8 > end { + return None; + } + let id = b.get(pos..pos + 4)?.try_into().ok()?; + let len = be32(b, pos + 4)? as usize; + let start = pos + 8; + let data = b.get(start..start.checked_add(len)?)?; + // Chunks are padded to an even length. + pos = start.checked_add(len)?.checked_add(len & 1)?; + Some(Chunk { id, data }) + }) +} + +fn parse_header(b: &[u8], chunky: bool) -> Result { + let bmhd = chunks(b).find(|c| &c.id == b"BMHD").ok_or_else(|| bad("no BMHD chunk"))?; + let d = bmhd.data; + if d.len() < 20 { + return Err(bad("BMHD chunk is too short")); + } + let width = u32::from(be16(d, 0).ok_or_else(|| bad("BMHD width"))?); + let height = u32::from(be16(d, 2).ok_or_else(|| bad("BMHD height"))?); + let planes = *d.get(8).ok_or_else(|| bad("BMHD planes"))?; + let masking = *d.get(9).ok_or_else(|| bad("BMHD masking"))?; + let compression = *d.get(10).ok_or_else(|| bad("BMHD compression"))?; + if width == 0 || height == 0 { + return Err(bad("zero-sized bitmap")); + } + if !(1..=8).contains(&planes) && planes != DEEP_PLANES { + return Err(unsupported(format!("{planes} bitplanes (1–8 with a palette, or 24)"))); + } + if masking > 2 { + return Err(unsupported(format!("masking {masking}"))); + } + if planes == DEEP_PLANES && masking == 2 { + return Err(unsupported("a transparent color in a 24-bitplane ILBM")); + } + if compression > 1 { + return Err(unsupported(format!("compression {compression} (only stored and ByteRun1)"))); + } + if chunky && (planes != 8 || masking == 1) { + return Err(unsupported("PBM requires 8-bit indices without a mask plane")); + } + let transparent = be16(d, 12).ok_or_else(|| bad("BMHD transparent colour"))?; + Ok(Header { width, height, planes, masking, compression, transparent, chunky }) +} + +/// ByteRun1 / PackBits decode of one plane row into `out`; returns the bytes consumed. A `0x80` +/// control byte is a no-op, as the layout specifies. +fn unpack_byte_run1(src: &[u8], out: &mut [u8]) -> Result { + let (mut i, mut o) = (0usize, 0usize); + while o < out.len() { + let n = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 data"))? as i8; + i += 1; + if n >= 0 { + let count = n as usize + 1; + let lit = src.get(i..i.checked_add(count).ok_or_else(|| bad("ByteRun1 overflow"))?).ok_or_else(|| bad("truncated ByteRun1 run"))?; + if count > out.len() - o { + return Err(bad("ByteRun1 run crosses the plane row")); + } + out[o..o + count].copy_from_slice(lit); + i += count; + o += count; + } else if n != -128 { + let count = (1 - n as i32) as usize; + let value = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 repeat"))?; + i += 1; + if count > out.len() - o { + return Err(bad("ByteRun1 repeat crosses the plane row")); + } + out[o..o + count].fill(value); + o += count; + } + } + Ok(i) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_iff(b) { + return Err(bad("not an IFF/ILBM file (no FORM ... ILBM)")); + } + let h = parse_header(b, b.get(8..12) == Some(b"PBM "))?; + if let Some(camg) = chunks(b).find(|c| &c.id == b"CAMG") { + let mode = be32(camg.data, 0).ok_or_else(|| bad("short CAMG chunk"))?; + if mode & (0x80 | 0x800) != 0 { + return Err(unsupported("EHB and HAM viewport modes")); + } + } + let layout = if h.masking == 0 { ChannelLayout::Rgb } else { ChannelLayout::Rgba }; + limits.check(h.width, h.height, layout, SampleType::U8)?; + + // Palette: three bytes per colour; missing entries read as black. + let cmap = chunks(b).find(|c| &c.id == b"CMAP").map(|c| c.data).filter(|p| !p.is_empty()); + let colors = 1usize << h.planes; + let palette = |idx: usize| -> [u8; 3] { + if let Some(p) = cmap { + p.get(idx * 3..idx * 3 + 3).map(|c| [c[0], c[1], c[2]]).unwrap_or([0; 3]) + } else { + let v = ((idx as u32 * 255) / (colors as u32 - 1).max(1)) as u8; + [v, v, v] + } + }; + + let body = chunks(b).find(|c| &c.id == b"BODY").map(|c| c.data).ok_or_else(|| bad("no BODY chunk"))?; + let (w, hh) = (h.width as usize, h.height as usize); + // Each plane row is stored as a whole number of 16-bit words. + let plane_row = if h.chunky { w.div_ceil(2) * 2 } else { w.div_ceil(16) * 2 }; + // A masking type 1 bitmap carries one extra (mask) plane per row. + let rows_planes: usize = if h.chunky { 1 } else { usize::from(h.planes) + usize::from(h.masking == 1) }; + + // Decode every stored plane row up front, then recombine. + let total_rows = hh.checked_mul(rows_planes).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?; + let total_bytes = total_rows.checked_mul(plane_row).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?; + if total_bytes as u64 > limits.max_alloc { + return Err(CodecError::LimitExceeded("bitmap storage exceeds max_alloc".into())); + } + Limits::check_plausible(total_bytes as u64, body.len(), if h.compression == 0 { 1 } else { 128 })?; + let mut stored = crate::options::alloc_zeroed::(total_bytes)?; + let mut cursor = 0usize; + for row in stored.chunks_mut(plane_row.max(1)).take(total_rows) { + match h.compression { + 0 => { + let src = body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?; + let n = row.len().min(src.len()); + row[..n].copy_from_slice(&src[..n]); + cursor += plane_row; + } + _ => { + let tail = body.get(cursor..).ok_or_else(|| bad("truncated BODY data"))?; + cursor += unpack_byte_run1(tail, row)?; + } + } + } + + let mut out = Vec::new(); + out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; + let planes = usize::from(h.planes); + for y in 0..hh { + for x in 0..w { + let index = if h.chunky { + usize::from(*stored.get(y * plane_row + x).ok_or_else(|| bad("PBM pixel outside BODY"))?) + } else { + (0..planes).filter(|&p| bit_at(&stored, (y * rows_planes + p) * plane_row, x)).fold(0usize, |acc, p| acc | (1 << p)) + }; + let c = if h.planes == DEEP_PLANES { [index as u8, (index >> 8) as u8, (index >> 16) as u8] } else { palette(index) }; + out.extend_from_slice(&c); + if h.masking != 0 { + let opaque = if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) }; + out.push(if opaque { 255 } else { 0 }); + } + } + } + Image::from_u8(h.width, h.height, layout, out) +} + +/// The bit for pixel `x` of a stored plane row at `base` (bits run MSB-first). +fn bit_at(stored: &[u8], base: usize, x: usize) -> bool { + stored.get(base + x / 8).is_some_and(|byte| byte & (0x80 >> (x % 8)) != 0) +} + +/// ByteRun1 / PackBits encode of one plane row, appended to `out`: three or more equal bytes +/// become a repeat (at most 128), everything else literal runs (at most 128 bytes). +fn pack_byte_run1(row: &[u8], out: &mut Vec) { + let mut i = 0usize; + while let Some(&value) = row.get(i) { + let repeat = row[i..].iter().take(128).take_while(|&&b| b == value).count(); + if repeat >= 3 { + out.push((1 - repeat as i32) as i8 as u8); + out.push(value); + i += repeat; + continue; + } + let start = i; + while i < row.len() && i - start < 128 { + if row.get(i..i + 3).is_some_and(|r| r[0] == r[1] && r[1] == r[2]) { + break; + } + i += 1; + } + out.push((i - start - 1) as u8); + out.extend_from_slice(&row[start..i]); + } +} + +fn push_chunk(out: &mut Vec, id: &[u8; 4], data: &[u8]) -> Result<(), CodecError> { + let len = u32::try_from(data.len()).map_err(|_| CodecError::encode(F, "chunk too large for an IFF file (4 GiB)"))?; + out.extend_from_slice(id); + out.extend_from_slice(&len.to_be_bytes()); + out.extend_from_slice(data); + // Chunks are padded to an even length. + if data.len() % 2 == 1 { + out.push(0); + } + Ok(()) +} + +/// Writes a 24-bitplane `FORM ILBM`: `BMHD` then a ByteRun1-compressed `BODY`, each row stored as +/// the red, green and blue planes (least significant bit first) and, for RGBA, a mask plane. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + let masked = match img.layout() { + ChannelLayout::Rgb => false, + ChannelLayout::Rgba => true, + l => return Err(enc(format!("IFF/ILBM cannot store {l:?}"))), + }; + if img.sample_type() != SampleType::U8 { + return Err(enc(format!("IFF/ILBM cannot store {:?} samples", img.sample_type()))); + } + let (w, h) = img.dimensions(); + let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else { + return Err(enc("IFF/ILBM stores at most 65535 × 65535 pixels".into())); + }; + let (w, ch) = (w as usize, img.layout().channels()); + let data = img.data(); + let row_len = w.checked_mul(ch).ok_or_else(|| enc("image too large".into()))?; + if data.len() != row_len.checked_mul(h as usize).ok_or_else(|| enc("image too large".into()))? { + return Err(enc("the image has the wrong number of samples".into())); + } + let planes = usize::from(DEEP_PLANES) + usize::from(masked); + let mut plane_row = vec![0u8; w.div_ceil(16) * 2]; + let mut body = Vec::new(); + for px in data.chunks_exact(row_len.max(1)) { + for plane in 0..planes { + plane_row.fill(0); + for (x, p) in px.chunks_exact(ch).enumerate() { + let on = if plane == usize::from(DEEP_PLANES) { p[3] >= 128 } else { (p[plane / 8] >> (plane % 8)) & 1 == 1 }; + if on { + plane_row[x / 8] |= 0x80 >> (x % 8); + } + } + pack_byte_run1(&plane_row, &mut body); + } + } + + let mut bmhd = Vec::with_capacity(20); + bmhd.extend_from_slice(&w16.to_be_bytes()); + bmhd.extend_from_slice(&h16.to_be_bytes()); + bmhd.extend_from_slice(&[0, 0, 0, 0]); // x/y origin + bmhd.push(DEEP_PLANES); + bmhd.push(u8::from(masked)); // masking: 1 = a mask plane follows the color planes + bmhd.push(1); // ByteRun1 + bmhd.push(0); // pad + bmhd.extend_from_slice(&[0, 0]); // transparent color (unused) + bmhd.extend_from_slice(&[1, 1]); // square pixels + bmhd.extend_from_slice(&w16.to_be_bytes()); // page size + bmhd.extend_from_slice(&h16.to_be_bytes()); + + let mut out = b"FORM".to_vec(); + out.extend_from_slice(&[0; 4]); // patched below + out.extend_from_slice(b"ILBM"); + push_chunk(&mut out, b"BMHD", &bmhd)?; + push_chunk(&mut out, b"BODY", &body)?; + let form_len = u32::try_from(out.len() - 8).map_err(|_| enc("image too large for an IFF file (4 GiB)".into()))?; + out[4..8].copy_from_slice(&form_len.to_be_bytes()); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A `FORM ILBM` with a `BMHD`, `CMAP` and `BODY`. `planes` plane rows per row, + /// `compression` 0 (stored) or 1 (ByteRun1), MSB-first. + fn ilbm(w: u16, h: u16, planes: u8, palette: &[[u8; 3]], compression: u8, body: &[u8]) -> Vec { + let mut b = b"FORM".to_vec(); + b.extend_from_slice(&0u32.to_be_bytes()); // patched below + b.extend_from_slice(b"ILBM"); + // BMHD (20 bytes) + b.extend_from_slice(b"BMHD"); + b.extend_from_slice(&20u32.to_be_bytes()); + b.extend_from_slice(&w.to_be_bytes()); + b.extend_from_slice(&h.to_be_bytes()); + b.extend_from_slice(&[0, 0, 0, 0]); // x/y origin + b.push(planes); + b.push(0); // masking + b.push(compression); + b.push(0); // pad + b.extend_from_slice(&[0, 0]); // transparent colour + b.extend_from_slice(&[1, 1]); // aspect + b.extend_from_slice(&[0, 0, 0, 0]); // page size + // CMAP + b.extend_from_slice(b"CMAP"); + b.extend_from_slice(&((palette.len() * 3) as u32).to_be_bytes()); + for c in palette { + b.extend_from_slice(c); + } + if palette.len() % 2 == 1 { + b.push(0); + } + // BODY + b.extend_from_slice(b"BODY"); + b.extend_from_slice(&(body.len() as u32).to_be_bytes()); + b.extend_from_slice(body); + if body.len() % 2 == 1 { + b.push(0); + } + let size = (b.len() - 8) as u32; + b[4..8].copy_from_slice(&size.to_be_bytes()); + b + } + + #[test] + fn reads_a_stored_two_plane_bitmap() { + // 2×1, 2 planes: pixel 0 index 0 (plane bits 0,0), pixel 1 index 3 (bits 1,1). + // Plane 0 = 0b0100_0000, plane 1 = 0b0100_0000, each padded to 2 bytes. + let body = [0b0100_0000, 0, 0b0100_0000, 0]; + let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]]; + let b = ilbm(2, 1, 2, &pal, 0, &body); + assert!(looks_like_iff(&b)); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); + assert_eq!(img.data(), &[10, 20, 30, 100, 110, 120]); + } + + #[test] + fn reads_byte_run1_compressed_rows() { + // Same image, ByteRun1: literal run of two bytes (0x01) then the two plane bytes each row. + let body = [0x01, 0b0100_0000, 0, 0x01, 0b0100_0000, 0]; + let pal = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [250, 251, 252]]; + let b = ilbm(2, 1, 2, &pal, 1, &body); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[1, 2, 3, 250, 251, 252]); + } + + #[test] + fn no_palette_falls_back_to_a_grey_ramp() { + // 2×1, 1 plane, no CMAP: indices 0 and 1 → black and white. + let body = [0b0100_0000, 0]; + let b = ilbm(2, 1, 1, &[], 0, &body); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[0, 0, 0, 255, 255, 255]); + } + + #[test] + fn pbm_reads_chunky_indices_with_odd_width_padding() { + let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]; + for (compression, body) in [(0, vec![2, 0, 1, 255, 1, 2, 0, 255]), (1, vec![3, 2, 0, 1, 255, 3, 1, 2, 0, 255])] { + let mut b = ilbm(3, 2, 8, &pal, compression, &body); + b[8..12].copy_from_slice(b"PBM "); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[70, 80, 90, 10, 20, 30, 40, 50, 60, 40, 50, 60, 70, 80, 90, 10, 20, 30]); + } + } + + #[test] + fn transparency_uses_the_mask_plane_or_transparent_index() { + for masking in [1, 2] { + let mut b = ilbm(2, 1, 1, &[[10, 20, 30], [40, 50, 60]], 0, if masking == 1 { &[0x40, 0, 0x40, 0] } else { &[0x40, 0] }); + b[29] = masking; + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert_eq!(img.data(), &[10, 20, 30, 0, 40, 50, 60, 255]); + } + let mut b = ilbm(2, 1, 8, &[[10, 20, 30], [40, 50, 60]], 0, &[0, 1]); + b[8..12].copy_from_slice(b"PBM "); + b[29] = 2; + b[32..34].copy_from_slice(&1u16.to_be_bytes()); + assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[10, 20, 30, 255, 40, 50, 60, 0]); + } + + #[test] + fn rejects_ham_and_ehb_instead_of_returning_wrong_colours() { + for mode in [0x80u32, 0x800] { + let mut b = ilbm(1, 1, 1, &[], 0, &[0, 0]); + b.extend_from_slice(b"CAMG"); + b.extend_from_slice(&4u32.to_be_bytes()); + b.extend_from_slice(&mode.to_be_bytes()); + let len = (b.len() - 8) as u32; + b[4..8].copy_from_slice(&len.to_be_bytes()); + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } + } + + #[test] + fn missing_palette_entries_are_black() { + let b = ilbm(1, 1, 2, &[[10, 20, 30]], 0, &[0x80, 0, 0x80, 0]); + assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[0, 0, 0]); + } + + fn plan(layout: ChannelLayout) -> Plan { + Plan { layout, sample: SampleType::U8 } + } + + #[test] + fn reads_a_stored_24_plane_bitmap() { + // 2×1: pixel 0 = (1, 0, 128), pixel 1 = (0, 255, 0). Each plane row is 2 bytes; red bit 0 + // has pixel 0 set, green bits 0–7 have pixel 1 set, blue bit 7 has pixel 0 set. + let mut body = Vec::new(); + for plane in 0..24 { + let byte = match plane { + 0 | 23 => 0x80, + 8..=15 => 0x40, + _ => 0, + }; + body.extend_from_slice(&[byte, 0]); + } + let b = ilbm(2, 1, 24, &[], 0, &body); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); + assert_eq!(img.data(), &[1, 0, 128, 0, 255, 0]); + } + + #[test] + fn writes_24_plane_byte_run1_that_reads_back_exactly() { + // 37 pixels wide: three plane-row words, the last one partly padding. + let (w, h) = (37u32, 3u32); + let data: Vec = (0..w * h * 3).map(|i| (i * 7 % 256) as u8).collect(); + let img = Image::from_u8(w, h, ChannelLayout::Rgb, data.clone()).unwrap(); + let bytes = encode(&img, plan(ChannelLayout::Rgb)).unwrap(); + assert!(looks_like_iff(&bytes)); + assert_eq!(be32(&bytes, 4).unwrap() as usize, bytes.len() - 8, "FORM length"); + let h = parse_header(&bytes, false).unwrap(); + assert_eq!((h.width, h.height, h.planes, h.masking, h.compression), (37, 3, 24, 0, 1)); + assert!(chunks(&bytes).all(|c| &c.id != b"CMAP"), "a deep ILBM has no palette"); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Rgb); + assert_eq!(back.data(), &data[..]); + } + + #[test] + fn alpha_is_written_as_a_mask_plane() { + let data = [10, 20, 30, 0, 40, 50, 60, 127, 70, 80, 90, 128, 1, 2, 3, 255]; + let img = Image::from_u8(4, 1, ChannelLayout::Rgba, data.to_vec()).unwrap(); + let bytes = encode(&img, plan(ChannelLayout::Rgba)).unwrap(); + assert_eq!(parse_header(&bytes, false).unwrap().masking, 1); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Rgba); + assert_eq!(back.data(), &[10, 20, 30, 0, 40, 50, 60, 0, 70, 80, 90, 255, 1, 2, 3, 255]); + } + + #[test] + fn byte_run1_round_trips_runs_literals_and_long_rows() { + let mut rows: Vec> = vec![vec![], vec![5], vec![5, 5], vec![5, 5, 5], vec![1, 2, 2, 2, 3], vec![9; 300]]; + rows.push((0..300).map(|i| (i % 251) as u8).collect()); + rows.push([vec![1, 2], vec![7; 129], vec![3]].concat()); + for row in rows { + let mut packed = Vec::new(); + pack_byte_run1(&row, &mut packed); + let mut back = vec![0; row.len()]; + assert_eq!(unpack_byte_run1(&packed, &mut back).unwrap(), packed.len(), "{row:?}"); + assert_eq!(back, row); + } + // A long repeat packs into two-byte runs. + let mut packed = Vec::new(); + pack_byte_run1(&[0; 256], &mut packed); + assert_eq!(packed, [0x81, 0, 0x81, 0]); + } + + #[test] + fn rejects_what_ilbm_cannot_store() { + let gray = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap(); + assert!(encode(&gray, plan(ChannelLayout::Gray)).is_err()); + let wide = Image::from_u8(65536, 1, ChannelLayout::Rgb, vec![0; 65536 * 3]).unwrap(); + assert!(encode(&wide, plan(ChannelLayout::Rgb)).is_err()); + // A transparent color index has no meaning without a palette. + let mut b = ilbm(1, 1, 24, &[], 0, &[0; 48]); + b[29] = 2; + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } + + #[test] + fn hostile_input_errors_not_panics() { + // Truncations of a valid file never panic. + let body = [0b0100_0000, 0, 0b0100_0000, 0]; + let full = ilbm(2, 1, 2, &[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], 0, &body); + for cut in [0, 4, 8, 12, 40, full.len() - 1] { + assert!(decode(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}"); + } + // Too many planes is unsupported. + let bad = ilbm(1, 1, 9, &[], 0, &[0, 0]); + assert!(matches!(decode(&bad, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs new file mode 100644 index 000000000..e2e854622 --- /dev/null +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -0,0 +1,931 @@ +//! JPEG: decode via `zune-jpeg` (gray, YCbCr, CMYK, YCCK; arithmetic-coded and 12-bit files +//! via [`craft_jpeg`]), encode via +//! `jpeg-encoder` (gray, RGB, CMYK). Metadata (APP0 JFIF density, APP1 +//! EXIF/XMP, APP2 multi-segment ICC, APP13 Photoshop resources, APP14 Adobe) is +//! parsed by our own marker scanner so behaviour does not depend on decoder internals. +//! +//! **Resolution** (#1691) can be recorded four times; like Photoshop the first usable one of +//! these wins: the APP13 Photoshop ResolutionInfo, the XMP `tiff:` resolution, the EXIF IFD0 +//! resolution, and last the JFIF density (a unit-0 JFIF density is only an aspect ratio and +//! never counts). Cameras record their resolution in EXIF only, and many tools write a default +//! 72 into JFIF, so JFIF can't outrank EXIF (see [`crate::resolution`]). On export RGB and gray +//! images carry density in JFIF; CMYK omits JFIF APP0, which is invalid for four components, and +//! carries an available density in EXIF instead. + +use zune_core::bytestream::ZCursor; +use zune_core::colorspace::ColorSpace; +use zune_core::options::DecoderOptions; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, JpegFormat, Limits}; +use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; + +const F: Format = Format::Jpeg; +const EXIF_HEADER: &[u8] = b"Exif\0\0"; +const XMP_HEADER: &[u8] = b"http://ns.adobe.com/xap/1.0/\0"; +const ICC_HEADER: &[u8] = b"ICC_PROFILE\0"; +const PHOTOSHOP_HEADER: &[u8] = b"Photoshop 3.0\0"; + +#[derive(Default, Debug)] +pub(crate) struct JpegMeta { + pub icc: Option>, + pub exif: Option>, + pub xmp: Option, + pub dpi: Option<(f32, f32)>, + /// The APP13 Photoshop image resource blocks (`8BIM` ...), segments joined, without the header. + pub photoshop: Option>, + pub adobe_transform: Option, + /// (width, height, components) from the first SOFn. + pub frame: Option<(u32, u32, u8)>, +} + +/// Walk the marker segments up to SOS and collect metadata. +pub(crate) fn scan_metadata(b: &[u8]) -> JpegMeta { + let mut m = JpegMeta::default(); + let mut icc_parts: Vec<(u8, u8, &[u8])> = Vec::new(); + // Photoshop splits a large resource block over consecutive APP13 segments. + let mut photoshop: Vec = Vec::new(); + let mut jfif_dpi = None; + let mut i = 2; + while i + 4 <= b.len() { + if b[i] != 0xFF { + break; + } + let marker = b[i + 1]; + if marker == 0xFF { + i += 1; + continue; + } + if marker == 0xD8 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 { + i += 2; + continue; + } + if marker == 0xDA || marker == 0xD9 { + break; + } + let len = u16::from_be_bytes([b[i + 2], b[i + 3]]) as usize; + if len < 2 || i + 2 + len > b.len() { + break; + } + let seg = &b[i + 4..i + 2 + len]; + match marker { + 0xE0 if seg.len() >= 12 && seg.starts_with(b"JFIF\0") => { + let unit = seg[7]; + let x = u16::from_be_bytes([seg[8], seg[9]]) as f32; + let y = u16::from_be_bytes([seg[10], seg[11]]) as f32; + if x > 0.0 && y > 0.0 { + jfif_dpi = match unit { + 1 => Some((x, y)), + 2 => Some((x * 2.54, y * 2.54)), + _ => None, + }; + } + } + 0xE1 if seg.starts_with(EXIF_HEADER) && m.exif.is_none() => { + m.exif = Some(seg[EXIF_HEADER.len()..].to_vec()); + } + 0xE1 if seg.starts_with(XMP_HEADER) && m.xmp.is_none() => { + m.xmp = std::str::from_utf8(&seg[XMP_HEADER.len()..]).ok().map(|s| s.trim_end_matches('\0').to_owned()); + } + 0xED if seg.starts_with(PHOTOSHOP_HEADER) && photoshop.len() < 1 << 24 => { + photoshop.extend_from_slice(&seg[PHOTOSHOP_HEADER.len()..]); + } + 0xE2 if seg.len() >= 14 && seg.starts_with(ICC_HEADER) => { + icc_parts.push((seg[12], seg[13], &seg[14..])); + } + 0xC0..=0xCF if !matches!(marker, 0xC4 | 0xC8 | 0xCC) && seg.len() >= 6 && m.frame.is_none() => { + let h = u16::from_be_bytes([seg[1], seg[2]]) as u32; + let w = u16::from_be_bytes([seg[3], seg[4]]) as u32; + m.frame = Some((w, h, seg[5])); + } + 0xEE if seg.len() >= 12 && seg.starts_with(b"Adobe") => { + m.adobe_transform = Some(seg[11]); + } + _ => {} + } + i += 2 + len; + } + m.dpi = photoshop_resolution(&photoshop) + .or_else(|| m.xmp.as_deref().and_then(xmp_resolution)) + .or_else(|| m.exif.as_deref().and_then(exif_resolution)) + .or(jfif_dpi); + m.photoshop = Some(photoshop).filter(|p| !p.is_empty()); + if !icc_parts.is_empty() { + icc_parts.sort_by_key(|p| p.0); + let total = icc_parts[0].1 as usize; + let seqs_ok = icc_parts.len() == total && icc_parts.iter().enumerate().all(|(k, p)| p.0 as usize == k + 1 && p.1 as usize == total); + if seqs_ok || icc_parts.len() == 1 { + m.icc = Some(icc_parts.iter().flat_map(|p| p.2.iter().copied()).collect()); + } + } + m +} + +/// How a JPEG's data ends, seen from its marker structure. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum DataEnd { + /// An end-of-image marker follows the scans (or the structure is too odd to tell). + Complete, + /// The file ends inside the image data, before the end-of-image marker. + Truncated, + /// The file ends before the first scan holds any data: nothing can be decoded. + Empty, +} + +/// Walks the marker segments (skipped by their length) and the entropy-coded scan data +/// (skipped up to the next marker that isn't a stuffed `FF 00`, a restart or a fill byte), +/// so an embedded thumbnail's end marker or data after the end of the image don't count. +/// Also returns how many scan-data bytes the walk crossed: the truncated-input budget is +/// measured on these, not on the file length, which APP padding can inflate without +/// adding image data. +fn data_end(b: &[u8]) -> (DataEnd, u64) { + // Where the bytes run out: inside the image data once a scan has held some. + let cut = |scan_data: bool, scanned: u64| (if scan_data { DataEnd::Truncated } else { DataEnd::Empty }, scanned); + let mut i = 2; // past SOI + let mut scan_data = false; + let mut scanned = 0u64; + loop { + let Some(&byte) = b.get(i) else { return cut(scan_data, scanned) }; + if byte != 0xFF { + // Not where a marker should be: skip to the next one, as a lenient decoder does, so + // segments after the stray bytes (padding among them) are still recognised. Stray + // bytes are not scan data, so they never count (junk would buy budget as padding + // did); with no marker left, the image data ends here. + let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { return cut(scan_data, scanned) }; + i += p; + continue; + } + let Some(&code) = b.get(i + 1) else { return cut(scan_data, scanned) }; + match code { + 0xD9 => return (DataEnd::Complete, scanned), + 0xFF => i += 1, + 0x01 | 0xD0..=0xD8 => i += 2, + _ => { + let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else { + return cut(scan_data, scanned); + }; + i = i.saturating_add(2 + usize::from(len)); + if code == 0xDA { + let start = i; + loop { + let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { + // EOF inside this scan: everything from the scan start to the end + // of the file is scan data (inside a declared segment it may not + // be, so only count what remains). + return cut(scan_data || b.len() > start, scanned + u64::try_from(b.len().saturating_sub(start)).unwrap_or(0)); + }; + i += p; + match b.get(i + 1) { + Some(0x00 | 0xD0..=0xD7) => i += 2, + Some(0xFF) => i += 1, + _ => break, + } + } + scan_data |= i > start; + scanned += u64::try_from(i - start).unwrap_or(0); + } + } + } + } +} + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +const EXTENDED_XMP_HEADER: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; + +/// The file without its Extended XMP APP1 segments (`None` when it has none), for `zune-jpeg`. +/// +/// zune-jpeg keeps every Extended XMP fragment until a GUID's set is complete and re-sorts and +/// re-checks all pending fragments after every marker it parses, so many small, consistent but +/// incomplete fragments (or a few followed by many other markers) cost quadratic time before a +/// pixel is decoded. Nothing here reads its reassembled Extended XMP ([`scan_metadata`] works on +/// the original bytes), so the fragments are left out of what it parses. The walk stops at the +/// first scan, as the header parse does, and skips stray bytes to the next `FF` as it does. +fn without_extended_xmp(b: &[u8]) -> Option> { + let mut drop: Vec> = Vec::new(); + let mut i = 2; // past SOI + while let Some(&byte) = b.get(i) { + if byte != 0xFF { + i += b.get(i..)?.iter().position(|&v| v == 0xFF)?; + continue; + } + let Some(&code) = b.get(i + 1) else { break }; + match code { + 0xFF => i += 1, + 0x01 | 0xD0..=0xD8 => i += 2, + 0xD9 | 0xDA => break, + _ => { + let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else { break }; + let end = i.saturating_add(2 + usize::from(len)); + if code == 0xE1 && b.get(i + 4..).is_some_and(|p| p.starts_with(EXTENDED_XMP_HEADER)) { + drop.push(i..end.min(b.len())); + } + i = end; + } + } + } + if drop.is_empty() { + return None; + } + // Reserve what is kept, not the input: the dropped fragments may be most of it. + let kept = b.len().saturating_sub(drop.iter().map(|r| r.end - r.start).sum::()); + let mut out = Vec::with_capacity(kept); + let mut at = 0; + for r in drop { + out.extend_from_slice(b.get(at..r.start)?); + at = r.end; + } + out.extend_from_slice(b.get(at..)?); + Some(out) +} + +/// Whether `craft-jpeg` (rather than `zune-jpeg`) decodes the file: arithmetic coding +/// (SOF9–SOF11) or a DCT frame whose precision isn't 8 bits (#4934, #5003). Lossless (SOF3) and +/// 12-bit progressive Huffman (SOF2) go there too: craft-jpeg reports them unsupported, where +/// zune-jpeg's failure would read as a damaged file. +fn uses_craft_jpeg(bytes: &[u8]) -> bool { + match craft_jpeg::frame(bytes) { + Some(f) if (0xC9..=0xCB).contains(&f.marker) || f.marker == 0xC3 => true, + Some(f) if matches!(f.marker, 0xC0..=0xC2) => f.precision != 8, + _ => false, + } +} + +fn craft_jpeg_error(e: craft_jpeg::Error) -> CodecError { + match e { + craft_jpeg::Error::Malformed(m) => err(m), + craft_jpeg::Error::Unsupported(m) => CodecError::unsupported(F, m), + craft_jpeg::Error::LimitExceeded(m) => CodecError::LimitExceeded(m), + e => err(e), + } +} + +/// Decode a file [`uses_craft_jpeg`] routes away from `zune-jpeg`; `damaged` says entropy-coded +/// data was cut short or corrupt. +fn decode_craft_jpeg(bytes: &[u8], limits: &Limits, adobe_transform: Option) -> Result<(Image, bool), CodecError> { + let mut lim = craft_jpeg::Limits::default(); + lim.max_pixels = limits.max_pixels; + lim.max_alloc = limits.max_alloc; + let d = craft_jpeg::decode(bytes, &lim, adobe_transform).map_err(craft_jpeg_error)?; + let layout = match d.components { + 1 => ChannelLayout::Gray, + 3 => ChannelLayout::Rgb, + 4 => ChannelLayout::Cmyk, + n => return Err(err(format!("{n} components"))), + }; + let img = match d.samples { + craft_jpeg::Samples::U8(px) => Image::from_raw(d.width, d.height, layout, SampleType::U8, px)?, + craft_jpeg::Samples::U16(px) => Image::from_u16(d.width, d.height, layout, &px)?, + }; + Ok((img, d.damaged)) +} + +/// Most pixels one byte of a JPEG may account for. Every 8×8 block of a frame needs at least a +/// one-bit DC code (512 pixels a byte for a flat image under ideal Huffman tables), and real files, +/// truncated or not, are far above this. +const MAX_PIXELS_PER_BYTE: u64 = 2048; + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let (end, scan_bytes) = data_end(bytes); + if end == DataEnd::Empty { + return Err(err("the file ends before any image data")); + } + let meta = scan_metadata(bytes); + if let Some((w, h, nc)) = meta.frame { + limits.check_bytes(w, h, u64::from(nc.max(1)))?; + } + if uses_craft_jpeg(bytes) { + // craft-jpeg sizes its buffers from the frame header before it reads a scan. + if scan_bytes == 0 { + return Err(err("the file has no scan data")); + } + if let Some((w, h, _)) = meta.frame + && end == DataEnd::Truncated + // Only truncated scans: arithmetic coding (SOF9–SOF11) can spend well under a bit on + // a flat block, so a complete scan may be shorter than this bound. + && u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes + { + return Err(err("the truncated scan is too short for the frame it declares")); + } + let (mut img, damaged) = decode_craft_jpeg(bytes, limits, meta.adobe_transform)?; + img.icc = meta.icc; + img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; + if end == DataEnd::Truncated || damaged { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + return Ok(img); + } + let options = DecoderOptions::default().set_strict_mode(false).set_max_width(65535).set_max_height(65535); + let stripped = without_extended_xmp(bytes); + let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(stripped.as_deref().unwrap_or(bytes)), options); + dec.decode_headers().map_err(err)?; + let (w, h) = dec.dimensions().ok_or_else(|| err("no dimensions"))?; + let (w, h) = (w as u32, h as u32); + let input = dec.input_colorspace().ok_or_else(|| err("no colorspace"))?; + let (out_cs, layout) = match input { + ColorSpace::Luma | ColorSpace::LumaA => (ColorSpace::Luma, ChannelLayout::Gray), + ColorSpace::CMYK => (ColorSpace::CMYK, ChannelLayout::Cmyk), + ColorSpace::YCCK => (ColorSpace::YCCK, ChannelLayout::Cmyk), + _ => (ColorSpace::RGB, ChannelLayout::Rgb), + }; + limits.check(w, h, layout, SampleType::U8)?; + // The backend fills whatever the scan no longer describes, and allocates the whole output + // once a scan holds a byte, so a short scan under a large frame is a gigabyte of synthesized + // output from a few kilobytes of file (#8597), truncated or not. The budget is the scan data + // itself, not `bytes.len()`, which APP padding can inflate without describing one more pixel; + // where the walk stops early on an odd layout, what it couldn't read counts as scan data. + if u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes { + let what = if end == DataEnd::Truncated { "the truncated scan" } else { "the scan data" }; + return Err(err(format!("{what} is too short for the frame it declares"))); + } + dec.set_options(dec.options().jpeg_set_out_colorspace(out_cs)); + let mut px = dec.decode().map_err(err)?; + let expected = w as usize * h as usize * layout.channels(); + if px.len() < expected { + return Err(err("short pixel buffer")); + } + px.truncate(expected); + match input { + // Adobe-style CMYK is stored inverted (255 = no ink). + ColorSpace::CMYK => px.iter_mut().for_each(|v| *v = 255 - *v), + ColorSpace::YCCK => { + for p in px.as_chunks_mut::<4>().0 { + let (y, cb, cr) = (p[0] as f32, p[1] as f32 - 128.0, p[2] as f32 - 128.0); + let c = y + 1.402 * cr; + let m = y - 0.344_136 * cb - 0.714_136 * cr; + let yy = y + 1.772 * cb; + p[0] = c.round().clamp(0.0, 255.0) as u8; + p[1] = m.round().clamp(0.0, 255.0) as u8; + p[2] = yy.round().clamp(0.0, 255.0) as u8; + p[3] = 255 - p[3]; + } + } + _ => {} + } + let mut img = Image::from_raw(w, h, layout, SampleType::U8, px)?; + img.icc = meta.icc; + img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; + if end == DataEnd::Truncated { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(img) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let (w16, h16) = match (u16::try_from(w), u16::try_from(h)) { + (Ok(a), Ok(b)) => (a, b), + _ => { + return Err(CodecError::encode(F, "JPEG dimensions are limited to 65535")); + } + }; + let ct = match img.layout() { + ChannelLayout::Gray => jpeg_encoder::ColorType::Luma, + ChannelLayout::Rgb => jpeg_encoder::ColorType::Rgb, + ChannelLayout::Cmyk => jpeg_encoder::ColorType::Cmyk, + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, opts.jpeg_quality.clamp(1, 100)); + // CMYK is always written 4:4:4: subsampled 4-component JPEGs are not + // decoded consistently across implementations. + let subsampled = opts.jpeg_chroma_subsampling && img.layout() == ChannelLayout::Rgb; + let sampling = if subsampled { jpeg_encoder::SamplingFactor::R_4_2_0 } else { jpeg_encoder::SamplingFactor::R_4_4_4 }; + enc.set_sampling_factor(sampling); + // JPEG Options › Format Options. Fitted Huffman tables make a baseline scan non-interleaved + // (one scan per component), which zune-jpeg 0.5.15 decodes wrongly with 4:2:0 chroma + // (etemesi254/zune-image#419; see `web::encode_jpeg_rgb8`), so a subsampled Baseline + // Optimized keeps the standard tables until that decoder is updated. Progressive is fine. + let progressive = opts.jpeg_format == JpegFormat::Progressive; + enc.set_progressive(progressive); + let optimized = opts.jpeg_format == JpegFormat::Optimized && !subsampled; + enc.set_optimized_huffman_tables(progressive || optimized); + let e = |e: jpeg_encoder::EncodingError| CodecError::encode(F, e); + if opts.embed_metadata { + if img.layout() != ChannelLayout::Cmyk + && let Some((x, y)) = img.meta.dpi + && x >= 1.0 + && y >= 1.0 + { + enc.set_density(jpeg_encoder::Density::Inch { x: x.round().min(65535.0) as u16, y: y.round().min(65535.0) as u16 }); + } + // Metadata too large for its one segment is left out (and reported by the fidelity + // warnings) rather than failing the whole export. + let exif = if img.layout() == ChannelLayout::Cmyk { + match img.meta.exif.as_deref() { + Some(exif) => exif_segment_with_resolution(exif, img.meta.dpi), + None if usable_dpi(img.meta.dpi) => exif_segment_with_resolution(&[], img.meta.dpi), + None => None, + } + } else { + img.meta.exif.as_deref().and_then(|x| exif_segment(x, img.meta.dpi)) + }; + if let Some(seg) = exif { + enc.add_app_segment(1, &seg).map_err(e)?; + } + if let Some(seg) = img.meta.xmp.as_deref().and_then(|x| xmp_segment(x, img.meta.dpi)) { + enc.add_app_segment(1, &seg).map_err(e)?; + } + } + if opts.embed_icc + && let Some(icc) = &img.icc + { + enc.add_icc_profile(icc).map_err(e)?; + } + enc.encode(img.data(), w16, h16, ct).map_err(e)?; + if img.layout() == ChannelLayout::Cmyk { + strip_jfif_app0(&mut out); + } + Ok(out) +} + +/// JFIF APP0 requires one or three image components. A four-component CMYK JPEG must not carry +/// that marker; keep Adobe APP14, which identifies the CMYK encoding instead. +fn strip_jfif_app0(bytes: &mut Vec) { + if !bytes.starts_with(&[0xff, 0xd8]) { + return; + } + let mut i = 2usize; + loop { + if bytes.get(i) != Some(&0xff) { + return; + } + let Some(&marker) = bytes.get(i.saturating_add(1)) else { return }; + if matches!(marker, 0xda | 0xd9) { + return; + } + if marker == 0xff { + i = i.saturating_add(1); + continue; + } + if marker == 0x01 || (0xd0..=0xd8).contains(&marker) { + i = i.saturating_add(2); + continue; + } + let Some(length_bytes) = i.checked_add(2).and_then(|at| bytes.get(at..at.checked_add(2)?)) else { return }; + let Ok(length_bytes) = <[u8; 2]>::try_from(length_bytes) else { return }; + let length = usize::from(u16::from_be_bytes(length_bytes)); + if length < 2 { + return; + } + let Some(end) = i.checked_add(2).and_then(|at| at.checked_add(length)) else { return }; + if end > bytes.len() { + return; + } + if marker == 0xe0 && bytes.get(i.saturating_add(4)..i.saturating_add(9)) == Some(b"JFIF\0") { + bytes.drain(i..end); + } else { + i = end; + } + } +} + +/// Return whether both resolution values are finite and within the supported metadata range. +fn usable_dpi(dpi: Option<(f32, f32)>) -> bool { + dpi.is_some_and(|(x, y)| x.is_finite() && y.is_finite() && (1.0..=1_000_000.0).contains(&x) && (1.0..=1_000_000.0).contains(&y)) +} + +/// The most data one APP segment holds (its 16-bit length counts its own two bytes). +const MAX_SEGMENT: usize = 65533; + +/// XMP properties (local names, any namespace prefix) that only describe the layered document: +/// every document ever placed in it, the text of every type layer. A flat image doesn't need them, +/// and they alone can outgrow a segment. +const LAYERED_ONLY_XMP: [&str; 2] = ["DocumentAncestors", "TextLayers"]; + +/// The EXIF APP1 segment, or `None` when it doesn't fit in one segment. +pub(crate) fn exif_segment(exif: &[u8], dpi: Option<(f32, f32)>) -> Option> { + // The pixels are written as they are shown: never let a viewer rotate them again; and store + // the output resolution, not the source's (#1691). + let seg = [EXIF_HEADER, export_exif(exif, dpi).as_ref()].concat(); + (seg.len() <= MAX_SEGMENT).then_some(seg) +} + +/// Build an EXIF APP1 segment, adding missing resolution tags when the source metadata is valid. +fn exif_segment_with_resolution(exif: &[u8], dpi: Option<(f32, f32)>) -> Option> { + let seg = [EXIF_HEADER, crate::resolution::export_exif_adding_missing_resolution(exif, dpi).as_ref()].concat(); + (seg.len() <= MAX_SEGMENT).then_some(seg) +} + +/// The XMP APP1 segment. When the packet doesn't fit in one segment, the layered-document +/// properties are left out; `None` when it still doesn't fit. +pub(crate) fn xmp_segment(xmp: &str, dpi: Option<(f32, f32)>) -> Option> { + let xmp = export_xmp(xmp, dpi); + let seg = |x: &str| [XMP_HEADER, x.as_bytes()].concat(); + if XMP_HEADER.len() + xmp.len() <= MAX_SEGMENT { + return Some(seg(&xmp)); + } + let trimmed = LAYERED_ONLY_XMP.iter().fold(xmp.into_owned(), |x, name| remove_element(&x, name)); + (XMP_HEADER.len() + trimmed.len() <= MAX_SEGMENT).then(|| seg(&trimmed)) +} + +/// `xmp` without its `…` and `` elements. Unbalanced +/// markup is kept as is. +fn remove_element(xmp: &str, name: &str) -> String { + let local = format!(":{name}"); + let mut out = String::with_capacity(xmp.len()); + let mut rest = xmp; + while let Some(j) = rest.find(&local) { + let head = rest.get(..j).unwrap_or_default(); + let prefix_len = head.bytes().rev().take_while(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.')).count(); + let lt = j - prefix_len; + let after = rest.get(j + local.len()..).unwrap_or_default(); + // An opening tag of exactly this name (not a closing tag, an attribute or a longer name). + let opening = prefix_len > 0 && lt > 0 && rest.get(lt - 1..lt) == Some("<"); + if !opening || !after.starts_with(['>', '/', ' ', '\t', '\r', '\n']) { + out.push_str(rest.get(..j + local.len()).unwrap_or_default()); + rest = after; + continue; + } + let Some(gt) = after.find('>') else { break }; + let end = if after.get(..gt).is_some_and(|s| s.ends_with('/')) { + gt + 1 + } else { + let close = format!("", rest.get(lt..j).unwrap_or_default()); + let Some(c) = after.find(&close) else { break }; + c + close.len() + }; + out.push_str(rest.get(..lt - 1).unwrap_or_default()); + rest = after.get(end..).unwrap_or_default(); + } + out.push_str(rest); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{Format, Metadata}; + + /// Build a little-endian TIFF with an empty IFD0 for metadata tests. + fn empty_tiff() -> Vec { + // Little-endian TIFF with an empty IFD0 and no next IFD. + b"II*\0\x08\0\0\0\0\0\0\0\0\0\0\0".to_vec() + } + + /// Return whether the JPEG begins with a JFIF APP0 segment. + fn has_jfif_app0(bytes: &[u8]) -> bool { + bytes.get(2..11) == Some(b"\xff\xe0\0\x10JFIF\0") + } + + /// An APP1 Extended XMP fragment (GUID, total size, offset, data). + fn extended_xmp(offset: u32) -> Vec { + let mut seg = EXTENDED_XMP_HEADER.to_vec(); + seg.extend_from_slice(&[b'A'; 32]); + seg.extend_from_slice(&1_000_000u32.to_be_bytes()); + seg.extend_from_slice(&offset.to_be_bytes()); + seg.push(b'x'); + let mut out = vec![0xFF, 0xE1]; + out.extend_from_slice(&u16::try_from(seg.len() + 2).unwrap().to_be_bytes()); + out.extend_from_slice(&seg); + out + } + + #[test] + fn extended_xmp_fragments_are_left_out_of_the_backend_parse() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![120; 16 * 16 * 3]).unwrap(); + let plain = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert!(without_extended_xmp(&plain).is_none()); + // 20000 consistent but incomplete fragments right after SOI: zune-jpeg would re-sort and + // re-check the growing pending list after each one. + let mut hostile = plain[..2].to_vec(); + for k in 0..20_000u32 { + hostile.extend_from_slice(&extended_xmp(k * 2)); + } + hostile.extend_from_slice(&plain[2..]); + assert_eq!(without_extended_xmp(&hostile).as_deref(), Some(&plain[..])); + let back = decode(&hostile, &Limits::default()).unwrap(); + assert_eq!(back.dimensions(), (16, 16)); + } + + /// A tiny truncated scan under a frame patched to 20000×20000 is refused before the backend + /// fills 400 million pixels from it; the same file with its real size still recovers. + #[test] + fn a_short_truncated_scan_cannot_declare_a_huge_frame() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![128; 16 * 16 * 3]).unwrap(); + let full = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let cut = &full[..full.len() - 4]; + assert_eq!(data_end(cut).0, DataEnd::Truncated); + assert!(crate::decode_as(Format::Jpeg, cut).is_ok()); + let sof = cut.windows(2).position(|w| w == [0xFF, 0xC0] || w == [0xFF, 0xC2]).unwrap(); + let mut huge = cut.to_vec(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode_as(Format::Jpeg, &huge).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + } + + /// The truncated-scan budget counts scan data, not the file length: APP padding before the + /// window cannot admit an oversized output. The same padded file with its real size still + /// decodes (with the truncation warning). + #[test] + fn app_padding_cannot_admit_an_oversized_truncated_frame() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![128; 16 * 16 * 3]).unwrap(); + let full = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let cut = &full[..full.len() - 4]; + // Four full-length COMMENT segments (~256 KiB) stand where APP padding would; they are + // complete, so the file otherwise parses normally. + let fill = [b'p'; 65533]; + let mut padded = cut[..2].to_vec(); + for _ in 0..4 { + padded.extend_from_slice(&[0xFF, 0xFE]); + padded.extend_from_slice(&0xFFFFu16.to_be_bytes()); + padded.extend_from_slice(&fill); + } + padded.extend_from_slice(&cut[2..]); + assert_eq!(data_end(&padded).0, DataEnd::Truncated); + // Well above the frame's pixels / budget, yet far above the scan data: the old + // file-length bound admitted this frame. + assert!(padded.len() > (u64::from(20_000u32) * u64::from(20_000u32) / MAX_PIXELS_PER_BYTE) as usize, "padding too small"); + assert!(crate::decode_as(Format::Jpeg, &padded).is_ok()); + let sof = padded.windows(2).position(|w| w == [0xFF, 0xC0] || w == [0xFF, 0xC2]).unwrap(); + let mut huge = padded.clone(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode_as(Format::Jpeg, &huge).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + } + + /// A complete file is measured on its scan data too: padding can't admit an oversized frame. + /// The same padded file at its real size decodes, and so does an odd layout whose unread tail + /// counts as scan data. + #[test] + fn app_padding_cannot_admit_an_oversized_complete_frame() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![128; 16 * 16 * 3]).unwrap(); + let full = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let fill = [b'p'; 65533]; + let mut padded = full[..2].to_vec(); + for _ in 0..4 { + padded.extend_from_slice(&[0xFF, 0xFE]); + padded.extend_from_slice(&0xFFFFu16.to_be_bytes()); + padded.extend_from_slice(&fill); + } + padded.extend_from_slice(&full[2..]); + let (end, scanned) = data_end(&padded); + assert_eq!(end, DataEnd::Complete); + assert!(scanned < 1024, "the padding isn't scan data: {scanned}"); + assert!(padded.len() as u64 > u64::from(20_000u32) * 20_000 / MAX_PIXELS_PER_BYTE, "padding too small"); + assert!(crate::decode_as(Format::Jpeg, &padded).is_ok()); + let sof = padded.windows(2).position(|w| w == [0xFF, 0xC0] || w == [0xFF, 0xC2]).unwrap(); + let mut huge = padded.clone(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode_as(Format::Jpeg, &huge).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + // A stray byte where a marker should be, before the padding: the walk skips it and still + // recognises the padding, so an oversized frame is refused there too. + let mut odd = huge.clone(); + odd.insert(2, 0x00); + let (end, scanned) = data_end(&odd); + assert_eq!(end, DataEnd::Complete); + assert!(scanned < 1024, "the padding after a stray byte isn't scan data: {scanned}"); + let e = crate::decode_as(Format::Jpeg, &odd).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + // Stray bytes never count: 256 KiB of junk where a marker should be (the decoder skips + // it), then the oversized frame's segments, is refused. (Bytes after a scan starts are + // entropy-coded data, any value, and count as such.) + let mut junk = huge[..2].to_vec(); + junk.extend(std::iter::repeat_n(0x00u8, 4 * 65536)); + junk.extend_from_slice(&huge[2..]); + let (end, scanned) = data_end(&junk); + assert_eq!(end, DataEnd::Complete); + assert!(scanned < 1024, "junk isn't scan data: {scanned}"); + let e = crate::decode_as(Format::Jpeg, &junk).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + // Junk with no marker after it: no image data at all. + let mut tail = vec![0xFF, 0xD8]; + tail.extend(std::iter::repeat_n(0x00u8, 4096)); + assert_eq!(data_end(&tail), (DataEnd::Empty, 0)); + } + + /// CMYK JPEG output must omit JFIF while retaining Adobe's CMYK marker. + #[test] + fn cmyk_jpeg_omits_jfif_but_keeps_adobe_cmyk_marker() { + let img = Image::from_raw(2, 1, ChannelLayout::Cmyk, SampleType::U8, vec![0, 32, 64, 96, 255, 224, 192, 160]).unwrap(); + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + let meta = scan_metadata(&bytes); + assert!(!has_jfif_app0(&bytes)); + assert_eq!(meta.frame, Some((2, 1, 4))); + assert_eq!(meta.adobe_transform, Some(0)); + assert_eq!(crate::decode_as(Format::Jpeg, &bytes).unwrap().layout(), ChannelLayout::Cmyk); + } + + /// CMYK JPEG output stores image resolution in EXIF when the source has no EXIF. + #[test] + fn cmyk_jpeg_preserves_dpi_without_source_exif() { + let mut img = Image::from_raw(1, 1, ChannelLayout::Cmyk, SampleType::U8, vec![0, 0, 0, 0]).unwrap(); + img.meta.dpi = Some((300.0, 150.0)); + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + assert!(!has_jfif_app0(&bytes)); + assert_eq!(crate::decode_as(Format::Jpeg, &bytes).unwrap().meta.dpi, Some((300.0, 150.0))); + } + + /// CMYK JPEG output adds missing EXIF resolution tags to existing metadata. + #[test] + fn cmyk_jpeg_preserves_dpi_with_source_exif_without_resolution_tags() { + let mut img = Image::from_raw(1, 1, ChannelLayout::Cmyk, SampleType::U8, vec![0, 0, 0, 0]).unwrap(); + img.meta = Metadata { exif: Some(empty_tiff()), dpi: Some((240.0, 120.0)), ..Default::default() }; + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + assert_eq!(crate::decode_as(Format::Jpeg, &bytes).unwrap().meta.dpi, Some((240.0, 120.0))); + } + + /// Adding missing resolution tags makes them readable from the rewritten IFD0. + #[test] + fn exif_with_resolution_adds_missing_ifd0_entries() { + let exif = empty_tiff(); + let rewritten = crate::resolution::export_exif_adding_missing_resolution(&exif, Some((240.0, 120.0))); + + assert_eq!(crate::resolution::exif_resolution(&rewritten), Some((240.0, 120.0))); + } + + /// RGB JPEG output continues to store image resolution in JFIF density fields. + #[test] + fn rgb_jpeg_keeps_jfif_density() { + let mut img = Image::from_raw(1, 1, ChannelLayout::Rgb, SampleType::U8, vec![12, 34, 56]).unwrap(); + img.meta.dpi = Some((96.0, 96.0)); + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + assert!(has_jfif_app0(&bytes)); + assert_eq!(scan_metadata(&bytes).dpi, Some((96.0, 96.0))); + } + + // --- Arithmetic-coded and 12-bit files, decoded by craft-jpeg --- + + fn segment(out: &mut Vec, marker: u8, body: &[u8]) { + out.extend_from_slice(&[0xFF, marker]); + out.extend_from_slice(&((body.len() + 2) as u16).to_be_bytes()); + out.extend_from_slice(body); + } + + /// One 8×8 gray block, quantizer 1, with entropy-coded scan data `data`. + fn gray_block(sof: u8, precision: u8, dht: Option<&[u8]>, data: &[u8]) -> Vec { + let mut s = vec![0xFF, 0xD8]; + let mut dqt = vec![0u8]; + dqt.extend([1u8; 64]); + segment(&mut s, 0xDB, &dqt); + segment(&mut s, sof, &[precision, 0, 8, 0, 8, 1, 1, 0x11, 0]); + if let Some(dht) = dht { + segment(&mut s, 0xC4, dht); + } + segment(&mut s, 0xDA, &[1, 1, 0x00, 0, 63, 0]); + s.extend_from_slice(data); + s.extend([0xFF, 0xD9]); + s + } + + /// SOF9 with only a DC coefficient (from a T.81 arithmetic encoder): 8-bit DC 200 → sample + /// 200 / 8 + 128 = 153; 12-bit DC 1000 → 1000 / 8 + 2048 = 2173. + fn arithmetic_block(precision: u8) -> Vec { + let data: &[u8] = if precision == 8 { &[210, 217, 68, 0] } else { &[210, 234, 134, 0] }; + gray_block(0xC9, precision, None, data) + } + + /// A 12-bit SOF1 stream: one DC coefficient. The DC table has one code (`0`, category 13), + /// the AC table one (`0`, end of block). DC = 7616 → sample 7616 / 8 + 2048 = 3000. + fn twelve_bit_huffman_block() -> Vec { + let mut dht = vec![0x00, 1]; + dht.extend([0u8; 15]); + dht.push(13); + dht.extend([0x10, 1]); + dht.extend([0u8; 15]); + dht.push(0x00); + // Bits: `0`, 7616 in 13 bits, `0`, one pad `1`. + let word: u16 = (7616 << 2) | 1; + gray_block(0xC1, 12, Some(&dht), &word.to_be_bytes()) + } + + fn twelve_bit(v: f32) -> u16 { + (v * 65535.0 / 4095.0).round() as u16 + } + + #[test] + fn routing() { + assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC1, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC9, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC1, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(!uses_craft_jpeg(&[0xFF, 0xD8])); + assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC2, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC2, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC3, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + } + + #[test] + fn lossless_and_twelve_bit_progressive_are_unsupported_not_damaged() { + for marker in [0xC3, 0xC2] { + let mut bytes = vec![0xFF, 0xD8, 0xFF, marker, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0]; + bytes.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, 1, 0, 0, 0x3F, 0, 0x12, 0x34, 0xFF, 0xD9]); + let e = crate::decode(&bytes).unwrap_err(); + assert!(matches!(e, CodecError::Unsupported { .. }), "{marker:#x}: {e:?}"); + } + } + + #[test] + fn a_twelve_bit_frame_without_scan_data_is_refused_before_decoding() { + let mut bytes = vec![0xFF, 0xD8, 0xFF, 0xC1, 0, 11, 12, 0x40, 0, 0x40, 0, 1, 1, 0x11, 0]; + bytes.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, 1, 0, 0, 0x3F, 0, 0xFF, 0xD9]); + let e = crate::decode(&bytes).unwrap_err(); + assert!(e.to_string().contains("no scan data"), "{e}"); + } + + #[test] + fn arithmetic_eight_bit_block_is_eight_bit() { + let img = crate::decode(&arithmetic_block(8)).unwrap(); + assert_eq!((img.dimensions(), img.sample_type(), img.layout()), ((8, 8), SampleType::U8, ChannelLayout::Gray)); + assert!(img.data().iter().all(|&v| v.abs_diff(153) <= 1), "{:?}", &img.data()[..4]); + assert!(img.warnings.is_empty()); + } + + #[test] + fn arithmetic_twelve_bit_block_is_sixteen_bit() { + let img = crate::decode(&arithmetic_block(12)).unwrap(); + assert_eq!((img.dimensions(), img.sample_type(), img.layout()), ((8, 8), SampleType::U16, ChannelLayout::Gray)); + let want = twelve_bit(2173.0); + assert!(img.to_u16_samples().unwrap().iter().all(|&v| v.abs_diff(want) <= 1)); + } + + #[test] + fn twelve_bit_huffman_block_decodes_to_sixteen_bit() { + let img = crate::decode(&twelve_bit_huffman_block()).unwrap(); + assert_eq!((img.dimensions(), img.sample_type()), ((8, 8), SampleType::U16)); + let want = twelve_bit(3000.0); + let px = img.to_u16_samples().unwrap(); + assert!(px.iter().all(|&v| v.abs_diff(want) <= 1), "{:?} vs {want}", &px[..4]); + assert!(img.warnings.is_empty()); + } + + #[test] + fn twelve_bit_jpeg_keeps_its_photoshop_resources() { + // An APP13 `8BIM` block (paths, Photoshop settings) right after SOI, as on the zune-jpeg path. + let resources = b"8BIM\x04\x04\0\0\0\0\0\0".to_vec(); + let mut app13 = PHOTOSHOP_HEADER.to_vec(); + app13.extend_from_slice(&resources); + let stream = twelve_bit_huffman_block(); + let mut with = stream[..2].to_vec(); + segment(&mut with, 0xED, &app13); + with.extend_from_slice(&stream[2..]); + assert!(uses_craft_jpeg(&with)); + let img = crate::decode(&with).unwrap(); + assert_eq!(img.meta.photoshop_resources.as_deref(), Some(resources.as_slice())); + } + + /// Where the entropy-coded data of the last scan starts. + fn last_scan_data(s: &[u8]) -> usize { + let sos = s.windows(2).rposition(|p| p == [0xFF, 0xDA]).unwrap(); + sos + 2 + usize::from(u16::from_be_bytes([s[sos + 2], s[sos + 3]])) + } + + /// A 12-bit scan whose entropy data runs out, EOI kept, is never a clean image. + #[test] + fn twelve_bit_scan_out_of_data_is_not_clean() { + let full = twelve_bit_huffman_block(); + let data = last_scan_data(&full); + for keep in 0..2 { + let mut s = full[..data + keep].to_vec(); + s.extend([0xFF, 0xD9]); + if let Ok(img) = crate::decode(&s) { + assert!(!img.warnings.is_empty(), "keep {keep}: clean decode of missing data"); + } + } + } + + /// A multi-MCU SOF1 12-bit scan cut in half, EOI kept, decodes with the Truncated warning; a + /// truncated scan too short for its frame is refused, as on the zune-jpeg path. + #[test] + fn damaged_or_truncated_twelve_bit_scan() { + let (w, h) = (64usize, 64usize); + let rgb: Vec = (0..w * h).flat_map(|i| [(i % w * 4) as u8, (i / w * 4) as u8, (i * 7) as u8]).collect(); + let mut out = Vec::new(); + jpeg_encoder::Encoder::new(&mut out, 90).encode(&rgb, w as u16, h as u16, jpeg_encoder::ColorType::Rgb).unwrap(); + // Relabel the baseline frame as 12-bit SOF1: the Huffman data still parses, as different values. + let sof = out.windows(2).position(|p| p == [0xFF, 0xC0]).unwrap(); + out[sof + 1] = 0xC1; + out[sof + 4] = 12; + let data = last_scan_data(&out); + let end = out.len() - 2; + let mut cut = out[..data + (end - data) / 2].to_vec(); + if cut.last() == Some(&0xFF) { + cut.pop(); + } + cut.extend([0xFF, 0xD9]); + let img = crate::decode(&out).unwrap(); + assert_eq!(img.sample_type(), SampleType::U16); + assert!(img.warnings.is_empty(), "{:?}", img.warnings); + let img = crate::decode(&cut).unwrap(); + assert_eq!(img.warnings, vec![DecodeWarning::Truncated { format: Format::Jpeg }]); + // Without EOI the same cut is a truncated file; under a 20000×20000 frame it is refused. + cut.truncate(cut.len() - 2); + assert_eq!(data_end(&cut).0, DataEnd::Truncated); + assert!(crate::decode(&cut).is_ok()); + cut[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode(&cut).unwrap_err(); + assert!(e.to_string().contains("too short for the frame it declares"), "{e}"); + } +} diff --git a/crates/image-codecs/src/codecs/jxl.rs b/crates/image-codecs/src/codecs/jxl.rs new file mode 100644 index 000000000..11591c930 --- /dev/null +++ b/crates/image-codecs/src/codecs/jxl.rs @@ -0,0 +1,159 @@ +//! Pure-Rust lossless JPEG XL (Modular) writer. +//! +//! Currently writes a bare JPEG XL codestream. The encoder accepts 8- or 16-bit +//! gray, RGB and RGBA samples, but does not write ICC/EXIF/XMP boxes. +//! Gray+alpha sources are expanded losslessly to RGBA by the export plan. +//! The flat export pipeline converts tagged RGB documents to sRGB first. + +use std::panic::{AssertUnwindSafe, catch_unwind}; + +use zune_core::{bit_depth::BitDepth, colorspace::ColorSpace, options::EncoderOptions as ZuneOptions}; +use zune_jpegxl::JxlSimpleEncoder; + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; + +const F: Format = Format::Jxl; + +// zune-jpegxl 0.5.2 emits ExtraChannelInfo(all_default=true) for every alpha +// channel. In JPEG XL that flag means *8-bit* alpha, even when the modular +// payload was encoded from 16-bit samples. Replace only that metadata bundle, +// leaving the entire encoded frame and its TOC untouched. The replacement is +// exactly 16 bits longer, so subsequent byte-aligned data stays aligned. +// +// This deliberately verifies the pinned encoder's header layout rather than +// trying to parse or rewrite arbitrary JPEG XL codestreams. A different header +// must fail closed, not produce a file that silently corrupts transparency. +fn correct_16_bit_alpha_header(bytes: &[u8], width: u32, height: u32) -> Result, CodecError> { + fn read(bytes: &[u8], pos: &mut usize, count: usize) -> Option { + let mut value = 0u32; + for bit in 0..count { + let offset = *pos; + let byte = *bytes.get(offset / 8)?; + value |= u32::from((byte >> (offset % 8)) & 1) << bit; + *pos += 1; + } + Some(value) + } + + fn read_dimension(bytes: &[u8], pos: &mut usize) -> Option { + let bits = match read(bytes, pos, 2)? { + 0 => 9, + 1 => 13, + 2 => 18, + _ => 30, + }; + read(bytes, pos, bits)?.checked_add(1) + } + + fn write_bits(out: &mut Vec, pos: &mut usize, count: usize, value: u32) { + for bit in 0..count { + if (*pos).is_multiple_of(8) { + out.push(0); + } + out[*pos / 8] |= (((value >> bit) & 1) as u8) << (*pos % 8); + *pos += 1; + } + } + + let mut pos = 0; + let header_matches = read(bytes, &mut pos, 16) == Some(0x0aff) + && read(bytes, &mut pos, 1) == Some(0) + && read_dimension(bytes, &mut pos) == Some(height) + && read(bytes, &mut pos, 3) == Some(0) + && read_dimension(bytes, &mut pos) == Some(width) + && read(bytes, &mut pos, 1) == Some(0) // metadata not all-default + && read(bytes, &mut pos, 1) == Some(0) // no extra fields + && read(bytes, &mut pos, 1) == Some(0) // integer samples + && read(bytes, &mut pos, 2) == Some(3) // custom bit depth + && read(bytes, &mut pos, 6) == Some(15) // 16 bits + && read(bytes, &mut pos, 1) == Some(0) // uses >14 bits + && read(bytes, &mut pos, 2) == Some(1); // one extra channel + let alpha_flag_pos = pos; + let header_matches = header_matches + && read(bytes, &mut pos, 1) == Some(1) // incorrect default 8-bit alpha + && read(bytes, &mut pos, 1) == Some(0) // not XYB + && read(bytes, &mut pos, 1) == Some(1) // default sRGB encoding (RGBA) + && read(bytes, &mut pos, 2) == Some(0) // no extensions + && read(bytes, &mut pos, 1) == Some(1); // default transform + + let frame_offset = pos.div_ceil(8); + let header_matches = header_matches && frame_offset <= bytes.len() && (pos..frame_offset * 8).all(|bit| bytes[bit / 8] & (1 << (bit % 8)) == 0); + if !header_matches { + return Err(CodecError::encode(F, "unexpected zune-jpegxl header for 16-bit alpha")); + } + + let mut fixed = Vec::with_capacity(bytes.len() + 2); + let mut cursor = 0; + for bit in 0..alpha_flag_pos { + write_bits(&mut fixed, &mut cursor, 1, u32::from((bytes[bit / 8] >> (bit % 8)) & 1)); + } + + // JPEG XL ExtraChannelInfo, non-default, straight 16-bit alpha: + // type=kAlpha (Enum=0), bit_depth=uint16, dim_shift=0, name_len=0, + // alpha_associated=false. Adds exactly 16 bits versus all_default=true. + write_bits(&mut fixed, &mut cursor, 1, 0); + write_bits(&mut fixed, &mut cursor, 2, 0); + write_bits(&mut fixed, &mut cursor, 1, 0); + write_bits(&mut fixed, &mut cursor, 2, 3); + write_bits(&mut fixed, &mut cursor, 6, 15); + write_bits(&mut fixed, &mut cursor, 2, 0); + write_bits(&mut fixed, &mut cursor, 2, 0); + write_bits(&mut fixed, &mut cursor, 1, 0); + + for bit in alpha_flag_pos + 1..frame_offset * 8 { + write_bits(&mut fixed, &mut cursor, 1, u32::from((bytes[bit / 8] >> (bit % 8)) & 1)); + } + if cursor != (frame_offset + 2) * 8 { + return Err(CodecError::encode(F, "JPEG XL alpha header alignment mismatch")); + } + fixed.extend_from_slice(&bytes[frame_offset..]); + Ok(fixed) +} + +pub(crate) fn encode(image: &Image, plan: Plan) -> Result, CodecError> { + let image = image.converted(plan.layout, plan.sample); + let space = match image.layout() { + ChannelLayout::Gray => ColorSpace::Luma, + ChannelLayout::GrayA => ColorSpace::LumaA, + ChannelLayout::Rgb => ColorSpace::RGB, + ChannelLayout::Rgba => ColorSpace::RGBA, + _ => return Err(CodecError::unsupported(F, "JPEG XL export supports grayscale and RGB, with optional alpha")), + }; + let depth = match image.sample_type() { + SampleType::U8 => BitDepth::Eight, + SampleType::U16 => BitDepth::Sixteen, + _ => return Err(CodecError::unsupported(F, "JPEG XL lossless export supports 8 or 16-bit integer pixels")), + }; + let (width, height) = image.dimensions(); + let samples = (width as usize) + .checked_mul(height as usize) + .and_then(|n| n.checked_mul(image.layout().channels())) + .and_then(|n| n.checked_mul(image.sample_type().bytes())) + .ok_or_else(|| CodecError::encode(F, "image dimensions overflow the buffer size"))?; + if image.data().len() != samples { + return Err(CodecError::InvalidImage("incorrect JPEG XL pixel buffer length".into())); + } + // A panic in an external image encoder must not terminate the host application. + catch_unwind(AssertUnwindSafe(|| { + let options = ZuneOptions::new(width as usize, height as usize, space, depth); + let mut output = Vec::new(); + JxlSimpleEncoder::new(image.data(), options).encode(&mut output).map_err(|e| CodecError::encode(F, e.to_string()))?; + if depth == BitDepth::Sixteen && image.layout() == ChannelLayout::Rgba { correct_16_bit_alpha_header(&output, width, height) } else { Ok(output) } + })) + .unwrap_or_else(|_| Err(CodecError::encode(F, "the JPEG XL encoder failed"))) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn alpha_header_correction_rejects_unexpected_or_truncated_headers() { + for bytes in [&[][..], &[0xff, 0x0a][..], &[0; 32][..]] { + assert!(correct_16_bit_alpha_header(bytes, 2, 2).is_err()); + } + } +} diff --git a/crates/image-codecs/src/codecs/pcx.rs b/crates/image-codecs/src/codecs/pcx.rs new file mode 100644 index 000000000..48dbf1169 --- /dev/null +++ b/crates/image-codecs/src/codecs/pcx.rs @@ -0,0 +1,139 @@ +//! ZSoft PCX (Paintbrush) reader: strictly bounded scanline RLE, 16/256-colour +//! palettes and 8-bit three-plane RGB. Read-only: modern saves should use PNG. + +use crate::SampleType; +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, Image}; +use crate::options::Limits; + +const HEADER: usize = 128; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::Pcx, reason) +} +fn le16(b: &[u8], i: usize) -> u16 { + u16::from_le_bytes([b[i], b[i + 1]]) +} + +/// PCX lacks a strong magic number. Require plausible complete header fields before +/// identifying it, rather than classifying every file starting with 0x0a as PCX. +pub(crate) fn looks_like_pcx(b: &[u8]) -> bool { + if b.len() < HEADER || b[0] != 0x0a || !matches!(b[1], 0 | 2 | 3 | 5) || b[2] != 1 { + return false; + } + let bits = b[3]; + let planes = b[65]; + let stride = u32::from(le16(b, 66)); + let (x0, y0, x1, y1) = (le16(b, 4), le16(b, 6), le16(b, 8), le16(b, 10)); + if x1 < x0 || y1 < y0 || !matches!(bits, 1 | 2 | 4 | 8) || !(1..=4).contains(&planes) { + return false; + } + let width = u32::from(x1 - x0) + 1; + let min_stride = (width * u32::from(bits)).div_ceil(8); + stride >= min_stride && stride > 0 && (bits != 8 || matches!(planes, 1 | 3)) && (bits == 8 || u32::from(bits) * u32::from(planes) <= 4) +} + +/// Read one compressed scanline, including all planes. RLE packets never cross a scanline. +fn read_row(b: &[u8], pos: &mut usize, out: &mut [u8]) -> Result<(), CodecError> { + let mut n = 0; + while n < out.len() { + let code = *b.get(*pos).ok_or_else(|| bad("truncated pixel RLE"))?; + *pos += 1; + let (count, value) = if code >= 0xc0 { + let count = usize::from(code & 0x3f); + let value = *b.get(*pos).ok_or_else(|| bad("truncated RLE packet"))?; + *pos += 1; + (count, value) + } else { + (1, code) + }; + if count == 0 || count > out.len() - n { + return Err(bad("RLE packet crosses a scanline or has zero length")); + } + out[n..n + count].fill(value); + n += count; + } + Ok(()) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_pcx(b) { + return Err(bad("invalid PCX header or unsupported pixel layout")); + } + let (w, h) = (u32::from(le16(b, 8) - le16(b, 4)) + 1, u32::from(le16(b, 10) - le16(b, 6)) + 1); + limits.check(w, h, ChannelLayout::Rgb, SampleType::U8)?; + let (bits, planes, stride) = (usize::from(b[3]), usize::from(b[65]), usize::from(le16(b, 66))); + let row_len = stride.checked_mul(planes).ok_or_else(|| bad("row size overflow"))?; + let total = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(3)).ok_or_else(|| bad("pixel size overflow"))?; + // The last 769 bytes are the optional VGA palette for 8-bit indexed images. + let vga = bits == 8 && planes == 1 && b.len() >= HEADER + 769 && b[b.len() - 769] == 0x0c; + if bits == 8 && planes == 1 && !vga && le16(b, 68) != 2 { + return Err(bad("8-bit indexed PCX has no 256-colour palette")); + } + let image_end = if vga { b.len() - 769 } else { b.len() }; + // A 63-byte RLE packet takes 2 bytes; a trailing remainder of 1 byte below 0xC0 + // can be encoded as a 1-byte literal, while any larger remainder takes at least 2 bytes. + // Because RLE packets may span planes within a scanline (`row_len`), compute the + // minimum encoded bytes across the full scanline (`row_len`) and multiply by `h`. + // Reject inputs that cannot possibly contain all scanlines before allocating + // the decoded image buffer. + let min_packets = (row_len / 63) + .checked_mul(2) + .and_then(|n| { + n.checked_add(match row_len % 63 { + 0 => 0, + 1 => 1, + _ => 2, + }) + }) + .ok_or_else(|| bad("compressed row size overflow"))?; + let min_encoded = min_packets.checked_mul(h as usize).ok_or_else(|| bad("compressed image size overflow"))?; + let encoded_len = image_end.saturating_sub(HEADER); + if encoded_len < min_encoded { + return Err(bad("PCX pixel data is too short for the declared image")); + } + let mut row = vec![0u8; row_len]; + let mut pixels = Vec::new(); + pixels.try_reserve_exact(total).map_err(|_| bad("PCX pixel buffer is too large"))?; + pixels.resize(total, 0); + let mut p = HEADER; + let width = w as usize; + for y in 0..h as usize { + read_row(&b[..image_end], &mut p, &mut row)?; + for x in 0..width { + let rgb = if bits == 8 && planes == 3 { + [row[x], row[stride + x], row[2 * stride + x]] + } else { + let index = if bits == 8 { + usize::from(row[x]) + } else { + let bit = x * bits; + let shift = 8 - bits - (bit % 8); + let mask = (1usize << bits) - 1; + (0..planes).fold(0usize, |v, plane| { + let sample = (usize::from(row[plane * stride + bit / 8]) >> shift) & mask; + v | sample << (plane * bits) + }) + }; + if vga { + let o = image_end + 1 + index * 3; + [b[o], b[o + 1], b[o + 2]] + } else if bits == 8 { + let g = index as u8; + [g, g, g] // palette-info 2: gray scale + } else { + let o = 16 + index * 3; + [b[o], b[o + 1], b[o + 2]] + } + }; + pixels[(y * width + x) * 3..(y * width + x + 1) * 3].copy_from_slice(&rgb); + } + } + let mut image = Image::from_u8(w, h, ChannelLayout::Rgb, pixels)?; + let (xdpi, ydpi) = (le16(b, 12), le16(b, 14)); + if xdpi > 0 && ydpi > 0 { + image.meta.dpi = Some((f32::from(xdpi), f32::from(ydpi))); + } + Ok(image) +} diff --git a/crates/image-codecs/src/codecs/png.rs b/crates/image-codecs/src/codecs/png.rs new file mode 100644 index 000000000..d1ff5140c --- /dev/null +++ b/crates/image-codecs/src/codecs/png.rs @@ -0,0 +1,2139 @@ +//! PNG via the `png` crate directly: 8/16-bit, Adam7 read (and write), +//! iCCP, eXIf, tEXt/zTXt/iTXt (XMP in `XML:com.adobe.xmp`), pHYs. + +use std::io::{self, Cursor, Write}; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, Limits, PngCompression, PngStereo}; + +const F: Format = Format::Png; +pub(crate) const XMP_KEYWORD: &str = "XML:com.adobe.xmp"; +const METERS_PER_INCH: f32 = 0.0254; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +/// Pixels per inch for `pHYs` pixels per metre. A whole number of ppi is stored rounded to whole +/// pixels per metre (300 ppi = 11811), so reading it back gives 299.9994: the whole number that +/// rounds to the stored value is the resolution that was meant, as Photoshop reads it. +pub(crate) fn dpi_of_ppm(ppm: u32) -> f32 { + let dpi = f64::from(ppm) * f64::from(METERS_PER_INCH); + let whole = dpi.round(); + if (whole / f64::from(METERS_PER_INCH)).round() == f64::from(ppm) { whole as f32 } else { dpi as f32 } +} + +/// Decodes a whole PNG, by driving a [`PngDecodeRows`] to its end. +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + PngDecodeRows::new(bytes, limits)?.step(&mut || true)?.ok_or_else(|| err("the decoder stopped before the image ended")) +} + +/// PNG bytes shared between the decoder and whoever else keeps them (a placed file's embedded +/// source): cloning shares the buffer. +#[derive(Debug, Clone)] +pub struct SharedBytes(pub std::sync::Arc>); + +impl AsRef<[u8]> for SharedBytes { + fn as_ref(&self) -> &[u8] { + self.0.as_slice() + } +} + +/// The input of a [`PngDecodeRows`] decoder: a cursor that also tells where the reader stopped, which +/// the text chunks after the image are read up to. +struct Tracked { + cursor: Cursor, + at: std::sync::Arc, +} + +impl> Tracked { + fn sync(&self) { + self.at.store(self.cursor.position(), std::sync::atomic::Ordering::Relaxed); + } +} + +impl> std::io::Read for Tracked { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let n = self.cursor.read(buf)?; + self.sync(); + Ok(n) + } +} + +impl> std::io::BufRead for Tracked { + fn fill_buf(&mut self) -> std::io::Result<&[u8]> { + self.cursor.fill_buf() + } + + fn consume(&mut self, amt: usize) { + self.cursor.consume(amt); + self.sync(); + } +} + +impl> std::io::Seek for Tracked { + fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { + let at = self.cursor.seek(pos)?; + self.sync(); + Ok(at) + } +} + +/// The decoder's input, shared between the reader and the metadata pass: moving an owned buffer in +/// doesn't keep a second copy of it. +struct SharedInput(std::sync::Arc); + +impl Clone for SharedInput { + fn clone(&self) -> Self { + Self(std::sync::Arc::clone(&self.0)) + } +} + +impl> AsRef<[u8]> for SharedInput { + fn as_ref(&self) -> &[u8] { + (*self.0).as_ref() + } +} + +/// A PNG decoded in resumable steps, for a caller that must not block for the whole image (the +/// web build, which has no threads): [`PngDecodeRows::step`] decodes rows until the caller says to +/// stop, and returns the [`Image`] once the last row and the chunks after it are read. [`decode`] +/// drives one to the end, so the two give the same image. +/// +/// An interlaced (Adam7) PNG decodes in a single step: its passes land at stride positions all +/// over the image, so no row is final before the last pass. +pub struct PngDecodeRows> { + reader: png::Reader>>, + at: std::sync::Arc, + bytes: SharedInput, + limits: Limits, + buf: Vec, + layout: ChannelLayout, + sample: SampleType, + /// The image's size (the first frame's, for an animation) and bytes per output row. + width: u32, + height: usize, + line: usize, + /// The decoder's whole output size, for an interlaced image's single frame decode. + size: usize, + rows: usize, + /// For [`PngDecodeRows::step_stream`]: the first row of the group held in `buf`. + group_start: usize, + interlaced: bool, + images: u64, + /// The chunks after the pixels: scanned from `text_at` up to `text_end`, a batch at a time, + /// with `text` keeping what was accepted and the decompressor's state. + text_at: usize, + text_end: usize, + text: TextScan, + /// True once the pixel reader has been finished and the tail is known. + tail: bool, + done: bool, + /// Test-only count of the pixel finalization: metadata yields must not repeat it. + #[cfg(test)] + pixel_finalizations: u32, +} + +impl> PngDecodeRows { + /// Reads the header and refuses what [`decode`] refuses before any pixel is decoded: sizes + /// past `limits`, and a file too small for the image it declares. + pub fn new(bytes: B, limits: &Limits) -> Result { + Self::open(bytes, limits, true) + } + + /// [`PngDecodeRows::new`], optionally without reserving the whole image: a caller that hands + /// the rows on as they come ([`decode_rows`]) never holds them all. + fn open(bytes: B, limits: &Limits, reserve: bool) -> Result { + let at = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0)); + let bytes = SharedInput(std::sync::Arc::new(bytes)); + let input = Tracked { cursor: Cursor::new(bytes.clone()), at: at.clone() }; + let mut decoder = png::Decoder::new_with_limits(input, png::Limits { bytes: limits.alloc_usize() }); + decoder.set_transformations(png::Transformations::EXPAND); + // png's lazy text get_text() inflates without consulting its allocation limit. + // Read text separately below, without retaining compressed copies in the decoder. + decoder.set_ignore_text_chunk(true); + { + let info = decoder.read_header_info().map_err(map_png_err)?; + let (w, h) = info.size(); + // Size checks now; the allocation budget once the output layout (channels and sample + // size) is known below, so a Gray8 image isn't charged for RGBA16. + limits.check_size(w, h)?; + // zlib never beats 1032:1; refuse a file too small for the declared image before the + // output buffer exists. Measured on the stored samples (before palette/bit expansion). + let stored = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(info.bits_per_pixel() as u64).div_ceil(8); + Limits::check_plausible(stored, bytes.as_ref().len(), 1100)?; + } + let reader = decoder.read_info().map_err(map_png_err)?; + // APNG: the IDAT image is the first frame, or an extra default image when no fcTL precedes it. + let images = reader.info().animation_control.map_or(1, |a| u64::from(a.num_frames) + u64::from(reader.info().frame_control.is_none())); + let (color, depth) = reader.output_color_type(); + let layout = match color { + png::ColorType::Grayscale => ChannelLayout::Gray, + png::ColorType::GrayscaleAlpha => ChannelLayout::GrayA, + png::ColorType::Rgb => ChannelLayout::Rgb, + png::ColorType::Rgba => ChannelLayout::Rgba, + png::ColorType::Indexed => return Err(err("palette was not expanded")), + }; + let sample = match depth { + png::BitDepth::Eight => SampleType::U8, + png::BitDepth::Sixteen => SampleType::U16, + d => return Err(err(format!("unexpected output bit depth {d:?}"))), + }; + // The frame the pixels are of: an fcTL before the IDAT overrides the header's size. + let info = reader.info(); + let (width, height) = info.frame_control.map_or_else(|| info.size(), |f| (f.width, f.height)); + let line = reader.output_line_size(width).ok_or_else(|| err("image too large"))?; + // The output layout is known: charge its real bytes per pixel, not RGBA16's worst case. + limits.check(width, height, layout, sample)?; + let height = usize::try_from(height).map_err(|_| err("image too large"))?; + if width == 0 || height == 0 { + return Err(err("the image is empty")); + } + let size = reader.output_buffer_size().ok_or_else(|| err("image too large"))?; + if line.checked_mul(height).is_none_or(|needed| needed > size) { + return Err(err("image too large")); + } + // Reserve without touching: each row is zero-filled as it is decoded, so the first slice + // doesn't zero-fill the whole image (a stall on the web build). + let mut buf = Vec::new(); + if reserve { + buf.try_reserve_exact(size).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {size} bytes for the image")))?; + } + let interlaced = info.interlaced; + Ok(Self { + reader, + at, + bytes, + limits: *limits, + buf, + layout, + sample, + width, + height, + line, + size, + rows: 0, + group_start: 0, + interlaced, + images, + text_at: 8, + text_end: 0, + text: TextScan::default(), + tail: false, + done: false, + #[cfg(test)] + pixel_finalizations: 0, + }) + } + + /// [`PngDecodeRows::new`] without reserving the whole image, for a caller that streams the rows + /// ([`PngDecodeRows::step_stream`]) and so never holds them all. A caller that finds it must + /// decode the whole image after all (an interlaced file) calls + /// [`PngDecodeRows::reserve_output`] before [`PngDecodeRows::step`]. + pub fn new_streaming(bytes: B, limits: &Limits) -> Result { + Self::open(bytes, limits, false) + } + + /// Reserves the buffer [`PngDecodeRows::step`] decodes the whole image into. + pub fn reserve_output(&mut self) -> Result<(), CodecError> { + let size = self.size; + self.buf.try_reserve_exact(size).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {size} bytes for the image"))) + } + + /// Whether the rows come in final order, so [`PngDecodeRows::step_stream`] can hand them on: + /// not an interlaced file, not an APNG (its first frame may be a sub-rectangle), and not one + /// whose `eXIf` before the image data asks for another orientation. (An `eXIf` after the image + /// data shows only in [`PngExtras::orientation`] at the end.) + pub fn streamable(&self) -> bool { + let info = self.reader.info(); + let upright = info.exif_metadata.as_deref().is_none_or(|e| crate::orientation::exif_orientation(e) == 1); + !self.interlaced && info.animation_control.is_none() && upright + } + + /// The size and pixel format the rows of [`PngDecodeRows::step_stream`] come in. + pub fn header(&self) -> PngHeader { + PngHeader { width: self.width, height: u32::try_from(self.height).unwrap_or(u32::MAX), layout: self.layout, sample: self.sample } + } + + /// The image's width and height in pixels. + pub fn size(&self) -> (u32, u32) { + (self.width, u32::try_from(self.height).unwrap_or(u32::MAX)) + } + + /// How much of the image is decoded, `0.0..=1.0`. + pub fn progress(&self) -> f32 { + if self.done { 1.0 } else { self.rows as f32 / self.height.max(1) as f32 } + } + + /// Decodes at least one row (the whole image if it is interlaced), then goes on while `more()` + /// returns true. `Some` once the image is complete, after which the decoder is spent. + pub fn step(&mut self, more: &mut dyn FnMut() -> bool) -> Result, CodecError> { + if self.done { + return Err(err("the image was already decoded")); + } + // Once the pixels are complete and the tail is known, a step only reads metadata: + // running the pixel reader again after `begin_tail` would decode an Adam7 frame twice + // or finish the image data again on every metadata yield. + if !self.tail { + if self.interlaced { + // An Adam7 frame is decoded in one go, over the whole output buffer. + self.buf.resize(self.size, 0); + let out = self.reader.next_frame(&mut self.buf).map_err(map_png_err)?; + self.buf.truncate(out.buffer_size()); + self.rows = self.height; + if self.sample == SampleType::U16 { + to_native_endian(&mut self.buf); + } + } + while self.rows < self.height { + let at = self.rows.checked_mul(self.line).ok_or_else(|| err("image too large"))?; + let end = at.checked_add(self.line).ok_or_else(|| err("image too large"))?; + // Grow by one row at a time: the zero-fill of the output is spread over the decodes + // instead of stalling the first slice. + if self.buf.len() < end { + self.buf.resize(end, 0); + } + let row = self.buf.get_mut(at..end).ok_or_else(|| err("the image data is longer than the image"))?; + self.reader.read_row(row).map_err(map_png_err)?.ok_or_else(|| err("the image data ended early"))?; + if self.sample == SampleType::U16 { + to_native_endian(row); + } + self.rows += 1; + if self.rows < self.height && !more() { + return Ok(None); + } + } + if !self.interlaced { + #[cfg(test)] + { + self.pixel_finalizations += 1; + } + // No row is left: this reads on to the end of the image data. + if self.reader.read_row(&mut []).map_err(map_png_err)?.is_some() { + return Err(err("the image has more rows than its header declares")); + } + self.buf.truncate(self.line.saturating_mul(self.height)); + } + // The chunks after the pixels, one bounded batch at a time: a large text chunk never + // makes one step block, and what was accepted stays accepted when a step stops. + self.begin_tail()?; + } + while !self.text.done { + self.text_batch()?; + if !self.text.done && !more() { + return Ok(None); + } + } + self.done = true; + self.finish().map(Some) + } + + /// Decodes the rows of a [`streamable`](PngDecodeRows::streamable) file a group at a time: + /// `sink(y0, data)` gets whole rows `y0..` in final order, `group` of them at a time (the last + /// group may be shorter; 0 counts as 1), native-endian, exactly the samples [`decode`] would + /// return, and the decoder keeps only that group. Decodes at least one row, then goes on while + /// `more()` returns true; `Some` with the extras once the file is read to its end, with the + /// resolution a format that lacks one takes from its EXIF or XMP. A damaged file fails with + /// the error [`decode`] gives, after however many rows were delivered. An orientation (an + /// `eXIf` after the image data may set one) is for the caller to apply. + pub fn step_stream>( + &mut self, + group: u32, + more: &mut dyn FnMut() -> bool, + sink: &mut PngRowSink<'_, E>, + ) -> Result, E> { + if self.done { + return Err(err("the image was already decoded").into()); + } + let group = usize::try_from(group.max(1)).unwrap_or(usize::MAX); + if !self.tail { + while self.rows < self.height { + let held = self.rows - self.group_start; + let at = held.checked_mul(self.line).ok_or_else(|| err("image too large"))?; + let end = at.checked_add(self.line).ok_or_else(|| err("image too large"))?; + if self.buf.len() < end { + self.buf.try_reserve_exact(end - self.buf.len()).map_err(|_| CodecError::LimitExceeded("cannot allocate a group of rows".into()))?; + self.buf.resize(end, 0); + } + let row = self.buf.get_mut(at..end).ok_or_else(|| err("invalid PNG row group"))?; + if self.reader.read_row(row).map_err(map_png_err)?.is_none() { + return Err(err("the image data ends before the last row").into()); + } + if self.sample == SampleType::U16 { + to_native_endian(row); + } + self.rows += 1; + if self.rows - self.group_start == group || self.rows == self.height { + let data = self.buf.get(..end).ok_or_else(|| err("invalid PNG row group"))?; + sink(u32::try_from(self.group_start).map_err(|_| err("image too large"))?, data)?; + self.group_start = self.rows; + } + if self.rows < self.height && !more() { + return Ok(None); + } + } + // Past the last row: trailing damage fails the same way it does in `decode`. + if self.reader.read_row(&mut []).map_err(map_png_err)?.is_some() { + return Err(err("the image has more rows than its header declares").into()); + } + self.begin_tail()?; + } + while !self.text.done { + self.text_batch()?; + if !self.text.done && !more() { + return Ok(None); + } + } + self.done = true; + let mut extras = self.extras(); + // As `decode_as_with` does for every format: a resolution the format lacks comes from EXIF or XMP. + if extras.meta.dpi.is_none() { + extras.meta.dpi = crate::resolution::from_metadata(extras.meta.exif.as_deref(), extras.meta.xmp.as_deref()); + } + Ok(Some(extras)) + } + + /// Finishes the pixel reader and fixes where the metadata scan ends. Tolerate malformed + /// tails once pixels are complete, but never swallow an allocation-limit failure. + fn begin_tail(&mut self) -> Result<(), CodecError> { + if let Err(e @ png::DecodingError::LimitsExceeded) = self.reader.finish() { + return Err(map_png_err(e)); + } + self.text_end = usize::try_from(self.at.load(std::sync::atomic::Ordering::Relaxed)).map_err(|_| err("invalid PNG reader position"))?; + self.text.remaining = self.limits.alloc_usize(); + self.tail = true; + Ok(()) + } + + /// Reads one bounded batch of the chunks after the pixels: up to [`TEXT_CHUNK_BATCH`] chunks, + /// or one batch of one text chunk's CRC or value while either is mid-stream. + fn text_batch(&mut self) -> Result<(), CodecError> { + let bytes = self.bytes.clone(); + let bytes = bytes.as_ref(); + if let Some(pending) = self.text.pending.take() { + match pending { + TextPending::Crc(p) => self.crc_batch(p, bytes)?, + TextPending::Value(p) => match inflate_text(p, bytes)? { + TextBatch::More(p) => self.text.pending = Some(TextPending::Value(p)), + TextBatch::Finished(p) => { + let (keyword, keyword_len, kind, text) = (p.keyword, p.keyword_len, p.kind, p.out); + self.text.accept(keyword, keyword_len, kind, text)?; + } + TextBatch::Invalid(produced) => self.text.charge(produced)?, + }, + } + return Ok(()); + } + let mut chunks = 0; + while chunks < TEXT_CHUNK_BATCH && !self.text.done { + if self.scan_text_chunk(bytes)? { + break; + } + chunks += 1; + } + Ok(()) + } + + /// Hashes one bounded batch of a text chunk's data. Only a fully CRC-valid chunk is parsed + /// into a value, so a broken chunk is still dropped before its keyword or flags are used. + fn crc_batch(&mut self, mut p: PendingCrc, bytes: &[u8]) -> Result<(), CodecError> { + let Some(left) = p.data.end.checked_sub(p.at) else { return Ok(()) }; + let take = left.min(TEXT_BATCH); + let Some(end) = p.at.checked_add(take) else { return Ok(()) }; + let Some(slice) = bytes.get(p.at..end) else { return Ok(()) }; + p.crc.update(slice); + p.at = end; + if p.at < p.data.end { + self.text.pending = Some(TextPending::Crc(p)); + return Ok(()); + } + if p.crc.sum() != p.checksum { + return Ok(()); + } + let Some(data) = bytes.get(p.data.clone()) else { return Ok(()) }; + if let Some(value) = self.pending_text(p.kind, data, p.data.start)? { + self.text.pending = Some(TextPending::Value(value)); + } + Ok(()) + } + + /// The value of a CRC-valid text chunk, with its decoded keyword and where its bytes start. + fn pending_text(&self, kind: [u8; 4], data: &[u8], data_at: usize) -> Result, CodecError> { + // A keyword is 1..=79 Latin-1 bytes, so only the start of the chunk can hold it. + let Some(nul) = data.iter().take(80).position(|&b| b == 0) else { return Ok(None) }; + let Some(keyword) = data.get(..nul) else { return Ok(None) }; + let Some(value_start) = nul.checked_add(1) else { return Ok(None) }; + let Some(value) = data.get(value_start..) else { return Ok(None) }; + if keyword.is_empty() { + return Ok(None); + } + let (value, compressed, latin1, kind) = match &kind { + b"tEXt" => (value, false, true, TextKind::Plain), + b"zTXt" => { + let Some((&0, value)) = value.split_first() else { return Ok(None) }; + (value, true, true, TextKind::Compressed) + } + _ => { + let Some(([flag, method], value)) = value.split_first_chunk::<2>() else { return Ok(None) }; + if *flag > 1 || (*flag == 1 && *method != 0) { + return Ok(None); + } + let Some((language, value)) = split_nul(value) else { return Ok(None) }; + let Some((translated, value)) = split_nul(value) else { return Ok(None) }; + if !language.is_ascii() || std::str::from_utf8(translated).is_err() { + return Ok(None); + } + (value, *flag == 1, false, TextKind::International) + } + }; + let keyword_len = latin1_len(keyword).ok_or_else(text_limit)?; + let keyword: String = keyword.iter().map(|&b| char::from(b)).collect(); + let Some(value_at) = data_at.checked_add(data.len().saturating_sub(value.len())) else { return Ok(None) }; + let Some(value_end) = value_at.checked_add(value.len()) else { return Ok(None) }; + Ok(Some(PendingText { + kind, + keyword, + keyword_len, + limit: self.text.remaining.saturating_sub(keyword_len), + input: value_at..value_end, + at: value_at, + latin1, + decoder: compressed.then(|| flate2::Decompress::new(true)), + // An uncompressed iTXt value's UTF-8 is checked batch by batch, before its budget. + utf8: (!compressed && !latin1).then(Utf8Check::default), + over: false, + out: Vec::new(), + })) + } + + /// Reads the header of the next chunk, starting a text chunk's CRC (which then reads its + /// bytes in batches) or stepping over any other. False when the scan can go on in this + /// batch; true when a text chunk was started and its bytes need their own batches. Sets + /// [`TextScan::done`] at the end of the tail, at `IEND`, or at a malformed one, as the + /// previous one-pass reader did. + fn scan_text_chunk(&mut self, bytes: &[u8]) -> Result { + let Some(rest) = bytes.get(self.text_at..self.text_end) else { + self.text.done = true; + return Ok(false); + }; + let Some((length, rest)) = rest.split_first_chunk::<4>() else { + self.text.done = true; + return Ok(false); + }; + let length = u32::from_be_bytes(*length); + // PNG chunk lengths are at most 2^31-1. Broken tails remain tolerated. + if length > i32::MAX as u32 { + self.text.done = true; + return Ok(false); + } + let Ok(length) = usize::try_from(length) else { + self.text.done = true; + return Ok(false); + }; + let Some((kind, rest)) = rest.split_first_chunk::<4>() else { + self.text.done = true; + return Ok(false); + }; + let Some((_data, rest)) = rest.split_at_checked(length) else { + self.text.done = true; + return Ok(false); + }; + let Some((checksum, _)) = rest.split_first_chunk::<4>() else { + self.text.done = true; + return Ok(false); + }; + let Some(next) = self.text_at.checked_add(12).and_then(|at| at.checked_add(length)).filter(|&at| at <= self.text_end) else { + self.text.done = true; + return Ok(false); + }; + let Some(data_at) = self.text_at.checked_add(8) else { + self.text.done = true; + return Ok(false); + }; + self.text_at = next; + if kind == b"IEND" { + self.text.done = true; + return Ok(false); + } + if !matches!(kind, b"tEXt" | b"zTXt" | b"iTXt") { + return Ok(false); + } + let mut crc = flate2::Crc::new(); + crc.update(kind); + let Some(data_end) = data_at.checked_add(length) else { + self.text.done = true; + return Ok(false); + }; + // The CRC of a large chunk is hashed a bounded batch at a time too, so one metadata + // step never scans the whole chunk. + self.text.pending = + Some(TextPending::Crc(PendingCrc { kind: *kind, data: data_at..data_end, at: data_at, crc, checksum: u32::from_be_bytes(*checksum) })); + Ok(true) + } + + /// What the file holds besides its pixels, with what the metadata scan accepted. + fn extras(&mut self) -> PngExtras { + let info = self.reader.info(); + let icc = info.icc_profile.as_ref().map(|c| c.to_vec()); + let mut meta = Metadata { exif: info.exif_metadata.as_ref().map(|c| c.to_vec()), ..Default::default() }; + if let Some(d) = info.pixel_dims + && d.unit == png::Unit::Meter + && d.xppu > 0 + && d.yppu > 0 + { + meta.dpi = Some((dpi_of_ppm(d.xppu), dpi_of_ppm(d.yppu))); + } + meta.text = std::mem::take(&mut self.text.plain); + meta.text.extend(std::mem::take(&mut self.text.compressed)); + meta.text.extend(std::mem::take(&mut self.text.international)); + meta.xmp = self.text.xmp.take(); + let mut warnings = Vec::new(); + if self.images > 1 { + warnings.push(DecodeWarning::MoreFrames { total: u32::try_from(self.images).ok() }); + } + let orientation = meta.exif.as_deref().map_or(1, crate::orientation::exif_orientation); + PngExtras { icc, meta, warnings, orientation } + } + + /// The image itself, with what the metadata scan accepted. + fn finish(&mut self) -> Result { + let height = u32::try_from(self.height).map_err(|_| err("image too large"))?; + let mut img = Image::from_raw(self.width, height, self.layout, self.sample, std::mem::take(&mut self.buf))?; + let extras = self.extras(); + img.icc = extras.icc; + img.meta = extras.meta; + img.warnings = extras.warnings; + Ok(img) + } +} + +/// 16-bit samples from the file's big-endian bytes to the machine's. +fn to_native_endian(samples: &mut [u8]) { + for c in samples.as_chunks_mut::<2>().0 { + let v = u16::from_be_bytes([c[0], c[1]]); + c.copy_from_slice(&v.to_ne_bytes()); + } +} + +/// What a PNG holds besides its pixels. These can sit after the image data (`eXIf`, text), so +/// they are complete only once the pixels have been read. +pub struct PngExtras { + pub icc: Option>, + pub meta: Metadata, + pub warnings: Vec, + /// The EXIF orientation (1-8) the pixels are stored in; 1 when there is none. + pub orientation: u16, +} + +/// Inspect APNG's declared images, including a separate default image, without decoding pixels. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + let limits = Limits::default(); + let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits { bytes: limits.alloc_usize() }); + decoder.set_ignore_text_chunk(true); + let reader = decoder.read_info().map_err(map_png_err)?; + let info = reader.info(); + Ok(info.animation_control.is_some_and(|a| u64::from(a.num_frames) + u64::from(info.frame_control.is_none()) > 1)) +} + +/// The size and pixel format the rows of [`decode_rows`] come in. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct PngHeader { + pub width: u32, + pub height: u32, + pub layout: ChannelLayout, + pub sample: SampleType, +} + +/// Where [`decode_rows`] sends rows: the index of the first, then whole rows. +pub type PngRowSink<'a, E> = dyn FnMut(u32, &[u8]) -> Result<(), E> + 'a; + +/// Decodes a PNG a group of rows at a time, so the caller never needs the whole decoded image: +/// `begin` gets the header once and says whether to go on (after the same size and plausibility checks [`decode`] makes), +/// then `rows(y0, data)` gets whole rows `y0..` in final order, `group` of them at a time (the +/// last group may be shorter; a `group` of 0 counts as 1), native-endian, exactly the samples [`decode`] would return. Returns the extras once the file +/// has been read to its end, or `None` (having called neither closure) when the rows can't be +/// had in final order: an interlaced (Adam7) file, and an APNG, whose first frame may be a +/// sub-rectangle. A damaged file fails with the error [`decode`] gives, after however many rows +/// were delivered. The limits are checked before the first row; an orientation, which an `eXIf` +/// after the image data may set, is for the caller to apply. +pub(crate) fn decode_rows>( + bytes: &[u8], + limits: &Limits, + group: u32, + begin: &mut dyn FnMut(&PngHeader) -> Result, + rows: &mut PngRowSink<'_, E>, +) -> Result, E> { + // The limits and plausibility checks are made here, before any row; the output is not reserved. + let mut dec = PngDecodeRows::new_streaming(bytes, limits)?; + if !dec.streamable() || !begin(&dec.header())? { + return Ok(None); + } + // With a `more` that always says go on, the first call decodes the whole file. + dec.step_stream(group, &mut || true, rows) +} + +/// The colour table of a palette PNG (`PLTE`) and the one entry its `tRNS` makes transparent. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PngPalette { + pub colors: Vec<[u8; 3]>, + pub transparent: Option, +} + +/// The colour table of a palette PNG (colour type 3), read from the headers without decoding +/// pixels. `None` for other colour types, and for a `tRNS` with partial alpha or several +/// transparent entries: an Indexed Color document holds one transparent entry. +pub fn palette(bytes: &[u8]) -> Result, CodecError> { + let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits::default()); + decoder.set_ignore_text_chunk(true); + let reader = decoder.read_info().map_err(map_png_err)?; + let info = reader.info(); + if info.color_type != png::ColorType::Indexed { + return Ok(None); + } + let Some(plte) = info.palette.as_deref() else { return Ok(None) }; + let colors = plte.as_chunks::<3>().0.to_vec(); + let mut transparent = None; + for (i, &a) in info.trns.as_deref().unwrap_or_default().iter().enumerate() { + match a { + 255 => {} + 0 if transparent.is_none() && i < colors.len() => transparent = u8::try_from(i).ok(), + _ => return Ok(None), + } + } + Ok(Some(PngPalette { colors, transparent })) +} + +/// The stereo pairing a PNG declares in its `sTER` chunk (PNG Extensions 1.4.0, §3.6), read +/// from the headers without decoding pixels. `None` when the file has no CRC-valid `sTER` with a +/// defined mode byte. The first valid chunk wins, as a later one would contradict it. +pub fn stereo(bytes: &[u8]) -> Option { + let mut rest = bytes.get(8..)?; + while let Some((length, tail)) = rest.split_first_chunk::<4>() { + let Some((kind, tail)) = tail.split_first_chunk::<4>() else { break }; + let length = u32::from_be_bytes(*length); + if length > i32::MAX as u32 { + break; + } + let Ok(length) = usize::try_from(length) else { break }; + let Some(data) = tail.get(..length) else { break }; + let Some((checksum, next)) = tail.get(length..).and_then(<[u8]>::split_first_chunk::<4>) else { break }; + rest = next; + if kind == b"IEND" { + break; + } + if kind != b"sTER" || data.len() != 1 { + continue; + } + let mut crc = flate2::Crc::new(); + crc.update(kind); + crc.update(data); + if crc.sum() != u32::from_be_bytes(*checksum) { + continue; + } + return PngStereo::from_mode(data[0]); + } + None +} + +fn map_png_err(e: png::DecodingError) -> CodecError { + match e { + png::DecodingError::LimitsExceeded => CodecError::LimitExceeded("PNG decoder memory limit".into()), + e => err(e), + } +} + +fn text_limit() -> CodecError { + CodecError::LimitExceeded("PNG decoded text metadata exceeds max_alloc".into()) +} + +fn split_nul(bytes: &[u8]) -> Option<(&[u8], &[u8])> { + let at = bytes.iter().position(|&b| b == 0)?; + Some((bytes.get(..at)?, bytes.get(at.checked_add(1)?..)?)) +} + +fn latin1_len(bytes: &[u8]) -> Option { + bytes.len().checked_add(bytes.iter().filter(|&&b| b >= 128).count()) +} + +/// The text chunks after the pixels, accepted one bounded batch at a time: one shared budget for +/// decoded UTF-8 keywords and values (including overwritten XMP), the CRC and decompressor of a +/// chunk kept between batches. Invalid ancillary text stays optional, as it was with png's text +/// decoder. +#[derive(Default)] +struct TextScan { + /// UTF-8 bytes left of [`Limits::max_alloc`] for the text. + remaining: usize, + plain: Vec<(String, String)>, + compressed: Vec<(String, String)>, + international: Vec<(String, String)>, + xmp: Option, + pending: Option, + done: bool, +} + +impl TextScan { + /// Charges `produced` bytes to the shared budget: what a chunk inflated counts even when its + /// text is then dropped (bad UTF-8, a corrupt or truncated stream), or every rejected chunk + /// could inflate up to the whole remaining budget again. + fn charge(&mut self, produced: usize) -> Result<(), CodecError> { + self.remaining = self.remaining.checked_sub(produced).ok_or_else(text_limit)?; + Ok(()) + } + + /// Keeps a decoded value, charging the keyword and text to the shared budget; invalid UTF-8 + /// text is dropped, but what it inflated is still charged. + fn accept(&mut self, keyword: String, keyword_len: usize, kind: TextKind, out: Vec) -> Result<(), CodecError> { + let produced = out.len(); + let Ok(text) = String::from_utf8(out) else { return self.charge(produced) }; + self.remaining = self.remaining.checked_sub(keyword_len).and_then(|left| left.checked_sub(text.len())).ok_or_else(text_limit)?; + match kind { + // Some writers put XMP in Latin-1 text chunks instead of the usual iTXt. + // Treat the reserved keyword consistently, after the same budget checks. + _ if keyword == XMP_KEYWORD => self.xmp = Some(text), + TextKind::Plain => self.plain.push((keyword, text)), + TextKind::Compressed => self.compressed.push((keyword, text)), + TextKind::International => self.international.push((keyword, text)), + } + Ok(()) + } +} + +/// Which list a text chunk is kept in, as the previous decoder grouped them by chunk type. +#[derive(Clone, Copy, PartialEq, Eq)] +enum TextKind { + Plain, + Compressed, + International, +} + +/// A text chunk being read: its CRC first, then (when valid) its value. +enum TextPending { + Crc(PendingCrc), + Value(PendingText), +} + +/// The CRC of a text chunk's data, hashed a bounded batch at a time before the chunk is parsed. +struct PendingCrc { + kind: [u8; 4], + /// The chunk's data in the file, and the next byte a batch hashes. + data: std::ops::Range, + at: usize, + crc: flate2::Crc, + checksum: u32, +} + +/// A text chunk's value being read, with its position in the file and (when compressed) the +/// inflator kept between batches. +struct PendingText { + kind: TextKind, + keyword: String, + keyword_len: usize, + /// The most decoded bytes this value may take: the budget left minus its keyword. + limit: usize, + /// The value's bytes in the file (the compressed ones for zTXt and compressed iTXt). + input: std::ops::Range, + /// The next byte of `input` a batch reads or inflates. + at: usize, + latin1: bool, + /// Some for a compressed value. + decoder: Option, + /// Some for an uncompressed iTXt value: its UTF-8 is checked before its bytes are charged. + utf8: Option, + /// The value has outgrown the budget but its UTF-8 validity is still being checked, so an + /// invalid value is still dropped instead of failing the decode. + over: bool, + out: Vec, +} + +/// Incremental UTF-8 validation across batches, so a value that spans batches is checked as a +/// whole before it can fail the budget. A sequence straddling two batches keeps its expected +/// continuation range here. +#[derive(Default)] +struct Utf8Check { + /// Continuation bytes still needed, and the range the next one may take. + need: u8, + min: u8, + max: u8, +} + +impl Utf8Check { + /// Feeds the next bytes of the value; false at the first one that isn't valid UTF-8. + fn feed(&mut self, bytes: &[u8]) -> bool { + for &b in bytes { + if self.need > 0 { + if b < self.min || b > self.max { + return false; + } + self.need -= 1; + (self.min, self.max) = (0x80, 0xBF); + } else { + match b { + 0x00..=0x7f => {} + 0xc2..=0xdf => { + self.need = 1; + (self.min, self.max) = (0x80, 0xBF); + } + 0xe0 => { + self.need = 2; + (self.min, self.max) = (0xa0, 0xBF); + } + 0xe1..=0xec | 0xee..=0xef => { + self.need = 2; + (self.min, self.max) = (0x80, 0xBF); + } + 0xed => { + self.need = 2; + (self.min, self.max) = (0x80, 0x9f); + } + 0xf0 => { + self.need = 3; + (self.min, self.max) = (0x90, 0xBF); + } + 0xf1..=0xf3 => { + self.need = 3; + (self.min, self.max) = (0x80, 0xBF); + } + 0xf4 => { + self.need = 3; + (self.min, self.max) = (0x80, 0x8f); + } + _ => return false, + } + } + } + true + } + + /// Whether the bytes so far end on a whole character. + fn complete(&self) -> bool { + self.need == 0 + } +} + +/// What one batch of a text value produced. +enum TextBatch { + /// The value needs another batch. + More(PendingText), + /// Every byte of the value was read; `out` is its UTF-8 text. + Finished(PendingText), + /// A broken chunk: drop it, as the previous reader did. + /// The bytes it inflated, which are charged to the budget even though the text is dropped. + Invalid(usize), +} + +/// How much of one text value one batch reads or inflates. +const TEXT_BATCH: usize = 8192; +/// How many chunks one batch scans when it isn't continuing a text value. +const TEXT_CHUNK_BATCH: usize = 64; + +/// Reads one batch of a text value. Inflation uses a fixed scratch buffer and checks each batch +/// against the budget before growing the output, so no input-sized allocation is made first. +fn inflate_text(mut p: PendingText, bytes: &[u8]) -> Result { + let Some(input) = bytes.get(p.input.clone()) else { return Ok(TextBatch::Invalid(p.out.len())) }; + let mut scratch = [0; TEXT_BATCH]; + if let Some(decoder) = p.decoder.as_mut() { + let Some(tail) = input.get(p.at.saturating_sub(p.input.start)..) else { return Ok(TextBatch::Invalid(p.out.len())) }; + let start_in = decoder.total_in(); + let start_out = decoder.total_out(); + let status = decoder.decompress(tail, &mut scratch, flate2::FlushDecompress::None); + let used = usize::try_from(decoder.total_in().saturating_sub(start_in)).unwrap_or(usize::MAX); + let made = usize::try_from(decoder.total_out().saturating_sub(start_out)).unwrap_or(usize::MAX); + let Ok(status) = status else { + // The backend may write a batch before it reports corruption: charge it too, Latin-1 + // text at its UTF-8 length as on the success path. + let charged = match scratch.get(..made) { + Some(batch) if p.latin1 => latin1_len(batch).unwrap_or(usize::MAX), + _ => made, + }; + return Ok(TextBatch::Invalid(p.out.len().saturating_add(charged))); + }; + let (Some(used), Some(batch)) = (p.at.checked_add(used), scratch.get(..made)) else { return Ok(TextBatch::Invalid(p.out.len())) }; + p.at = used; + if !append_checked(&mut p.out, &mut p.utf8, &mut p.over, batch, p.latin1, p.limit)? { + return Ok(TextBatch::Invalid(p.out.len())); + } + if status == flate2::Status::StreamEnd { + return Ok(TextBatch::Finished(p)); + } + if start_in == decoder.total_in() && start_out == decoder.total_out() { + return Ok(TextBatch::Invalid(p.out.len())); // Truncated stream: never keep partial text. + } + } else { + let remaining = input.len().saturating_sub(p.at.saturating_sub(p.input.start)); + let take = remaining.min(TEXT_BATCH); + let start = p.at.saturating_sub(p.input.start); + let Some(batch) = input.get(start..start.saturating_add(take)) else { return Ok(TextBatch::Invalid(0)) }; + p.at = p.at.saturating_add(take); + if !append_checked(&mut p.out, &mut p.utf8, &mut p.over, batch, p.latin1, p.limit)? { + // Stored text isn't inflated: nothing is charged for it. + return Ok(TextBatch::Invalid(0)); + } + if take == remaining { + if p.utf8.as_ref().is_some_and(|check| !check.complete()) { + return Ok(TextBatch::Invalid(0)); + } + if p.over { + return Err(text_limit()); + } + return Ok(TextBatch::Finished(p)); + } + } + Ok(TextBatch::More(p)) +} + +/// Appends one batch of decoded bytes, refusing to grow past the budget. Every Latin-1 code +/// point fits in at most two UTF-8 bytes. +/// +/// An uncompressed iTXt value is UTF-8 checked batch by batch before it is charged: an invalid +/// value is dropped even when it is over the budget, as the previous whole-value check did. +/// Its budget error is deferred until every byte has been seen and found valid. +fn append_checked(out: &mut Vec, utf8: &mut Option, over: &mut bool, bytes: &[u8], latin1: bool, limit: usize) -> Result { + if let Some(check) = utf8.as_mut() + && !check.feed(bytes) + { + return Ok(false); + } + if *over { + return Ok(true); + } + let added = if latin1 { latin1_len(bytes).ok_or_else(text_limit)? } else { bytes.len() }; + if out.len().checked_add(added).is_none_or(|len| len > limit) { + if utf8.is_some() { + *over = true; + return Ok(true); + } + return Err(text_limit()); + } + out.try_reserve(added).map_err(|_| text_limit())?; + if latin1 { + let mut buf = [0; 2]; + for &b in bytes { + out.extend_from_slice(char::from(b).encode_utf8(&mut buf).as_bytes()); + } + } else { + out.extend_from_slice(bytes); + } + Ok(true) +} + +fn is_latin1_keyword(k: &str) -> bool { + !k.is_empty() && k.len() <= 79 && k.chars().all(|c| (' '..='~').contains(&c) || ('\u{a1}'..='\u{ff}').contains(&c)) +} + +/// Makes the PNG writer's output growth report an encode error on allocation failure. +struct FallibleOutput<'a>(&'a mut Vec); + +impl Write for FallibleOutput<'_> { + fn write(&mut self, bytes: &[u8]) -> io::Result { + self.0.try_reserve(bytes.len()).map_err(|_| io::Error::other("not enough memory for PNG output"))?; + self.0.extend_from_slice(bytes); + Ok(bytes.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let bpp = img.layout().channels() * img.sample_type().bytes(); + // Big-endian sample bytes. + let mut data = std::borrow::Cow::Borrowed(img.data()); + if img.sample_type() == SampleType::U16 { + to_big_endian(data.to_mut()); + } + let mut out = Vec::new(); + { + let mut output = FallibleOutput(&mut out); + let mut writer = start(&mut output, (w, h), (img.layout(), img.sample_type()), img.icc.as_ref(), &img.meta, opts)?; + if opts.png_interlaced { + let idat = adam7_idat(&data, w as usize, h as usize, bpp, opts.png_compression)?; + write_idat(&mut writer, &idat, MAX_IDAT)?; + } else if let Some(idat) = parallel_idat(&data, w as usize, h as usize, bpp, opts.png_compression) { + write_idat(&mut writer, &idat, MAX_IDAT)?; + } else { + writer.write_image_data(&data).map_err(|e| CodecError::encode(F, e))?; + } + writer.finish().map_err(|e| CodecError::encode(F, e))?; + } + Ok(out) +} + +/// 16-bit native-endian samples to PNG's big-endian order, in place. +fn to_big_endian(data: &mut [u8]) { + for c in data.as_chunks_mut::<2>().0 { + let v = u16::from_ne_bytes([c[0], c[1]]); + c.copy_from_slice(&v.to_be_bytes()); + } +} + +/// Writes the signature and every chunk ahead of the image data for a `w` × `h` PNG of +/// `layout` and `sample` (already the encode plan's), with its profile and metadata. +fn start( + out: W, + (w, h): (u32, u32), + (layout, sample): (ChannelLayout, SampleType), + icc: Option<&Vec>, + meta: &Metadata, + opts: &EncodeOptions, +) -> Result, CodecError> { + let color = match layout { + ChannelLayout::Gray => png::ColorType::Grayscale, + ChannelLayout::GrayA => png::ColorType::GrayscaleAlpha, + ChannelLayout::Rgb => png::ColorType::Rgb, + ChannelLayout::Rgba => png::ColorType::Rgba, + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let depth = match sample { + SampleType::U8 => png::BitDepth::Eight, + SampleType::U16 => png::BitDepth::Sixteen, + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + }; + let mut info = png::Info::with_size(w, h); + info.color_type = color; + info.bit_depth = depth; + info.interlaced = opts.png_interlaced; + if opts.embed_icc + && let Some(icc) = icc + { + info.icc_profile = Some(icc.clone().into()); + } + if opts.embed_metadata { + if let Some(exif) = &meta.exif { + // The pixels are written as they are shown: never let a viewer rotate them again. + info.exif_metadata = Some(crate::resolution::export_exif(exif, meta.dpi).into_owned().into()); + } + if let Some((x, y)) = meta.dpi + && x > 0.0 + && y > 0.0 + { + info.pixel_dims = + Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + } + } + let mut encoder = png::Encoder::with_info(out, info).map_err(|e| CodecError::encode(F, e))?; + encoder.set_compression(match opts.png_compression { + PngCompression::None => png::Compression::NoCompression, + PngCompression::Fast => png::Compression::Fast, + PngCompression::Default => png::Compression::Balanced, + PngCompression::Best => png::Compression::High, + }); + if opts.embed_metadata { + for (k, v) in &meta.text { + let latin1_text = v.chars().all(|c| (c as u32) < 256); + let res = if is_latin1_keyword(k) && latin1_text { + encoder.add_text_chunk(k.clone(), v.clone()) + } else if is_latin1_keyword(k) { + encoder.add_itxt_chunk(k.clone(), v.clone()) + } else { + continue; + }; + res.map_err(|e| CodecError::encode(F, e))?; + } + if let Some(xmp) = &meta.xmp { + encoder.add_itxt_chunk(XMP_KEYWORD.into(), crate::resolution::export_xmp(xmp, meta.dpi).into_owned()).map_err(|e| CodecError::encode(F, e))?; + } + } + let mut writer = encoder.write_header().map_err(|e| CodecError::encode(F, e))?; + if let Some(stereo) = opts.png_stereo { + // Ancillary chunk before IDAT, as the spec recommends for `sTER`. + writer.write_chunk(png::chunk::ChunkType(*b"sTER"), &[stereo.mode()]).map_err(|e| CodecError::encode(F, e))?; + } + Ok(writer) +} + +/// Largest IDAT chunk written. A chunk holds at most 2^31 - 1 bytes, so a zlib stream past that +/// (a multi-gigapixel image) is split over several IDAT chunks, which readers join back. +const MAX_IDAT: usize = 1 << 30; + +/// `zlib` as IDAT chunks of at most `max` bytes. +fn write_idat(writer: &mut png::Writer, zlib: &[u8], max: usize) -> Result<(), CodecError> { + for part in zlib.chunks(max.max(1)) { + writer.write_chunk(png::chunk::IDAT, part).map_err(|e| CodecError::encode(F, e))?; + } + Ok(()) +} + +/// A PNG encoded a band of rows at a time, for images too large to hold whole (a multi-gigapixel +/// print): each band is converted to the encode plan, filtered and deflated on all cores as it +/// arrives and written as IDAT chunks, so only about one band is ever held. The deflate bands are +/// [`parallel_idat`]'s, so the zlib stream is the one [`crate::encode`] writes for a large image. +/// Never interlaced (Adam7 needs the whole image). +pub struct PngRows { + writer: png::Writer, + plan: Plan, + width: u32, + height: u32, + /// Rows received so far. + rows: u32, + level: PngCompression, + /// Converted, big-endian rows that don't fill a deflate band yet. + pending: Vec, + /// The last row deflated: the row above `pending` for the filters. + prev: Vec, + adler: u32, + /// Whether the zlib header is written. + started: bool, +} + +impl PngRows { + /// Writes the header of a `first.width()` × `height` PNG to `out`. The layout, sample type, + /// profile and metadata come from `first`, the first band (not written yet: [`Self::push`] + /// it like the others), through the same plan as [`crate::encode`]. + pub fn new(out: W, first: &Image, height: u32, opts: &EncodeOptions) -> Result { + if opts.png_interlaced { + return Err(CodecError::unsupported(F, "an interlaced PNG can't be written a band of rows at a time")); + } + let width = first.width(); + if width == 0 || height == 0 { + return Err(CodecError::InvalidImage("cannot encode an empty image".into())); + } + let plan = crate::fidelity::plan(first, F, opts); + let writer = start(out, (width, height), (plan.layout, plan.sample), first.icc.as_ref(), &first.meta, opts)?; + Ok(Self { writer, plan, width, height, rows: 0, level: opts.png_compression, pending: Vec::new(), prev: Vec::new(), adler: 1, started: false }) + } + + /// Encodes the next rows of the image: `band` is as wide as the image, in any layout and + /// sample type (converted like the first band). + pub fn push(&mut self, band: &Image) -> Result<(), CodecError> { + if band.width() != self.width { + return Err(CodecError::InvalidImage(format!("a {} px wide band for a {} px wide image", band.width(), self.width))); + } + let rows = self.rows.checked_add(band.height()).filter(|&r| r <= self.height); + let Some(rows) = rows else { + return Err(CodecError::InvalidImage(format!("more than the image's {} rows", self.height))); + }; + let img = band.converted(self.plan.layout, self.plan.sample); + let at = self.pending.len(); + self.pending.try_reserve(img.data().len()).map_err(|_| CodecError::encode(F, "not enough memory for a band of rows"))?; + self.pending.extend_from_slice(img.data()); + if self.plan.sample == SampleType::U16 { + to_big_endian(self.pending.get_mut(at..).unwrap_or_default()); + } + self.rows = rows; + self.deflate_pending() + } + + /// Deflates the whole bands held, and the rest once every row is in. + fn deflate_pending(&mut self) -> Result<(), CodecError> { + let bpp = self.plan.layout.channels() * self.plan.sample.bytes(); + let stride = self.width as usize * bpp; + let per_band = band_rows(stride); + let held = self.pending.len() / stride.max(1); + let finish = self.rows == self.height; + let take = if finish { held } else { held / per_band * per_band }; + if take == 0 { + return Ok(()); + } + let rows = self.pending.get(..take * stride).unwrap_or_default(); + let prev = (!self.prev.is_empty()).then_some(self.prev.as_slice()); + let parts = deflate_rows(rows, prev, stride, bpp, per_band, deflate_level(self.level), finish, encode_threads()) + .ok_or_else(|| CodecError::encode(F, "a band of rows could not be compressed"))?; + let mut zlib = Vec::new(); + zlib.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6).map_err(|_| CodecError::encode(F, "not enough memory for PNG data"))?; + if !self.started { + zlib.extend_from_slice(&zlib_header(self.level)); + self.started = true; + } + for p in &parts { + zlib.extend_from_slice(&p.z); + self.adler = adler32_combine(self.adler, p.adler, p.len); + } + if finish { + zlib.extend_from_slice(&self.adler.to_be_bytes()); + } + write_idat(&mut self.writer, &zlib, MAX_IDAT)?; + self.prev.clear(); + self.prev.extend_from_slice(rows.get(rows.len() - stride..).unwrap_or_default()); + self.pending.drain(..take * stride); + Ok(()) + } + + /// Ends the file. Fails when fewer rows than the image's height were pushed. + pub fn finish(self) -> Result<(), CodecError> { + if self.rows != self.height || !self.pending.is_empty() { + return Err(CodecError::InvalidImage(format!("{} of the image's {} rows were written", self.rows, self.height))); + } + self.writer.finish().map_err(|e| CodecError::encode(F, e)) + } +} + +/// Build the zlib stream for an Adam7-interlaced image (filter type 0). +fn adam7_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression) -> Result, CodecError> { + // (x0, y0, dx, dy) per pass. + const PASSES: [(usize, usize, usize, usize); 7] = [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)]; + let mut raw = Vec::new(); + raw.try_reserve_exact(data.len() + h * 7).map_err(|_| CodecError::encode(F, "not enough memory for PNG rows"))?; + for (x0, y0, dx, dy) in PASSES { + if x0 >= w || y0 >= h { + continue; + } + let mut y = y0; + while y < h { + raw.push(0u8); + let mut x = x0; + while x < w { + let o = (y * w + x) * bpp; + raw.extend_from_slice(&data[o..o + bpp]); + x += dx; + } + y += dy; + } + } + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), deflate_level(level)); + z.write_all(&raw).map_err(|e| CodecError::encode(F, e))?; + z.finish().map_err(|e| CodecError::encode(F, e)) +} + +/// The deflate level of [`PngCompression::Fast`]: zlib-rs level 1 writes static Huffman blocks +/// only (20-30% bigger on filtered photos); level 3 is the lowest that is never bigger than +/// miniz's level 1 was, at about twice its time. +const FAST: flate2::Compression = flate2::Compression::new(3); + +/// Images at least this large (bytes) are filtered and deflated on all cores. +const PARALLEL_MIN_BYTES: usize = 4 << 20; + +/// The zlib stream of a non-interlaced image, built in parallel (`None` below +/// [`PARALLEL_MIN_BYTES`], on wasm, or without compression): rows get the adaptive filter +/// (minimum sum of absolute differences over the five PNG filters, as libpng and the `png` +/// crate do), then bands of rows are deflated independently at the requested level, each +/// ending on a sync flush so the raw deflate streams concatenate into one (as `pigz` does). +/// A 36 MP RGB export drops from ~9 s to well under a second on 12 cores; the output is a few +/// percent larger (no dictionary across bands). +fn parallel_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression) -> Option> { + if cfg!(target_arch = "wasm32") || data.len() < PARALLEL_MIN_BYTES || level == PngCompression::None || w == 0 || h == 0 { + return None; + } + banded_idat(data, w, h, bpp, level, encode_threads()) +} + +/// Filtered bytes per deflate band. The bands depend only on the image, never on the number of +/// threads, so the same image and options give the same bytes whatever the machine's core count. +const BAND_BYTES: usize = 512 << 10; + +/// The deflate effort for `level`. +fn deflate_level(level: PngCompression) -> flate2::Compression { + match level { + PngCompression::None => flate2::Compression::none(), + PngCompression::Fast => FAST, + PngCompression::Default => flate2::Compression::default(), + PngCompression::Best => flate2::Compression::best(), + } +} + +/// The zlib stream header: deflate, 32 K window; FLEVEL only advertises the effort. +fn zlib_header(level: PngCompression) -> [u8; 2] { + match level { + PngCompression::None | PngCompression::Fast => [0x78, 0x01], + PngCompression::Best => [0x78, 0xDA], + PngCompression::Default => [0x78, 0x9C], + } +} + +/// Rows per deflate band for rows of `stride` bytes. +fn band_rows(stride: usize) -> usize { + (BAND_BYTES / stride.saturating_add(1)).max(1) +} + +/// Worker threads for the banded encoders: one on wasm (no threads), else the machine's cores. +fn encode_threads() -> usize { + if cfg!(target_arch = "wasm32") { 1 } else { std::thread::available_parallelism().map_or(1, |n| n.get()).clamp(1, 32) } +} + +/// [`parallel_idat`] on `threads` worker threads. +fn banded_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression, threads: usize) -> Option> { + let stride = w * bpp; + let parts = deflate_rows(data.get(..h * stride)?, None, stride, bpp, band_rows(stride), deflate_level(level), true, threads)?; + let mut out = Vec::new(); + out.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6).ok()?; + out.extend_from_slice(&zlib_header(level)); + let mut adler = 1u32; + for p in &parts { + out.extend_from_slice(&p.z); + adler = adler32_combine(adler, p.adler, p.len); + } + out.extend_from_slice(&adler.to_be_bytes()); + Some(out) +} + +/// One band of rows filtered and deflated: the raw deflate bytes, and the Adler-32 and length of +/// the filtered bytes they hold. +struct Deflated { + z: Vec, + adler: u32, + len: usize, +} + +/// `rows` (whole rows of `stride` bytes; `prev` is the row above the first, if any) filtered and +/// deflated in independent bands of `band_rows` rows on up to `threads` workers (inline for one). +/// The last band finishes the deflate stream when `finish`; every other band ends on a sync flush. +/// `None` when a band did not deflate in one call ([`deflate_band`]). +#[allow(clippy::too_many_arguments)] +fn deflate_rows( + rows: &[u8], + prev: Option<&[u8]>, + stride: usize, + bpp: usize, + band_rows: usize, + lvl: flate2::Compression, + finish: bool, + threads: usize, +) -> Option> { + let h = rows.len() / stride.max(1); + let bands: Vec<(usize, usize)> = (0..h).step_by(band_rows.max(1)).map(|y| (y, (y + band_rows).min(h))).collect(); + let band = |&(y0, y1): &(usize, usize)| -> Option { + let mut filtered = Vec::new(); + filtered.try_reserve_exact((y1 - y0) * (stride + 1)).ok()?; + let mut scratch = Vec::new(); + scratch.try_reserve_exact(stride).ok()?; + scratch.resize(stride, 0); + for y in y0..y1 { + let row = rows.get(y * stride..(y + 1) * stride)?; + let above = if y > 0 { rows.get((y - 1) * stride..y * stride) } else { prev }; + filter_row(row, above, bpp, &mut scratch, &mut filtered); + } + let adler = adler32(1, &filtered); + let z = deflate_band(&filtered, lvl, finish && y1 == h)?; + Some(Deflated { z, adler, len: filtered.len() }) + }; + if threads <= 1 || bands.len() <= 1 { + return bands.iter().map(band).collect(); + } + let next = std::sync::atomic::AtomicUsize::new(0); + let mut parts: Vec<(usize, Deflated)> = std::thread::scope(|sc| { + let workers: Vec<_> = (0..threads.min(bands.len())) + .map(|_| { + sc.spawn(|| { + let mut out = Vec::new(); + loop { + let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let Some(b) = bands.get(i) else { break }; + let Some(d) = band(b) else { return Vec::new() }; + out.push((i, d)); + } + out + }) + }) + .collect(); + workers.into_iter().flat_map(|w| w.join().unwrap_or_default()).collect() + }); + if parts.len() != bands.len() { + return None; + } + parts.sort_by_key(|p| p.0); + Some(parts.into_iter().map(|p| p.1).collect()) +} + +/// Raw deflate of one band: ends on a sync flush, or finishes the stream for the `last` band. +/// The output buffer is sized past deflate's worst case so that one call takes the whole band +/// and completes the flush: when a sync flush fills the buffer, miniz_oxide (flate2's former Rust +/// backend) can return before compressing the last lookahead bytes, and the next call only +/// drains the pending output, so a resumed flush silently drops data. `None` (the caller then +/// uses the serial encoder) if the single call did not finish. +fn deflate_band(filtered: &[u8], lvl: flate2::Compression, last: bool) -> Option> { + let mut c = flate2::Compress::new(lvl, false); + // Stored blocks cost 5 bytes per 64 KiB, the flush an empty stored block: 1/8 is ample. + let mut z = Vec::new(); + z.try_reserve_exact(filtered.len() + filtered.len() / 8 + 1024).ok()?; + let flush = if last { flate2::FlushCompress::Finish } else { flate2::FlushCompress::Sync }; + let status = c.compress_vec(filtered, &mut z, flush).ok()?; + let done = if last { status == flate2::Status::StreamEnd } else { c.total_in() as usize == filtered.len() && z.len() < z.capacity() }; + done.then_some(z) +} + +/// One scanline with the adaptive filter choice, appended to `out` (type byte + data). +fn filter_row(row: &[u8], prev: Option<&[u8]>, bpp: usize, scratch: &mut [u8], out: &mut Vec) { + let n = row.len(); + let up = |i: usize| prev.map_or(0, |p| p[i]); + let left = |i: usize| if i >= bpp { row[i - bpp] } else { 0 }; + let ul = |i: usize| if i >= bpp { prev.map_or(0, |p| p[i - bpp]) } else { 0 }; + let paeth = |a: u8, b: u8, c: u8| { + let p = i16::from(a) + i16::from(b) - i16::from(c); + let (pa, pb, pc) = ((p - i16::from(a)).abs(), (p - i16::from(b)).abs(), (p - i16::from(c)).abs()); + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } + }; + let cost = |v: &[u8]| v.iter().map(|&b| u64::from((b as i8).unsigned_abs())).sum::(); + let mut best = cost(row); + let start = out.len(); + out.push(0); + out.extend_from_slice(row); + for kind in 1u8..=4 { + if best == 0 { + break; + } + let mut c = 0u64; + for i in 0..n { + let x = row[i]; + scratch[i] = match kind { + 1 => x.wrapping_sub(left(i)), + 2 => x.wrapping_sub(up(i)), + 3 => x.wrapping_sub(((u16::from(left(i)) + u16::from(up(i))) / 2) as u8), + _ => x.wrapping_sub(paeth(left(i), up(i), ul(i))), + }; + c += u64::from((scratch[i] as i8).unsigned_abs()); + if c >= best { + break; + } + } + if c < best { + best = c; + out[start] = kind; + out[start + 1..].copy_from_slice(&scratch[..n]); + } + } +} + +const ADLER_MOD: u32 = 65_521; + +fn adler32(start: u32, data: &[u8]) -> u32 { + let (mut a, mut b) = (start & 0xffff, start >> 16); + for chunk in data.chunks(5552) { + for &x in chunk { + a += u32::from(x); + b += a; + } + a %= ADLER_MOD; + b %= ADLER_MOD; + } + (b << 16) | a +} + +/// Adler-32 of A ‖ B from adler(A), adler(B) and |B| (zlib's `adler32_combine`). +fn adler32_combine(a1: u32, a2: u32, len2: usize) -> u32 { + let rem = (len2 % ADLER_MOD as usize) as u64; + let m = u64::from(ADLER_MOD); + let (s1a, s2a) = (u64::from(a1 & 0xffff), u64::from(a1 >> 16)); + let (s1b, s2b) = (u64::from(a2 & 0xffff), u64::from(a2 >> 16)); + let s1 = (s1a + s1b + m - 1) % m; + let s2 = (s2a + s2b + (rem * s1a) % m + m - rem) % m; + ((s2 as u32) << 16) | s1 as u32 +} + +/// Writes an 8-bit palette PNG (PNG-8): `indices` row-major, `palette` RGB entries (≤ 256), +/// optional fully transparent entry (`tRNS`) and optional resolution in dpi (`pHYs`, written when +/// both axes are positive and finite). Used for Indexed Color documents. +pub fn encode_indexed( + width: u32, + height: u32, + indices: &[u8], + palette: &[[u8; 3]], + transparent: Option, + dpi: Option<(f32, f32)>, + stereo: Option, +) -> Result, CodecError> { + if width == 0 || height == 0 || indices.len() != width as usize * height as usize { + return Err(CodecError::InvalidImage("index data does not match the size".into())); + } + if palette.is_empty() || palette.len() > 256 || indices.iter().any(|&i| usize::from(i) >= palette.len()) { + return Err(CodecError::InvalidImage("palette must have 1..=256 entries covering every index".into())); + } + let mut info = png::Info::with_size(width, height); + info.color_type = png::ColorType::Indexed; + info.bit_depth = png::BitDepth::Eight; + info.palette = Some(palette.iter().flatten().copied().collect::>().into()); + if let Some(t) = transparent.filter(|&t| usize::from(t) < palette.len()) { + let mut trns = vec![255u8; usize::from(t) + 1]; + trns[usize::from(t)] = 0; + info.trns = Some(trns.into()); + } + if let Some((x, y)) = dpi + && x.is_finite() + && y.is_finite() + && x > 0.0 + && y > 0.0 + { + info.pixel_dims = + Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + } + let mut out = Vec::new(); + { + let encoder = png::Encoder::with_info(&mut out, info).map_err(|e| CodecError::encode(F, e))?; + let mut writer = encoder.write_header().map_err(|e| CodecError::encode(F, e))?; + if let Some(stereo) = stereo { + writer.write_chunk(png::chunk::ChunkType(*b"sTER"), &[stereo.mode()]).map_err(|e| CodecError::encode(F, e))?; + } + writer.write_image_data(indices).map_err(|e| CodecError::encode(F, e))?; + writer.finish().map_err(|e| CodecError::encode(F, e))?; + } + Ok(out) +} + +#[cfg(test)] +mod stereo_tests { + use super::*; + + /// One CRC-valid chunk as it appears in a file. + fn chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + out.extend_from_slice(kind); + out.extend_from_slice(data); + let mut crc = flate2::Crc::new(); + crc.update(kind); + crc.update(data); + out.extend_from_slice(&crc.sum().to_be_bytes()); + out + } + + fn png_with(stereo: &[u8]) -> Vec { + let mut out = b"\x89PNG\r\n\x1a\n".to_vec(); + out.extend_from_slice(&chunk(b"IHDR", &[0, 0, 0, 2, 0, 0, 0, 1, 8, 2, 0, 0, 0])); + if !stereo.is_empty() { + out.extend_from_slice(&chunk(b"sTER", stereo)); + } + out.extend_from_slice(&chunk(b"IDAT", &[0x78, 0x01, 0x01, 0x00, 0xff, 0xff, 0, 3, 1, 0, 0])); + out.extend_from_slice(&chunk(b"IEND", &[])); + out + } + + #[test] + fn reads_both_defined_views() { + assert_eq!(stereo(&png_with(&[0])), Some(PngStereo::CrossFuse)); + assert_eq!(stereo(&png_with(&[1])), Some(PngStereo::DivergingFuse)); + assert_eq!(stereo(&png_with(&[])), None); + // Reserved and unknown mode bytes are not a pairing we can keep. + assert_eq!(stereo(&png_with(&[2])), None); + assert_eq!(stereo(&png_with(&[255])), None); + // A two-byte payload is not sTER data. + assert_eq!(stereo(&png_with(&[0, 0])), None); + assert_eq!(stereo(b"not a png"), None); + } + + #[test] + fn ster_mode_bytes_describe_eye_order_not_vertical_packing() { + // PNG Extensions 1.4.0 §3.6: both views occupy horizontal halves. + // Each view is eight columns, so the second starts on an eight-column boundary. + for (mode, left_pixel, right_pixel) in [(0, [0, 0, 255], [255, 0, 0]), (1, [255, 0, 0], [0, 0, 255])] { + let pixels: Vec = [left_pixel; 8].into_iter().chain([right_pixel; 8]).flatten().collect(); + let img = Image::from_raw(16, 1, ChannelLayout::Rgb, SampleType::U8, pixels.clone()).unwrap(); + let pairing = if mode == 0 { PngStereo::CrossFuse } else { PngStereo::DivergingFuse }; + let opts = EncodeOptions { png_stereo: Some(pairing), ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + let chunk = bytes.windows(4).position(|w| w == b"sTER").unwrap(); + assert_eq!(bytes[chunk + 4], mode, "the specification's literal mode byte"); + let decoded = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(decoded.dimensions(), (16, 1)); + assert_eq!(decoded.data(), pixels); + } + } + + #[test] + fn a_broken_crc_is_not_a_stereo_mark() { + let mut bytes = png_with(&[0]); + let kind = bytes.windows(4).position(|w| w == b"sTER").unwrap(); + // The CRC trails the type and the one-byte payload of the chunk. + bytes[kind + 8] ^= 0xff; + assert_eq!(stereo(&bytes), None); + } + + #[test] + fn encode_writes_the_chunk_before_idat_and_it_reads_back() { + let img = Image::from_raw(2, 1, ChannelLayout::Rgb, SampleType::U8, vec![9; 6]).unwrap(); + for view in [PngStereo::CrossFuse, PngStereo::DivergingFuse] { + let opts = EncodeOptions { png_stereo: Some(view), ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + assert_eq!(stereo(&bytes), Some(view)); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.data(), img.data()); + let ter = bytes.windows(4).position(|w| w == b"sTER").unwrap(); + let idat = bytes.windows(4).position(|w| w == b"IDAT").unwrap(); + assert!(ter < idat, "sTER before IDAT"); + } + let plain = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(stereo(&plain), None); + } + + #[test] + fn interlaced_and_indexed_encoders_keep_the_mark() { + let img = Image::from_raw(2, 2, ChannelLayout::Rgb, SampleType::U8, vec![9; 12]).unwrap(); + let opts = EncodeOptions { png_stereo: Some(PngStereo::DivergingFuse), png_interlaced: true, ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + assert_eq!(stereo(&bytes), Some(PngStereo::DivergingFuse)); + let pal = [[255, 0, 0], [0, 0, 255]]; + let bytes = encode_indexed(2, 1, &[0, 1], &pal, None, None, Some(PngStereo::CrossFuse)).unwrap(); + assert_eq!(stereo(&bytes), Some(PngStereo::CrossFuse)); + assert_eq!(palette(&bytes).unwrap().map(|p| p.colors), Some(pal.to_vec())); + } +} + +#[cfg(test)] +mod indexed_tests { + use super::*; + + #[test] + fn png8_roundtrips_through_the_decoder() { + let pal = [[255, 0, 0], [0, 0, 255], [0, 0, 0]]; + let idx = [0u8, 1, 2, 1, 0, 2]; + let bytes = encode_indexed(3, 2, &idx, &pal, Some(2), None, None).unwrap(); + let img = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(img.dimensions(), (3, 2)); + let px = img.convert(ChannelLayout::Rgba, SampleType::U8); + assert_eq!(&px.data()[..8], &[255, 0, 0, 255, 0, 0, 255, 255]); + assert_eq!(px.data()[11], 0, "transparent entry"); + assert!(encode_indexed(3, 2, &[9; 6], &pal, None, None, None).is_err()); + assert!(encode_indexed(3, 3, &idx, &pal, None, None, None).is_err()); + } + + fn png(color: png::ColorType, trns: Option<&[u8]>) -> Vec { + let mut out = Vec::new(); + let mut e = png::Encoder::new(&mut out, 2, 1); + e.set_color(color); + e.set_depth(png::BitDepth::Eight); + e.set_palette(vec![1, 2, 3, 4, 5, 6]); + if let Some(t) = trns { + e.set_trns(t.to_vec()); + } + let data = if color == png::ColorType::Rgb { vec![0; 6] } else { vec![0, 1] }; + e.write_header().unwrap().write_image_data(&data).unwrap(); + out + } + + #[test] + fn palette_reads_the_table_and_its_one_transparent_entry() { + let bytes = encode_indexed(2, 1, &[0, 1], &[[1, 2, 3], [4, 5, 6]], Some(1), None, None).unwrap(); + let colors = vec![[1, 2, 3], [4, 5, 6]]; + assert_eq!(palette(&bytes).unwrap(), Some(PngPalette { colors: colors.clone(), transparent: Some(1) })); + assert_eq!(palette(&png(png::ColorType::Indexed, None)).unwrap(), Some(PngPalette { colors, transparent: None })); + // A truecolour PNG's suggested palette is not a colour table. + assert_eq!(palette(&png(png::ColorType::Rgb, None)).unwrap(), None); + // Partial alpha or two transparent entries don't fit one transparent index. + assert_eq!(palette(&png(png::ColorType::Indexed, Some(&[255, 128]))).unwrap(), None); + assert_eq!(palette(&png(png::ColorType::Indexed, Some(&[0, 0]))).unwrap(), None); + } +} + +#[cfg(test)] +mod parallel_tests { + use super::*; + + #[test] + #[ignore = "manual 24 MP release benchmark"] + fn benchmark_filter_selection() { + use std::{hint::black_box, time::Instant}; + for pattern in ["zero", "identical", "varied"] { + let row: Vec = (0..6000 * 4).map(|i| if pattern == "zero" { 0 } else { (i * 17 + i / 13) as u8 }).collect(); + let prev: Vec<_> = row.iter().map(|v| if pattern == "identical" { *v } else { v.wrapping_add(3) }).collect(); + let mut scratch = vec![0; row.len()]; + let mut out = Vec::with_capacity(row.len() + 1); + for trial in 0..6 { + type Filter = fn(&[u8], Option<&[u8]>, usize, &mut [u8], &mut Vec); + let mut paths: [(&str, Filter); 2] = [("main", previous_filter_row), ("split", filter_row)]; + if trial % 2 == 1 { + paths.reverse(); + } + for (name, path) in paths { + let t = Instant::now(); + for _ in 0..4000 { + out.clear(); + black_box(path)(black_box(&row), Some(black_box(&prev)), 4, &mut scratch, &mut out); + black_box(&out); + } + println!("filter,{pattern},{trial},{name},{:.6}", t.elapsed().as_secs_f64() * 1000.0); + } + } + } + } + + // The main-branch implementation before this refactor. + fn previous_filter_row(row: &[u8], prev: Option<&[u8]>, bpp: usize, scratch: &mut [u8], out: &mut Vec) { + let n = row.len(); + let up = |i: usize| prev.map_or(0, |p| p[i]); + let left = |i: usize| if i >= bpp { row[i - bpp] } else { 0 }; + let ul = |i: usize| if i >= bpp { prev.map_or(0, |p| p[i - bpp]) } else { 0 }; + let paeth = |a: u8, b: u8, c: u8| { + let p = i16::from(a) + i16::from(b) - i16::from(c); + let (pa, pb, pc) = ((p - i16::from(a)).abs(), (p - i16::from(b)).abs(), (p - i16::from(c)).abs()); + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } + }; + let cost = |v: &[u8]| v.iter().map(|&b| u64::from((b as i8).unsigned_abs())).sum::(); + let mut best = (cost(row), 0u8); + for kind in 1u8..=4 { + for i in 0..n { + let x = row[i]; + scratch[i] = match kind { + 1 => x.wrapping_sub(left(i)), + 2 => x.wrapping_sub(up(i)), + 3 => x.wrapping_sub(((u16::from(left(i)) + u16::from(up(i))) / 2) as u8), + _ => x.wrapping_sub(paeth(left(i), up(i), ul(i))), + }; + } + let c = cost(&scratch[..n]); + if c < best.0 { + best = (c, kind); + } + } + out.push(best.1); + if best.1 == 0 { + out.extend_from_slice(row); + return; + } + for i in 0..n { + let x = row[i]; + scratch[i] = match best.1 { + 1 => x.wrapping_sub(left(i)), + 2 => x.wrapping_sub(up(i)), + 3 => x.wrapping_sub(((u16::from(left(i)) + u16::from(up(i))) / 2) as u8), + _ => x.wrapping_sub(paeth(left(i), up(i), ul(i))), + }; + } + out.extend_from_slice(&scratch[..n]); + } + + #[test] + fn adaptive_filter_preserves_minimum_cost_and_ties() { + for bpp in [1, 2, 3, 4, 6, 8] { + for seed in 0..32usize { + let row: Vec = (0..137).map(|i| (i * seed + i / 5) as u8).collect(); + let shifted: Vec<_> = row.iter().map(|v| v.wrapping_add(seed as u8)).collect(); + for prev in [None, Some(row.as_slice()), Some(shifted.as_slice())] { + let mut expected = vec![99]; + let mut actual = expected.clone(); + let mut scratch = vec![0; row.len()]; + previous_filter_row(&row, prev, bpp, &mut scratch, &mut expected); + filter_row(&row, prev, bpp, &mut scratch, &mut actual); + assert_eq!(actual, expected, "bpp={bpp}, seed={seed}"); + } + } + } + let mut out = Vec::new(); + filter_row(&[0; 16], None, 4, &mut [0; 16], &mut out); + assert_eq!(out, vec![0; 17]); + out.clear(); + filter_row(&[1; 16], Some(&[0; 16]), 1, &mut [0; 16], &mut out); + assert_eq!(out, [vec![1, 1], vec![0; 15]].concat()); + } + + #[test] + fn adler_combine_matches_one_pass() { + let a: Vec = (0..100_000u32).map(|i| (i * 7 + i / 13) as u8).collect(); + let b: Vec = (0..70_001u32).map(|i| (i * 31) as u8).collect(); + let whole: Vec = a.iter().chain(&b).copied().collect(); + assert_eq!(adler32_combine(adler32(1, &a), adler32(1, &b), b.len()), adler32(1, &whole)); + } + + #[test] + fn bands_that_end_just_past_a_block_keep_all_their_data() { + // Deflate output ~0.6 of the input, cut just past one of miniz_oxide's block + // boundaries: a sync flush that fills the output buffer there used to lose the + // last lookahead bytes, corrupting every band after it. + for (k, len) in [(20u32, 136_916usize), (20, 345_044), (24, 195_796), (28, 189_142)] { + let mut x = 0x9E37_79B9u32 ^ k; + let data: Vec = (0..len) + .map(|_| { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + ((x >> 8) % k) as u8 + }) + .collect(); + for last in [false, true] { + let z = deflate_band(&data, flate2::Compression::default(), last).unwrap(); + let mut d = flate2::Decompress::new(false); + let mut back = Vec::with_capacity(len + 1); + d.decompress_vec(&z, &mut back, flate2::FlushDecompress::Sync).unwrap(); + assert!(back == data, "k {k} len {len} last {last}: {} of {len} bytes", back.len()); + } + } + } + + /// The bytes depend on the image, not on the machine: one, two, seven and 32 worker threads + /// (and the machine's own count, through `encode`) give the same stream. + #[test] + fn the_parallel_encoder_gives_the_same_bytes_on_any_thread_count() { + let (w, h, bpp) = (1201usize, 1013usize, 4usize); + let px: Vec = + (0..w * h * bpp).map(|i| ((i / 5) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 11) as u8 / 5 + (i % 89) as u8).collect(); + assert!(px.len() >= PARALLEL_MIN_BYTES && px.len() > 2 * BAND_BYTES); + for level in [PngCompression::Fast, PngCompression::Default, PngCompression::Best] { + let one = banded_idat(&px, w, h, bpp, level, 1).unwrap(); + for threads in [2, 7, 32] { + assert!(banded_idat(&px, w, h, bpp, level, threads).unwrap() == one, "{level:?}: {threads} threads differ from one"); + } + assert!(parallel_idat(&px, w, h, bpp, level).unwrap() == one, "{level:?}: this machine's thread count differs from one"); + let img = Image::from_raw(w as u32, h as u32, ChannelLayout::Rgba, SampleType::U8, px.clone()).unwrap(); + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let png = crate::encode(&img, Format::Png, &opts).unwrap(); + assert!(png.windows(one.len()).any(|c| c == one.as_slice()), "{level:?}: the file carries the stream"); + assert_eq!(decode(&png, &Limits::default()).unwrap().data(), img.data()); + } + } + + #[test] + fn large_images_roundtrip_through_the_parallel_encoder() { + for (layout, sample, ch, bytes) in [(ChannelLayout::Rgb, SampleType::U8, 3usize, 1usize), (ChannelLayout::Rgba, SampleType::U16, 4, 2)] { + let (w, h) = (1531u32, 977u32); + let n = w as usize * h as usize * ch * bytes; + let px: Vec = (0..n).map(|i| ((i / 3) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect(); + let img = Image::from_raw(w, h, layout, sample, px.clone()).unwrap(); + assert!(n >= PARALLEL_MIN_BYTES || sample == SampleType::U8); + for level in [PngCompression::Fast, PngCompression::Default] { + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.dimensions(), (w, h)); + assert_eq!(back.convert(layout, sample).data(), img.data(), "{layout:?} {sample:?} {level:?}"); + // The oracle decoder agrees. + let o = image::load_from_memory_with_format(&bytes, image::ImageFormat::Png).unwrap(); + assert_eq!((o.width(), o.height()), (w, h)); + } + } + } +} + +#[cfg(test)] +mod streamed_tests { + use super::*; + + /// (bytes ahead of the first IDAT, the IDAT payloads joined, how many IDAT chunks). + fn split(png: &[u8]) -> (Vec, Vec, usize) { + let (mut at, mut head, mut zlib, mut count) = (8, None, Vec::new(), 0); + while at + 12 <= png.len() { + let len = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize; + if &png[at + 4..at + 8] == b"IDAT" { + head.get_or_insert_with(|| png[..at].to_vec()); + zlib.extend_from_slice(&png[at + 8..at + 8 + len]); + count += 1; + } + at += 12 + len; + } + (head.unwrap(), zlib, count) + } + + fn noise(n: usize, seed: u32) -> Vec { + (0..n).map(|i| ((i / 3) as u32 ^ seed).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect() + } + + /// `img` pushed in bands of `heights` rows (the last one takes the rest). + fn streamed(img: &Image, heights: &[u32], opts: &EncodeOptions) -> Vec { + let (w, h) = img.dimensions(); + let row = img.data().len() / h as usize; + let mut bands = Vec::new(); + let mut y = 0; + for &n in heights.iter().chain(std::iter::once(&u32::MAX)) { + let n = n.min(h - y); + if n == 0 { + continue; + } + let data = img.data()[y as usize * row..(y + n) as usize * row].to_vec(); + bands.push(Image::from_raw(w, n, img.layout(), img.sample_type(), data).unwrap().with_icc(img.icc.clone()).with_meta(img.meta.clone())); + y += n; + } + let mut out = Vec::new(); + let mut rows = PngRows::new(&mut out, &bands[0], h, opts).unwrap(); + for b in &bands { + rows.push(b).unwrap(); + } + rows.finish().unwrap(); + out + } + + /// Bands of any height give the zlib stream, header chunks and pixels the whole-image encoder + /// gives, at 8 and 16 bits, with a profile, resolution, text and XMP. + #[test] + fn streamed_rows_match_the_whole_image_encoder() { + for (layout, sample, ch, bytes) in [(ChannelLayout::Rgba, SampleType::U8, 4usize, 1usize), (ChannelLayout::Rgb, SampleType::U16, 3, 2)] { + let (w, h) = (1201u32, 1013u32); + let meta = Metadata { + dpi: Some((300.0, 300.0)), + text: vec![("Title".into(), "Tent".into())], + xmp: Some("".into()), + ..Default::default() + }; + let img = Image::from_raw(w, h, layout, sample, noise(w as usize * h as usize * ch * bytes, 7)) + .unwrap() + .with_icc(Some((0..600u32).map(|i| (i % 251) as u8).collect())) + .with_meta(meta); + for level in [PngCompression::Fast, PngCompression::Default] { + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let whole = crate::encode(&img, Format::Png, &opts).unwrap(); + let (head, zlib, _) = split(&whole); + for heights in [&[1u32, 77, 300][..], &[256, 256, 256], &[1013]] { + let png = streamed(&img, heights, &opts); + let (h2, z2, _) = split(&png); + assert!(h2 == head, "{layout:?} {level:?} {heights:?}: header chunks differ"); + assert!(z2 == zlib, "{layout:?} {level:?} {heights:?}: zlib stream differs"); + assert_eq!(decode(&png, &Limits::default()).unwrap().data(), img.data()); + } + } + } + } + + /// Small images, no compression and bands in another sample type (converted by the plan). + #[test] + fn small_uncompressed_and_converted_bands_round_trip() { + let img = Image::from_raw(3, 5, ChannelLayout::GrayA, SampleType::U8, noise(30, 3)).unwrap(); + for level in [PngCompression::None, PngCompression::Best] { + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let png = streamed(&img, &[2, 1], &opts); + assert_eq!(decode(&png, &Limits::default()).unwrap().data(), img.data(), "{level:?}"); + } + // Float bands are written with 16 bits, as `encode` writes them. + let f = img.convert(ChannelLayout::GrayA, SampleType::F32); + let png = streamed(&f, &[4], &EncodeOptions::default()); + let back = decode(&png, &Limits::default()).unwrap(); + assert_eq!(back.sample_type(), SampleType::U16); + assert_eq!(back.convert(ChannelLayout::GrayA, SampleType::U8).data(), img.data()); + } + + /// A zlib stream longer than one chunk may hold is split over IDAT chunks that read back. + #[test] + fn long_streams_split_over_several_idat_chunks() { + let img = Image::from_raw(64, 40, ChannelLayout::Rgb, SampleType::U8, noise(64 * 40 * 3, 9)).unwrap(); + let whole = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let (_, zlib, _) = split(&whole); + let mut out = Vec::new(); + let mut w = start(&mut out, (64, 40), (ChannelLayout::Rgb, SampleType::U8), None, &Metadata::default(), &EncodeOptions::default()).unwrap(); + write_idat(&mut w, &zlib, 100).unwrap(); + w.finish().unwrap(); + let (_, joined, count) = split(&out); + assert_eq!(count, zlib.len().div_ceil(100)); + assert!(joined == zlib); + assert_eq!(decode(&out, &Limits::default()).unwrap().data(), img.data()); + } + + /// Bands that don't fit the image, missing rows and interlacing are errors, not panics. + #[test] + fn bands_that_dont_fit_are_errors() { + let band = Image::from_raw(4, 2, ChannelLayout::Rgb, SampleType::U8, vec![0; 24]).unwrap(); + let opts = EncodeOptions::default(); + assert!(PngRows::new(Vec::new(), &band, 0, &opts).is_err()); + assert!(PngRows::new(Vec::new(), &band, 3, &EncodeOptions { png_interlaced: true, ..Default::default() }).is_err()); + let mut rows = PngRows::new(Vec::new(), &band, 3, &opts).unwrap(); + rows.push(&band).unwrap(); + // Two more rows than the image holds. + assert!(rows.push(&band).is_err()); + let narrow = Image::from_raw(3, 1, ChannelLayout::Rgb, SampleType::U8, vec![0; 9]).unwrap(); + assert!(rows.push(&narrow).is_err()); + // Two of three rows. + assert!(rows.finish().is_err()); + } +} + +#[cfg(test)] +mod rows_tests { + use super::*; + + /// The pixels `decode` read before it was built on [`PngDecodeRows`]: the whole image in one + /// `next_frame` call, then the byte order of 16-bit samples. + fn one_call(bytes: &[u8]) -> Result<(u32, u32, Vec), CodecError> { + let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits { bytes: Limits::default().alloc_usize() }); + decoder.set_transformations(png::Transformations::EXPAND); + decoder.set_ignore_text_chunk(true); + let mut reader = decoder.read_info().map_err(map_png_err)?; + let mut buf = vec![0u8; reader.output_buffer_size().ok_or_else(|| err("image too large"))?]; + let out = reader.next_frame(&mut buf).map_err(map_png_err)?; + buf.truncate(out.buffer_size()); + if reader.output_color_type().1 == png::BitDepth::Sixteen { + to_native_endian(&mut buf); + } + Ok((out.width, out.height, buf)) + } + + fn image(w: u32, h: u32, layout: ChannelLayout, sample: SampleType) -> Image { + let len = w as usize * h as usize * layout.channels() * sample.bytes(); + let data: Vec = (0..len).map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8).collect(); + Image::from_raw(w, h, layout, sample, data).unwrap() + } + + #[test] + fn rows_give_the_pixels_of_a_single_call() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for sample in [SampleType::U8, SampleType::U16] { + for interlaced in [false, true] { + let img = image(33, 21, layout, sample); + let opts = EncodeOptions { png_interlaced: interlaced, ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + let (w, h, pixels) = one_call(&bytes).unwrap(); + for per_call in [1usize, 7, usize::MAX] { + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut left = per_call; + let got = loop { + left -= 1; + let mut more = || { + left = left.saturating_sub(1); + left > 0 + }; + if let Some(img) = rows.step(&mut more).unwrap() { + break img; + } + left = per_call; + }; + assert_eq!((got.width(), got.height()), (w, h)); + assert_eq!(got.data(), pixels.as_slice(), "{layout:?} {sample:?} interlaced={interlaced} per_call={per_call}"); + assert_eq!(got.data(), img.data()); + } + } + } + } + } + + /// A metadata yield after the last row must not finish the pixel reader again on every call. + #[test] + fn metadata_yields_do_not_finalize_the_pixels_again() { + let text = "A".repeat(20_000); + let img = image(8, 6, ChannelLayout::Rgb, SampleType::U8).with_meta(Metadata { text: vec![("Comment".into(), text.clone())], ..Default::default() }); + let bytes = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + let got = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 10_000, "the decoder never finished"); + }; + assert!(calls > 1, "the metadata took more than the pixel step"); + assert_eq!(rows.pixel_finalizations, 1, "a metadata yield must not finalize the pixels again"); + assert_eq!(got.meta.text, vec![("Comment".into(), text)]); + } + /// A compressed iTXt chunk whose text inflates to `len` bytes of invalid UTF-8. + fn bad_itxt(len: usize) -> Vec { + use std::io::Write; + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); + z.write_all(&vec![0xff; len]).unwrap(); + let mut data = b"Comment\0\x01\x00\0\0".to_vec(); + data.extend_from_slice(&z.finish().unwrap()); + let mut out = (data.len() as u32).to_be_bytes().to_vec(); + out.extend_from_slice(b"iTXt"); + out.extend_from_slice(&data); + let mut crc = flate2::Crc::new(); + crc.update(b"iTXt"); + crc.update(&data); + out.extend_from_slice(&crc.sum().to_be_bytes()); + out + } + + /// A small PNG with `chunks` placed before its `IEND`. + fn png_with(chunks: &[u8]) -> Vec { + let png = crate::encode(&image(4, 4, ChannelLayout::Rgb, SampleType::U8), Format::Png, &EncodeOptions::default()).unwrap(); + let (head, iend) = png.split_at(png.len() - 12); + [head, chunks, iend].concat() + } + + #[test] + fn dropped_text_still_counts_against_the_budget() { + let limits = Limits { max_alloc: 1 << 20, ..Limits::default() }; + let decode = |bytes: &[u8]| PngDecodeRows::new(bytes, &limits)?.step(&mut || true)?.ok_or_else(|| err("unfinished")); + let one = decode(&png_with(&bad_itxt(100_000))).unwrap(); + assert!(one.meta.text.is_empty(), "invalid UTF-8 is dropped"); + // Twenty chunks inflate 2 MB between them: each is dropped, but the work is charged. + assert!(matches!(decode(&png_with(&bad_itxt(100_000).repeat(20))), Err(CodecError::LimitExceeded(_)))); + } +} diff --git a/crates/image-codecs/src/codecs/pnm.rs b/crates/image-codecs/src/codecs/pnm.rs new file mode 100644 index 000000000..c16cccfd0 --- /dev/null +++ b/crates/image-codecs/src/codecs/pnm.rs @@ -0,0 +1,355 @@ +//! Netpbm family, implemented here: P1–P6 (PBM/PGM/PPM, ASCII and binary), +//! P7 (PAM, including the common `CMYK`/`CMYK_ALPHA` tuple types) and PFM +//! (`PF` colour / `Pf` gray float). +//! +//! Writing picks the subtype from the image: float → PFM (gray/RGB), gray +//! → P5, RGB → P6, anything with alpha or CMYK → P7. + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::{EncodeOptions, Limits}; + +const F: Format = Format::Pnm; + +fn err(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +struct Reader<'a> { + b: &'a [u8], + pos: usize, +} + +impl<'a> Reader<'a> { + fn skip_ws_and_comments(&mut self) { + while self.pos < self.b.len() { + let c = self.b[self.pos]; + if c == b'#' { + while self.pos < self.b.len() && self.b[self.pos] != b'\n' && self.b[self.pos] != b'\r' { + self.pos += 1; + } + } else if c.is_ascii_whitespace() { + self.pos += 1; + } else { + break; + } + } + } + + fn token(&mut self) -> Result<&'a [u8], CodecError> { + self.skip_ws_and_comments(); + let start = self.pos; + while self.pos < self.b.len() && !self.b[self.pos].is_ascii_whitespace() && self.b[self.pos] != b'#' { + self.pos += 1; + } + if start == self.pos { + return Err(err("unexpected end of header")); + } + Ok(&self.b[start..self.pos]) + } + + fn uint(&mut self) -> Result { + let t = self.token()?; + std::str::from_utf8(t).ok().and_then(|s| s.parse::().ok()).ok_or_else(|| err("invalid integer in header")) + } + + /// Consume the single whitespace byte that separates header and raster. + fn single_ws(&mut self) -> Result<(), CodecError> { + match self.b.get(self.pos) { + Some(c) if c.is_ascii_whitespace() => { + self.pos += 1; + Ok(()) + } + _ => Err(err("missing whitespace after header")), + } + } + + /// PAM ends its header with an LF-terminated line, unlike P4–P6's single separator. + fn pam_endhdr(&mut self) -> Result<(), CodecError> { + while let Some(&c) = self.b.get(self.pos) { + self.pos += 1; + if c == b'\n' { + // Stop at the first LF: whitespace and '#' after it are raster bytes. + return Ok(()); + } + if !c.is_ascii_whitespace() { + return Err(err("unexpected data after PAM ENDHDR")); + } + } + Err(err("unterminated PAM ENDHDR line")) + } + + fn rest(&self) -> &'a [u8] { + &self.b[self.pos.min(self.b.len())..] + } +} + +fn sample_for_maxval(maxval: u32) -> Result { + match maxval { + 1..=255 => Ok(SampleType::U8), + 256..=65535 => Ok(SampleType::U16), + _ => Err(err(format!("invalid maxval {maxval}"))), + } +} + +/// The decoded raster, built straight in the output representation. It is the only buffer sized +/// by the header, so `Limits::check` on the output budgets all of it: no wider scratch buffer +/// (such as one `u32` per sample) sits beside it (#2938). +struct Samples { + maxval: u32, + sample: SampleType, + bytes: Vec, +} + +impl Samples { + /// Reserve room for `cap` samples (callers bound `cap` by the file length where the header + /// alone could overstate it). + fn with_capacity(maxval: u32, cap: usize) -> Result { + let sample = sample_for_maxval(maxval)?; + let len = cap.checked_mul(sample.bytes()).ok_or_else(|| CodecError::LimitExceeded(format!("{cap} samples do not fit in memory")))?; + let mut bytes = Vec::new(); + bytes.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} bytes for the image")))?; + Ok(Samples { maxval, sample, bytes }) + } + + /// Append one raw value in `[0, maxval]`, rescaled to the full range of the output sample. + fn push(&mut self, v: u32) -> Result<(), CodecError> { + let m = self.maxval; + if v > m { + return Err(err("sample exceeds maxval")); + } + match (self.sample, m) { + (SampleType::U8, 255) => self.bytes.push(v as u8), + (SampleType::U8, m) => self.bytes.push(((v * 255 + m / 2) / m) as u8), + (_, 65535) => self.bytes.extend_from_slice(&(v as u16).to_ne_bytes()), + (_, m) => self.bytes.extend_from_slice(&(((v as u64 * 65535 + m as u64 / 2) / m as u64) as u16).to_ne_bytes()), + } + Ok(()) + } + + fn into_image(self, w: u32, h: u32, layout: ChannelLayout) -> Result { + Image::from_raw(w, h, layout, self.sample, self.bytes) + } +} + +/// `w × h × channels` samples. `Limits::none()` lets any header through, so the product is +/// checked here rather than trusted to the limits. +fn sample_count(w: u32, h: u32, channels: usize) -> Result { + (w as usize) + .checked_mul(h as usize) + .and_then(|p| p.checked_mul(channels)) + .ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{h}x{channels} samples do not fit in memory"))) +} + +fn read_binary(data: &[u8], n: usize, maxval: u32) -> Result { + let mut out = Samples::with_capacity(maxval, n)?; + if maxval < 256 { + let Some(raw) = data.get(..n) else { + return Err(err("truncated raster")); + }; + for &v in raw { + out.push(v as u32)?; + } + } else { + let Some(raw) = n.checked_mul(2).and_then(|len| data.get(..len)) else { + return Err(err("truncated raster")); + }; + for c in raw.as_chunks::<2>().0 { + out.push(u16::from_be_bytes(*c) as u32)?; + } + } + Ok(out) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + if bytes.len() < 2 || bytes[0] != b'P' { + return Err(err("not a Netpbm file")); + } + let kind = bytes[1]; + let mut r = Reader { b: bytes, pos: 2 }; + match kind { + b'1' | b'4' => { + let (w, h) = (r.uint()?, r.uint()?); + limits.check(w, h, ChannelLayout::Gray, SampleType::U8)?; + let n = sample_count(w, h, 1)?; + let mut px; + if kind == b'4' { + r.single_ws()?; + let row = (w as usize).div_ceil(8); + let data = r.rest(); + if row.checked_mul(h as usize).is_none_or(|len| data.len() < len) { + return Err(err("truncated raster")); + } + px = Vec::with_capacity(n); + for y in 0..h as usize { + for x in 0..w as usize { + let bit = (data[y * row + x / 8] >> (7 - x % 8)) & 1; + px.push(if bit == 1 { 0 } else { 255 }); + } + } + } else { + // Each ASCII pixel takes at least a byte, so the file bounds the reservation (#1110). + px = Vec::with_capacity(n.min(r.rest().len())); + while px.len() < n { + r.skip_ws_and_comments(); + match r.b.get(r.pos) { + Some(b'0') => px.push(255), + Some(b'1') => px.push(0), + Some(_) => return Err(err("invalid PBM digit")), + None => return Err(err("truncated raster")), + } + r.pos += 1; + } + } + Image::from_u8(w, h, ChannelLayout::Gray, px) + } + b'2' | b'3' | b'5' | b'6' => { + let (w, h, maxval) = (r.uint()?, r.uint()?, r.uint()?); + let layout = if matches!(kind, b'2' | b'5') { ChannelLayout::Gray } else { ChannelLayout::Rgb }; + limits.check(w, h, layout, sample_for_maxval(maxval)?)?; + let n = sample_count(w, h, layout.channels())?; + let samples = if matches!(kind, b'5' | b'6') { + r.single_ws()?; + read_binary(r.rest(), n, maxval)? + } else { + // Each ASCII sample is a digit and a separator (the last may lack one), so the + // file, not the header, bounds the reservation (#1110). + let mut v = Samples::with_capacity(maxval, n.min(r.rest().len() / 2 + 1))?; + for _ in 0..n { + v.push(r.uint()?)?; + } + v + }; + samples.into_image(w, h, layout) + } + b'7' => decode_pam(&mut r, limits), + b'F' | b'f' => { + let layout = if kind == b'F' { ChannelLayout::Rgb } else { ChannelLayout::Gray }; + let (w, h) = (r.uint()?, r.uint()?); + let scale: f32 = std::str::from_utf8(r.token()?) + .ok() + .and_then(|s| s.parse().ok()) + .filter(|s: &f32| s.is_finite() && *s != 0.0) + .ok_or_else(|| err("invalid PFM scale"))?; + r.single_ws()?; + limits.check(w, h, layout, SampleType::F32)?; + let nc = layout.channels(); + let row = w as usize * nc; + let data = r.rest(); + if sample_count(w, h, nc)?.checked_mul(4).is_none_or(|len| data.len() < len) { + return Err(err("truncated raster")); + } + let le = scale < 0.0; + // Write native-endian bytes straight into the output buffer: a separate `Vec` + // would double the budgeted allocation (#2938). + let row_bytes = row * 4; + let mut out = vec![0u8; row_bytes * h as usize]; + for (y, src) in data.chunks_exact(row_bytes).take(h as usize).enumerate() { + // PFM rows are stored bottom-to-top. + let dst_start = (h as usize - 1 - y) * row_bytes; + let Some(dst) = out.get_mut(dst_start..dst_start + row_bytes) else { + return Err(err("truncated raster")); + }; + for (d, s) in dst.as_chunks_mut::<4>().0.iter_mut().zip(src.as_chunks::<4>().0) { + let v = if le { f32::from_le_bytes(*s) } else { f32::from_be_bytes(*s) }; + *d = v.to_ne_bytes(); + } + } + Image::from_raw(w, h, layout, SampleType::F32, out) + } + _ => Err(err("unknown Netpbm subtype")), + } +} + +fn decode_pam(r: &mut Reader<'_>, limits: &Limits) -> Result { + let (mut w, mut h, mut depth, mut maxval) = (None, None, None, None); + let mut tupltype = String::new(); + loop { + let tok = r.token()?; + match tok { + b"WIDTH" => w = Some(r.uint()?), + b"HEIGHT" => h = Some(r.uint()?), + b"DEPTH" => depth = Some(r.uint()?), + b"MAXVAL" => maxval = Some(r.uint()?), + b"TUPLTYPE" => { + let t = r.token()?; + if !tupltype.is_empty() { + tupltype.push(' '); + } + tupltype.push_str(std::str::from_utf8(t).map_err(|_| err("bad TUPLTYPE"))?); + } + b"ENDHDR" => break, + _ => return Err(err("unknown PAM header field")), + } + } + r.pam_endhdr()?; + let (w, h, depth, maxval) = match (w, h, depth, maxval) { + (Some(a), Some(b), Some(c), Some(d)) => (a, b, c, d), + _ => return Err(err("incomplete PAM header")), + }; + let layout = match (tupltype.as_str(), depth) { + ("BLACKANDWHITE" | "GRAYSCALE", 1) | ("", 1) => ChannelLayout::Gray, + ("BLACKANDWHITE_ALPHA" | "GRAYSCALE_ALPHA", 2) | ("", 2) => ChannelLayout::GrayA, + ("RGB", 3) | ("", 3) => ChannelLayout::Rgb, + ("RGB_ALPHA", 4) | ("", 4) => ChannelLayout::Rgba, + ("CMYK", 4) => ChannelLayout::Cmyk, + ("CMYK_ALPHA", 5) => ChannelLayout::CmykA, + (t, d) => { + return Err(CodecError::unsupported(F, format!("PAM tuple type {t:?} with depth {d}"))); + } + }; + limits.check(w, h, layout, sample_for_maxval(maxval)?)?; + let n = sample_count(w, h, layout.channels())?; + read_binary(r.rest(), n, maxval)?.into_image(w, h, layout) +} + +pub(crate) fn encode(src: &Image, plan: Plan, _opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let layout = img.layout(); + let mut out = Vec::new(); + match img.sample_type() { + SampleType::F32 => { + let tag = match layout { + ChannelLayout::Gray => "Pf", + ChannelLayout::Rgb => "PF", + l => return Err(CodecError::encode(F, format!("PFM cannot store {l:?}"))), + }; + out.extend_from_slice(format!("{tag}\n{w} {h}\n-1.0\n").as_bytes()); + let s = img.to_f32_samples().unwrap_or_default(); + let row = w as usize * layout.channels(); + for y in (0..h as usize).rev() { + for v in &s[y * row..(y + 1) * row] { + out.extend_from_slice(&v.to_le_bytes()); + } + } + } + sample @ (SampleType::U8 | SampleType::U16) => { + let maxval = if sample == SampleType::U8 { 255 } else { 65535 }; + match layout { + ChannelLayout::Gray => out.extend_from_slice(format!("P5\n{w} {h}\n{maxval}\n").as_bytes()), + ChannelLayout::Rgb => out.extend_from_slice(format!("P6\n{w} {h}\n{maxval}\n").as_bytes()), + l => { + let t = match l { + ChannelLayout::GrayA => "GRAYSCALE_ALPHA", + ChannelLayout::Rgba => "RGB_ALPHA", + ChannelLayout::Cmyk => "CMYK", + _ => "CMYK_ALPHA", + }; + out.extend_from_slice(format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes()); + } + } + if sample == SampleType::U8 { + out.extend_from_slice(img.data()); + } else { + for v in img.to_u16_samples().unwrap_or_default() { + out.extend_from_slice(&v.to_be_bytes()); + } + } + } + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + } + Ok(out) +} diff --git a/crates/image-codecs/src/codecs/tga.rs b/crates/image-codecs/src/codecs/tga.rs new file mode 100644 index 000000000..2f382efcf --- /dev/null +++ b/crates/image-codecs/src/codecs/tga.rs @@ -0,0 +1,175 @@ +//! Targa writer. The `image` crate still reads Targa ([`super::via_image`]); writing is ours +//! because its run-length packets ran on from the end of one row into the next. TGA 2.0 says a +//! packet must not cross a scanline, and readers that decode a row at a time (Pillow, texture +//! converters, game engines) misread such files (#6382). +//! +//! Like Photoshop's Targa: RLE (types 10 and 11), rows stored bottom-up (the bottom-left origin +//! every reader assumes, also those that ignore the origin bits), alpha bits in the descriptor, +//! and the TGA 2.0 footer. + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, SampleType}; + +/// Most pixels one packet holds (a 7-bit count, plus one). +const MAX_PACKET: usize = 128; + +/// The TGA 2.0 footer: no extension or developer area, then the signature. +const FOOTER: &[u8; 26] = b"\0\0\0\0\0\0\0\0TRUEVISION-XFILE.\0"; + +/// Encodes an 8-bit Gray, Gray+alpha, RGB or RGBA image as a run-length encoded Targa. +pub(crate) fn encode(img: &Image) -> Result, CodecError> { + let (w, h) = img.dimensions(); + if img.sample_type() != SampleType::U8 { + return Err(CodecError::encode(Format::Tga, format!("{:?} samples (Targa stores 8 bits)", img.sample_type()))); + } + let (bpp, image_type, alpha_bits) = match img.layout() { + ChannelLayout::Gray => (1, 11, 0), + ChannelLayout::GrayA => (2, 11, 8), + ChannelLayout::Rgb => (3, 10, 0), + ChannelLayout::Rgba => (4, 10, 8), + l => return Err(CodecError::encode(Format::Tga, format!("no Targa layout for {l:?}"))), + }; + let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else { + return Err(CodecError::encode(Format::Tga, format!("dimensions exceed 65535x65535: {w}x{h}"))); + }; + let row = w as usize * bpp; + let data = img.data(); + if data.len() != row * h as usize { + return Err(CodecError::InvalidImage("buffer size mismatch".into())); + } + let mut out = Vec::with_capacity(18 + data.len() + data.len() / MAX_PACKET + FOOTER.len()); + out.extend_from_slice(&[0, 0, image_type, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + out.extend_from_slice(&w16.to_le_bytes()); + out.extend_from_slice(&h16.to_le_bytes()); + // Bits per pixel; the descriptor holds the alpha bits, origin bits 0 (bottom-left). + out.extend_from_slice(&[(bpp * 8) as u8, alpha_bits]); + let mut px = vec![0u8; row]; + for y in (0..h as usize).rev() { + px.copy_from_slice(&data[y * row..(y + 1) * row]); + // Targa stores colour as BGR. + if bpp >= 3 { + for p in px.chunks_exact_mut(bpp) { + p.swap(0, 2); + } + } + rle_row(&px, bpp, &mut out); + } + out.extend_from_slice(FOOTER); + Ok(out) +} + +/// One scanline as run-length packets that end with it: a run of two or more equal pixels is a +/// run packet, anything else goes out in raw packets. +fn rle_row(row: &[u8], bpp: usize, out: &mut Vec) { + let px: Vec<&[u8]> = row.chunks_exact(bpp).collect(); + let n = px.len(); + let mut i = 0; + while i < n { + let mut run = 1; + while i + run < n && run < MAX_PACKET && px[i + run] == px[i] { + run += 1; + } + if run >= 2 { + out.push(0x80 | (run - 1) as u8); + out.extend_from_slice(px[i]); + i += run; + continue; + } + // Raw until two equal pixels start a run, the packet is full or the row ends. The first + // pixel always goes in: it differs from the one after it, or it is the row's last. + let start = i; + while i < n && i - start < MAX_PACKET && !(i > start && i + 1 < n && px[i] == px[i + 1]) { + i += 1; + } + out.push((i - start - 1) as u8); + for p in &px[start..i] { + out.extend_from_slice(p); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn run_packets_at_the_128_pixel_boundary() { + for bpp in 1..=4 { + for (pixels, headers) in [(1, vec![0]), (2, vec![0x81]), (127, vec![0xfe]), (128, vec![0xff]), (129, vec![0xff, 0])] { + let pixel: Vec = (1..=bpp as u8).collect(); + let mut encoded = Vec::new(); + rle_row(&pixel.repeat(pixels), bpp, &mut encoded); + let expected: Vec = headers.into_iter().flat_map(|header| std::iter::once(header).chain(pixel.iter().copied())).collect(); + assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel"); + } + } + } + + #[test] + fn raw_packets_at_the_128_pixel_boundary() { + for bpp in 1..=4 { + for pixels in [127usize, 128, 129] { + let row: Vec = (0..pixels).flat_map(|x| vec![x as u8; bpp]).collect(); + let mut encoded = Vec::new(); + rle_row(&row, bpp, &mut encoded); + let mut expected = vec![(pixels.min(128) - 1) as u8]; + expected.extend_from_slice(&row[..pixels.min(128) * bpp]); + if pixels > 128 { + expected.push(0); + expected.extend_from_slice(&row[128 * bpp..]); + } + assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel"); + } + } + } + + #[test] + fn raw_packets_stop_before_a_run_of_complete_pixels() { + // Equal first channels alone do not make an RGBA run; alpha participates too. + let a = [10, 20, 30, 40]; + let b = [10, 20, 30, 41]; + let c = [50, 60, 70, 80]; + let row = [a, b, c, c, a].concat(); + let mut encoded = Vec::new(); + rle_row(&row, 4, &mut encoded); + assert_eq!(encoded, [vec![1], a.to_vec(), b.to_vec(), vec![0x81], c.to_vec(), vec![0], a.to_vec()].concat()); + } + + #[test] + fn rejects_sample_types_and_layouts_targa_cannot_store() { + for sample in [SampleType::U16, SampleType::F16, SampleType::F32] { + let img = Image::new(1, 1, ChannelLayout::Rgb, sample).unwrap(); + assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{sample:?}"); + } + for layout in [ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab, ChannelLayout::LabA] { + let img = Image::new(1, 1, layout, SampleType::U8).unwrap(); + assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{layout:?}"); + } + } + + #[test] + fn dimensions_fit_the_unsigned_16_bit_header() { + for (width, height) in [(65535, 1), (1, 65535)] { + let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap(); + let file = encode(&img).unwrap(); + assert_eq!(&file[12..14], &(width as u16).to_le_bytes()); + assert_eq!(&file[14..16], &(height as u16).to_le_bytes()); + } + for (width, height) in [(65536, 1), (1, 65536)] { + let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap(); + assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{width}x{height}"); + } + } + + #[test] + fn invalid_buffers_are_rejected_before_targa_encoding() { + // Image keeps its buffer private, so malformed buffers must fail at construction. + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let len = 2 * 2 * layout.channels(); + for bytes in [len - 1, len + 1] { + assert!(matches!(Image::from_u8(2, 2, layout, vec![0; bytes]), Err(CodecError::InvalidImage(_))), "{layout:?}: {bytes} bytes"); + } + } + } +} diff --git a/crates/image-codecs/src/codecs/tiff.rs b/crates/image-codecs/src/codecs/tiff.rs new file mode 100644 index 000000000..c74ab4436 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff.rs @@ -0,0 +1,220 @@ +//! TIFF and BigTIFF: 1/2/4/8/16/32/64-bit integer and 16/32/64-bit float samples, gray / RGB / +//! CMYK / palette with optional alpha (unassociated, or associated and un-premultiplied) and +//! extra samples, WhiteIsZero, strips or tiles, planar configuration 1 or 2, ICC (34675), XMP +//! (700), DPI and a few ASCII text tags. +//! +//! **Structure.** [`super::tiff_ifd`] walks the directories with bounds checks (classic and +//! BigTIFF, either byte order, the whole IFD chain and its SubIFDs, cycles and hostile counts +//! defeated) and lists them as pages. Decoding opens the first full-resolution page, like +//! Photoshop (reduced-resolution copies and masks are skipped); further pages are reported as a +//! [`DecodeWarning::MorePages`], and [`decode_page`] opens any directory. +//! +//! **Pixels.** The `tiff` crate decompresses one strip or tile at a time (none, LZW, Deflate, +//! PackBits, with the horizontal and floating-point predictors). The decoder sees the chosen +//! directory through a byte view whose header points at it ([`Patched`]), so SubIFDs decode +//! like IFD0. Decoding is banded: the output image is allocated once, at its final size, and +//! each band of rows (one strip, or one row of tiles, across every plane) is decoded and +//! converted straight into it, in parallel on native targets. A strip that already has the +//! output's layout is decoded in place; anything else goes through one strip- or tile-sized +//! scratch buffer per worker, so no second full-size buffer exists. Image data cut off by the +//! end of the file leaves the missing chunks empty and adds a [`DecodeWarning::Truncated`]. +//! [`TiffBands`] runs the same decode a group of bands at a time and hands each group to its +//! caller, so a document can be filled without the image ever existing whole (#8169). +//! +//! Photoshop's private tags are carried as opaque bytes in [`Metadata`]: 34377 (image +//! resources) and 37724 (`ImageSourceData`, the layers of a layered TIFF). They are read with +//! a bounds-checked walk of the first directory ([`photoshop_tags`]) rather than through the +//! decoder, which would materialize a multi-megabyte layer block as one `Value` per byte, and +//! written back as BYTE / UNDEFINED tags. The encoder writes files in the native byte order +//! (see [`writes_little_endian`]), which the layer data must match. + +use std::borrow::Cow; +use std::io::{self, Read, Seek, SeekFrom}; + +use half::f16; +use tiff::decoder::{ChunkType, Decoder}; +use tiff::encoder::colortype::{self, ColorType as TiffColorType}; +use tiff::encoder::{Rational, TiffEncoder, TiffKind, TiffKindBig, TiffKindStandard, TiffValue}; +use tiff::tags::{PhotometricInterpretation, ResolutionUnit, SampleFormat, Tag, Type}; + +use super::tiff_exif::{self, ExifDirs}; +use super::tiff_ifd::{self as ifd, File, Ifd}; +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, Limits, TiffCompression}; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +fn map_err(e: tiff::TiffError) -> CodecError { + match e { + tiff::TiffError::LimitsExceeded => CodecError::LimitExceeded("TIFF decoder limit".into()), + tiff::TiffError::UnsupportedError(u) => CodecError::unsupported(F, u.to_string()), + e => err(e), + } +} + +/// TIFF/EP `PhotometricInterpretation` for colour filter array (sensor) data. +const PHOTOMETRIC_CFA: u32 = 32803; +/// DNG `PhotometricInterpretation` for demosaiced but undeveloped sensor data. +const PHOTOMETRIC_LINEAR_RAW: u32 = 34892; +const TAG_PHOTOMETRIC: u16 = 262; +const TAG_SUB_IFDS: u16 = 330; +const TAG_DNG_VERSION: u16 = 50706; + +/// Recognizes TIFF-structured camera raw files (TIFF/EP, DNG, CR2 and the +/// TIFF-based NEF/ARW/PEF… layouts): a CR2 signature, a DNGVersion tag, or +/// a CFA / LinearRaw image in IFD0, its SubIFDs or the next few IFDs. Their +/// first IFD is often a reduced preview, so this runs before decoding. +/// Reads only bounds-checked header bytes; anything malformed is "not raw". +fn camera_raw(b: &[u8]) -> bool { + let le = match b.get(0..4) { + Some(b"II*\0") => true, + Some(b"MM\0*") => false, + _ => return false, // BigTIFF and others: no raw layouts to recognize + }; + let u16_at = |o: usize| { + let s = b.get(o..o.checked_add(2)?)?; + Some(if le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + }; + let u32_at = |o: usize| { + let s: [u8; 4] = b.get(o..o.checked_add(4)?)?.try_into().ok()?; + Some(if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) }) + }; + // CR2: "CR" and major version 2 right after the TIFF header. + if b.get(8..11) == Some(b"CR\x02") { + return true; + } + // Entry value (count 1) as SHORT or LONG; anything else is ignored. + let value = |e: usize| match u16_at(e.saturating_add(2))? { + 3 => u16_at(e.saturating_add(8)).map(u32::from), + 4 | 13 => u32_at(e.saturating_add(8)), + _ => None, + }; + let mut queue: Vec = u32_at(4).into_iter().collect(); + let mut visited = 0; + while let Some(ifd) = queue.pop() { + // A handful of IFDs is enough for every real layout; also stops loops. + visited += 1; + if visited > 16 { + break; + } + let ifd = ifd as usize; + let Some(n) = u16_at(ifd) else { continue }; + for i in 0..usize::from(n) { + let e = ifd.saturating_add(2 + 12 * i); + let (Some(tag), Some(count)) = (u16_at(e), u32_at(e.saturating_add(4))) else { + break; + }; + match tag { + TAG_DNG_VERSION => return true, + TAG_PHOTOMETRIC if matches!(value(e), Some(PHOTOMETRIC_CFA | PHOTOMETRIC_LINEAR_RAW)) => { + return true; + } + TAG_SUB_IFDS if count == 1 => queue.extend(value(e)), + TAG_SUB_IFDS => { + // More than one offset: they are stored at the entry's offset. + let at = u32_at(e.saturating_add(8)).unwrap_or(0) as usize; + for k in 0..count.min(8) as usize { + queue.extend(u32_at(at.saturating_add(4 * k))); + } + } + _ => {} + } + } + if let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) { + queue.push(next); + } + } + false +} + +macro_rules! custom_colortype { + ($name:ident, $inner:ty, $photo:expr, $bits:expr, $fmt:expr, int) => { + struct $name; + impl TiffColorType for $name { + type Inner = $inner; + const TIFF_VALUE: PhotometricInterpretation = $photo; + const BITS_PER_SAMPLE: &'static [u16] = $bits; + const SAMPLE_FORMAT: &'static [SampleFormat] = $fmt; + fn horizontal_predict(row: &[Self::Inner], result: &mut Vec) { + let n = Self::SAMPLE_FORMAT.len(); + let n = n.min(row.len()); + result.extend_from_slice(&row[..n]); + result.extend(row.iter().zip(&row[n..]).map(|(p, c)| c.wrapping_sub(*p))); + } + } + }; + ($name:ident, $inner:ty, $photo:expr, $bits:expr, $fmt:expr, float) => { + struct $name; + impl TiffColorType for $name { + type Inner = $inner; + const TIFF_VALUE: PhotometricInterpretation = $photo; + const BITS_PER_SAMPLE: &'static [u16] = $bits; + const SAMPLE_FORMAT: &'static [SampleFormat] = $fmt; + fn horizontal_predict(row: &[Self::Inner], result: &mut Vec) { + result.extend_from_slice(row); + } + } + }; +} + +use PhotometricInterpretation as PI; + +mod bands; +mod decode; +mod encode; +mod tags; +mod ycbcr; + +pub use decode::TiffBands; +pub(crate) use decode::{decode, decode_page, has_unretained_samples, orientation}; +pub(crate) use encode::encode; +#[cfg(test)] +use encode::{Extras, try_with_capacity}; +use tags::F; +pub use tags::{PhotoshopTags, photoshop_tags, writes_little_endian}; +custom_colortype!(GrayA8, u8, PI::BlackIsZero, &[8, 8], &[SampleFormat::Uint; 2], int); +custom_colortype!(GrayA16, u16, PI::BlackIsZero, &[16, 16], &[SampleFormat::Uint; 2], int); +custom_colortype!(GrayA32F, f32, PI::BlackIsZero, &[32, 32], &[SampleFormat::IEEEFP; 2], float); +custom_colortype!(CmykA16, u16, PI::CMYK, &[16; 5], &[SampleFormat::Uint; 5], int); +custom_colortype!(CmykA32F, f32, PI::CMYK, &[32; 5], &[SampleFormat::IEEEFP; 5], float); +// CIELab as Photoshop writes it: a* and b* signed, the sample format left unsigned. +custom_colortype!(Lab8, u8, PI::CIELab, &[8; 3], &[SampleFormat::Uint; 3], int); +custom_colortype!(Lab16, u16, PI::CIELab, &[16; 3], &[SampleFormat::Uint; 3], int); +custom_colortype!(LabA8, u8, PI::CIELab, &[8; 4], &[SampleFormat::Uint; 4], int); +custom_colortype!(LabA16, u16, PI::CIELab, &[16; 4], &[SampleFormat::Uint; 4], int); + +#[cfg(test)] +mod bits_len { + use super::Extras; + use tiff::encoder::colortype::ColorType as TiffColorType; + + #[test] + fn const_generic_sample_tables_have_one_entry_per_sample() { + // A repeat expression in an associated const (`&[BITS; N]`) has, on some compilers, + // promoted to a slice shorter than `N`. The encoder sizes every strip from that length. + assert_eq!( as TiffColorType>::BITS_PER_SAMPLE.len(), 4, "{:?}", as TiffColorType>::BITS_PER_SAMPLE); + assert_eq!( as TiffColorType>::BITS_PER_SAMPLE.len(), 5); + assert_eq!( as TiffColorType>::BITS_PER_SAMPLE.len(), 19); + assert_eq!( as TiffColorType>::SAMPLE_FORMAT.len(), 6); + } +} + +#[cfg(test)] +mod extra_sample_buffers { + use super::try_with_capacity; + + /// The extra-sample writer's buffers are allocated fallibly: a size no allocation can serve + /// is an out-of-memory error the save reports, not an abort. + #[test] + fn an_impossible_buffer_is_an_error() { + let err = try_with_capacity::(usize::MAX).unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::OutOfMemory); + assert!(try_with_capacity::(usize::MAX / 2).is_err()); + assert!(try_with_capacity::(64).is_ok_and(|v| v.is_empty() && v.capacity() >= 64)); + } +} diff --git a/crates/image-codecs/src/codecs/tiff/bands.rs b/crates/image-codecs/src/codecs/tiff/bands.rs new file mode 100644 index 000000000..093f90efd --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/bands.rs @@ -0,0 +1,509 @@ +use super::decode::{Layout, Patched, Prepared, Src, is_eof}; +use super::{CodecError, Decoder, SampleType, err, f16, map_err}; + +/// The a* and b* samples (1 and 2 of every pixel) of a Lab image between the file's encoding and +/// PhotoCraft's (8 bits: `a + 128`; 16 bits: `(a + 128) · 257`). In the file, CIELab holds them +/// signed (8 bits: `a`; 16 bits: `a · 256`) and ICCLab offset (8 bits: `a + 128`; 16 bits: +/// `(a + 128) · 256`). `to_file` picks the direction; samples are native-endian. +pub(super) fn convert_ab(data: &mut [u8], channels: usize, sample: SampleType, signed: bool, to_file: bool) { + match sample { + SampleType::U8 if signed => data.chunks_exact_mut(channels).for_each(|px| { + px[1] ^= 0x80; + px[2] ^= 0x80; + }), + SampleType::U16 => data.chunks_exact_mut(channels * 2).for_each(|px| { + for c in [1, 2] { + let v = u16::from_ne_bytes([px[2 * c], px[2 * c + 1]]); + // The file's value as `a · 256` (signed) and back. + let file_a256 = |v: u16| if signed { f64::from(v as i16) } else { f64::from(v) - 32768.0 }; + let out = if to_file { + let a256 = (f64::from(v) - 32896.0) * 256.0 / 257.0; + let a256 = a256.round().clamp(-32768.0, 32767.0); + if signed { (a256 as i16) as u16 } else { (a256 + 32768.0) as u16 } + } else { + (file_a256(v) * 257.0 / 256.0 + 32896.0).round().clamp(0.0, 65535.0) as u16 + }; + px[2 * c..2 * c + 2].copy_from_slice(&out.to_ne_bytes()); + } + }), + _ => {} + } +} + +/// File sample indices that are not a colour channel and not the one alpha sample, capped at +/// [`crate::MAX_EXTRA_SAMPLES`], and how many past the cap were left out. +pub(super) fn extra_sample_indices(spp: usize, color: usize, alpha_at: Option, has_alpha: bool) -> (Vec, usize) { + let mut used = vec![false; spp]; + for i in 0..color.min(spp) { + if let Some(slot) = used.get_mut(i) { + *slot = true; + } + } + if has_alpha { + let a = alpha_at.map(|k| color.saturating_add(k)).unwrap_or(color); + if let Some(slot) = used.get_mut(a) { + *slot = true; + } + } + let mut all: Vec = used.into_iter().enumerate().filter(|(_, on)| !*on).map(|(i, _)| i).collect(); + let over = all.len().saturating_sub(crate::MAX_EXTRA_SAMPLES); + all.truncate(crate::MAX_EXTRA_SAMPLES); + (all, over) +} + +/// Interleaved extra samples (`extra_at.len()` per pixel) → one plane per sample. Each plane is +/// allocated fallibly (the caller has checked their total against the limits). +pub(super) fn split_extra_samples( + buf: &[u8], + w: usize, + h: usize, + at: &[usize], + sample: SampleType, + mut kind_of: impl FnMut(usize) -> u16, +) -> Result, CodecError> { + let b = sample.bytes(); + let n = w.saturating_mul(h); + let spp = at.len(); + let mut out = Vec::with_capacity(spp); + for (ei, &si) in at.iter().enumerate() { + let mut data = zeroed(n.saturating_mul(b))?; + for i in 0..n { + let src = (i.saturating_mul(spp).saturating_add(ei)).saturating_mul(b); + let dst = i.saturating_mul(b); + if let (Some(s), Some(d)) = (buf.get(src..src.saturating_add(b)), data.get_mut(dst..dst.saturating_add(b))) { + d.copy_from_slice(s); + } + } + out.push(crate::ExtraSample { kind: kind_of(si), data }); + } + Ok(out) +} + +/// A zero-filled buffer, allocated fallibly. On native targets the zeroing (and the first +/// touch of every page, which dominates on a fresh multi-hundred-megabyte buffer) is spread over +/// all cores. +pub(super) fn zeroed(len: usize) -> Result, CodecError> { + let mut v = Vec::new(); + v.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} bytes for the image")))?; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + // The capacity is already there: this only writes the zeros. + v.par_extend(rayon::iter::repeat_n(0u8, len)); + } + #[cfg(target_arch = "wasm32")] + v.resize(len, 0); + Ok(v) +} + +/// `v` refilled with `len` zero bytes, reusing its allocation when it is large enough. +pub(super) fn refill_zeroed(mut v: Vec, len: usize) -> Result, CodecError> { + if v.capacity() < len { + drop(v); + return zeroed(len); + } + v.clear(); + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + v.par_extend(rayon::iter::repeat_n(0u8, len)); + } + #[cfg(target_arch = "wasm32")] + v.resize(len, 0); + Ok(v) +} + +/// Chunks decoded and chunks whose data ended early. +#[derive(Default, Clone, Copy)] +pub(super) struct Tally { + pub(super) decoded: usize, + pub(super) missing: usize, +} + +impl std::ops::Add for Tally { + type Output = Tally; + fn add(self, o: Tally) -> Tally { + Tally { decoded: self.decoded + o.decoded, missing: self.missing + o.missing } + } +} + +/// Decodes the bands from `first` on into `data` (`band_bytes` each), in parallel on native +/// targets (one decoder and scratch buffer per worker). `extra` receives samples that are not +/// part of the colour image, `extra_band` bytes per band, when it is non-empty. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn decode_bands( + p: &Prepared, + b: &[u8], + data: &mut [u8], + band_bytes: usize, + first: usize, + extra: &mut [u8], + extra_band: usize, +) -> Result { + use rayon::prelude::*; + if extra.is_empty() { + return data + .par_chunks_mut(band_bytes.max(1)) + .enumerate() + .map_init( + || p.open(b).map(|d| (d, Vec::new())).map_err(|e| e.to_string()), + |worker, (i, band)| { + let (dec, scratch) = worker.as_mut().map_err(|e| err(e.clone()))?; + decode_band(&p.lay, dec, scratch, first + i, band, None) + }, + ) + .try_reduce(Tally::default, |a, b| Ok(a + b)); + } + data.par_chunks_mut(band_bytes.max(1)) + .zip(extra.par_chunks_mut(extra_band.max(1))) + .enumerate() + .map_init( + || p.open(b).map(|d| (d, Vec::new())).map_err(|e| e.to_string()), + |worker, (i, (band, extra_band))| { + let (dec, scratch) = worker.as_mut().map_err(|e| err(e.clone()))?; + decode_band(&p.lay, dec, scratch, first + i, band, Some(extra_band)) + }, + ) + .try_reduce(Tally::default, |a, b| Ok(a + b)) +} + +/// Decodes the bands from `first` on into `data` (`band_bytes` each), one after the other. +#[cfg(target_arch = "wasm32")] +pub(super) fn decode_bands( + p: &Prepared, + b: &[u8], + data: &mut [u8], + band_bytes: usize, + first: usize, + extra: &mut [u8], + extra_band: usize, +) -> Result { + let mut dec = p.open(b)?; + let mut scratch = Vec::new(); + let mut tally = Tally::default(); + if extra.is_empty() { + for (i, band) in data.chunks_mut(band_bytes.max(1)).enumerate() { + tally = tally + decode_band(&p.lay, &mut dec, &mut scratch, first + i, band, None)?; + } + return Ok(tally); + } + for (i, (band, extra_band)) in data.chunks_mut(band_bytes.max(1)).zip(extra.chunks_mut(extra_band.max(1))).enumerate() { + tally = tally + decode_band(&p.lay, &mut dec, &mut scratch, first + i, band, Some(extra_band))?; + } + Ok(tally) +} + +/// Decodes band `b` (one strip, or one row of tiles, in every plane) into its output rows. +fn decode_band( + lay: &Layout, + dec: &mut Decoder>, + scratch: &mut Vec, + b: usize, + band: &mut [u8], + mut extra: Option<&mut [u8]>, +) -> Result { + let out_row = lay.out_row(); + let rows = band.len() / out_row.max(1); + let px = lay.out_channels() * lay.out.bytes(); + let mut tally = Tally::default(); + for plane in 0..lay.planes { + let file_plane = lay.source_sample(plane); + for col in 0..lay.across { + let chunk = file_plane + .checked_mul(lay.chunks_per_plane) + .and_then(|c| c.checked_add(b.checked_mul(lay.across)?)) + .and_then(|c| c.checked_add(col)) + .and_then(|c| u32::try_from(c).ok()) + .ok_or_else(|| err("chunk index out of range"))?; + let pref = dec.image_chunk_buffer_layout(chunk).map_err(map_err)?; + let stride = pref.row_stride.map(|s| s.get()).ok_or_else(|| err("empty chunk row"))?; + // In place: the strip's decoded bytes are exactly its output rows. + if lay.direct() && pref.len == band.len() && stride == out_row { + match dec.read_chunk_bytes(chunk, band) { + Ok(()) => tally.decoded += 1, + // Rows read before the end would survive: a missing strip is zeros. + Err(e) if is_eof(&e) => { + band.fill(0); + tally.missing += 1; + } + Err(e) => return Err(map_err(e)), + } + continue; + } + if pref.len > lay.scratch_cap { + return Err(CodecError::LimitExceeded(format!("a {} byte strip or tile", pref.len))); + } + if scratch.len() < pref.len { + scratch + .try_reserve_exact(pref.len - scratch.len()) + .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {} bytes for a strip or tile", pref.len)))?; + scratch.resize(pref.len, 0); + } + let buf = scratch.get_mut(..pref.len).ok_or_else(|| err("scratch buffer"))?; + match dec.read_chunk_bytes(chunk, buf) { + Ok(()) => tally.decoded += 1, + Err(e) if is_eof(&e) => { + tally.missing += 1; + continue; + } + Err(e) => return Err(map_err(e)), + } + let x0 = col * lay.cw; + let n = lay.cw.min(lay.w.saturating_sub(x0)); + for (ri, (src_row, dst_row)) in buf.chunks_exact(stride).zip(band.chunks_exact_mut(out_row)).take(rows).enumerate() { + let Some(dst) = dst_row.get_mut(x0 * px..(x0 + n) * px) else { continue }; + convert_row(lay, src_row, dst, n, plane); + if !lay.planar && plane == 0 && !lay.extra_at.is_empty() { + let erow = lay.extra_row(); + if let Some(extra) = extra.as_deref_mut() + && let Some(er) = extra.get_mut(ri.saturating_mul(erow)..ri.saturating_mul(erow).saturating_add(erow)) + { + deposit_extras(lay, src_row, er, n, x0); + } + } + } + } + } + if lay.planar && !lay.extra_at.is_empty() { + read_planar_extras(lay, dec, scratch, b, rows, extra, &mut tally)?; + } + finish_band(lay, band); + Ok(tally) +} + +/// Copies the non-colour samples of a chunky row into the extra-sample band. +fn deposit_extras(lay: &Layout, src: &[u8], dst: &mut [u8], n: usize, x0: usize) { + let ech = lay.extra_at.len(); + if ech == 0 { + return; + } + let px = ech * lay.out.bytes(); + let Some(dst) = dst.get_mut(x0.saturating_mul(px)..(x0.saturating_add(n)).saturating_mul(px)) else { return }; + for (ei, &si) in lay.extra_at.iter().enumerate() { + transfer(lay.src, src, lay.chunk_spp, si, dst, lay.out, ech, ei, n); + } +} + +/// Planar files store each extra sample as its own plane. Read those planes into `extra`. +fn read_planar_extras( + lay: &Layout, + dec: &mut Decoder>, + scratch: &mut Vec, + b: usize, + rows: usize, + extra: Option<&mut [u8]>, + tally: &mut Tally, +) -> Result<(), CodecError> { + let Some(extra) = extra else { return Ok(()) }; + let ech = lay.extra_at.len(); + let erow = lay.extra_row(); + if ech == 0 || erow == 0 { + return Ok(()); + } + for (ei, &file_plane) in lay.extra_at.iter().enumerate() { + for col in 0..lay.across { + let chunk = file_plane + .checked_mul(lay.chunks_per_plane) + .and_then(|c| c.checked_add(b.checked_mul(lay.across)?)) + .and_then(|c| c.checked_add(col)) + .and_then(|c| u32::try_from(c).ok()) + .ok_or_else(|| err("chunk index out of range"))?; + let pref = dec.image_chunk_buffer_layout(chunk).map_err(map_err)?; + let stride = pref.row_stride.map(|s| s.get()).ok_or_else(|| err("empty chunk row"))?; + if pref.len > lay.scratch_cap { + return Err(CodecError::LimitExceeded(format!("a {} byte strip or tile", pref.len))); + } + if scratch.len() < pref.len { + scratch + .try_reserve_exact(pref.len - scratch.len()) + .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {} bytes for a strip or tile", pref.len)))?; + scratch.resize(pref.len, 0); + } + let buf = scratch.get_mut(..pref.len).ok_or_else(|| err("scratch buffer"))?; + match dec.read_chunk_bytes(chunk, buf) { + Ok(()) => tally.decoded += 1, + Err(e) if is_eof(&e) => { + tally.missing += 1; + continue; + } + Err(e) => return Err(map_err(e)), + } + let x0 = col * lay.cw; + let n = lay.cw.min(lay.w.saturating_sub(x0)); + for (ri, src_row) in buf.chunks_exact(stride).take(rows).enumerate() { + let start = ri.saturating_mul(erow); + let Some(er) = extra.get_mut(start..start.saturating_add(erow)) else { continue }; + let px = ech * lay.out.bytes(); + let Some(dst) = er.get_mut(x0.saturating_mul(px)..(x0.saturating_add(n)).saturating_mul(px)) else { continue }; + transfer(lay.src, src_row, 1, 0, dst, lay.out, ech, ei, n); + } + } + } + Ok(()) +} + +/// Converts `n` pixels of one decoded chunk row into output pixels (`plane` for planar data). +fn convert_row(lay: &Layout, src: &[u8], dst: &mut [u8], n: usize, plane: usize) { + let och = lay.out_channels(); + if let Some(map) = &lay.palette { + let ob = lay.out.bytes(); + for (i, d) in dst.chunks_exact_mut(och * ob).take(n).enumerate() { + let index = sample_index(lay.src, src, i); + let Some(rgb) = map.get(index) else { continue }; + for (c, v) in d.chunks_exact_mut(ob).zip(rgb) { + if ob == 1 { + c.copy_from_slice(&[(v >> 8) as u8]); + } else { + c.copy_from_slice(&v.to_ne_bytes()); + } + } + } + return; + } + if lay.copyable() { + if let Some(s) = src.get(..dst.len()) { + dst.copy_from_slice(s); + } + return; + } + if lay.planar { + transfer(lay.src, src, 1, 0, dst, lay.out, och, plane, n); + } else { + for k in 0..och { + transfer(lay.src, src, lay.chunk_spp, lay.source_sample(k), dst, lay.out, och, k, n); + } + } +} + +/// The value of pixel `i` in a row of one-sample indices (palette and sub-byte gray). +fn sample_index(src: Src, row: &[u8], i: usize) -> usize { + match src { + Src::Bits(bits) => { + let bits = usize::from(bits); + let bit = i * bits; + let byte = row.get(bit / 8).copied().unwrap_or(0); + usize::from((byte >> (8 - bits - bit % 8)) & ((1u8 << bits) - 1)) + } + Src::U8 => row.get(i).copied().map_or(0, usize::from), + Src::U16 => row.get(i * 2..i * 2 + 2).and_then(|s| s.try_into().ok()).map_or(0, |s| usize::from(u16::from_ne_bytes(s))), + _ => 0, + } +} + +/// Copies sample `si` of each of `n` decoded pixels (`sspp` samples each) to output channel +/// `di` (`dch` channels), converting the sample type. +#[allow(clippy::too_many_arguments)] +fn transfer(src: Src, row: &[u8], sspp: usize, si: usize, dst: &mut [u8], out: SampleType, dch: usize, di: usize, n: usize) { + let ob = out.bytes(); + if let Src::Bits(bits) = src { + let max = (1u32 << bits) - 1; + for (i, d) in dst.chunks_exact_mut(dch * ob).take(n).enumerate() { + if let Some(d) = d.get_mut(di) { + *d = (sample_index(src, row, i) as u32 * 255 / max) as u8; + } + } + return; + } + let sb = src.bytes(); + if src.is_identity() { + // Same sample type: a strided copy. + if sb == 1 { + for (d, s) in dst.iter_mut().skip(di).step_by(dch).zip(row.iter().skip(si).step_by(sspp)).take(n) { + *d = *s; + } + } else { + for (d, s) in dst.chunks_exact_mut(ob).skip(di).step_by(dch).zip(row.chunks_exact(sb).skip(si).step_by(sspp)).take(n) { + d.copy_from_slice(s); + } + } + return; + } + for (s, d) in row.chunks_exact(sspp * sb).zip(dst.chunks_exact_mut(dch * ob)).take(n) { + let (Some(s), Some(d)) = (s.get(si * sb..(si + 1) * sb), d.get_mut(di * ob..(di + 1) * ob)) else { continue }; + match src { + Src::U32 => { + let v = s.try_into().map_or(0, u32::from_ne_bytes); + d.copy_from_slice(&((v >> 16) as u16).to_ne_bytes()); + } + Src::U64 => { + let v = s.try_into().map_or(0, u64::from_ne_bytes); + d.copy_from_slice(&((v >> 48) as u16).to_ne_bytes()); + } + Src::F64 => { + let v = s.try_into().map_or(0.0, f64::from_ne_bytes); + d.copy_from_slice(&(v as f32).to_ne_bytes()); + } + _ => d.copy_from_slice(s), + } + } +} + +/// Applies what the stored samples leave to the reader: WhiteIsZero inversion of the gray +/// channel, and turning associated (premultiplied) alpha into straight alpha. +fn finish_band(lay: &Layout, band: &mut [u8]) { + if !lay.invert && !lay.premultiplied { + return; + } + let ch = lay.out_channels(); + let ob = lay.out.bytes(); + let color = lay.out_layout.color_channels(); + for p in band.chunks_exact_mut(ch * ob) { + match lay.out { + SampleType::U8 => { + if lay.invert + && let Some(g) = p.first_mut() + { + *g = 255 - *g; + } + if lay.premultiplied { + let a = u32::from(p.get(ch - 1).copied().unwrap_or(255)); + for c in p.iter_mut().take(color) { + *c = (u32::from(*c) * 255 + a / 2).checked_div(a).unwrap_or(0).min(255) as u8; + } + } + } + SampleType::U16 => { + let get = |p: &[u8], i: usize| p.get(i * 2..i * 2 + 2).and_then(|s| s.try_into().ok()).map_or(0, u16::from_ne_bytes); + let a = u32::from(get(p, ch - 1)); + for i in 0..color { + let mut v = u32::from(get(p, i)); + if lay.invert && i == 0 { + v = 65535 - v; + } + if lay.premultiplied { + v = (v * 65535 + a / 2).checked_div(a).unwrap_or(0).min(65535); + } + if let Some(s) = p.get_mut(i * 2..i * 2 + 2) { + s.copy_from_slice(&(v as u16).to_ne_bytes()); + } + } + } + SampleType::F16 | SampleType::F32 => { + let get = |p: &[u8], i: usize| -> f32 { + if ob == 2 { + p.get(i * 2..i * 2 + 2).and_then(|s| s.try_into().ok()).map_or(0.0, |s| f16::from_bits(u16::from_ne_bytes(s)).to_f32()) + } else { + p.get(i * 4..i * 4 + 4).and_then(|s| s.try_into().ok()).map_or(0.0, f32::from_ne_bytes) + } + }; + let a = get(p, ch - 1); + for i in 0..color { + let mut v = get(p, i); + if lay.invert && i == 0 { + v = 1.0 - v; + } + if lay.premultiplied && a > 0.0 { + v /= a; + } + if ob == 2 { + if let Some(s) = p.get_mut(i * 2..i * 2 + 2) { + s.copy_from_slice(&f16::from_f32(v).to_bits().to_ne_bytes()); + } + } else if let Some(s) = p.get_mut(i * 4..i * 4 + 4) { + s.copy_from_slice(&v.to_ne_bytes()); + } + } + } + } + } +} diff --git a/crates/image-codecs/src/codecs/tiff/decode.rs b/crates/image-codecs/src/codecs/tiff/decode.rs new file mode 100644 index 000000000..d8e11c447 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/decode.rs @@ -0,0 +1,932 @@ +use std::borrow::Cow; + +use super::bands::{Tally, convert_ab, decode_bands, extra_sample_indices, refill_zeroed, split_extra_samples, zeroed}; +use super::tags::{F, TAG_IMAGE_SOURCE_DATA, TAG_PHOTOSHOP, TAG_XMP, TEXT_TAGS, photoshop_tags}; +use super::ycbcr::YCbCr; +use super::{ + ChannelLayout, ChunkType, CodecError, DecodeWarning, Decoder, ExifDirs, File, Ifd, Image, Limits, Metadata, Read, SampleType, Seek, SeekFrom, Tag, + camera_raw, err, ifd, io, map_err, tiff_exif, +}; + +/// Decodes the page Photoshop would open: the first full-resolution directory. +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + decode_page(bytes, None, limits) +} + +/// Decodes page `page` of [`ifd::tiff_info`]'s list (`None`: the default page). The image +/// carries the page's metadata, a [`DecodeWarning::MorePages`] when the file holds more +/// full-resolution pages, and a [`DecodeWarning::Truncated`] when image data is cut off. +pub(crate) fn decode_page(bytes: &[u8], page: Option, limits: &Limits) -> Result { + let (file, dir, more) = open_page(bytes, page)?; + let mut img = decode_ifd(&file, &dir, limits)?; + let extra_samples = std::mem::take(&mut img.meta.extra_samples); + img.meta = metadata(&file, &dir, &mut img.icc); + img.meta.extra_samples = extra_samples; + img.warnings.extend(more); + Ok(img) +} + +/// Inspect the selected directory's sample classification without decoding any pixels. +/// Mirror document import: inspect bands first, then the default whole-image route when +/// auxiliary channels are present. Preparation reads metadata, not strips or tiles. +pub(crate) fn has_unretained_samples(bytes: &[u8], offset: u64) -> Result { + let file = File::parse(bytes).ok_or_else(|| err("not a TIFF or BigTIFF header"))?; + let dir = file.ifd(offset).ok_or_else(|| err("image directory cut off"))?; + let bands = prepare(&file, &dir, &Limits::banded(), cfg!(target_pointer_width = "32"))?; + if bands.extras_over_limit > 0 || bands.extras_dropped > 0 { + return Ok(true); + } + if bands.lay.extra_at.is_empty() { + return Ok(false); + } + Ok(prepare(&file, &dir, &Limits::default(), true)?.extras_dropped > 0) +} + +/// The file, page `page`'s directory (`None`: the default page) and the +/// [`DecodeWarning::MorePages`] the page opens with when the file holds more. +fn open_page(bytes: &[u8], page: Option) -> Result<(File<'_>, Ifd, Option), CodecError> { + if camera_raw(bytes) { + return Err(CodecError::unsupported(F, "camera raw files (such as CR2, NEF, ARW or DNG) are not flat images; decode them with photocraft-raw")); + } + let file = File::parse(bytes).ok_or_else(|| err("not a TIFF or BigTIFF header"))?; + let info = ifd::info_of(&file)?; + let index = match page { + Some(i) => i, + None => info.default_page().ok_or_else(|| err("no image directory"))?, + }; + let p = info + .pages + .get(index) + .ok_or_else(|| CodecError::InvalidImage(format!("TIFF page {index} does not exist (the file has {} directories)", info.pages.len())))?; + let dir = file.ifd(p.ifd_offset).ok_or_else(|| err("image directory cut off"))?; + let pages = info.page_count(); + let more = (pages > 1).then(|| DecodeWarning::MorePages { total: info.complete.then_some(u32::try_from(pages).unwrap_or(u32::MAX)) }); + Ok((file, dir, more)) +} + +/// Output bytes [`TiffBands::decode`] aims to decode at once (at least one band per worker): small +/// next to the document the rows are written into, and to the copy a caller may convert them to. +const GROUP_BYTES: usize = 64 << 20; + +/// A TIFF page opened for decoding a group of bands at a time, for images too large to hold +/// twice (#8169): [`TiffBands::decode`] hands each group to its caller, who writes it into the +/// document, so only one group exists beside the document. The pixels, layout and metadata are +/// those [`decode_page`] returns; [`TiffBands::upright`] applies the orientation as +/// [`Image::oriented`] does. +pub struct TiffBands<'a> { + b: &'a [u8], + p: Prepared, + /// The page's Orientation tag (1–8). + stored: u16, + /// The orientation the bands are turned by (1: as stored). + turn: u16, + /// `Limits::max_alloc`: the most one group of bands may take. + cap: u64, + pub icc: Option>, + pub meta: Metadata, + /// What the decoder noticed: more pages, and after [`TiffBands::decode`] data cut off. + pub warnings: Vec, +} + +impl<'a> TiffBands<'a> { + /// Opens page `page` (`None`: the page [`decode_page`] opens by default) and checks it + /// against `limits`; on 64-bit targets its `max_alloc` bounds each group of bands, not the + /// whole image. + pub fn open(bytes: &'a [u8], page: Option, limits: &Limits) -> Result { + let (file, dir, more) = open_page(bytes, page)?; + // On 32-bit targets the whole image must still fit `max_alloc`, as it did before: the + // document holding it shares the same small address space (wasm32: 4 GiB). + let p = prepare(&file, &dir, limits, cfg!(target_pointer_width = "32"))?; + let band = p.band_bytes()?; + if band as u64 > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!( + "one strip or row of tiles of this {}x{} image decodes to {band} bytes, over the {} byte limit", + p.w, p.h, limits.max_alloc + ))); + } + let mut icc = None; + let meta = metadata(&file, &dir, &mut icc); + let stored = ifd::orientation_of(&file, &dir); + // Extra samples are dropped here only on 32-bit targets, where the whole image counts. + let mut warnings = prepared_warnings(&p); + warnings.extend(more); + Ok(TiffBands { b: bytes, p, stored, turn: 1, cap: limits.max_alloc, icc, meta, warnings }) + } + + /// The bands turned upright for the page's Orientation tag, as [`Image::oriented`] turns a + /// whole image: the size and resolution swap for 5–8, and the EXIF and XMP orientation are + /// rewritten to 1. + pub fn upright(mut self) -> Self { + let o = self.stored; + if !(2..=8).contains(&o) || self.turn == o { + return self; + } + self.turn = o; + if let Some(e) = &mut self.meta.exif + && let Cow::Owned(fixed) = crate::orientation::upright_exif(e) + { + *e = fixed; + } + if let Some(x) = &mut self.meta.xmp + && let Cow::Owned(fixed) = crate::orientation::upright_xmp(x) + { + *x = fixed; + } + if o >= 5 { + self.meta.dpi = self.meta.dpi.map(|(x, y)| (y, x)); + } + self + } + + /// The page's Orientation tag (1–8; 1 when absent or invalid). + pub fn orientation(&self) -> u16 { + self.stored + } + + /// The size of the image the bands make up (upright when [`TiffBands::upright`] was applied). + pub fn dimensions(&self) -> (u32, u32) { + if self.turn >= 5 { (self.p.h, self.p.w) } else { (self.p.w, self.p.h) } + } + + pub fn layout(&self) -> ChannelLayout { + self.p.lay.out_layout + } + + /// How many samples of a pixel are neither colour nor the alpha channel: alpha and spot + /// channels that [`TiffBands::decode`] does not read (a whole-image decode does). + pub fn extra_samples(&self) -> usize { + self.p.lay.extra_at.len() + } + + pub fn sample_type(&self) -> SampleType { + self.p.lay.out + } + + /// Bands decoded at once: about [`GROUP_BYTES`], at least one per worker, at most `max_alloc`. + fn bands_per_group(&self) -> Result { + let band = self.p.band_bytes()?.max(1); + #[cfg(not(target_arch = "wasm32"))] + let workers = rayon::current_num_threads(); + #[cfg(target_arch = "wasm32")] + let workers = 1; + let cap = usize::try_from(self.cap).unwrap_or(usize::MAX); + Ok((GROUP_BYTES / band).max(workers).min(cap / band).max(1)) + } + + /// Bytes of the largest group [`TiffBands::decode`] hands over: what decoding needs beside + /// the document, for a caller checking that it fits in memory. A file stored as one strip + /// is one group, the whole image. + pub fn group_bytes(&self) -> Result { + let rows = self.bands_per_group()?.saturating_mul(self.p.band_rows).min(self.p.h as usize); + self.p.lay.out_row().checked_mul(rows).ok_or_else(|| CodecError::LimitExceeded("band size".into())) + } + + /// A conservative bound on the decoder's own scratch memory in a whole-image decode of this + /// page (as `decode_page` does it): one strip or tile per worker that decodes a band at + /// once. Zero when strips are decoded in place. Add the decoded image itself and any + /// extra-sample planes. + pub fn decode_scratch_bytes(&self) -> u64 { + #[cfg(not(target_arch = "wasm32"))] + let workers = rayon::current_num_threads() as u64; + #[cfg(target_arch = "wasm32")] + let workers = 1; + scratch_bound(self.p.lay.direct(), self.p.scratch_bytes as u64, self.p.band_rows, self.p.h, workers) + } + + /// Decodes the image a group of bands at a time, in order, handing each group to `sink` + /// with the position of its top-left pixel in the image [`TiffBands::dimensions`] gives. A + /// group is about 64 MiB (at least one strip or row of tiles per worker, at most `max_alloc`; + /// see [`TiffBands::group_bytes`]). Data cut off by the end of the file adds a + /// [`DecodeWarning::Truncated`] to `warnings`. + pub fn decode>(&mut self, mut sink: impl FnMut(u32, u32, &Image) -> Result<(), E>) -> Result<(), E> { + let per_group = self.bands_per_group()?; + let p = &self.p; + let band = p.band_bytes()?.max(1); + let bands = (p.h as usize).div_ceil(p.band_rows.max(1)); + let mut buf: Vec = Vec::new(); + let mut tally = Tally::default(); + let mut first = 0; + while first < bands { + let n = per_group.min(bands - first); + let y0 = first * p.band_rows; + let rows = (n * p.band_rows).min(p.h as usize - y0); + let len = p.lay.out_row().checked_mul(rows).ok_or_else(|| CodecError::LimitExceeded("band size".into()))?; + // Missing chunks leave their rows as they are: they must start empty in every group. + buf = refill_zeroed(buf, len)?; + tally = tally + decode_bands(p, self.b, &mut buf, band, first, &mut [], 0)?; + if let Some(c) = p.ycbcr { + c.convert(&mut buf, p.lay.out_channels(), p.lay.out); + } + if let Some(signed) = p.lab { + convert_ab(&mut buf, p.lay.out_channels(), p.lay.out, signed, false); + } + let rows = u32::try_from(rows).map_err(|_| err("band too tall"))?; + let y0 = u32::try_from(y0).map_err(|_| err("band out of range"))?; + let group = Image::from_raw(p.w, rows, p.lay.out_layout, p.lay.out, std::mem::take(&mut buf))?; + let (x, y) = match self.turn { + 3 | 4 => (0, p.h.saturating_sub(y0 + rows)), + 5 | 8 => (y0, 0), + 6 | 7 => (p.h.saturating_sub(y0 + rows), 0), + _ => (0, y0), + }; + let group = group.oriented(self.turn)?; + sink(x, y, &group)?; + buf = group.into_data(); + first += n; + } + if tally.decoded == 0 && tally.missing > 0 { + return Err(err("the image data is missing (cut off or past the end of the file)").into()); + } + if tally.missing > 0 { + // After the extra-sample warnings and before `MorePages`, as the whole-image decode + // orders them. + let at = self.warnings.iter().position(|w| matches!(w, DecodeWarning::MorePages { .. })).unwrap_or(self.warnings.len()); + self.warnings.insert(at, DecodeWarning::Truncated { format: F }); + } + Ok(()) + } +} + +/// [`TiffBands::decode_scratch_bytes`]'s bound: none when chunks are decoded in place, +/// otherwise one chunk per worker that decodes a band, and never more chunks than the image has. +fn scratch_bound(direct: bool, chunk_bytes: u64, band_rows: usize, h: u32, workers: u64) -> u64 { + if direct { + return 0; + } + let bands = u64::from(h).div_ceil(band_rows.max(1) as u64).max(1); + chunk_bytes.saturating_mul(workers.min(bands)) +} + +/// The Orientation (1–8) of page `page` (`None`: the default page); 1 when absent, malformed +/// or out of range. Classic TIFF and BigTIFF. +pub(crate) fn orientation(bytes: &[u8], page: Option) -> u16 { + let Some(file) = File::parse(bytes) else { return 1 }; + let Ok(info) = ifd::info_of(&file) else { return 1 }; + page.or_else(|| info.default_page()).and_then(|i| info.pages.get(i)).and_then(|p| file.ifd(p.ifd_offset)).map_or(1, |d| ifd::orientation_of(&file, &d)) +} + +/// The page's metadata; the ICC profile goes to `icc`. +fn metadata(f: &File<'_>, dir: &Ifd, icc: &mut Option>) -> Metadata { + *icc = f.tag_bytes(dir, Tag::IccProfile.to_u16()).filter(|b| !b.is_empty()).map(<[u8]>::to_vec); + let text = |tag: u16| { + let b = f.tag_bytes(dir, tag)?; + let s = std::str::from_utf8(b).ok()?.trim_end_matches('\0'); + (!s.is_empty()).then(|| s.to_owned()) + }; + let mut meta = Metadata { xmp: text(TAG_XMP), ..Default::default() }; + let unit = f.tag_uint(dir, Tag::ResolutionUnit.to_u16()).unwrap_or(2); + if let (Some(x), Some(y)) = (f.tag_f64(dir, Tag::XResolution.to_u16()), f.tag_f64(dir, Tag::YResolution.to_u16())) + && x > 0.0 + && y > 0.0 + && x.is_finite() + && y.is_finite() + { + meta.dpi = match unit { + 2 => Some((x as f32, y as f32)), + 3 => Some(((x * 2.54) as f32, (y * 2.54) as f32)), + _ => None, + }; + } + for (tag, key) in TEXT_TAGS { + if let Some(s) = text(tag.to_u16()) { + meta.text.push(((*key).to_owned(), s)); + } + } + // Photoshop keeps a layered TIFF's layers and resources in IFD0, where `photoshop_tags` + // reads them; any other page reads its own directory's copies. + let (resources, layers) = + if dir.at == f.first_ifd { photoshop_tags(f.b) } else { (f.tag_bytes(dir, TAG_PHOTOSHOP), f.tag_bytes(dir, TAG_IMAGE_SOURCE_DATA)) }; + meta.photoshop_resources = resources.map(<[u8]>::to_vec); + meta.photoshop_layers = layers.map(<[u8]>::to_vec); + // EXIF: the Exif and GPS directories IFD0 points to, with IFD0's descriptive tags, as an + // EXIF block (the other pages are reduced copies or layers of the same image). + if dir.at == f.first_ifd + && let Some(e) = tiff_exif::read(f.b).filter(ExifDirs::has_directories) + { + meta.exif = Some(tiff_exif::block(&e)); + } + meta +} + +// --------------------------------------------------------------------------- +// Banded pixel decoding +// --------------------------------------------------------------------------- + +/// The file seen with a few byte ranges replaced: the header's first-IFD offset (so the `tiff` +/// decoder opens the chosen directory as its IFD0) and, for WhiteIsZero and palette images, +/// the PhotometricInterpretation value (so the decoder hands over the stored samples and this +/// module applies the interpretation itself). Reads past the end return no bytes. +#[derive(Clone, Copy)] +pub(super) struct Patched<'a> { + b: &'a [u8], + patches: &'a [(u64, Vec)], + pos: u64, +} + +impl Read for Patched<'_> { + fn read(&mut self, out: &mut [u8]) -> io::Result { + let Some(src) = usize::try_from(self.pos).ok().and_then(|p| self.b.get(p..)) else { return Ok(0) }; + let n = out.len().min(src.len()); + let (Some(o), Some(s)) = (out.get_mut(..n), src.get(..n)) else { return Ok(0) }; + o.copy_from_slice(s); + let end = self.pos.saturating_add(n as u64); + for (at, bytes) in self.patches { + let lo = (*at).max(self.pos); + let hi = at.saturating_add(bytes.len() as u64).min(end); + for i in lo..hi { + let (Ok(oi), Ok(pi)) = (usize::try_from(i - self.pos), usize::try_from(i - at)) else { continue }; + if let (Some(d), Some(&v)) = (o.get_mut(oi), bytes.get(pi)) { + *d = v; + } + } + } + self.pos = end; + Ok(n) + } +} + +impl Seek for Patched<'_> { + fn seek(&mut self, to: SeekFrom) -> io::Result { + let new = match to { + SeekFrom::Start(n) => Some(n), + SeekFrom::End(d) => (self.b.len() as u64).checked_add_signed(d), + SeekFrom::Current(d) => self.pos.checked_add_signed(d), + }; + let new = new.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "seek out of range"))?; + self.pos = new; + Ok(new) + } +} + +/// How a stored sample is laid out in the `tiff` crate's chunk buffer (native endian). +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(super) enum Src { + /// 1, 2 or 4-bit unsigned, packed MSB first, rows padded to a byte. + Bits(u8), + U8, + U16, + U32, + U64, + F16, + F32, + F64, +} + +impl Src { + pub(super) fn bytes(self) -> usize { + match self { + Src::Bits(_) | Src::U8 => 1, + Src::U16 | Src::F16 => 2, + Src::U32 | Src::F32 => 4, + Src::U64 | Src::F64 => 8, + } + } + + /// The output sample type: integers above 16 bits keep their top 16, doubles become f32. + fn out(self) -> SampleType { + match self { + Src::Bits(_) | Src::U8 => SampleType::U8, + Src::U16 | Src::U32 | Src::U64 => SampleType::U16, + Src::F16 => SampleType::F16, + Src::F32 | Src::F64 => SampleType::F32, + } + } + + /// Stored bytes and output bytes are the same: a plain copy. + pub(super) fn is_identity(self) -> bool { + matches!(self, Src::U8 | Src::U16 | Src::F16 | Src::F32) + } +} + +/// Everything needed to decode and place the chunks of one directory. +pub(super) struct Layout { + pub(super) w: usize, + pub(super) src: Src, + pub(super) out: SampleType, + pub(super) out_layout: ChannelLayout, + /// Samples per pixel in a decoded chunk (1 for planar data). + pub(super) chunk_spp: usize, + pub(super) planar: bool, + /// Planes decoded for planar data: one per output channel (extra planes are skipped). + pub(super) planes: usize, + /// Tile width (a strip is `w` wide). + pub(super) cw: usize, + pub(super) across: usize, + pub(super) chunks_per_plane: usize, + /// WhiteIsZero: invert the gray channel. + pub(super) invert: bool, + /// The decoded sample (chunky) or file plane (planar) of the alpha channel (when there is one). + alpha_sample: usize, + /// Associated (premultiplied) alpha to turn straight. + pub(super) premultiplied: bool, + /// Palette images: the colour map (16-bit RGB per index). + pub(super) palette: Option>, + /// Upper bound for one chunk's scratch buffer. + pub(super) scratch_cap: usize, + /// File sample indices that are neither colour nor the chosen alpha (spot / extra alpha). + pub(super) extra_at: Vec, +} + +impl Layout { + pub(super) fn out_channels(&self) -> usize { + self.out_layout.channels() + } + /// The decoded sample (chunky) or file plane (planar) of output channel `k`. + pub(super) fn source_sample(&self, k: usize) -> usize { + if self.out_layout.has_alpha() && k + 1 == self.out_channels() { self.alpha_sample } else { k } + } + pub(super) fn out_row(&self) -> usize { + self.w * self.out_channels() * self.out.bytes() + } + pub(super) fn extra_row(&self) -> usize { + self.w.saturating_mul(self.extra_at.len()).saturating_mul(self.out.bytes()) + } + /// A chunky strip already in the output's layout: decoded in place. + pub(super) fn direct(&self) -> bool { + self.copyable() && self.across == 1 + } + /// Chunk rows are output rows: copied without conversion. + pub(super) fn copyable(&self) -> bool { + !self.planar && self.palette.is_none() && self.src.is_identity() && self.chunk_spp == self.out_channels() + } +} + +/// A chunk read that hit the end of the file (the data is missing, not malformed). +pub(super) fn is_eof(e: &tiff::TiffError) -> bool { + matches!(e, tiff::TiffError::IoError(io) if io.kind() == io::ErrorKind::UnexpectedEof) +} + +/// The sample type of a (SampleFormat, BitsPerSample) pair. +fn sample_src(format: u64, bits: u64) -> Result { + Ok(match (format, bits) { + (1, 1 | 2 | 4) => Src::Bits(bits as u8), + (1, 8) => Src::U8, + (1, 16) => Src::U16, + (1, 32) => Src::U32, + (1, 64) => Src::U64, + (3, 16) => Src::F16, + (3, 32) => Src::F32, + (3, 64) => Src::F64, + (2, _) => return Err(CodecError::unsupported(F, "signed integer samples")), + _ => return Err(CodecError::unsupported(F, format!("{bits}-bit samples (sample format {format})"))), + }) +} + +/// The ColorMap of a palette image as RGB entries. +fn read_palette(f: &File<'_>, dir: &Ifd, spp: u64, bits: u64, src: Src) -> Result, CodecError> { + if spp != 1 || !matches!(src, Src::Bits(_) | Src::U8 | Src::U16) { + return Err(CodecError::unsupported(F, format!("palette image with {spp} samples of {bits} bits"))); + } + let n = 1u64 << bits; + let map = dir.get(ifd::TAG_COLOR_MAP).filter(|e| e.ty == 3 && e.count == 3 * n).ok_or_else(|| err("palette image without a valid ColorMap"))?; + let at = |i: u64| f.uint(map, i).and_then(|v| u16::try_from(v).ok()); + let table: Option> = (0..n).map(|i| Some([at(i)?, at(n + i)?, at(2 * n + i)?])).collect(); + table.ok_or_else(|| err("palette ColorMap cut off")) +} + +/// The layout of a multiband read that was asked for every sample. +fn multiband_layout(photometric: u64, has_alpha: bool, gray_alpha: bool) -> ChannelLayout { + match (photometric, has_alpha) { + (2 | 8 | 9, true) => ChannelLayout::Rgba, + (2 | 8 | 9, false) => ChannelLayout::Rgb, + (5, true) => ChannelLayout::CmykA, + (5, false) => ChannelLayout::Cmyk, + _ if gray_alpha => ChannelLayout::GrayA, + _ => ChannelLayout::Gray, + } +} + +/// The chunk tables must match the geometry. Checked in 64 bits, before the `tiff` crate does +/// the same arithmetic in narrower types. +fn check_chunk_tables(f: &File<'_>, dir: &Ifd, (w, h): (u32, u32), spp: u64, planar: bool, tiled: bool) -> Result<(u64, u64, u64), CodecError> { + let need = |tag: u16, name: &str| f.tag_uint(dir, tag).ok_or_else(|| err(format!("missing or unreadable {name}"))); + let (cw, ch) = if tiled { + let tw = need(ifd::TAG_TILE_WIDTH, "TileWidth")?; + let tl = need(ifd::TAG_TILE_LENGTH, "TileLength")?; + (tw, tl) + } else { + (u64::from(w), f.tag_uint(dir, ifd::TAG_ROWS_PER_STRIP).unwrap_or(u64::from(h)).min(u64::from(h))) + }; + if cw == 0 || ch == 0 { + return Err(err("zero tile or strip size")); + } + // The chunk tables must match the geometry. Checked here, in 64 bits, before the `tiff` + // crate does the same arithmetic in narrower types. + let planes_in_file = if planar { spp } else { 1 }; + let expected = u64::from(w) + .div_ceil(cw) + .checked_mul(u64::from(h).div_ceil(ch)) + .and_then(|n| n.checked_mul(planes_in_file)) + .filter(|&n| n <= u64::from(u32::MAX)) + .ok_or_else(|| err("too many strips or tiles"))?; + let (offsets_tag, counts_tag) = if tiled { (324, 325) } else { (273, 279) }; + let counts = (dir.get(offsets_tag).map(|e| e.count), dir.get(counts_tag).map(|e| e.count)); + if counts != (Some(expected), Some(expected)) { + return Err(err(format!("{expected} strips or tiles expected, the tables list {counts:?}"))); + } + Ok((cw, ch, expected)) +} + +/// Byte patches the `tiff` crate reads through: the directory is seen as the last of its chain, +/// and PhotometricInterpretation is handed over as black-is-zero (or RGB for Lab) when asked to. +fn header_patches( + f: &File<'_>, + dir: &Ifd, + photometric_entry: Option, + patch_photometric: bool, + lab_only: bool, +) -> Result)>, CodecError> { + let mut patches: Vec<(u64, Vec)> = Vec::new(); + let encode_u = |v: u64, len: usize| -> Vec { + let b = if f.le { v.to_le_bytes() } else { v.to_be_bytes() }; + if f.le { b.get(..len).map(<[u8]>::to_vec).unwrap_or_default() } else { b.get(8 - len..).map(<[u8]>::to_vec).unwrap_or_default() } + }; + if f.big { + patches.push((8, encode_u(dir.at, 8))); + } else { + let at = u32::try_from(dir.at).map_err(|_| err("directory offset out of range"))?; + patches.push((4, encode_u(u64::from(at), 4))); + } + // The directory is seen as the last of its chain: the chain was walked already, and a + // cycle through it must not fail this page. + patches.push((dir.next_at, vec![0; if f.big { 8 } else { 4 }])); + if patch_photometric { + let e = photometric_entry.ok_or_else(|| err("missing PhotometricInterpretation"))?; + let len = match e.ty { + 3 if e.count == 1 => 2, + 4 if e.count == 1 => 4, + _ => return Err(err("malformed PhotometricInterpretation")), + }; + patches.push((e.value_at, encode_u(if lab_only { 2 } else { 1 }, len))); + } + Ok(patches) +} + +/// Lab and YCbCr are converted after decoding, from 8 or 16-bit samples only: Lab needs at least +/// its three colour samples, YCbCr exactly three. +fn check_converted_model(photometric: u64, spp: u64, bits: u64, src: Src) -> Result<(), CodecError> { + let (name, samples_ok) = match photometric { + 8 | 9 => ("Lab", spp >= 3), + 6 => ("YCbCr", spp == 3), + _ => return Ok(()), + }; + if samples_ok && matches!(src, Src::U8 | Src::U16) { + return Ok(()); + } + Err(CodecError::unsupported(F, format!("{name} image with {spp} samples of {bits} bits"))) +} + +/// A directory checked and laid out for decoding, before any pixel is read. +pub(super) struct Prepared { + w: u32, + h: u32, + pub(super) lay: Layout, + /// Rows per band: one strip, or one row of tiles. + band_rows: usize, + patches: Vec<(u64, Vec)>, + tl: tiff::decoder::Limits, + /// Lab samples: `Some(true)` for CIELab (a* and b* stored signed), `Some(false)` for ICCLab. + lab: Option, + /// YCbCr samples: the conversion to RGB (#10316). + ycbcr: Option, + /// The colour samples of a pixel in the file's colour model. + color: usize, + /// The ExtraSamples tag (0 unspecified, 1 associated alpha, 2 unassociated alpha). + extra_kinds: Vec, + /// Extra samples past `MAX_EXTRA_SAMPLES`, left out. + extras_over_limit: usize, + /// Extra samples left out because they did not fit the allocation budget beside the image. + extras_dropped: u32, + /// Bytes one strip or tile decodes to, before clipping to the image: a worker's scratch + /// buffer. A strip is the full width; a tile keeps its declared size even at an edge. + scratch_bytes: usize, +} + +impl Prepared { + /// A decoder over `b` (the file the directory was read from) that opens the directory. + pub(super) fn open<'a>(&'a self, b: &'a [u8]) -> Result>, CodecError> { + Decoder::new(Patched { b, patches: &self.patches, pos: 0 }).map(|d| d.with_limits(self.tl.clone())).map_err(map_err) + } + + /// Output bytes of one full band. + fn band_bytes(&self) -> Result { + self.lay.out_row().checked_mul(self.band_rows).ok_or_else(|| CodecError::LimitExceeded("band size".into())) + } +} + +/// Decodes the pixels of one directory. +fn decode_ifd(f: &File<'_>, dir: &Ifd, limits: &Limits) -> Result { + let p = prepare(f, dir, limits, true)?; + // The output, allocated once and fallibly. + let len = p.lay.out_row().checked_mul(p.h as usize).ok_or_else(|| CodecError::LimitExceeded("image size".into()))?; + let mut data = zeroed(len)?; + let extra_row = p.lay.extra_row(); + let extra_len = extra_row.checked_mul(p.h as usize).ok_or_else(|| CodecError::LimitExceeded("extra samples".into()))?; + let mut extra_data = if p.lay.extra_at.is_empty() { Vec::new() } else { zeroed(extra_len)? }; + let extra_band = extra_row.saturating_mul(p.band_rows); + let tally = decode_bands(&p, f.b, &mut data, p.band_bytes()?, 0, extra_data.as_mut_slice(), extra_band)?; + if tally.decoded == 0 && tally.missing > 0 { + return Err(err("the image data is missing (cut off or past the end of the file)")); + } + if let Some(c) = p.ycbcr { + c.convert(&mut data, p.lay.out_channels(), p.lay.out); + } + if let Some(signed) = p.lab { + convert_ab(&mut data, p.lay.out_channels(), p.lay.out, signed, false); + } + let mut img = Image::from_raw(p.w, p.h, p.lay.out_layout, p.lay.out, data)?; + if !p.lay.extra_at.is_empty() { + // TIFF 6.0 defines ExtraSamples 0, 1 and 2 only; a reserved value reads as unspecified, + // so a save never writes it back. + let kind_of = |si: usize| match si.checked_sub(p.color).and_then(|k| p.extra_kinds.get(k)).copied() { + Some(v @ 0..=2) => v as u16, + _ => 0, + }; + img.meta.extra_samples = split_extra_samples(&extra_data, p.w as usize, p.h as usize, &p.lay.extra_at, p.lay.out, kind_of)?; + } + img.warnings.extend(prepared_warnings(&p)); + if tally.missing > 0 { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(img) +} + +/// What [`prepare`] left out of the image: extra samples past the supported count, then extra +/// samples over the allocation budget. Both decoders report them, in this order. +fn prepared_warnings(p: &Prepared) -> Vec { + let mut warnings = Vec::new(); + if p.extras_over_limit > 0 { + let count = u32::try_from(p.extras_over_limit).unwrap_or(u32::MAX); + warnings.push(DecodeWarning::ExtraSamplesOverLimit { format: F, count }); + } + if p.extras_dropped > 0 { + warnings.push(DecodeWarning::ExtraSamplesDropped { format: F, count: p.extras_dropped }); + } + warnings +} + +/// Checks one directory and lays it out for decoding. `whole`: the image is decoded into one +/// buffer, which `limits.max_alloc` bounds; otherwise only its size is checked here. +fn prepare(f: &File<'_>, dir: &Ifd, limits: &Limits, whole: bool) -> Result { + // The values the `tiff` crate reads must exist: a count past the end of the file is + // neither allocated nor read. + if let Some(e) = dir.entries.iter().find(|e| ifd::STRUCTURAL_TAGS.contains(&e.tag) && f.data(e).is_none()) { + return Err(err(format!("tag {} points past the end of the file", e.tag))); + } + let need = |tag: u16, name: &str| f.tag_uint(dir, tag).ok_or_else(|| err(format!("missing or unreadable {name}"))); + let w = u32::try_from(need(ifd::TAG_WIDTH, "ImageWidth")?).map_err(|_| err("ImageWidth out of range"))?; + let h = u32::try_from(need(ifd::TAG_HEIGHT, "ImageLength")?).map_err(|_| err("ImageLength out of range"))?; + if w == 0 || h == 0 { + return Err(CodecError::InvalidImage(format!("zero-sized image {w}x{h}"))); + } + let spp = f.tag_uint(dir, ifd::TAG_SAMPLES).unwrap_or(1); + if spp == 0 || spp > 64 { + return Err(CodecError::unsupported(F, format!("{spp} samples per pixel"))); + } + let bits_all = f.tag_uints(dir, ifd::TAG_BITS, spp).unwrap_or_else(|| vec![1]); + let bits = bits_all.first().copied().unwrap_or(1); + if bits_all.iter().any(|&b| b != bits) { + return Err(CodecError::unsupported(F, format!("mixed bits per sample {bits_all:?}"))); + } + let format = f.tag_uint(dir, ifd::TAG_SAMPLE_FORMAT).unwrap_or(1); + let photometric_entry = dir.get(ifd::TAG_PHOTOMETRIC).copied(); + let photometric = f.tag_uint(dir, ifd::TAG_PHOTOMETRIC).ok_or_else(|| CodecError::unsupported(F, "missing PhotometricInterpretation"))?; + let planar = f.tag_uint(dir, ifd::TAG_PLANAR) == Some(2); + let extra = f.tag_uints(dir, ifd::TAG_EXTRA_SAMPLES, 64).unwrap_or_default(); + let tiled = dir.get(ifd::TAG_TILE_OFFSETS).is_some(); + let (cw, ch, expected) = check_chunk_tables(f, dir, (w, h), spp, planar, tiled)?; + let (offsets_tag, counts_tag) = if tiled { (324, 325) } else { (273, 279) }; + let src = sample_src(format, bits)?; + // WhiteIsZero and palette images are handed to the `tiff` crate as BlackIsZero: it then + // returns the stored samples, and the interpretation is applied here (for every depth and + // with alpha, which the crate's own inversion does not cover). + let invert = photometric == 0; + let palette = if photometric == 3 { Some(read_palette(f, dir, spp, bits, src)?) } else { None }; + // CIE L*a*b*: CIELab (8; a* and b* signed) and ICCLab (9; a* and b* offset) are handed to the + // `tiff` crate as RGB, then labelled Lab with a* and b* in PhotoCraft's encoding + // (`convert_ab`). + let lab = matches!(photometric, 8 | 9); + // YCbCr (6), as scanners and cameras write it (usually JPEG-compressed with subsampled + // chroma): the `tiff` crate decodes and upsamples the samples, and they are turned into RGB + // after decoding (#10316). + let ycbcr = photometric == 6; + check_converted_model(photometric, spp, bits, src)?; + // TIFF 6.0 lets any extra sample be the alpha (#1349). For RGB and CMYK the `tiff` crate + // returns the alpha only when it is the first extra sample: otherwise it is asked for every + // sample (as multiband gray) and the colour and alpha samples are picked here. An alpha + // position past the samples the file has is ignored. + let color: usize = match photometric { + 2 | 6 | 8 | 9 => 3, + 5 => 4, + _ => 1, + }; + let alpha_at = extra.iter().position(|&e| e == 1 || e == 2).filter(|&a| (color as u64).saturating_add(a as u64) < spp); + // Samples the colour model does not keep. RGB, CMYK and Lab only hand back every sample when + // asked (photometric patched to black-is-zero below); gray already comes back as multiband. + let unused = !extra.is_empty() && (spp as usize) > color.saturating_add(usize::from(alpha_at.is_some())); + let all_samples = palette.is_none() && matches!(photometric, 2 | 5 | 8 | 9) && (alpha_at.is_some_and(|a| a > 0) || (!planar && unused)); + let patches = header_patches(f, dir, photometric_entry, invert || palette.is_some() || all_samples || lab, lab && !all_samples)?; + + let tl = { + let mut l = tiff::decoder::Limits::default(); + l.decoding_buffer_size = limits.alloc_usize(); + // No compressed chunk can be larger than the file. + l.intermediate_buffer_size = f.b.len().max(1); + l + }; + let mut dec = Decoder::new(Patched { b: f.b, patches: &patches, pos: 0 }).map(|d| d.with_limits(tl.clone())).map_err(map_err)?; + if dec.dimensions().map_err(map_err)? != (w, h) || (dec.get_chunk_type() == ChunkType::Tile) != tiled { + return Err(err("inconsistent image directory")); + } + let ct = dec.colortype().map_err(map_err)?; + let gray_alpha = alpha_at.is_some() || (extra.is_empty() && spp == 2); + let (out_layout, chunk_spp) = match ct { + _ if palette.is_some() => (ChannelLayout::Rgb, 1), + tiff::ColorType::Gray(_) => (ChannelLayout::Gray, 1), + tiff::ColorType::Multiband { num_samples, .. } if all_samples => { + // Lab reads as RGB here and is relabelled below. + let layout = multiband_layout(photometric, alpha_at.is_some(), gray_alpha); + (layout, usize::from(num_samples)) + } + tiff::ColorType::Multiband { num_samples, .. } if photometric <= 1 => { + (if gray_alpha { ChannelLayout::GrayA } else { ChannelLayout::Gray }, usize::from(num_samples)) + } + tiff::ColorType::RGB(_) => (ChannelLayout::Rgb, 3), + tiff::ColorType::YCbCr(_) if ycbcr => (ChannelLayout::Rgb, 3), + tiff::ColorType::RGBA(_) => (ChannelLayout::Rgba, 4), + tiff::ColorType::CMYK(_) => (ChannelLayout::Cmyk, 4), + tiff::ColorType::CMYKA(_) => (ChannelLayout::CmykA, 5), + other => return Err(CodecError::unsupported(F, format!("colour type {other:?}"))), + }; + let out_layout = match out_layout { + ChannelLayout::Rgb if lab => ChannelLayout::Lab, + ChannelLayout::Rgba if lab => ChannelLayout::LabA, + _ if lab => return Err(err("Lab image that does not read as three colour samples")), + l => l, + }; + let chunk_spp = if planar { 1 } else { chunk_spp }; + // The sample (chunky) or plane (planar) that holds the alpha: the colour samples come first. + let alpha_sample = out_layout.color_channels() + alpha_at.unwrap_or(0); + if out_layout.has_alpha() && (alpha_sample as u64 >= spp || (!planar && alpha_sample >= chunk_spp)) { + return Err(err("the alpha sample is past the samples of a pixel")); + } + if matches!(src, Src::Bits(_)) && (out_layout.channels() > 1 || spp != 1) && palette.is_none() { + return Err(CodecError::unsupported(F, format!("{bits}-bit samples with {spp} samples per pixel"))); + } + if (!planar && palette.is_none() && chunk_spp < out_layout.channels()) || (planar && out_layout.channels() as u64 > spp) { + return Err(err("fewer samples than the colour type needs")); + } + let out = match (&palette, src) { + (Some(_), Src::U16) => SampleType::U16, + (Some(_), _) => SampleType::U8, + _ => src.out(), + }; + if whole { + limits.check(w, h, out_layout, out)?; + } else { + limits.check_size(w, h)?; + } + // Decompression-bomb guard: no compression method expands data by more than a few thousand + // times (Deflate at most ~1032:1, LZW ~2730:1, PackBits 128:1), so a file far too small for + // its declared size is refused before the image is allocated. CCITT Group 4 codes a blank row + // of a strip or tile in one bit (a V0 code), so its bound is the chunk width (the decoder + // reads at most 65535): a blank landscape A4 page at 600 dpi, 7016 px wide, compresses past + // 4096:1 (Groups 3 and 2 are not decoded). Uncompressed data may be up + // to three quarters missing (that decodes with a `Truncated` warning). Only the bytes the + // strip or tile tables point at inside the file count (and never more than the file), so + // padding the file while pointing the chunks past its end does not raise the bound: the + // banded decoder has no whole-image `max_alloc` to fall back on. + let compression = f.tag_uint(dir, ifd::TAG_COMPRESSION).unwrap_or(1); + let stored = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(spp).saturating_mul(bits).div_ceil(8); + let ratio: u64 = match compression { + 1 => 4, + 32773 => 130, + 4 => cw.clamp(4096, 65_535), + _ => 4096, + }; + let len = f.b.len() as u64; + let (offsets, sizes) = ( + dir.get(offsets_tag).ok_or_else(|| err("missing strip or tile offsets"))?, + dir.get(counts_tag).ok_or_else(|| err("missing strip or tile byte counts"))?, + ); + let mut held = 0u64; + for i in 0..expected { + if held >= len { + break; + } + if let (Some(at), Some(n)) = (f.uint(offsets, i), f.uint(sizes, i)) { + held = held.saturating_add(n.min(len.saturating_sub(at))); + } + } + let held = held.min(len); + if stored > held.saturating_mul(ratio).saturating_add(1 << 20) { + return Err(err(format!("the strips or tiles in this file hold {held} bytes, too few for a {w}x{h} image"))); + } + + let (wu, hu) = (w as usize, h as usize); + let (cw, ch) = (usize::try_from(cw).map_err(|_| err("tile too wide"))?, usize::try_from(ch).map_err(|_| err("tile too tall"))?); + let across = wu.div_ceil(cw); + let down = hu.div_ceil(ch); + let chunks_per_plane = across.checked_mul(down).ok_or_else(|| err("too many tiles"))?; + let planes_in_file = if planar { spp as usize } else { 1 }; + let total = if tiled { dec.tile_count() } else { dec.strip_count() }.map_err(map_err)?; + drop(dec); + if Some(total as usize) != chunks_per_plane.checked_mul(planes_in_file) { + return Err(err("strip or tile count does not match the image size")); + } + // One chunk's decoded bytes, clipped to the image: the scratch buffer may hold a little + // more (the `tiff` crate reads whole tiles in some planar cases) but never an amount that + // only a hostile tile size could ask for. + let chunk_bytes = cw + .min(wu) + .checked_mul(ch.min(hu)) + .and_then(|px| px.checked_mul(chunk_spp)) + .and_then(|s| s.checked_mul(src.bytes())) + .ok_or_else(|| CodecError::LimitExceeded("tile size".into()))?; + let scratch_cap = chunk_bytes.saturating_mul(2).max(64 << 20).min(limits.alloc_usize()); + // One chunk as the decoder actually reads it: a tile keeps its declared size at an edge, + // which can be more than the clipped image bytes above. + let scratch_bytes = cw.saturating_mul(ch).saturating_mul(chunk_spp).saturating_mul(src.bytes()); + let (extra_at, extras_over_limit) = extra_sample_indices(spp as usize, color, alpha_at, out_layout.has_alpha()); + let mut lay = Layout { + w: wu, + src, + out, + out_layout, + chunk_spp, + planar, + planes: if planar { out_layout.channels() } else { 1 }, + cw, + across, + chunks_per_plane, + invert: invert && palette.is_none(), + alpha_sample, + premultiplied: out_layout.has_alpha() && alpha_at.and_then(|a| extra.get(a)) == Some(&1), + palette, + scratch_cap, + extra_at, + }; + // Extra samples share the allocation budget with the image: their interleaved buffer and the + // planes split from it are held next to the image, so both count against `max_alloc`. When + // they don't fit, the image still opens (it passed its own check above) without them. + let mut extras_dropped = 0; + if whole && !lay.extra_at.is_empty() { + let per_pixel = out_layout.channels().saturating_add(lay.extra_at.len().saturating_mul(2)).saturating_mul(out.bytes()); + if limits.check_bytes(w, h, per_pixel as u64).is_err() { + extras_dropped = u32::try_from(lay.extra_at.len()).unwrap_or(u32::MAX); + lay.extra_at.clear(); + } + } + Ok(Prepared { + w, + h, + lay, + band_rows: ch.min(hu), + patches, + tl, + lab: lab.then_some(photometric == 8), + ycbcr: ycbcr.then(|| YCbCr::of(f, dir, compression, if out == SampleType::U16 { 65535.0 } else { 255.0 })), + color, + extra_kinds: extra, + extras_over_limit, + extras_dropped, + scratch_bytes, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EncodeOptions, ExtraSample, Format}; + + /// `TiffBands::open` drops extra samples over the budget only on 32-bit targets (wasm32), + /// so it reports them through the same function as the whole-image decode. + #[test] + fn extra_samples_over_the_budget_are_reported_by_both_decoders() { + // Several strips (about 1 MB each), so one strip of all four samples fits the image's budget. + let (w, h) = (1024u32, 1024u32); + let n = (w * h) as usize; + let mut img = Image::from_raw(w, h, ChannelLayout::Rgb, SampleType::U8, vec![90; 3 * n]).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![7; n] }]; + let bytes = crate::encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let (file, dir, _) = open_page(&bytes, None).unwrap(); + let tight = Limits { max_alloc: 3 * n as u64, ..Limits::default() }; + let p = prepare(&file, &dir, &tight, true).unwrap(); + assert_eq!(prepared_warnings(&p), vec![DecodeWarning::ExtraSamplesDropped { format: F, count: 1 }]); + assert!(prepared_warnings(&prepare(&file, &dir, &tight, false).unwrap()).is_empty(), "banded: only the band counts"); + assert!(prepared_warnings(&prepare(&file, &dir, &Limits::default(), true).unwrap()).is_empty()); + let whole = decode(&bytes, &tight).unwrap(); + assert_eq!(whole.warnings, prepared_warnings(&p)); + } + + /// The scratch bound a whole-image decode reserves: none for chunks decoded in place, one + /// chunk per worker otherwise, never more chunks than the image has bands. + #[test] + fn whole_image_scratch_is_one_chunk_per_worker() { + assert_eq!(scratch_bound(true, 1 << 20, 16, 1024, 8), 0, "strips decoded in place need no scratch"); + assert_eq!(scratch_bound(false, 1 << 20, 16, 1024, 8), 8 << 20, "one strip per worker"); + assert_eq!(scratch_bound(false, 1 << 20, 16, 32, 8), 2 << 20, "never more strips than the image has"); + assert_eq!(scratch_bound(false, 1 << 20, 0, 2, 8), 2 << 20, "a zero band height counts as one row"); + assert_eq!(scratch_bound(false, u64::MAX, 1, 1, 8), u64::MAX, "the bound saturates"); + } +} diff --git a/crates/image-codecs/src/codecs/tiff/encode.rs b/crates/image-codecs/src/codecs/tiff/encode.rs new file mode 100644 index 000000000..18b02a603 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/encode.rs @@ -0,0 +1,659 @@ +use super::bands::convert_ab; +use super::tags::{F, TAG_IMAGE_SOURCE_DATA, TAG_PHOTOSHOP, TAG_XMP, TEXT_TAGS}; +use super::{ + ChannelLayout, CmykA16, CmykA32F, CodecError, Cow, EncodeOptions, ExifDirs, GrayA8, GrayA16, GrayA32F, Image, Lab8, Lab16, LabA8, LabA16, + PhotometricInterpretation, Plan, Rational, ResolutionUnit, SampleFormat, SampleType, Tag, TiffColorType, TiffCompression, TiffEncoder, TiffKind, + TiffKindBig, TiffKindStandard, TiffValue, Type, colortype, io, tiff_exif, +}; +use std::io::Cursor; + +// --------------------------------------------------------------------------- +// Encoding +// --------------------------------------------------------------------------- + +/// Bytes written with TIFF type UNDEFINED (7), as required for ICC. +struct Undefined<'a>(&'a [u8]); + +impl TiffValue for Undefined<'_> { + const BYTE_LEN: u8 = 1; + const FIELD_TYPE: Type = Type::UNDEFINED; + fn count(&self) -> usize { + self.0.len() + } + fn data(&self) -> Cow<'_, [u8]> { + Cow::Borrowed(self.0) + } +} + +// Colour types with 1..=MAX_EXTRA_SAMPLES trailing extra samples (spot channels, extra alpha). +// One generic type per (sample, photometric, bit depth, total sample count) instead of a +// hand-written type for every layout. `N` is the total sample count, not the extra count. +const fn extra_photo(v: u16) -> PhotometricInterpretation { + match v { + 1 => PhotometricInterpretation::BlackIsZero, + 2 => PhotometricInterpretation::RGB, + 5 => PhotometricInterpretation::CMYK, + 8 => PhotometricInterpretation::CIELab, + _ => PhotometricInterpretation::RGB, + } +} + +trait ExtraKind: Copy + TiffValue + 'static { + const FORMAT: SampleFormat; + fn predict(row: &[Self], out: &mut Vec, samples: usize); +} + +impl ExtraKind for u8 { + const FORMAT: SampleFormat = SampleFormat::Uint; + fn predict(row: &[Self], out: &mut Vec, samples: usize) { + let n = samples.min(row.len()); + out.extend_from_slice(&row[..n]); + out.extend(row.iter().zip(&row[n..]).map(|(prev, current)| current.wrapping_sub(*prev))); + } +} + +impl ExtraKind for u16 { + const FORMAT: SampleFormat = SampleFormat::Uint; + fn predict(row: &[Self], out: &mut Vec, samples: usize) { + let n = samples.min(row.len()); + out.extend_from_slice(&row[..n]); + out.extend(row.iter().zip(&row[n..]).map(|(prev, current)| current.wrapping_sub(*prev))); + } +} + +impl ExtraKind for f32 { + const FORMAT: SampleFormat = SampleFormat::IEEEFP; + fn predict(row: &[Self], out: &mut Vec, _samples: usize) { + out.extend_from_slice(row); + } +} + +pub(super) struct Extras(pub(super) std::marker::PhantomData); + +impl TiffColorType for Extras +where + T: ExtraKind, +{ + type Inner = T; + const TIFF_VALUE: PhotometricInterpretation = extra_photo(PHOTO); + const BITS_PER_SAMPLE: &'static [u16] = &[BITS; N]; + const SAMPLE_FORMAT: &'static [SampleFormat] = &[T::FORMAT; N]; + fn horizontal_predict(row: &[Self::Inner], result: &mut Vec) { + T::predict(row, result, N); + } +} + +macro_rules! write_extras { + ($n:expr, $enc:expr, $w:expr, $h:expr, $data:expr, $tags:expr, $sample:ty, $photo:literal, $bits:literal, $base:literal) => { + match $n { + 1 => write_one::, K>($enc, $w, $h, $data, $tags), + 2 => write_one::, K>($enc, $w, $h, $data, $tags), + 3 => write_one::, K>($enc, $w, $h, $data, $tags), + 4 => write_one::, K>($enc, $w, $h, $data, $tags), + 5 => write_one::, K>($enc, $w, $h, $data, $tags), + 6 => write_one::, K>($enc, $w, $h, $data, $tags), + 7 => write_one::, K>($enc, $w, $h, $data, $tags), + 8 => write_one::, K>($enc, $w, $h, $data, $tags), + 9 => write_one::, K>($enc, $w, $h, $data, $tags), + 10 => write_one::, K>($enc, $w, $h, $data, $tags), + 11 => write_one::, K>($enc, $w, $h, $data, $tags), + 12 => write_one::, K>($enc, $w, $h, $data, $tags), + 13 => write_one::, K>($enc, $w, $h, $data, $tags), + 14 => write_one::, K>($enc, $w, $h, $data, $tags), + 15 => write_one::, K>($enc, $w, $h, $data, $tags), + 16 => write_one::, K>($enc, $w, $h, $data, $tags), + other => Err(io::Error::new(io::ErrorKind::InvalidInput, format!("unsupported extra samples ({other})")).into()), + } + }; +} + +/// An empty buffer with room for `len` elements, allocated fallibly: a save too large for memory +/// is an error, not an abort. +pub(super) fn try_with_capacity(len: usize) -> io::Result> { + let mut v = Vec::new(); + v.try_reserve_exact(len).map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, format!("cannot allocate {len} samples for the extra samples")))?; + Ok(v) +} + +fn bytes_as_u16(b: &[u8]) -> io::Result> { + let chunks = b.as_chunks::<2>().0; + let mut v = try_with_capacity(chunks.len())?; + v.extend(chunks.iter().map(|c| u16::from_ne_bytes(*c))); + Ok(v) +} + +fn bytes_as_f32(b: &[u8]) -> io::Result> { + let chunks = b.as_chunks::<4>().0; + let mut v = try_with_capacity(chunks.len())?; + v.extend(chunks.iter().map(|c| f32::from_ne_bytes(*c))); + Ok(v) +} + +/// Interleaves colour (and alpha) samples with [`Metadata::extra_samples`]. +fn interleave_extras(img: &Image) -> Result, String> { + let (w, h) = img.dimensions(); + let n = (w as usize).checked_mul(h as usize).ok_or_else(|| "image size overflows".to_string())?; + let sb = img.sample_type().bytes(); + let ch = img.layout().channels(); + let extras = &img.meta.extra_samples; + let total = ch.checked_add(extras.len()).ok_or_else(|| "too many samples".to_string())?; + let px = total.checked_mul(sb).ok_or_else(|| "sample size overflows".to_string())?; + let len = n.checked_mul(px).ok_or_else(|| "image size overflows".to_string())?; + let mut out = try_with_capacity::(len).map_err(|e| e.to_string())?; + out.resize(len, 0); + let base = img.data(); + let base_px = ch.saturating_mul(sb); + for i in 0..n { + let src = i.saturating_mul(base_px); + let dst = i.saturating_mul(px); + let Some(s) = base.get(src..src.saturating_add(base_px)) else { + return Err("image buffer is shorter than its dimensions".into()); + }; + let Some(d) = out.get_mut(dst..dst.saturating_add(base_px)) else { + return Err("extra-sample buffer is short".into()); + }; + d.copy_from_slice(s); + for (k, e) in extras.iter().enumerate() { + let es = i.saturating_mul(sb); + let ed = dst.saturating_add(base_px).saturating_add(k.saturating_mul(sb)); + let Some(s) = e.data.get(es..es.saturating_add(sb)) else { + return Err(format!("extra sample {k} is shorter than the image")); + }; + let Some(d) = out.get_mut(ed..ed.saturating_add(sb)) else { + return Err("extra-sample buffer is short".into()); + }; + d.copy_from_slice(s); + } + } + Ok(out) +} + +fn write_with_extras(enc: &mut TiffEncoder<&mut Cursor>, K>, img: &Image, tags: &TagSet<'_>) -> tiff::TiffResult<()> { + use ChannelLayout as L; + use SampleType as S; + let (w, h) = img.dimensions(); + let n = img.meta.extra_samples.len(); + if img.layout().is_lab() { + let file = lab_file(img); + let bytes = interleave_extras(&file).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + return match (file.layout(), file.sample_type()) { + (L::Lab, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 8, 8, 3), + (L::Lab, _) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 8, 16, 3) + } + (_, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 8, 8, 4), + _ => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 8, 16, 4) + } + }; + } + let bytes = interleave_extras(img).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + match (img.layout(), img.sample_type()) { + (L::Gray, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 1, 8, 1), + (L::Gray, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 1, 16, 1) + } + (L::Gray, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 1, 32, 1) + } + (L::GrayA, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 1, 8, 2), + (L::GrayA, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 1, 16, 2) + } + (L::GrayA, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 1, 32, 2) + } + (L::Rgb, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 2, 8, 3), + (L::Rgb, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 2, 16, 3) + } + (L::Rgb, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 2, 32, 3) + } + (L::Rgba, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 2, 8, 4), + (L::Rgba, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 2, 16, 4) + } + (L::Rgba, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 2, 32, 4) + } + (L::Cmyk, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 5, 8, 4), + (L::Cmyk, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 5, 16, 4) + } + (L::Cmyk, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 5, 32, 4) + } + (L::CmykA, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 5, 8, 5), + (L::CmykA, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 5, 16, 5) + } + (L::CmykA, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 5, 32, 5) + } + _ => Err(io::Error::new(io::ErrorKind::InvalidInput, format!("unsupported extra samples ({n})")).into()), + } +} + +struct TagSet<'a> { + icc: Option<&'a [u8]>, + /// The EXIF block's directories and IFD0 tags, written as the TIFF's own. + exif: Option, + xmp: Option<&'a [u8]>, + dpi: Option<(f32, f32)>, + text: Vec<(Tag, &'a str)>, + /// ExtraSamples tag, including the unassociated-alpha marker when the image has alpha. + extra_samples: &'a [u16], + photoshop_resources: Option<&'a [u8]>, + photoshop_layers: Option<&'a [u8]>, + /// An image pyramid level: written with `NewSubfileType` 1 (reduced resolution). + reduced: bool, +} + +/// TIFF `NewSubfileType` (254). +const TAG_NEW_SUBFILE_TYPE: u16 = 254; + +/// Writes one image directory. (The `tiff` encoder only compresses through `write_data`, which +/// takes every sample at once.) +fn write_one(enc: &mut TiffEncoder<&mut Cursor>, K>, w: u32, h: u32, data: &[C::Inner], tags: &TagSet<'_>) -> tiff::TiffResult<()> +where + C: TiffColorType, + [C::Inner]: TiffValue, + K: TiffKind, +{ + // The Exif and GPS directories go before the image, outside its chain; IFD0 points to them. + let mut sub = |e: &ExifDirs, fields: &[tiff_exif::Field]| (!fields.is_empty()).then(|| tiff_exif::write_directory(enc, fields, e.le)).transpose(); + let exif_at = match &tags.exif { + Some(e) => sub(e, &e.exif)?, + None => None, + }; + let gps_at = match &tags.exif { + Some(e) => sub(e, &e.gps)?, + None => None, + }; + let mut im = enc.new_image::(w, h)?; + if let Some((x, y)) = tags.dpi { + im.resolution_unit(ResolutionUnit::Inch); + im.x_resolution(Rational { n: (x * 1000.0).round() as u32, d: 1000 }); + im.y_resolution(Rational { n: (y * 1000.0).round() as u32, d: 1000 }); + } + let d = im.encoder(); + if tags.reduced { + d.write_tag(Tag::Unknown(TAG_NEW_SUBFILE_TYPE), 1u32)?; + } + if !tags.extra_samples.is_empty() { + d.write_tag(Tag::ExtraSamples, tags.extra_samples)?; + } + if let Some(icc) = tags.icc { + d.write_tag(Tag::IccProfile, Undefined(icc))?; + } + if let Some(xmp) = tags.xmp { + d.write_tag(Tag::Unknown(TAG_XMP), xmp)?; + } + for (tag, s) in &tags.text { + d.write_tag(*tag, *s)?; + } + if let Some(e) = &tags.exif { + // The EXIF block's descriptive IFD0 tags, unless the text metadata already wrote them. + let ifd0: Vec = e.ifd0.iter().filter(|f| !tags.text.iter().any(|(t, _)| t.to_u16() == f.tag)).cloned().collect(); + tiff_exif::write_fields(d, &ifd0, e.le)?; + } + if let Some(at) = exif_at { + d.write_tag(Tag::Unknown(tiff_exif::TAG_EXIF_IFD), at)?; + } + if let Some(at) = gps_at { + d.write_tag(Tag::Unknown(tiff_exif::TAG_GPS_IFD), at)?; + } + if let Some(r) = tags.photoshop_resources { + d.write_tag(Tag::Unknown(TAG_PHOTOSHOP), r)?; + } + if let Some(l) = tags.photoshop_layers { + d.write_tag(Tag::Unknown(TAG_IMAGE_SOURCE_DATA), Undefined(l))?; + } + im.write_data(data) +} + +/// A Lab image as CIELab stores it: a* and b* signed, 8 or 16 bits (CIELab has no float form: +/// 32-bit Lab is written with 16 bits). Extra samples follow the new sample type. +fn lab_file(img: &Image) -> Image { + let sample = if img.sample_type() == SampleType::U8 { SampleType::U8 } else { SampleType::U16 }; + // `convert` brings the extra samples to the new sample type with the image. + let mut file = img.convert(img.layout(), sample); + let channels = file.layout().channels(); + convert_ab(file.data_mut(), channels, sample, true, true); + file +} + +/// Writes the image with the colour type matching its layout and sample type. +fn write_image(enc: &mut TiffEncoder<&mut Cursor>, K>, img: &Image, tags: &TagSet<'_>) -> tiff::TiffResult<()> { + use ChannelLayout as L; + use SampleType as S; + if !img.meta.extra_samples.is_empty() { + return write_with_extras::(enc, img, tags); + } + let (w, h) = img.dimensions(); + let u16s = || img.to_u16_samples().unwrap_or_default(); + let f32s = || img.to_f32_samples().unwrap_or_default(); + let d8 = img.data(); + match (img.layout(), img.sample_type()) { + (L::Gray, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Gray, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Gray, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::GrayA, S::U8) => write_one::(enc, w, h, d8, tags), + (L::GrayA, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::GrayA, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Rgb, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Rgb, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Rgb, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Rgba, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Rgba, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Rgba, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Cmyk, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Cmyk, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Cmyk, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::CmykA, S::U8) => write_one::(enc, w, h, d8, tags), + (L::CmykA, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::CmykA, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Lab | L::LabA, _) => { + let file = lab_file(img); + let u16s = || file.to_u16_samples().unwrap_or_default(); + match (file.layout(), file.sample_type()) { + (L::Lab, S::U8) => write_one::(enc, w, h, file.data(), tags), + (L::Lab, _) => write_one::(enc, w, h, &u16s(), tags), + (_, S::U8) => write_one::(enc, w, h, file.data(), tags), + _ => write_one::(enc, w, h, &u16s(), tags), + } + } + (l, s) => Err(io::Error::new(io::ErrorKind::InvalidInput, format!("unsupported {l:?} {s:?}")).into()), + } +} + +/// Writes the image pyramid after the full image: each level half the size of the one before +/// (rounded up), colour and alpha only, until the longer side is at most +/// [`crate::TIFF_PYRAMID_MIN`]. An image already that small gets no levels. +fn write_pyramid(enc: &mut TiffEncoder<&mut Cursor>, K>, img: &Image, tags: &TagSet<'_>) -> tiff::TiffResult<()> { + let mut level: Option = None; + loop { + let prev = level.as_ref().unwrap_or(img); + let (w, h) = prev.dimensions(); + if w.max(h) <= crate::TIFF_PYRAMID_MIN { + return Ok(()); + } + let next = half_size(prev).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e.to_string()))?; + write_image::(enc, &next, tags)?; + level = Some(next); + } +} + +/// `img` at half its size, rounded up, by averaging each 2×2 block (an odd last row or column +/// averages what it has). Colour is weighted by alpha, so transparent pixels don't darken +/// the edges of what they surround. Extra samples are not carried over. Rows are worked in +/// parallel on native targets. +pub(super) fn half_size(img: &Image) -> Result { + let (w, h) = img.dimensions(); + let (nw, nh) = (w.div_ceil(2).max(1), h.div_ceil(2).max(1)); + let layout = img.layout(); + let sample = img.sample_type(); + let ch = layout.channels(); + let bps = sample.bytes(); + let src_row = (w as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "image row size overflows"))?; + let out_row = (nw as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "pyramid row size overflows"))?; + let len = out_row.checked_mul(nh as usize).ok_or_else(|| CodecError::encode(F, "pyramid level size overflows"))?; + if img.data().len() < src_row.saturating_mul(h as usize) { + return Err(CodecError::encode(F, "image buffer is shorter than its dimensions")); + } + let mut out = super::bands::zeroed(len)?; + let level = Level { data: img.data(), w: w as usize, h: h as usize, ch, sample, src_row, alpha: layout.has_alpha() }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(out_row.max(1)).enumerate().for_each(|(y, row)| level.row(y, row)); + } + #[cfg(target_arch = "wasm32")] + out.chunks_mut(out_row.max(1)).enumerate().for_each(|(y, row)| level.row(y, row)); + let mut img2 = Image::from_raw(nw, nh, layout, sample, out)?; + img2.icc = img.icc.clone(); + Ok(img2) +} + +/// The source of one [`half_size`] level. +struct Level<'a> { + data: &'a [u8], + w: usize, + h: usize, + ch: usize, + sample: SampleType, + src_row: usize, + /// The last channel is alpha. + alpha: bool, +} + +impl Level<'_> { + /// Source row `y` as numbers (empty past the bottom). + fn samples(&self, y: usize) -> Vec { + let Some(row) = (y < self.h).then(|| self.data.get(y * self.src_row..(y + 1) * self.src_row)).flatten() else { return Vec::new() }; + match self.sample { + SampleType::U8 => row.iter().map(|&v| f64::from(v)).collect(), + SampleType::U16 => row.as_chunks::<2>().0.iter().map(|b| f64::from(u16::from_ne_bytes(*b))).collect(), + SampleType::F32 => row.as_chunks::<4>().0.iter().map(|b| f64::from(f32::from_ne_bytes(*b))).collect(), + SampleType::F16 => row.as_chunks::<2>().0.iter().map(|b| f64::from(half::f16::from_bits(u16::from_ne_bytes(*b)).to_f32())).collect(), + } + } + + /// Writes output row `y` into `out`. + fn row(&self, y: usize, out: &mut [u8]) { + let rows = [self.samples(y * 2), self.samples(y * 2 + 1)]; + let ch = self.ch; + let bps = self.sample.bytes(); + let mut sums = vec![0f64; ch]; + for (x, px) in out.chunks_exact_mut(ch * bps).enumerate() { + sums.iter_mut().for_each(|s| *s = 0.0); + let (mut n, mut weight) = (0f64, 0f64); + for row in rows.iter().filter(|r| !r.is_empty()) { + for sx in [x * 2, x * 2 + 1].into_iter().filter(|&sx| sx < self.w) { + let Some(p) = row.get(sx * ch..(sx + 1) * ch) else { continue }; + let a = if self.alpha { p.last().copied().unwrap_or(1.0) } else { 1.0 }; + for (c, (sum, v)) in sums.iter_mut().zip(p).enumerate() { + *sum += if self.alpha && c + 1 == ch { *v } else { v * a }; + } + n += 1.0; + weight += a; + } + } + for (c, (sum, dst)) in sums.iter().zip(px.chunks_exact_mut(bps)).enumerate() { + let v = if self.alpha && c + 1 == ch { + sum / n.max(1.0) + } else if weight > 0.0 { + sum / weight + } else { + 0.0 + }; + match self.sample { + SampleType::U8 => dst.copy_from_slice(&[v.round().clamp(0.0, 255.0) as u8]), + SampleType::U16 => dst.copy_from_slice(&(v.round().clamp(0.0, 65535.0) as u16).to_ne_bytes()), + SampleType::F32 => dst.copy_from_slice(&(v as f32).to_ne_bytes()), + SampleType::F16 => dst.copy_from_slice(&half::f16::from_f32(v as f32).to_bits().to_ne_bytes()), + } + } + } + } +} + +/// Classic TIFF offsets are 32-bit: a file that may grow past this is written as BigTIFF. +const CLASSIC_LIMIT: u64 = u32::MAX as u64; + +/// An upper estimate of the file size: the uncompressed samples plus every tag payload plus +/// room for the directory and strip tables. +fn estimated_size(img: &Image, tags: &TagSet<'_>) -> u64 { + let payloads = [tags.icc, tags.xmp, tags.photoshop_resources, tags.photoshop_layers].iter().flatten().map(|p| p.len() as u64).sum::(); + let text = tags.text.iter().map(|(_, s)| s.len() as u64).sum::() + + tags.exif.iter().flat_map(|e| e.ifd0.iter().chain(&e.exif).chain(&e.gps)).map(|f| f.value.len() as u64 + 12).sum::(); + // Strip tables: one offset and one count per strip of about 1 MB. + let tables = (img.data().len() as u64 / (1 << 20) + 1) * 16; + let extras: u64 = img.meta.extra_samples.iter().map(|e| e.data.len() as u64).sum(); + (img.data().len() as u64).saturating_add(extras).saturating_add(payloads).saturating_add(text).saturating_add(tables).saturating_add(1 << 20) +} + +/// The byte length of one extra-sample plane: one sample per pixel of `img`, in its sample type. +/// `None` when the pixel count overflows a `usize` (no such image buffer can exist). +fn extra_plane_len(img: &Image) -> Option { + let (w, h) = img.dimensions(); + (w as usize).checked_mul(h as usize)?.checked_mul(img.sample_type().bytes()) +} + +/// Every extra plane must hold exactly one sample per pixel of `img`, in its sample type. +/// +/// The writer copies one sample per pixel out of each plane, so a plane of any other length +/// would be silently truncated (long) or read past its end (short). A sample-type conversion +/// reinterprets the bytes in the new width instead, so an unchecked plane would also change +/// meaning rather than fail. Refuse the image instead of writing either. +fn check_extra_planes(img: &Image) -> Result<(), CodecError> { + let (w, h) = img.dimensions(); + let sample = img.sample_type(); + let plane_len = extra_plane_len(img).ok_or_else(|| CodecError::encode(F, format!("the extra-sample size of a {w}x{h} image overflows")))?; + for (k, e) in img.meta.extra_samples.iter().enumerate() { + if e.data.len() != plane_len { + return Err(CodecError::encode( + F, + format!("extra sample {k} is {} bytes; a {w}x{h} {sample:?} image needs exactly {plane_len} (one sample per pixel)", e.data.len()), + )); + } + } + Ok(()) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + // Check the source planes before `converted`, which brings them to the plan's sample type + // with the image but deliberately leaves a wrong-sized plane alone: past that point a bad + // plane would be reinterpreted in the new width rather than rejected. + check_extra_planes(src)?; + // `converted` brings the extra samples to the plan's sample type with the image. + encode_ready(&src.converted(plan.layout, plan.sample), opts) +} + +fn encode_ready(img: &Image, opts: &EncodeOptions) -> Result, CodecError> { + let compression = match opts.tiff_compression { + TiffCompression::None => tiff::encoder::Compression::Uncompressed, + TiffCompression::Lzw => tiff::encoder::Compression::Lzw, + TiffCompression::Deflate => tiff::encoder::Compression::Deflate(tiff::encoder::DeflateLevel::Balanced), + TiffCompression::PackBits => tiff::encoder::Compression::Packbits, + }; + let predictor = if !img.sample_type().is_float() && matches!(opts.tiff_compression, TiffCompression::Lzw | TiffCompression::Deflate) { + tiff::encoder::Predictor::Horizontal + } else { + tiff::encoder::Predictor::None + }; + let text: Vec<(Tag, &str)> = if opts.embed_metadata { + img.meta + .text + .iter() + .filter_map(|(k, v)| TEXT_TAGS.iter().find(|(_, key)| key.eq_ignore_ascii_case(k)).map(|(t, _)| (*t, v.as_str()))) + .filter(|(_, v)| v.is_ascii() && !v.contains('\0')) + .collect() + } else { + Vec::new() + }; + // TIFF writes no Orientation tag (= 1): keep the XMP from contradicting it. + let xmp = if opts.embed_metadata { img.meta.xmp.as_deref().map(|x| crate::resolution::export_xmp(x, img.meta.dpi)) } else { None }; + if img.meta.extra_samples.len() > crate::MAX_EXTRA_SAMPLES { + return Err(CodecError::encode(F, format!("TIFF stores at most {} extra samples", crate::MAX_EXTRA_SAMPLES))); + } + // Validate again in the sample type the planes now hold: `Image::convert` takes a plane's + // bytes before it rebuilds them, so one it could not rebuild would arrive here empty. The Lab + // path's later re-type through `lab_file` converts a plane only when its length already + // matches the image, which this guarantees. + check_extra_planes(img)?; + let mut kinds = Vec::with_capacity(img.meta.extra_samples.len().saturating_add(1)); + if img.layout().has_alpha() { + kinds.push(2); + } + // TIFF 6.0 reserves ExtraSamples values above 2: those are written as 0 (unspecified). + for e in &img.meta.extra_samples { + kinds.push(if e.kind <= 2 { e.kind } else { 0 }); + } + let tags = TagSet { + icc: if opts.embed_icc { img.icc.as_deref() } else { None }, + exif: if opts.embed_metadata { img.meta.exif.as_deref().and_then(|e| tiff_exif::read(crate::orientation::tiff_body(e))) } else { None }, + xmp: xmp.as_deref().map(str::as_bytes), + dpi: if opts.embed_metadata { img.meta.dpi.filter(|d| d.0 > 0.0 && d.1 > 0.0) } else { None }, + text, + extra_samples: &kinds, + // Document content, not metadata: written whenever present. + photoshop_resources: img.meta.photoshop_resources.as_deref().filter(|r| !r.is_empty()), + photoshop_layers: img.meta.photoshop_layers.as_deref().filter(|l| !l.is_empty()), + reduced: false, + }; + // A pyramid level holds the colour and alpha only, with the profile so it shows the same. + let alpha_kind: &[u16] = if img.layout().has_alpha() { &[2] } else { &[] }; + let level_tags = TagSet { + icc: tags.icc, + exif: None, + xmp: None, + dpi: None, + text: Vec::new(), + extra_samples: alpha_kind, + photoshop_resources: None, + photoshop_layers: None, + reduced: true, + }; + + // BigTIFF when asked, or when the file could pass the 4 GiB a classic TIFF can address. A + // classic write that still runs out of 32-bit offsets (compression grew the data) is redone + // as BigTIFF. + let estimate = estimated_size(img, &tags); + // The pyramid adds at most a third of the image (1/4 + 1/16 + …). + let estimate = if opts.tiff_pyramid { estimate.saturating_add(img.data().len() as u64 / 3) } else { estimate }; + let write = |big: bool| -> tiff::TiffResult> { + // An uncompressed file is allocated once at its estimated size: grown as written, the + // buffer would double near the end and copy the file (#6607). Compressed output is + // usually a fraction of the estimate, so it grows as written instead: reserving the + // estimate would hold a second full-size buffer while encoding, which wasm32 never + // returns and Windows counts as committed memory. + let mut out = Vec::new(); + if matches!(opts.tiff_compression, TiffCompression::None) { + // Best effort: without the room the buffer grows as before. + let _ = out.try_reserve(usize::try_from(estimate).unwrap_or(usize::MAX)); + } + let mut cursor = Cursor::new(out); + if big { + let mut enc = TiffEncoder::new_big(&mut cursor)?.with_compression(compression).with_predictor(predictor); + write_image::(&mut enc, img, &tags)?; + if opts.tiff_pyramid { + write_pyramid::(&mut enc, img, &level_tags)?; + } + } else { + let mut enc = TiffEncoder::new(&mut cursor)?.with_compression(compression).with_predictor(predictor); + write_image::(&mut enc, img, &tags)?; + if opts.tiff_pyramid { + write_pyramid::(&mut enc, img, &level_tags)?; + } + } + // Growth (or an estimate that was too high) can leave spare capacity: give it back + // instead of holding it while the file is saved. + let mut out = cursor.into_inner(); + if out.capacity() - out.len() > out.len() / 8 { + out.shrink_to_fit(); + } + Ok(out) + }; + let big = opts.tiff_bigtiff || estimate > CLASSIC_LIMIT; + match write(big) { + Ok(v) => Ok(v), + Err(tiff::TiffError::IntSizeError) if !big => write(true).map_err(|e| CodecError::encode(F, e)), + Err(e) => Err(CodecError::encode(F, e)), + } +} diff --git a/crates/image-codecs/src/codecs/tiff/tags.rs b/crates/image-codecs/src/codecs/tiff/tags.rs new file mode 100644 index 000000000..a41b15413 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/tags.rs @@ -0,0 +1,99 @@ +use super::{Format, Tag}; + +pub(super) const F: Format = Format::Tiff; + +pub(super) const TAG_XMP: u16 = 700; + +/// Photoshop image resources (`8BIM` blocks). +pub(super) const TAG_PHOTOSHOP: u16 = 34377; + +/// Photoshop `ImageSourceData`: the layers of a layered TIFF. +pub(super) const TAG_IMAGE_SOURCE_DATA: u16 = 37724; + +/// `true` when [`encode`] writes little-endian (`II`) files: the `tiff` encoder writes the +/// running target's byte order, and every target PhotoCraft builds for is little-endian. +/// Byte-order-sensitive payloads (Photoshop layer data) must be produced to match. +pub fn writes_little_endian() -> bool { + cfg!(target_endian = "little") +} + +/// `(34377 image resources, 37724 image source data)` payloads, each absent when the tag is. +pub type PhotoshopTags<'a> = (Option<&'a [u8]>, Option<&'a [u8]>); + +/// The raw payloads of the Photoshop private tags in the first directory: `(34377 image +/// resources, 37724 image source data)`. Classic and BigTIFF, either byte order; only +/// header bytes and the two payloads are touched, and nothing is allocated. Anything malformed +/// reads as "absent". +pub fn photoshop_tags(b: &[u8]) -> PhotoshopTags<'_> { + fn walk(b: &[u8]) -> Option> { + let le = match b.get(0..2)? { + b"II" => true, + b"MM" => false, + _ => return None, + }; + let u16_at = |o: u64| -> Option { + let o = usize::try_from(o).ok()?; + let s = b.get(o..o.checked_add(2)?)?; + Some(if le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + }; + let u32_at = |o: u64| -> Option { + let o = usize::try_from(o).ok()?; + let s: [u8; 4] = b.get(o..o.checked_add(4)?)?.try_into().ok()?; + Some(if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) }) + }; + let u64_at = |o: u64| -> Option { + let o = usize::try_from(o).ok()?; + let s: [u8; 8] = b.get(o..o.checked_add(8)?)?.try_into().ok()?; + Some(if le { u64::from_le_bytes(s) } else { u64::from_be_bytes(s) }) + }; + let (big, ifd) = match u16_at(2)? { + 42 => (false, u64::from(u32_at(4)?)), + 43 => (true, u64_at(8)?), + _ => return None, + }; + let n = if big { u64_at(ifd)? } else { u64::from(u16_at(ifd)?) }.min(4096); + let (entry, first, inline) = if big { (20u64, ifd.checked_add(8)?, 8u64) } else { (12, ifd.checked_add(2)?, 4) }; + let mut resources = None; + let mut layers = None; + for i in 0..n { + let e = first.checked_add(i.checked_mul(entry)?)?; + let tag = u16_at(e)?; + if tag != TAG_PHOTOSHOP && tag != TAG_IMAGE_SOURCE_DATA { + continue; + } + // BYTE, ASCII, SBYTE or UNDEFINED: one byte per element. + if !matches!(u16_at(e + 2)?, 1 | 2 | 6 | 7) { + continue; + } + let count = if big { u64_at(e + 4)? } else { u64::from(u32_at(e + 4)?) }; + let value_at = e + if big { 12 } else { 8 }; + let start = if count <= inline { + value_at + } else if big { + u64_at(value_at)? + } else { + u64::from(u32_at(value_at)?) + }; + let start = usize::try_from(start).ok()?; + let data = b.get(start..start.checked_add(usize::try_from(count).ok()?)?)?; + if tag == TAG_PHOTOSHOP { + resources = Some(data); + } else { + layers = Some(data); + } + } + Some((resources, layers)) + } + walk(b).unwrap_or((None, None)) +} + +/// ASCII tags mapped to `Metadata::text` keys. +pub(super) const TEXT_TAGS: &[(Tag, &str)] = &[ + (Tag::ImageDescription, "Description"), + (Tag::Make, "Make"), + (Tag::Model, "Model"), + (Tag::Software, "Software"), + (Tag::DateTime, "DateTime"), + (Tag::Artist, "Artist"), + (Tag::Copyright, "Copyright"), +]; diff --git a/crates/image-codecs/src/codecs/tiff/ycbcr.rs b/crates/image-codecs/src/codecs/tiff/ycbcr.rs new file mode 100644 index 000000000..2983a8050 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/ycbcr.rs @@ -0,0 +1,222 @@ +//! YCbCr (PhotometricInterpretation 6) to RGB, after the `tiff` crate has decoded the samples. +//! +//! The crate decompresses JPEG data and upsamples subsampled chroma, but hands back the stored +//! Y, Cb and Cr values. This module turns them into RGB in place: ReferenceBlackWhite scales the +//! codes and YCbCrCoefficients gives the luma weights. JPEG-compressed data (compression 7) uses +//! the JPEG (JFIF) conversion, full range with the BT.601 weights, and ignores both tags, as +//! libtiff does when it asks the JPEG library for RGB. +//! +//! # Sources +//! +//! - TIFF Revision 6.0 (Final, June 3, 1992), Section 21 "YCbCr Images", pp. 89–92; the +//! YCbCrCoefficients field and its inverse equations are on pp. 90–91: +//! `R = Cr·(2 - 2·LumaRed) + Y`, `B = Cb·(2 - 2·LumaBlue) + Y`, +//! `G = (Y - LumaBlue·B - LumaRed·R) / LumaGreen`. +//! +//! - libtiff, `libtiff/tif_color.c`: `TIFFYCbCrToRGBInit` (tables) and `TIFFYCbCrtoRGB` (per pixel). +//! +//! +//! # Green: a compatibility choice +//! +//! Red and blue follow the spec. Green follows libtiff, which rewrites the spec's equation as +//! `G = Y - (LumaRed·(2 - 2·LumaRed) / LumaGreen)·Cr - (LumaBlue·(2 - 2·LumaBlue) / LumaGreen)·Cb`, +//! adding Y unscaled. The two agree only when the weights sum to 1. Every standard set does +//! (the spec's own examples, BT.601 and BT.709), but the spec only calls the weights "the +//! proportions of red, green, and blue respectively in luminance" and does not require the sum. +//! For other weights the spec's equation scales Y in green by `(1 - LumaRed - LumaBlue) / LumaGreen`, +//! which tints neutral grays. +//! +//! PhotoCraft deliberately follows libtiff here (decided in PR #10319, for issue #10316), so such +//! files open as they do in libtiff-based tools (ImageMagick, GIMP) and grays stay neutral. This is +//! a contributor choice, not a requirement of the spec; the test +//! `weights_that_do_not_sum_to_one_keep_grays_neutral` in `tests/it/tiff_ycbcr.rs` pins it. + +use super::{File, Ifd, SampleType}; + +const TAG_YCBCR_COEFFICIENTS: u16 = 529; +const TAG_REFERENCE_BLACK_WHITE: u16 = 532; + +/// BT.601 luma weights: the TIFF default for YCbCrCoefficients. +const BT601: [f64; 3] = [0.299, 0.587, 0.114]; + +/// How one file's YCbCr codes map to RGB. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(super) struct YCbCr { + /// Luma weights of red, green and blue. + luma: [f64; 3], + /// Black and white codes of Y, Cb and Cr. + ref_bw: [f64; 6], +} + +impl YCbCr { + /// The conversion for directory `dir`, whose samples have `max` as their largest code + /// (255 or 65535). Missing or unusable tags fall back to the TIFF defaults. + pub(super) fn of(f: &File<'_>, dir: &Ifd, compression: u64, max: f64) -> Self { + let defaults = Self::defaults(max); + if compression == 7 { + return defaults; + } + let luma = f + .tag_f64s(dir, TAG_YCBCR_COEFFICIENTS, 3) + .and_then(|v| <[f64; 3]>::try_from(v).ok()) + .filter(|l| l.iter().all(|c| c.is_finite() && *c > 0.0 && *c < 1.0)) + .unwrap_or(defaults.luma); + let ref_bw = f + .tag_f64s(dir, TAG_REFERENCE_BLACK_WHITE, 6) + .and_then(|v| <[f64; 6]>::try_from(v).ok()) + .filter(|r| r.iter().all(|v| v.is_finite()) && r.as_chunks::<2>().0.iter().all(|[black, white]| (white - black).abs() > f64::EPSILON)) + .unwrap_or(defaults.ref_bw); + YCbCr { luma, ref_bw } + } + + /// TIFF's defaults: BT.601 weights, full-range luma, chroma centred on the middle code. + fn defaults(max: f64) -> Self { + let mid = (max + 1.0) / 2.0; + YCbCr { luma: BT601, ref_bw: [0.0, max, mid, max, mid, max] } + } + + /// Converts interleaved Y, Cb, Cr pixels of `channels` samples (the first three are the + /// colour) to R, G, B in place. Other sample types are left as they are. + pub(super) fn convert(self, data: &mut [u8], channels: usize, sample: SampleType) { + match sample { + SampleType::U8 => { + let t = Tables::new(&self, 255); + for_each_pixel(data, channels, |px| { + if let [y, cb, cr, ..] = px { + let [r, g, b] = t.rgb(usize::from(*y), usize::from(*cb), usize::from(*cr)); + (*y, *cb, *cr) = (r as u8, g as u8, b as u8); + } + }); + } + SampleType::U16 => { + let t = Tables::new(&self, 65535); + for_each_pixel(data, channels * 2, |px| { + if let [y0, y1, b0, b1, r0, r1, ..] = px { + let get = |a: u8, b: u8| usize::from(u16::from_ne_bytes([a, b])); + let [r, g, b] = t.rgb(get(*y0, *y1), get(*b0, *b1), get(*r0, *r1)); + [*y0, *y1] = (r as u16).to_ne_bytes(); + [*b0, *b1] = (g as u16).to_ne_bytes(); + [*r0, *r1] = (b as u16).to_ne_bytes(); + } + }); + } + _ => {} + } + } +} + +/// Runs `f` on every `stride`-byte pixel, in parallel on native targets. +fn for_each_pixel(data: &mut [u8], stride: usize, f: impl Fn(&mut [u8]) + Sync + Send) { + if stride == 0 { + return; + } + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + // Bands of whole pixels, so each task has a useful amount of work. + let band = stride.saturating_mul(1 << 14); + data.par_chunks_mut(band).for_each(|b| b.chunks_exact_mut(stride).for_each(&f)); + } + #[cfg(target_arch = "wasm32")] + data.chunks_exact_mut(stride).for_each(f); +} + +/// Per-code contributions, so a pixel costs a few additions. +struct Tables { + max: f64, + /// Scaled luma of each Y code. + y: Vec, + /// Red and green contributions of each Cr code. + cr_r: Vec, + cr_g: Vec, + /// Green and blue contributions of each Cb code. + cb_g: Vec, + cb_b: Vec, +} + +impl Tables { + fn new(c: &YCbCr, max: u32) -> Self { + let m = f64::from(max); + let [lr, lg, lb] = c.luma; + let [y_black, y_white, cb_black, cb_white, cr_black, cr_white] = c.ref_bw; + // TIFF 6.0: chroma codes span ±(max + 1) / 2 - 1 around the black code; with the + // default ReferenceBlackWhite that is the code minus the middle code. + let half = (m + 1.0) / 2.0 - 1.0; + let codes = || (0..=max).map(f64::from); + let y = codes().map(|v| (v - y_black) * m / (y_white - y_black)).collect(); + let cb: Vec = codes().map(|v| (v - cb_black) * half / (cb_white - cb_black)).collect(); + let cr: Vec = codes().map(|v| (v - cr_black) * half / (cr_white - cr_black)).collect(); + // R = Y + (2 - 2·Lr)·Cr and B = Y + (2 - 2·Lb)·Cb (TIFF 6.0 p. 91); green as libtiff: + // G = Y - (Lr·(2 - 2·Lr) / Lg)·Cr - (Lb·(2 - 2·Lb) / Lg)·Cb (see the module notes). + let (kr, kb) = (2.0 - 2.0 * lr, 2.0 - 2.0 * lb); + Tables { + max: m, + y, + cr_r: cr.iter().map(|v| kr * v).collect(), + cr_g: cr.iter().map(|v| -lr * kr * v / lg).collect(), + cb_g: cb.iter().map(|v| -lb * kb * v / lg).collect(), + cb_b: cb.iter().map(|v| kb * v).collect(), + } + } + + /// R, G and B of one pixel, rounded and clamped to the code range. + fn rgb(&self, y: usize, cb: usize, cr: usize) -> [u32; 3] { + let at = |t: &[f64], i: usize| t.get(i).copied().unwrap_or(0.0); + let yv = at(&self.y, y); + // Green adds Y unscaled, as libtiff does (see "Green: a compatibility choice" above). + let r = yv + at(&self.cr_r, cr); + let g = yv + at(&self.cr_g, cr) + at(&self.cb_g, cb); + let b = yv + at(&self.cb_b, cb); + [r, g, b].map(|v| v.round().clamp(0.0, self.max) as u32) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn convert(c: &YCbCr, px: [u8; 3]) -> [u8; 3] { + let mut d = px.to_vec(); + c.convert(&mut d, 3, SampleType::U8); + [d[0], d[1], d[2]] + } + + #[test] + fn jfif_conversion_matches_known_colours() { + let c = YCbCr::defaults(255.0); + assert_eq!(convert(&c, [0, 128, 128]), [0, 0, 0]); + assert_eq!(convert(&c, [255, 128, 128]), [255, 255, 255]); + assert_eq!(convert(&c, [128, 128, 128]), [128, 128, 128]); + // Pure red, green and blue as JFIF encodes them. + assert_eq!(convert(&c, [76, 85, 255]), [254, 0, 0]); + assert_eq!(convert(&c, [150, 44, 21]), [0, 255, 1]); + assert_eq!(convert(&c, [29, 255, 107]), [0, 0, 254]); + } + + #[test] + fn sixteen_bit_and_extra_samples() { + let c = YCbCr::defaults(65535.0); + let mut d: Vec = [65535u16, 32768, 32768, 1234].iter().flat_map(|v| v.to_ne_bytes()).collect(); + c.convert(&mut d, 4, SampleType::U16); + let v: Vec = d.as_chunks::<2>().0.iter().map(|b| u16::from_ne_bytes(*b)).collect(); + assert_eq!(v, [65535, 65535, 65535, 1234]); + } + + #[test] + fn studio_range_reference_black_white() { + // Y 16–235, chroma 128 ± 112 (ITU-R BT.601 studio range). + let c = YCbCr { luma: BT601, ref_bw: [16.0, 235.0, 128.0, 240.0, 128.0, 240.0] }; + assert_eq!(convert(&c, [16, 128, 128]), [0, 0, 0]); + assert_eq!(convert(&c, [235, 128, 128]), [255, 255, 255]); + } + + #[test] + fn odd_buffers_do_not_panic() { + let c = YCbCr::defaults(255.0); + let mut d = vec![1u8, 2]; + c.convert(&mut d, 3, SampleType::U8); + c.convert(&mut d, 0, SampleType::U8); + c.convert(&mut d, 0, SampleType::U16); + assert_eq!(d, [1, 2]); + } +} diff --git a/crates/image-codecs/src/codecs/tiff_exif.rs b/crates/image-codecs/src/codecs/tiff_exif.rs new file mode 100644 index 000000000..2e6885b6e --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff_exif.rs @@ -0,0 +1,238 @@ +//! EXIF in a TIFF file: the Exif (34665) and GPS (34853) directories that IFD0 points to, plus +//! IFD0's descriptive tags (make, model, software, artist, …), the way cameras and Photoshop +//! write them. +//! +//! An EXIF block (`Metadata::exif`) is itself a small TIFF stream, so both directions go through +//! the same reader ([`super::tiff_ifd::File`]): reading a TIFF builds a block from the file's own +//! directories, writing one copies the block's directories into the file. Values are copied as +//! bytes (re-ordered when the byte orders differ), so tags PhotoCraft doesn't know survive. +//! Classic TIFF only on the reading side: an EXIF block has 4-byte offsets. What is copied is +//! bounded by the stream's own size and a fixed cap, because entries of a hostile file can +//! overlap (see [`read`]). + +use tiff::encoder::{DirectoryEncoder, TiffEncoder, TiffKind}; +use tiff::tags::{Tag, Type}; +use tiff::{Directory, TiffResult}; + +use super::tiff_ifd::{File, Ifd}; + +/// IFD0's pointer to the Exif directory. +pub(crate) const TAG_EXIF_IFD: u16 = 34665; +/// IFD0's pointer to the GPS directory. +pub(crate) const TAG_GPS_IFD: u16 = 34853; +/// The Exif directory's PixelXDimension and PixelYDimension: the size of the image the +/// directory describes. +const TAG_PIXEL_X_DIMENSION: u16 = 40962; +const TAG_PIXEL_Y_DIMENSION: u16 = 40963; +/// The Exif directory's pointer to the Interoperability directory (not carried: it holds only +/// the interoperability index, and a copied pointer would point nowhere). +const TAG_INTEROP_IFD: u16 = 40965; +/// IFD0 tags an EXIF block carries next to its directories: ImageDescription, Make, Model, +/// Software, DateTime, Artist, Copyright. Structure, resolution and orientation belong to the +/// image and are written from it. +const IFD0_TAGS: [u16; 7] = [270, 271, 272, 305, 306, 315, 33432]; +/// At most this many entries are copied from one directory. +const MAX_FIELDS: usize = 1024; +/// At most this many value bytes are copied from one stream, over all its directories. Real EXIF +/// is far smaller (the largest Nikon maker note seen is under 1 MB); the cap leaves room for +/// that and for a few such notes. A stream never yields more than its own length either (see +/// [`read`]). +const MAX_COPIED_BYTES: usize = 4 * 1024 * 1024; + +/// One entry with its value bytes in the byte order of the stream it came from. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct Field { + pub tag: u16, + pub ty: u16, + pub count: u32, + pub value: Vec, +} + +/// The EXIF parts of a TIFF stream, in its byte order (`le`). +#[derive(Clone, Debug, Default, PartialEq)] +pub(crate) struct ExifDirs { + pub le: bool, + pub ifd0: Vec, + pub exif: Vec, + pub gps: Vec, +} + +impl ExifDirs { + /// Whether there is an Exif or GPS directory (IFD0's descriptive tags alone are not EXIF). + pub fn has_directories(&self) -> bool { + !self.exif.is_empty() || !self.gps.is_empty() + } +} + +/// The entries of `ifd` that `keep` accepts, with their values copied. An entry's value only has +/// to lie inside the file, so entries may overlap: `budget` is the number of value bytes that may +/// still be copied (shared by all the directories of one stream), and an entry whose value does +/// not fit in what is left is skipped, so later, smaller entries can still be kept. +fn fields(f: &File<'_>, ifd: &Ifd, budget: &mut usize, keep: impl Fn(u16) -> bool) -> Vec { + let mut out = Vec::new(); + for e in ifd.entries.iter().filter(|e| keep(e.tag) && !matches!(e.tag, TAG_EXIF_IFD | TAG_GPS_IFD | TAG_INTEROP_IFD)) { + if out.len() >= MAX_FIELDS { + break; + } + let (Ok(count), Some(value)) = (u32::try_from(e.count), f.data(e)) else { continue }; + let Some(left) = budget.checked_sub(value.len()) else { continue }; + *budget = left; + out.push(Field { tag: e.tag, ty: e.ty, count, value: value.to_vec() }); + } + out +} + +fn sub(f: &File<'_>, ifd: &Ifd, tag: u16) -> Option { + let at = f.tag_uint(ifd, tag)?; + (at != ifd.at).then(|| f.ifd(at)).flatten() +} + +/// The EXIF parts of the classic TIFF stream `b` (a TIFF file, or an EXIF block without its +/// `Exif\0\0` prefix): IFD0's descriptive tags and the Exif and GPS directories. +/// +/// The values copied add up to at most [`MAX_COPIED_BYTES`] and to at most `b.len()`: the values +/// of a well-formed stream are distinct bytes of it, while a hostile one can have many entries +/// point at the same bytes and so ask for far more than its size. Entries that do not fit in what +/// is left are left out (the small descriptive directories come first, so a huge maker note +/// cannot crowd out the GPS position). +pub(crate) fn read(b: &[u8]) -> Option { + read_file(File::parse(b)?) +} + +/// The EXIF parts of a camera raw built on TIFF (NEF, ARW, CR2, DNG, Panasonic RW2), as +/// [`read`] gives them, without the Exif directory's PixelXDimension and PixelYDimension: in a +/// raw they give the sensor's or the preview's size, not the developed image's. +pub(crate) fn read_raw(b: &[u8]) -> Option { + let mut dirs = read_file(File::parse_raw(b)?)?; + dirs.exif.retain(|f| !matches!(f.tag, TAG_PIXEL_X_DIMENSION | TAG_PIXEL_Y_DIMENSION)); + Some(dirs) +} + +fn read_file(f: File<'_>) -> Option { + let b = f.b; + if f.big { + return None; + } + let ifd0 = f.ifd(f.first_ifd)?; + let mut budget = MAX_COPIED_BYTES.min(b.len()); + let ifd0_fields = fields(&f, &ifd0, &mut budget, |t| IFD0_TAGS.contains(&t)); + let mut dir = |tag: u16| sub(&f, &ifd0, tag).map(|d| fields(&f, &d, &mut budget, |_| true)).unwrap_or_default(); + let gps = dir(TAG_GPS_IFD); + let exif = dir(TAG_EXIF_IFD); + Some(ExifDirs { le: f.le, ifd0: ifd0_fields, exif, gps }) +} + +/// Bytes per element whose order a byte-order change reverses: a RATIONAL is two LONGs. +fn element(ty: u16) -> usize { + match ty { + 3 | 8 => 2, + 4 | 5 | 9 | 10 | 11 | 13 => 4, + 12 | 16 | 17 | 18 => 8, + _ => 1, + } +} + +/// `value` of type `ty` in byte order `to_le`, from `from_le`. +pub(crate) fn reorder(ty: u16, value: &[u8], from_le: bool, to_le: bool) -> Vec { + let n = element(ty); + if from_le == to_le || n == 1 { + return value.to_vec(); + } + value.chunks(n).flat_map(|c| c.iter().rev().copied()).collect() +} + +/// An EXIF block: a classic TIFF stream in `dirs.le` order whose IFD0 holds `dirs.ifd0` and +/// points to the Exif and GPS directories. +pub(crate) fn block(dirs: &ExifDirs) -> Vec { + let le = dirs.le; + let u16b = |v: u16| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let u32b = |v: u32| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let mut ifd0: Vec = dirs.ifd0.clone(); + // Placeholders for the pointers; their offsets are known once the layout is. + let pointer = |tag: u16| Field { tag, ty: 4, count: 1, value: vec![0; 4] }; + if !dirs.exif.is_empty() { + ifd0.push(pointer(TAG_EXIF_IFD)); + } + if !dirs.gps.is_empty() { + ifd0.push(pointer(TAG_GPS_IFD)); + } + // Layout: header, then each directory followed by its out-of-line values. + let size = |fs: &[Field]| 2 + 12 * fs.len() + 4 + fs.iter().filter(|f| f.value.len() > 4).map(|f| f.value.len().next_multiple_of(2)).sum::(); + let at0 = 8; + let at_exif = at0 + size(&ifd0); + let at_gps = at_exif + if dirs.exif.is_empty() { 0 } else { size(&dirs.exif) }; + for f in &mut ifd0 { + match f.tag { + TAG_EXIF_IFD => f.value = u32b(at_exif as u32).to_vec(), + TAG_GPS_IFD => f.value = u32b(at_gps as u32).to_vec(), + _ => {} + } + } + let mut out = Vec::new(); + out.extend_from_slice(if le { b"II" } else { b"MM" }); + out.extend_from_slice(&u16b(42)); + out.extend_from_slice(&u32b(at0 as u32)); + let write_dir = |out: &mut Vec, fs: &[Field]| { + let mut fs = fs.to_vec(); + fs.sort_by_key(|f| f.tag); + let start = out.len(); + let mut extra = start + 2 + 12 * fs.len() + 4; + let mut values = Vec::new(); + out.extend_from_slice(&u16b(fs.len() as u16)); + for f in &fs { + out.extend_from_slice(&u16b(f.tag)); + out.extend_from_slice(&u16b(f.ty)); + out.extend_from_slice(&u32b(f.count)); + if f.value.len() <= 4 { + let mut inline = f.value.clone(); + inline.resize(4, 0); + out.extend_from_slice(&inline); + } else { + out.extend_from_slice(&u32b(extra as u32)); + values.extend_from_slice(&f.value); + if f.value.len() % 2 == 1 { + values.push(0); + } + extra += f.value.len().next_multiple_of(2); + } + } + out.extend_from_slice(&u32b(0)); + out.extend_from_slice(&values); + }; + write_dir(&mut out, &ifd0); + if !dirs.exif.is_empty() { + write_dir(&mut out, &dirs.exif); + } + if !dirs.gps.is_empty() { + write_dir(&mut out, &dirs.gps); + } + out +} + +/// Writes `fields` (in byte order `from_le`) into a directory of `enc` that is not part of the +/// page chain, and returns its offset for the pointer tag. +pub(crate) fn write_directory( + enc: &mut TiffEncoder, + fields: &[Field], + from_le: bool, +) -> TiffResult { + let mut sub = enc.extra_directory()?; + write_fields(&mut sub, fields, from_le)?; + Ok(sub.finish_with_offsets()?.offset) +} + +/// Adds `fields` (in byte order `from_le`) to directory `d`, in the file's (native) byte order. +pub(crate) fn write_fields( + d: &mut DirectoryEncoder<'_, W, K>, + fields: &[Field], + from_le: bool, +) -> TiffResult<()> { + let mut dir = Directory::empty(); + for f in fields { + let Some(ty) = Type::from_u16(f.ty) else { continue }; + let entry = d.write_entry_bytes(ty, &reorder(f.ty, &f.value, from_le, cfg!(target_endian = "little")))?; + dir.extend([(Tag::from_u16_exhaustive(f.tag), entry)]); + } + d.extend_from(&dir); + Ok(()) +} diff --git a/crates/image-codecs/src/codecs/tiff_ifd.rs b/crates/image-codecs/src/codecs/tiff_ifd.rs new file mode 100644 index 000000000..8fac8c6b4 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff_ifd.rs @@ -0,0 +1,502 @@ +//! A bounds-checked reader for the TIFF directory structure: classic TIFF (version 42, 4-byte +//! offsets) and BigTIFF (version 43, 8-byte offsets and the LONG8 / SLONG8 / IFD8 types), in +//! either byte order. +//! +//! Every offset and count comes from the file and is untrusted: reads go through `get` with +//! checked arithmetic, directory tables must lie wholly inside the file, the IFD chain and the +//! SubIFD trees (tag 330) are walked with a seen-set, a depth limit and a cap on the number of +//! directories, and nothing is allocated in proportion to a declared count before the bytes +//! behind it are known to exist. +//! +//! This module lists the pages of a file ([`TiffInfo`]) and reads the metadata of a directory +//! without decoding pixels; `tiff.rs` decodes the pixels of one directory. + +use std::collections::HashSet; + +use crate::error::CodecError; +use crate::format::Format; + +/// At most this many directories are listed (main chain and SubIFDs together). +pub(crate) const MAX_IFDS: usize = 10_000; +/// Entries per directory: the classic format's own limit (a `u16` count), applied to BigTIFF too. +const MAX_ENTRIES: u64 = u16::MAX as u64; +/// SubIFD nesting depth that is followed (real files use one level). +const MAX_SUB_DEPTH: usize = 4; +/// SubIFD offsets followed per directory. +const MAX_SUBIFDS: u64 = 256; + +pub(crate) const TAG_NEW_SUBFILE_TYPE: u16 = 254; +pub(crate) const TAG_SUBFILE_TYPE: u16 = 255; +pub(crate) const TAG_WIDTH: u16 = 256; +pub(crate) const TAG_HEIGHT: u16 = 257; +pub(crate) const TAG_BITS: u16 = 258; +pub(crate) const TAG_COMPRESSION: u16 = 259; +pub(crate) const TAG_PHOTOMETRIC: u16 = 262; +pub(crate) const TAG_ORIENTATION: u16 = 274; +pub(crate) const TAG_SAMPLES: u16 = 277; +pub(crate) const TAG_ROWS_PER_STRIP: u16 = 278; +pub(crate) const TAG_PLANAR: u16 = 284; +pub(crate) const TAG_COLOR_MAP: u16 = 320; +pub(crate) const TAG_TILE_WIDTH: u16 = 322; +pub(crate) const TAG_TILE_LENGTH: u16 = 323; +pub(crate) const TAG_TILE_OFFSETS: u16 = 324; +pub(crate) const TAG_SUB_IFDS: u16 = 330; +pub(crate) const TAG_EXTRA_SAMPLES: u16 = 338; +pub(crate) const TAG_SAMPLE_FORMAT: u16 = 339; + +/// Tags the `tiff` crate reads to set up decoding. Their values must lie inside the file, so a +/// hostile count can neither make it allocate nor read past the end. +pub(crate) const STRUCTURAL_TAGS: &[u16] = &[256, 257, 258, 259, 262, 273, 277, 278, 279, 284, 317, 322, 323, 324, 325, 338, 339, 347, 530]; + +/// Bytes per value of a TIFF field type; `None` for unknown types (whose entries are skipped). +fn type_size(ty: u16) -> Option { + Some(match ty { + 1 | 2 | 6 | 7 => 1, // BYTE, ASCII, SBYTE, UNDEFINED + 3 | 8 => 2, // SHORT, SSHORT + 4 | 9 | 11 | 13 => 4, // LONG, SLONG, FLOAT, IFD + 5 | 10 | 12 | 16 | 17 | 18 => 8, // RATIONAL, SRATIONAL, DOUBLE, LONG8, SLONG8, IFD8 + _ => return None, + }) +} + +/// One directory entry, located but not read. +#[derive(Clone, Copy, Debug)] +pub(crate) struct Entry { + pub tag: u16, + pub ty: u16, + pub count: u64, + /// Position of the value: inside the entry when it fits, else the stored offset. + pub value_at: u64, +} + +/// One directory: its entries in file order and the next-IFD offset (0 at the end of a chain). +#[derive(Clone, Debug)] +pub(crate) struct Ifd { + pub at: u64, + pub entries: Vec, + pub next: u64, + /// Position of the next-IFD pointer field. + pub next_at: u64, +} + +impl Ifd { + /// The first entry with this tag. + pub fn get(&self, tag: u16) -> Option<&Entry> { + self.entries.iter().find(|e| e.tag == tag) + } +} + +/// A TIFF or BigTIFF file's header and bytes. +#[derive(Clone, Copy)] +pub(crate) struct File<'a> { + pub b: &'a [u8], + pub le: bool, + pub big: bool, + pub first_ifd: u64, +} + +impl<'a> File<'a> { + /// The header, or `None` when this is not a (Big)TIFF. + pub fn parse(b: &'a [u8]) -> Option { + let le = match b.get(0..2)? { + b"II" => true, + b"MM" => false, + _ => return None, + }; + let mut f = File { b, le, big: false, first_ifd: 0 }; + match f.u16_at(2)? { + 42 => f.first_ifd = u64::from(f.u32_at(4)?), + 43 => { + // Bytesize of offsets (always 8) and a reserved zero. + if f.u16_at(4)? != 8 || f.u16_at(6)? != 0 { + return None; + } + f.big = true; + f.first_ifd = f.u64_at(8)?; + } + _ => return None, + } + Some(f) + } + + /// [`parse`](Self::parse), also taking a Panasonic RW2 raw, whose classic little-endian TIFF + /// header has 0x55 (`IIU\0`) in place of 42. + pub fn parse_raw(b: &'a [u8]) -> Option { + if let Some(f) = Self::parse(b) { + return Some(f); + } + let mut f = File { b, le: true, big: false, first_ifd: 0 }; + if b.get(0..2)? != b"II" || f.u16_at(2)? != 0x55 { + return None; + } + f.first_ifd = u64::from(f.u32_at(4)?); + Some(f) + } + + /// Length of the header; no directory can start inside it. + pub fn header_len(&self) -> u64 { + if self.big { 16 } else { 8 } + } + + fn slice(&self, at: u64, len: u64) -> Option<&'a [u8]> { + let at = usize::try_from(at).ok()?; + let len = usize::try_from(len).ok()?; + self.b.get(at..at.checked_add(len)?) + } + + pub fn u16_at(&self, at: u64) -> Option { + let s: [u8; 2] = self.slice(at, 2)?.try_into().ok()?; + Some(if self.le { u16::from_le_bytes(s) } else { u16::from_be_bytes(s) }) + } + + pub fn u32_at(&self, at: u64) -> Option { + let s: [u8; 4] = self.slice(at, 4)?.try_into().ok()?; + Some(if self.le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) }) + } + + pub fn u64_at(&self, at: u64) -> Option { + let s: [u8; 8] = self.slice(at, 8)?.try_into().ok()?; + Some(if self.le { u64::from_le_bytes(s) } else { u64::from_be_bytes(s) }) + } + + /// The declared entry count of the directory at `at`, before its table is parsed. + pub fn entry_count(&self, at: u64) -> Option { + if at < self.header_len() { + return None; + } + if self.big { self.u64_at(at) } else { self.u16_at(at).map(u64::from) } + } + + /// The directory at `at`: its whole table, up to and including the next-IFD pointer, must + /// be inside the file and after the header. + pub fn ifd(&self, at: u64) -> Option { + if at < self.header_len() { + return None; + } + let (n, first, entry, inline) = + if self.big { (self.u64_at(at)?, at.checked_add(8)?, 20u64, 8u64) } else { (u64::from(self.u16_at(at)?), at.checked_add(2)?, 12, 4) }; + if n > MAX_ENTRIES { + return None; + } + let next_at = first.checked_add(n.checked_mul(entry)?)?; + let next = if self.big { self.u64_at(next_at)? } else { u64::from(self.u32_at(next_at)?) }; + let mut entries = Vec::with_capacity(usize::try_from(n).ok()?); + for i in 0..n { + let e = first.checked_add(i.checked_mul(entry)?)?; + let tag = self.u16_at(e)?; + let ty = self.u16_at(e.checked_add(2)?)?; + let count = if self.big { self.u64_at(e.checked_add(4)?)? } else { u64::from(self.u32_at(e.checked_add(4)?)?) }; + let field = e.checked_add(if self.big { 12 } else { 8 })?; + // Unknown types are skipped, as the specification says. + let Some(size) = type_size(ty) else { + // Keep an unreadable child declaration visible to the completeness check. + if tag == TAG_SUB_IFDS { + entries.push(Entry { tag, ty, count, value_at: field }); + } + continue; + }; + let fits = count.checked_mul(size).is_some_and(|len| len <= inline); + let value_at = if fits { + field + } else if self.big { + self.u64_at(field)? + } else { + u64::from(self.u32_at(field)?) + }; + entries.push(Entry { tag, ty, count, value_at }); + } + Some(Ifd { at, entries, next, next_at }) + } + + /// The value bytes of an entry, when they are all inside the file. + pub fn data(&self, e: &Entry) -> Option<&'a [u8]> { + self.slice(e.value_at, e.count.checked_mul(type_size(e.ty)?)?) + } + + /// Value `i` of an unsigned integer entry (BYTE, SHORT, LONG, LONG8, IFD, IFD8). + pub fn uint(&self, e: &Entry, i: u64) -> Option { + if i >= e.count { + return None; + } + let size = type_size(e.ty)?; + let at = e.value_at.checked_add(i.checked_mul(size)?)?; + match e.ty { + 1 => self.slice(at, 1)?.first().map(|&v| u64::from(v)), + 3 => self.u16_at(at).map(u64::from), + 4 | 13 => self.u32_at(at).map(u64::from), + 16 | 18 => self.u64_at(at), + _ => None, + } + } + + /// The first value of an unsigned integer tag. + pub fn tag_uint(&self, ifd: &Ifd, tag: u16) -> Option { + self.uint(ifd.get(tag)?, 0) + } + + /// Up to `max` values of an unsigned integer tag (`None` if any of them is unreadable). + pub fn tag_uints(&self, ifd: &Ifd, tag: u16, max: u64) -> Option> { + let e = ifd.get(tag)?; + (0..e.count.min(max)).map(|i| self.uint(e, i)).collect() + } + + /// The first value of a numeric tag as a float: RATIONAL, FLOAT, DOUBLE or an integer. + pub fn tag_f64(&self, ifd: &Ifd, tag: u16) -> Option { + self.float(ifd.get(tag)?, 0) + } + + /// The values of a numeric tag as floats, when it holds exactly `n` (`None` for any other + /// count or an unreadable value). + pub fn tag_f64s(&self, ifd: &Ifd, tag: u16, n: u64) -> Option> { + let e = ifd.get(tag)?; + if e.count != n { + return None; + } + (0..n).map(|i| self.float(e, i)).collect() + } + + /// Value `i` of a numeric entry as a float: RATIONAL, FLOAT, DOUBLE or an integer. + fn float(&self, e: &Entry, i: u64) -> Option { + if i >= e.count { + return None; + } + let at = e.value_at.checked_add(i.checked_mul(type_size(e.ty)?)?)?; + match e.ty { + 5 => { + let (n, d) = (self.u32_at(at)?, self.u32_at(at.checked_add(4)?)?); + (d != 0).then(|| f64::from(n) / f64::from(d)) + } + 11 => Some(f64::from(f32::from_bits(self.u32_at(at)?))), + 12 => Some(f64::from_bits(self.u64_at(at)?)), + _ => self.uint(e, i).map(|v| v as f64), + } + } + + /// The bytes of a one-byte-per-value tag (BYTE, ASCII, SBYTE or UNDEFINED). + pub fn tag_bytes(&self, ifd: &Ifd, tag: u16) -> Option<&'a [u8]> { + let e = ifd.get(tag)?; + matches!(e.ty, 1 | 2 | 6 | 7).then(|| self.data(e)).flatten() + } + + /// SubIFD offsets of a directory (tag 330 as LONG, IFD, LONG8 or IFD8). + fn sub_ifds(&self, ifd: &Ifd) -> (Vec, bool) { + let mut entries = ifd.entries.iter().filter(|e| e.tag == TAG_SUB_IFDS); + let Some(e) = entries.next() else { return (Vec::new(), true) }; + let mut complete = entries.next().is_none() && e.count <= MAX_SUBIFDS; + if e.count == 0 { + return (Vec::new(), complete); + } + if !matches!(e.ty, 4 | 13 | 16 | 18) { + return (Vec::new(), false); + } + let mut children = Vec::new(); + for i in 0..e.count.min(MAX_SUBIFDS) { + let Some(at) = self.uint(e, i) else { + complete = false; + break; + }; + if at != 0 { + children.push(at); + } + } + (children, complete) + } +} + +/// A directory's Orientation (1–8), with the rules of [`crate::exif_orientation`]: the first +/// Orientation entry wins, it must be exactly one SHORT or LONG, and anything else reads as 1. +pub(crate) fn orientation_of(f: &File<'_>, ifd: &Ifd) -> u16 { + ifd.get(TAG_ORIENTATION) + .filter(|e| e.count == 1 && matches!(e.ty, 3 | 4)) + .and_then(|e| f.uint(e, 0)) + .and_then(|v| u16::try_from(v).ok()) + .filter(|v| (1..=8).contains(v)) + .unwrap_or(1) +} + +/// What a TIFF directory holds, from `NewSubfileType` (254) or the older `SubfileType` (255). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TiffPageKind { + /// A full-resolution image: a page of the document. + Page, + /// A reduced-resolution copy of another image (a thumbnail or a pyramid level). + ReducedResolution, + /// A transparency mask for another image. + Mask, +} + +/// One image directory of a TIFF file, described without decoding its pixels. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TiffPage { + /// Byte offset of the directory in the file. + pub ifd_offset: u64, + /// For a SubIFD (tag 330): the index (in [`TiffInfo::pages`]) of the directory listing it. + /// `None` for the directories of the main IFD chain. + pub parent: Option, + pub kind: TiffPageKind, + /// 0 when the directory has no readable dimensions. + pub width: u32, + pub height: u32, + pub samples_per_pixel: u16, + pub bits_per_sample: u16, + /// TIFF `SampleFormat`: 1 unsigned integer, 2 signed integer, 3 IEEE float. + pub sample_format: u16, + /// TIFF `PhotometricInterpretation`, when present. + pub photometric: Option, + /// TIFF `Compression` (1 = none). + pub compression: u16, + /// Tiles rather than strips. + pub tiled: bool, + /// `PlanarConfiguration` 2: each sample in its own plane. + pub planar: bool, +} + +/// The directory structure of a TIFF file: its byte order, BigTIFF or not, and every image +/// directory of the main chain and its SubIFDs. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TiffInfo { + pub big_tiff: bool, + pub little_endian: bool, + /// Main-chain directories in file order, each followed by its SubIFDs (depth first). + pub pages: Vec, + /// `false` for unreadable directories or child lists, cycles, or directory/depth/count limits. + /// The pages listed up to that point are still valid. + pub complete: bool, +} + +impl TiffInfo { + /// The page opened by default, like Photoshop: the first full-resolution directory of the + /// main chain (reduced-resolution copies and masks are skipped), else the first directory. + pub fn default_page(&self) -> Option { + self.pages + .iter() + .position(|p| p.parent.is_none() && p.kind == TiffPageKind::Page && p.width > 0 && p.height > 0) + .or_else(|| (!self.pages.is_empty()).then_some(0)) + } + + /// Full-resolution pages in the main chain (what Photoshop calls the pages of the file). + pub fn page_count(&self) -> usize { + self.pages.iter().filter(|p| p.parent.is_none() && p.kind == TiffPageKind::Page).count() + } +} + +fn describe(f: &File<'_>, ifd: &Ifd, parent: Option) -> TiffPage { + let new_kind = f.tag_uint(ifd, TAG_NEW_SUBFILE_TYPE).unwrap_or(0); + let old_kind = f.tag_uint(ifd, TAG_SUBFILE_TYPE).unwrap_or(1); + let kind = if new_kind & 4 != 0 { + TiffPageKind::Mask + } else if new_kind & 1 != 0 || old_kind == 2 { + TiffPageKind::ReducedResolution + } else { + TiffPageKind::Page + }; + let u32_of = |tag| f.tag_uint(ifd, tag).and_then(|v| u32::try_from(v).ok()).unwrap_or(0); + let u16_of = |tag, default| f.tag_uint(ifd, tag).and_then(|v| u16::try_from(v).ok()).unwrap_or(default); + TiffPage { + ifd_offset: ifd.at, + parent, + kind, + width: u32_of(TAG_WIDTH), + height: u32_of(TAG_HEIGHT), + samples_per_pixel: u16_of(TAG_SAMPLES, 1), + bits_per_sample: u16_of(TAG_BITS, 1), + sample_format: u16_of(TAG_SAMPLE_FORMAT, 1), + photometric: f.tag_uint(ifd, TAG_PHOTOMETRIC).and_then(|v| u16::try_from(v).ok()), + compression: u16_of(TAG_COMPRESSION, 1), + tiled: ifd.get(TAG_TILE_OFFSETS).is_some(), + planar: f.tag_uint(ifd, TAG_PLANAR) == Some(2), + } +} + +struct Walk<'a, 'f> { + f: &'f File<'a>, + seen: HashSet, + pages: Vec, + complete: bool, + /// Directory entries that may still be parsed. Real directory tables are disjoint, so their + /// total size is bounded by the file; overlapping tables (a hostile file) would otherwise + /// make every one of thousands of directories cost a full 65,535-entry parse. + entry_budget: u64, +} + +impl Walk<'_, '_> { + /// Follows a chain of directories starting at `at`, listing each with `parent` and then + /// its SubIFDs. + fn chain(&mut self, mut at: u64, parent: Option, depth: usize) { + while at != 0 { + if self.pages.len() >= MAX_IFDS || !self.seen.insert(at) { + self.complete = false; + return; + } + let Some(n) = self.f.entry_count(at) else { + self.complete = false; + return; + }; + if n > self.entry_budget { + self.complete = false; + return; + } + self.entry_budget -= n; + let Some(ifd) = self.f.ifd(at) else { + self.complete = false; + return; + }; + let index = self.pages.len(); + self.pages.push(describe(self.f, &ifd, parent)); + let (children, complete) = self.f.sub_ifds(&ifd); + self.complete &= complete; + if depth < MAX_SUB_DEPTH { + for sub in children { + self.chain(sub, Some(index), depth + 1); + } + } else if !children.is_empty() { + self.complete = false; + } + at = ifd.next; + } + } +} + +/// Lists the directories of a (Big)TIFF file. An error only when the bytes are not a TIFF or +/// the first directory cannot be read; a damaged chain after it ends the list early +/// (`complete == false`). +pub fn tiff_info(bytes: &[u8]) -> Result { + let f = File::parse(bytes).ok_or_else(|| CodecError::malformed(Format::Tiff, "not a TIFF or BigTIFF header"))?; + info_of(&f) +} + +pub(crate) fn info_of(f: &File<'_>) -> Result { + let mut w = Walk { f, seen: HashSet::new(), pages: Vec::new(), complete: true, entry_budget: (f.b.len() as u64) / 12 + 64 }; + w.chain(f.first_ifd, None, 0); + if w.pages.is_empty() { + return Err(CodecError::malformed(Format::Tiff, "the first image directory is missing or cut off")); + } + Ok(TiffInfo { big_tiff: f.big, little_endian: f.le, pages: w.pages, complete: w.complete }) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// 10,000 directories 4 bytes apart, each declaring 65,535 overlapping entries: the walk must + /// stop after the file-size entry budget instead of parsing ~6.5e8 entries. + #[test] + fn overlapping_directory_tables_exhaust_the_entry_budget() { + let first = 8usize; + let table = 2 + 65_535 * 12 + 4; + let mut b = vec![0u8; first + table + 4 * 10_000]; + b[..8].copy_from_slice(&[b'I', b'I', 42, 0, first as u8, 0, 0, 0]); + for i in 0..10_000usize { + let at = first + 4 * i; + b[at..at + 2].copy_from_slice(&u16::MAX.to_le_bytes()); + let next_cell = at + 2 + 65_535 * 12; + let next = (first + 4 * (i + 1)) as u32; + if let Some(cell) = b.get_mut(next_cell..next_cell + 4) { + cell.copy_from_slice(&next.to_le_bytes()); + } + } + let info = tiff_info(&b).expect("the first directory is readable"); + assert!(!info.complete); + assert!(info.pages.len() < 200, "{} directories parsed", info.pages.len()); + } +} diff --git a/crates/image-codecs/src/codecs/via_image.rs b/crates/image-codecs/src/codecs/via_image.rs new file mode 100644 index 000000000..7c02c614e --- /dev/null +++ b/crates/image-codecs/src/codecs/via_image.rs @@ -0,0 +1,455 @@ +//! Formats delegated to the `image` crate (pure-Rust features only): +//! GIF, BMP, TGA, ICO, QOI, Radiance HDR, and AVIF encode (feature `avif`). Targa is written by +//! [`super::tga`] and only read here. + +use std::io::{Cursor, Read}; + +use image::{DynamicImage, ImageDecoder, ImageFormat}; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType}; +use crate::options::{EncodeOptions, Limits}; + +fn image_format(f: Format) -> Option { + Some(match f { + Format::Gif => ImageFormat::Gif, + Format::Bmp => ImageFormat::Bmp, + Format::Tga => ImageFormat::Tga, + Format::Ico => ImageFormat::Ico, + Format::Qoi => ImageFormat::Qoi, + Format::Hdr => ImageFormat::Hdr, + Format::Avif => ImageFormat::Avif, + _ => return None, + }) +} + +fn map_err(f: Format, e: image::ImageError) -> CodecError { + match e { + image::ImageError::Limits(l) => CodecError::LimitExceeded(l.to_string()), + image::ImageError::Unsupported(u) => CodecError::unsupported(f, u.to_string()), + e => CodecError::malformed(f, e), + } +} + +pub(crate) fn bmp_palette(bytes: &[u8]) -> Result>, CodecError> { + let decoder = image::codecs::bmp::BmpDecoder::new(Cursor::new(bytes)).map_err(|e| map_err(Format::Bmp, e))?; + let mut palette = decoder.get_palette().map(|palette| palette.to_vec()); + // The decoder always hands back 256 entries (it pads a short table with black to survive + // corrupt files); the file's own count is what the document's colour table should hold. + if let (Some(p), Some(n)) = (palette.as_mut(), bmp_palette_len(bytes)) { + p.truncate(n.max(1)); + } + Ok(palette) +} + +/// How many colours a palette BMP's header declares: `biClrUsed`, or 2^bits when it is 0. +/// `None` for a header this does not recognise, which leaves the table as decoded. +fn bmp_palette_len(bytes: &[u8]) -> Option { + let u16_at = |at: usize| bytes.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes); + let u32_at = |at: usize| bytes.get(at..at + 4).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes); + let (bit_count, used) = match u32_at(14)? { + // OS/2 BITMAPCOREHEADER: no colour count, the bit depth sets the table size. + 12 => (u16_at(24)?, 0), + n if n >= 40 => (u16_at(28)?, u32_at(46)?), + _ => return None, + }; + if !(1..=8).contains(&bit_count) { + return None; + } + let max = 1usize << bit_count; + Some(if used == 0 { max } else { (used as usize).min(max) }) +} + +/// Writes an 8-bit palette BMP: `indices` row-major, `palette` RGB entries (1..=256). The +/// counterpart of [`bmp_palette`], for Indexed Color documents. +pub(crate) fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palette: &[[u8; 3]]) -> Result, CodecError> { + let f = Format::Bmp; + if width == 0 || height == 0 || u64::from(width) * u64::from(height) != indices.len() as u64 { + return Err(CodecError::InvalidImage("index data does not match the size".into())); + } + if palette.is_empty() || palette.len() > 256 { + return Err(CodecError::InvalidImage("a palette BMP holds 1 to 256 colours".into())); + } + if indices.iter().any(|&i| usize::from(i) >= palette.len()) { + return Err(CodecError::InvalidImage("palette index out of range".into())); + } + let mut out = Cursor::new(Vec::new()); + image::codecs::bmp::BmpEncoder::new(&mut out) + .encode_with_palette(indices, width, height, image::ExtendedColorType::L8, Some(palette)) + .map_err(|e| CodecError::encode(f, e))?; + Ok(out.into_inner()) +} + +/// The `image` decoder for `f` with our limits, its header read and its size checked. +fn open<'a>(f: Format, bytes: &'a [u8], limits: &Limits) -> Result { + let fmt = image_format(f).ok_or_else(|| CodecError::unsupported(f, "not handled by image backend"))?; + let mut reader = image::ImageReader::with_format(Cursor::new(bytes), fmt); + let mut il = image::Limits::default(); + il.max_image_width = Some(limits.max_width); + il.max_image_height = Some(limits.max_height); + il.max_alloc = Some(limits.max_alloc); + reader.limits(il); + if f == Format::Ico { + // The ICO decoder builds its embedded PNG decoder without limits, which inflates an + // iCCP profile up to usize::MAX while the reader is constructed, before `il` applies. + ico_png_profiles_fit(bytes, limits)?; + } + let dec = reader.into_decoder().map_err(|e| map_err(f, e))?; + let (w, h) = dec.dimensions(); + let bpp = dec.color_type().bytes_per_pixel() as u64; + limits.check_bytes(w, h, bpp)?; + // `DynamicImage::from_decoder` allocates the whole image before reading a pixel, so a + // header-only file must not be able to ask for gigabytes (#8597). GIF's LZW tops out near + // 11000:1 of RGBA output and TGA's and QOI's runs far below; AVIF has already decoded by now. + // Radiance HDR's old-style RLE repeat markers multiply their counts by 256 each in a row, so + // no ratio bounds a valid file: only the allocation limit above applies to it. + let declared = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp); + match f { + Format::Avif | Format::Hdr => {} + Format::Gif => gif_plausible(bytes, w, h, bpp)?, + // A BMP's RLE escapes (end of line, end of bitmap, delta) leave pixels unwritten, so no + // ratio bounds a valid file either. The decoder still allocates and clears the whole + // canvas, so like a GIF's screen only `UNBACKED_CANVAS` of it is free of data. + Format::Bmp if bmp_is_rle(bytes) => Limits::check_plausible(declared.saturating_sub(UNBACKED_CANVAS), bytes.len(), 1 << 14)?, + _ => Limits::check_plausible(declared, bytes.len(), 1 << 14)?, + } + Ok(dec) +} + +/// Canvas bytes a GIF's logical screen or a sparse RLE BMP may declare without data behind +/// them (256 MiB: an 8192x8192 RGBA screen). Past this, the file must back the rest. +const UNBACKED_CANVAS: u64 = 1 << 28; + +/// Whether a GIF whose `w`x`h` logical screen decodes to `bpp` bytes a pixel is plausible for +/// its file size. The first frame is what the file must hold: the logical screen around it needs +/// no data, so up to [`UNBACKED_CANVAS`] of screen is free and only the rest must be backed. +/// Shared by the first-frame decode and the animation decode ([`super::gif_frames`]), whose +/// backend also initializes the whole screen before reading the first frame. +pub(crate) fn gif_plausible(bytes: &[u8], w: u32, h: u32, bpp: u64) -> Result<(), CodecError> { + let declared = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp); + let declared = match gif_first_frame(bytes) { + Some((fw, fh)) => u64::from(fw).saturating_mul(u64::from(fh)).saturating_mul(bpp).max(declared.saturating_sub(UNBACKED_CANVAS)), + None => declared, + }; + Limits::check_plausible(declared, bytes.len(), 1 << 14) +} + +/// Most bytes one embedded iCCP profile of an icon may inflate to: 255 APP2 segments of a JPEG, +/// the largest profile any of our formats carries, and far past any real one. +const MAX_ICO_ICC_BYTES: u64 = 255 * 65_519; +/// Most bytes all of an icon's embedded iCCP profiles may inflate to together while checked. +/// Entries may point into overlapping chunk chains, so this bounds the check's own work. +const MAX_ICO_ICC_TOTAL: u64 = 64 << 20; + +/// Refuses an icon whose embedded PNG images carry an iCCP profile larger than the profile cap +/// (or `max_alloc`). The `image` crate's ICO decoder opens the PNG with no limits, and the `png` +/// crate inflates the profile while the decoder is built: a few kilobytes of zlib would ask for +/// gigabytes. Each profile is inflated here into a sink, so the check itself allocates nothing. +fn ico_png_profiles_fit(b: &[u8], limits: &Limits) -> Result<(), CodecError> { + const PNG_SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]; + let u16_at = |at: usize| b.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes); + let u32_at = |at: usize| b.get(at..at.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes); + let be32 = |at: usize| b.get(at..at.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_be_bytes); + let cap = MAX_ICO_ICC_BYTES.min(limits.max_alloc); + let Some(count) = u16_at(4) else { return Ok(()) }; + let mut offsets: Vec = (0..usize::from(count)).filter_map(|i| u32_at(6 + 16 * i + 12)).filter_map(|o| usize::try_from(o).ok()).collect(); + offsets.sort_unstable(); + offsets.dedup(); + let mut total = 0u64; + // Chunk offsets already walked by any start: a walk from here repeats the earlier one, so stop. + let mut visited = std::collections::HashSet::new(); + for start in offsets { + if b.get(start..start.saturating_add(8)) != Some(&PNG_SIGNATURE[..]) { + continue; + } + // Chunks up to the image data: the png crate parses iCCP only before IDAT. + let mut at = start + 8; + while let (Some(len), Some(kind)) = (be32(at), b.get(at.saturating_add(4)..at.saturating_add(8))) { + if !visited.insert(at) { + break; + } + let data_at = at + 8; + let Some(data) = usize::try_from(len).ok().and_then(|n| b.get(data_at..data_at.checked_add(n)?)) else { break }; + match kind { + b"IDAT" | b"IEND" => break, + b"iCCP" => { + // Profile name, NUL, compression method, then the zlib stream. + let Some(z) = data.iter().position(|&c| c == 0).and_then(|n| data.get(n + 2..)) else { break }; + // Count what inflates before a corrupt stream fails too: the png crate + // allocates that much before it finds the error. + let mut inflated = flate2::read::ZlibDecoder::new(z).take(cap.saturating_add(1)); + let (mut n, mut buf) = (0u64, [0u8; 8192]); + while let Ok(got @ 1..) = inflated.read(&mut buf) { + n += got as u64; + } + total = total.saturating_add(n); + if n > cap || total > MAX_ICO_ICC_TOTAL { + return Err(CodecError::LimitExceeded(format!("an icon's embedded colour profile inflates past {cap} bytes"))); + } + // The png crate refuses a second iCCP chunk without inflating it. + break; + } + _ => {} + } + at = data_at + data.len() + 4; + } + } + Ok(()) +} + +pub(crate) fn decode(f: Format, bytes: &[u8], limits: &Limits) -> Result { + let mut dec = open(f, bytes, limits)?; + let icc = dec.icc_profile().ok().flatten(); + let exif = dec.exif_metadata().ok().flatten(); + let dynimg = DynamicImage::from_decoder(dec).map_err(|e| map_err(f, e))?; + let mut img = from_dynamic(f, dynimg)?; + img.icc = icc; + img.meta.exif = exif; + if f == Format::Gif { + img.warnings.extend(gif_more_frames(bytes)); + } + Ok(img) +} + +/// The stored size and EXIF block [`decode`] would find, from the decoder's header alone: the +/// same reader, limits and size check, without decoding pixels. AVIF is refused: its decoder +/// decodes the image as it opens. +pub(crate) fn probe(f: Format, bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option>), CodecError> { + if f == Format::Avif { + return Err(CodecError::unsupported(f, "no header-only size probe")); + } + let mut dec = open(f, bytes, limits)?; + let (w, h) = dec.dimensions(); + Ok((w, h, dec.exif_metadata().ok().flatten())) +} + +/// Whether a BMP's `biCompression` is RLE8 or RLE4 (only headers of 40 bytes or more have one). +fn bmp_is_rle(b: &[u8]) -> bool { + let u32_at = |at: usize| b.get(at..at + 4).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes); + u32_at(14).is_some_and(|n| n >= 40) && matches!(u32_at(30), Some(1 | 2)) +} + +/// The width and height of a GIF's first image descriptor, if the blocks reach one. +fn gif_first_frame(b: &[u8]) -> Option<(u32, u32)> { + let mut at = None; + gif_walk(b, |i| { + at = Some(i); + false + }); + let u16_at = |at: usize| b.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes); + // Descriptor: separator, left, top, width, height (little-endian u16s), packed fields. + let i = at?; + Some((u32::from(u16_at(i + 5)?), u32::from(u16_at(i + 7)?))) +} + +/// Counts a GIF's frames (image descriptors) by walking its blocks, without decompressing any. +/// The total is unknown when the blocks end before the trailer or break off. +pub(crate) fn gif_more_frames(b: &[u8]) -> Option { + let (frames, total) = gif_walk(b, |_| true); + (frames > 1).then_some(DecodeWarning::MoreFrames { total }) +} + +/// Whether a GIF holds more than one frame, or None when the walk stops before the trailer +/// after at most one frame (so frames past the break could not be counted). +pub(crate) fn gif_has_more_frames(b: &[u8]) -> Option { + let (frames, total) = gif_walk(b, |_| true); + if frames > 1 { Some(true) } else { total.map(|_| false) } +} + +/// Walks a GIF's blocks without decompressing any, handing `frame` the offset of each image +/// descriptor until it returns false. Gives the descriptors seen, and the frame count if the +/// blocks reached the trailer. +fn gif_walk(b: &[u8], mut frame: impl FnMut(usize) -> bool) -> (u32, Option) { + // Size of the colour table a packed-fields byte announces. + let table = |flags: u8| if flags & 0x80 != 0 { 3usize << ((flags & 7) + 1) } else { 0 }; + // Skips the data sub-blocks starting at `i` (each length-prefixed, ended by a zero length). + let skip = |mut i: usize| -> Option { + loop { + let n = usize::from(*b.get(i)?); + i = i.checked_add(1 + n)?; + if n == 0 { + return Some(i); + } + } + }; + // Header (6 bytes), logical screen descriptor (7) and the global colour table. + let Some(&flags) = b.get(10) else { + return (0, None); + }; + let mut i = 13 + table(flags); + let mut frames = 0u32; + let total = loop { + let next = match b.get(i) { + Some(0x3B) => break Some(frames), + Some(0x21) => skip(i + 2), + Some(0x2C) => { + frames = frames.saturating_add(1); + if !frame(i) { + break None; + } + // Descriptor (10 bytes, packed fields last), local colour table, LZW code size. + b.get(i + 9).and_then(|&flags| skip(i + 11 + table(flags))) + } + _ => None, + }; + match next { + Some(n) => i = n, + None => break None, + } + }; + (frames, total) +} + +pub(crate) fn from_dynamic(f: Format, d: DynamicImage) -> Result { + let (w, h) = (d.width(), d.height()); + Ok(match d { + DynamicImage::ImageLuma8(b) => Image::from_u8(w, h, ChannelLayout::Gray, b.into_raw())?, + DynamicImage::ImageLumaA8(b) => Image::from_u8(w, h, ChannelLayout::GrayA, b.into_raw())?, + DynamicImage::ImageRgb8(b) => Image::from_u8(w, h, ChannelLayout::Rgb, b.into_raw())?, + DynamicImage::ImageRgba8(b) => Image::from_u8(w, h, ChannelLayout::Rgba, b.into_raw())?, + DynamicImage::ImageLuma16(b) => Image::from_u16(w, h, ChannelLayout::Gray, &b.into_raw())?, + DynamicImage::ImageLumaA16(b) => Image::from_u16(w, h, ChannelLayout::GrayA, &b.into_raw())?, + DynamicImage::ImageRgb16(b) => Image::from_u16(w, h, ChannelLayout::Rgb, &b.into_raw())?, + DynamicImage::ImageRgba16(b) => Image::from_u16(w, h, ChannelLayout::Rgba, &b.into_raw())?, + DynamicImage::ImageRgb32F(b) => Image::from_f32(w, h, ChannelLayout::Rgb, &b.into_raw())?, + DynamicImage::ImageRgba32F(b) => Image::from_f32(w, h, ChannelLayout::Rgba, &b.into_raw())?, + other => { + let b = other.into_rgba32f(); + Image::from_f32(w, h, ChannelLayout::Rgba, &b.into_raw()).map_err(|e| CodecError::malformed(f, e))? + } + }) +} + +fn to_dynamic(img: &Image) -> Result { + let (w, h) = img.dimensions(); + let bad = || CodecError::InvalidImage("buffer size mismatch".into()); + Ok(match (img.layout(), img.sample_type()) { + (ChannelLayout::Gray, SampleType::U8) => DynamicImage::ImageLuma8(image::GrayImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::GrayA, SampleType::U8) => DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::Rgb, SampleType::U8) => DynamicImage::ImageRgb8(image::RgbImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::Rgba, SampleType::U8) => DynamicImage::ImageRgba8(image::RgbaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::Rgb, SampleType::F32) => { + DynamicImage::ImageRgb32F(image::Rgb32FImage::from_raw(w, h, img.to_f32_samples().unwrap_or_default()).ok_or_else(bad)?) + } + (ChannelLayout::Rgba, SampleType::F32) => { + DynamicImage::ImageRgba32F(image::Rgba32FImage::from_raw(w, h, img.to_f32_samples().unwrap_or_default()).ok_or_else(bad)?) + } + (l, s) => { + return Err(CodecError::InvalidImage(format!("no image-crate mapping for {l:?} {s:?}"))); + } + }) +} + +pub(crate) fn encode(f: Format, src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let fmt = image_format(f).ok_or_else(|| CodecError::unsupported(f, "not handled by image backend"))?; + if let Some((mw, mh)) = f.max_dimensions() + && (src.width() > mw || src.height() > mh) + { + return Err(CodecError::encode(f, format!("dimensions exceed {mw}x{mh}"))); + } + let img = src.converted(plan.layout, plan.sample); + // Targa is written by our encoder: the image crate's RLE packets cross scanlines (#6382). + if f == Format::Tga { + return super::tga::encode(&img); + } + let dynimg = to_dynamic(&img)?; + let mut cursor = Cursor::new(Vec::new()); + match f { + #[cfg(feature = "avif")] + Format::Avif => { + let enc = image::codecs::avif::AvifEncoder::new_with_speed_quality(&mut cursor, 8, opts.jpeg_quality.clamp(1, 100)); + dynimg.write_with_encoder(enc).map_err(|e| CodecError::encode(f, e))?; + } + _ => { + let _ = opts; + dynimg.write_to(&mut cursor, fmt).map_err(|e| CodecError::encode(f, e))?; + } + } + Ok(cursor.into_inner()) +} + +#[cfg(test)] +mod indexed_tests { + use super::*; + + #[test] + fn palette_bmp_roundtrips_its_table_and_pixels() { + // 3 px wide: rows are padded to 4 bytes, so a wrong stride shows in the second row. + let pal = [[255, 0, 0], [0, 0, 255], [10, 20, 30]]; + let idx = [0u8, 1, 2, 2, 1, 0]; + let bytes = encode_bmp_indexed(3, 2, &idx, &pal).unwrap(); + assert_eq!(bmp_palette(&bytes).unwrap().as_deref(), Some(&pal[..]), "the table survives, not a 256-entry grey ramp"); + let img = decode(Format::Bmp, &bytes, &Limits::default()).unwrap(); + assert_eq!(img.dimensions(), (3, 2)); + let rgb = img.convert(ChannelLayout::Rgb, SampleType::U8); + let want: Vec = idx.iter().flat_map(|&i| pal[usize::from(i)]).collect(); + assert_eq!(rgb.data(), want.as_slice()); + } + + #[test] + fn palette_len_follows_the_header() { + let mut bytes = encode_bmp_indexed(2, 1, &[0, 1], &[[1, 2, 3], [4, 5, 6]]).unwrap(); + assert_eq!(bmp_palette_len(&bytes), Some(2)); + // biClrUsed = 0 means the full 2^bits table. + bytes[46..50].copy_from_slice(&0u32.to_le_bytes()); + assert_eq!(bmp_palette_len(&bytes), Some(256)); + // A short or unknown header leaves the decoded table alone instead of panicking. + assert_eq!(bmp_palette_len(&bytes[..30]), None); + bytes[14..18].copy_from_slice(&7u32.to_le_bytes()); + assert_eq!(bmp_palette_len(&bytes), None); + } + + /// A 64x48 icon holding one PNG with an iCCP chunk of `profile` bytes (zeros). + fn icon_with_profile(profile: usize) -> Vec { + use std::io::Write; + let img = Image::from_u8(64, 48, ChannelLayout::Rgba, vec![128; 64 * 48 * 4]).unwrap(); + let png = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); + z.write_all(&vec![0u8; profile]).unwrap(); + let mut data = b"icc\0\0".to_vec(); + data.extend_from_slice(&z.finish().unwrap()); + let mut crc = flate2::Crc::new(); + crc.update(b"iCCP"); + crc.update(&data); + let mut chunk = (data.len() as u32).to_be_bytes().to_vec(); + chunk.extend_from_slice(b"iCCP"); + chunk.extend_from_slice(&data); + chunk.extend_from_slice(&crc.sum().to_be_bytes()); + // Signature (8) and IHDR (25), then the profile. + let mut png_icc = png[..33].to_vec(); + png_icc.extend_from_slice(&chunk); + png_icc.extend_from_slice(&png[33..]); + let mut ico = vec![0, 0, 1, 0, 1, 0, 64, 48, 0, 0, 1, 0, 32, 0]; + ico.extend_from_slice(&(png_icc.len() as u32).to_le_bytes()); + ico.extend_from_slice(&22u32.to_le_bytes()); + ico.extend_from_slice(&png_icc); + ico + } + + #[test] + fn an_icon_profile_inflating_past_the_cap_is_refused_before_the_png_opens() { + let small = icon_with_profile(4096); + let img = decode(Format::Ico, &small, &Limits::default()).unwrap(); + assert_eq!(img.dimensions(), (64, 48)); + let huge = icon_with_profile(MAX_ICO_ICC_BYTES as usize + 1); + assert!(huge.len() < 64 << 10, "{} bytes", huge.len()); + for r in [decode(Format::Ico, &huge, &Limits::default()).map(|_| ()), probe(Format::Ico, &huge, &Limits::default()).map(|_| ())] { + assert!(matches!(&r, Err(CodecError::LimitExceeded(m)) if m.contains("colour profile")), "{r:?}"); + } + // max_alloc lowers the cap. + let tight = Limits { max_alloc: 2048, ..Limits::default() }; + assert!(matches!(decode(Format::Ico, &small, &tight), Err(CodecError::LimitExceeded(_)))); + } + + #[test] + fn palette_bmp_rejects_bad_input() { + let pal = [[0, 0, 0], [255, 255, 255]]; + assert!(encode_bmp_indexed(3, 2, &[0; 5], &pal).is_err(), "size mismatch"); + assert!(encode_bmp_indexed(0, 2, &[], &pal).is_err(), "empty"); + assert!(encode_bmp_indexed(2, 1, &[0, 2], &pal).is_err(), "index past the table"); + assert!(encode_bmp_indexed(1, 1, &[0], &[]).is_err(), "no colours"); + assert!(encode_bmp_indexed(1, 1, &[0], &[[0; 3]; 257]).is_err(), "more than 256 colours"); + } +} diff --git a/crates/image-codecs/src/codecs/vp8.rs b/crates/image-codecs/src/codecs/vp8.rs new file mode 100644 index 000000000..a24fb727c --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8.rs @@ -0,0 +1,848 @@ +//! A clean-room VP8 key-frame encoder (the lossy WebP bitstream), implemented from RFC 6386. +//! +//! One intra frame: 16×16 macroblocks in raster order, each predicted from the frame's own +//! reconstruction with one of the four whole-block luma modes or the sixteen 4×4 sub-block +//! modes (`B_PRED`), chroma with one of four modes; the residual goes through the 4×4 DCT +//! (and the WHT of the luma DCs for whole-block modes), one frame-wide quantizer derived from the +//! quality, and the boolean entropy coder with the specification's default token probabilities, +//! refined per frame from the frame's own token statistics. Macroblocks without coefficients +//! are skipped. The loop filter is left to the decoder at a level matched to the quantizer. +//! +//! What it does not do (yet): rate-distortion optimized mode decisions and quantization, +//! segments, multiple token partitions, or SIMD. It is deterministic, allocation-bounded and +//! never panics: dimensions are checked up front and every index is derived from them. + +mod bool_encoder; +mod predict; +mod tables; +mod transform; + +use bool_encoder::BoolEncoder; +use predict::{Plane, SubEdges, predict_block, predict_sub}; +use tables::{ + AC_QLOOKUP, B_DC_PRED, B_HE_PRED, B_PRED, B_TM_PRED, B_VE_PRED, BMODE_TREE, CAT_BASE, COEFF_BANDS, COEFF_TREE, COEFF_UPDATE_PROBS, DC_PRED, DC_QLOOKUP, + DCT_0, DCT_1, DCT_2, DCT_3, DCT_4, DCT_CAT1, DCT_CAT2, DCT_CAT3, DCT_CAT4, DCT_CAT5, DCT_CAT6, DCT_EOB, DEFAULT_COEFF_PROBS, H_PRED, KF_BMODE_PROBS, + KF_UV_MODE_PROB, KF_YMODE_PROB, KF_YMODE_TREE, PCAT, TM_PRED, UV_MODE_TREE, V_PRED, ZIGZAG, +}; +use transform::{fdct, fwht, idct, iwht}; + +/// The largest dimension a VP8 frame header can hold (14 bits). +pub const MAX_DIMENSION: u32 = 16383; + +/// A planar 4:2:0 picture: `y` is `width × height`, `u` and `v` are `ceil(w/2) × ceil(h/2)`. +pub struct Yuv { + pub width: u32, + pub height: u32, + pub y: Vec, + pub u: Vec, + pub v: Vec, +} + +impl Yuv { + /// Converts interleaved 8-bit RGB (`stride` bytes per pixel, 3 or 4) to studio-range BT.601 + /// Y'CbCr with the chroma averaged over each 2 × 2 block, the convention WebP decoders invert. + pub fn from_rgb(rgb: &[u8], width: u32, height: u32, stride: usize) -> Yuv { + let (w, h) = (width as usize, height as usize); + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let mut y = vec![0u8; w * h]; + let mut u = vec![0u8; cw * ch]; + let mut v = vec![0u8; cw * ch]; + let px = |x: usize, yy: usize| -> (i32, i32, i32) { + let i = (yy.min(h - 1) * w + x.min(w - 1)) * stride; + (i32::from(rgb[i]), i32::from(rgb[i + 1]), i32::from(rgb[i + 2])) + }; + for yy in 0..h { + for x in 0..w { + let (r, g, b) = px(x, yy); + y[yy * w + x] = (((66 * r + 129 * g + 25 * b + 128) >> 8) + 16) as u8; + } + } + for cy in 0..ch { + for cx in 0..cw { + let (mut r, mut g, mut b) = (0, 0, 0); + for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { + let (pr, pg, pb) = px(cx * 2 + dx, cy * 2 + dy); + r += pr; + g += pg; + b += pb; + } + let (r, g, b) = ((r + 2) >> 2, (g + 2) >> 2, (b + 2) >> 2); + u[cy * cw + cx] = (((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128).clamp(0, 255) as u8; + v[cy * cw + cx] = (((112 * r - 94 * g - 18 * b + 128) >> 8) + 128).clamp(0, 255) as u8; + } + } + Yuv { width, height, y, u, v } + } +} + +/// Encoder settings. +#[derive(Debug, Clone, Copy)] +pub struct Params { + /// 0 (smallest) … 100 (best); maps to the frame quantizer. + pub quality: u8, +} + +/// Why a frame cannot be encoded. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Vp8Error { + /// A dimension is 0 or above [`MAX_DIMENSION`]. + Dimensions, + /// The plane buffers do not match the dimensions. + Planes, +} + +impl std::fmt::Display for Vp8Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Vp8Error::Dimensions => write!(f, "VP8 frames are 1 to {MAX_DIMENSION} pixels per side"), + Vp8Error::Planes => write!(f, "plane sizes do not match the dimensions"), + } + } +} + +/// Quality 0..=100 → quantizer index 0..=127 (0 is finest). A gentle curve: quality 90 ≈ 11, +/// 75 ≈ 28, 50 ≈ 60, 25 ≈ 92. +pub fn quantizer_index(quality: u8) -> u8 { + let q = f64::from(quality.min(100)); + let t = (100.0 - q) / 100.0; + (t.powf(1.1) * 127.0).round().clamp(0.0, 127.0) as u8 +} + +/// Dequantization factors for one quantizer index (RFC 6386 §14.1 and dixie's `dequant_init`). +#[derive(Debug, Clone, Copy)] +struct Quant { + y_dc: i32, + y_ac: i32, + y2_dc: i32, + y2_ac: i32, + uv_dc: i32, + uv_ac: i32, +} + +impl Quant { + fn new(qi: u8) -> Quant { + let qi = usize::from(qi.min(127)); + Quant { + y_dc: DC_QLOOKUP[qi], + y_ac: AC_QLOOKUP[qi], + y2_dc: DC_QLOOKUP[qi] * 2, + y2_ac: (AC_QLOOKUP[qi] * 155 / 100).max(8), + uv_dc: DC_QLOOKUP[qi].min(132), + uv_ac: AC_QLOOKUP[qi], + } + } +} + +/// Quantizes one coefficient: round to nearest for DC, with a small dead zone for AC (fewer +/// ±1 coefficients cost far fewer bits than they are worth in distortion). +#[inline] +fn quantize(coeff: i32, factor: i32, dc: bool) -> i32 { + let bias = if dc { factor / 2 } else { factor * 7 / 16 }; + let q = (coeff.abs() + bias) / factor; + let q = q.min(2048); + if coeff < 0 { -q } else { q } +} + +/// Block types of the token probability table (RFC 6386 §13.3). +const TYPE_Y_AFTER_Y2: usize = 0; +const TYPE_Y2: usize = 1; +const TYPE_UV: usize = 2; +const TYPE_Y_WITH_DC: usize = 3; + +/// Which coefficient token codes `v` and, for the categories, the extra bits to send. +fn token_of(v: i32) -> (u8, Option<(usize, u32)>) { + let a = v.unsigned_abs() as i32; + match a { + 0 => (DCT_0, None), + 1 => (DCT_1, None), + 2 => (DCT_2, None), + 3 => (DCT_3, None), + 4 => (DCT_4, None), + 5..=6 => (DCT_CAT1, Some((0, (a - CAT_BASE[0]) as u32))), + 7..=10 => (DCT_CAT2, Some((1, (a - CAT_BASE[1]) as u32))), + 11..=18 => (DCT_CAT3, Some((2, (a - CAT_BASE[2]) as u32))), + 19..=34 => (DCT_CAT4, Some((3, (a - CAT_BASE[3]) as u32))), + 35..=66 => (DCT_CAT5, Some((4, (a - CAT_BASE[4]) as u32))), + _ => (DCT_CAT6, Some((5, (a - CAT_BASE[5]) as u32))), + } +} + +/// One coded block's quantized coefficients in zig-zag order, from `first` (1 for Y after Y2). +struct Block { + coeffs: [i32; 16], + first: usize, + /// One past the last non-zero zig-zag position (`first` when the block is empty). + eob: usize, +} + +impl Block { + fn from_raster(raster: &[i32; 16], first: usize) -> Block { + let mut coeffs = [0i32; 16]; + for (pos, &idx) in ZIGZAG.iter().enumerate() { + coeffs[pos] = raster[idx]; + } + let eob = (first..16).rev().find(|&p| coeffs[p] != 0).map_or(first, |p| p + 1); + Block { coeffs, first, eob } + } + + fn has_coeffs(&self) -> bool { + self.eob > self.first + } +} + +/// Token-count statistics, `[type][band][ctx][token]`, from which the frame's probabilities are +/// derived (and the per-node branch counts needed to turn them into tree probabilities). +#[derive(Clone)] +struct Stats { + /// Per tree node (11 per context): how often the bool was 0 and how often 1. + node: Vec<[[[[u32; 2]; 11]; 3]; 8]>, +} + +impl Stats { + fn new() -> Stats { + Stats { node: vec![[[[[0; 2]; 11]; 3]; 8]; 4] } + } +} + +/// Everything about a block's tokens the two passes share: the branch path of each token in the +/// coefficient tree. Walked once per block to count branches, once to write them. +fn tree_path(value: u8, start: usize) -> Vec<(usize, bool)> { + fn find(node: usize, value: u8, path: &mut Vec<(usize, bool)>) -> bool { + for bit in [false, true] { + let i = node + usize::from(bit); + let Some(&entry) = COEFF_TREE.get(i) else { continue }; + path.push((node >> 1, bit)); + if entry <= 0 { + if (-entry) as u8 == value { + return true; + } + } else if find(entry as usize, value, path) { + return true; + } + path.pop(); + } + false + } + let mut path = Vec::with_capacity(8); + find(start, value, &mut path); + path +} + +/// One bool of a block's token sequence. +enum Event { + /// A coefficient-tree branch: `(type, band, ctx, node, bit)`, coded with `probs[type][band][ctx][node]`. + Node(usize, usize, usize, usize, bool), + /// The extra bits of a category token: `(category, bits)`. + Extra(usize, u32), + /// The sign of a non-zero coefficient (`true` = negative). + Sign(bool), +} + +/// Visits every bool of a block's token sequence in coding order. +fn for_each_token_bool(block: &Block, ty: usize, ctx0: usize, mut f: impl FnMut(Event)) { + let mut ctx = ctx0; + let mut prev_zero = false; + let mut pos = block.first; + while pos < block.eob { + let v = block.coeffs[pos]; + let (token, cat) = token_of(v); + let band = COEFF_BANDS[pos]; + for (node, bit) in tree_path(token, if prev_zero { 2 } else { 0 }) { + f(Event::Node(ty, band, ctx, node, bit)); + } + if let Some((c, bits)) = cat { + f(Event::Extra(c, bits)); + } + if v != 0 { + f(Event::Sign(v < 0)); + } + ctx = match v.abs() { + 0 => 0, + 1 => 1, + _ => 2, + }; + prev_zero = v == 0; + pos += 1; + } + if block.eob < 16 { + // End of block (never directly after a zero: the coded run ends with a non-zero value). + let band = COEFF_BANDS[block.eob]; + for (node, bit) in tree_path(DCT_EOB, 0) { + f(Event::Node(ty, band, ctx, node, bit)); + } + } +} + +/// A macroblock's decisions and coefficients. +struct Macroblock { + y_mode: u8, + b_modes: [u8; 16], + uv_mode: u8, + /// Y2 (whole-block modes only), 16 Y, 4 U, 4 V. + y2: Option, + y: Vec, + u: Vec, + v: Vec, + skip: bool, +} + +/// Non-zero contexts: 4 Y (by column for "above", by row for "left"), 2 U, 2 V, 1 Y2. +type Ctx = [u8; 9]; + +struct Encoder<'a> { + src: &'a Yuv, + mb_cols: usize, + mb_rows: usize, + q: Quant, + rec_y: Plane, + rec_u: Plane, + rec_v: Plane, + mbs: Vec, +} + +impl<'a> Encoder<'a> { + fn new(src: &'a Yuv, qi: u8) -> Encoder<'a> { + let mb_cols = (src.width as usize).div_ceil(16); + let mb_rows = (src.height as usize).div_ceil(16); + Encoder { + src, + mb_cols, + mb_rows, + q: Quant::new(qi), + rec_y: Plane::new(mb_cols * 16, mb_rows * 16), + rec_u: Plane::new(mb_cols * 8, mb_rows * 8), + rec_v: Plane::new(mb_cols * 8, mb_rows * 8), + mbs: Vec::with_capacity(mb_cols * mb_rows), + } + } + + /// Source luma at (`x`, `y`), the frame's last row and column extended over the padding. + #[inline] + fn src_y(&self, x: usize, y: usize) -> i32 { + let (w, h) = (self.src.width as usize, self.src.height as usize); + i32::from(self.src.y[y.min(h - 1) * w + x.min(w - 1)]) + } + + #[inline] + fn src_c(&self, plane: &[u8], x: usize, y: usize) -> i32 { + let (w, h) = ((self.src.width as usize).div_ceil(2), (self.src.height as usize).div_ceil(2)); + i32::from(plane[y.min(h - 1) * w + x.min(w - 1)]) + } + + /// Encodes one 4×4 block: residual → coefficients → quantized (in raster order, with the + /// quantization factors) → the dequantized coefficients the decoder will see. + fn code_block(residual: &[i32; 16], dc_factor: i32, ac_factor: i32, with_dc: bool) -> ([i32; 16], [i32; 16]) { + let coeffs = fdct(residual); + let mut quantized = [0i32; 16]; + let mut dequantized = [0i32; 16]; + for i in 0..16 { + let factor = if i == 0 { dc_factor } else { ac_factor }; + if i == 0 && !with_dc { + continue; + } + quantized[i] = quantize(coeffs[i], factor, i == 0); + dequantized[i] = quantized[i] * factor; + } + (quantized, dequantized) + } + + /// Sum of squared differences between a predicted block and the source. + fn sse(pred: &[u8], src: &[i32]) -> u64 { + pred.iter() + .zip(src) + .map(|(&p, &s)| { + let d = i64::from(p) - i64::from(s); + (d * d) as u64 + }) + .sum() + } + + /// Chroma of one macroblock: one mode for both planes; returns it with the U and V blocks. + fn encode_chroma(&mut self, mbx: usize, mby: usize) -> (u8, Vec, Vec) { + let (cx0, cy0) = (mbx * 8, mby * 8); + let mut src_u = [0i32; 64]; + let mut src_v = [0i32; 64]; + for r in 0..8 { + for c in 0..8 { + src_u[r * 8 + c] = self.src_c(&self.src.u, cx0 + c, cy0 + r); + src_v[r * 8 + c] = self.src_c(&self.src.v, cx0 + c, cy0 + r); + } + } + let mut uv_mode = DC_PRED; + let mut uv_best = u64::MAX; + let mut pred_u = [0u8; 64]; + let mut pred_v = [0u8; 64]; + let mut pu = [0u8; 64]; + let mut pv = [0u8; 64]; + for mode in [DC_PRED, V_PRED, H_PRED, TM_PRED] { + predict_block(&self.rec_u, cx0, cy0, 8, mode, &mut pu); + predict_block(&self.rec_v, cx0, cy0, 8, mode, &mut pv); + let e = Self::sse(&pu, &src_u) + Self::sse(&pv, &src_v); + if e < uv_best { + uv_best = e; + uv_mode = mode; + pred_u = pu; + pred_v = pv; + } + } + let mut u_blocks = Vec::with_capacity(4); + let mut v_blocks = Vec::with_capacity(4); + for (plane_src, pred, blocks, is_u) in [(&src_u, &pred_u, &mut u_blocks, true), (&src_v, &pred_v, &mut v_blocks, false)] { + for b in 0..4 { + let mut residual = [0i32; 16]; + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 1) * 4 + r) * 8 + (b & 1) * 4 + c; + residual[r * 4 + c] = plane_src[i] - i32::from(pred[i]); + } + } + let (quantized, dequantized) = Self::code_block(&residual, self.q.uv_dc, self.q.uv_ac, true); + let recon = idct(&dequantized); + let (bx, by) = (cx0 + (b & 1) * 4, cy0 + (b >> 1) * 4); + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 1) * 4 + r) * 8 + (b & 1) * 4 + c; + let value = (i32::from(pred[i]) + recon[r * 4 + c]).clamp(0, 255) as u8; + if is_u { + self.rec_u.set(bx + c, by + r, value); + } else { + self.rec_v.set(bx + c, by + r, value); + } + } + } + blocks.push(Block::from_raster(&quantized, 0)); + } + } + (uv_mode, u_blocks, v_blocks) + } + + // Sub-block indices address several arrays by geometry; ranges read better than iterators here. + #[allow(clippy::needless_range_loop)] + fn encode_macroblock(&mut self, mbx: usize, mby: usize) { + let (x0, y0) = (mbx * 16, mby * 16); + // Source luma of this macroblock. + let mut src = [0i32; 256]; + for r in 0..16 { + for c in 0..16 { + src[r * 16 + c] = self.src_y(x0 + c, y0 + r); + } + } + // Whole-block luma modes: the least squared prediction error wins. + let mut pred16 = [0u8; 256]; + let mut best_mode = DC_PRED; + let mut best_sse = u64::MAX; + let mut best_pred = [0u8; 256]; + for mode in [DC_PRED, V_PRED, H_PRED, TM_PRED] { + predict_block(&self.rec_y, x0, y0, 16, mode, &mut pred16); + let e = Self::sse(&pred16, &src); + if e < best_sse { + best_sse = e; + best_mode = mode; + best_pred = pred16; + } + } + // Rate-aware comparison with B_PRED: each sub-block mode costs about three bits, and + // the whole-block path codes the DCs once in Y2. lambda scales distortion per bit with + // the quantizer (squared error units). + let lambda = u64::from((self.q.y_ac * self.q.y_ac) as u32) / 8; + let whole_cost = best_sse + 3 * lambda; + + // B_PRED: sub-blocks in raster order, each predicted from the reconstruction so far. + let saved_y = self.rec_y.data.clone(); + let mut b_modes = [B_DC_PRED; 16]; + let mut b_blocks: Vec = Vec::with_capacity(16); + let mut b_sse = 0u64; + { + for b in 0..16 { + let (bx, by) = (x0 + (b & 3) * 4, y0 + (b >> 2) * 4); + let edges = SubEdges::gather(&self.rec_y, x0, y0, b, self.mb_cols); + let mut sub_src = [0i32; 16]; + for r in 0..4 { + for c in 0..4 { + sub_src[r * 4 + c] = src[((b >> 2) * 4 + r) * 16 + (b & 3) * 4 + c]; + } + } + let mut best = (u64::MAX, B_DC_PRED, [0u8; 16]); + let mut pred = [0u8; 16]; + for mode in 0..10u8 { + predict_sub(&edges, mode, &mut pred); + let e = Self::sse(&pred, &sub_src); + if e < best.0 { + best = (e, mode, pred); + } + } + b_modes[b] = best.1; + b_sse += best.0; + let residual: [i32; 16] = std::array::from_fn(|i| sub_src[i] - i32::from(best.2[i])); + let (quantized, dequantized) = Self::code_block(&residual, self.q.y_dc, self.q.y_ac, true); + let recon = idct(&dequantized); + for r in 0..4 { + for c in 0..4 { + self.rec_y.set(bx + c, by + r, (i32::from(best.2[r * 4 + c]) + recon[r * 4 + c]).clamp(0, 255) as u8); + } + } + b_blocks.push(Block::from_raster(&quantized, 0)); + } + } + let b_cost = b_sse + 50 * lambda; + + let (y_mode, y2, y_blocks) = if b_cost < whole_cost { + (B_PRED, None, b_blocks) + } else { + // Whole-block mode: undo the B_PRED reconstruction, code 16 blocks + Y2. + self.rec_y.data = saved_y; + let mut dcs = [0i32; 16]; + let mut ac_blocks: Vec<[i32; 16]> = Vec::with_capacity(16); + let mut deq_blocks: Vec<[i32; 16]> = Vec::with_capacity(16); + for b in 0..16 { + let mut residual = [0i32; 16]; + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 2) * 4 + r) * 16 + (b & 3) * 4 + c; + residual[r * 4 + c] = src[i] - i32::from(best_pred[i]); + } + } + let coeffs = fdct(&residual); + dcs[b] = coeffs[0]; + let mut quantized = [0i32; 16]; + let mut dequantized = [0i32; 16]; + for i in 1..16 { + quantized[i] = quantize(coeffs[i], self.q.y_ac, false); + dequantized[i] = quantized[i] * self.q.y_ac; + } + ac_blocks.push(quantized); + deq_blocks.push(dequantized); + } + // The DCs through the WHT, quantized as Y2, and back through the inverse WHT so the + // reconstruction uses exactly the decoder's DC values. + let y2_coeffs = fwht(&dcs); + let mut y2_q = [0i32; 16]; + let mut y2_deq = [0i32; 16]; + for i in 0..16 { + let factor = if i == 0 { self.q.y2_dc } else { self.q.y2_ac }; + y2_q[i] = quantize(y2_coeffs[i], factor, i == 0); + y2_deq[i] = y2_q[i] * factor; + } + let dc_rec = iwht(&y2_deq); + for b in 0..16 { + deq_blocks[b][0] = dc_rec[b]; + let recon = idct(&deq_blocks[b]); + let (bx, by) = (x0 + (b & 3) * 4, y0 + (b >> 2) * 4); + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 2) * 4 + r) * 16 + (b & 3) * 4 + c; + self.rec_y.set(bx + c, by + r, (i32::from(best_pred[i]) + recon[r * 4 + c]).clamp(0, 255) as u8); + } + } + } + let blocks = ac_blocks.iter().map(|q| Block::from_raster(q, 1)).collect(); + (best_mode, Some(Block::from_raster(&y2_q, 0)), blocks) + }; + + let (uv_mode, u_blocks, v_blocks) = self.encode_chroma(mbx, mby); + + let skip = y2.as_ref().is_none_or(|b| !b.has_coeffs()) + && y_blocks.iter().all(|b| !b.has_coeffs()) + && u_blocks.iter().all(|b| !b.has_coeffs()) + && v_blocks.iter().all(|b| !b.has_coeffs()); + self.mbs.push(Macroblock { y_mode, b_modes, uv_mode, y2, y: y_blocks, u: u_blocks, v: v_blocks, skip }); + } + + /// Runs `f` over every token bool of the frame in coding order, with the non-zero contexts + /// maintained exactly as the decoder does (RFC 6386 §13.3; skipped macroblocks reset them, + /// leaving Y2 alone for `B_PRED` macroblocks). + fn walk_tokens(&self, mut f: impl FnMut(&Block, usize, usize), mut on_mb: impl FnMut(usize, &Macroblock)) { + let mut above: Vec = vec![[0; 9]; self.mb_cols]; + for mby in 0..self.mb_rows { + let mut left: Ctx = [0; 9]; + for (mbx, a) in above.iter_mut().enumerate() { + let mb = &self.mbs[mby * self.mb_cols + mbx]; + on_mb(mby * self.mb_cols + mbx, mb); + if mb.skip { + for i in 0..8 { + left[i] = 0; + a[i] = 0; + } + if mb.y_mode != B_PRED { + left[8] = 0; + a[8] = 0; + } + continue; + } + if let Some(y2) = &mb.y2 { + let ctx = usize::from(left[8] + a[8]); + f(y2, TYPE_Y2, ctx); + let t = u8::from(y2.has_coeffs()); + left[8] = t; + a[8] = t; + } + let ty = if mb.y2.is_some() { TYPE_Y_AFTER_Y2 } else { TYPE_Y_WITH_DC }; + for (i, b) in mb.y.iter().enumerate() { + let (ai, li) = (i & 3, i >> 2); + let ctx = usize::from(left[li] + a[ai]); + f(b, ty, ctx); + let t = u8::from(b.has_coeffs()); + left[li] = t; + a[ai] = t; + } + for (base, blocks) in [(4usize, &mb.u), (6usize, &mb.v)] { + for (i, b) in blocks.iter().enumerate() { + let (ai, li) = (base + (i & 1), base + (i >> 1)); + let ctx = usize::from(left[li] + a[ai]); + f(b, TYPE_UV, ctx); + let t = u8::from(b.has_coeffs()); + left[li] = t; + a[ai] = t; + } + } + } + } + } + + /// Token probabilities for this frame: the defaults, replaced where the frame's own branch + /// counts give a clearly better estimate (sent as updates in the header). + fn choose_probs(&self, stats: &Stats) -> [[[[u8; 11]; 3]; 8]; 4] { + let mut probs = DEFAULT_COEFF_PROBS; + for (t, plane) in probs.iter_mut().enumerate() { + for (b, band) in plane.iter_mut().enumerate() { + for (c, ctx) in band.iter_mut().enumerate() { + for (n, p) in ctx.iter_mut().enumerate() { + let [zeros, ones] = stats.node[t][b][c][n]; + let total = zeros + ones; + if total < 24 { + continue; + } + // Probability of a 0 branch, kept inside 1..=255. + let est = ((u64::from(zeros) * 256 + u64::from(total) / 2) / u64::from(total)).clamp(1, 255) as u8; + // Only worth the update bits when the estimate moves the coding cost. + let cost = |pr: u8| -> f64 { + let p0 = f64::from(pr) / 256.0; + -(f64::from(zeros) * p0.log2() + f64::from(ones) * (1.0 - p0).log2()) + }; + let update_bits = 8.0 + 1.0; + if cost(*p) - cost(est) > update_bits * 2.0 { + *p = est; + } + } + } + } + } + probs + } + + fn write_frame(&self, probs: &[[[[u8; 11]; 3]; 8]; 4], loop_filter_level: u8, qi: u8) -> Vec { + // First partition: frame header, then per-macroblock prediction records. + let mut h = BoolEncoder::new(); + h.put_flag(false); // color_space: BT.601-like + h.put_flag(false); // clamping_type: the decoder clamps + h.put_flag(false); // segmentation_enabled + h.put_flag(false); // filter_type: normal + h.put_literal(6, u32::from(loop_filter_level.min(63))); + h.put_literal(3, 0); // sharpness_level + h.put_flag(false); // loop_filter_adj_enable + h.put_literal(2, 0); // one token partition + h.put_literal(7, u32::from(qi.min(127))); // y_ac_qi + for _ in 0..5 { + h.put_flag(false); // no quantizer deltas + } + h.put_flag(true); // refresh_entropy_probs (meaningless for a single frame) + for t in 0..4 { + for b in 0..8 { + for c in 0..3 { + for n in 0..11 { + let changed = probs[t][b][c][n] != DEFAULT_COEFF_PROBS[t][b][c][n]; + h.put(COEFF_UPDATE_PROBS[t][b][c][n], changed); + if changed { + h.put_literal(8, u32::from(probs[t][b][c][n])); + } + } + } + } + } + h.put_flag(true); // mb_no_skip_coeff + let skipped = self.mbs.iter().filter(|m| m.skip).count() as u64; + let total = self.mbs.len().max(1) as u64; + let prob_skip_false = (((total - skipped) * 256 + total / 2) / total).clamp(1, 255) as u8; + h.put_literal(8, u32::from(prob_skip_false)); + + for mby in 0..self.mb_rows { + for mbx in 0..self.mb_cols { + let mb = &self.mbs[mby * self.mb_cols + mbx]; + h.put(prob_skip_false, mb.skip); + h.put_tree(KF_YMODE_TREE, &KF_YMODE_PROB, mb.y_mode); + if mb.y_mode == B_PRED { + for b in 0..16 { + let above = if b >= 4 { + mb.b_modes[b - 4] + } else if mby == 0 { + B_DC_PRED + } else { + self.sub_mode_of(&self.mbs[(mby - 1) * self.mb_cols + mbx], b + 12) + }; + let left = if b & 3 != 0 { + mb.b_modes[b - 1] + } else if mbx == 0 { + B_DC_PRED + } else { + self.sub_mode_of(&self.mbs[mby * self.mb_cols + mbx - 1], b + 3) + }; + h.put_tree(BMODE_TREE, &KF_BMODE_PROBS[usize::from(above)][usize::from(left)], mb.b_modes[b]); + } + } + h.put_tree(UV_MODE_TREE, &KF_UV_MODE_PROB, mb.uv_mode); + } + } + let first = h.finish(); + + // Token partition. + let mut t = BoolEncoder::new(); + self.walk_tokens( + |block, ty, ctx0| { + for_each_token_bool(block, ty, ctx0, |ev| match ev { + Event::Node(ty, band, ctx, node, bit) => t.put(probs[ty][band][ctx][node], bit), + Event::Extra(cat, bits) => { + let p = PCAT[cat]; + for (i, &prob) in p.iter().enumerate() { + let shift = p.len() - 1 - i; + t.put(prob, (bits >> shift) & 1 != 0); + } + } + Event::Sign(negative) => t.put_flag(negative), + }); + }, + |_, _| {}, + ); + let tokens = t.finish(); + + // Frame tag, start code, dimensions, partitions. + let mut out = Vec::with_capacity(10 + first.len() + tokens.len()); + let size = (first.len() as u32).min((1 << 19) - 1); + // Bit 0 = 0: key frame; bits 1-3 = 0: version 0; bit 4: show_frame; then the size. + let tag: u32 = (1 << 4) | (size << 5); + out.extend_from_slice(&tag.to_le_bytes()[..3]); + out.extend_from_slice(&[0x9d, 0x01, 0x2a]); + out.extend_from_slice(&(self.src.width as u16).to_le_bytes()); + out.extend_from_slice(&(self.src.height as u16).to_le_bytes()); + out.extend_from_slice(&first); + out.extend_from_slice(&tokens); + out + } + + /// The sub-block mode context a neighbouring macroblock provides for sub-block `b` (an + /// index into the neighbour's 16 sub-blocks): its own sub-block mode when it is `B_PRED`, + /// else the sub-block equivalent of its whole-block mode (RFC 6386 §11.3). + fn sub_mode_of(&self, mb: &Macroblock, b: usize) -> u8 { + match mb.y_mode { + B_PRED => mb.b_modes[b.min(15)], + V_PRED => B_VE_PRED, + H_PRED => B_HE_PRED, + TM_PRED => B_TM_PRED, + _ => B_DC_PRED, + } + } +} + +/// Encodes `yuv` as one VP8 key frame (the payload of a WebP `VP8 ` chunk). +pub fn encode_frame(yuv: &Yuv, params: Params) -> Result, Vp8Error> { + if yuv.width == 0 || yuv.height == 0 || yuv.width > MAX_DIMENSION || yuv.height > MAX_DIMENSION { + return Err(Vp8Error::Dimensions); + } + let (w, h) = (yuv.width as usize, yuv.height as usize); + if yuv.y.len() != w * h || yuv.u.len() != w.div_ceil(2) * h.div_ceil(2) || yuv.v.len() != yuv.u.len() { + return Err(Vp8Error::Planes); + } + let qi = quantizer_index(params.quality); + let mut enc = Encoder::new(yuv, qi); + for mby in 0..enc.mb_rows { + for mbx in 0..enc.mb_cols { + enc.encode_macroblock(mbx, mby); + } + } + // Pass 1 over the tokens: branch statistics → this frame's probabilities. + let mut stats = Stats::new(); + enc.walk_tokens( + |block, ty, ctx0| { + for_each_token_bool(block, ty, ctx0, |ev| { + if let Event::Node(ty, band, ctx, node, bit) = ev { + stats.node[ty][band][ctx][node][usize::from(bit)] += 1; + } + }); + }, + |_, _| {}, + ); + let probs = enc.choose_probs(&stats); + // The loop filter smooths block edges in proportion to the quantizer; none at the finest. + let loop_filter_level = if qi == 0 { 0 } else { (u32::from(qi) / 2 + 4).min(63) as u8 }; + Ok(enc.write_frame(&probs, loop_filter_level, qi)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn quantizer_curve_is_monotone_and_spans_the_range() { + assert_eq!(quantizer_index(100), 0); + assert_eq!(quantizer_index(0), 127); + let mut last = 0; + for q in (0..=100).rev() { + let qi = quantizer_index(q); + assert!(qi >= last, "quality {q}"); + last = qi; + } + assert!((8..=14).contains(&quantizer_index(90)), "{}", quantizer_index(90)); + assert!((24..=32).contains(&quantizer_index(75)), "{}", quantizer_index(75)); + } + + #[test] + fn tokens_cover_every_magnitude() { + for v in [0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 18, 19, 34, 35, 66, 67, 1000, 2048] { + let (tok, extra) = token_of(v); + let base = match extra { + None => i32::from(tok), + Some((c, bits)) => { + assert!(bits < (1 << PCAT[c].len()), "{v}"); + CAT_BASE[c] + bits as i32 + } + }; + assert_eq!(base, v); + assert_eq!(token_of(-v).0, tok); + } + } + + #[test] + fn block_eob_is_after_the_last_nonzero() { + let mut raster = [0i32; 16]; + let b = Block::from_raster(&raster, 1); + assert_eq!(b.eob, 1); + assert!(!b.has_coeffs()); + raster[ZIGZAG[5]] = 3; + let b = Block::from_raster(&raster, 0); + assert_eq!(b.eob, 6); + assert!(b.has_coeffs()); + raster[ZIGZAG[15]] = -1; + assert_eq!(Block::from_raster(&raster, 0).eob, 16); + } + + #[test] + fn rejects_bad_dimensions() { + let y = Yuv { width: 0, height: 4, y: vec![], u: vec![], v: vec![] }; + assert_eq!(encode_frame(&y, Params { quality: 80 }).unwrap_err(), Vp8Error::Dimensions); + let y = Yuv { width: 4, height: 4, y: vec![0; 15], u: vec![0; 4], v: vec![0; 4] }; + assert_eq!(encode_frame(&y, Params { quality: 80 }).unwrap_err(), Vp8Error::Planes); + } + + #[test] + fn frame_header_is_well_formed() { + let rgb: Vec = (0..(20 * 13 * 3)).map(|i| (i * 7 % 256) as u8).collect(); + let yuv = Yuv::from_rgb(&rgb, 20, 13, 3); + let frame = encode_frame(&yuv, Params { quality: 75 }).unwrap(); + assert_eq!(frame[0] & 1, 0, "key frame"); + assert_eq!((frame[0] >> 4) & 1, 1, "shown"); + assert_eq!(&frame[3..6], &[0x9d, 0x01, 0x2a]); + assert_eq!(u16::from_le_bytes([frame[6], frame[7]]) & 0x3fff, 20); + assert_eq!(u16::from_le_bytes([frame[8], frame[9]]) & 0x3fff, 13); + let first_size = (u32::from(frame[0]) | (u32::from(frame[1]) << 8) | (u32::from(frame[2]) << 16)) >> 5; + assert!((10 + first_size as usize) < frame.len()); + } +} diff --git a/crates/image-codecs/src/codecs/vp8/bool_encoder.rs b/crates/image-codecs/src/codecs/vp8/bool_encoder.rs new file mode 100644 index 000000000..d16341bce --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/bool_encoder.rs @@ -0,0 +1,273 @@ +//! The VP8 boolean entropy coder, writing side (RFC 6386 §7): every symbol is a bool with an +//! 8-bit probability of being zero; literals and tree-coded values are built from bools. + +use super::tables::Tree; + +/// Writes bools at given probabilities into a byte buffer. +pub struct BoolEncoder { + out: Vec, + range: u32, + bottom: u32, + bit_count: i32, +} + +impl Default for BoolEncoder { + fn default() -> Self { + Self::new() + } +} + +impl BoolEncoder { + pub fn new() -> Self { + BoolEncoder { out: Vec::new(), range: 255, bottom: 0, bit_count: 24 } + } + + /// Propagates a carry into the bytes already written (never past the start: the coded + /// value is below one). + fn add_one_to_output(&mut self) { + for b in self.out.iter_mut().rev() { + if *b == 255 { + *b = 0; + } else { + *b += 1; + return; + } + } + } + + /// Writes `bit` whose probability of being 0 is `prob / 256`. + pub fn put(&mut self, prob: u8, bit: bool) { + let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8); + if bit { + self.bottom = self.bottom.wrapping_add(split); + self.range -= split; + } else { + self.range = split; + } + while self.range < 128 { + self.range <<= 1; + if self.bottom & (1 << 31) != 0 { + self.add_one_to_output(); + } + self.bottom <<= 1; + self.bit_count -= 1; + if self.bit_count == 0 { + self.out.push((self.bottom >> 24) as u8); + self.bottom &= (1 << 24) - 1; + self.bit_count = 8; + } + } + } + + /// A bool at probability 1/2. + pub fn put_flag(&mut self, bit: bool) { + self.put(128, bit); + } + + /// An `n`-bit unsigned literal, most significant bit first, each bit at probability 1/2. + pub fn put_literal(&mut self, n: u32, value: u32) { + for i in (0..n).rev() { + self.put_flag((value >> i) & 1 != 0); + } + } + + /// A tree-coded value: the path of bools from the root to the leaf `value`, each node's bool + /// at `probs[node / 2]`. + pub fn put_tree(&mut self, tree: Tree, probs: &[u8], value: u8) { + self.put_tree_from(tree, probs, value, 0); + } + + /// [`Self::put_tree`] starting at node `start` (2 skips the first branch of the coefficient tree). + pub fn put_tree_from(&mut self, tree: Tree, probs: &[u8], value: u8, start: usize) { + // Find the path to the leaf by searching the tree; trees are tiny (≤ 22 entries). + let mut path = Vec::with_capacity(8); + if !Self::find(tree, start, value, &mut path) { + return; + } + for (node, bit) in path { + self.put(probs.get(node >> 1).copied().unwrap_or(128), bit); + } + } + + fn find(tree: Tree, node: usize, value: u8, path: &mut Vec<(usize, bool)>) -> bool { + for bit in [false, true] { + let i = node + usize::from(bit); + let Some(&entry) = tree.get(i) else { continue }; + path.push((node, bit)); + if entry <= 0 { + if (-entry) as u8 == value { + return true; + } + } else if Self::find(tree, entry as usize, value, path) { + return true; + } + path.pop(); + } + false + } + + /// Finishes the partition and returns its bytes. + pub fn finish(mut self) -> Vec { + let mut c = self.bit_count; + let mut v = self.bottom; + if c <= 32 && v & (1u32 << (32 - c)) != 0 { + self.add_one_to_output(); + } + v <<= (c & 7) as u32; + c >>= 3; + while c > 0 { + v <<= 8; + c -= 1; + } + for _ in 0..4 { + self.out.push((v >> 24) as u8); + v <<= 8; + } + self.out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The decoder from RFC 6386 §7.3, for the round-trip tests. + struct BoolDecoder<'a> { + input: &'a [u8], + pos: usize, + range: u32, + value: u32, + bit_count: i32, + } + + impl<'a> BoolDecoder<'a> { + fn new(input: &'a [u8]) -> Self { + let mut value = 0; + for i in 0..2 { + value = (value << 8) | u32::from(input.get(i).copied().unwrap_or(0)); + } + BoolDecoder { input, pos: 2, range: 255, value, bit_count: 0 } + } + fn get(&mut self, prob: u8) -> bool { + let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8); + let big_split = split << 8; + let bit = if self.value >= big_split { + self.range -= split; + self.value -= big_split; + true + } else { + self.range = split; + false + }; + while self.range < 128 { + self.value <<= 1; + self.range <<= 1; + self.bit_count += 1; + if self.bit_count == 8 { + self.bit_count = 0; + self.value |= u32::from(self.input.get(self.pos).copied().unwrap_or(0)); + self.pos += 1; + } + } + bit + } + fn literal(&mut self, n: u32) -> u32 { + (0..n).fold(0, |v, _| (v << 1) | u32::from(self.get(128))) + } + fn tree(&mut self, tree: Tree, probs: &[u8], start: usize) -> u8 { + let mut i = start; + loop { + let bit = self.get(probs[i >> 1]); + let entry = tree[i + usize::from(bit)]; + if entry <= 0 { + return (-entry) as u8; + } + i = entry as usize; + } + } + } + + #[test] + fn bools_round_trip_at_every_probability() { + let mut rng = 0x1234_5678_9abc_def0u64; + let mut next = || { + rng ^= rng << 13; + rng ^= rng >> 7; + rng ^= rng << 17; + rng + }; + let symbols: Vec<(u8, bool)> = (0..20_000) + .map(|_| { + let r = next(); + let prob = (r & 0xff) as u8; + // Bits follow their probability, roughly, so both paths are exercised. + ((prob.max(1)), (r >> 8) & 0xff >= u64::from(prob)) + }) + .collect(); + let mut e = BoolEncoder::new(); + for &(p, b) in &symbols { + e.put(p, b); + } + let bytes = e.finish(); + let mut d = BoolDecoder::new(&bytes); + for &(p, b) in &symbols { + assert_eq!(d.get(p), b); + } + } + + #[test] + fn literals_and_trees_round_trip() { + use super::super::tables::*; + let mut e = BoolEncoder::new(); + e.put_literal(7, 93); + e.put_literal(19, 0x7_ffff); + e.put_literal(1, 0); + e.put_tree(KF_YMODE_TREE, &KF_YMODE_PROB, TM_PRED); + e.put_tree(KF_YMODE_TREE, &KF_YMODE_PROB, B_PRED); + e.put_tree(UV_MODE_TREE, &KF_UV_MODE_PROB, H_PRED); + for m in 0..10u8 { + e.put_tree(BMODE_TREE, &KF_BMODE_PROBS[3][7], m); + } + let probs = [200u8, 150, 100, 128, 128, 128, 128, 128, 128, 128, 128]; + for t in 0..12u8 { + e.put_tree(COEFF_TREE, &probs, t); + } + // From node 2 (after a zero token) the end-of-block leaf is unreachable: tokens 0..=10. + for t in 0..11u8 { + e.put_tree_from(COEFF_TREE, &probs, t, 2); + } + let bytes = e.finish(); + let mut d = BoolDecoder::new(&bytes); + assert_eq!(d.literal(7), 93); + assert_eq!(d.literal(19), 0x7_ffff); + assert_eq!(d.literal(1), 0); + assert_eq!(d.tree(KF_YMODE_TREE, &KF_YMODE_PROB, 0), TM_PRED); + assert_eq!(d.tree(KF_YMODE_TREE, &KF_YMODE_PROB, 0), B_PRED); + assert_eq!(d.tree(UV_MODE_TREE, &KF_UV_MODE_PROB, 0), H_PRED); + for m in 0..10u8 { + assert_eq!(d.tree(BMODE_TREE, &KF_BMODE_PROBS[3][7], 0), m); + } + for t in 0..12u8 { + assert_eq!(d.tree(COEFF_TREE, &probs, 0), t); + } + for t in 0..11u8 { + assert_eq!(d.tree(COEFF_TREE, &probs, 2), t); + } + } + + #[test] + fn carry_propagates_through_full_bytes() { + // Many ones at a tiny probability push the interval up and force carries. + let mut e = BoolEncoder::new(); + for _ in 0..3000 { + e.put(1, true); + e.put(255, false); + } + let bytes = e.finish(); + let mut d = BoolDecoder::new(&bytes); + for _ in 0..3000 { + assert!(d.get(1)); + assert!(!d.get(255)); + } + } +} diff --git a/crates/image-codecs/src/codecs/vp8/predict.rs b/crates/image-codecs/src/codecs/vp8/predict.rs new file mode 100644 index 000000000..7ddea0423 --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/predict.rs @@ -0,0 +1,382 @@ +//! Intra prediction (RFC 6386 §12): whole-macroblock luma and chroma modes, and the ten 4×4 +//! sub-block modes. Predictions read the frame's *unfiltered* reconstruction, which is what the +//! decoder predicts from too (the loop filter runs on the finished frame). + +#[cfg(test)] +use super::tables::{B_DC_PRED, DC_PRED}; +use super::tables::{B_HD_PRED, B_HE_PRED, B_HU_PRED, B_LD_PRED, B_RD_PRED, B_TM_PRED, B_VE_PRED, B_VL_PRED, B_VR_PRED, H_PRED, TM_PRED, V_PRED}; + +/// A plane of the reconstruction with the virtual border VP8 defines: the row above the frame +/// reads as 127, the column left of it as 129, and the pixel above-left of the frame as 127. +pub struct Plane { + pub data: Vec, + pub width: usize, +} + +impl Plane { + pub fn new(width: usize, height: usize) -> Self { + Plane { data: vec![0; width * height], width } + } + + #[inline] + pub fn at(&self, x: isize, y: isize) -> u8 { + if y < 0 { + 127 + } else if x < 0 { + 129 + } else { + self.data.get(y as usize * self.width + x as usize).copied().unwrap_or(127) + } + } + + /// The row above an `n`-wide block at (`x`, `y`), plus the four pixels to its upper right + /// (sub-block modes use them; `avail_right` says whether those exist in the frame yet). + pub fn above(&self, x: usize, y: usize, n: usize, out: &mut [u8]) { + for (i, o) in out.iter_mut().enumerate().take(n) { + *o = self.at((x + i) as isize, y as isize - 1); + } + } + + pub fn left(&self, x: usize, y: usize, n: usize, out: &mut [u8]) { + for (i, o) in out.iter_mut().enumerate().take(n) { + *o = self.at(x as isize - 1, (y + i) as isize); + } + } + + /// The pixel above-left of (`x`, `y`). + pub fn corner(&self, x: usize, y: usize) -> u8 { + self.at(x as isize - 1, y as isize - 1) + } + + #[inline] + pub fn set(&mut self, x: usize, y: usize, v: u8) { + if let Some(p) = self.data.get_mut(y * self.width + x) { + *p = v; + } + } +} + +#[inline] +fn clamp255(v: i32) -> u8 { + v.clamp(0, 255) as u8 +} + +/// Whole-block prediction of an `n × n` block at (`x`, `y`) in `plane` (16 for luma, 8 for chroma) +/// with one of DC / V / H / TM, written row-major into `out`. +pub fn predict_block(plane: &Plane, x: usize, y: usize, n: usize, mode: u8, out: &mut [u8]) { + let mut above = [0u8; 16]; + let mut left = [0u8; 16]; + plane.above(x, y, n, &mut above); + plane.left(x, y, n, &mut left); + let top_row = y == 0; + let left_col = x == 0; + match mode { + V_PRED => { + for r in 0..n { + out[r * n..r * n + n].copy_from_slice(&above[..n]); + } + } + H_PRED => { + for r in 0..n { + out[r * n..r * n + n].fill(left[r]); + } + } + TM_PRED => { + let p = i32::from(plane.corner(x, y)); + for r in 0..n { + for c in 0..n { + out[r * n + c] = clamp255(i32::from(left[r]) + i32::from(above[c]) - p); + } + } + } + _ => { + // DC: the average of the visible edge pixels; 128 in the top-left corner. + let shift = if n == 16 { 4 } else { 3 }; + let v = match (top_row, left_col) { + (true, true) => 128, + (true, false) => { + let s: u32 = left[..n].iter().map(|&v| u32::from(v)).sum(); + ((s + (1 << (shift - 1))) >> shift) as u8 + } + (false, true) => { + let s: u32 = above[..n].iter().map(|&v| u32::from(v)).sum(); + ((s + (1 << (shift - 1))) >> shift) as u8 + } + (false, false) => { + let s: u32 = above[..n].iter().chain(&left[..n]).map(|&v| u32::from(v)).sum(); + ((s + (1 << shift)) >> (shift + 1)) as u8 + } + }; + out[..n * n].fill(v); + } + } +} + +#[inline] +fn avg2(a: u8, b: u8) -> u8 { + ((u16::from(a) + u16::from(b) + 1) >> 1) as u8 +} + +#[inline] +fn avg3(a: u8, b: u8, c: u8) -> u8 { + ((u16::from(a) + 2 * u16::from(b) + u16::from(c) + 2) >> 2) as u8 +} + +/// The edge pixels a 4×4 sub-block prediction reads: `above[0..8]` (the four above and the four +/// above-right), `left[0..4]`, and the corner `p` above-left. +pub struct SubEdges { + pub above: [u8; 8], + pub left: [u8; 4], + pub p: u8, +} + +impl SubEdges { + /// Edges of sub-block `b` (0..16, raster) of the macroblock at (`mx`, `my`) in luma pixels. + /// The above-right pixels of the right-column sub-blocks come from the row above the + /// macroblock (positions (-1, 16..20)), replicated from (-1, 15) for the rightmost macroblock + /// (RFC 6386 §12.3); for the top macroblock row they read 127 like every above pixel. + pub fn gather(plane: &Plane, mx: usize, my: usize, b: usize, mb_cols: usize) -> SubEdges { + let bx = mx + (b & 3) * 4; + let by = my + (b >> 2) * 4; + let mut above = [0u8; 8]; + let mut left = [0u8; 4]; + plane.above(bx, by, 4, &mut above); + plane.left(bx, by, 4, &mut left); + let p = plane.corner(bx, by); + if b & 3 == 3 { + // Right column: the above-right pixels are those above the macroblock's right edge. + let last_col = mx / 16 + 1 == mb_cols; + for i in 0..4 { + let ax = if last_col { mx as isize + 15 } else { mx as isize + 16 + i as isize }; + above[4 + i] = plane.at(ax, my as isize - 1); + } + } else { + for i in 0..4 { + above[4 + i] = plane.at((bx + 4 + i) as isize, by as isize - 1); + } + } + SubEdges { above, left, p } + } +} + +/// Predicts one 4×4 sub-block with `mode` into `out` (row-major), RFC 6386 §12.3. +// Written as the specification's position-by-position assignments, which index on purpose. +#[allow(clippy::needless_range_loop)] +pub fn predict_sub(e: &SubEdges, mode: u8, out: &mut [u8; 16]) { + let a = &e.above; + let l = &e.left; + // E: the nine edge pixels from bottom-left, through the corner, to the right. + let ed = [l[3], l[2], l[1], l[0], e.p, a[0], a[1], a[2], a[3]]; + // avg3 centred on E[i], avg2 of E[i] and E[i + 1]. + let e3 = |i: usize| avg3(ed[i - 1], ed[i], ed[i + 1]); + let e2 = |i: usize| avg2(ed[i], ed[i + 1]); + // The same over the above row (A[-1] is P). + let ap = |i: isize| if i < 0 { e.p } else { a[i as usize] }; + let a3 = |i: isize| avg3(ap(i - 1), ap(i), ap(i + 1)); + let a2 = |i: isize| avg2(ap(i), ap(i + 1)); + let lp = |i: isize| if i < 0 { e.p } else { l[i as usize] }; + let l3 = |i: isize| avg3(lp(i - 1), lp(i), lp(i + 1)); + let l2 = |i: isize| avg2(lp(i), lp(i + 1)); + let mut b = [[0u8; 4]; 4]; + match mode { + B_TM_PRED => { + for r in 0..4 { + for c in 0..4 { + b[r][c] = clamp255(i32::from(l[r]) + i32::from(a[c]) - i32::from(e.p)); + } + } + } + B_VE_PRED => { + for c in 0..4 { + let v = a3(c as isize); + for r in 0..4 { + b[r][c] = v; + } + } + } + B_HE_PRED => { + let rows = [l3(0), l3(1), l3(2), avg3(l[2], l[3], l[3])]; + for r in 0..4 { + b[r] = [rows[r]; 4]; + } + } + B_LD_PRED => { + b[0][0] = a3(1); + b[0][1] = a3(2); + b[1][0] = b[0][1]; + b[0][2] = a3(3); + b[1][1] = b[0][2]; + b[2][0] = b[0][2]; + b[0][3] = a3(4); + b[1][2] = b[0][3]; + b[2][1] = b[0][3]; + b[3][0] = b[0][3]; + b[1][3] = a3(5); + b[2][2] = b[1][3]; + b[3][1] = b[1][3]; + b[2][3] = a3(6); + b[3][2] = b[2][3]; + b[3][3] = avg3(a[6], a[7], a[7]); + } + B_RD_PRED => { + b[3][0] = e3(1); + b[3][1] = e3(2); + b[2][0] = b[3][1]; + b[3][2] = e3(3); + b[2][1] = b[3][2]; + b[1][0] = b[3][2]; + b[3][3] = e3(4); + b[2][2] = b[3][3]; + b[1][1] = b[3][3]; + b[0][0] = b[3][3]; + b[2][3] = e3(5); + b[1][2] = b[2][3]; + b[0][1] = b[2][3]; + b[1][3] = e3(6); + b[0][2] = b[1][3]; + b[0][3] = e3(7); + } + B_VR_PRED => { + b[3][0] = e3(2); + b[2][0] = e3(3); + b[3][1] = e3(4); + b[1][0] = b[3][1]; + b[2][1] = e2(4); + b[0][0] = b[2][1]; + b[3][2] = e3(5); + b[1][1] = b[3][2]; + b[2][2] = e2(5); + b[0][1] = b[2][2]; + b[3][3] = e3(6); + b[1][2] = b[3][3]; + b[2][3] = e2(6); + b[0][2] = b[2][3]; + b[1][3] = e3(7); + b[0][3] = e2(7); + } + B_VL_PRED => { + b[0][0] = a2(0); + b[1][0] = a3(1); + b[2][0] = a2(1); + b[0][1] = b[2][0]; + b[1][1] = a3(2); + b[3][0] = b[1][1]; + b[2][1] = a2(2); + b[0][2] = b[2][1]; + b[3][1] = a3(3); + b[1][2] = b[3][1]; + b[2][2] = a2(3); + b[0][3] = b[2][2]; + b[3][2] = a3(4); + b[1][3] = b[3][2]; + b[2][3] = a3(5); + b[3][3] = a3(6); + } + B_HD_PRED => { + b[3][0] = e2(0); + b[3][1] = e3(1); + b[2][0] = e2(1); + b[3][2] = b[2][0]; + b[2][1] = e3(2); + b[3][3] = b[2][1]; + b[2][2] = e2(2); + b[1][0] = b[2][2]; + b[2][3] = e3(3); + b[1][1] = b[2][3]; + b[1][2] = e2(3); + b[0][0] = b[1][2]; + b[1][3] = e3(4); + b[0][1] = b[1][3]; + b[0][2] = e3(5); + b[0][3] = e3(6); + } + B_HU_PRED => { + b[0][0] = l2(0); + b[0][1] = l3(1); + b[0][2] = l2(1); + b[1][0] = b[0][2]; + b[0][3] = l3(2); + b[1][1] = b[0][3]; + b[1][2] = l2(2); + b[2][0] = b[1][2]; + b[1][3] = avg3(l[2], l[3], l[3]); + b[2][1] = b[1][3]; + b[2][2] = l[3]; + b[2][3] = l[3]; + b[3] = [l[3]; 4]; + } + _ => { + // B_DC_PRED: the average of the four above and four left pixels. + let s: u32 = a[..4].iter().chain(&l[..]).map(|&v| u32::from(v)).sum(); + b = [[((s + 4) >> 3) as u8; 4]; 4]; + } + } + for r in 0..4 { + out[r * 4..r * 4 + 4].copy_from_slice(&b[r]); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn border_values() { + let p = Plane::new(16, 16); + assert_eq!(p.at(-1, -1), 127); + assert_eq!(p.at(5, -1), 127); + assert_eq!(p.at(-1, 5), 129); + assert_eq!(p.corner(0, 0), 127); + assert_eq!(p.corner(0, 16), 129); + assert_eq!(p.corner(16, 0), 127); + } + + #[test] + fn dc_prediction_edge_cases() { + let mut p = Plane::new(32, 32); + for y in 0..32 { + for x in 0..32 { + p.set(x, y, if y < 16 { 10 } else { 200 }); + } + } + let mut out = [0u8; 256]; + predict_block(&p, 0, 0, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == 128), "top-left is 128"); + predict_block(&p, 16, 0, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == 10), "top row averages the left column only"); + predict_block(&p, 0, 16, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == 10), "left column averages the above row only"); + predict_block(&p, 16, 16, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == (10 + 200) / 2)); + predict_block(&p, 16, 16, 16, TM_PRED, &mut out); + // L + A - P = 200 + 10 - 10 = 200 everywhere. + assert!(out.iter().all(|&v| v == 200)); + } + + #[test] + fn sub_block_modes_are_in_range_and_dc_averages() { + let mut p = Plane::new(32, 32); + for y in 0..32 { + for x in 0..32 { + p.set(x, y, ((x * 7 + y * 13) % 256) as u8); + } + } + for b in 0..16 { + let e = SubEdges::gather(&p, 16, 16, b, 2); + let mut out = [0u8; 16]; + for mode in 0..10u8 { + predict_sub(&e, mode, &mut out); + } + predict_sub(&e, B_DC_PRED, &mut out); + let s: u32 = e.above[..4].iter().chain(&e.left).map(|&v| u32::from(v)).sum(); + assert!(out.iter().all(|&v| u32::from(v) == (s + 4) >> 3)); + } + // Rightmost macroblock: the above-right pixels replicate (-1, 15). + let e = SubEdges::gather(&p, 16, 16, 3, 2); + assert!(e.above[4..].iter().all(|&v| v == p.at(31, 15))); + // Top row: everything above is 127. + let e = SubEdges::gather(&p, 0, 0, 3, 2); + assert!(e.above.iter().all(|&v| v == 127)); + assert_eq!(e.p, 127); + } +} diff --git a/crates/image-codecs/src/codecs/vp8/tables.rs b/crates/image-codecs/src/codecs/vp8/tables.rs new file mode 100644 index 000000000..261231e9e --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/tables.rs @@ -0,0 +1,593 @@ +//! Constant tables of the VP8 bitstream, as published in RFC 6386. The four large tables were +//! generated from the RFC text (sections 11.5, 13.4, 13.5 and 14.1); the small ones are +//! transcribed from sections 11.2, 11.4, 13.2 and 13.3. + +/// Luma intra modes of a macroblock (RFC 6386 §11.2), in the order of `intra_mbmode`. +pub const DC_PRED: u8 = 0; +pub const V_PRED: u8 = 1; +pub const H_PRED: u8 = 2; +pub const TM_PRED: u8 = 3; +pub const B_PRED: u8 = 4; + +/// Sub-block intra modes (RFC 6386 §11.2), in the order of `intra_bmode`. +pub const B_DC_PRED: u8 = 0; +pub const B_TM_PRED: u8 = 1; +pub const B_VE_PRED: u8 = 2; +pub const B_HE_PRED: u8 = 3; +pub const B_LD_PRED: u8 = 4; +pub const B_RD_PRED: u8 = 5; +pub const B_VR_PRED: u8 = 6; +pub const B_VL_PRED: u8 = 7; +pub const B_HD_PRED: u8 = 8; +pub const B_HU_PRED: u8 = 9; + +/// A coding tree as in the RFC: positive entries index the next node pair, `-leaf` entries end +/// a path (the leaf value is negated; `-0` is written as the sentinel below). +pub type Tree = &'static [i8]; + +/// `kf_ymode_tree`: `B_PRED` = "0", then DC "100", V "101", H "110", TM "111". +pub const KF_YMODE_TREE: Tree = &[-(B_PRED as i8), 2, 4, 6, -(DC_PRED as i8), -(V_PRED as i8), -(H_PRED as i8), -(TM_PRED as i8)]; +pub const KF_YMODE_PROB: [u8; 4] = [145, 156, 163, 128]; + +/// `uv_mode_tree`: DC "0", V "10", H "110", TM "111". +pub const UV_MODE_TREE: Tree = &[-(DC_PRED as i8), 2, -(V_PRED as i8), 4, -(H_PRED as i8), -(TM_PRED as i8)]; +pub const KF_UV_MODE_PROB: [u8; 3] = [142, 114, 183]; + +/// `bmode_tree` (RFC 6386 §11.2). +pub const BMODE_TREE: Tree = &[ + -(B_DC_PRED as i8), + 2, + -(B_TM_PRED as i8), + 4, + -(B_VE_PRED as i8), + 6, + 8, + 12, + -(B_HE_PRED as i8), + 10, + -(B_RD_PRED as i8), + -(B_VR_PRED as i8), + -(B_LD_PRED as i8), + 14, + -(B_VL_PRED as i8), + 16, + -(B_HD_PRED as i8), + -(B_HU_PRED as i8), +]; + +/// Coefficient tokens (RFC 6386 §13.2). +pub const DCT_0: u8 = 0; +pub const DCT_1: u8 = 1; +pub const DCT_2: u8 = 2; +pub const DCT_3: u8 = 3; +pub const DCT_4: u8 = 4; +pub const DCT_CAT1: u8 = 5; +pub const DCT_CAT2: u8 = 6; +pub const DCT_CAT3: u8 = 7; +pub const DCT_CAT4: u8 = 8; +pub const DCT_CAT5: u8 = 9; +pub const DCT_CAT6: u8 = 10; +pub const DCT_EOB: u8 = 11; + +/// `coeff_tree` (RFC 6386 §13.2). The EOB leaf is at index 0, so the tree is also walked from +/// index 2 when an EOB cannot follow (after a zero token). +pub const COEFF_TREE: Tree = &[ + -(DCT_EOB as i8), + 2, + -(DCT_0 as i8), + 4, + -(DCT_1 as i8), + 6, + 8, + 12, + -(DCT_2 as i8), + 10, + -(DCT_3 as i8), + -(DCT_4 as i8), + 14, + 16, + -(DCT_CAT1 as i8), + -(DCT_CAT2 as i8), + 18, + 20, + -(DCT_CAT3 as i8), + -(DCT_CAT4 as i8), + -(DCT_CAT5 as i8), + -(DCT_CAT6 as i8), +]; + +/// Extra-bit probabilities of the six value categories (RFC 6386 §13.2), most significant bit +/// first; the category bases are 5, 7, 11, 19, 35 and 67. +pub const PCAT: [&[u8]; 6] = + [&[159], &[165, 145], &[173, 148, 140], &[176, 155, 140, 135], &[180, 157, 141, 134, 130], &[254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129]]; +pub const CAT_BASE: [i32; 6] = [5, 7, 11, 19, 35, 67]; + +/// Coefficient position → probability band (RFC 6386 §13.3). +pub const COEFF_BANDS: [usize; 16] = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7]; + +/// Zig-zag scan: the raster index of the coefficient coded at each position (RFC 6386 §13). +pub const ZIGZAG: [usize; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15]; + +/// Default token probabilities `[plane][band][context][token]` (RFC 6386 §13.5). +pub const DEFAULT_COEFF_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [ + [ + [ + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128], + [189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128], + [106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128], + ], + [ + [1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128], + [181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128], + [78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128], + ], + [ + [1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128], + [184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128], + [77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128], + ], + [ + [1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128], + [170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128], + [37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128], + ], + [ + [1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128], + [207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128], + [102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128], + ], + [ + [1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128], + [177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128], + [80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128], + ], + [ + [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + ], + ], + [ + [ + [198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62], + [131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1], + [68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128], + ], + [ + [1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128], + [184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128], + [81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128], + ], + [ + [1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128], + [99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128], + [23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128], + ], + [ + [1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128], + [109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128], + [44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128], + ], + [ + [1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128], + [94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128], + [22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128], + ], + [ + [1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128], + [124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128], + [35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128], + ], + [ + [1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128], + [121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128], + [45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128], + ], + [ + [1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128], + [203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128], + [137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128], + ], + ], + [ + [ + [253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128], + [175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128], + [73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128], + ], + [ + [1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128], + [239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128], + [155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128], + ], + [ + [1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128], + [201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128], + [69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128], + ], + [ + [1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128], + [223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128], + [141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128], + ], + [ + [1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128], + [190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128], + [149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128], + [213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128], + [55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + ], + ], + [ + [ + [202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255], + [126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128], + [61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128], + ], + [ + [1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128], + [166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128], + [39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128], + ], + [ + [1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128], + [124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128], + [24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128], + ], + [ + [1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128], + [149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128], + [28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128], + ], + [ + [1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128], + [123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128], + [20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128], + ], + [ + [1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128], + [168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128], + [47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128], + ], + [ + [1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128], + [141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128], + [42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128], + ], + [ + [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + ], +]; + +/// Probabilities with which each token probability's update flag is coded (RFC 6386 §13.4). +pub const COEFF_UPDATE_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [ + [ + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255], + [249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255], + [234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255], + [250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], + [ + [ + [217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255], + [234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255], + ], + [ + [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], + [ + [ + [186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255], + [234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255], + [251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255], + ], + [ + [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], + [ + [ + [248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255], + [248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255], + [248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], +]; + +/// DC dequantization factors by quantizer index (RFC 6386 §14.1). +pub const DC_QLOOKUP: [i32; 128] = [ + 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, + 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, + 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, + 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, +]; + +/// AC dequantization factors by quantizer index (RFC 6386 §14.1). +pub const AC_QLOOKUP: [i32; 128] = [ + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, + 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 181, 185, 189, 193, 197, + 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, +]; + +/// Key-frame sub-block mode probabilities `[above mode][left mode][tree node]` (RFC 6386 §11.5). +pub const KF_BMODE_PROBS: [[[u8; 9]; 10]; 10] = [ + [ + [231, 120, 48, 89, 115, 113, 120, 152, 112], + [152, 179, 64, 126, 170, 118, 46, 70, 95], + [175, 69, 143, 80, 85, 82, 72, 155, 103], + [56, 58, 10, 171, 218, 189, 17, 13, 152], + [144, 71, 10, 38, 171, 213, 144, 34, 26], + [114, 26, 17, 163, 44, 195, 21, 10, 173], + [121, 24, 80, 195, 26, 62, 44, 64, 85], + [170, 46, 55, 19, 136, 160, 33, 206, 71], + [63, 20, 8, 114, 114, 208, 12, 9, 226], + [81, 40, 11, 96, 182, 84, 29, 16, 36], + ], + [ + [134, 183, 89, 137, 98, 101, 106, 165, 148], + [72, 187, 100, 130, 157, 111, 32, 75, 80], + [66, 102, 167, 99, 74, 62, 40, 234, 128], + [41, 53, 9, 178, 241, 141, 26, 8, 107], + [104, 79, 12, 27, 217, 255, 87, 17, 7], + [74, 43, 26, 146, 73, 166, 49, 23, 157], + [65, 38, 105, 160, 51, 52, 31, 115, 128], + [87, 68, 71, 44, 114, 51, 15, 186, 23], + [47, 41, 14, 110, 182, 183, 21, 17, 194], + [66, 45, 25, 102, 197, 189, 23, 18, 22], + ], + [ + [88, 88, 147, 150, 42, 46, 45, 196, 205], + [43, 97, 183, 117, 85, 38, 35, 179, 61], + [39, 53, 200, 87, 26, 21, 43, 232, 171], + [56, 34, 51, 104, 114, 102, 29, 93, 77], + [107, 54, 32, 26, 51, 1, 81, 43, 31], + [39, 28, 85, 171, 58, 165, 90, 98, 64], + [34, 22, 116, 206, 23, 34, 43, 166, 73], + [68, 25, 106, 22, 64, 171, 36, 225, 114], + [34, 19, 21, 102, 132, 188, 16, 76, 124], + [62, 18, 78, 95, 85, 57, 50, 48, 51], + ], + [ + [193, 101, 35, 159, 215, 111, 89, 46, 111], + [60, 148, 31, 172, 219, 228, 21, 18, 111], + [112, 113, 77, 85, 179, 255, 38, 120, 114], + [40, 42, 1, 196, 245, 209, 10, 25, 109], + [100, 80, 8, 43, 154, 1, 51, 26, 71], + [88, 43, 29, 140, 166, 213, 37, 43, 154], + [61, 63, 30, 155, 67, 45, 68, 1, 209], + [142, 78, 78, 16, 255, 128, 34, 197, 171], + [41, 40, 5, 102, 211, 183, 4, 1, 221], + [51, 50, 17, 168, 209, 192, 23, 25, 82], + ], + [ + [125, 98, 42, 88, 104, 85, 117, 175, 82], + [95, 84, 53, 89, 128, 100, 113, 101, 45], + [75, 79, 123, 47, 51, 128, 81, 171, 1], + [57, 17, 5, 71, 102, 57, 53, 41, 49], + [115, 21, 2, 10, 102, 255, 166, 23, 6], + [38, 33, 13, 121, 57, 73, 26, 1, 85], + [41, 10, 67, 138, 77, 110, 90, 47, 114], + [101, 29, 16, 10, 85, 128, 101, 196, 26], + [57, 18, 10, 102, 102, 213, 34, 20, 43], + [117, 20, 15, 36, 163, 128, 68, 1, 26], + ], + [ + [138, 31, 36, 171, 27, 166, 38, 44, 229], + [67, 87, 58, 169, 82, 115, 26, 59, 179], + [63, 59, 90, 180, 59, 166, 93, 73, 154], + [40, 40, 21, 116, 143, 209, 34, 39, 175], + [57, 46, 22, 24, 128, 1, 54, 17, 37], + [47, 15, 16, 183, 34, 223, 49, 45, 183], + [46, 17, 33, 183, 6, 98, 15, 32, 183], + [65, 32, 73, 115, 28, 128, 23, 128, 205], + [40, 3, 9, 115, 51, 192, 18, 6, 223], + [87, 37, 9, 115, 59, 77, 64, 21, 47], + ], + [ + [104, 55, 44, 218, 9, 54, 53, 130, 226], + [64, 90, 70, 205, 40, 41, 23, 26, 57], + [54, 57, 112, 184, 5, 41, 38, 166, 213], + [30, 34, 26, 133, 152, 116, 10, 32, 134], + [75, 32, 12, 51, 192, 255, 160, 43, 51], + [39, 19, 53, 221, 26, 114, 32, 73, 255], + [31, 9, 65, 234, 2, 15, 1, 118, 73], + [88, 31, 35, 67, 102, 85, 55, 186, 85], + [56, 21, 23, 111, 59, 205, 45, 37, 192], + [55, 38, 70, 124, 73, 102, 1, 34, 98], + ], + [ + [102, 61, 71, 37, 34, 53, 31, 243, 192], + [69, 60, 71, 38, 73, 119, 28, 222, 37], + [68, 45, 128, 34, 1, 47, 11, 245, 171], + [62, 17, 19, 70, 146, 85, 55, 62, 70], + [75, 15, 9, 9, 64, 255, 184, 119, 16], + [37, 43, 37, 154, 100, 163, 85, 160, 1], + [63, 9, 92, 136, 28, 64, 32, 201, 85], + [86, 6, 28, 5, 64, 255, 25, 248, 1], + [56, 8, 17, 132, 137, 255, 55, 116, 128], + [58, 15, 20, 82, 135, 57, 26, 121, 40], + ], + [ + [164, 50, 31, 137, 154, 133, 25, 35, 218], + [51, 103, 44, 131, 131, 123, 31, 6, 158], + [86, 40, 64, 135, 148, 224, 45, 183, 128], + [22, 26, 17, 131, 240, 154, 14, 1, 209], + [83, 12, 13, 54, 192, 255, 68, 47, 28], + [45, 16, 21, 91, 64, 222, 7, 1, 197], + [56, 21, 39, 155, 60, 138, 23, 102, 213], + [85, 26, 85, 85, 128, 128, 32, 146, 171], + [18, 11, 7, 63, 144, 171, 4, 4, 246], + [35, 27, 10, 146, 174, 171, 12, 26, 128], + ], + [ + [190, 80, 35, 99, 180, 80, 126, 54, 45], + [85, 126, 47, 87, 176, 51, 41, 20, 32], + [101, 75, 128, 139, 118, 146, 116, 128, 85], + [56, 41, 15, 176, 236, 85, 37, 9, 62], + [146, 36, 19, 30, 171, 255, 97, 27, 20], + [71, 30, 17, 119, 118, 255, 17, 18, 138], + [101, 38, 60, 138, 55, 70, 43, 26, 142], + [138, 45, 61, 62, 219, 1, 81, 188, 64], + [32, 41, 20, 117, 151, 142, 20, 21, 163], + [112, 19, 12, 61, 195, 128, 48, 4, 24], + ], +]; diff --git a/crates/image-codecs/src/codecs/vp8/transform.rs b/crates/image-codecs/src/codecs/vp8/transform.rs new file mode 100644 index 000000000..398bf7130 --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/transform.rs @@ -0,0 +1,183 @@ +//! The 4×4 transforms. The inverse DCT and inverse WHT are the bit-exact procedures of RFC 6386 +//! §14.3 and §14.4 (the encoder reconstructs with them so its prediction matches the decoder's). +//! The forward transforms are their derived counterparts: the inverse DCT is twice the +//! orthonormal transform per dimension and then `>> 3`, so coefficients are twice the +//! orthonormal DCT of the residual; the inverse WHT is an unnormalized Hadamard pair `>> 3`, so +//! the Y2 input is half the Hadamard pair of the DC values. + +/// `sqrt(2) * cos(pi / 8) - 1` and `sqrt(2) * sin(pi / 8)` in 16.16 fixed point (RFC 6386 §14.4). +const C1: i32 = 20091; +const S1: i32 = 35468; + +/// Residual (16 values, row-major) → coefficients (row-major; the zig-zag is applied when coding). +pub fn fdct(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + // Columns first (so that the row pass mirrors the decoder's order of rounding steps). + for i in 0..4 { + let (x0, x1, x2, x3) = (input[i], input[4 + i], input[8 + i], input[12 + i]); + let a = x0 + x3; + let b = x1 + x2; + let c = x1 - x2; + let d = x0 - x3; + tmp[i] = a + b; + tmp[8 + i] = a - b; + // X1 = d * sqrt2cos(pi/8) + c * sqrt2sin(pi/8); X3 = d * sqrt2sin(pi/8) - c * sqrt2cos(pi/8). + tmp[4 + i] = ((d * (65536 + C1) + c * S1 + 32768) >> 16).clamp(-32768, 32767); + tmp[12 + i] = ((d * S1 - c * (65536 + C1) + 32768) >> 16).clamp(-32768, 32767); + } + let mut out = [0i32; 16]; + for r in 0..4 { + let (x0, x1, x2, x3) = (tmp[r * 4], tmp[r * 4 + 1], tmp[r * 4 + 2], tmp[r * 4 + 3]); + let a = x0 + x3; + let b = x1 + x2; + let c = x1 - x2; + let d = x0 - x3; + // Two passes give 4× the orthonormal transform; the coefficients are 2× it. + out[r * 4] = (a + b + 1) >> 1; + out[r * 4 + 2] = (a - b + 1) >> 1; + out[r * 4 + 1] = ((d * (65536 + C1) + c * S1 + 65536) >> 17).clamp(-32768, 32767); + out[r * 4 + 3] = ((d * S1 - c * (65536 + C1) + 65536) >> 17).clamp(-32768, 32767); + } + out +} + +/// Dequantized coefficients → residual, bit-exact with the decoder (RFC 6386 §14.4). +pub fn idct(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + for i in 0..4 { + let (ip0, ip4, ip8, ip12) = (input[i], input[4 + i], input[8 + i], input[12 + i]); + let a1 = ip0 + ip8; + let b1 = ip0 - ip8; + let temp1 = (ip4 * S1) >> 16; + let temp2 = ip12 + ((ip12 * C1) >> 16); + let c1 = temp1 - temp2; + let temp1 = ip4 + ((ip4 * C1) >> 16); + let temp2 = (ip12 * S1) >> 16; + let d1 = temp1 + temp2; + tmp[i] = a1 + d1; + tmp[12 + i] = a1 - d1; + tmp[4 + i] = b1 + c1; + tmp[8 + i] = b1 - c1; + } + let mut out = [0i32; 16]; + for r in 0..4 { + let (ip0, ip1, ip2, ip3) = (tmp[r * 4], tmp[r * 4 + 1], tmp[r * 4 + 2], tmp[r * 4 + 3]); + let a1 = ip0 + ip2; + let b1 = ip0 - ip2; + let temp1 = (ip1 * S1) >> 16; + let temp2 = ip3 + ((ip3 * C1) >> 16); + let c1 = temp1 - temp2; + let temp1 = ip1 + ((ip1 * C1) >> 16); + let temp2 = (ip3 * S1) >> 16; + let d1 = temp1 + temp2; + out[r * 4] = (a1 + d1 + 4) >> 3; + out[r * 4 + 3] = (a1 - d1 + 4) >> 3; + out[r * 4 + 1] = (b1 + c1 + 4) >> 3; + out[r * 4 + 2] = (b1 - c1 + 4) >> 3; + } + out +} + +/// The 16 luma DC values (row-major by sub-block) → Y2 coefficients. +pub fn fwht(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + for i in 0..4 { + let (x0, x1, x2, x3) = (input[i], input[4 + i], input[8 + i], input[12 + i]); + let a1 = x0 + x3; + let b1 = x1 + x2; + let c1 = x1 - x2; + let d1 = x0 - x3; + tmp[i] = a1 + b1; + tmp[4 + i] = c1 + d1; + tmp[8 + i] = a1 - b1; + tmp[12 + i] = d1 - c1; + } + let mut out = [0i32; 16]; + for r in 0..4 { + let (x0, x1, x2, x3) = (tmp[r * 4], tmp[r * 4 + 1], tmp[r * 4 + 2], tmp[r * 4 + 3]); + let a1 = x0 + x3; + let b1 = x1 + x2; + let c1 = x1 - x2; + let d1 = x0 - x3; + // Half the unnormalized pair, rounded to nearest (ties away from zero). + let half = |v: i32| (v + v.signum()) / 2; + out[r * 4] = half(a1 + b1); + out[r * 4 + 1] = half(c1 + d1); + out[r * 4 + 2] = half(a1 - b1); + out[r * 4 + 3] = half(d1 - c1); + } + out +} + +/// Dequantized Y2 coefficients → the 16 luma DC values, bit-exact with the decoder (RFC 6386 §14.3). +pub fn iwht(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + for i in 0..4 { + let a1 = input[i] + input[12 + i]; + let b1 = input[4 + i] + input[8 + i]; + let c1 = input[4 + i] - input[8 + i]; + let d1 = input[i] - input[12 + i]; + tmp[i] = a1 + b1; + tmp[4 + i] = c1 + d1; + tmp[8 + i] = a1 - b1; + tmp[12 + i] = d1 - c1; + } + let mut out = [0i32; 16]; + for r in 0..4 { + let a1 = tmp[r * 4] + tmp[r * 4 + 3]; + let b1 = tmp[r * 4 + 1] + tmp[r * 4 + 2]; + let c1 = tmp[r * 4 + 1] - tmp[r * 4 + 2]; + let d1 = tmp[r * 4] - tmp[r * 4 + 3]; + out[r * 4] = (a1 + b1 + 3) >> 3; + out[r * 4 + 1] = (c1 + d1 + 3) >> 3; + out[r * 4 + 2] = (a1 - b1 + 3) >> 3; + out[r * 4 + 3] = (d1 - c1 + 3) >> 3; + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn lcg(seed: &mut u64) -> i32 { + *seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + ((*seed >> 33) % 511) as i32 - 255 + } + + #[test] + fn dct_inverts_within_rounding() { + let mut seed = 7; + for _ in 0..2000 { + let input: [i32; 16] = std::array::from_fn(|_| lcg(&mut seed)); + let back = idct(&fdct(&input)); + for (a, b) in input.iter().zip(&back) { + assert!((a - b).abs() <= 1, "{input:?} -> {back:?}"); + } + } + } + + #[test] + fn dct_of_a_constant_block_is_a_dc_only_block() { + let c = fdct(&[37; 16]); + assert_eq!(c[0], 37 * 8, "DC is 8× the mean (2× orthonormal, 4 × 4 samples)"); + assert!(c[1..].iter().all(|&v| v == 0)); + assert_eq!(idct(&c), [37; 16]); + } + + #[test] + fn wht_inverts_exactly_in_the_decoders_range() { + let mut seed = 11; + for _ in 0..2000 { + let input: [i32; 16] = std::array::from_fn(|_| lcg(&mut seed) * 8); + let back = iwht(&fwht(&input)); + for (a, b) in input.iter().zip(&back) { + assert!((a - b).abs() <= 1, "{input:?} -> {back:?}"); + } + } + let c = fwht(&[80; 16]); + assert_eq!(c[0], 80 * 8); + assert!(c[1..].iter().all(|&v| v == 0)); + assert_eq!(iwht(&c), [80; 16]); + } +} diff --git a/crates/image-codecs/src/codecs/webp.rs b/crates/image-codecs/src/codecs/webp.rs new file mode 100644 index 000000000..3f69d3c9c --- /dev/null +++ b/crates/image-codecs/src/codecs/webp.rs @@ -0,0 +1,254 @@ +//! WebP: decode lossy/lossless (first frame) via `image-webp`; encode lossless via +//! `image-webp`'s VP8L encoder, or lossy with our own VP8 encoder (`super::vp8`) wrapped in the +//! RIFF container written here (a bare `VP8 ` chunk, or `VP8X` with `ALPH` alpha and `ICCP` / +//! `EXIF` / `XMP ` chunks). ICC/EXIF/XMP both ways. + +use std::io::Cursor; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, Limits, alloc_zeroed}; + +const F: Format = Format::WebP; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +type Decoder<'a> = image_webp::WebPDecoder>; + +/// The decoder with its chunks read, the stored size and the output layout, the size checked. +fn open<'a>(bytes: &'a [u8], limits: &Limits) -> Result<(Decoder<'a>, u32, u32, ChannelLayout), CodecError> { + let mut dec = image_webp::WebPDecoder::new(Cursor::new(bytes)).map_err(err)?; + dec.set_memory_limit(limits.alloc_usize()); + let (w, h) = dec.dimensions(); + let layout = if dec.has_alpha() { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + limits.check(w, h, layout, SampleType::U8)?; + // VP8L back-references and VP8 skipped macroblocks compress flat areas extremely well, so the + // ratio is generous; it still stops a header-only file from asking for a gigabyte. + Limits::check_plausible(u64::from(w) * u64::from(h) * layout.channels() as u64, bytes.len(), 1 << 16)?; + Ok((dec, w, h, layout)) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let (mut dec, w, h, layout) = open(bytes, limits)?; + let size = dec.output_buffer_size().ok_or_else(|| err("image too large"))?; + let mut buf = alloc_zeroed::(size)?; + dec.read_image(&mut buf).map_err(err)?; + let icc = dec.icc_profile().ok().flatten(); + let exif = dec.exif_metadata().ok().flatten(); + let xmp = dec.xmp_metadata().ok().flatten().and_then(|b| String::from_utf8(b).ok()); + let mut img = Image::from_u8(w, h, layout, buf)?; + img.icc = icc; + img.meta = Metadata { exif, xmp, ..Default::default() }; + if dec.is_animated() && dec.num_frames() > 1 { + img.warnings.push(DecodeWarning::MoreFrames { total: Some(dec.num_frames()) }); + } + Ok(img) +} + +/// The stored size and EXIF block [`decode`] would find, from the container's chunks alone: the +/// same header reads and size checks, without decoding the bitstream. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option>), CodecError> { + let (mut dec, w, h, _) = open(bytes, limits)?; + Ok((w, h, dec.exif_metadata().ok().flatten())) +} + +/// Whether `bytes` is an animated WebP of more than one frame. +pub(crate) fn is_animated(bytes: &[u8]) -> bool { + image_webp::WebPDecoder::new(Cursor::new(bytes)).is_ok_and(|d| d.is_animated() && d.num_frames() > 1) +} + +/// Every frame of an animated WebP, or `None` when it is a still one (or an animation of one +/// frame), which [`decode`] reads whole. The frames come composited onto the canvas by +/// `image-webp` (offsets, blending and disposal applied), each a full picture with the file's +/// ICC profile and its duration in milliseconds. +/// +/// The first frame must decode. If a later frame is damaged the frames before it are returned +/// and the first one carries [`DecodeWarning::Truncated`]. The frame count +/// ([`super::gif_frames::MAX_GIF_FRAMES`]) and the total decoded bytes are checked against +/// `limits` before each frame is kept. +pub(crate) fn decode_frames(bytes: &[u8], limits: &Limits) -> Result>, CodecError> { + use super::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; + if !is_animated(bytes) { + return Ok(None); + } + let (mut dec, w, h, layout) = open(bytes, limits)?; + let size = dec.output_buffer_size().ok_or_else(|| err("image too large"))?; + let icc = dec.icc_profile().ok().flatten(); + let mut buf = alloc_zeroed::(size)?; + let mut out: Vec = Vec::new(); + let mut damaged = false; + for _ in 0..dec.num_frames() { + if out.len() >= MAX_GIF_FRAMES { + return Err(CodecError::LimitExceeded(format!("more than {MAX_GIF_FRAMES} frames"))); + } + // Every frame keeps its own copy of the file's ICC profile along with its pixels. + let frame_bytes = (size as u64).saturating_add(icc.as_ref().map_or(0, |p| p.len() as u64)); + if frame_bytes.saturating_mul(out.len() as u64 + 1) > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("{} frames of {w}x{h} exceed max_alloc {}", out.len() + 1, limits.max_alloc))); + } + let delay_ms = match dec.read_frame(&mut buf) { + Ok(d) => d, + Err(e) if out.is_empty() => return Err(err(e)), + Err(_) => { + damaged = true; + break; + } + }; + let mut image = Image::from_u8(w, h, layout, buf.clone())?; + image.icc.clone_from(&icc); + out.push(AnimationFrame { image, delay_ms }); + } + if damaged && let Some(first) = out.first_mut() { + first.image.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(Some(out)) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + if !opts.webp_lossless { + return encode_lossy(&img, opts); + } + let ct = match img.layout() { + ChannelLayout::Gray => image_webp::ColorType::L8, + ChannelLayout::GrayA => image_webp::ColorType::La8, + ChannelLayout::Rgb => image_webp::ColorType::Rgb8, + ChannelLayout::Rgba => image_webp::ColorType::Rgba8, + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let mut out = Vec::new(); + let mut enc = image_webp::WebPEncoder::new(&mut out); + if opts.embed_icc + && let Some(icc) = &img.icc + { + enc.set_icc_profile(icc.clone()); + } + if opts.embed_metadata { + if let Some(exif) = export_exif(&img) { + enc.set_exif_metadata(exif); + } + if let Some(xmp) = &img.meta.xmp { + enc.set_xmp_metadata(crate::resolution::export_xmp(xmp, img.meta.dpi).as_bytes().to_vec()); + } + } + enc.encode(img.data(), img.width(), img.height(), ct).map_err(|e| CodecError::encode(F, e))?; + Ok(out) +} + +/// Lossy: the picture as a VP8 key frame, alpha (when any pixel is translucent) as an +/// uncompressed `ALPH` chunk, metadata as chunks. +fn encode_lossy(img: &Image, opts: &EncodeOptions) -> Result, CodecError> { + use super::vp8; + let (w, h) = img.dimensions(); + if w > vp8::MAX_DIMENSION || h > vp8::MAX_DIMENSION { + return Err(CodecError::encode(F, format!("lossy WebP holds at most {} pixels per side", vp8::MAX_DIMENSION))); + } + // The VP8 path takes 8-bit RGB(A); gray becomes RGB with flat chroma. + let has_alpha = img.layout().has_alpha(); + let target = if has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + let rgb = img.converted(target, SampleType::U8); + let stride = target.channels(); + let yuv = vp8::Yuv::from_rgb(rgb.data(), w, h, stride); + let frame = vp8::encode_frame(&yuv, vp8::Params { quality: opts.webp_quality.min(100) }).map_err(|e| CodecError::encode(F, e))?; + let alpha: Option> = if has_alpha { + let a: Vec = rgb.data().iter().skip(3).step_by(4).copied().collect(); + if a.iter().all(|&v| v == 255) { None } else { Some(a) } + } else { + None + }; + let icc = if opts.embed_icc { img.icc.as_deref() } else { None }; + let exif = if opts.embed_metadata { export_exif(img) } else { None }; + let xmp = if opts.embed_metadata { img.meta.xmp.as_deref().map(|x| crate::resolution::export_xmp(x, img.meta.dpi)) } else { None }; + Ok(riff(w, h, &frame, alpha.as_deref(), icc, exif.as_deref(), xmp.as_deref().map(str::as_bytes))) +} + +/// The EXIF to write: the image's, upright (the pixels are written as they are shown, so a viewer +/// must never rotate them again), carrying the resolution. WebP has no resolution field of its own, +/// so the resolution goes into EXIF, added when the image has none (#2399). +pub(crate) fn export_exif(img: &Image) -> Option> { + let exif = img.meta.exif.as_deref().unwrap_or_default(); + // A WebP without EXIF already reads as 72 ppi, so the default needs no block of its own (and + // the extended container it would bring to every web export). + if exif.is_empty() && img.meta.dpi.is_none_or(is_default_ppi) { + return None; + } + let out = crate::resolution::export_exif_adding_missing_resolution(exif, img.meta.dpi).into_owned(); + (!out.is_empty()).then_some(out) +} + +/// Whether a WebP of `img` keeps its resolution: the EXIF written carries it. Not when the image's +/// own EXIF can't be parsed: that block is written as it is rather than replaced, so the +/// resolution is dropped (and reported) instead of the metadata. +pub(crate) fn keeps_dpi(img: &Image) -> bool { + let bare_default = img.meta.exif.as_deref().is_none_or(<[u8]>::is_empty) && img.meta.dpi.is_none_or(is_default_ppi); + // The XMP written beside it carries the resolution too, and readers look there first. + let in_xmp = img.meta.xmp.as_deref().is_some_and(|x| crate::resolution::xmp_resolution(&crate::resolution::export_xmp(x, img.meta.dpi)).is_some()); + bare_default || in_xmp || export_exif(img).is_some_and(|e| crate::resolution::exif_resolution(&e).is_some()) +} + +/// 72 ppi each way: what a file without a resolution opens as. +fn is_default_ppi((x, y): (f32, f32)) -> bool { + (x - 72.0).abs() < 0.01 && (y - 72.0).abs() < 0.01 +} + +/// A RIFF chunk: FourCC, little-endian size, payload, padded to an even length. +fn chunk(out: &mut Vec, fourcc: &[u8; 4], payload: &[u8]) { + out.extend_from_slice(fourcc); + out.extend_from_slice(&(payload.len() as u32).to_le_bytes()); + out.extend_from_slice(payload); + if payload.len() % 2 == 1 { + out.push(0); + } +} + +/// The WebP container around a VP8 frame: the simple form when there is nothing else, else the +/// extended form (`VP8X`) with the alpha plane and the metadata chunks in the specified order. +fn riff(w: u32, h: u32, frame: &[u8], alpha: Option<&[u8]>, icc: Option<&[u8]>, exif: Option<&[u8]>, xmp: Option<&[u8]>) -> Vec { + let mut body = b"WEBP".to_vec(); + let extended = alpha.is_some() || icc.is_some() || exif.is_some() || xmp.is_some(); + if extended { + let mut flags = 0u8; + if icc.is_some() { + flags |= 0x20; + } + if alpha.is_some() { + flags |= 0x10; + } + if exif.is_some() { + flags |= 0x08; + } + if xmp.is_some() { + flags |= 0x04; + } + let mut vp8x = vec![flags, 0, 0, 0]; + vp8x.extend_from_slice(&(w - 1).to_le_bytes()[..3]); + vp8x.extend_from_slice(&(h - 1).to_le_bytes()[..3]); + chunk(&mut body, b"VP8X", &vp8x); + if let Some(icc) = icc { + chunk(&mut body, b"ICCP", icc); + } + if let Some(a) = alpha { + // Header byte: reserved, no pre-processing, no filtering, no compression. + let mut alph = Vec::with_capacity(a.len() + 1); + alph.push(0); + alph.extend_from_slice(a); + chunk(&mut body, b"ALPH", &alph); + } + } + chunk(&mut body, b"VP8 ", frame); + if let Some(exif) = exif { + chunk(&mut body, b"EXIF", exif); + } + if let Some(xmp) = xmp { + chunk(&mut body, b"XMP ", xmp); + } + let mut out = Vec::with_capacity(body.len() + 8); + out.extend_from_slice(b"RIFF"); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend_from_slice(&body); + out +} diff --git a/crates/image-codecs/src/codecs/xpm.rs b/crates/image-codecs/src/codecs/xpm.rs new file mode 100644 index 000000000..c20f3c58e --- /dev/null +++ b/crates/image-codecs/src/codecs/xpm.rs @@ -0,0 +1,226 @@ +//! X PixMap reader for XPM2 plain-text icons and XPM3 C-source icon arrays. +//! Supports transparency (None), RGB hex at 4/8/16 bits per channel and +//! common X11 colour names. Does not execute C code. + +use std::collections::HashMap; + +mod x11_colors; + +use crate::SampleType; +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, Image}; +use crate::options::Limits; + +const MAX_SOURCE: usize = 256 << 20; +const MAX_COLOURS: usize = 65_536; +const MAX_CPP: usize = 8; + +fn bad(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::Xpm, msg) +} + +/// Extract C string literals without treating quoted text or // in a string as a comment. +/// XPM uses one literal for the header, each palette entry and each row. +fn c_strings(bytes: &[u8]) -> Result>, CodecError> { + let mut lines = Vec::new(); + let mut i = 0usize; + while i < bytes.len() { + if bytes[i..].starts_with(b"/*") { + let tail = &bytes[i + 2..]; + let Some(end) = tail.windows(2).position(|p| p == b"*/") else { return Err(bad("unterminated C comment")) }; + i += end + 4; + } else if bytes[i..].starts_with(b"//") { + i += bytes[i..].iter().position(|&c| c == b'\n').unwrap_or(bytes.len() - i); + } else if bytes[i] == b'"' { + i += 1; + let mut s = Vec::new(); + let mut closed = false; + while i < bytes.len() { + let c = bytes[i]; + i += 1; + match c { + b'"' => { + closed = true; + break; + } + b'\\' => { + let next = *bytes.get(i).ok_or_else(|| bad("truncated C escape"))?; + i += 1; + s.push(match next { + b'n' => b'\n', + b'r' => b'\r', + b't' => b'\t', + b'0' => 0, + _ => next, // quoted backslash or quote + }); + } + _ => s.push(c), + } + } + if !closed { + return Err(bad("unterminated XPM string")); + } + lines.push(s); + } else { + i += 1; + } + } + Ok(lines) +} + +/// Extract XPM2's plain-text lines without trimming pixel rows: leading and +/// trailing spaces can be palette keys, and physical CRLF is not image data. +fn xpm2_lines(bytes: &[u8]) -> Result>, CodecError> { + let mut input = bytes.split(|&b| b == b'\n'); + let header = input.next().ok_or_else(|| bad("missing XPM2 marker"))?; + if header.strip_suffix(b"\r").unwrap_or(header) != b"! XPM2" { + return Err(bad("not an XPM2 image")); + } + let mut lines = Vec::new(); + for raw in input { + let line = raw.strip_suffix(b"\r").unwrap_or(raw); + // The header may be preceded by comments and blank lines. After the + // header, a leading '!' is allowed as a palette key or pixel value. + if lines.is_empty() && (line.is_empty() || line.starts_with(b"!")) { + continue; + } + lines.push(line.to_vec()); + } + Ok(lines) +} + +fn channel(hex: &str) -> Option { + let v = u32::from_str_radix(hex, 16).ok()?; + let max = (1u32 << (hex.len() * 4)) - 1; + Some(((v * 255 + max / 2) / max) as u8) +} + +fn named(name: &str) -> Option<[u8; 3]> { + let s = name.to_ascii_lowercase().replace([' ', '-', '_'], ""); + // XPM imports X11 names, whose RGB values differ from CSS for names + // such as gray, maroon and purple. Keep these exact rather than guessing. + if let Ok(index) = x11_colors::X11_COLORS.binary_search_by_key(&s.as_str(), |(name, _)| *name) { + return Some(x11_colors::X11_COLORS[index].1); + } + // Retain useful web color aliases not present in the X11 database. + Some(match s.as_str() { + "silver" => [192, 192, 192], + "olive" => [128, 128, 0], + "teal" => [0, 128, 128], + "fuchsia" => [255, 0, 255], + "navyblue" => [0, 0, 128], + _ => return None, + }) +} + +fn color(name: &str) -> Option<[u8; 4]> { + if name.eq_ignore_ascii_case("none") { + return Some([0, 0, 0, 0]); + } + let rgb = if let Some(hex) = name.strip_prefix('#') { + // UTF-8 byte lengths can match a supported hex width while slicing would panic. + // `from_str_radix` would also take a leading `+`: accept hex digits only. + if !hex.bytes().all(|b| b.is_ascii_hexdigit()) { + return None; + } + let digits = match hex.len() { + 3 => 1, + 6 => 2, + 12 => 4, + _ => return None, + }; + let rgb = (0..3).map(|i| channel(&hex[i * digits..(i + 1) * digits])).collect::>>()?; + [rgb[0], rgb[1], rgb[2]] + } else { + named(name)? + }; + Some([rgb[0], rgb[1], rgb[2], 255]) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + if bytes.len() > MAX_SOURCE { + return Err(CodecError::LimitExceeded("XPM source exceeds 256 MiB".into())); + } + let lines = if bytes.starts_with(b"/* XPM */") { + c_strings(bytes)? + } else if bytes.starts_with(b"! XPM2\n") || bytes.starts_with(b"! XPM2\r\n") { + xpm2_lines(bytes)? + } else { + return Err(bad("not an XPM2/XPM3 image")); + }; + decode_lines(&lines, limits) +} + +fn decode_lines(lines: &[Vec], limits: &Limits) -> Result { + let header = lines.first().ok_or_else(|| bad("missing image header"))?; + let parts = std::str::from_utf8(header).map_err(|_| bad("non-UTF-8 XPM header"))?; + let mut parts = parts.split_ascii_whitespace(); + let number = |key: &str, s: Option<&str>| -> Result { + s.ok_or_else(|| bad(format!("missing {key}")))?.parse::().map_err(|_| bad(format!("invalid {key}"))) + }; + let width = number("width", parts.next())?; + let height = number("height", parts.next())?; + let colours = number("palette size", parts.next())?; + let cpp = number("chars per pixel", parts.next())?; + if width == 0 || height == 0 || colours == 0 || colours > MAX_COLOURS || !(1..=MAX_CPP).contains(&cpp) { + return Err(bad("invalid XPM dimensions, palette size or chars per pixel")); + } + let (w, h) = (u32::try_from(width).map_err(|_| bad("width overflow"))?, u32::try_from(height).map_err(|_| bad("height overflow"))?); + limits.check(w, h, ChannelLayout::Rgba, SampleType::U8)?; + let required = 1usize.checked_add(colours).and_then(|n| n.checked_add(height)).ok_or_else(|| bad("row count overflow"))?; + if lines.len() < required { + return Err(bad("truncated palette or image rows")); + } + let mut palette = HashMap::, [u8; 4]>::with_capacity(colours); + for line in &lines[1..1 + colours] { + if line.len() < cpp { + return Err(bad("truncated colour key")); + } + let fields = std::str::from_utf8(&line[cpp..]).map_err(|_| bad("invalid colour name"))?; + // XPM priority is colour, gray, four-level gray, then monochrome, + // regardless of the order of keys in the palette entry. + let terms: Vec<&str> = fields.split_ascii_whitespace().collect(); + let mut picked: Option<(u8, String)> = None; + let mut i = 0; + while i < terms.len() { + let priority = match terms[i] { + "c" => 4, + "g" => 3, + "g4" => 2, + "m" => 1, + _ => 0, // Symbolic names ('s') are not colour values. + }; + let start = i + 1; + i = start; + while i < terms.len() && !matches!(terms[i], "c" | "g" | "g4" | "m" | "s") { + i += 1; + } + if priority > 0 && i > start && picked.as_ref().is_none_or(|(current, _)| priority > *current) { + picked = Some((priority, terms[start..i].join(" "))); + } + } + let value = picked.as_ref().and_then(|(_, name)| color(name)).ok_or_else(|| bad("unsupported or missing XPM colour"))?; + if palette.insert(line[..cpp].to_vec(), value).is_some() { + return Err(bad("duplicate XPM palette key")); + } + } + let row_len = width.checked_mul(cpp).ok_or_else(|| bad("row byte count overflow"))?; + // Validate all source rows before reserving the decoded RGBA image. A malformed + // short row must not be able to make a large header-sized allocation first. + if lines[1 + colours..required].iter().any(|line| line.len() != row_len) { + return Err(bad("XPM row has the wrong width")); + } + let total = width.checked_mul(height).and_then(|n| n.checked_mul(4)).ok_or_else(|| bad("pixel size overflow"))?; + let mut pixels = Vec::new(); + pixels.try_reserve_exact(total).map_err(|_| bad("XPM pixel buffer is too large"))?; + pixels.resize(total, 0); + for (y, line) in lines[1 + colours..required].iter().enumerate() { + for x in 0..width { + let key = &line[x * cpp..(x + 1) * cpp]; + let rgba = palette.get(key).ok_or_else(|| bad("unknown palette key"))?; + pixels[(y * width + x) * 4..(y * width + x + 1) * 4].copy_from_slice(rgba); + } + } + Image::from_u8(w, h, ChannelLayout::Rgba, pixels) +} diff --git a/crates/image-codecs/src/codecs/xpm/x11_colors.rs b/crates/image-codecs/src/codecs/xpm/x11_colors.rs new file mode 100644 index 000000000..1d47b3706 --- /dev/null +++ b/crates/image-codecs/src/codecs/xpm/x11_colors.rs @@ -0,0 +1,666 @@ +//! X11 color-name database (X.Org rgb.txt) normalized to lowercase without +//! spaces, underscores, or hyphens. RGB values are *X11* values, not CSS values. +//! Source: https://cgit.freedesktop.org/xorg/app/rgb/tree/rgb.txt +//! License: MIT (X.Org Foundation), see the upstream app/rgb COPYING file. +//! Aliases differing only in case or spaces have been collapsed. +//! Keep sorted: decoder uses binary search without allocation per candidate. + +pub(super) const X11_COLORS: &[(&str, [u8; 3])] = &[ + ("aliceblue", [240, 248, 255]), + ("antiquewhite", [250, 235, 215]), + ("antiquewhite1", [255, 239, 219]), + ("antiquewhite2", [238, 223, 204]), + ("antiquewhite3", [205, 192, 176]), + ("antiquewhite4", [139, 131, 120]), + ("aquamarine", [127, 255, 212]), + ("aquamarine1", [127, 255, 212]), + ("aquamarine2", [118, 238, 198]), + ("aquamarine3", [102, 205, 170]), + ("aquamarine4", [69, 139, 116]), + ("azure", [240, 255, 255]), + ("azure1", [240, 255, 255]), + ("azure2", [224, 238, 238]), + ("azure3", [193, 205, 205]), + ("azure4", [131, 139, 139]), + ("beige", [245, 245, 220]), + ("bisque", [255, 228, 196]), + ("bisque1", [255, 228, 196]), + ("bisque2", [238, 213, 183]), + ("bisque3", [205, 183, 158]), + ("bisque4", [139, 125, 107]), + ("black", [0, 0, 0]), + ("blanchedalmond", [255, 235, 205]), + ("blue", [0, 0, 255]), + ("blue1", [0, 0, 255]), + ("blue2", [0, 0, 238]), + ("blue3", [0, 0, 205]), + ("blue4", [0, 0, 139]), + ("blueviolet", [138, 43, 226]), + ("brown", [165, 42, 42]), + ("brown1", [255, 64, 64]), + ("brown2", [238, 59, 59]), + ("brown3", [205, 51, 51]), + ("brown4", [139, 35, 35]), + ("burlywood", [222, 184, 135]), + ("burlywood1", [255, 211, 155]), + ("burlywood2", [238, 197, 145]), + ("burlywood3", [205, 170, 125]), + ("burlywood4", [139, 115, 85]), + ("cadetblue", [95, 158, 160]), + ("cadetblue1", [152, 245, 255]), + ("cadetblue2", [142, 229, 238]), + ("cadetblue3", [122, 197, 205]), + ("cadetblue4", [83, 134, 139]), + ("chartreuse", [127, 255, 0]), + ("chartreuse1", [127, 255, 0]), + ("chartreuse2", [118, 238, 0]), + ("chartreuse3", [102, 205, 0]), + ("chartreuse4", [69, 139, 0]), + ("chocolate", [210, 105, 30]), + ("chocolate1", [255, 127, 36]), + ("chocolate2", [238, 118, 33]), + ("chocolate3", [205, 102, 29]), + ("chocolate4", [139, 69, 19]), + ("coral", [255, 127, 80]), + ("coral1", [255, 114, 86]), + ("coral2", [238, 106, 80]), + ("coral3", [205, 91, 69]), + ("coral4", [139, 62, 47]), + ("cornflowerblue", [100, 149, 237]), + ("cornsilk", [255, 248, 220]), + ("cornsilk1", [255, 248, 220]), + ("cornsilk2", [238, 232, 205]), + ("cornsilk3", [205, 200, 177]), + ("cornsilk4", [139, 136, 120]), + ("cyan", [0, 255, 255]), + ("cyan1", [0, 255, 255]), + ("cyan2", [0, 238, 238]), + ("cyan3", [0, 205, 205]), + ("cyan4", [0, 139, 139]), + ("darkblue", [0, 0, 139]), + ("darkcyan", [0, 139, 139]), + ("darkgoldenrod", [184, 134, 11]), + ("darkgoldenrod1", [255, 185, 15]), + ("darkgoldenrod2", [238, 173, 14]), + ("darkgoldenrod3", [205, 149, 12]), + ("darkgoldenrod4", [139, 101, 8]), + ("darkgray", [169, 169, 169]), + ("darkgreen", [0, 100, 0]), + ("darkgrey", [169, 169, 169]), + ("darkkhaki", [189, 183, 107]), + ("darkmagenta", [139, 0, 139]), + ("darkolivegreen", [85, 107, 47]), + ("darkolivegreen1", [202, 255, 112]), + ("darkolivegreen2", [188, 238, 104]), + ("darkolivegreen3", [162, 205, 90]), + ("darkolivegreen4", [110, 139, 61]), + ("darkorange", [255, 140, 0]), + ("darkorange1", [255, 127, 0]), + ("darkorange2", [238, 118, 0]), + ("darkorange3", [205, 102, 0]), + ("darkorange4", [139, 69, 0]), + ("darkorchid", [153, 50, 204]), + ("darkorchid1", [191, 62, 255]), + ("darkorchid2", [178, 58, 238]), + ("darkorchid3", [154, 50, 205]), + ("darkorchid4", [104, 34, 139]), + ("darkred", [139, 0, 0]), + ("darksalmon", [233, 150, 122]), + ("darkseagreen", [143, 188, 143]), + ("darkseagreen1", [193, 255, 193]), + ("darkseagreen2", [180, 238, 180]), + ("darkseagreen3", [155, 205, 155]), + ("darkseagreen4", [105, 139, 105]), + ("darkslateblue", [72, 61, 139]), + ("darkslategray", [47, 79, 79]), + ("darkslategray1", [151, 255, 255]), + ("darkslategray2", [141, 238, 238]), + ("darkslategray3", [121, 205, 205]), + ("darkslategray4", [82, 139, 139]), + ("darkslategrey", [47, 79, 79]), + ("darkturquoise", [0, 206, 209]), + ("darkviolet", [148, 0, 211]), + ("deeppink", [255, 20, 147]), + ("deeppink1", [255, 20, 147]), + ("deeppink2", [238, 18, 137]), + ("deeppink3", [205, 16, 118]), + ("deeppink4", [139, 10, 80]), + ("deepskyblue", [0, 191, 255]), + ("deepskyblue1", [0, 191, 255]), + ("deepskyblue2", [0, 178, 238]), + ("deepskyblue3", [0, 154, 205]), + ("deepskyblue4", [0, 104, 139]), + ("dimgray", [105, 105, 105]), + ("dimgrey", [105, 105, 105]), + ("dodgerblue", [30, 144, 255]), + ("dodgerblue1", [30, 144, 255]), + ("dodgerblue2", [28, 134, 238]), + ("dodgerblue3", [24, 116, 205]), + ("dodgerblue4", [16, 78, 139]), + ("firebrick", [178, 34, 34]), + ("firebrick1", [255, 48, 48]), + ("firebrick2", [238, 44, 44]), + ("firebrick3", [205, 38, 38]), + ("firebrick4", [139, 26, 26]), + ("floralwhite", [255, 250, 240]), + ("forestgreen", [34, 139, 34]), + ("gainsboro", [220, 220, 220]), + ("ghostwhite", [248, 248, 255]), + ("gold", [255, 215, 0]), + ("gold1", [255, 215, 0]), + ("gold2", [238, 201, 0]), + ("gold3", [205, 173, 0]), + ("gold4", [139, 117, 0]), + ("goldenrod", [218, 165, 32]), + ("goldenrod1", [255, 193, 37]), + ("goldenrod2", [238, 180, 34]), + ("goldenrod3", [205, 155, 29]), + ("goldenrod4", [139, 105, 20]), + ("gray", [190, 190, 190]), + ("gray0", [0, 0, 0]), + ("gray1", [3, 3, 3]), + ("gray10", [26, 26, 26]), + ("gray100", [255, 255, 255]), + ("gray11", [28, 28, 28]), + ("gray12", [31, 31, 31]), + ("gray13", [33, 33, 33]), + ("gray14", [36, 36, 36]), + ("gray15", [38, 38, 38]), + ("gray16", [41, 41, 41]), + ("gray17", [43, 43, 43]), + ("gray18", [46, 46, 46]), + ("gray19", [48, 48, 48]), + ("gray2", [5, 5, 5]), + ("gray20", [51, 51, 51]), + ("gray21", [54, 54, 54]), + ("gray22", [56, 56, 56]), + ("gray23", [59, 59, 59]), + ("gray24", [61, 61, 61]), + ("gray25", [64, 64, 64]), + ("gray26", [66, 66, 66]), + ("gray27", [69, 69, 69]), + ("gray28", [71, 71, 71]), + ("gray29", [74, 74, 74]), + ("gray3", [8, 8, 8]), + ("gray30", [77, 77, 77]), + ("gray31", [79, 79, 79]), + ("gray32", [82, 82, 82]), + ("gray33", [84, 84, 84]), + ("gray34", [87, 87, 87]), + ("gray35", [89, 89, 89]), + ("gray36", [92, 92, 92]), + ("gray37", [94, 94, 94]), + ("gray38", [97, 97, 97]), + ("gray39", [99, 99, 99]), + ("gray4", [10, 10, 10]), + ("gray40", [102, 102, 102]), + ("gray41", [105, 105, 105]), + ("gray42", [107, 107, 107]), + ("gray43", [110, 110, 110]), + ("gray44", [112, 112, 112]), + ("gray45", [115, 115, 115]), + ("gray46", [117, 117, 117]), + ("gray47", [120, 120, 120]), + ("gray48", [122, 122, 122]), + ("gray49", [125, 125, 125]), + ("gray5", [13, 13, 13]), + ("gray50", [127, 127, 127]), + ("gray51", [130, 130, 130]), + ("gray52", [133, 133, 133]), + ("gray53", [135, 135, 135]), + ("gray54", [138, 138, 138]), + ("gray55", [140, 140, 140]), + ("gray56", [143, 143, 143]), + ("gray57", [145, 145, 145]), + ("gray58", [148, 148, 148]), + ("gray59", [150, 150, 150]), + ("gray6", [15, 15, 15]), + ("gray60", [153, 153, 153]), + ("gray61", [156, 156, 156]), + ("gray62", [158, 158, 158]), + ("gray63", [161, 161, 161]), + ("gray64", [163, 163, 163]), + ("gray65", [166, 166, 166]), + ("gray66", [168, 168, 168]), + ("gray67", [171, 171, 171]), + ("gray68", [173, 173, 173]), + ("gray69", [176, 176, 176]), + ("gray7", [18, 18, 18]), + ("gray70", [179, 179, 179]), + ("gray71", [181, 181, 181]), + ("gray72", [184, 184, 184]), + ("gray73", [186, 186, 186]), + ("gray74", [189, 189, 189]), + ("gray75", [191, 191, 191]), + ("gray76", [194, 194, 194]), + ("gray77", [196, 196, 196]), + ("gray78", [199, 199, 199]), + ("gray79", [201, 201, 201]), + ("gray8", [20, 20, 20]), + ("gray80", [204, 204, 204]), + ("gray81", [207, 207, 207]), + ("gray82", [209, 209, 209]), + ("gray83", [212, 212, 212]), + ("gray84", [214, 214, 214]), + ("gray85", [217, 217, 217]), + ("gray86", [219, 219, 219]), + ("gray87", [222, 222, 222]), + ("gray88", [224, 224, 224]), + ("gray89", [227, 227, 227]), + ("gray9", [23, 23, 23]), + ("gray90", [229, 229, 229]), + ("gray91", [232, 232, 232]), + ("gray92", [235, 235, 235]), + ("gray93", [237, 237, 237]), + ("gray94", [240, 240, 240]), + ("gray95", [242, 242, 242]), + ("gray96", [245, 245, 245]), + ("gray97", [247, 247, 247]), + ("gray98", [250, 250, 250]), + ("gray99", [252, 252, 252]), + ("green", [0, 255, 0]), + ("green1", [0, 255, 0]), + ("green2", [0, 238, 0]), + ("green3", [0, 205, 0]), + ("green4", [0, 139, 0]), + ("greenyellow", [173, 255, 47]), + ("grey", [190, 190, 190]), + ("grey0", [0, 0, 0]), + ("grey1", [3, 3, 3]), + ("grey10", [26, 26, 26]), + ("grey100", [255, 255, 255]), + ("grey11", [28, 28, 28]), + ("grey12", [31, 31, 31]), + ("grey13", [33, 33, 33]), + ("grey14", [36, 36, 36]), + ("grey15", [38, 38, 38]), + ("grey16", [41, 41, 41]), + ("grey17", [43, 43, 43]), + ("grey18", [46, 46, 46]), + ("grey19", [48, 48, 48]), + ("grey2", [5, 5, 5]), + ("grey20", [51, 51, 51]), + ("grey21", [54, 54, 54]), + ("grey22", [56, 56, 56]), + ("grey23", [59, 59, 59]), + ("grey24", [61, 61, 61]), + ("grey25", [64, 64, 64]), + ("grey26", [66, 66, 66]), + ("grey27", [69, 69, 69]), + ("grey28", [71, 71, 71]), + ("grey29", [74, 74, 74]), + ("grey3", [8, 8, 8]), + ("grey30", [77, 77, 77]), + ("grey31", [79, 79, 79]), + ("grey32", [82, 82, 82]), + ("grey33", [84, 84, 84]), + ("grey34", [87, 87, 87]), + ("grey35", [89, 89, 89]), + ("grey36", [92, 92, 92]), + ("grey37", [94, 94, 94]), + ("grey38", [97, 97, 97]), + ("grey39", [99, 99, 99]), + ("grey4", [10, 10, 10]), + ("grey40", [102, 102, 102]), + ("grey41", [105, 105, 105]), + ("grey42", [107, 107, 107]), + ("grey43", [110, 110, 110]), + ("grey44", [112, 112, 112]), + ("grey45", [115, 115, 115]), + ("grey46", [117, 117, 117]), + ("grey47", [120, 120, 120]), + ("grey48", [122, 122, 122]), + ("grey49", [125, 125, 125]), + ("grey5", [13, 13, 13]), + ("grey50", [127, 127, 127]), + ("grey51", [130, 130, 130]), + ("grey52", [133, 133, 133]), + ("grey53", [135, 135, 135]), + ("grey54", [138, 138, 138]), + ("grey55", [140, 140, 140]), + ("grey56", [143, 143, 143]), + ("grey57", [145, 145, 145]), + ("grey58", [148, 148, 148]), + ("grey59", [150, 150, 150]), + ("grey6", [15, 15, 15]), + ("grey60", [153, 153, 153]), + ("grey61", [156, 156, 156]), + ("grey62", [158, 158, 158]), + ("grey63", [161, 161, 161]), + ("grey64", [163, 163, 163]), + ("grey65", [166, 166, 166]), + ("grey66", [168, 168, 168]), + ("grey67", [171, 171, 171]), + ("grey68", [173, 173, 173]), + ("grey69", [176, 176, 176]), + ("grey7", [18, 18, 18]), + ("grey70", [179, 179, 179]), + ("grey71", [181, 181, 181]), + ("grey72", [184, 184, 184]), + ("grey73", [186, 186, 186]), + ("grey74", [189, 189, 189]), + ("grey75", [191, 191, 191]), + ("grey76", [194, 194, 194]), + ("grey77", [196, 196, 196]), + ("grey78", [199, 199, 199]), + ("grey79", [201, 201, 201]), + ("grey8", [20, 20, 20]), + ("grey80", [204, 204, 204]), + ("grey81", [207, 207, 207]), + ("grey82", [209, 209, 209]), + ("grey83", [212, 212, 212]), + ("grey84", [214, 214, 214]), + ("grey85", [217, 217, 217]), + ("grey86", [219, 219, 219]), + ("grey87", [222, 222, 222]), + ("grey88", [224, 224, 224]), + ("grey89", [227, 227, 227]), + ("grey9", [23, 23, 23]), + ("grey90", [229, 229, 229]), + ("grey91", [232, 232, 232]), + ("grey92", [235, 235, 235]), + ("grey93", [237, 237, 237]), + ("grey94", [240, 240, 240]), + ("grey95", [242, 242, 242]), + ("grey96", [245, 245, 245]), + ("grey97", [247, 247, 247]), + ("grey98", [250, 250, 250]), + ("grey99", [252, 252, 252]), + ("honeydew", [240, 255, 240]), + ("honeydew1", [240, 255, 240]), + ("honeydew2", [224, 238, 224]), + ("honeydew3", [193, 205, 193]), + ("honeydew4", [131, 139, 131]), + ("hotpink", [255, 105, 180]), + ("hotpink1", [255, 110, 180]), + ("hotpink2", [238, 106, 167]), + ("hotpink3", [205, 96, 144]), + ("hotpink4", [139, 58, 98]), + ("indianred", [205, 92, 92]), + ("indianred1", [255, 106, 106]), + ("indianred2", [238, 99, 99]), + ("indianred3", [205, 85, 85]), + ("indianred4", [139, 58, 58]), + ("ivory", [255, 255, 240]), + ("ivory1", [255, 255, 240]), + ("ivory2", [238, 238, 224]), + ("ivory3", [205, 205, 193]), + ("ivory4", [139, 139, 131]), + ("khaki", [240, 230, 140]), + ("khaki1", [255, 246, 143]), + ("khaki2", [238, 230, 133]), + ("khaki3", [205, 198, 115]), + ("khaki4", [139, 134, 78]), + ("lavender", [230, 230, 250]), + ("lavenderblush", [255, 240, 245]), + ("lavenderblush1", [255, 240, 245]), + ("lavenderblush2", [238, 224, 229]), + ("lavenderblush3", [205, 193, 197]), + ("lavenderblush4", [139, 131, 134]), + ("lawngreen", [124, 252, 0]), + ("lemonchiffon", [255, 250, 205]), + ("lemonchiffon1", [255, 250, 205]), + ("lemonchiffon2", [238, 233, 191]), + ("lemonchiffon3", [205, 201, 165]), + ("lemonchiffon4", [139, 137, 112]), + ("lightblue", [173, 216, 230]), + ("lightblue1", [191, 239, 255]), + ("lightblue2", [178, 223, 238]), + ("lightblue3", [154, 192, 205]), + ("lightblue4", [104, 131, 139]), + ("lightcoral", [240, 128, 128]), + ("lightcyan", [224, 255, 255]), + ("lightcyan1", [224, 255, 255]), + ("lightcyan2", [209, 238, 238]), + ("lightcyan3", [180, 205, 205]), + ("lightcyan4", [122, 139, 139]), + ("lightgoldenrod", [238, 221, 130]), + ("lightgoldenrod1", [255, 236, 139]), + ("lightgoldenrod2", [238, 220, 130]), + ("lightgoldenrod3", [205, 190, 112]), + ("lightgoldenrod4", [139, 129, 76]), + ("lightgoldenrodyellow", [250, 250, 210]), + ("lightgray", [211, 211, 211]), + ("lightgreen", [144, 238, 144]), + ("lightgrey", [211, 211, 211]), + ("lightpink", [255, 182, 193]), + ("lightpink1", [255, 174, 185]), + ("lightpink2", [238, 162, 173]), + ("lightpink3", [205, 140, 149]), + ("lightpink4", [139, 95, 101]), + ("lightsalmon", [255, 160, 122]), + ("lightsalmon1", [255, 160, 122]), + ("lightsalmon2", [238, 149, 114]), + ("lightsalmon3", [205, 129, 98]), + ("lightsalmon4", [139, 87, 66]), + ("lightseagreen", [32, 178, 170]), + ("lightskyblue", [135, 206, 250]), + ("lightskyblue1", [176, 226, 255]), + ("lightskyblue2", [164, 211, 238]), + ("lightskyblue3", [141, 182, 205]), + ("lightskyblue4", [96, 123, 139]), + ("lightslateblue", [132, 112, 255]), + ("lightslategray", [119, 136, 153]), + ("lightslategrey", [119, 136, 153]), + ("lightsteelblue", [176, 196, 222]), + ("lightsteelblue1", [202, 225, 255]), + ("lightsteelblue2", [188, 210, 238]), + ("lightsteelblue3", [162, 181, 205]), + ("lightsteelblue4", [110, 123, 139]), + ("lightyellow", [255, 255, 224]), + ("lightyellow1", [255, 255, 224]), + ("lightyellow2", [238, 238, 209]), + ("lightyellow3", [205, 205, 180]), + ("lightyellow4", [139, 139, 122]), + ("limegreen", [50, 205, 50]), + ("linen", [250, 240, 230]), + ("magenta", [255, 0, 255]), + ("magenta1", [255, 0, 255]), + ("magenta2", [238, 0, 238]), + ("magenta3", [205, 0, 205]), + ("magenta4", [139, 0, 139]), + ("maroon", [176, 48, 96]), + ("maroon1", [255, 52, 179]), + ("maroon2", [238, 48, 167]), + ("maroon3", [205, 41, 144]), + ("maroon4", [139, 28, 98]), + ("mediumaquamarine", [102, 205, 170]), + ("mediumblue", [0, 0, 205]), + ("mediumorchid", [186, 85, 211]), + ("mediumorchid1", [224, 102, 255]), + ("mediumorchid2", [209, 95, 238]), + ("mediumorchid3", [180, 82, 205]), + ("mediumorchid4", [122, 55, 139]), + ("mediumpurple", [147, 112, 219]), + ("mediumpurple1", [171, 130, 255]), + ("mediumpurple2", [159, 121, 238]), + ("mediumpurple3", [137, 104, 205]), + ("mediumpurple4", [93, 71, 139]), + ("mediumseagreen", [60, 179, 113]), + ("mediumslateblue", [123, 104, 238]), + ("mediumspringgreen", [0, 250, 154]), + ("mediumturquoise", [72, 209, 204]), + ("mediumvioletred", [199, 21, 133]), + ("midnightblue", [25, 25, 112]), + ("mintcream", [245, 255, 250]), + ("mistyrose", [255, 228, 225]), + ("mistyrose1", [255, 228, 225]), + ("mistyrose2", [238, 213, 210]), + ("mistyrose3", [205, 183, 181]), + ("mistyrose4", [139, 125, 123]), + ("moccasin", [255, 228, 181]), + ("navajowhite", [255, 222, 173]), + ("navajowhite1", [255, 222, 173]), + ("navajowhite2", [238, 207, 161]), + ("navajowhite3", [205, 179, 139]), + ("navajowhite4", [139, 121, 94]), + ("navy", [0, 0, 128]), + ("navyblue", [0, 0, 128]), + ("oldlace", [253, 245, 230]), + ("olivedrab", [107, 142, 35]), + ("olivedrab1", [192, 255, 62]), + ("olivedrab2", [179, 238, 58]), + ("olivedrab3", [154, 205, 50]), + ("olivedrab4", [105, 139, 34]), + ("orange", [255, 165, 0]), + ("orange1", [255, 165, 0]), + ("orange2", [238, 154, 0]), + ("orange3", [205, 133, 0]), + ("orange4", [139, 90, 0]), + ("orangered", [255, 69, 0]), + ("orangered1", [255, 69, 0]), + ("orangered2", [238, 64, 0]), + ("orangered3", [205, 55, 0]), + ("orangered4", [139, 37, 0]), + ("orchid", [218, 112, 214]), + ("orchid1", [255, 131, 250]), + ("orchid2", [238, 122, 233]), + ("orchid3", [205, 105, 201]), + ("orchid4", [139, 71, 137]), + ("palegoldenrod", [238, 232, 170]), + ("palegreen", [152, 251, 152]), + ("palegreen1", [154, 255, 154]), + ("palegreen2", [144, 238, 144]), + ("palegreen3", [124, 205, 124]), + ("palegreen4", [84, 139, 84]), + ("paleturquoise", [175, 238, 238]), + ("paleturquoise1", [187, 255, 255]), + ("paleturquoise2", [174, 238, 238]), + ("paleturquoise3", [150, 205, 205]), + ("paleturquoise4", [102, 139, 139]), + ("palevioletred", [219, 112, 147]), + ("palevioletred1", [255, 130, 171]), + ("palevioletred2", [238, 121, 159]), + ("palevioletred3", [205, 104, 137]), + ("palevioletred4", [139, 71, 93]), + ("papayawhip", [255, 239, 213]), + ("peachpuff", [255, 218, 185]), + ("peachpuff1", [255, 218, 185]), + ("peachpuff2", [238, 203, 173]), + ("peachpuff3", [205, 175, 149]), + ("peachpuff4", [139, 119, 101]), + ("peru", [205, 133, 63]), + ("pink", [255, 192, 203]), + ("pink1", [255, 181, 197]), + ("pink2", [238, 169, 184]), + ("pink3", [205, 145, 158]), + ("pink4", [139, 99, 108]), + ("plum", [221, 160, 221]), + ("plum1", [255, 187, 255]), + ("plum2", [238, 174, 238]), + ("plum3", [205, 150, 205]), + ("plum4", [139, 102, 139]), + ("powderblue", [176, 224, 230]), + ("purple", [160, 32, 240]), + ("purple1", [155, 48, 255]), + ("purple2", [145, 44, 238]), + ("purple3", [125, 38, 205]), + ("purple4", [85, 26, 139]), + ("red", [255, 0, 0]), + ("red1", [255, 0, 0]), + ("red2", [238, 0, 0]), + ("red3", [205, 0, 0]), + ("red4", [139, 0, 0]), + ("rosybrown", [188, 143, 143]), + ("rosybrown1", [255, 193, 193]), + ("rosybrown2", [238, 180, 180]), + ("rosybrown3", [205, 155, 155]), + ("rosybrown4", [139, 105, 105]), + ("royalblue", [65, 105, 225]), + ("royalblue1", [72, 118, 255]), + ("royalblue2", [67, 110, 238]), + ("royalblue3", [58, 95, 205]), + ("royalblue4", [39, 64, 139]), + ("saddlebrown", [139, 69, 19]), + ("salmon", [250, 128, 114]), + ("salmon1", [255, 140, 105]), + ("salmon2", [238, 130, 98]), + ("salmon3", [205, 112, 84]), + ("salmon4", [139, 76, 57]), + ("sandybrown", [244, 164, 96]), + ("seagreen", [46, 139, 87]), + ("seagreen1", [84, 255, 159]), + ("seagreen2", [78, 238, 148]), + ("seagreen3", [67, 205, 128]), + ("seagreen4", [46, 139, 87]), + ("seashell", [255, 245, 238]), + ("seashell1", [255, 245, 238]), + ("seashell2", [238, 229, 222]), + ("seashell3", [205, 197, 191]), + ("seashell4", [139, 134, 130]), + ("sienna", [160, 82, 45]), + ("sienna1", [255, 130, 71]), + ("sienna2", [238, 121, 66]), + ("sienna3", [205, 104, 57]), + ("sienna4", [139, 71, 38]), + ("skyblue", [135, 206, 235]), + ("skyblue1", [135, 206, 255]), + ("skyblue2", [126, 192, 238]), + ("skyblue3", [108, 166, 205]), + ("skyblue4", [74, 112, 139]), + ("slateblue", [106, 90, 205]), + ("slateblue1", [131, 111, 255]), + ("slateblue2", [122, 103, 238]), + ("slateblue3", [105, 89, 205]), + ("slateblue4", [71, 60, 139]), + ("slategray", [112, 128, 144]), + ("slategray1", [198, 226, 255]), + ("slategray2", [185, 211, 238]), + ("slategray3", [159, 182, 205]), + ("slategray4", [108, 123, 139]), + ("slategrey", [112, 128, 144]), + ("snow", [255, 250, 250]), + ("snow1", [255, 250, 250]), + ("snow2", [238, 233, 233]), + ("snow3", [205, 201, 201]), + ("snow4", [139, 137, 137]), + ("springgreen", [0, 255, 127]), + ("springgreen1", [0, 255, 127]), + ("springgreen2", [0, 238, 118]), + ("springgreen3", [0, 205, 102]), + ("springgreen4", [0, 139, 69]), + ("steelblue", [70, 130, 180]), + ("steelblue1", [99, 184, 255]), + ("steelblue2", [92, 172, 238]), + ("steelblue3", [79, 148, 205]), + ("steelblue4", [54, 100, 139]), + ("tan", [210, 180, 140]), + ("tan1", [255, 165, 79]), + ("tan2", [238, 154, 73]), + ("tan3", [205, 133, 63]), + ("tan4", [139, 90, 43]), + ("thistle", [216, 191, 216]), + ("thistle1", [255, 225, 255]), + ("thistle2", [238, 210, 238]), + ("thistle3", [205, 181, 205]), + ("thistle4", [139, 123, 139]), + ("tomato", [255, 99, 71]), + ("tomato1", [255, 99, 71]), + ("tomato2", [238, 92, 66]), + ("tomato3", [205, 79, 57]), + ("tomato4", [139, 54, 38]), + ("turquoise", [64, 224, 208]), + ("turquoise1", [0, 245, 255]), + ("turquoise2", [0, 229, 238]), + ("turquoise3", [0, 197, 205]), + ("turquoise4", [0, 134, 139]), + ("violet", [238, 130, 238]), + ("violetred", [208, 32, 144]), + ("violetred1", [255, 62, 150]), + ("violetred2", [238, 58, 140]), + ("violetred3", [205, 50, 120]), + ("violetred4", [139, 34, 82]), + ("wheat", [245, 222, 179]), + ("wheat1", [255, 231, 186]), + ("wheat2", [238, 216, 174]), + ("wheat3", [205, 186, 150]), + ("wheat4", [139, 126, 102]), + ("white", [255, 255, 255]), + ("whitesmoke", [245, 245, 245]), + ("yellow", [255, 255, 0]), + ("yellow1", [255, 255, 0]), + ("yellow2", [238, 238, 0]), + ("yellow3", [205, 205, 0]), + ("yellow4", [139, 139, 0]), + ("yellowgreen", [154, 205, 50]), +]; diff --git a/crates/image-codecs/src/error.rs b/crates/image-codecs/src/error.rs new file mode 100644 index 000000000..fec9f35c1 --- /dev/null +++ b/crates/image-codecs/src/error.rs @@ -0,0 +1,31 @@ +use crate::format::Format; + +/// Errors produced by this crate. Decoders never panic on malformed input; +/// they return [`CodecError::Malformed`] instead. +#[derive(Debug, thiserror::Error)] +pub enum CodecError { + #[error("unrecognized image format")] + UnknownFormat, + #[error("{format:?}: {reason}")] + Unsupported { format: Format, reason: String }, + #[error("{format:?}: malformed data: {message}")] + Malformed { format: Format, message: String }, + #[error("limit exceeded: {0}")] + LimitExceeded(String), + #[error("{format:?}: encode failed: {message}")] + Encode { format: Format, message: String }, + #[error("invalid image: {0}")] + InvalidImage(String), +} + +impl CodecError { + pub(crate) fn malformed(format: Format, e: impl std::fmt::Display) -> Self { + CodecError::Malformed { format, message: e.to_string() } + } + pub(crate) fn encode(format: Format, e: impl std::fmt::Display) -> Self { + CodecError::Encode { format, message: e.to_string() } + } + pub(crate) fn unsupported(format: Format, reason: impl Into) -> Self { + CodecError::Unsupported { format, reason: reason.into() } + } +} diff --git a/crates/image-codecs/src/exif_color.rs b/crates/image-codecs/src/exif_color.rs new file mode 100644 index 000000000..4259bb15c --- /dev/null +++ b/crates/image-codecs/src/exif_color.rs @@ -0,0 +1,138 @@ +//! The EXIF ColorSpace tag (0xA001, in the Exif sub-IFD that IFD0's tag 0x8769 points to): +//! 1 = sRGB, 0xFFFF = uncalibrated (EXIF 2.3, §4.6.5). +//! +//! Cameras write it instead of an ICC profile. Photoshop opens a file with no embedded profile but +//! ColorSpace = sRGB as sRGB, unless Preferences › File Handling › Ignore EXIF Profile Tag is on. +//! Anything malformed, truncated or missing reads as no tag. + +use crate::orientation::{Order, ifd0_entries, tiff_body}; + +/// IFD0's pointer to the Exif sub-IFD. +const TAG_EXIF_IFD: u16 = 0x8769; +/// The ColorSpace tag in the Exif sub-IFD. +const TAG_COLOR_SPACE: u16 = 0xA001; +const TYPE_SHORT: u16 = 3; +const TYPE_LONG: u16 = 4; +/// TIFF type 13: an IFD offset (some writers use it for sub-IFD pointers). +const TYPE_IFD: u16 = 13; +/// Bytes per classic TIFF IFD entry; EXIF is never BigTIFF. +const ENTRY: usize = 12; +/// No IFD can start inside the 8-byte TIFF header. +const HEADER: usize = 8; + +/// The ColorSpace value meaning sRGB. +pub const EXIF_COLOR_SPACE_SRGB: u16 = 1; + +/// The EXIF ColorSpace value of a TIFF-structured block (an EXIF payload with or without the +/// `Exif\0\0` prefix), or `None` when it has none or it is malformed. +pub fn exif_color_space(exif: &[u8]) -> Option { + let b = tiff_body(exif); + let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; + if big_tiff { + return None; + } + let ptr = find(order, b, first, count, entry_bytes, TAG_EXIF_IFD)?; + if !matches!(order.u16(b, ptr.checked_add(2)?)?, TYPE_LONG | TYPE_IFD) || order.u32(b, ptr.checked_add(4)?)? != 1 { + return None; + } + let ifd = usize::try_from(order.u32(b, ptr.checked_add(8)?)?).ok()?; + if ifd < HEADER { + return None; + } + let n = usize::from(order.u16(b, ifd)?); + let first = ifd.checked_add(2)?; + // The whole table must be present. + if first.checked_add(n.checked_mul(ENTRY)?)? > b.len() { + return None; + } + let e = find(order, b, first, n, ENTRY, TAG_COLOR_SPACE)?; + if order.u32(b, e.checked_add(4)?)? != 1 { + return None; + } + match order.u16(b, e.checked_add(2)?)? { + TYPE_SHORT => order.u16(b, e.checked_add(8)?), + TYPE_LONG => u16::try_from(order.u32(b, e.checked_add(8)?)?).ok(), + _ => None, + } +} + +/// Offset of the first entry tagged `tag` in the IFD whose `count` entries start at `first`. +fn find(order: Order, b: &[u8], first: usize, count: usize, entry_bytes: usize, tag: u16) -> Option { + (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(tag)) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A little- or big-endian EXIF block: IFD0 holding only the Exif pointer, then an Exif IFD + /// holding `entries` (tag, type, count, value). + fn exif(big_endian: bool, entries: &[(u16, u16, u32, u32)]) -> Vec { + let u16b = |v: u16| if big_endian { v.to_be_bytes() } else { v.to_le_bytes() }; + let u32b = |v: u32| if big_endian { v.to_be_bytes() } else { v.to_le_bytes() }; + let mut b = if big_endian { b"MM\0\x2a".to_vec() } else { b"II\x2a\0".to_vec() }; + b.extend(u32b(8)); + // IFD0: one entry, then the next-IFD pointer. The Exif IFD starts right after (8 + 2 + 12 + 4). + b.extend(u16b(1)); + b.extend(u16b(TAG_EXIF_IFD)); + b.extend(u16b(TYPE_LONG)); + b.extend(u32b(1)); + b.extend(u32b(26)); + b.extend(u32b(0)); + b.extend(u16b(entries.len() as u16)); + for &(tag, ty, count, value) in entries { + b.extend(u16b(tag)); + b.extend(u16b(ty)); + b.extend(u32b(count)); + // SHORT values are left-justified in the 4-byte field. + if ty == TYPE_SHORT { + b.extend(u16b(value as u16)); + b.extend([0, 0]); + } else { + b.extend(u32b(value)); + } + } + b.extend(u32b(0)); + b + } + + #[test] + fn reads_the_color_space_in_either_byte_order() { + for big in [false, true] { + let srgb = exif(big, &[(0x9000, 7, 4, 0x3032_3330), (TAG_COLOR_SPACE, TYPE_SHORT, 1, 1)]); + assert_eq!(exif_color_space(&srgb), Some(EXIF_COLOR_SPACE_SRGB), "big endian: {big}"); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend(&srgb); + assert_eq!(exif_color_space(&prefixed), Some(1)); + assert_eq!(exif_color_space(&exif(big, &[(TAG_COLOR_SPACE, TYPE_SHORT, 1, 0xFFFF)])), Some(0xFFFF)); + assert_eq!(exif_color_space(&exif(big, &[(TAG_COLOR_SPACE, TYPE_LONG, 1, 1)])), Some(1), "LONG is accepted"); + } + } + + #[test] + fn missing_or_malformed_reads_as_no_tag() { + assert_eq!(exif_color_space(&exif(false, &[(0x9000, 7, 4, 0)])), None, "no ColorSpace entry"); + assert_eq!(exif_color_space(&exif(false, &[(TAG_COLOR_SPACE, TYPE_SHORT, 2, 1)])), None, "count ≠ 1"); + assert_eq!(exif_color_space(&exif(false, &[(TAG_COLOR_SPACE, 2, 1, 1)])), None, "ASCII type"); + assert_eq!(exif_color_space(&exif(false, &[(TAG_COLOR_SPACE, TYPE_LONG, 1, 0x1_0000)])), None, "LONG over u16"); + let good = exif(false, &[(TAG_COLOR_SPACE, TYPE_SHORT, 1, 1)]); + // Every truncation is rejected without panicking. + for len in 0..good.len() - 4 { + assert_eq!(exif_color_space(&good[..len]), None, "truncated to {len}"); + } + // An Exif pointer past the end, into the header, or of the wrong type. + for (at, value) in [(18usize, 0xFFFF_FFF0u32), (18, 4), (18, u32::MAX)] { + let mut b = good.clone(); + b[at..at + 4].copy_from_slice(&value.to_le_bytes()); + assert_eq!(exif_color_space(&b), None, "pointer {value:#x}"); + } + let mut b = good.clone(); + b[12..14].copy_from_slice(&TYPE_SHORT.to_le_bytes()); + assert_eq!(exif_color_space(&b), None, "pointer type SHORT"); + // An Exif IFD claiming more entries than the block holds. + let mut b = good; + b[26..28].copy_from_slice(&u16::MAX.to_le_bytes()); + assert_eq!(exif_color_space(&b), None); + assert_eq!(exif_color_space(b"not exif"), None); + } +} diff --git a/crates/image-codecs/src/exr_stream.rs b/crates/image-codecs/src/exr_stream.rs new file mode 100644 index 000000000..1ed8ba127 --- /dev/null +++ b/crates/image-codecs/src/exr_stream.rs @@ -0,0 +1,294 @@ +//! Bounded, seekable single-part scanline OpenEXR decoding. Total pixel count does not +//! size a decoded image buffer: only one compression block is returned at a time. +use std::io::{Cursor, Read, Seek, SeekFrom}; + +use crate::{CodecError, Format}; +use exr::{ + block::{ + UncompressedBlock, + chunk::{Chunk, CompressedBlock, CompressedScanLineBlock}, + lines::LineIndex, + }, + meta::{BlockDescription, MetaData, attribute::SampleType}, +}; + +const META_BYTES: usize = 8 << 20; +const BLOCK_BYTES: usize = 64 << 20; +const MAX_CHUNKS: usize = 1 << 20; + +fn invalid(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::OpenExr, e) +} +fn limit(s: &str) -> CodecError { + CodecError::LimitExceeded(s.into()) +} +fn unsupported(s: &str) -> CodecError { + CodecError::unsupported(Format::OpenExr, s) +} + +fn read(r: &mut impl Read) -> Result<[u8; N], CodecError> { + let mut bytes = [0; N]; + r.read_exact(&mut bytes).map_err(invalid)?; + Ok(bytes) +} + +fn string(r: &mut impl Read, prefix: &mut Vec) -> Result { + for i in 0..256 { + let b = read::<1>(r)?; + prefix.extend(b); + if b == [0] { + return Ok(i != 0); + } + } + Err(limit("EXR attribute name exceeds 255 bytes")) +} + +fn metadata(r: &mut impl Read, cancel: &dyn Fn() -> bool) -> Result, CodecError> { + let start = read::<8>(r)?; + if start.get(..4) != Some(&[0x76, 0x2f, 0x31, 0x01]) { + return Err(invalid("EXR signature")); + } + let version = u32::from_le_bytes(start.get(4..).ok_or_else(|| invalid("version"))?.try_into().map_err(invalid)?); + if version & (0x200 | 0x800 | 0x1000) != 0 { + return Err(unsupported("streaming requires a single flat scanline part")); + } + let mut prefix = start.to_vec(); + let mut attributes = 0; + loop { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + if !string(r, &mut prefix)? { + break; + } + attributes += 1; + if attributes > 4096 { + return Err(limit("EXR metadata exceeds 4096 attributes")); + } + string(r, &mut prefix)?; + let length = read::<4>(r)?; + prefix.extend(length); + let n = usize::try_from(i32::from_le_bytes(length)).map_err(invalid)?; + if n > META_BYTES || prefix.len().checked_add(n).is_none_or(|v| v > META_BYTES) { + return Err(limit("EXR metadata exceeds 8 MiB")); + } + let begin = prefix.len(); + prefix.try_reserve_exact(n).map_err(|_| limit("EXR metadata allocation"))?; + prefix.resize(begin + n, 0); + for bytes in prefix.get_mut(begin..).ok_or_else(|| invalid("attribute size"))?.chunks_mut(64 << 10) { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + r.read_exact(bytes).map_err(invalid)?; + } + } + Ok(prefix) +} + +/// Header facts used by the document owner to admit tile and working memory. +#[derive(Clone, Copy, Debug)] +pub struct Info { + pub width: u32, + pub height: u32, + pub gray: bool, + pub has_alpha: bool, + pub all_half: bool, + pub working_bytes: u64, + pub blocks: usize, +} + +/// Interleaved little-endian F32, GrayA or RGBA, at source resolution. +pub struct Band { + pub x: u32, + pub y: u32, + pub width: u32, + pub height: u32, + pub data: Vec, +} + +pub struct Decoder { + source: R, + meta: MetaData, + info: Info, + offsets: Vec, + seen: Vec, + channels: Vec>, + next: usize, + file_len: u64, + chunks_start: u64, + raw_limit: usize, + rows: usize, +} + +impl Decoder { + /// Read only bounded metadata and offsets; allocate no full-image or channel buffer. + pub fn new(mut source: R, cancel: &dyn Fn() -> bool) -> Result { + let file_len = source.seek(SeekFrom::End(0)).map_err(invalid)?; + source.seek(SeekFrom::Start(0)).map_err(invalid)?; + let prefix = metadata(&mut source, cancel)?; + let meta = std::panic::catch_unwind(|| MetaData::read_from_buffered(Cursor::new(&prefix), false)) + .map_err(|_| invalid("invalid EXR metadata"))? + .map_err(invalid)?; + let header = meta.headers.first().ok_or_else(|| invalid("missing header"))?; + if meta.headers.len() != 1 || header.deep || !matches!(header.blocks, BlockDescription::ScanLines) { + return Err(unsupported("streaming requires a single flat scanline part")); + } + let (w, h) = (header.layer_size.0, header.layer_size.1); + if w == 0 || h == 0 || w > 1 << 18 || h > 1 << 18 { + return Err(limit("EXR dimensions exceed the streaming limit of 262144")); + } + let list = &header.channels.list; + if list.is_empty() || list.len() > 64 || list.iter().any(|c| c.sampling.0 != 1 || c.sampling.1 != 1) { + return Err(unsupported("EXR channel count or subsampling")); + } + // The same channels the whole-file decoder shows, so both routes open the same image. + let names: Vec = list.iter().map(|c| c.name.to_string()).collect(); + let (layout, picked) = crate::image::exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| unsupported("no R/G/B or Y channels"))?; + let gray = matches!(layout, crate::ChannelLayout::Gray | crate::ChannelLayout::GrayA); + let mut selected: Vec> = picked.into_iter().map(Some).collect(); + if !matches!(layout, crate::ChannelLayout::GrayA | crate::ChannelLayout::Rgba) { + selected.push(None); + } + if selected.len() != if gray { 2 } else { 4 } { + return Err(unsupported("EXR channel selection")); + } + // The document path currently tags Rec. 709 linear samples. Keep custom + // primaries on the existing decoder until their profiles can be mapped. + if !gray && header.shared_attributes.chromaticities.is_some() { + return Err(unsupported("streaming custom EXR chromaticities requires a color profile")); + } + let all_half = selected.iter().flatten().all(|i| list.get(*i).is_some_and(|c| c.sample_type == SampleType::F16)); + let has_alpha = selected.last().copied().flatten().is_some(); + let channels = (0..list.len()).map(|i| selected.iter().position(|s| *s == Some(i))).collect(); + let rows = header.max_block_pixel_size().1; + let expected = h.div_ceil(rows.max(1)); + if rows == 0 || header.chunk_count != expected || expected > MAX_CHUNKS { + return Err(limit("EXR block count exceeds the streaming budget")); + } + let raw_limit = w + .checked_mul(rows) + .and_then(|n| n.checked_mul(header.channels.bytes_per_pixel)) + .filter(|n| *n <= BLOCK_BYTES) + .ok_or_else(|| limit("EXR compression block exceeds 64 MiB"))?; + let output = (w as u64).saturating_mul(rows as u64).saturating_mul(selected.len() as u64 * 4); + let info = Info { + width: w as u32, + height: h as u32, + gray, + has_alpha, + all_half, + blocks: expected, + working_bytes: (raw_limit as u64).saturating_mul(8).saturating_add(output.saturating_mul(2)).saturating_add(1 << 20), + }; + let chunks_start = source + .stream_position() + .map_err(invalid)? + .checked_add(expected as u64 * 8) + .filter(|v| *v <= file_len) + .ok_or_else(|| invalid("truncated offset table"))?; + let mut offsets = Vec::new(); + offsets.try_reserve_exact(expected).map_err(|_| limit("EXR offset allocation"))?; + for _ in 0..expected { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + let offset = u64::from_le_bytes(read(&mut source)?); + if offset < chunks_start || offset.checked_add(8).is_none_or(|v| v > file_len) { + return Err(invalid("EXR chunk offset")); + } + offsets.push(offset); + } + Ok(Self { source, meta, info, offsets, seen: vec![false; expected], channels, next: 0, file_len, chunks_start, raw_limit, rows }) + } + + pub fn info(&self) -> Info { + self.info + } + + /// Decode one block. Check cancellation while reading and between channel rows. + pub fn next_band(&mut self, cancel: &dyn Fn() -> bool) -> Result, CodecError> { + let Some(offset) = self.offsets.get(self.next).copied() else { + return Ok(None); + }; + if cancel() { + return Err(invalid("EXR import cancelled")); + } + self.source.seek(SeekFrom::Start(offset)).map_err(invalid)?; + let y = i32::from_le_bytes(read(&mut self.source)?); + let n = usize::try_from(i32::from_le_bytes(read(&mut self.source)?)).map_err(invalid)?; + if n > self.raw_limit.saturating_mul(2).saturating_add(64 << 10) + || offset < self.chunks_start + || offset.checked_add(8).and_then(|v| v.checked_add(n as u64)).is_none_or(|v| v > self.file_len) + { + return Err(invalid("EXR compressed block length")); + } + let header = self.meta.headers.first().ok_or_else(|| invalid("missing header"))?; + let row = i64::from(y) - i64::from(header.own_attributes.layer_position.1); + if row < 0 || row as u64 >= u64::from(self.info.height) || !(row as usize).is_multiple_of(self.rows) { + return Err(invalid("EXR block coordinate")); + } + let seen = self.seen.get_mut(row as usize / self.rows).ok_or_else(|| invalid("EXR block coordinate"))?; + if *seen { + return Err(invalid("duplicate EXR block")); + } + *seen = true; + let mut packed = Vec::new(); + packed.try_reserve_exact(n).map_err(|_| limit("EXR block allocation"))?; + packed.resize(n, 0); + for bytes in packed.chunks_mut(64 << 10) { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + self.source.read_exact(bytes).map_err(invalid)?; + } + let chunk = + Chunk { layer_index: 0, compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }) }; + let block = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| UncompressedBlock::decompress_chunk(chunk, &self.meta, false))) + .map_err(|_| invalid("invalid EXR compression block"))? + .map_err(invalid)?; + let (w, h) = (block.index.pixel_size.0, block.index.pixel_size.1); + if w != self.info.width as usize + || block.index.pixel_position.0 != 0 + || block.index.pixel_position.1 != row as usize + || h != self.rows.min(self.info.height as usize - row as usize) + { + return Err(invalid("EXR block shape")); + } + let expected = w.checked_mul(h).and_then(|n| n.checked_mul(header.channels.bytes_per_pixel)).ok_or_else(|| invalid("EXR block size"))?; + if block.data.len() != expected { + return Err(invalid("EXR block sample count")); + } + let nc = if self.info.gray { 2 } else { 4 }; + let count = w.checked_mul(h).and_then(|n| n.checked_mul(nc * 4)).ok_or_else(|| limit("EXR output block size"))?; + let mut data = Vec::new(); + data.try_reserve_exact(count).map_err(|_| limit("EXR output allocation"))?; + data.resize(count, 0); + if !self.info.has_alpha { + for pixel in data.chunks_exact_mut(nc * 4) { + pixel.get_mut((nc - 1) * 4..).ok_or_else(|| invalid("alpha sample"))?.copy_from_slice(&1_f32.to_le_bytes()); + } + } + for (range, line) in LineIndex::lines_in_block(block.index, &header.channels) { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + let Some(channel) = self.channels.get(line.channel).copied().flatten() else { + continue; + }; + let sample = header.channels.list.get(line.channel).ok_or_else(|| invalid("EXR channel"))?.sample_type; + let input = block.data.get(range).ok_or_else(|| invalid("EXR row size"))?; + let y = line.position.1.checked_sub(block.index.pixel_position.1).ok_or_else(|| invalid("EXR row coordinate"))?; + let output = data.get_mut(y * w * nc * 4..(y + 1) * w * nc * 4).ok_or_else(|| invalid("EXR row coordinate"))?; + for (bytes, pixel) in input.chunks_exact(sample.bytes_per_sample()).zip(output.chunks_exact_mut(nc * 4)) { + let value = match sample { + SampleType::F16 => half::f16::from_bits(u16::from_ne_bytes(bytes.try_into().map_err(invalid)?)).to_f32(), + SampleType::F32 => f32::from_ne_bytes(bytes.try_into().map_err(invalid)?), + SampleType::U32 => u32::from_ne_bytes(bytes.try_into().map_err(invalid)?) as f32, + }; + pixel.get_mut(channel * 4..channel * 4 + 4).ok_or_else(|| invalid("EXR channel"))?.copy_from_slice(&value.to_le_bytes()); + } + } + self.next += 1; + Ok(Some(Band { x: 0, y: row as u32, width: w as u32, height: h as u32, data })) + } +} diff --git a/crates/image-codecs/src/fidelity.rs b/crates/image-codecs/src/fidelity.rs new file mode 100644 index 000000000..984f2ac45 --- /dev/null +++ b/crates/image-codecs/src/fidelity.rs @@ -0,0 +1,282 @@ +//! Encode planning and fidelity warnings. `encode` and `fidelity_warnings` +//! share [`plan`], so a warning is emitted exactly when the encoder will +//! actually transform or drop something. + +use std::fmt; + +use crate::codecs::dpx; +use crate::format::{Format, caps}; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::{DdsCompression, EncodeOptions}; + +/// Something that will be lost or altered when writing an image to a format. +#[derive(Debug, Clone, PartialEq)] +pub enum FidelityWarning { + /// The format cannot be written in this build. + WriteUnsupported { + format: Format, + }, + /// Bit depth / precision reduced, e.g. 16-bit → 8-bit or f32 → f16. + DepthReduced { + from: SampleType, + to: SampleType, + }, + /// Samples will be stored with fewer bits than their sample type holds: DPX at 10 or 12 + /// bits, Cineon at 10. + BitsReduced { + from: SampleType, + bits: u8, + }, + /// Float samples outside `[0, 1]` will be clipped by integer storage. + RangeClipped, + /// Non-opaque alpha will be discarded. + AlphaDiscarded, + /// Alpha will be reduced to fully transparent / fully opaque (GIF, DDS BC1, IFF). + AlphaBinarized, + /// CMYK data will be converted (naively, not colour-managed) to another model. + CmykConverted { + to: ChannelLayout, + }, + /// L*a*b* data will be converted to another model (sRGB, D50 to D65). + LabConverted { + to: ChannelLayout, + }, + /// Colour data will be reduced to grayscale. + ColorToGray, + /// The image will be quantized to a 256-colour palette. + PaletteQuantized, + /// The format's encoder is lossy. + LossyCompression, + IccDropped, + ExifDropped, + XmpDropped, + DpiDropped, + TextDropped, + /// Metadata larger than the format can hold in one piece (`what`: "EXIF", "XMP") will be + /// dropped. + MetadataTooLarge { + what: &'static str, + bytes: usize, + }, + /// Image exceeds the format's maximum size; encoding will fail. + DimensionsExceeded { + max_width: u32, + max_height: u32, + }, +} + +impl FidelityWarning { + /// `true` for warnings that make encoding fail outright. + pub fn is_fatal(&self) -> bool { + matches!(self, FidelityWarning::WriteUnsupported { .. } | FidelityWarning::DimensionsExceeded { .. }) + } +} + +fn depth_name(s: SampleType) -> &'static str { + match s { + SampleType::U8 => "8-bit", + SampleType::U16 => "16-bit", + SampleType::F16 => "16-bit float", + SampleType::F32 => "32-bit float", + } +} + +impl fmt::Display for FidelityWarning { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use FidelityWarning::*; + match self { + WriteUnsupported { format } => { + write!(f, "{} cannot be written in this build", format.name()) + } + DepthReduced { from, to } => write!(f, "{} will be reduced to {}", depth_name(*from), depth_name(*to)), + BitsReduced { from, bits } => write!(f, "{} will be reduced to {bits}-bit", depth_name(*from)), + RangeClipped => write!(f, "HDR values outside 0..1 will be clipped"), + AlphaDiscarded => write!(f, "alpha will be discarded"), + AlphaBinarized => write!(f, "alpha will be reduced to on/off transparency"), + CmykConverted { to } => { + write!(f, "CMYK will be converted to {to:?} (not colour-managed)") + } + LabConverted { to } => write!(f, "Lab will be converted to {to:?} (sRGB)"), + ColorToGray => write!(f, "colour will be converted to grayscale"), + PaletteQuantized => write!(f, "colours will be quantized to a 256-colour palette"), + LossyCompression => write!(f, "lossy compression"), + IccDropped => write!(f, "ICC profile not supported; it will be dropped"), + ExifDropped => write!(f, "EXIF not supported; it will be dropped"), + XmpDropped => write!(f, "XMP not supported; it will be dropped"), + DpiDropped => write!(f, "resolution (DPI) not supported; it will be dropped"), + TextDropped => write!(f, "text metadata not supported; it will be dropped"), + MetadataTooLarge { what, bytes } => write!(f, "{what} ({bytes} bytes) is too large for the format; it will be dropped"), + DimensionsExceeded { max_width, max_height } => { + write!(f, "image exceeds the format maximum of {max_width}x{max_height}") + } + } + } +} + +/// Target storage chosen for an image/format pair. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct Plan { + pub layout: ChannelLayout, + pub sample: SampleType, +} + +fn pick_sample(src: SampleType, depths: &[SampleType]) -> SampleType { + if depths.contains(&src) { + return src; + } + let pref: &[SampleType] = match src { + // Integers widen into float losslessly; F32 represents U8/U16 exactly. + SampleType::U8 => &[SampleType::U16, SampleType::F32, SampleType::F16], + SampleType::U16 => &[SampleType::F32, SampleType::U8, SampleType::F16], + SampleType::F16 => &[SampleType::F32, SampleType::U16, SampleType::U8], + SampleType::F32 => &[SampleType::F16, SampleType::U16, SampleType::U8], + }; + pref.iter().copied().find(|s| depths.contains(s)).unwrap_or(depths[0]) +} + +fn pick_layout(src: ChannelLayout, layouts: &[ChannelLayout], alpha: bool) -> ChannelLayout { + let mut cand = src; + if !alpha { + cand = cand.without_alpha(); + } + if layouts.contains(&cand) { + return cand; + } + // CMYK and Lab fall back to RGB; gray expands to RGB. + if cand.is_cmyk() || cand.is_lab() || cand.is_gray() { + cand = if cand.has_alpha() { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + } + if layouts.contains(&cand) { + return cand; + } + if layouts.contains(&cand.with_alpha()) { + return cand.with_alpha(); + } + if layouts.contains(&cand.without_alpha()) { + return cand.without_alpha(); + } + layouts[0] +} + +pub(crate) fn plan(image: &Image, format: Format, opts: &EncodeOptions) -> Plan { + let c = caps(format); + let (layout, sample) = (image.layout(), image.sample_type()); + match format { + // PNM: integer data can use any layout (PAM); float goes to PFM + // which only has gray and RGB. + Format::Pnm if sample.is_float() => Plan { layout: pick_layout(layout, &[ChannelLayout::Gray, ChannelLayout::Rgb], false), sample: SampleType::F32 }, + // DPX has no luma + alpha descriptor: gray with alpha is written as luma, alpha dropped. + // The sample type follows the chosen bits (U8 for 8, U16 for 10, 12 and 16). + Format::Dpx => Plan { + layout: if layout == ChannelLayout::GrayA { ChannelLayout::Gray } else { pick_layout(layout, c.layouts, c.alpha) }, + sample: dpx::plan_sample(opts.dpx_bits, sample), + }, + // CIELab has no float form: float Lab is written with 16 bits. + Format::Tiff if layout.is_lab() && sample.is_float() => Plan { layout: pick_layout(layout, c.layouts, c.alpha), sample: SampleType::U16 }, + // A block-compressed DDS stores RGBA whatever the source; uncompressed keeps gray and RGB. + Format::Dds if opts.dds.compression.is_block() => Plan { layout: ChannelLayout::Rgba, sample: SampleType::U8 }, + _ => Plan { layout: pick_layout(layout, c.layouts, c.alpha), sample: pick_sample(sample, c.depths) }, + } +} + +/// Warnings for writing `image` as `format` with default options. +pub fn fidelity_warnings(image: &Image, format: Format) -> Vec { + fidelity_warnings_with(image, format, &EncodeOptions::default()) +} + +/// Warnings for writing `image` as `format` with the given options. +pub fn fidelity_warnings_with(image: &Image, format: Format, opts: &EncodeOptions) -> Vec { + use FidelityWarning as W; + let c = caps(format); + let mut w = Vec::new(); + if !c.write { + w.push(W::WriteUnsupported { format }); + return w; + } + if let Some((mw, mh)) = format.max_dimensions() + && (image.width() > mw || image.height() > mh) + { + w.push(W::DimensionsExceeded { max_width: mw, max_height: mh }); + } + let p = plan(image, format, opts); + let (sl, ss) = (image.layout(), image.sample_type()); + + // Depth + let lossy_depth = match (ss, p.sample) { + (a, b) if a == b => false, + (SampleType::U8, _) => false, + (SampleType::U16, SampleType::F32) => false, + (SampleType::F16, SampleType::F32) => false, + _ => true, + }; + // DPX at 10 or 12 bits and Cineon keep fewer bits than their U16 plan: float samples always + // lose precision there, 16-bit ones unless they are already code values at that depth. + if let Some(bits) = dpx::packed_bits(format, opts).filter(|_| ss != SampleType::U8) { + if ss.is_float() || !dpx::keeps_u16_samples(&image.converted(p.layout, SampleType::U16), bits) { + w.push(W::BitsReduced { from: ss, bits }); + } + } else if lossy_depth { + w.push(W::DepthReduced { from: ss, to: p.sample }); + } + // FITS stores floats as 0..1 (see `codecs::fits`), so out-of-range values are clipped too. + if ss.is_float() && (!p.sample.is_float() || format == Format::Fits) && image.has_out_of_range() { + w.push(W::RangeClipped); + } + + // Layout + if sl.has_alpha() && !p.layout.has_alpha() && image.has_translucency() { + w.push(W::AlphaDiscarded); + } + // An IFF/ILBM mask plane holds one bit per pixel. + let binary_alpha = matches!(format, Format::Gif | Format::Iff) || (format == Format::Dds && opts.dds.compression == DdsCompression::Bc1); + if binary_alpha && p.layout.has_alpha() && image.has_translucency() { + w.push(W::AlphaBinarized); + } + if sl.is_cmyk() && !p.layout.is_cmyk() { + w.push(W::CmykConverted { to: p.layout }); + } + if sl.is_lab() && !p.layout.is_lab() { + w.push(W::LabConverted { to: p.layout }); + } + if !sl.is_gray() && p.layout.is_gray() { + w.push(W::ColorToGray); + } + + // Compression + if format == Format::Gif { + w.push(W::PaletteQuantized); + } else if c.lossy || (format == Format::WebP && !opts.webp_lossless) || (format == Format::Dds && opts.dds.compression.is_block()) { + w.push(W::LossyCompression); + } + + // Metadata + if image.icc.is_some() && opts.embed_icc && (!c.icc || !sl.same_model(p.layout)) { + w.push(W::IccDropped); + } + if opts.embed_metadata { + let m = &image.meta; + if m.exif.is_some() && !c.exif { + w.push(W::ExifDropped); + } + if m.xmp.is_some() && !c.xmp { + w.push(W::XmpDropped); + } + if format == Format::Jpeg { + if let Some(exif) = m.exif.as_deref().filter(|e| crate::codecs::jpeg::exif_segment(e, None).is_none()) { + w.push(W::MetadataTooLarge { what: "EXIF", bytes: exif.len() }); + } + if let Some(xmp) = m.xmp.as_deref().filter(|x| crate::codecs::jpeg::xmp_segment(x, None).is_none()) { + w.push(W::MetadataTooLarge { what: "XMP", bytes: xmp.len() }); + } + } + if m.dpi.is_some() && (!c.dpi || (format == Format::WebP && !crate::codecs::webp::keeps_dpi(image))) { + w.push(W::DpiDropped); + } + // DPX keeps its own header fields, not free-form text. + let text_kept = |k: &str| c.text && (format != Format::Dpx || crate::codecs::dpx::KEYS.contains(&k)); + if m.text.iter().any(|(k, _)| !text_kept(k)) { + w.push(W::TextDropped); + } + } + w +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs new file mode 100644 index 000000000..20a520e31 --- /dev/null +++ b/crates/image-codecs/src/format.rs @@ -0,0 +1,425 @@ +//! Format identification and capability declarations. + +use crate::image::{ChannelLayout, SampleType}; + +/// Supported (or known) raster file formats. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Format { + Png, + Jpeg, + Tiff, + WebP, + Gif, + Bmp, + Tga, + Ico, + /// Netpbm family: PBM/PGM/PPM (P1–P6), PAM (P7) and PFM (`PF`/`Pf`). + Pnm, + /// ZSoft Paintbrush: 1/2/4-bit planar palettes, 8-bit indexed and 24-bit RGB. + Pcx, + /// X PixMap icons (XPM2 plain text and XPM3 C-string arrays). + Xpm, + /// DirectDraw Surface: a 2D texture, uncompressed or BC1–BC3. Reading takes the top mip + /// level; writing can add the full mip chain ([`crate::DdsOptions`]). + Dds, + Qoi, + OpenExr, + /// Radiance RGBE. + Hdr, + /// Encode-only behind the `avif` feature; see [`ASYMMETRIC_EXCEPTIONS`]. + Avif, + /// HEIF/HEIC (HEVC-coded): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. + Heif, + /// FITS (Flexible Image Transport System), the astronomy format: gray or RGB image data. + Fits, + /// DPX (SMPTE 268M Digital Picture Exchange): luma, RGB or RGBA film and video frames. + Dpx, + /// JPEG XL: lossless export-only until the separate importer lands. + Jxl, + /// Kodak Cineon (`.cin`, the 10-bit film-scan format DPX grew from). + Cineon, + /// DICOM single-frame images (uncompressed): read-only. + Dicom, + /// Amiga IFF/ILBM (`FORM ... ILBM`): planar bitplanes with a palette or 24-bit RGB. + Iff, +} + +/// What a format can hold **and** what this crate reads/writes for it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct FormatCaps { + /// Decoding is available in this build. + pub read: bool, + /// Encoding is available in this build. + pub write: bool, + /// Sample types stored without conversion. + pub depths: &'static [SampleType], + /// Layouts stored *and read back* without conversion. + pub layouts: &'static [ChannelLayout], + pub alpha: bool, + pub icc: bool, + pub exif: bool, + pub xmp: bool, + /// Physical resolution (DPI) is preserved. + pub dpi: bool, + /// Free-form key/value text is preserved. + pub text: bool, + /// The container can hold animation/multiple frames. This crate only + /// decodes the first frame and encodes a single frame. + pub animation: bool, + /// Our encoder is lossy (quantization or lossy compression). + pub lossy: bool, +} + +/// Formats that are enabled but intentionally not symmetric, with the reason. +/// The test-suite asserts that every other enabled format is read+write. +pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[ + (Format::Xpm, "XPM is a legacy Unix icon interchange format. Reading existing XPM icons is supported; writing modern image assets should use PNG instead."), + ( + Format::Dicom, + "DICOM single-frame images are imported for medical review; this crate has no DICOM writer, so save an imported frame as TIFF or PNG instead.", + ), + ( + Format::Pcx, + "PCX is supported for opening legacy paint and clipart assets. New files should use PNG; writing obsolete indexed and planar variants is intentionally not provided.", + ), + ( + Format::Avif, + "AVIF encode uses ravif (pure Rust) but decoding requires dav1d (C); read stays unsupported \ + until a pure-Rust AV1 decoder is viable. Only enabled with the non-default `avif` feature.", + ), + (Format::Jxl, "Lossless JPEG XL export uses the pure-Rust zune-jpegxl encoder; JPEG XL reading is tracked separately and is not yet available."), + ( + Format::Heif, + "HEIC decode uses heic-rs (pure Rust, in the optional craft-heif crate behind the \ + non-default `heif` feature, which official builds enable), so iPhone and Mac photos open; \ + writing needs an HEVC encoder, and the mature ones (x265, libheif) are C, so write stays \ + unsupported. Without the feature HEIF is detected but neither read nor written.", + ), +]; + +use ChannelLayout as L; +use SampleType as S; + +const RGB_GRAY: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba]; +const ALL_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA]; +/// TIFF also stores CIE L*a*b* (CIELab). +const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA, L::Lab, L::LabA]; + +impl Format { + /// Every format known to the crate (enabled or not). + pub const ALL: [Format; 23] = [ + Format::Png, + Format::Jpeg, + Format::Jxl, + Format::Tiff, + Format::WebP, + Format::Gif, + Format::Bmp, + Format::Tga, + Format::Ico, + Format::Pnm, + Format::Pcx, + Format::Xpm, + Format::Dds, + Format::Qoi, + Format::OpenExr, + Format::Hdr, + Format::Avif, + Format::Heif, + Format::Fits, + Format::Dpx, + Format::Cineon, + Format::Dicom, + Format::Iff, + ]; + + pub fn caps(self) -> FormatCaps { + caps(self) + } + + /// Readable or writable in this build. + pub fn is_enabled(self) -> bool { + let c = caps(self); + c.read || c.write + } + + pub fn name(self) -> &'static str { + match self { + Format::Png => "PNG", + Format::Jpeg => "JPEG", + Format::Tiff => "TIFF", + Format::WebP => "WebP", + Format::Gif => "GIF", + Format::Bmp => "BMP", + Format::Tga => "TGA", + Format::Ico => "ICO", + Format::Pnm => "Netpbm", + Format::Pcx => "PCX", + Format::Xpm => "XPM", + Format::Dds => "DDS", + Format::Qoi => "QOI", + Format::OpenExr => "OpenEXR", + Format::Hdr => "Radiance HDR", + Format::Avif => "AVIF", + Format::Heif => "HEIF", + Format::Fits => "FITS", + Format::Dpx => "DPX", + Format::Jxl => "JPEG XL", + Format::Cineon => "Cineon", + Format::Dicom => "DICOM", + Format::Iff => "Amiga IFF", + } + } + + /// Recognized file extensions (lowercase, without dot); the first one is + /// the preferred extension for writing. + pub fn extensions(self) -> &'static [&'static str] { + match self { + Format::Png => &["png", "apng"], + Format::Jpeg => &["jpg", "jpeg", "jpe", "jfif"], + Format::Tiff => &["tif", "tiff"], + Format::WebP => &["webp"], + Format::Gif => &["gif"], + Format::Bmp => &["bmp", "dib"], + Format::Tga => &["tga", "icb", "vda", "vst"], + Format::Ico => &["ico"], + Format::Pnm => &["pnm", "pbm", "pgm", "ppm", "pam", "pfm"], + Format::Pcx => &["pcx"], + Format::Xpm => &["xpm"], + Format::Dds => &["dds"], + Format::Qoi => &["qoi"], + Format::OpenExr => &["exr"], + Format::Hdr => &["hdr"], + Format::Avif => &["avif"], + Format::Heif => &["heic", "heif", "hif"], + Format::Fits => &["fits", "fit", "fts"], + Format::Dpx => &["dpx"], + Format::Jxl => &["jxl"], + Format::Cineon => &["cin"], + Format::Dicom => &["dcm", "dicom", "dc3"], + Format::Iff => &["iff", "lbm", "ilbm"], + } + } + + pub fn mime_type(self) -> &'static str { + match self { + Format::Png => "image/png", + Format::Jpeg => "image/jpeg", + Format::Tiff => "image/tiff", + Format::WebP => "image/webp", + Format::Gif => "image/gif", + Format::Bmp => "image/bmp", + Format::Tga => "image/x-tga", + Format::Ico => "image/x-icon", + Format::Pnm => "image/x-portable-anymap", + Format::Pcx => "image/x-pcx", + Format::Xpm => "image/x-xpixmap", + Format::Dds => "image/vnd-ms.dds", + Format::Qoi => "image/qoi", + Format::OpenExr => "image/x-exr", + Format::Hdr => "image/vnd.radiance", + Format::Avif => "image/avif", + Format::Heif => "image/heif", + Format::Fits => "image/fits", + Format::Dpx => "image/x-dpx", + Format::Jxl => "image/jxl", + Format::Cineon => "image/x-cin", + Format::Dicom => "application/dicom", + Format::Iff => "image/x-iff", + } + } + + /// Maximum encodable dimensions, if the format has a hard limit. + pub fn max_dimensions(self) -> Option<(u32, u32)> { + match self { + Format::Jpeg | Format::Gif | Format::Iff => Some((65535, 65535)), + Format::WebP => Some((16384, 16384)), + Format::Ico => Some((256, 256)), + _ => None, + } + } +} + +/// Capability table. `read`/`write` reflect the current build's features. +pub fn caps(format: Format) -> FormatCaps { + let base = FormatCaps { + read: true, + write: true, + depths: &[S::U8], + layouts: &[L::Rgb, L::Rgba], + alpha: true, + icc: false, + exif: false, + xmp: false, + dpi: false, + text: false, + animation: false, + lossy: false, + }; + match format { + Format::Png => { + FormatCaps { depths: &[S::U8, S::U16], layouts: RGB_GRAY, icc: true, exif: true, xmp: true, dpi: true, text: true, animation: true, ..base } + } + Format::Jpeg => FormatCaps { layouts: &[L::Gray, L::Rgb, L::Cmyk], alpha: false, icc: true, exif: true, xmp: true, dpi: true, lossy: true, ..base }, + Format::Tiff => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: TIFF_LAYOUTS, icc: true, exif: true, xmp: true, dpi: true, text: true, ..base }, + // Resolution travels in EXIF (WebP has no field of its own). + Format::WebP => FormatCaps { icc: true, exif: true, xmp: true, dpi: true, animation: true, ..base }, + Format::Gif => FormatCaps { layouts: &[L::Rgba], animation: true, lossy: true, ..base }, + Format::Bmp => base, + Format::Tga => FormatCaps { layouts: RGB_GRAY, ..base }, + Format::Ico => FormatCaps { layouts: &[L::Rgba], ..base }, + Format::Pnm => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: ALL_LAYOUTS, ..base }, + Format::Pcx => FormatCaps { read: true, write: false, layouts: &[L::Rgb], alpha: false, ..base }, + Format::Xpm => FormatCaps { read: true, write: false, layouts: &[L::Rgba], ..base }, + // Uncompressed by default; a block compression is lossy (see `fidelity_warnings_with`). + Format::Dds => FormatCaps { layouts: RGB_GRAY, ..base }, + Format::Qoi => base, + Format::OpenExr => FormatCaps { depths: &[S::F16, S::F32], layouts: RGB_GRAY, ..base }, + Format::Hdr => FormatCaps { depths: &[S::F32], layouts: &[L::Rgb], alpha: false, lossy: true, ..base }, + Format::Avif => FormatCaps { read: false, write: cfg!(feature = "avif"), lossy: true, ..base }, + Format::Heif => { + FormatCaps { read: cfg!(feature = "heif"), write: false, depths: &[S::U8, S::U16], icc: true, exif: true, xmp: true, lossy: true, ..base } + } + Format::Fits => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: &[L::Gray, L::Rgb], alpha: false, text: true, ..base }, + // `text`: the DPX header fields only (see `fidelity_warnings_with`). + Format::Dpx => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb, L::Rgba], text: true, ..base }, + // GrayA is widened to RGBA at the same depth; the 16-bit alpha header is corrected. + Format::Jxl => FormatCaps { read: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb, L::Rgba], ..base }, + Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, + Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, + Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + } +} + +/// Identify a format from its leading bytes (magic numbers). TGA has no +/// magic and is recognized last, by header plausibility. +pub fn detect(bytes: &[u8]) -> Option { + let b = bytes; + if b.starts_with(b"\x89PNG\r\n\x1a\n") { + return Some(Format::Png); + } + if b.starts_with(&[0xFF, 0xD8, 0xFF]) { + return Some(Format::Jpeg); + } + // JPEG XL bare codestream and ISO/IEC 18181-2 container signature box. + if b.starts_with(&[0xFF, 0x0A]) || b.starts_with(b"\0\0\0\x0cJXL \r\n\x87\n") { + return Some(Format::Jxl); + } + if b.starts_with(b"II*\0") || b.starts_with(b"MM\0*") || b.starts_with(b"II+\0") || b.starts_with(b"MM\0+") { + return Some(Format::Tiff); + } + if b.len() >= 12 && &b[0..4] == b"RIFF" && &b[8..12] == b"WEBP" { + return Some(Format::WebP); + } + if b.starts_with(b"GIF87a") || b.starts_with(b"GIF89a") { + return Some(Format::Gif); + } + if b.starts_with(b"qoif") { + return Some(Format::Qoi); + } + if b.starts_with(b"DDS ") { + return Some(Format::Dds); + } + if b.starts_with(&[0x76, 0x2F, 0x31, 0x01]) { + return Some(Format::OpenExr); + } + if b.starts_with(b"#?RADIANCE") || b.starts_with(b"#?RGBE") { + return Some(Format::Hdr); + } + if b.len() >= 12 && &b[4..8] == b"ftyp" { + if is_avif_ftyp(b) { + return Some(Format::Avif); + } + if is_heif_ftyp(b) { + return Some(Format::Heif); + } + } + if b.len() >= 14 && b.starts_with(b"BM") { + return Some(Format::Bmp); + } + if b.len() >= 6 && b[0..4] == [0, 0, 1, 0] && (b[4] != 0 || b[5] != 0) { + return Some(Format::Ico); + } + if b.len() >= 3 && b[0] == b'P' && matches!(b[1], b'1'..=b'7' | b'F' | b'f') && b[2].is_ascii_whitespace() { + return Some(Format::Pnm); + } + if b.starts_with(b"SDPX") || b.starts_with(b"XPDS") { + return Some(Format::Dpx); + } + if crate::codecs::dpx::is_cineon(b) { + return Some(Format::Cineon); + } + if crate::codecs::dicom::is_dicom(b) { + return Some(Format::Dicom); + } + if crate::codecs::iff::looks_like_iff(b) { + return Some(Format::Iff); + } + if b.starts_with(b"SIMPLE =") { + return Some(Format::Fits); + } + if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { + return Some(Format::Xpm); + } + if crate::codecs::pcx::looks_like_pcx(b) { + return Some(Format::Pcx); + } + if looks_like_tga(b) { + return Some(Format::Tga); + } + None +} + +/// The `ftyp` brands: the major brand, then the compatible ones (the minor version between them is skipped). +fn ftyp_brands(b: &[u8]) -> impl Iterator { + let size = u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize; + let end = size.clamp(12, b.len().min(64)); + b[8..end].as_chunks::<4>().0.iter().enumerate().filter(|(i, _)| *i != 1).map(|(_, c)| c) +} + +fn is_avif_ftyp(b: &[u8]) -> bool { + ftyp_brands(b).any(|c| c == b"avif" || c == b"avis") +} + +/// HEVC-coded HEIF (`heic`, `heix`, ...) or a generic HEIF file (`mif1`, `msf1`, `miaf`) that is +/// not AVIF (checked first). Sequence-only files (`hevc`, `msf1`) are recognised so that opening +/// one says what is unsupported instead of "unrecognized format". +fn is_heif_ftyp(b: &[u8]) -> bool { + ftyp_brands(b).any(|c| matches!(c, b"heic" | b"heix" | b"heim" | b"heis" | b"hevc" | b"hevx" | b"mif1" | b"msf1" | b"miaf")) +} + +fn looks_like_tga(b: &[u8]) -> bool { + if b.len() < 18 { + return false; + } + if b.len() >= 26 && &b[b.len() - 18..] == b"TRUEVISION-XFILE.\0" { + return true; + } + let cmap_type = b[1]; + let img_type = b[2]; + let cmap_len = u16::from_le_bytes([b[5], b[6]]); + let cmap_depth = b[7]; + let w = u16::from_le_bytes([b[12], b[13]]); + let h = u16::from_le_bytes([b[14], b[15]]); + let depth = b[16]; + let desc = b[17]; + let type_ok = matches!(img_type, 1 | 2 | 3 | 9 | 10 | 11); + let cmap_ok = match cmap_type { + 0 => cmap_len == 0 && cmap_depth == 0 && !matches!(img_type, 1 | 9), + 1 => matches!(img_type, 1 | 9) && cmap_len > 0 && matches!(cmap_depth, 15 | 16 | 24 | 32), + _ => false, + }; + let depth_ok = match img_type { + 1 | 9 => depth == 8, + 3 | 11 => matches!(depth, 8 | 16), + _ => matches!(depth, 15 | 16 | 24 | 32), + }; + type_ok && cmap_ok && depth_ok && w > 0 && h > 0 && desc & 0xC0 == 0 +} + +/// Look up a format by file extension (case-insensitive, with or without a +/// leading dot, or a full file name/path). +pub fn from_extension(ext: &str) -> Option { + let ext = ext.rsplit(['.', '/', '\\']).next().unwrap_or(ext).to_ascii_lowercase(); + Format::ALL.into_iter().find(|f| f.extensions().contains(&ext.as_str())) +} diff --git a/crates/image-codecs/src/image.rs b/crates/image-codecs/src/image.rs new file mode 100644 index 000000000..fb3294948 --- /dev/null +++ b/crates/image-codecs/src/image.rs @@ -0,0 +1,1028 @@ +//! The crate's own flat raster image type. +//! +//! Samples are stored interleaved (`RGBARGBA…`), row-major, top-to-bottom, +//! in **native endianness**. Integer samples are unsigned and normalized to +//! `[0, max]`; float samples are nominally `[0, 1]` but may exceed it (HDR). + +use std::fmt; + +use crate::error::CodecError; +use crate::format::Format; +use half::f16; + +/// Channel layout of an [`Image`]. Alpha, when present, is always the last +/// channel and is *straight* (not premultiplied). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum ChannelLayout { + Gray, + GrayA, + Rgb, + Rgba, + /// Ink amounts, `0 = no ink`, `max = full ink` (not Adobe-inverted). + Cmyk, + CmykA, + /// CIE L*a*b* (D50), as PhotoCraft's Lab documents hold it: L* 0..100 as 0..1, a* and b* + /// −128..127 as `(v + 128) / 255` (Photoshop's unsigned encoding). + Lab, + LabA, +} + +impl ChannelLayout { + pub const ALL: [ChannelLayout; 8] = [ + ChannelLayout::Gray, + ChannelLayout::GrayA, + ChannelLayout::Rgb, + ChannelLayout::Rgba, + ChannelLayout::Cmyk, + ChannelLayout::CmykA, + ChannelLayout::Lab, + ChannelLayout::LabA, + ]; + + /// Total number of channels including alpha. + pub const fn channels(self) -> usize { + match self { + ChannelLayout::Gray => 1, + ChannelLayout::GrayA => 2, + ChannelLayout::Rgb | ChannelLayout::Lab => 3, + ChannelLayout::Rgba | ChannelLayout::Cmyk | ChannelLayout::LabA => 4, + ChannelLayout::CmykA => 5, + } + } + + /// Number of colour (non-alpha) channels. + pub const fn color_channels(self) -> usize { + self.channels() - self.has_alpha() as usize + } + + pub const fn has_alpha(self) -> bool { + matches!(self, ChannelLayout::GrayA | ChannelLayout::Rgba | ChannelLayout::CmykA | ChannelLayout::LabA) + } + + pub const fn is_gray(self) -> bool { + matches!(self, ChannelLayout::Gray | ChannelLayout::GrayA) + } + + pub const fn is_rgb(self) -> bool { + matches!(self, ChannelLayout::Rgb | ChannelLayout::Rgba) + } + + pub const fn is_cmyk(self) -> bool { + matches!(self, ChannelLayout::Cmyk | ChannelLayout::CmykA) + } + + pub const fn is_lab(self) -> bool { + matches!(self, ChannelLayout::Lab | ChannelLayout::LabA) + } + + /// The same colour model with alpha added. + pub const fn with_alpha(self) -> Self { + match self { + ChannelLayout::Gray | ChannelLayout::GrayA => ChannelLayout::GrayA, + ChannelLayout::Rgb | ChannelLayout::Rgba => ChannelLayout::Rgba, + ChannelLayout::Cmyk | ChannelLayout::CmykA => ChannelLayout::CmykA, + ChannelLayout::Lab | ChannelLayout::LabA => ChannelLayout::LabA, + } + } + + /// The same colour model without alpha. + pub const fn without_alpha(self) -> Self { + match self { + ChannelLayout::Gray | ChannelLayout::GrayA => ChannelLayout::Gray, + ChannelLayout::Rgb | ChannelLayout::Rgba => ChannelLayout::Rgb, + ChannelLayout::Cmyk | ChannelLayout::CmykA => ChannelLayout::Cmyk, + ChannelLayout::Lab | ChannelLayout::LabA => ChannelLayout::Lab, + } + } + + /// `true` if both layouts share a colour model (ignoring alpha). + pub const fn same_model(self, other: Self) -> bool { + (self.is_gray() && other.is_gray()) || (self.is_rgb() && other.is_rgb()) || (self.is_cmyk() && other.is_cmyk()) || (self.is_lab() && other.is_lab()) + } +} + +/// Storage type of one sample. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum SampleType { + U8, + U16, + /// IEEE 754 half float, stored as its 16 bit pattern. + F16, + F32, +} + +impl SampleType { + pub const ALL: [SampleType; 4] = [SampleType::U8, SampleType::U16, SampleType::F16, SampleType::F32]; + + pub const fn bytes(self) -> usize { + match self { + SampleType::U8 => 1, + SampleType::U16 | SampleType::F16 => 2, + SampleType::F32 => 4, + } + } + + pub const fn is_float(self) -> bool { + matches!(self, SampleType::F16 | SampleType::F32) + } +} + +/// Basic, format-independent metadata. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Metadata { + /// Raw EXIF payload in TIFF structure (starting with `II*\0`/`MM\0*`), + /// **without** the JPEG `Exif\0\0` prefix. + pub exif: Option>, + /// XMP packet as UTF-8. + pub xmp: Option, + /// Horizontal and vertical resolution in dots per inch. + pub dpi: Option<(f32, f32)>, + /// Free-form key/value text (PNG tEXt/iTXt, TIFF ASCII tags). + pub text: Vec<(String, String)>, + /// Photoshop image resources (`8BIM` resource blocks): TIFF tag 34377. Kept opaque here; + /// `photocraft-io` reads and writes them through `photocraft-psd`. + pub photoshop_resources: Option>, + /// Photoshop layer data (TIFF tag 37724 `ImageSourceData`): a layered TIFF's layers. Kept + /// opaque here; `photocraft-io` reads and writes it through `photocraft-psd`. + pub photoshop_layers: Option>, + /// Samples per pixel beyond the colour channels and the one alpha sample. TIFF extra + /// samples (spot colours, extra alpha channels) the pixel layout does not hold. Each entry + /// is one sample per pixel, native endian, in the image's [`SampleType`], row-major. + pub extra_samples: Vec, +} + +/// How many extra TIFF samples an encoded file may carry. Decoders keep at most this many. +/// Sixteen covers a UV-DTF stack (white, varnish, gloss, primer, and further spots) without +/// approaching the PSD 56-channel limit. +pub const MAX_EXTRA_SAMPLES: usize = 16; + +/// One TIFF extra sample (a spot channel or an alpha channel that is not the image alpha). +#[derive(Debug, Clone, PartialEq)] +pub struct ExtraSample { + /// TIFF ExtraSamples value: 0 unspecified, 1 associated alpha, 2 unassociated alpha. + pub kind: u16, + /// One sample per pixel, native endian, same sample type as the image. + pub data: Vec, +} + +impl Metadata { + pub fn is_empty(&self) -> bool { + self.exif.is_none() + && self.xmp.is_none() + && self.dpi.is_none() + && self.text.is_empty() + && self.photoshop_resources.is_none() + && self.photoshop_layers.is_none() + && self.extra_samples.is_empty() + } +} + +/// Something a decoder noticed about the file that the decoded pixels don't show: the file +/// held more than was decoded, or less than it should. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum DecodeWarning { + /// Only the first frame of an animation was decoded; `total` counts every frame when known. + MoreFrames { total: Option }, + /// Only the first page of a multi-page file was decoded; `total` counts every page when known. + MorePages { total: Option }, + /// Only one part of a multi-part file was decoded; `total` counts every part when known. + MoreParts { total: Option }, + /// The image opened on the full `display_window` stage; the data region sits at `origin`. + DisplayWindowStage { stage: (u32, u32), origin: (i32, i32) }, + /// The image data ends early (truncated or damaged file); the decoder filled in the rest + /// (a baseline JPEG's missing rows come out grey). + Truncated { format: Format }, + /// Deep image data (per-pixel sample lists) was composited into the flat image; the samples + /// themselves (Z and other deep channels) are not part of the flat result. + DeepFlattened { format: Format, max_samples_per_pixel: u32 }, + /// Only the first image element of a DPX file was decoded; `total` counts them all. + MoreElements { total: u32 }, + /// A DPX element's transfer characteristic and colorimetric specification (SMPTE 268M codes, + /// 255 when undefined). The code values are imported as stored, without a colour transform. + DpxEncoding { transfer: u8, colorimetric: u8 }, + /// The file's `count` extra samples (spot or extra alpha channels) did not fit the allocation + /// limit next to the image, so only the image was decoded. + ExtraSamplesDropped { format: Format, count: u32 }, + /// The file has `count` extra samples past the first [`MAX_EXTRA_SAMPLES`], which are all a + /// decoder keeps; those past the cap were not imported. + ExtraSamplesOverLimit { format: Format, count: u32 }, +} + +/// The SMPTE 268M name of a transfer characteristic or colorimetric code. +fn dpx_code_name(code: u8) -> &'static str { + match code { + 0 => "user-defined", + 1 => "printing density", + 2 => "linear", + 3 => "logarithmic", + 4 => "unspecified video", + 5 => "SMPTE 274M", + 6 => "ITU-R BT.709", + 7 => "ITU-R BT.601 625-line", + 8 => "ITU-R BT.601 525-line", + 9 => "NTSC composite video", + 10 => "PAL composite video", + 11 => "Z linear", + 12 => "Z homogeneous", + 255 => "undefined", + _ => "unknown", + } +} + +impl fmt::Display for DecodeWarning { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + DecodeWarning::MoreFrames { total: Some(n) } => write!(f, "only the first of {n} frames was imported"), + DecodeWarning::MoreFrames { total: None } => write!(f, "only the first frame of the animation was imported"), + DecodeWarning::MorePages { total: Some(n) } => write!(f, "only the first of {n} pages was imported"), + DecodeWarning::MorePages { total: None } => write!(f, "only the first page of the file was imported"), + DecodeWarning::MoreParts { total: Some(n) } => write!(f, "only one of {n} EXR parts was imported"), + DecodeWarning::MoreParts { total: None } => write!(f, "only one EXR part of the file was imported"), + DecodeWarning::DisplayWindowStage { stage, origin } => { + write!(f, "opened on the full display window stage ({}x{}); the data sits at ({}, {})", stage.0, stage.1, origin.0, origin.1) + } + DecodeWarning::Truncated { format } => { + write!(f, "{} data ends early (the file is truncated or damaged); part of the image is missing", format.name()) + } + DecodeWarning::DeepFlattened { format, max_samples_per_pixel } => write!( + f, + "{format} deep data was composited into a flat image (up to {max_samples_per_pixel} samples per pixel); per-pixel depth is not kept", + format = format.name() + ), + DecodeWarning::MoreElements { total } => write!(f, "only the first of {total} image elements was imported"), + DecodeWarning::DpxEncoding { transfer, colorimetric } => write!( + f, + "DPX transfer characteristic: {} ({transfer}), colorimetric: {} ({colorimetric}); the code values were imported as stored, without a colour transform", + dpx_code_name(*transfer), + dpx_code_name(*colorimetric) + ), + DecodeWarning::ExtraSamplesDropped { format, count } => write!( + f, + "{count} extra {} file channel{} (spot or alpha) did not fit in memory and {} not imported", + format.name(), + if *count == 1 { "" } else { "s" }, + if *count == 1 { "was" } else { "were" } + ), + DecodeWarning::ExtraSamplesOverLimit { format, count } => write!( + f, + "{count} extra {} file channel{} (spot or alpha) past the first {MAX_EXTRA_SAMPLES} {} not imported", + format.name(), + if *count == 1 { "" } else { "s" }, + if *count == 1 { "was" } else { "were" } + ), + } + } +} + +/// One channel of a [`DeepImage`]: the samples of every pixel, one flat list in pixel order. +#[derive(Debug, Clone, PartialEq)] +pub struct DeepChannel { + /// The channel name as in the file (e.g. `R`, `G`, `B`, `A`, `Z`). + pub name: String, + /// The sample type as stored in the file; `samples` always holds the numeric value as + /// f32 (half and 32-bit floats exactly; 32-bit unsigned integers exactly up to 2^24). + pub sample: SampleType, + /// Every sample of the channel, in pixel order; the samples of pixel `i` are + /// `counts[i]..counts[i+1]` (see [`DeepImage::counts`]). + pub samples: Vec, +} + +/// Deep image data (OpenEXR `deepscanline`/`deeptile`): a variable-length list of samples +/// per pixel, each with its own colour, alpha and depth. All channels share the same sample +/// layout, so a sample is one index into every channel. +#[derive(Debug, Clone, PartialEq)] +pub struct DeepImage { + pub width: u32, + pub height: u32, + /// The full stage the image opens on: the EXR `display_window` size, when the file declares + /// one the data region fits into and the limits allow. `None` when absent, absurd or not + /// containing the data region — then the image opens on the data window alone, as before. + pub stage: Option<(u32, u32)>, + /// Where this image's (0, 0) sits on the stage: `data_window.position - display_window.position`. + /// `(0, 0)` when [`Self::stage`] is `None`. + pub origin: (i32, i32), + /// The sample channels in file order. + pub channels: Vec, + /// Cumulative sample counts, `len == width as usize * height as usize + 1`: pixel `i` + /// holds the samples `counts[i]..counts[i+1]` of every channel. Monotonically + /// non-decreasing, starting and ending at the total sample count. + pub counts: Vec, +} + +impl DeepImage { + /// A channel by its exact name. + pub fn channel(&self, name: &str) -> Option<&DeepChannel> { + self.channels.iter().find(|c| c.name == name) + } + + /// The samples of one pixel, as indices into every channel's `samples`. + pub fn sample_range(&self, pixel: usize) -> std::ops::Range { + let end = self.counts.get(pixel + 1).copied().unwrap_or(0); + self.counts.get(pixel).copied().unwrap_or(end)..end + } + + /// The total number of samples over all pixels. + pub fn total_samples(&self) -> u64 { + self.counts.last().copied().unwrap_or(0) + } +} + +/// One channel of an [`ExrPartInfo`]: the name as in the file (a Maya/Arnold AOV layer +/// keeps its prefix, e.g. `diffuse.R`) and the sample type that part stores it with. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ExrChannelInfo { + pub name: String, + pub sample: SampleType, +} + +/// One part of a multi-part OpenEXR file (what a Maya/Arnold render writes per AOV): +/// header facts only, no pixel data. [`crate::exr_info`] lists them, and the index is +/// what [`crate::decode_exr_part`] decodes; [`crate::decode_as_with`] opens the part +/// the ranking in [`ExrPartInfo::color_rank`] prefers. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ExrPartInfo { + /// Index into the part list, in file order. + pub index: usize, + /// The part `name` attribute, when the file records one (Arnold names parts after AOVs). + pub name: Option, + /// The part `view` attribute for stereo files (`left`/`right`). + pub view: Option, + pub width: u32, + pub height: u32, + /// Absolute data-window bounds: x, y, width and height. + pub data_window: (i32, i32, u32, u32), + /// Absolute display-window bounds: x, y, width and height. + pub display_window: (i32, i32, u32, u32), + /// True for `deepscanline`/`deeptile` parts; those are composited by the deep + /// decoder ([`crate::decode_deep_exr`]), not decoded part by part. + pub deep: bool, + /// True when the part is stored as tiles instead of scanlines. + pub tiled: bool, + /// The part's channels in file order. + pub channels: Vec, +} + +impl ExrPartInfo { + /// How much this part looks like a colour image the plain [`Image`] decode can show: + /// 4 RGBA, 3 RGB, 2 YA, 1 Y or any-single-channel, 0 anything else (e.g. `Z` beside `A`). + /// The colour and alpha channels must share a complete layer prefix. The highest-ranked + /// group wins; equal groups prefer unprefixed channels, then the first prefix alphabetically. + /// The auto-pick takes the highest rank; ties go to the earlier part. + pub fn color_rank(&self) -> u8 { + exr_color_channels(self.channels.iter().map(|c| c.name.as_str())).map_or(0, |(layout, _)| layout.channels() as u8) + } +} + +/// One selection policy for header ranking/probing and flat decoding: never synthesize +/// RGB(A) or YA from separate layers. OpenEXR layers use the full prefix before the last +/// dot, including nested layers; a leading or trailing dot alone does not name a layer. +pub(crate) fn exr_color_channels<'a>(names: impl IntoIterator) -> Option<(ChannelLayout, Vec)> { + #[derive(Default)] + struct Group { + r: Option, + g: Option, + b: Option, + a: Option, + y: Option, + } + + // Sorted prefixes make rank ties deterministic independently of channel order, with + // the empty (unprefixed) group first. Unsupported channels cannot complete a group. + let mut groups = std::collections::BTreeMap::<&str, Group>::new(); + let mut count = 0usize; + for (index, name) in names.into_iter().enumerate() { + count = count.saturating_add(1); + let (prefix, base) = name.rsplit_once('.').filter(|(prefix, base)| !prefix.is_empty() && !base.is_empty()).unwrap_or(("", name)); + if !matches!(base, "R" | "G" | "B" | "A" | "Y") { + continue; + } + let group = groups.entry(prefix).or_default(); + let component = match base { + "R" => &mut group.r, + "G" => &mut group.g, + "B" => &mut group.b, + "A" => &mut group.a, + "Y" => &mut group.y, + _ => continue, + }; + component.get_or_insert(index); + } + + let mut best = None; + let mut best_rank = 0; + for group in groups.values() { + let candidate = if let (Some(r), Some(g), Some(b)) = (group.r, group.g, group.b) { + match group.a { + Some(a) => (ChannelLayout::Rgba, vec![r, g, b, a]), + None => (ChannelLayout::Rgb, vec![r, g, b]), + } + } else if let Some(y) = group.y { + match group.a { + Some(a) => (ChannelLayout::GrayA, vec![y, a]), + None => (ChannelLayout::Gray, vec![y]), + } + } else { + continue; + }; + let rank = candidate.0.channels(); + if rank > best_rank { + best = Some(candidate); + best_rank = rank; + } + } + best.or_else(|| (count == 1).then(|| (ChannelLayout::Gray, vec![0]))) +} + +/// Strip an optional `layer.` prefix from a channel name (`diffuse.R` → `R`). +pub(crate) fn base_channel_name(name: &str) -> &str { + name.rsplit('.').next().unwrap_or(name) +} + +/// One Cryptomatte layer of an EXR file (Cryptomatte specification 1.2): per-pixel object or +/// material IDs with coverage, as written by Arnold, V-Ray, Redshift, Mantra/Karma or Cycles. +/// Header facts plus the parsed manifest; the samples come from +/// [`crate::decode_cryptomatte`]. +#[derive(Debug, Clone, PartialEq)] +pub struct CryptomatteLayer { + /// The part (see [`crate::exr_info`]) the channels and attributes live in. + pub part: usize, + /// The `cryptomatte//name` attribute, e.g. `CryptoAsset` or `crypto_asset`. + pub name: String, + /// The 7-hex-digit metadata key of the layer. + pub key: String, + /// The `conversion` attribute, when recorded (canonically `uint32_to_float32`). + pub conversion: Option, + /// The manifest: object names with their ID values. A sidecar manifest file is only + /// recorded in [`Self::sidecar_manifest`] and not read here (the codec is I/O free). + pub manifest: Vec<(f32, String)>, + /// The `manif_file` sidecar attribute, when the manifest is not embedded. + pub sidecar_manifest: Option, + /// The stream channel bases in file order, e.g. `["crypto_asset00", "crypto_asset01"]`; + /// each holds an ID (`.red`) and a coverage (`.green`) channel. + pub channels: Vec, +} + +impl CryptomatteLayer { + /// The manifest name for an ID value, when the manifest lists it. + pub fn name_of(&self, id: f32) -> Option<&str> { + self.manifest.iter().find(|(i, _)| *i == id).map(|(_, n)| n.as_str()) + } +} + +/// Decoded Cryptomatte samples: per pixel the (ID, coverage) pairs with coverage above zero, +/// sorted by descending coverage. The IDs are the float values as stored (compared by value, +/// never by bit pattern, like the reference readers). +#[derive(Debug, Clone, PartialEq)] +pub struct CryptomatteBuffer { + pub width: u32, + pub height: u32, + pub pixels: Vec>, +} + +impl CryptomatteBuffer { + /// The pairs of one pixel; empty for a pixel covered by nothing. + pub fn at(&self, x: u32, y: u32) -> &[(f32, f32)] { + let i = y as usize * self.width as usize + x as usize; + self.pixels.get(i).map_or(&[], |v| v) + } +} + +/// A single flat raster image. +#[derive(Debug, Clone, PartialEq)] +pub struct Image { + width: u32, + height: u32, + layout: ChannelLayout, + sample: SampleType, + data: Vec, + /// Embedded ICC profile (must describe `layout`'s colour model). + pub icc: Option>, + /// The file's CICP colour description (a HEIF `nclx`), when it has one. Only an image whose + /// `icc` is `None` relies on it. + pub cicp: Option, + pub meta: Metadata, + /// What the decoder noticed about the file: empty for a complete, single image and for + /// images built in memory. Encoders ignore it. + pub warnings: Vec, +} + +fn byte_len(width: u32, height: u32, layout: ChannelLayout, sample: SampleType) -> Option { + (width as usize).checked_mul(height as usize)?.checked_mul(layout.channels())?.checked_mul(sample.bytes()) +} + +impl Image { + /// A zero-filled image, or an error if the byte size overflows `usize`. + pub fn new(width: u32, height: u32, layout: ChannelLayout, sample: SampleType) -> Result { + let len = byte_len(width, height, layout, sample).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; + Ok(Image { width, height, layout, sample, data: vec![0; len], icc: None, cicp: None, meta: Metadata::default(), warnings: Vec::new() }) + } + + /// Wrap raw interleaved, native-endian bytes. + pub fn from_raw(width: u32, height: u32, layout: ChannelLayout, sample: SampleType, data: Vec) -> Result { + let expected = byte_len(width, height, layout, sample).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; + if data.len() != expected { + return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{height} {layout:?} {sample:?}", data.len()))); + } + Ok(Image { width, height, layout, sample, data, icc: None, cicp: None, meta: Metadata::default(), warnings: Vec::new() }) + } + + pub fn from_u8(width: u32, height: u32, layout: ChannelLayout, data: Vec) -> Result { + Self::from_raw(width, height, layout, SampleType::U8, data) + } + + pub fn from_u16(width: u32, height: u32, layout: ChannelLayout, data: &[u16]) -> Result { + let bytes = data.iter().flat_map(|v| v.to_ne_bytes()).collect(); + Self::from_raw(width, height, layout, SampleType::U16, bytes) + } + + pub fn from_f16(width: u32, height: u32, layout: ChannelLayout, data: &[f16]) -> Result { + let bytes = data.iter().flat_map(|v| v.to_bits().to_ne_bytes()).collect(); + Self::from_raw(width, height, layout, SampleType::F16, bytes) + } + + pub fn from_f32(width: u32, height: u32, layout: ChannelLayout, data: &[f32]) -> Result { + let bytes = data.iter().flat_map(|v| v.to_ne_bytes()).collect(); + Self::from_raw(width, height, layout, SampleType::F32, bytes) + } + + /// Build an image from normalized f32 samples (`[0,1]` for integer + /// targets; values are clamped and rounded when quantizing). + pub fn from_normalized(width: u32, height: u32, layout: ChannelLayout, sample: SampleType, values: &[f32]) -> Result { + let data = quantize(values, sample); + Self::from_raw(width, height, layout, sample, data) + } + + pub fn with_icc(mut self, icc: Option>) -> Self { + self.icc = icc; + self + } + + pub fn with_meta(mut self, meta: Metadata) -> Self { + self.meta = meta; + self + } + + pub fn width(&self) -> u32 { + self.width + } + pub fn height(&self) -> u32 { + self.height + } + pub fn dimensions(&self) -> (u32, u32) { + (self.width, self.height) + } + pub fn layout(&self) -> ChannelLayout { + self.layout + } + pub fn sample_type(&self) -> SampleType { + self.sample + } + pub fn pixel_count(&self) -> usize { + self.width as usize * self.height as usize + } + /// Number of samples (pixels × channels). + pub fn sample_count(&self) -> usize { + self.pixel_count() * self.layout.channels() + } + /// Raw interleaved native-endian bytes. + pub fn data(&self) -> &[u8] { + &self.data + } + pub fn data_mut(&mut self) -> &mut [u8] { + &mut self.data + } + pub fn into_data(self) -> Vec { + self.data + } + + /// Borrow samples as `u8` (only when the sample type is `U8`). + pub fn as_u8(&self) -> Option<&[u8]> { + (self.sample == SampleType::U8).then_some(&self.data[..]) + } + + /// Copy samples out as `u16` (only when the sample type is `U16`). + pub fn to_u16_samples(&self) -> Option> { + (self.sample == SampleType::U16).then(|| self.data.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes([c[0], c[1]])).collect()) + } + + /// Copy samples out as `f16` (only when the sample type is `F16`). + pub fn to_f16_samples(&self) -> Option> { + (self.sample == SampleType::F16).then(|| self.data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]]))).collect()) + } + + /// Copy samples out as `f32` (only when the sample type is `F32`). + pub fn to_f32_samples(&self) -> Option> { + (self.sample == SampleType::F32).then(|| self.data.as_chunks::<4>().0.iter().map(|c| f32::from_ne_bytes([c[0], c[1], c[2], c[3]])).collect()) + } + + /// Sample `index` (in samples, not bytes) as a normalized f32. + pub fn sample_normalized(&self, index: usize) -> f32 { + read_normalized(&self.data, self.sample, index) + } + + /// Normalized f32 of channel `c` at pixel `(x, y)`. + pub fn get(&self, x: u32, y: u32, c: usize) -> f32 { + let idx = (y as usize * self.width as usize + x as usize) * self.layout.channels() + c; + self.sample_normalized(idx) + } + + /// All samples as normalized f32 (integers mapped to `[0,1]`). + pub fn to_normalized(&self) -> Vec { + to_normalized(&self.data, self.sample) + } + + /// `true` when the image has alpha and at least one pixel is not fully + /// opaque. + pub fn has_translucency(&self) -> bool { + if !self.layout.has_alpha() { + return false; + } + let ch = self.layout.channels(); + (0..self.pixel_count()).any(|p| self.sample_normalized(p * ch + ch - 1) < 1.0) + } + + /// `true` when this is a float image with any sample outside `[0, 1]` + /// (or NaN). + pub fn has_out_of_range(&self) -> bool { + if !self.sample.is_float() { + return false; + } + (0..self.sample_count()).any(|i| { + let v = self.sample_normalized(i); + !(0.0..=1.0).contains(&v) + }) + } + + /// Convert to another layout and/or sample type. The ICC profile is kept + /// only if the colour model is unchanged (an RGB profile on CMYK data + /// would be wrong). Metadata is kept. + pub fn convert(&self, layout: ChannelLayout, sample: SampleType) -> Image { + if layout == self.layout && sample == self.sample { + return self.clone(); + } + let data = convert_data(&self.data, self.layout, self.sample, layout, sample); + let mut meta = self.meta.clone(); + if sample != self.sample { + // Extra samples are stored in the image's sample type, so they convert with it. A + // plane of the wrong size is left as it is: importers drop it with a warning. + let plane_len = (self.width as usize).saturating_mul(self.height as usize).saturating_mul(self.sample.bytes()); + for e in meta.extra_samples.iter_mut().filter(|e| e.data.len() == plane_len) { + if let Ok(plane) = Image::from_raw(self.width, self.height, ChannelLayout::Gray, self.sample, std::mem::take(&mut e.data)) { + e.data = plane.convert(ChannelLayout::Gray, sample).data; + } + } + } + Image { + width: self.width, + height: self.height, + layout, + sample, + data, + icc: if layout.same_model(self.layout) { self.icc.clone() } else { None }, + cicp: if layout.same_model(self.layout) { self.cicp } else { None }, + meta, + warnings: self.warnings.clone(), + } + } + + /// [`Image::convert`], borrowing `self` when it already has that layout and sample type + /// (encoders use it so a full-size image isn't copied for nothing). + pub fn converted(&self, layout: ChannelLayout, sample: SampleType) -> std::borrow::Cow<'_, Image> { + if layout == self.layout && sample == self.sample { std::borrow::Cow::Borrowed(self) } else { std::borrow::Cow::Owned(self.convert(layout, sample)) } + } + + /// Interleaved RGBA, 8 bits per sample. + pub fn to_rgba8(&self) -> Vec { + self.convert(ChannelLayout::Rgba, SampleType::U8).data + } + + /// Interleaved RGBA, 16 bits per sample. + pub fn to_rgba16(&self) -> Vec { + self.convert(ChannelLayout::Rgba, SampleType::U16).to_u16_samples().unwrap_or_default() + } + + /// Interleaved RGBA as f32 (integer inputs normalized to `[0,1]`). + pub fn to_rgba_f32(&self) -> Vec { + convert_layout(&self.to_normalized(), self.layout, ChannelLayout::Rgba) + } +} + +/// The samples of `data` (`from` layout and sample type) in the `layout` and `sample` type: the +/// arithmetic of [`Image::convert`], on borrowed bytes. +fn convert_data(data: &[u8], from: ChannelLayout, from_sample: SampleType, layout: ChannelLayout, sample: SampleType) -> Vec { + if layout == from && sample == from_sample { + data.to_vec() + } else if sample == from_sample && !from.has_alpha() && layout == from.with_alpha() { + // Fast path (opening any opaque file): copy the samples, append an opaque alpha. + let bps = sample.bytes(); + let src = from.channels() * bps; + let one: Vec = match sample { + SampleType::U8 => vec![u8::MAX], + SampleType::U16 => u16::MAX.to_ne_bytes().to_vec(), + SampleType::F16 => f16::ONE.to_bits().to_ne_bytes().to_vec(), + SampleType::F32 => 1.0f32.to_ne_bytes().to_vec(), + }; + let mut out = Vec::with_capacity(data.len() / src * (src + bps)); + for px in data.chunks_exact(src) { + out.extend_from_slice(px); + out.extend_from_slice(&one); + } + out + } else if let Some(fast) = convert_int_direct(data, from, from_sample, layout, sample) { + fast + } else if layout == from { + quantize(&to_normalized(data, from_sample), sample) + } else { + let src = to_normalized(data, from_sample); + let values = convert_layout(&src, from, layout); + quantize(&values, sample) + } +} + +/// [`Image::convert`] on borrowed pixels: `rows` rows of `width` pixels in `from` (layout, sample +/// type) become the same pixels in `to`, bit for bit what the image conversion gives. Lets a +/// caller convert a band of a decoded image without copying it into an [`Image`] first. +pub fn convert_samples(src: &[u8], width: u32, rows: u32, from: (ChannelLayout, SampleType), to: (ChannelLayout, SampleType)) -> Result, CodecError> { + let expected = byte_len(width, rows, from.0, from.1).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; + if src.len() != expected { + return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{rows} {:?} {:?}", src.len(), from.0, from.1))); + } + Ok(convert_data(src, from.0, from.1, to.0, to.1)) +} + +/// Integer fast path of [`Image::convert`] for U8/U16 sources and targets whose result is +/// a pure channel copy: same colour model (alpha added as opaque, or dropped) or gray to +/// RGB. Bit-identical to the generic f32 route (see the equivalence tests) but without +/// the two intermediate f32 buffers. Returns `None` for everything else. +fn convert_int_direct(data: &[u8], from: ChannelLayout, from_sample: SampleType, layout: ChannelLayout, sample: SampleType) -> Option> { + let int = |s: SampleType| matches!(s, SampleType::U8 | SampleType::U16); + if !int(from_sample) || !int(sample) { + return None; + } + let gray_to_rgb = from.is_gray() && layout.is_rgb(); + if !gray_to_rgb && !layout.same_model(from) { + return None; + } + let one: &[u8] = match sample { + SampleType::U8 => &[u8::MAX], + _ => &u16::MAX.to_ne_bytes(), + }; + let (fc, tc) = (from.channels(), layout.channels()); + let (sb, db) = (from_sample.bytes(), sample.bytes()); + let (src_color, dst_color) = (from.color_channels(), layout.color_channels()); + let n = data.len() / (fc * sb); + let mut out = Vec::with_capacity(n.checked_mul(tc)?.checked_mul(db)?); + macro_rules! run { + ($conv:expr) => {{ + let conv = $conv; + for px in data.chunks_exact(fc * sb) { + for j in 0..dst_color { + let c = if src_color == 1 { 0 } else { j }; + conv(&px[c * sb..(c + 1) * sb], &mut out); + } + if layout.has_alpha() { + if from.has_alpha() { + conv(&px[src_color * sb..(src_color + 1) * sb], &mut out); + } else { + out.extend_from_slice(one); + } + } + } + }}; + } + match (from_sample, sample) { + (SampleType::U8, SampleType::U8) => run!(|s: &[u8], o: &mut Vec| o.push(s[0])), + (SampleType::U16, SampleType::U16) => run!(|s: &[u8], o: &mut Vec| o.extend_from_slice(s)), + (SampleType::U8, SampleType::U16) => run!(|s: &[u8], o: &mut Vec| o.extend_from_slice(&(u16::from(s[0]) * 257).to_ne_bytes())), + _ => run!(|s: &[u8], o: &mut Vec| o.push(((u32::from(u16::from_ne_bytes([s[0], s[1]])) + 128) / 257) as u8)), + } + Some(out) +} + +// --------------------------------------------------------------------------- +// Sample conversion helpers +// --------------------------------------------------------------------------- + +#[inline] +pub(crate) fn read_normalized(data: &[u8], sample: SampleType, i: usize) -> f32 { + match sample { + SampleType::U8 => data[i] as f32 / 255.0, + SampleType::U16 => u16::from_ne_bytes([data[2 * i], data[2 * i + 1]]) as f32 / 65535.0, + SampleType::F16 => f16::from_bits(u16::from_ne_bytes([data[2 * i], data[2 * i + 1]])).to_f32(), + SampleType::F32 => f32::from_ne_bytes([data[4 * i], data[4 * i + 1], data[4 * i + 2], data[4 * i + 3]]), + } +} + +pub(crate) fn to_normalized(data: &[u8], sample: SampleType) -> Vec { + match sample { + SampleType::U8 => data.iter().map(|&v| v as f32 / 255.0).collect(), + SampleType::U16 => data.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes([c[0], c[1]]) as f32 / 65535.0).collect(), + SampleType::F16 => data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]])).to_f32()).collect(), + SampleType::F32 => data.as_chunks::<4>().0.iter().map(|c| f32::from_ne_bytes([c[0], c[1], c[2], c[3]])).collect(), + } +} + +#[inline] +fn clamp01(v: f32) -> f32 { + if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) } +} + +pub(crate) fn quantize(values: &[f32], sample: SampleType) -> Vec { + match sample { + SampleType::U8 => values.iter().map(|&v| (clamp01(v) * 255.0).round() as u8).collect(), + SampleType::U16 => values.iter().flat_map(|&v| ((clamp01(v) * 65535.0).round() as u16).to_ne_bytes()).collect(), + SampleType::F16 => values.iter().flat_map(|&v| f16::from_f32(v).to_bits().to_ne_bytes()).collect(), + SampleType::F32 => values.iter().flat_map(|&v| v.to_ne_bytes()).collect(), + } +} + +/// Rec. 709 luma weights applied to encoded values. +const LUMA: [f32; 3] = [0.2126, 0.7152, 0.0722]; + +fn cmyk_to_rgb(c: f32, m: f32, y: f32, k: f32) -> [f32; 3] { + [(1.0 - c) * (1.0 - k), (1.0 - m) * (1.0 - k), (1.0 - y) * (1.0 - k)] +} + +fn rgb_to_cmyk(r: f32, g: f32, b: f32) -> [f32; 4] { + let k = 1.0 - r.max(g).max(b); + if k >= 1.0 { + return [0.0, 0.0, 0.0, 1.0]; + } + let d = 1.0 - k; + [(1.0 - r - k) / d, (1.0 - g - k) / d, (1.0 - b - k) / d, k] +} + +/// Convert normalized interleaved samples between layouts. Naive +/// (non-colour-managed) formulas; colour-managed conversion belongs in +/// `photocraft-color`. +pub(crate) fn convert_layout(src: &[f32], from: ChannelLayout, to: ChannelLayout) -> Vec { + if from == to { + return src.to_vec(); + } + let fc = from.channels(); + let tc = to.channels(); + let n = src.len() / fc; + let mut out = Vec::with_capacity(n * tc); + for px in src.chunks_exact(fc) { + let alpha = if from.has_alpha() { px[fc - 1] } else { 1.0 }; + let color = &px[..from.color_channels()]; + if from.same_model(to) { + out.extend_from_slice(color); + } else { + let rgb = match from { + ChannelLayout::Gray | ChannelLayout::GrayA => [color[0]; 3], + ChannelLayout::Rgb | ChannelLayout::Rgba => [color[0], color[1], color[2]], + ChannelLayout::Cmyk | ChannelLayout::CmykA => cmyk_to_rgb(color[0], color[1], color[2], color[3]), + ChannelLayout::Lab | ChannelLayout::LabA => crate::lab::lab_to_srgb([color[0], color[1], color[2]]), + }; + match to { + ChannelLayout::Gray | ChannelLayout::GrayA => { + out.push(rgb[0] * LUMA[0] + rgb[1] * LUMA[1] + rgb[2] * LUMA[2]); + } + ChannelLayout::Rgb | ChannelLayout::Rgba => out.extend_from_slice(&rgb), + ChannelLayout::Cmyk | ChannelLayout::CmykA => out.extend_from_slice(&rgb_to_cmyk(rgb[0], rgb[1], rgb[2])), + ChannelLayout::Lab | ChannelLayout::LabA => out.extend_from_slice(&crate::lab::srgb_to_lab(rgb)), + } + } + if to.has_alpha() { + out.push(alpha); + } + } + out +} + +#[cfg(test)] +mod tests { + #[test] + fn adding_alpha_fast_path_matches_the_general_conversion() { + for sample in SampleType::ALL { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Cmyk] { + let n = 7 * 3 * layout.channels(); + let vals: Vec = (0..n).map(|i| (i as f32 * 0.137) % 1.0).collect(); + let img = super::Image::from_normalized(7, 3, layout, sample, &vals).unwrap(); + let fast = img.convert(layout.with_alpha(), sample); + let slow = super::quantize(&super::convert_layout(&img.to_normalized(), layout, layout.with_alpha()), sample); + assert_eq!(fast.data(), &slow[..], "{layout:?} {sample:?}"); + } + } + } + + use super::*; + + #[test] + fn convert_samples_matches_image_convert_and_rejects_a_wrong_length() { + for sample in SampleType::ALL { + let vals: Vec = (0..5 * 2 * 3).map(|i| (i as f32 * 0.31) % 1.0).collect(); + let img = Image::from_normalized(5, 2, ChannelLayout::Rgb, sample, &vals).unwrap(); + for (layout, to) in [(ChannelLayout::Rgba, sample), (ChannelLayout::Gray, SampleType::F32), (ChannelLayout::Rgb, SampleType::U8)] { + let want = img.convert(layout, to); + let got = convert_samples(img.data(), 5, 2, (ChannelLayout::Rgb, sample), (layout, to)).unwrap(); + assert_eq!(got, want.data(), "{sample:?} to {layout:?} {to:?}"); + } + assert!(convert_samples(img.data().get(1..).unwrap_or_default(), 5, 2, (ChannelLayout::Rgb, sample), (ChannelLayout::Rgba, sample)).is_err()); + } + } + + /// The generic f32 route `convert` used before the integer fast paths (the reference). + fn convert_via_f32(img: &Image, layout: ChannelLayout, sample: SampleType) -> Vec { + quantize(&convert_layout(&img.to_normalized(), img.layout, layout), sample) + } + + #[test] + fn int_fast_path_matches_f32_route_for_every_value() { + // Every U8 and every U16 value through every depth pair (alpha goes through the same per-sample conversion; covered by the layout test). + let all8: Vec = (0..=255).collect(); + let all16: Vec = (0..=65535).collect(); + for (src, to) in + [(SampleType::U8, SampleType::U8), (SampleType::U8, SampleType::U16), (SampleType::U16, SampleType::U8), (SampleType::U16, SampleType::U16)] + { + let img = if src == SampleType::U8 { + Image::from_u8(256, 1, ChannelLayout::Gray, all8.clone()).unwrap() + } else { + Image::from_u16(65536, 1, ChannelLayout::Gray, &all16).unwrap() + }; + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb] { + assert_eq!(img.convert(layout, to).data(), &convert_via_f32(&img, layout, to)[..], "{src:?}->{to:?} {layout:?}"); + } + } + } + + #[test] + fn int_fast_path_matches_f32_route_for_every_layout() { + let mut seed = 0x9e3779b97f4a7c15u64; + let mut next = || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed + }; + for from_s in [SampleType::U8, SampleType::U16] { + for from_l in ChannelLayout::ALL { + let n = 64 * from_l.channels(); + let mut vals = Vec::new(); + for i in 0..n { + // Edge values (0, 1, max-1, max, mid) mixed with random ones. + let r = next(); + let v = match i % 7 { + 0 => 0.0, + 1 => 1.0, + 2 => 1.0 / 255.0, + 3 => 254.0 / 255.0, + _ => (r % 65536) as f32 / 65535.0, + }; + vals.push(v); + } + let img = Image::from_normalized(64, 1, from_l, from_s, &vals).unwrap(); + for to_l in ChannelLayout::ALL { + for to_s in SampleType::ALL { + let got = img.convert(to_l, to_s); + assert_eq!(got.data(), &convert_via_f32(&img, to_l, to_s)[..], "{from_l:?}/{from_s:?} -> {to_l:?}/{to_s:?}"); + } + } + } + } + } + + #[test] + fn u8_u16_roundtrip_exact() { + let v: Vec = (0..=255).collect(); + let img = Image::from_u8(256, 1, ChannelLayout::Gray, v.clone()).unwrap(); + let back = img.convert(ChannelLayout::Gray, SampleType::U16).convert(ChannelLayout::Gray, SampleType::U8); + assert_eq!(back.data(), &v[..]); + } + + #[test] + fn u16_f32_roundtrip_exact() { + let v: Vec = (0..=65535u32).step_by(7).map(|x| x as u16).collect(); + let img = Image::from_u16(v.len() as u32, 1, ChannelLayout::Gray, &v).unwrap(); + let back = img.convert(ChannelLayout::Gray, SampleType::F32).convert(ChannelLayout::Gray, SampleType::U16); + assert_eq!(back.to_u16_samples().unwrap(), v); + } + + #[test] + fn icc_dropped_on_model_change() { + let img = Image::new(2, 2, ChannelLayout::Cmyk, SampleType::U8).unwrap().with_icc(Some(vec![1, 2, 3])); + assert!(img.convert(ChannelLayout::Rgb, SampleType::U8).icc.is_none()); + assert!(img.convert(ChannelLayout::CmykA, SampleType::U16).icc.is_some()); + } + + #[test] + fn from_raw_rejects_bad_len() { + assert!(Image::from_raw(2, 2, ChannelLayout::Rgb, SampleType::U8, vec![0; 11]).is_err()); + } + + #[test] + fn rgba8_of_gray() { + let img = Image::from_u8(1, 1, ChannelLayout::GrayA, vec![10, 20]).unwrap(); + assert_eq!(img.to_rgba8(), vec![10, 10, 10, 20]); + } + + #[test] + fn cmyk_rgb_basic() { + let img = Image::from_u8(1, 1, ChannelLayout::Cmyk, vec![0, 0, 0, 255]).unwrap(); + assert_eq!(img.to_rgba8(), vec![0, 0, 0, 255]); + let img = Image::from_u8(1, 1, ChannelLayout::Rgb, vec![255, 0, 0]).unwrap(); + let c = img.convert(ChannelLayout::Cmyk, SampleType::U8); + assert_eq!(c.data(), &[0, 255, 255, 0]); + } +} diff --git a/crates/image-codecs/src/lab.rs b/crates/image-codecs/src/lab.rs new file mode 100644 index 000000000..c53ab4c33 --- /dev/null +++ b/crates/image-codecs/src/lab.rs @@ -0,0 +1,56 @@ +//! CIE L*a*b* (D50) ⇄ encoded sRGB for [`crate::ChannelLayout::Lab`] samples, the same formulas +//! and constants as `photocraft-color` (this crate stays standalone). Samples are normalized as +//! PhotoCraft holds Lab: L* 0..100 as 0..1, a* and b* −128..127 as `(v + 128) / 255`. + +const D50: [f32; 3] = [0.964_22, 1.0, 0.825_21]; +/// Linear sRGB → D50 XYZ (Bradford-adapted, as in the ICC sRGB profile). +const SRGB_TO_XYZ_D50: [[f32; 3]; 3] = [[0.436_074, 0.385_064, 0.143_080], [0.222_504, 0.716_878, 0.060_618], [0.013_932, 0.097_104, 0.714_173]]; +/// D50 XYZ → linear sRGB. +const XYZ_D50_TO_SRGB: [[f32; 3]; 3] = [[3.133_856, -1.616_867, -0.490_615], [-0.978_768, 1.916_142, 0.033_454], [0.071_945, -0.228_991, 1.405_243]]; + +fn mul(m: &[[f32; 3]; 3], v: [f32; 3]) -> [f32; 3] { + m.map(|r| r[0] * v[0] + r[1] * v[1] + r[2] * v[2]) +} + +fn to_linear(v: f32) -> f32 { + if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } +} + +fn to_encoded(v: f32) -> f32 { + if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } +} + +/// Normalized Lab → encoded sRGB in 0..1 (out-of-gamut colours clipped). +pub(crate) fn lab_to_srgb(n: [f32; 3]) -> [f32; 3] { + let (l, a, b) = (n[0] * 100.0, n[1] * 255.0 - 128.0, n[2] * 255.0 - 128.0); + let fy = (l + 16.0) / 116.0; + let (fx, fz) = (fy + a / 500.0, fy - b / 200.0); + let finv = |t: f32| if t > 6.0 / 29.0 { t * t * t } else { 3.0 * (6.0f32 / 29.0).powi(2) * (t - 4.0 / 29.0) }; + let xyz = [finv(fx) * D50[0], finv(fy) * D50[1], finv(fz) * D50[2]]; + mul(&XYZ_D50_TO_SRGB, xyz).map(|v| to_encoded(v.clamp(0.0, 1.0))) +} + +/// Encoded sRGB → normalized Lab. +pub(crate) fn srgb_to_lab(rgb: [f32; 3]) -> [f32; 3] { + let xyz = mul(&SRGB_TO_XYZ_D50, rgb.map(|v| to_linear(v.clamp(0.0, 1.0)))); + let f = |t: f32| if t > (6.0f32 / 29.0).powi(3) { t.cbrt() } else { t / (3.0 * (6.0f32 / 29.0).powi(2)) + 4.0 / 29.0 }; + let (fx, fy, fz) = (f(xyz[0] / D50[0]), f(xyz[1] / D50[1]), f(xyz[2] / D50[2])); + let (l, a, b) = (116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz)); + [(l / 100.0).clamp(0.0, 1.0), ((a + 128.0) / 255.0).clamp(0.0, 1.0), ((b + 128.0) / 255.0).clamp(0.0, 1.0)] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn white_black_and_round_trips() { + let w = srgb_to_lab([1.0; 3]); + assert!((w[0] - 1.0).abs() < 1e-3 && (w[1] - 128.0 / 255.0).abs() < 1e-3 && (w[2] - 128.0 / 255.0).abs() < 1e-3, "{w:?}"); + assert!(srgb_to_lab([0.0; 3])[0].abs() < 1e-4); + for rgb in [[0.2, 0.5, 0.8], [0.9, 0.1, 0.3], [0.5; 3]] { + let back = lab_to_srgb(srgb_to_lab(rgb)); + assert!(rgb.iter().zip(back).all(|(a, b)| (a - b).abs() < 2e-3), "{rgb:?} → {back:?}"); + } + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs new file mode 100644 index 000000000..a5922d323 --- /dev/null +++ b/crates/image-codecs/src/lib.rs @@ -0,0 +1,421 @@ +//! `craft-image-codecs`: symmetric, depth- and metadata-preserving codecs for +//! flat raster image formats. +//! +//! * Every format we **write** we can also **read** (see +//! [`ASYMMETRIC_EXCEPTIONS`] for the documented, feature-gated exception). +//! * Bit depth (8/16-bit integer, 16/32-bit float), ICC profiles and basic +//! metadata (EXIF, XMP, DPI, text) are preserved where the format allows. +//! * [`caps`] declares per-format capabilities and [`fidelity_warnings`] +//! tells the UI exactly what an export will lose. +//! * APIs use bytes or caller-provided readers, with no ambient filesystem access, +//! and build for `wasm32-unknown-unknown`. Byte decoders enforce [`Limits`]; +//! [`exr_stream`] instead bounds metadata and one decoded compression block. + +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod cicp; +mod codecs; +mod error; +pub mod exif_color; +pub mod exr_stream; +mod fidelity; +mod format; +mod image; +mod lab; +mod options; +pub mod orientation; +pub mod resolution; +pub mod source_settings; +pub mod web; +pub use crate::cicp::Cicp; +pub use crate::codecs::exr_cryptomatte::{cryptomatte_id, cryptomatte_key, cryptomatte_preview_color}; +pub use crate::codecs::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; +pub use crate::codecs::png::{ + PngDecodeRows, PngExtras, PngHeader, PngPalette, PngRowSink, PngRows, SharedBytes, encode_indexed as encode_png_indexed, palette as png_palette, + stereo as png_stereo, +}; +pub use crate::codecs::tiff::{ + PhotoshopTags as TiffPhotoshopTags, TiffBands, photoshop_tags as tiff_photoshop_tags, writes_little_endian as tiff_writes_little_endian, +}; +pub use crate::codecs::tiff_ifd::{TiffInfo, TiffPage, TiffPageKind}; +pub use crate::error::CodecError; +pub use crate::exif_color::{EXIF_COLOR_SPACE_SRGB, exif_color_space}; +pub use crate::fidelity::{FidelityWarning, fidelity_warnings, fidelity_warnings_with}; +pub use crate::format::{ASYMMETRIC_EXCEPTIONS, Format, FormatCaps, caps, detect, from_extension}; +pub use crate::image::{ + ChannelLayout, CryptomatteBuffer, CryptomatteLayer, DecodeWarning, DeepChannel, DeepImage, ExrChannelInfo, ExrPartInfo, ExtraSample, Image, + MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, +}; +pub use crate::options::{ + DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, + TiffCompression, +}; +pub use crate::orientation::{OrientSteps, exif_orientation, upright_exif, upright_xmp}; +pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; +pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; +pub use half::f16; + +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; + +/// Detect the format and decode with default [`Limits`]. +pub fn decode(bytes: &[u8]) -> Result { + decode_with(bytes, &DecodeOptions::default()) +} + +/// Detect the format and decode. +pub fn decode_with(bytes: &[u8], opts: &DecodeOptions) -> Result { + let format = detect(bytes).ok_or(CodecError::UnknownFormat)?; + decode_as_with(format, bytes, opts) +} + +/// Decode as a specific format with default [`Limits`]. +pub fn decode_as(format: Format, bytes: &[u8]) -> Result { + decode_as_with(format, bytes, &DecodeOptions::default()) +} + +/// Decode as a specific format. +pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Result { + // HEIF without the `heif` feature: its decoder stub says what is missing from this build. + if !caps(format).read && !matches!(format, Format::Heif | Format::Jxl) { + return Err(CodecError::unsupported(format, "decoding is not available for this format")); + } + let l = &opts.limits; + let img = match format { + Format::Png => png::decode(bytes, l), + Format::Jpeg => jpeg::decode(bytes, l), + Format::Tiff => tiff::decode(bytes, l), + Format::WebP => webp::decode(bytes, l), + Format::Pnm => pnm::decode(bytes, l), + Format::Pcx => pcx::decode(bytes, l), + Format::Xpm => xpm::decode(bytes, l), + Format::Dds => dds::decode(bytes, l), + Format::Fits => fits::decode(bytes, l), + Format::Dpx => dpx::decode(bytes, l), + Format::Cineon => dpx::decode_cineon(bytes, l), + Format::Dicom => dicom::decode(bytes, l), + Format::Iff => iff::decode(bytes, l), + Format::OpenExr => exr::decode(bytes, l), + Format::Heif => heif::decode(bytes, l, opts.keep_orientation), + Format::Jxl => Err(CodecError::unsupported(format, "JPEG XL reading is not yet available; export is supported")), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l), + }?; + finish_format(format, bytes, img, opts) +} + +/// What [`decode_as_with`] does with a PNG decoded in steps by [`PngDecodeRows`] (`img` is its +/// output): the resolution from the metadata, the upright orientation and the final limit check, +/// so the result equals [`decode_as_with`]'s on the same file. +pub fn finish_png_decode(img: Image, opts: &DecodeOptions) -> Result { + match begin_png_finish(img, opts)? { + PngFinish::Done(img) => Ok(*img), + PngFinish::Orient(mut steps) => loop { + if let Some(img) = steps.step(&mut || true)? { + return Ok(img); + } + }, + } +} + +/// Where [`begin_png_finish`] leaves a decoded PNG. +pub enum PngFinish { + /// Finished: it was already upright (or the options keep the orientation). + Done(Box), + /// To be turned upright, a band of rows at a time, by driving these steps. + Orient(Box), +} + +/// [`finish_png_decode`] with the turning upright (orientations 2-8) left to the caller, in +/// bands it can pace: the resolution from the metadata and the limit checks are made here, and the +/// result of the steps equals what [`finish_png_decode`] returns. +pub fn begin_png_finish(mut img: Image, opts: &DecodeOptions) -> Result { + if img.meta.dpi.is_none() { + img.meta.dpi = resolution::from_metadata(img.meta.exif.as_deref(), img.meta.xmp.as_deref()); + } + let o = img.meta.exif.as_deref().map_or(1, exif_orientation); + let l = &opts.limits; + // As `finish_decode` does: the final guard, then (turning 5-8 swaps the sides) the upright size. + l.check(img.width(), img.height(), img.layout(), img.sample_type())?; + if opts.keep_orientation || !(2..=8).contains(&o) { + return Ok(PngFinish::Done(Box::new(img))); + } + if (5..=8).contains(&o) { + l.check(img.height(), img.width(), img.layout(), img.sample_type())?; + } + Ok(PngFinish::Orient(Box::new(OrientSteps::new(img, o)?))) +} + +/// The steps after a decoder of `format` has produced `img` from `bytes`. +fn finish_format(format: Format, bytes: &[u8], mut img: Image, opts: &DecodeOptions) -> Result { + // Without a resolution of the format's own (PNG pHYs, TIFF tags, JPEG's metadata), the + // XMP or EXIF one counts, like Photoshop: a WebP, HEIF or PNG from a camera or phone + // opens at its recorded resolution, not at the 72 ppi default. + if img.meta.dpi.is_none() && format != Format::Tiff { + img.meta.dpi = resolution::from_metadata(img.meta.exif.as_deref(), img.meta.xmp.as_deref()); + } + // Turn the pixels upright, like Photoshop: a TIFF records it in the decoded page's own + // directory, the others in their EXIF block. The metadata is rewritten to Orientation = 1 + // on the way. HEIF keeps it in its container, and its decoder has already applied it. + let o = match format { + Format::Tiff => tiff::orientation(bytes, None), + Format::Heif => 1, + Format::Dpx => dpx::orientation(bytes), + Format::Cineon => dpx::cineon_orientation(bytes), + _ => img.meta.exif.as_deref().map_or(1, exif_orientation), + }; + finish_decode(img, o, opts) +} + +/// Decodes a PNG a group of rows at a time, for a caller that builds its own pixel storage and +/// never needs the whole decoded image. `begin` gets the [`PngHeader`] once and returns whether to go on, then `rows(y0, data)` +/// gets whole rows `y0..` (`group` of them, fewer in the last call; native-endian) in final order, holding exactly the +/// samples [`decode`] would return. The result is the metadata, complete once the file is read. +/// +/// `Ok(None)`, with no row delivered, means the rows can't be had in final order (an +/// interlaced or animated PNG) or `begin` returned false: use [`decode_as_with`]. Damaged files fail as [`decode_as_with`] +/// fails them, possibly after some rows. The limits are checked before the first row; the +/// orientation is not applied, so [`PngExtras::orientation`] other than 1 is for the caller to +/// handle (or to fall back to [`decode_as_with`], which turns the image upright). +pub fn decode_png_rows>( + bytes: &[u8], + opts: &DecodeOptions, + group: u32, + begin: &mut dyn FnMut(&PngHeader) -> Result, + rows: &mut PngRowSink<'_, E>, +) -> Result, E> { + png::decode_rows(bytes, &opts.limits, group, begin, rows) +} + +/// The upright pixel size [`decode`] would return, read from the file's header without decoding +/// pixels, for WebP, OpenEXR (flat) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same +/// header reads and size checks as the decoder (default [`Limits`]) and swaps the size for an +/// EXIF orientation of 5–8, so wherever [`decode`] succeeds the two agree. Other formats (and +/// deep EXR or AVIF) return [`CodecError::Unsupported`]: decode those to learn their size. +pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let format = detect(bytes).ok_or(CodecError::UnknownFormat)?; + if !caps(format).read { + return Err(CodecError::unsupported(format, "decoding is not available for this format")); + } + let l = &Limits::default(); + let (w, h, exif) = match format { + Format::WebP => webp::probe(bytes, l)?, + Format::OpenExr => exr::probe(bytes, l).map(|(w, h)| (w, h, None))?, + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::probe(format, bytes, l)?, + _ => return Err(CodecError::unsupported(format, "no header-only size probe")), + }; + let o = exif.as_deref().map_or(1, exif_orientation); + Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) }) +} + +/// The colour table of a GIF's first frame (its Local Color Table, else the Global one) and the +/// entry its Graphic Control Extension makes transparent, read without decoding pixels. +pub fn gif_palette(bytes: &[u8]) -> Option<(Vec<[u8; 3]>, Option)> { + crate::codecs::gif_palette::palette(bytes) +} + +/// Returns the embedded colour table for an indexed BMP, if it has one. +pub fn bmp_palette(bytes: &[u8]) -> Result>, CodecError> { + via_image::bmp_palette(bytes) +} + +/// Encodes `indices` (row-major, one byte per pixel) as an 8-bit palette BMP with the colour +/// table `palette` (1 to 256 entries). Used for Indexed Color documents; BMP has no transparency. +pub fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palette: &[[u8; 3]]) -> Result, CodecError> { + via_image::encode_bmp_indexed(width, height, indices, palette) +} + +/// Lists the pages (image directories) of a TIFF or BigTIFF file without decoding pixels: the +/// main IFD chain and its SubIFDs, each with its size, kind (page, reduced-resolution copy or +/// mask) and storage. [`TiffInfo::default_page`] is the page [`decode`] opens. +pub fn tiff_info(bytes: &[u8]) -> Result { + crate::codecs::tiff_ifd::tiff_info(bytes) +} + +/// Decodes page `page` (an index into [`tiff_info`]'s `pages`) of a TIFF or BigTIFF file, +/// applying that page's orientation unless `opts.keep_orientation`. +pub fn decode_tiff_page(bytes: &[u8], page: usize, opts: &DecodeOptions) -> Result { + let img = tiff::decode_page(bytes, Some(page), &opts.limits)?; + finish_decode(img, tiff::orientation(bytes, Some(page)), opts) +} + +/// Decodes every frame of a GIF (a still GIF yields one), each a full RGBA8 picture of the +/// logical screen with its delay. [`decode`] keeps the first frame only. See +/// [`MAX_GIF_FRAMES`] and `opts.limits` for what is refused. +pub fn decode_gif_frames(bytes: &[u8], opts: &DecodeOptions) -> Result, CodecError> { + crate::codecs::gif_frames::decode(bytes, opts) +} + +/// Decodes every frame of a WebP (a still one yields one frame with a 0 delay, as [`decode`] +/// reads it), each a full picture of the canvas with its duration and the file's ICC profile. +/// [`decode`] keeps the first frame only. See [`MAX_GIF_FRAMES`] and `opts.limits` for what is +/// refused. +pub fn decode_webp_frames(bytes: &[u8], opts: &DecodeOptions) -> Result, CodecError> { + match crate::codecs::webp::decode_frames(bytes, &opts.limits)? { + Some(frames) => Ok(frames), + None => Ok(vec![AnimationFrame { image: decode_as_with(Format::WebP, bytes, opts)?, delay_ms: 0 }]), + } +} + +/// Whether `bytes` is an animated WebP of more than one frame (which [`decode`] opens as its +/// first frame, with a warning, and [`decode_webp_frames`] reads whole). +pub fn webp_is_animated(bytes: &[u8]) -> bool { + crate::codecs::webp::is_animated(bytes) +} + +/// Inspect declared source data which a flat decode and save cannot retain: additional frames, +/// TIFF pages, masks or unretained channels, ICO entries, DDS mip levels, slices, faces or arrays, +/// DPX elements and FITS HDU payloads. +/// Pixels are not decoded; derived TIFF thumbnails do not require preservation here. +/// Other formats return false; this does not establish preservation of other format-specific data. +pub fn has_unretained_source_data(bytes: &[u8]) -> Result { + match detect(bytes) { + Some(Format::Gif) => via_image::gif_has_more_frames(bytes).ok_or_else(|| CodecError::malformed(Format::Gif, "cannot inspect every frame")), + Some(Format::Png) => png::has_additional_images(bytes), + Some(Format::WebP) => Ok(webp_is_animated(bytes)), + Some(Format::Tiff) => { + let info = tiff_info(bytes)?; + if !info.complete { + return Err(CodecError::malformed(Format::Tiff, "cannot inspect every source image directory")); + } + let retained = info.default_page(); + if info.pages.iter().enumerate().any(|(index, page)| Some(index) != retained && page.kind != TiffPageKind::ReducedResolution) { + return Ok(true); + } + retained.and_then(|index| info.pages.get(index)).map_or(Ok(false), |page| tiff::has_unretained_samples(bytes, page.ifd_offset)) + } + Some(Format::Ico) => { + let count = bytes.get(4..6).and_then(|b| <[u8; 2]>::try_from(b).ok()).ok_or_else(|| CodecError::malformed(Format::Ico, "truncated image count"))?; + Ok(u16::from_le_bytes(count) > 1) + } + Some(Format::Dds) => dds::has_additional_images(bytes), + Some(Format::Dpx) => dpx::has_additional_images(bytes), + Some(Format::Fits) => fits::has_additional_images(bytes), + _ => Ok(false), + } +} + +/// The Orientation (1–8) of the page [`decode`] opens from a TIFF or BigTIFF file; 1 when it is +/// absent, malformed or out of range. +pub fn tiff_orientation(bytes: &[u8]) -> u16 { + tiff::orientation(bytes, None) +} + +/// The Orientation (1–8) of page `page` (an index into [`tiff_info`]'s `pages`); 1 when it is +/// absent, malformed or out of range, or the page does not exist. +pub fn tiff_page_orientation(bytes: &[u8], page: usize) -> u16 { + tiff::orientation(bytes, Some(page)) +} + +/// The EXIF block (`Metadata::exif`) of a camera raw built on TIFF (NEF, ARW, CR2, DNG, +/// Panasonic RW2), for the image developed from it: IFD0's descriptive tags (make, model, +/// date…) and the Exif and GPS directories, as a TIFF opens with. Orientation is not part of +/// it, nor the Exif directory's pixel dimensions (the sensor's or the preview's size). When those +/// were all its Exif directory held, the block is IFD0's tags alone, so the camera's make and +/// model are still kept. `None` when the file is not built on a classic TIFF stream or has none +/// of these tags. +pub fn raw_exif(bytes: &[u8]) -> Option> { + let dirs = crate::codecs::tiff_exif::read_raw(bytes).filter(|d| d.has_directories() || !d.ifd0.is_empty())?; + Some(crate::codecs::tiff_exif::block(&dirs)) +} + +/// The limits check and the orientation, shared by every decoder. +fn finish_decode(img: Image, o: u16, opts: &DecodeOptions) -> Result { + let l = &opts.limits; + // Final guard for decoders whose header we could not pre-inspect. + l.check(img.width(), img.height(), img.layout(), img.sample_type())?; + if opts.keep_orientation { + return Ok(img); + } + // Orientations 5–8 swap width and height: with asymmetric limits a stored landscape + // that fit can turn into a portrait that does not, so check the upright size too. + if (5..=8).contains(&o) { + l.check(img.height(), img.width(), img.layout(), img.sample_type())?; + } + img.oriented(o) +} + +/// Lists the parts of an OpenEXR file without decoding pixels: header facts per part +/// (name, view, size, channels, deep/tiled), what a multi-part chooser — a Maya/Arnold +/// render writes one part per AOV — offers. The indices are what [`decode_exr_part`] +/// takes; [`decode_as_with`] opens the part with the highest [`ExrPartInfo::color_rank`]. +pub fn exr_info(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + exr::info(bytes, limits) +} + +/// Decodes part `part` (an index into [`exr_info`]'s list) of an OpenEXR file. +/// Deep parts stay with [`decode_deep_exr`]; a file that has any is refused here. +pub fn decode_exr_part(bytes: &[u8], part: usize, opts: &DecodeOptions) -> Result { + let img = exr::decode_part(bytes, part, &opts.limits)?; + finish_decode(img, 1, opts) +} + +/// Lists every Cryptomatte layer of an EXR file (specification 1.2: object/material IDs +/// with coverage, as written by Arnold, V-Ray, Redshift, Mantra/Karma and Cycles) from the +/// headers alone: name, metadata key, channels and the parsed embedded manifest. The +/// samples come from [`decode_cryptomatte`]; names hash to IDs with [`cryptomatte_id`]. +pub fn cryptomatte_layers(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + codecs::exr_cryptomatte::layers(bytes, limits) +} + +/// Decodes the samples of the Cryptomatte layer called `layer_name` (a +/// [`CryptomatteLayer::name`]): per pixel its (ID, coverage) pairs, coverage above zero, +/// sorted by descending coverage. +pub fn decode_cryptomatte(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result { + codecs::exr_cryptomatte::decode(bytes, layer_name, limits) +} + +/// Decodes the named channels of one part (exact names as the file stores them, e.g. +/// `diffuse.R`) into an image: R/G/B (plus A when named) give Rgba, a lone Y gives Gray. +/// This renders a channel group without the part's other channels in the way. +pub fn decode_exr_channels(bytes: &[u8], part: usize, names: &[&str], opts: &DecodeOptions) -> Result { + let img = exr::decode_channels(bytes, part, names, &opts.limits)?; + finish_decode(img, 1, opts) +} + +/// Deep OpenEXR samples (`deepscanline`/`deeptile`): the structured per-pixel sample lists +/// (colour, alpha and Z per sample), for callers that composite deep data themselves — +/// [`decode_as_with`] instead flattens a deep file into a normal [`Image`] with a +/// [`DecodeWarning::DeepFlattened`]. `Err(Unsupported)` when the file has no deep part. +pub fn decode_deep_exr(bytes: &[u8], limits: &Limits) -> Result { + codecs::exr::check_previews(bytes)?; + let mut cur = std::io::Cursor::new(bytes); + let meta = ::exr::meta::MetaData::read_from_buffered(&mut cur, false).map_err(|e| CodecError::malformed(Format::OpenExr, e))?; + codecs::deep_exr::decode_deep(&meta, bytes, limits) +} + +/// Encode `image` as `format`. Conversions follow the same plan that +/// [`fidelity_warnings_with`] reports; fatal conditions (unsupported +/// format, oversize image) return an error. +pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result, CodecError> { + if !caps(format).write { + return Err(CodecError::unsupported(format, "encoding is not available in this build")); + } + if image.width() == 0 || image.height() == 0 { + return Err(CodecError::InvalidImage("cannot encode an empty image".into())); + } + if let Some((mw, mh)) = format.max_dimensions() + && (image.width() > mw || image.height() > mh) + { + return Err(CodecError::encode(format, format!("dimensions exceed {mw}x{mh}"))); + } + let plan = fidelity::plan(image, format, opts); + match format { + Format::Png => png::encode(image, plan, opts), + Format::Jpeg => jpeg::encode(image, plan, opts), + Format::Tiff => tiff::encode(image, plan, opts), + Format::WebP => webp::encode(image, plan, opts), + Format::Pnm => pnm::encode(image, plan, opts), + Format::Pcx => Err(CodecError::unsupported(format, "PCX is import-only; save as PNG instead")), + Format::Xpm => Err(CodecError::unsupported(format, "XPM is import-only; save as PNG instead")), + Format::Dds => dds::encode(image, plan, opts), + Format::Fits => fits::encode(image, plan, opts), + Format::Dpx => dpx::encode(image, plan, opts), + Format::Cineon => dpx::encode_cineon(image, plan), + Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), + Format::Iff => iff::encode(image, plan), + Format::OpenExr => exr::encode(image, plan, opts), + Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), + Format::Jxl => jxl::encode(image, plan), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::encode(format, image, plan, opts), + } +} diff --git a/crates/image-codecs/src/options.rs b/crates/image-codecs/src/options.rs new file mode 100644 index 000000000..9674b4edf --- /dev/null +++ b/crates/image-codecs/src/options.rs @@ -0,0 +1,364 @@ +//! Decode limits and encode options. + +use crate::error::CodecError; +use crate::image::{ChannelLayout, SampleType}; + +/// Decompression-bomb guards. Dimension checks precede pixel allocation; +/// metadata decoding checks its budget before growing decoded output. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Limits { + pub max_width: u32, + pub max_height: u32, + /// Maximum `width * height`. + pub max_pixels: u64, + /// Maximum bytes for the decoded pixel buffer (and passed on as the + /// allocation budget to underlying decoders). + /// PNG also applies this to the aggregate decoded text/XMP UTF-8 bytes, + /// including keywords, separately from pixels. Animated WebP charges + /// every retained frame's full pixel copy plus its own copy of the + /// file's ICC profile against this budget. This is not a total RSS cap. + pub max_alloc: u64, +} + +impl Default for Limits { + fn default() -> Self { + // 2^30 pixels (e.g. 32768²) and, on 64-bit targets, 8 GiB: enough for the large + // documents Photoshop users open (a 20000² RGBA 16-bit image is 3.2 GB). On 32-bit + // targets (wasm32 has a 4 GiB address space shared with the document, history and + // the decoder's own buffers) 1 GiB is the most one buffer can safely ask for. + let max_alloc = if cfg!(target_pointer_width = "64") { 8 << 30 } else { 1 << 30 }; + Limits { max_width: 1 << 18, max_height: 1 << 18, max_pixels: 1 << 30, max_alloc } + } +} + +impl Limits { + /// For a decoder that writes a band of rows at a time into the document's tiles instead of + /// one buffer (TIFF, #8169): the largest document size, 300,000 px a side, on 64-bit + /// targets, with `max_alloc` bounding each group of bands. 32-bit targets keep the + /// [`Default`] limits: wasm32's 4 GiB address space can't hold a larger document anyway. + pub fn banded() -> Self { + let l = Limits::default(); + if cfg!(target_pointer_width = "64") { Limits { max_width: 300_000, max_height: 300_000, max_pixels: 300_000 * 300_000, ..l } } else { l } + } + + /// Effectively unlimited (still bounded by `usize` overflow checks). + pub fn none() -> Self { + Limits { max_width: u32::MAX, max_height: u32::MAX, max_pixels: u64::MAX, max_alloc: u64::MAX } + } + + /// Validate declared dimensions for an output buffer of the given layout + /// and sample type. + pub fn check(&self, width: u32, height: u32, layout: ChannelLayout, sample: SampleType) -> Result<(), CodecError> { + self.check_bytes(width, height, (layout.channels() * sample.bytes()) as u64) + } + + pub(crate) fn check_bytes(&self, width: u32, height: u32, bytes_per_pixel: u64) -> Result<(), CodecError> { + self.check_size(width, height)?; + let bytes = (width as u64 * height as u64).saturating_mul(bytes_per_pixel); + if bytes > self.max_alloc { + return Err(CodecError::LimitExceeded(format!("{bytes} bytes exceed max_alloc {}", self.max_alloc))); + } + if usize::try_from(bytes).is_err() { + return Err(CodecError::LimitExceeded(format!("{bytes} bytes do not fit in memory"))); + } + Ok(()) + } + + /// The dimension and pixel-count checks of [`Limits::check`], for a decoder that never holds + /// the whole image in one buffer (`max_alloc` then bounds each piece it decodes). + pub(crate) fn check_size(&self, width: u32, height: u32) -> Result<(), CodecError> { + if width == 0 || height == 0 { + return Err(CodecError::InvalidImage(format!("zero-sized image {width}x{height}"))); + } + if width > self.max_width || height > self.max_height { + return Err(CodecError::LimitExceeded(format!("dimensions {width}x{height} exceed max {}x{}", self.max_width, self.max_height))); + } + let pixels = width as u64 * height as u64; + if pixels > self.max_pixels { + return Err(CodecError::LimitExceeded(format!("{pixels} pixels exceed max {}", self.max_pixels))); + } + Ok(()) + } + + /// Decompression-bomb guard for formats that compress: refuse a file too small to hold + /// `declared_bytes` of (stored, pre-expansion) image data, given that the compression + /// never beats `max_ratio`:1. Called before the output buffer is allocated, so a + /// header-only hostile file cannot make the decoder reserve gigabytes. A fixed 1 MiB of + /// slack keeps tiny images and headers with large metadata unaffected. + pub(crate) fn check_plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> Result<(), CodecError> { + let capacity = (file_len as u64).saturating_mul(max_ratio).saturating_add(1 << 20); + if declared_bytes > capacity { + return Err(CodecError::LimitExceeded(format!("a {file_len} byte file cannot hold {declared_bytes} bytes of image data"))); + } + Ok(()) + } + + pub(crate) fn alloc_usize(&self) -> usize { + usize::try_from(self.max_alloc).unwrap_or(usize::MAX) + } +} + +/// Options for [`crate::decode_with`]. +#[derive(Debug, Clone, Default)] +pub struct DecodeOptions { + pub limits: Limits, + /// Keep the pixels as stored instead of applying the EXIF / TIFF + /// orientation (by default they are turned upright and the metadata's + /// orientation is rewritten to 1). + pub keep_orientation: bool, +} + +/// PNG zlib effort. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum PngCompression { + None, + Fast, + #[default] + Default, + Best, +} + +/// How a PNG's `sTER` chunk pairs its two views (PNG Extensions 1.4.0, §3.6). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PngStereo { + /// Side by side with the right-eye view first (cross-eyed viewing, mode 0). + CrossFuse, + /// Side by side with the left-eye view first (parallel viewing, mode 1). + DivergingFuse, +} + +impl PngStereo { + /// The view for an `sTER` mode byte; reserved and unknown values are `None`. + pub fn from_mode(mode: u8) -> Option { + match mode { + 0 => Some(Self::CrossFuse), + 1 => Some(Self::DivergingFuse), + _ => None, + } + } + + /// The `sTER` mode byte. + pub fn mode(self) -> u8 { + match self { + Self::CrossFuse => 0, + Self::DivergingFuse => 1, + } + } +} + +/// TIFF strip compression. The default is none, as in Photoshop: uncompressed strips are the +/// only kind every TIFF reader opens (Deflate and the predictor are not baseline TIFF, #4484). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum TiffCompression { + #[default] + None, + Lzw, + Deflate, + PackBits, +} + +/// JPEG scan layout: Photoshop's JPEG Options › Format Options. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum JpegFormat { + /// Baseline ("Standard"): one sequential scan with the standard Huffman tables. + #[default] + Baseline, + /// Baseline Optimized: one sequential scan with Huffman tables fitted to the image. Only at + /// 4:4:4 for now: a 4:2:0 (subsampled RGB) baseline scan keeps the standard tables, because + /// fitted tables make jpeg-encoder write one scan per component, which zune-jpeg 0.5.15 (our + /// decoder) reads back wrongly when chroma is subsampled (see `web::encode_jpeg_rgb8`). + Optimized, + /// Progressive: the image refines over several scans as it loads. + Progressive, +} + +impl JpegFormat { + /// `"baseline"`, `"optimized"` or `"progressive"`, as parameters and scripts spell it. + pub fn name(self) -> &'static str { + match self { + JpegFormat::Baseline => "baseline", + JpegFormat::Optimized => "optimized", + JpegFormat::Progressive => "progressive", + } + } + + /// The inverse of [`Self::name`]. + pub fn parse(s: &str) -> Option { + [JpegFormat::Baseline, JpegFormat::Optimized, JpegFormat::Progressive].into_iter().find(|f| f.name() == s) + } +} + +/// OpenEXR compression (lossless methods only). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum ExrCompression { + None, + Rle, + Zip1, + #[default] + Zip16, + Piz, +} + +/// Bits per sample of a DPX file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum DpxBits { + Bits8, + Bits10, + Bits12, + Bits16, +} + +impl DpxBits { + /// `8`, `10`, `12` or `16` as [`DpxBits`]; any other number is `None`. + pub fn from_bits(bits: u64) -> Option { + match bits { + 8 => Some(DpxBits::Bits8), + 10 => Some(DpxBits::Bits10), + 12 => Some(DpxBits::Bits12), + 16 => Some(DpxBits::Bits16), + _ => None, + } + } + + /// The bits per sample. + pub fn bits(self) -> u8 { + match self { + DpxBits::Bits8 => 8, + DpxBits::Bits10 => 10, + DpxBits::Bits12 => 12, + DpxBits::Bits16 => 16, + } + } +} + +/// How a DDS texture stores its pixels. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum DdsCompression { + /// Uncompressed, 8 bits per channel: BGRA (BGRX without alpha), or luminance with or + /// without alpha for grayscale. Lossless. + #[default] + None, + /// BC1 (DXT1): 4 bits per pixel. Alpha is on or off: texels under half opacity become + /// transparent black. + Bc1, + /// BC2 (DXT3): 8 bits per pixel, with 4 bits of explicit alpha per texel. + Bc2, + /// BC3 (DXT5): 8 bits per pixel, with smooth interpolated alpha. + Bc3, +} + +impl DdsCompression { + pub const ALL: [DdsCompression; 4] = [DdsCompression::None, DdsCompression::Bc1, DdsCompression::Bc2, DdsCompression::Bc3]; + + /// The name scripts and the CLI use: `none`, `bc1`, `bc2` or `bc3`. + pub fn name(self) -> &'static str { + match self { + DdsCompression::None => "none", + DdsCompression::Bc1 => "bc1", + DdsCompression::Bc2 => "bc2", + DdsCompression::Bc3 => "bc3", + } + } + + /// [`Self::name`] back to a compression, also taking the DXT names (`dxt1`, `dxt3`, + /// `dxt5`), in any case. Anything else is `None`. + pub fn from_name(name: &str) -> Option { + match name.to_ascii_lowercase().as_str() { + "none" => Some(DdsCompression::None), + "bc1" | "dxt1" => Some(DdsCompression::Bc1), + "bc2" | "dxt3" => Some(DdsCompression::Bc2), + "bc3" | "dxt5" => Some(DdsCompression::Bc3), + _ => None, + } + } + + /// A block compression (lossy), not uncompressed pixels. + pub fn is_block(self) -> bool { + self != DdsCompression::None + } +} + +/// What a DDS file is written with. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct DdsOptions { + pub compression: DdsCompression, + /// Write the full mip chain down to 1×1, each level a box filter of the one above. + pub mipmaps: bool, +} + +/// The longer side, in pixels, at which a TIFF image pyramid stops: the last level written is +/// the first one no longer than this ([`EncodeOptions::tiff_pyramid`]). +pub const TIFF_PYRAMID_MIN: u32 = 64; + +/// Options for [`crate::encode`]. +#[derive(Debug, Clone)] +pub struct EncodeOptions { + /// JPEG quality 1..=100 (also AVIF quality when enabled). + pub jpeg_quality: u8, + /// JPEG 4:2:0 chroma subsampling (`false` = 4:4:4). + pub jpeg_chroma_subsampling: bool, + /// JPEG scan layout (baseline, baseline optimized or progressive). + pub jpeg_format: JpegFormat, + pub png_compression: PngCompression, + /// Write Adam7-interlaced PNG. + pub png_interlaced: bool, + /// Write an `sTER` chunk marking the image as a stereo pair (PNG Extensions 1.4.0, §3.6), + /// so a stereo PNG keeps its mark when saved again (#6063). + pub png_stereo: Option, + /// WebP: lossless (VP8L, via `image-webp`) or lossy (our VP8 encoder, see + /// `codecs::vp8`) with [`Self::webp_quality`]. + pub webp_lossless: bool, + /// Lossy WebP quality 0..=100 (the source application's scale; 75–85 is typical for photos). + pub webp_quality: u8, + pub tiff_compression: TiffCompression, + /// Always write TIFF as BigTIFF (8-byte offsets). Without it a TIFF is written as BigTIFF + /// only when it could pass the 4 GiB a classic TIFF can address. + pub tiff_bigtiff: bool, + /// TIFF: after the full image, write Photoshop's "Save Image Pyramid" levels: copies at half, + /// a quarter, … of the size, each marked reduced-resolution (`NewSubfileType` 1), down to + /// [`crate::TIFF_PYRAMID_MIN`] px on the longer side. Readers that don't know pyramids open + /// the full image, which comes first. + pub tiff_pyramid: bool, + pub exr_compression: ExrCompression, + /// DPX bits per sample. `None` follows the image: 8 for 8-bit, 16 otherwise. + pub dpx_bits: Option, + /// DDS compression and mip chain. + pub dds: DdsOptions, + /// Embed the ICC profile when the format supports it. + pub embed_icc: bool, + /// Embed EXIF/XMP/DPI/text when the format supports it. + pub embed_metadata: bool, +} + +impl Default for EncodeOptions { + fn default() -> Self { + EncodeOptions { + jpeg_quality: 90, + jpeg_chroma_subsampling: true, + jpeg_format: JpegFormat::Baseline, + png_compression: PngCompression::Default, + png_interlaced: false, + png_stereo: None, + webp_lossless: true, + webp_quality: 80, + tiff_compression: TiffCompression::None, + tiff_bigtiff: false, + tiff_pyramid: false, + exr_compression: ExrCompression::Zip16, + dpx_bits: None, + dds: DdsOptions::default(), + embed_icc: true, + embed_metadata: true, + } + } +} + +/// A zero-filled buffer of `len` elements, allocated fallibly: an allocation failure becomes +/// [`CodecError::LimitExceeded`] instead of aborting the process (which `catch_unwind` cannot +/// stop). +pub(crate) fn alloc_zeroed(len: usize) -> Result, CodecError> { + let mut v = Vec::new(); + v.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} elements for the image")))?; + v.resize(len, T::default()); + Ok(v) +} diff --git a/crates/image-codecs/src/orientation.rs b/crates/image-codecs/src/orientation.rs new file mode 100644 index 000000000..5ad8be252 --- /dev/null +++ b/crates/image-codecs/src/orientation.rs @@ -0,0 +1,476 @@ +//! EXIF / TIFF orientation (tag 274): reading it, applying it to the pixels, +//! and rewriting it to 1 ("top-left", upright) once the pixels are upright. +//! +//! Decoders apply the orientation by default (like Photoshop and every photo +//! viewer), so a document is always edited upright. Encoders then write +//! Orientation = 1, so a viewer never rotates the already-upright pixels a +//! second time. +//! +//! Orientation values (TIFF 6.0, EXIF 2.3): 1 = upright, 2 = mirrored, 3 = +//! rotated 180°, 4 = flipped vertically, 5 = transposed, 6 = needs a 90° +//! clockwise turn, 7 = transverse, 8 = needs a 90° counter-clockwise turn. +//! Anything malformed, truncated or out of range reads as 1: an IFD0 that +//! starts inside the 8-byte header or is cut off before its next-IFD pointer, +//! or an Orientation entry that is not exactly one SHORT or LONG (the spec says +//! SHORT, but some cameras and tools write LONG). When IFD0 holds +//! several Orientation entries the first one wins (like libexif). + +use std::borrow::Cow; + +use crate::error::CodecError; +use crate::image::Image; + +/// The TIFF/EXIF Orientation tag. +const TAG_ORIENTATION: u16 = 274; +/// Field types accepted for the Orientation tag: SHORT (the spec) and LONG +/// (written by some cameras and tools). +const TYPE_SHORT: u16 = 3; +const TYPE_LONG: u16 = 4; +/// Bytes per IFD entry. +const ENTRY: usize = 12; +/// The TIFF header is 8 bytes, so no IFD can start before it. +const HEADER: usize = 8; +/// BigTIFF uses a 16-byte header, 20-byte entries, and 8-byte offsets. +const BIG_ENTRY: usize = 20; +const BIG_HEADER: usize = 16; + +/// Byte order of a TIFF structure. +#[derive(Clone, Copy)] +pub(crate) enum Order { + Little, + Big, +} + +impl Order { + pub(crate) fn u16(self, b: &[u8], at: usize) -> Option { + let s: [u8; 2] = b.get(at..at.checked_add(2)?)?.try_into().ok()?; + Some(match self { + Order::Little => u16::from_le_bytes(s), + Order::Big => u16::from_be_bytes(s), + }) + } + + pub(crate) fn u32(self, b: &[u8], at: usize) -> Option { + let s: [u8; 4] = b.get(at..at.checked_add(4)?)?.try_into().ok()?; + Some(match self { + Order::Little => u32::from_le_bytes(s), + Order::Big => u32::from_be_bytes(s), + }) + } + + pub(crate) fn u64(self, b: &[u8], at: usize) -> Option { + let s: [u8; 8] = b.get(at..at.checked_add(8)?)?.try_into().ok()?; + Some(match self { + Order::Little => u64::from_le_bytes(s), + Order::Big => u64::from_be_bytes(s), + }) + } +} + +/// The TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped). +pub(crate) fn tiff_body(b: &[u8]) -> &[u8] { + b.strip_prefix(b"Exif\0\0").unwrap_or(b) +} + +/// IFD0 of a TIFF structure: byte order, entry layout, and entry offsets. +/// +/// The full table and next-IFD pointer must be present, and IFD0 must start +/// after the corresponding TIFF or BigTIFF header. +pub(crate) fn ifd0_entries(b: &[u8]) -> Option<(Order, usize, usize, usize, bool)> { + let (order, big_tiff) = match b.get(0..4)? { + [b'I', b'I', 42, 0] => (Order::Little, false), + [b'M', b'M', 0, 42] => (Order::Big, false), + [b'I', b'I', 43, 0] => (Order::Little, true), + [b'M', b'M', 0, 43] => (Order::Big, true), + _ => return None, + }; + let (ifd, count_bytes, entry_bytes, next_ifd_bytes, header) = if big_tiff { + if order.u16(b, 4)? != 8 || order.u16(b, 6)? != 0 { + return None; + } + (usize::try_from(order.u64(b, 8)?).ok()?, 8usize, BIG_ENTRY, 8usize, BIG_HEADER) + } else { + (usize::try_from(order.u32(b, 4)?).ok()?, 2usize, ENTRY, 4usize, HEADER) + }; + if ifd < header { + return None; + } + let count = if big_tiff { usize::try_from(order.u64(b, ifd)?).ok()? } else { usize::from(order.u16(b, ifd)?) }; + let first = ifd.checked_add(count_bytes)?; + let next_ifd = first.checked_add(count.checked_mul(entry_bytes)?)?; + if next_ifd.checked_add(next_ifd_bytes)? > b.len() { + return None; + } + // In bounds: every entry ends at or before `next_ifd`. + Some((order, first, count, entry_bytes, big_tiff)) +} + +/// A well-formed Orientation entry's value: where it sits and its field type. +#[derive(Clone, Copy)] +struct Value { + at: usize, + ty: u16, +} + +impl Value { + /// The stored value (a SHORT or LONG, left-justified in the 4-byte field). + fn read(self, order: Order, b: &[u8]) -> Option { + match self.ty { + TYPE_SHORT => order.u16(b, self.at).map(u32::from), + _ => order.u32(b, self.at), + } + } + + /// 1 in this entry's own type, width and byte order. + fn one(self, order: Order) -> Vec { + match (self.ty, order) { + (TYPE_SHORT, Order::Little) => 1u16.to_le_bytes().to_vec(), + (TYPE_SHORT, Order::Big) => 1u16.to_be_bytes().to_vec(), + (_, Order::Little) => 1u32.to_le_bytes().to_vec(), + (_, Order::Big) => 1u32.to_be_bytes().to_vec(), + } + } +} + +/// The value of the entry at `e` if it is a well-formed Orientation entry: +/// exactly one SHORT or LONG. +fn orientation_value(order: Order, b: &[u8], e: usize, big_tiff: bool) -> Option { + let count = if big_tiff { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) }; + if order.u16(b, e)? != TAG_ORIENTATION || count != 1 { + return None; + } + let ty = order.u16(b, e.checked_add(2)?)?; + let value_offset = if big_tiff { 12 } else { 8 }; + matches!(ty, TYPE_SHORT | TYPE_LONG).then_some(Value { at: e.checked_add(value_offset)?, ty }) +} + +/// Locates the Orientation value in IFD0. +/// +/// The **first** Orientation entry wins (like libexif and Photoshop); if that +/// one is malformed (count ≠ 1, or neither SHORT nor LONG) the tag is ignored, +/// even when a later duplicate is well formed. +fn find_entry(b: &[u8]) -> Option<(Order, Value)> { + let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; + let e = (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(TAG_ORIENTATION))?; + Some((order, orientation_value(order, b, e, big_tiff)?)) +} + +/// The orientation (1–8) recorded in a TIFF-structured block: an EXIF payload +/// (with or without the `Exif\0\0` prefix) or a whole TIFF file. Missing, +/// malformed or out-of-range values give 1. +pub fn exif_orientation(exif: &[u8]) -> u16 { + let b = tiff_body(exif); + find_entry(b).and_then(|(order, v)| v.read(order, b)).and_then(|v| u16::try_from(v).ok()).filter(|v| (1..=8).contains(v)).unwrap_or(1) +} + +/// `exif` with its Orientation rewritten to 1, borrowed when there is nothing +/// to change (no well-formed tag, already 1, or unparseable). +/// +/// Only the value bytes of each well-formed Orientation entry change, written +/// as 1 in the entry's existing type and width (SHORT or LONG); every other +/// byte (other tags, their offset-based values, sub-IFDs) is kept as is. +/// Well-formed duplicates are all set to 1, so a reader that lets a later +/// duplicate win cannot rotate the upright pixels either. +pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> { + let body = tiff_body(exif); + let prefix = exif.len() - body.len(); + let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else { + return Cow::Borrowed(exif); + }; + let values: Vec = + (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).filter_map(|e| orientation_value(order, body, e, big_tiff)).collect(); + if values.iter().all(|v| v.read(order, body) == Some(1)) { + return Cow::Borrowed(exif); + } + let mut out = exif.to_vec(); + for v in values { + let one = v.one(order); + let slot = prefix.checked_add(v.at).and_then(|at| out.get_mut(at..at.checked_add(one.len())?)); + if let Some(slot) = slot { + slot.copy_from_slice(&one); + } + } + Cow::Owned(out) +} + +/// `xmp` with any `tiff:Orientation` (attribute or element form) set to 1, +/// borrowed when there is nothing to change. +pub fn upright_xmp(xmp: &str) -> Cow<'_, str> { + const KEY: &str = "tiff:Orientation"; + if !xmp.contains(KEY) { + return Cow::Borrowed(xmp); + } + let mut out = String::with_capacity(xmp.len()); + let mut rest = xmp; + let mut changed = false; + while let Some(i) = rest.find(KEY) { + let (head, tail) = rest.split_at(i + KEY.len()); + out.push_str(head); + rest = tail; + // `tiff:Orientation="6"`, `tiff:Orientation='6'` or `6<`. + let open = rest.char_indices().take_while(|&(_, c)| c.is_ascii_whitespace() || matches!(c, '=' | '"' | '\'' | '>')).last(); + let Some((at, c)) = open else { continue }; + if !matches!(c, '"' | '\'' | '>') { + continue; + } + let start = at + 1; + let digits = rest.get(start..).map_or(0, |s| s.bytes().take_while(u8::is_ascii_digit).count()); + if digits == 0 || rest.get(start..start + digits) == Some("1") { + continue; + } + out.push_str(rest.get(..start).unwrap_or_default()); + out.push('1'); + rest = rest.get(start + digits..).unwrap_or_default(); + changed = true; + } + out.push_str(rest); + if changed { Cow::Owned(out) } else { Cow::Borrowed(xmp) } +} + +/// Source pixel index for output pixel `(0, y2)` and the step per output column, for +/// orientation `o` of a `w`×`h` source (values 2–8; see the module docs). +fn walk(o: u16, w: isize, h: isize, y2: isize) -> (isize, isize) { + match o { + 2 => (y2 * w + w - 1, -1), + 3 => ((h - 1 - y2) * w + w - 1, -1), + 4 => ((h - 1 - y2) * w, 1), + 5 => (y2, w), + 6 => ((h - 1) * w + y2, -w), + 7 => ((h - 1) * w + (w - 1 - y2), -w), + 8 => (w - 1 - y2, w), + _ => (y2 * w, 1), + } +} + +/// Output columns per block: a block's source pixels (for a 90° turn, `BLOCK` +/// rows × the band height) stay in cache while it is written. +const BLOCK: usize = 64; +/// Output rows per parallel band. +const BAND: usize = 64; + +/// Writes the oriented pixels of one band of output rows, `N` bytes per pixel. +fn band(src: &[[u8; N]], dst: &mut [[u8; N]], o: u16, (w, h): (usize, usize), ow: usize, y0: usize) { + let (wi, hi) = (w as isize, h as isize); + let rows = dst.len() / ow.max(1); + for x0 in (0..ow).step_by(BLOCK) { + let x1 = (x0 + BLOCK).min(ow); + for r in 0..rows { + let (base, step) = walk(o, wi, hi, (y0 + r) as isize); + let Some(out) = dst.get_mut(r * ow + x0..r * ow + x1) else { return }; + let mut i = base + x0 as isize * step; + for px in out { + if let Some(s) = usize::try_from(i).ok().and_then(|i| src.get(i)) { + *px = *s; + } + i += step; + } + } + } +} + +/// Orients `src` (`N` bytes per pixel) into a new buffer, a band of rows per task. +fn orient_n(src: &[u8], o: u16, (w, h): (usize, usize), ow: usize) -> Result, CodecError> { + let mut out = alloc_zeroed(src.len())?; + let (s, _) = src.as_chunks::(); + let (d, _) = out.as_chunks_mut::(); + let per = (BAND * ow).max(1); + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + d.par_chunks_mut(per).enumerate().for_each(|(b, chunk)| band(s, chunk, o, (w, h), ow, b * BAND)); + } + #[cfg(target_arch = "wasm32")] + for (b, chunk) in d.chunks_mut(per).enumerate() { + band(s, chunk, o, (w, h), ow, b * BAND); + } + Ok(out) +} + +/// A zeroed buffer of `n` bytes, or an error (not an abort) when memory runs out. +fn alloc_zeroed(n: usize) -> Result, CodecError> { + let mut out = Vec::new(); + out.try_reserve_exact(n).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {n} bytes to orient the image")))?; + out.resize(n, 0); + Ok(out) +} + +/// Pixel-format-agnostic orientation: any byte size per pixel. +fn orient_bytes(src: &[u8], bpp: usize, o: u16, (w, h): (usize, usize), ow: usize) -> Result, CodecError> { + match bpp { + 1 => orient_n::<1>(src, o, (w, h), ow), + 2 => orient_n::<2>(src, o, (w, h), ow), + 3 => orient_n::<3>(src, o, (w, h), ow), + 4 => orient_n::<4>(src, o, (w, h), ow), + 5 => orient_n::<5>(src, o, (w, h), ow), + 6 => orient_n::<6>(src, o, (w, h), ow), + 8 => orient_n::<8>(src, o, (w, h), ow), + 10 => orient_n::<10>(src, o, (w, h), ow), + 12 => orient_n::<12>(src, o, (w, h), ow), + 16 => orient_n::<16>(src, o, (w, h), ow), + _ => orient_n::<20>(src, o, (w, h), ow), + } +} + +/// Writes the oriented output rows `y0..` that fit in `dst` (whole rows of `ow` pixels), `bpp` +/// bytes per pixel: one band of [`orient_bytes`], the same pixels it writes. +fn orient_band(src: &[u8], dst: &mut [u8], bpp: usize, o: u16, (w, h): (usize, usize), ow: usize, y0: usize) { + macro_rules! go { + ($n:literal) => {{ + let (s, _) = src.as_chunks::<$n>(); + let (d, _) = dst.as_chunks_mut::<$n>(); + band(s, d, o, (w, h), ow, y0) + }}; + } + match bpp { + 1 => go!(1), + 2 => go!(2), + 3 => go!(3), + 4 => go!(4), + 5 => go!(5), + 6 => go!(6), + 8 => go!(8), + 10 => go!(10), + 12 => go!(12), + 16 => go!(16), + _ => go!(20), + } +} + +/// An image turned upright a band of rows at a time, for a caller that must not block for the +/// whole image (the web build has no threads): [`OrientSteps::step`] gives the [`Image`] +/// [`Image::oriented`] gives, once every band is written. +pub struct OrientSteps { + src: Image, + o: u16, + bpp: usize, + /// The oriented size. + ow: usize, + oh: usize, + out: Vec, + /// Output rows written so far. + done: usize, +} + +impl OrientSteps { + /// Sets up turning `img` upright for orientation `o` (2–8; anything else leaves it as it is). + /// Allocates the output, which the image is turned into beside the original. + pub fn new(img: Image, o: u16) -> Result { + let (w, h) = (img.width() as usize, img.height() as usize); + let (ow, oh) = if o >= 5 { (h, w) } else { (w, h) }; + let (layout, sample) = (img.layout(), img.sample_type()); + let bpp = layout.channels() * sample.bytes(); + let turns = (2..=8).contains(&o); + if turns && (!matches!(bpp, 1..=6 | 8 | 10 | 12 | 16 | 20) || w == 0 || h == 0) { + return Err(CodecError::InvalidImage(format!("cannot orient {layout:?} {sample:?} {w}x{h}"))); + } + // Reserve without touching: `step` grows the buffer a band at a time, so no slice + // zero-fills the whole output. + let mut out = Vec::new(); + if turns { + let n = img.data().len(); + out.try_reserve_exact(n).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {n} bytes to orient the image")))?; + } + Ok(Self { src: img, o, bpp, ow, oh, out, done: 0 }) + } + + /// How much of the image is turned, `0.0..=1.0`. + pub fn progress(&self) -> f32 { + if self.oh == 0 { 1.0 } else { self.done as f32 / self.oh as f32 } + } + + /// Writes bands of [`BAND`] output rows, at least one, then goes on while `more()` returns + /// true; `Some` with the upright image once the last band is written. + pub fn step(&mut self, more: &mut dyn FnMut() -> bool) -> Result, CodecError> { + if !(2..=8).contains(&self.o) { + let placeholder = Image::from_raw(0, 0, self.src.layout(), self.src.sample_type(), Vec::new())?; + return Ok(Some(std::mem::replace(&mut self.src, placeholder))); + } + let (w, h) = (self.src.width() as usize, self.src.height() as usize); + while self.done < self.oh { + let rows = BAND.min(self.oh - self.done); + let row_bytes = self.ow.checked_mul(self.bpp).ok_or_else(|| CodecError::InvalidImage("image too large".into()))?; + let at = self.done.saturating_mul(row_bytes); + let end = at.saturating_add(rows.saturating_mul(row_bytes)); + if self.out.len() < end { + self.out.resize(end, 0); + } + let dst = self.out.get_mut(at..end).ok_or_else(|| CodecError::InvalidImage("the oriented rows are out of range".into()))?; + orient_band(self.src.data(), dst, self.bpp, self.o, (w, h), self.ow, self.done); + self.done += rows; + if self.done < self.oh && !more() { + return Ok(None); + } + } + let (layout, sample) = (self.src.layout(), self.src.sample_type()); + let placeholder = Image::from_raw(0, 0, layout, sample, Vec::new())?; + let src = std::mem::replace(&mut self.src, placeholder); + let data = std::mem::take(&mut self.out); + src.oriented_with(self.o, data).map(Some) + } +} + +impl Image { + /// The image turned upright for orientation `o` (1–8; anything else is + /// treated as 1 and returns the image unchanged). Width and height swap + /// for 5–8, as does the DPI, and the EXIF and XMP orientation are + /// rewritten to 1 so the result is not rotated again on display or export. + pub fn oriented(self, o: u16) -> Result { + if !(2..=8).contains(&o) { + return Ok(self); + } + let (w, h) = (self.width() as usize, self.height() as usize); + let ow = if o >= 5 { h } else { w }; + let (layout, sample) = (self.layout(), self.sample_type()); + let bpp = layout.channels() * sample.bytes(); + // Every layout/sample pair is 1–20 bytes per pixel and has a fixed-size path. + if !matches!(bpp, 1..=6 | 8 | 10 | 12 | 16 | 20) || w == 0 || h == 0 { + return Err(CodecError::InvalidImage(format!("cannot orient {layout:?} {sample:?} {w}x{h}"))); + } + let data = orient_bytes(self.data(), bpp, o, (w, h), ow)?; + self.oriented_with(o, data) + } + + /// The upright image of orientation `o` (2–8) whose turned pixels are `data`: the size, + /// metadata and extra samples of [`Image::oriented`]. + fn oriented_with(self, o: u16, data: Vec) -> Result { + let (w, h) = (self.width() as usize, self.height() as usize); + let swap = o >= 5; + let (ow, oh) = if swap { (h, w) } else { (w, h) }; + let (layout, sample) = (self.layout(), self.sample_type()); + let mut meta = self.meta.clone(); + // Extra samples (a TIFF's alpha and spot planes, one sample a pixel) turn with the image. + // A plane of the wrong size is left as it is: importers drop it with a warning. + let plane_len = w.saturating_mul(h).saturating_mul(sample.bytes()); + for e in meta.extra_samples.iter_mut().filter(|e| e.data.len() == plane_len) { + e.data = orient_bytes(&e.data, sample.bytes(), o, (w, h), ow)?; + } + if let Some(e) = &mut meta.exif + && let Cow::Owned(fixed) = upright_exif(e) + { + *e = fixed; + } + if let Some(x) = &mut meta.xmp + && let Cow::Owned(fixed) = upright_xmp(x) + { + *x = fixed; + } + if swap { + meta.dpi = meta.dpi.map(|(x, y)| (y, x)); + } + let mut out = Image::from_raw(ow as u32, oh as u32, layout, sample, data)?.with_icc(self.icc).with_meta(meta); + out.warnings = self.warnings; + Ok(out) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_impossible_allocation_is_an_error_not_an_abort() { + assert!(matches!(alloc_zeroed(usize::MAX), Err(CodecError::LimitExceeded(_)))); + assert!(matches!(orient_n::<1>(&[], 6, (0, 0), 0), Ok(v) if v.is_empty())); + assert_eq!(alloc_zeroed(3).unwrap(), vec![0, 0, 0]); + } +} diff --git a/crates/image-codecs/src/resolution.rs b/crates/image-codecs/src/resolution.rs new file mode 100644 index 000000000..a787cacbc --- /dev/null +++ b/crates/image-codecs/src/resolution.rs @@ -0,0 +1,575 @@ +//! Image resolution (pixels per inch) recorded in metadata blocks: EXIF / TIFF IFD0 +//! (XResolution 282, YResolution 283, ResolutionUnit 296), XMP (`tiff:XResolution`, +//! `tiff:YResolution`, `tiff:ResolutionUnit`) and Photoshop's image resource +//! ResolutionInfo (`8BIM` resource 0x03ED); reading them, and rewriting the EXIF and XMP +//! copies on export so they agree with the resolution the file is written at. +//! +//! Implemented from TIFF 6.0 (section 8, "Baseline fields": XResolution / YResolution are +//! RATIONAL, ResolutionUnit is SHORT, 1 = no absolute unit, 2 = inch, 3 = centimetre, +//! default 2), EXIF 2.3 (same tags in the 0th IFD, same default) and the Adobe Photoshop File +//! Formats Specification (ResolutionInfo: Fixed 16.16 hRes in pixels per inch, then the +//! display units; the vertical resolution likewise). +//! +//! **Precedence** when a file carries several, as Photoshop resolves them: Photoshop's own +//! ResolutionInfo, then the XMP `tiff:` properties, then EXIF, and only then the format's own +//! density field when it is the generic JFIF one (see the JPEG decoder). JFIF density is last +//! because cameras record the real resolution in EXIF while many tools and libraries write a +//! default JFIF density of 72 (or a bare aspect ratio) without knowing it; Photoshop shows a +//! camera JPEG at its EXIF resolution whatever its JFIF header says. Formats whose own field is +//! written deliberately (PNG `pHYs`, TIFF tags) keep it first; the metadata blocks only fill in +//! when it is missing. +//! +//! Everything read here is untrusted: offsets and counts are bounds-checked, both byte orders +//! and BigTIFF-style EXIF are handled, and anything malformed reads as "no resolution" rather +//! than as a wrong one. + +use std::borrow::Cow; + +use crate::orientation::{Order, ifd0_entries, tiff_body, upright_exif, upright_xmp}; + +const TAG_X_RESOLUTION: u16 = 282; +const TAG_Y_RESOLUTION: u16 = 283; +const TAG_RESOLUTION_UNIT: u16 = 296; +const TYPE_SHORT: u16 = 3; +const TYPE_RATIONAL: u16 = 5; +const CM_PER_INCH: f64 = 2.54; +/// Photoshop's ResolutionInfo image resource. +const RESOLUTION_INFO: u16 = 0x03ED; +/// Resolutions above this (pixels per inch) are treated as damage, not data. +const MAX_PPI: f64 = 1_000_000.0; +/// Two resolutions this close (pixels per inch) are the same. +const SAME_PPI: f64 = 1e-3; + +/// A usable resolution pair, or `None`. +fn ppi_pair(x: f64, y: f64) -> Option<(f32, f32)> { + let ok = |v: f64| v.is_finite() && v > 0.0 && v <= MAX_PPI; + (ok(x) && ok(y)).then_some((x as f32, y as f32)) +} + +fn same(a: (f32, f32), b: (f32, f32)) -> bool { + (f64::from(a.0) - f64::from(b.0)).abs() < SAME_PPI && (f64::from(a.1) - f64::from(b.1)).abs() < SAME_PPI +} + +/// `ppi` as a TIFF RATIONAL: a whole number over 1 when it is one, else over 1000. +fn rational(ppi: f32) -> (u32, u32) { + let p = f64::from(ppi); + if (p - p.round()).abs() < SAME_PPI { (p.round() as u32, 1) } else { ((p * 1000.0).round() as u32, 1000) } +} + +/// One of the three resolution entries of IFD0. +#[derive(Clone, Copy)] +struct Field { + /// Offset of the 12- (BigTIFF: 20-) byte entry. + entry: usize, + tag: u16, + ty: u16, + count: u64, + /// Where the value bytes are, when the type is one we read and they lie inside the block. + value: Option, +} + +impl Field { + fn short(&self, order: Order, b: &[u8]) -> Option { + (self.ty == TYPE_SHORT && self.count == 1).then_some(())?; + order.u16(b, self.value?) + } + + fn rational(&self, order: Order, b: &[u8]) -> Option { + (self.ty == TYPE_RATIONAL && self.count == 1).then_some(())?; + let at = self.value?; + let (n, d) = (order.u32(b, at)?, order.u32(b, at.checked_add(4)?)?); + (d != 0).then(|| f64::from(n) / f64::from(d)) + } +} + +/// IFD0's resolution entries. +struct Ifd0 { + order: Order, + big: bool, + first: usize, + count: usize, + entry_bytes: usize, + fields: Vec, +} + +impl Ifd0 { + fn parse(b: &[u8]) -> Option { + let (order, first, count, entry_bytes, big) = ifd0_entries(b)?; + let mut fields = Vec::new(); + for i in 0..count { + let e = first.checked_add(i.checked_mul(entry_bytes)?)?; + let tag = order.u16(b, e)?; + if !matches!(tag, TAG_X_RESOLUTION | TAG_Y_RESOLUTION | TAG_RESOLUTION_UNIT) { + continue; + } + let ty = order.u16(b, e.checked_add(2)?)?; + let count = if big { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) }; + let value = value_at(order, b, e, big, ty, count); + fields.push(Field { entry: e, tag, ty, count, value }); + } + Some(Ifd0 { order, big, first, count, entry_bytes, fields }) + } + + fn first_of(&self, tag: u16) -> Option<&Field> { + self.fields.iter().find(|f| f.tag == tag) + } +} + +/// Where an entry's SHORT or RATIONAL value bytes are: inline in the entry when they fit, +/// else at the offset it holds. `None` for other types or when they leave the block. +fn value_at(order: Order, b: &[u8], e: usize, big: bool, ty: u16, count: u64) -> Option { + let size = match ty { + TYPE_SHORT => 2u64, + TYPE_RATIONAL => 8, + _ => return None, + } + .checked_mul(count)?; + let (field, inline) = if big { (e.checked_add(12)?, 8) } else { (e.checked_add(8)?, 4) }; + let at = if size <= inline { + field + } else if big { + usize::try_from(order.u64(b, field)?).ok()? + } else { + usize::try_from(order.u32(b, field)?).ok()? + }; + (at.checked_add(usize::try_from(size).ok()?)? <= b.len()).then_some(at) +} + +/// The resolution (pixels per inch, horizontal and vertical) recorded in IFD0 of an EXIF block +/// (with or without the JPEG `Exif\0\0` prefix) or a TIFF structure. +/// +/// Both XResolution and YResolution must be one RATIONAL each with a non-zero denominator. +/// ResolutionUnit 2 (inch, also when the tag is absent) is used as is, 3 (centimetre) is +/// converted; 1 (no absolute unit), any other value or a malformed unit entry gives `None`. +/// The first of duplicated entries wins. +pub fn exif_resolution(exif: &[u8]) -> Option<(f32, f32)> { + let b = tiff_body(exif); + let ifd = Ifd0::parse(b)?; + let unit = match ifd.first_of(TAG_RESOLUTION_UNIT) { + None => 2, + Some(f) => f.short(ifd.order, b)?, + }; + let scale = match unit { + 2 => 1.0, + 3 => CM_PER_INCH, + _ => return None, + }; + let x = ifd.first_of(TAG_X_RESOLUTION)?.rational(ifd.order, b)?; + let y = ifd.first_of(TAG_Y_RESOLUTION)?.rational(ifd.order, b)?; + ppi_pair(x * scale, y * scale) +} + +/// `exif` with its IFD0 resolution set to `ppi` (pixels per inch), borrowed when it has no +/// resolution entries, already records that resolution, or cannot be parsed. +/// +/// Existing well-formed XResolution / YResolution (one RATIONAL) entries are rewritten in place, +/// and every well-formed ResolutionUnit (one SHORT) becomes 2 (inch). Malformed entries are +/// removed from IFD0 instead, so no stale value survives; all other bytes are kept. +pub fn exif_with_resolution(exif: &[u8], ppi: (f32, f32)) -> Cow<'_, [u8]> { + exif_with_resolution_inner(exif, ppi, false) +} + +/// Like [`exif_with_resolution`], but adds X/Y resolution tags when a valid IFD0 has none. +/// Existing TIFF data stays in place, so its offsets remain valid. Used by CMYK JPEG, which +/// cannot carry a JFIF density marker. +pub(crate) fn exif_with_resolution_adding_missing(exif: &[u8], ppi: (f32, f32)) -> Cow<'_, [u8]> { + exif_with_resolution_inner(exif, ppi, true) +} + +/// Rewrite valid IFD0 resolution tags and optionally append tags that are missing or malformed. +fn exif_with_resolution_inner(exif: &[u8], ppi: (f32, f32), add_missing: bool) -> Cow<'_, [u8]> { + if ppi_pair(f64::from(ppi.0), f64::from(ppi.1)).is_none() { + return Cow::Borrowed(exif); + } + if exif.is_empty() { + if !add_missing { + return Cow::Borrowed(exif); + } + // An image with DPI but no source EXIF still needs a valid EXIF block when the output + // format cannot carry resolution in its own header (CMYK JPEG has no valid JFIF marker). + return Cow::Owned(exif_with_resolution_inner(b"II*\0\x08\0\0\0\0\0\0\0\0\0\0", ppi, true).into_owned()); + } + let body = tiff_body(exif); + let prefix = exif.len() - body.len(); + let Some(ifd) = Ifd0::parse(body) else { return Cow::Borrowed(exif) }; + if exif_resolution(body).is_some_and(|r| same(r, ppi)) { + return Cow::Borrowed(exif); + } + let missing_xy = ifd.first_of(TAG_X_RESOLUTION).and_then(|f| f.rational(ifd.order, body)).is_none() + || ifd.first_of(TAG_Y_RESOLUTION).and_then(|f| f.rational(ifd.order, body)).is_none(); + if add_missing && missing_xy { + let Some(body) = append_resolution_ifd(body, &ifd, ppi) else { return Cow::Borrowed(exif) }; + let mut full = exif.get(..prefix).unwrap_or_default().to_vec(); + full.extend_from_slice(&body); + return Cow::Owned(full); + } + if ifd.fields.is_empty() { + return Cow::Borrowed(exif); + } + let order = ifd.order; + let mut out = body.to_vec(); + let mut removed = Vec::new(); + for f in &ifd.fields { + let bytes = match f.tag { + TAG_RESOLUTION_UNIT if f.short(order, body).is_some() => put_u16(order, 2).to_vec(), + TAG_X_RESOLUTION | TAG_Y_RESOLUTION if f.rational(order, body).is_some() => { + let (n, d) = rational(if f.tag == TAG_X_RESOLUTION { ppi.0 } else { ppi.1 }); + [put_u32(order, n), put_u32(order, d)].concat() + } + _ => { + removed.push(f.entry); + continue; + } + }; + let slot = f.value.and_then(|at| out.get_mut(at..at.checked_add(bytes.len())?)); + match slot { + Some(slot) => slot.copy_from_slice(&bytes), + None => removed.push(f.entry), + } + } + if !removed.is_empty() { + remove_entries(&mut out, &ifd, &removed); + } + let mut full = exif.get(..prefix).unwrap_or_default().to_vec(); + full.extend_from_slice(&out); + if full == exif { Cow::Borrowed(exif) } else { Cow::Owned(full) } +} + +/// Append a replacement IFD0 with fresh resolution tags. Every other entry keeps its original +/// bytes and offsets, since the TIFF block's existing data remains where it was. +fn append_resolution_ifd(body: &[u8], ifd: &Ifd0, ppi: (f32, f32)) -> Option> { + let (count_bytes, next_bytes, alignment) = if ifd.big { (8usize, 8usize, 8usize) } else { (2usize, 4usize, 2usize) }; + let mut entries = Vec::with_capacity(ifd.count.saturating_add(3)); + for index in 0..ifd.count { + let entry = ifd.first.checked_add(index.checked_mul(ifd.entry_bytes)?)?; + let tag = ifd.order.u16(body, entry)?; + if matches!(tag, TAG_X_RESOLUTION | TAG_Y_RESOLUTION | TAG_RESOLUTION_UNIT) { + continue; + } + entries.push((tag, body.get(entry..entry.checked_add(ifd.entry_bytes)?)?.to_vec())); + } + let count = entries.len().checked_add(3)?; + if !ifd.big && count > usize::from(u16::MAX) { + return None; + } + + let new_ifd = align_up(body.len(), alignment)?; + let table_end = new_ifd.checked_add(count_bytes)?.checked_add(count.checked_mul(ifd.entry_bytes)?)?; + let next_at = ifd.first.checked_add(ifd.count.checked_mul(ifd.entry_bytes)?)?; + let next_ifd = body.get(next_at..next_at.checked_add(next_bytes)?)?; + let rational_at = align_up(table_end.checked_add(next_bytes)?, if ifd.big { 8 } else { 4 })?; + let y_rational_at = rational_at.checked_add(8)?; + if !ifd.big && (new_ifd > u32::MAX as usize || y_rational_at.checked_add(8)? > u32::MAX as usize) { + return None; + } + + entries.push((TAG_X_RESOLUTION, resolution_entry(ifd.order, ifd.big, TAG_X_RESOLUTION, u64::try_from(rational_at).ok()?)?)); + entries.push((TAG_Y_RESOLUTION, resolution_entry(ifd.order, ifd.big, TAG_Y_RESOLUTION, u64::try_from(y_rational_at).ok()?)?)); + entries.push((TAG_RESOLUTION_UNIT, resolution_entry(ifd.order, ifd.big, TAG_RESOLUTION_UNIT, 2)?)); + entries.sort_by_key(|(tag, _)| *tag); + + let mut out = body.to_vec(); + out.resize(new_ifd, 0); + if ifd.big { + out.extend_from_slice(&put_u64(ifd.order, count as u64)); + } else { + out.extend_from_slice(&put_u16(ifd.order, count as u16)); + } + for (_, entry) in entries { + out.extend_from_slice(&entry); + } + out.extend_from_slice(next_ifd); + out.resize(rational_at, 0); + let (xn, xd) = rational(ppi.0); + let (yn, yd) = rational(ppi.1); + out.extend_from_slice(&put_u32(ifd.order, xn)); + out.extend_from_slice(&put_u32(ifd.order, xd)); + out.extend_from_slice(&put_u32(ifd.order, yn)); + out.extend_from_slice(&put_u32(ifd.order, yd)); + + let offset_at: usize = if ifd.big { 8 } else { 4 }; + if ifd.big { + out.get_mut(offset_at..offset_at.checked_add(8)?)?.copy_from_slice(&put_u64(ifd.order, new_ifd as u64)); + } else { + out.get_mut(offset_at..offset_at.checked_add(4)?)?.copy_from_slice(&put_u32(ifd.order, u32::try_from(new_ifd).ok()?)); + } + Some(out) +} + +/// One IFD entry. Rational fields store an offset; the SHORT unit is inline. +fn resolution_entry(order: Order, big: bool, tag: u16, value: u64) -> Option> { + let entry_bytes = if big { 20 } else { 12 }; + let count_at = 4; + let value_at = if big { 12 } else { 8 }; + let mut entry = vec![0; entry_bytes]; + entry.get_mut(..2)?.copy_from_slice(&put_u16(order, tag)); + entry.get_mut(2..4)?.copy_from_slice(&put_u16(order, if tag == TAG_RESOLUTION_UNIT { TYPE_SHORT } else { TYPE_RATIONAL })); + if big { + entry.get_mut(count_at..count_at + 8)?.copy_from_slice(&put_u64(order, 1)); + if tag == TAG_RESOLUTION_UNIT { + entry.get_mut(value_at..value_at + 2)?.copy_from_slice(&put_u16(order, value as u16)); + } else { + entry.get_mut(value_at..value_at + 8)?.copy_from_slice(&put_u64(order, value)); + } + } else { + entry.get_mut(count_at..count_at + 4)?.copy_from_slice(&put_u32(order, 1)); + if tag == TAG_RESOLUTION_UNIT { + entry.get_mut(value_at..value_at + 2)?.copy_from_slice(&put_u16(order, value as u16)); + } else { + entry.get_mut(value_at..value_at + 4)?.copy_from_slice(&put_u32(order, u32::try_from(value).ok()?)); + } + } + Some(entry) +} + +/// Round `value` up to the next `alignment` boundary, returning `None` on overflow. +fn align_up(value: usize, alignment: usize) -> Option { + value.checked_add((alignment - value % alignment) % alignment) +} + +fn put_u16(order: Order, v: u16) -> [u8; 2] { + match order { + Order::Little => v.to_le_bytes(), + Order::Big => v.to_be_bytes(), + } +} + +fn put_u32(order: Order, v: u32) -> [u8; 4] { + match order { + Order::Little => v.to_le_bytes(), + Order::Big => v.to_be_bytes(), + } +} + +fn put_u64(order: Order, v: u64) -> [u8; 8] { + match order { + Order::Little => v.to_le_bytes(), + Order::Big => v.to_be_bytes(), + } +} + +/// Removes the IFD0 entries at offsets `removed` (see [`exif_with_resolution`]). +fn remove_entries(b: &mut [u8], ifd: &Ifd0, removed: &[usize]) { + let eb = ifd.entry_bytes; + let next_bytes = if ifd.big { 8 } else { 4 }; + let count_bytes = if ifd.big { 8 } else { 2 }; + let Some(table_end) = ifd.count.checked_mul(eb).and_then(|n| ifd.first.checked_add(n)) else { return }; + let Some(old) = b.get(ifd.first..table_end.saturating_add(next_bytes)).map(<[u8]>::to_vec) else { return }; + let mut table = Vec::with_capacity(old.len()); + let mut kept = 0usize; + for (i, entry) in old.chunks_exact(eb).take(ifd.count).enumerate() { + if !removed.contains(&(ifd.first + i * eb)) { + table.extend_from_slice(entry); + kept += 1; + } + } + table.extend_from_slice(old.get(ifd.count * eb..).unwrap_or_default()); + table.resize(old.len(), 0); + let count: Vec = if ifd.big { put_u64(ifd.order, kept as u64).to_vec() } else { put_u16(ifd.order, kept as u16).to_vec() }; + let Some(count_at) = ifd.first.checked_sub(count_bytes) else { return }; + if let Some(slot) = b.get_mut(count_at..ifd.first) { + slot.copy_from_slice(&count); + } + if let Some(slot) = b.get_mut(ifd.first..ifd.first + table.len()) { + slot.copy_from_slice(&table); + } +} + +/// The resolution in a Photoshop image resource block (a sequence of `8BIM` resources, as in a +/// JPEG's APP13 "Photoshop 3.0" segment or TIFF tag 34377): its ResolutionInfo (0x03ED), whose +/// Fixed 16.16 hRes / vRes are pixels per inch whatever display unit follows them. +pub fn photoshop_resolution(resources: &[u8]) -> Option<(f32, f32)> { + let be16 = |at: usize| resources.get(at..at.checked_add(2)?).map(|s| u16::from_be_bytes([s[0], s[1]])); + let be32 = |at: usize| resources.get(at..at.checked_add(4)?).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]])); + let mut i = 0usize; + while i < resources.len() { + let signature = resources.get(i..i.checked_add(4)?)?; + let id = be16(i.checked_add(4)?)?; + // Pascal name: length byte plus text, padded to an even size. + let name_len = usize::from(*resources.get(i.checked_add(6)?)?); + let name = (name_len + 1).next_multiple_of(2); + let size_at = i.checked_add(6)?.checked_add(name)?; + let size = usize::try_from(be32(size_at)?).ok()?; + let data = size_at.checked_add(4)?; + if signature == b"8BIM" && id == RESOLUTION_INFO { + let info = resources.get(data..data.checked_add(size)?)?; + if info.len() < 16 { + return None; + } + let fixed = |at: usize| be32(data + at).map(|v| f64::from(v) / 65536.0); + return ppi_pair(fixed(0)?, fixed(8)?); + } + i = data.checked_add(size)?.checked_add(size % 2)?; + } + None +} + +/// The text ranges of every value of XMP property `key` (`key="v"`, `key='v'` or +/// `v`), in order. +fn xmp_values(xmp: &str, key: &str) -> Vec<(usize, usize)> { + let mut out = Vec::new(); + let mut from = 0; + while let Some(k) = xmp.get(from..).and_then(|s| s.find(key)).map(|k| k + from) { + from = k + key.len(); + // An attribute or an opening tag of exactly this name (not ``, not `xkey`). + if !xmp.get(..k).and_then(|h| h.chars().next_back()).is_some_and(|c| c == '<' || c.is_ascii_whitespace()) { + continue; + } + let rest = xmp.get(from..).unwrap_or_default(); + let after_key = rest.trim_start(); + let (value, close) = if let Some(r) = after_key.strip_prefix('=') { + let r = r.trim_start(); + match r.chars().next() { + Some(q @ ('"' | '\'')) => (r.get(1..).unwrap_or_default(), q), + _ => continue, + } + } else if let Some(r) = rest.strip_prefix('>') { + (r, '<') + } else { + continue; + }; + let start = xmp.len() - value.len(); + // Advance past this value before searching for the next key. Without this, `from` + // only moved past the key text, so property-like text repeated inside an unrelated + // attribute re-ran this full-suffix `find(close)` for every occurrence, which is + // quadratic in the packet size. A missing close means the value is unterminated, so + // no later value can parse either; stop scanning in that case. + let Some(len) = value.find(close) else { break }; + out.push((start, start + len)); + from = start + len + close.len_utf8(); + } + out +} + +/// An XMP rational (`300/1`) or plain number. +fn xmp_number(s: &str) -> Option { + let s = s.trim(); + match s.split_once('/') { + Some((n, d)) => { + let (n, d) = (n.trim().parse::().ok()?, d.trim().parse::().ok()?); + (d != 0.0).then(|| n / d) + } + None => s.parse().ok(), + } +} + +const XMP_X: &str = "tiff:XResolution"; +const XMP_Y: &str = "tiff:YResolution"; +const XMP_UNIT: &str = "tiff:ResolutionUnit"; + +/// The resolution (pixels per inch) in an XMP packet's `tiff:XResolution` / `tiff:YResolution` +/// (with `tiff:ResolutionUnit`, 2 when absent, 3 converted from centimetres), or `None`. +pub fn xmp_resolution(xmp: &str) -> Option<(f32, f32)> { + let first = |key: &str| xmp_values(xmp, key).first().and_then(|&(a, b)| xmp.get(a..b)); + let unit = match first(XMP_UNIT) { + None => 2.0, + Some(u) => xmp_number(u)?, + }; + let scale = if unit == 2.0 { + 1.0 + } else if unit == 3.0 { + CM_PER_INCH + } else { + return None; + }; + ppi_pair(xmp_number(first(XMP_X)?)? * scale, xmp_number(first(XMP_Y)?)? * scale) +} + +/// `xmp` with its `tiff:` resolution properties set to `ppi` (and the unit to 2, inch), +/// borrowed when it has none or already says so. +pub fn xmp_with_resolution(xmp: &str, ppi: (f32, f32)) -> Cow<'_, str> { + if ppi_pair(f64::from(ppi.0), f64::from(ppi.1)).is_none() || xmp_resolution(xmp).is_some_and(|r| same(r, ppi)) { + return Cow::Borrowed(xmp); + } + let text = |v: f32| { + let (n, d) = rational(v); + format!("{n}/{d}") + }; + let mut edits: Vec<(usize, usize, String)> = Vec::new(); + for (key, value) in [(XMP_X, text(ppi.0)), (XMP_Y, text(ppi.1)), (XMP_UNIT, "2".to_owned())] { + edits.extend(xmp_values(xmp, key).into_iter().map(|(a, b)| (a, b, value.clone()))); + } + if edits.is_empty() { + return Cow::Borrowed(xmp); + } + edits.sort_by_key(|e| e.0); + let mut out = String::with_capacity(xmp.len() + 16); + let mut at = 0; + for (a, b, v) in edits { + // Overlapping ranges can only come from malformed markup; keep the first edit. + if a < at { + continue; + } + out.push_str(xmp.get(at..a).unwrap_or_default()); + out.push_str(&v); + at = b; + } + out.push_str(xmp.get(at..).unwrap_or_default()); + if out == xmp { Cow::Borrowed(xmp) } else { Cow::Owned(out) } +} + +/// `exif` without its embedded thumbnail, or `None` when it has none (or doesn't parse). +/// +/// EXIF keeps the thumbnail in IFD1, the IFD IFD0's next-IFD pointer names. Finder, Android's +/// gallery and upload pickers show that thumbnail instead of decoding the image, so a copied one +/// keeps showing the photo as it was before it was edited (#2147, #2373). The pointer is cleared; +/// nothing else moves, so every other offset in the block stays valid. +fn exif_without_thumbnail(exif: &[u8]) -> Option> { + let body = tiff_body(exif); + let prefix = exif.len() - body.len(); + let (_, first, count, entry_bytes, big_tiff) = ifd0_entries(body)?; + let next = prefix.checked_add(first)?.checked_add(count.checked_mul(entry_bytes)?)?; + let width = if big_tiff { 8 } else { 4 }; + // In bounds: `ifd0_entries` checked that the whole pointer is present. + if exif.get(next..next.checked_add(width)?)?.iter().all(|b| *b == 0) { + return None; + } + let mut out = exif.to_vec(); + out.get_mut(next..next + width)?.fill(0); + Some(out) +} + +/// The EXIF to write next to upright pixels at resolution `ppi`: Orientation 1 (see +/// [`upright_exif`]), no thumbnail of the source image (see [`exif_without_thumbnail`]) and, +/// when `ppi` is known, the resolution (see [`exif_with_resolution`]). +pub fn export_exif(exif: &[u8], ppi: Option<(f32, f32)>) -> Cow<'_, [u8]> { + export_exif_inner(exif, ppi, false) +} + +/// Export EXIF for a format without its own resolution field, adding missing X/Y resolution +/// entries to valid metadata when necessary. +pub(crate) fn export_exif_adding_missing_resolution(exif: &[u8], ppi: Option<(f32, f32)>) -> Cow<'_, [u8]> { + export_exif_inner(exif, ppi, true) +} + +/// Normalize EXIF orientation and thumbnails, optionally adding missing output resolution tags. +fn export_exif_inner(exif: &[u8], ppi: Option<(f32, f32)>, add_missing: bool) -> Cow<'_, [u8]> { + let up = upright_exif(exif); + let up = match exif_without_thumbnail(&up) { + Some(v) => Cow::Owned(v), + None => up, + }; + let Some(ppi) = ppi else { return up }; + let result = if add_missing { exif_with_resolution_adding_missing(&up, ppi) } else { exif_with_resolution(&up, ppi) }; + if let Cow::Owned(v) = result { + return Cow::Owned(v); + } + up +} + +/// The XMP to write next to upright pixels at resolution `ppi` (see [`upright_xmp`] and +/// [`xmp_with_resolution`]). +pub fn export_xmp(xmp: &str, ppi: Option<(f32, f32)>) -> Cow<'_, str> { + let up = upright_xmp(xmp); + let Some(ppi) = ppi else { return up }; + if let Cow::Owned(v) = xmp_with_resolution(&up, ppi) { + return Cow::Owned(v); + } + up +} + +/// The resolution a decoded file's metadata blocks record, in precedence order: XMP, then +/// EXIF (Photoshop's ResolutionInfo is format-specific and read by the decoder). +pub(crate) fn from_metadata(exif: Option<&[u8]>, xmp: Option<&str>) -> Option<(f32, f32)> { + xmp.and_then(xmp_resolution).or_else(|| exif.and_then(exif_resolution)) +} diff --git a/crates/image-codecs/src/source_settings.rs b/crates/image-codecs/src/source_settings.rs new file mode 100644 index 000000000..ebd0421c3 --- /dev/null +++ b/crates/image-codecs/src/source_settings.rs @@ -0,0 +1,355 @@ +//! The encoder settings a lossy file was written with, read back from its own data, so a save +//! can write it again the way it came (File › Save keeps a JPEG's quality and a WebP's lossy or +//! lossless encoding). +//! +//! * **JPEG**: the quality is estimated from the quantisation tables (DQT). Most encoders scale +//! the example tables of ITU-T T.81 Annex K (K.1 luminance, K.2 chrominance) by the +//! Independent JPEG Group's quality curve, so the quality whose scaled tables match the file's +//! best is the one it was saved at (exact for such tables; the closest fit for others). +//! * **WebP**: a `VP8L` image is lossless; a `VP8 ` image is lossy, and its base quantiser index +//! (RFC 6386 §9.6) maps back to a quality on the scale our encoder uses. +//! +//! Everything here reads hostile bytes: every index is checked and a malformed file gives `None`. + +use crate::codecs::vp8; + +/// ITU-T T.81 Annex K.1, the example luminance quantisation table, in natural (row) order. +const ANNEX_K_LUMA: [u16; 64] = [ + 16, 11, 10, 16, 24, 40, 51, 61, // + 12, 12, 14, 19, 26, 58, 60, 55, // + 14, 13, 16, 24, 40, 57, 69, 56, // + 14, 17, 22, 29, 51, 87, 80, 62, // + 18, 22, 37, 56, 68, 109, 103, 77, // + 24, 35, 55, 64, 81, 104, 113, 92, // + 49, 64, 78, 87, 103, 121, 120, 101, // + 72, 92, 95, 98, 112, 100, 103, 99, +]; + +/// ITU-T T.81 Annex K.2, the example chrominance quantisation table, in natural order. +const ANNEX_K_CHROMA: [u16; 64] = [ + 17, 18, 24, 47, 99, 99, 99, 99, // + 18, 21, 26, 66, 99, 99, 99, 99, // + 24, 26, 56, 99, 99, 99, 99, 99, // + 47, 66, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, +]; + +/// The natural-order index of the k-th coefficient in zig-zag order (T.81 Figure A.6): DQT +/// stores its 64 values in this order. +const ZIGZAG: [usize; 64] = [ + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, + 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, +]; + +/// One quantisation table from a DQT segment, in natural order, with its precision. +#[derive(Debug, Clone, Copy)] +struct Table { + values: [u16; 64], + /// 16-bit entries (Pq = 1) rather than 8-bit. + wide: bool, +} + +/// The quality (1–100, the Independent JPEG Group's scale that Save As and Export As use) a +/// JPEG was most likely saved at, from its quantisation tables; `None` when it has no usable +/// luminance table (or isn't a JPEG). +pub fn jpeg_quality(bytes: &[u8]) -> Option { + let tables = jpeg_tables(bytes); + let [luma, chroma, ..] = tables; + let luma = luma?; + let mut best: Option<(u64, u8)> = None; + for q in 1..=100u8 { + let mut err = table_error(&luma, &ANNEX_K_LUMA, q); + if let Some(c) = chroma { + err = err.saturating_add(table_error(&c, &ANNEX_K_CHROMA, q)); + } + if best.is_none_or(|(e, _)| err < e) { + best = Some((err, q)); + } + } + best.map(|(_, q)| q) +} + +/// The Independent JPEG Group's quality scaling of `base` (libjpeg's `jpeg_quality_scaling` and +/// `jpeg_add_quant_table`, as published with its source): a percentage scale of 5000 / q below +/// quality 50 and 200 − 2q from it, rounded, and kept within 1..=255 (8-bit) or 1..=32767. +fn scaled(base: u16, q: u8, wide: bool) -> u32 { + let q = u32::from(q.clamp(1, 100)); + let scale = if q < 50 { 5000 / q } else { 200 - 2 * q }; + let v = (u32::from(base) * scale + 50) / 100; + v.clamp(1, if wide { 32767 } else { 255 }) +} + +/// How far `t` is from `base` scaled to quality `q`: the summed absolute difference. +fn table_error(t: &Table, base: &[u16; 64], q: u8) -> u64 { + t.values.iter().zip(base).map(|(&v, &b)| u64::from(u32::from(v).abs_diff(scaled(b, q, t.wide)))).sum() +} + +/// The quantisation tables 0..=3 defined before the first scan (a later definition of the same +/// slot replaces an earlier one). +fn jpeg_tables(b: &[u8]) -> [Option; 4] { + let mut tables = [None; 4]; + if b.get(..2) != Some(&[0xFF, 0xD8]) { + return tables; + } + let mut i = 2usize; + while let (Some(&0xFF), Some(&marker)) = (b.get(i), b.get(i.saturating_add(1))) { + if marker == 0xFF { + i = i.saturating_add(1); + continue; + } + if marker == 0xD8 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 { + i = i.saturating_add(2); + continue; + } + if marker == 0xDA || marker == 0xD9 { + break; + } + let Some(len) = b.get(i + 2..i + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { break }; + let Some(seg) = len.checked_sub(2).and_then(|n| b.get(i + 4..i + 4 + n)) else { break }; + if marker == 0xDB { + read_dqt(seg, &mut tables); + } + i = i.saturating_add(2).saturating_add(len); + } + tables +} + +/// The tables of one DQT segment; a truncated or malformed table ends the segment. +fn read_dqt(mut seg: &[u8], tables: &mut [Option
; 4]) { + while let Some((&pq_tq, rest)) = seg.split_first() { + let wide = pq_tq >> 4 == 1; + if pq_tq >> 4 > 1 { + return; + } + let slot = usize::from(pq_tq & 0x0F); + let n = if wide { 128 } else { 64 }; + let Some(data) = rest.get(..n) else { return }; + let mut values = [0u16; 64]; + for (k, &natural) in ZIGZAG.iter().enumerate() { + let v = if wide { data.get(2 * k..2 * k + 2).map_or(0, |p| u16::from_be_bytes([p[0], p[1]])) } else { data.get(k).map_or(0, |&p| u16::from(p)) }; + if let Some(dst) = values.get_mut(natural) { + *dst = v; + } + } + if let Some(dst) = tables.get_mut(slot) { + *dst = Some(Table { values, wide }); + } + seg = rest.get(n..).unwrap_or(&[]); + } +} + +/// How a WebP file's image is compressed. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WebpEncoding { + /// VP8L. + Lossless, + /// VP8, with the quality (0–100, our encoder's scale) its quantiser maps back to, when the + /// frame header could be read. + Lossy { quality: Option }, +} + +/// The compression of a WebP file's (first) image, or `None` when it isn't a WebP or holds no +/// image chunk. +pub fn webp_encoding(bytes: &[u8]) -> Option { + if bytes.get(..4) != Some(b"RIFF") || bytes.get(8..12) != Some(b"WEBP") { + return None; + } + find_image_chunk(bytes.get(12..)?, 0) +} + +/// The first `VP8 ` or `VP8L` chunk among `chunks`, looking inside animation frames. +fn find_image_chunk(mut chunks: &[u8], depth: u8) -> Option { + while let Some(head) = chunks.get(..8) { + let fourcc = head.get(..4)?; + let size = usize::try_from(u32::from_le_bytes([head[4], head[5], head[6], head[7]])).ok()?; + let end = 8usize.checked_add(size)?; + // A truncated last chunk still holds its header. + let payload = chunks.get(8..end.min(chunks.len()))?; + match fourcc { + b"VP8L" => return Some(WebpEncoding::Lossless), + b"VP8 " => return Some(WebpEncoding::Lossy { quality: vp8_quality(payload) }), + // An animation frame: a 16-byte header, then the frame's own chunks. + b"ANMF" if depth == 0 => { + if let Some(found) = payload.get(16..).and_then(|c| find_image_chunk(c, 1)) { + return Some(found); + } + } + _ => {} + } + chunks = chunks.get(end.checked_add(size % 2)?..).unwrap_or(&[]); + } + None +} + +/// The quality a VP8 key frame's base quantiser index maps back to on our encoder's scale (the +/// quality whose index is nearest; the middle one when several share it). +fn vp8_quality(frame: &[u8]) -> Option { + let qi = vp8_base_quantizer(frame)?; + let distance = |q: u8| vp8::quantizer_index(q).abs_diff(qi); + let best = (0..=100u8).map(distance).min()?; + let matches: Vec = (0..=100u8).filter(|&q| distance(q) == best).collect(); + let mid = matches.get(matches.len() / 2)?; + Some(*mid) +} + +/// The base quantiser index of a VP8 key frame: `y_ac_qi` from its frame header (RFC 6386 +/// §9.2–9.6, §19.2), averaged over the segments when segmentation sets absolute quantisers or +/// adjusted by their mean delta otherwise. +fn vp8_base_quantizer(frame: &[u8]) -> Option { + let tag = frame.get(..3)?; + // Bit 0 clear: a key frame, which carries the start code and dimensions. + if tag[0] & 1 != 0 || frame.get(3..6)? != [0x9D, 0x01, 0x2A] { + return None; + } + let mut d = BoolDecoder::new(frame.get(10..)?); + d.literal(1); // color_space + d.literal(1); // clamping_type + let mut segments: Option<(bool, [i32; 4])> = None; + if d.flag() { + let update_map = d.flag(); + if d.flag() { + let absolute = d.flag(); + let mut q = [0i32; 4]; + for v in &mut q { + *v = if d.flag() { d.signed(7) } else { 0 }; + } + for _ in 0..4 { + if d.flag() { + d.signed(6); + } + } + segments = Some((absolute, q)); + } + if update_map { + for _ in 0..3 { + if d.flag() { + d.literal(8); + } + } + } + } + d.literal(1); // filter_type + d.literal(6); // loop_filter_level + d.literal(3); // sharpness_level + if d.flag() && d.flag() { + // loop_filter_adj_enable, mode_ref_lf_delta_update: 4 reference and 4 mode deltas. + for _ in 0..8 { + if d.flag() { + d.signed(6); + } + } + } + d.literal(2); // log2_nbr_of_dct_partitions + if d.exhausted() { + return None; + } + let base = d.literal(7) as i32; + let qi = match segments { + Some((true, q)) => q.iter().sum::() / 4, + Some((false, q)) => base + q.iter().sum::() / 4, + None => base, + }; + u8::try_from(qi.clamp(0, 127)).ok() +} + +/// RFC 6386 §7's boolean decoder, reading zeros past the end of its data. +struct BoolDecoder<'a> { + data: &'a [u8], + pos: usize, + value: u32, + range: u32, + bit_count: u32, + /// Bytes read past the end of the data. + overrun: usize, +} + +impl<'a> BoolDecoder<'a> { + fn new(data: &'a [u8]) -> Self { + let mut d = BoolDecoder { data, pos: 0, value: 0, range: 255, bit_count: 0, overrun: 0 }; + d.value = (d.next_byte() << 8) | d.next_byte(); + d + } + + fn next_byte(&mut self) -> u32 { + match self.data.get(self.pos) { + Some(&b) => { + self.pos += 1; + u32::from(b) + } + None => { + self.overrun = self.overrun.saturating_add(1); + 0 + } + } + } + + /// The header was read from beyond the end of the data (a couple of bytes of look-ahead + /// are normal). + fn exhausted(&self) -> bool { + self.overrun > 2 + } + + fn bool(&mut self, prob: u32) -> bool { + let split = 1 + (((self.range - 1) * prob) >> 8); + let big_split = split << 8; + let bit = if self.value >= big_split { + self.range -= split; + self.value -= big_split; + true + } else { + self.range = split; + false + }; + while self.range < 128 { + self.value <<= 1; + self.range <<= 1; + self.bit_count += 1; + if self.bit_count == 8 { + self.bit_count = 0; + self.value |= self.next_byte(); + } + } + bit + } + + fn flag(&mut self) -> bool { + self.bool(128) + } + + fn literal(&mut self, bits: u32) -> u32 { + (0..bits).fold(0, |v, _| (v << 1) | u32::from(self.flag())) + } + + /// A magnitude of `bits` bits followed by a sign bit. + fn signed(&mut self, bits: u32) -> i32 { + let v = self.literal(bits) as i32; + if self.flag() { -v } else { v } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn scaling_matches_the_published_curve() { + // Quality 50 is the Annex K table itself; 100 is all ones; 75 halves it. + assert_eq!(scaled(16, 50, false), 16); + assert_eq!(scaled(121, 100, false), 1); + assert_eq!(scaled(16, 75, false), 8); + assert_eq!(scaled(121, 1, false), 255); + assert_eq!(scaled(121, 1, true), 6050); + } + + #[test] + fn zigzag_is_a_permutation() { + let mut seen = [false; 64]; + for &n in &ZIGZAG { + seen[n] = true; + } + assert!(seen.iter().all(|&s| s)); + } +} diff --git a/crates/image-codecs/src/web.rs b/crates/image-codecs/src/web.rs new file mode 100644 index 000000000..dfd0cd9b9 --- /dev/null +++ b/crates/image-codecs/src/web.rs @@ -0,0 +1,525 @@ +//! Web-optimised writers for Save for Web: indexed GIF (our own LZW encoder, GIF89a spec), +//! WBMP (WAP Wireless Bitmap, type 0) and baseline/progressive JPEG from 8-bit RGB. +//! +//! Indexed PNG is [`crate::encode_png_indexed`]. Every writer here has a matching reader (GIF +//! through the regular decoder, WBMP through [`decode_wbmp`]), keeping the crate symmetric. + +use std::collections::HashMap; + +use crate::error::CodecError; +use crate::format::Format; + +fn gif_err(msg: impl Into) -> CodecError { + CodecError::InvalidImage(msg.into()) +} + +/// Packs variable-length LZW codes LSB-first into GIF data sub-blocks. +struct BitSink { + bytes: Vec, + acc: u32, + nbits: u32, +} + +impl BitSink { + fn put(&mut self, code: u16, size: u32) { + self.acc |= u32::from(code) << self.nbits; + self.nbits += size; + while self.nbits >= 8 { + self.bytes.push(self.acc as u8); + self.acc >>= 8; + self.nbits -= 8; + } + } + fn finish(mut self) -> Vec { + if self.nbits > 0 { + self.bytes.push(self.acc as u8); + } + self.bytes + } +} + +/// GIF variable-code-length LZW compression of `indices` (each < 2^`min_code_size`). +pub fn lzw_encode(indices: &[u8], min_code_size: u8) -> Vec { + let min = u32::from(min_code_size.clamp(2, 8)); + let clear = 1u16 << min; + let eoi = clear + 1; + let mut size = min + 1; + let mut next = eoi + 1; + let mut dict: HashMap = HashMap::with_capacity(4096); + let mut out = BitSink { bytes: Vec::with_capacity(indices.len() / 2 + 16), acc: 0, nbits: 0 }; + out.put(clear, size); + let Some((&first, rest)) = indices.split_first() else { + out.put(eoi, size); + return out.finish(); + }; + let mut prefix = u16::from(first); + for &k in rest { + let key = u32::from(prefix) << 8 | u32::from(k); + if let Some(&c) = dict.get(&key) { + prefix = c; + continue; + } + out.put(prefix, size); + // The decoder adds its entries one code late, so widen once the next free code no + // longer fits (before assigning it), exactly like the classic compress-based encoders. + if u32::from(next) >= 1 << size && size < 12 { + size += 1; + } + if next < 4096 { + dict.insert(key, next); + next += 1; + } else { + out.put(clear, size); + dict.clear(); + size = min + 1; + next = eoi + 1; + } + prefix = u16::from(k); + } + out.put(prefix, size); + if u32::from(next) >= 1 << size && size < 12 { + size += 1; + } + out.put(eoi, size); + out.finish() +} + +/// Rows in GIF interlaced order (passes every 8th from 0, every 8th from 4, every 4th from 2, +/// every 2nd from 1). +fn interlaced_rows(h: usize) -> Vec { + let mut v = Vec::with_capacity(h); + for (start, step) in [(0, 8), (4, 8), (2, 4), (1, 2)] { + v.extend((start..h).step_by(step)); + } + v +} + +/// An indexed GIF89a: `palette` (1..=256 entries), one index per pixel, optional transparent +/// index (Graphic Control Extension) and interlacing. +pub fn encode_gif_indexed( + width: u32, + height: u32, + indices: &[u8], + palette: &[[u8; 3]], + transparent: Option, + interlaced: bool, +) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + if width == 0 || height == 0 || width > 65535 || height > 65535 || indices.len() != w * h { + return Err(gif_err("GIF size must be 1..=65535 and match the index data")); + } + if palette.is_empty() || palette.len() > 256 || indices.iter().any(|&i| usize::from(i) >= palette.len()) { + return Err(gif_err("palette must have 1..=256 entries covering every index")); + } + // Colour table size: 2^(bits) entries, bits ≥ 1. + let bits = (1..=8u8).find(|b| (1usize << b) >= palette.len()).unwrap_or(8); + let mut out = b"GIF89a".to_vec(); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(0x80 | ((bits - 1) << 4) | (bits - 1)); + out.push(transparent.unwrap_or(0)); + out.push(0); + for i in 0..(1usize << bits) { + out.extend(palette.get(i).copied().unwrap_or([0, 0, 0])); + } + if let Some(t) = transparent { + out.extend([0x21, 0xF9, 0x04, 0x01, 0, 0, t, 0]); + } + out.push(0x2C); + out.extend([0, 0, 0, 0]); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(if interlaced { 0x40 } else { 0 }); + let min = bits.max(2); + out.push(min); + let data = if interlaced { + let mut v = Vec::with_capacity(indices.len()); + for y in interlaced_rows(h) { + v.extend_from_slice(&indices[y * w..(y + 1) * w]); + } + lzw_encode(&v, min) + } else { + lzw_encode(indices, min) + }; + for chunk in data.chunks(255) { + out.push(chunk.len() as u8); + out.extend_from_slice(chunk); + } + out.push(0); + out.push(0x3B); + Ok(out) +} + +/// One frame of an animated GIF: palette indices, its (local) palette, an optional transparent +/// index, and the on-screen delay in centiseconds. +pub struct GifFrame { + pub indices: Vec, + pub palette: Vec<[u8; 3]>, + pub transparent: Option, + pub delay_cs: u16, +} + +/// Encode an animated GIF (GIF89a). Each frame carries its own local colour table; `loop_forever` +/// adds the NETSCAPE2.0 looping extension. +pub fn encode_gif_animated(width: u32, height: u32, frames: &[GifFrame], loop_forever: bool) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + if width == 0 || height == 0 || width > 65535 || height > 65535 { + return Err(gif_err("GIF size must be 1..=65535")); + } + if frames.is_empty() { + return Err(gif_err("an animated GIF needs at least one frame")); + } + let mut out = b"GIF89a".to_vec(); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(0x70); // no global colour table, 8-bit colour resolution + out.push(0); // background colour index + out.push(0); // pixel aspect ratio + if loop_forever { + out.extend([0x21, 0xFF, 0x0B]); + out.extend_from_slice(b"NETSCAPE2.0"); + out.extend([0x03, 0x01, 0x00, 0x00, 0x00]); // loop count 0 = forever + } + for f in frames { + if f.palette.is_empty() || f.palette.len() > 256 || f.indices.len() != w * h || f.indices.iter().any(|&i| usize::from(i) >= f.palette.len()) { + return Err(gif_err("every frame's palette must have 1..=256 entries covering its indices")); + } + let bits = (1..=8u8).find(|b| (1usize << b) >= f.palette.len()).unwrap_or(8); + // Graphic Control Extension: delay and optional transparency. Every frame covers the whole + // image, so an opaque one is left in place (disposal 1), while one with a transparent index + // is cleared afterwards (disposal 2): otherwise the frames before it show through. + let packed = if f.transparent.is_some() { 0x09 } else { 0x04 }; + out.extend([0x21, 0xF9, 0x04, packed]); + out.extend(f.delay_cs.to_le_bytes()); + out.push(f.transparent.unwrap_or(0)); + out.push(0); + // Image Descriptor with a Local Colour Table. + out.push(0x2C); + out.extend([0, 0, 0, 0]); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(0x80 | (bits - 1)); + for i in 0..(1usize << bits) { + out.extend(f.palette.get(i).copied().unwrap_or([0, 0, 0])); + } + let min = bits.max(2); + out.push(min); + for chunk in lzw_encode(&f.indices, min).chunks(255) { + out.push(chunk.len() as u8); + out.extend_from_slice(chunk); + } + out.push(0); + } + out.push(0x3B); + Ok(out) +} + +fn put_multibyte(out: &mut Vec, v: u32) { + let mut groups = vec![(v & 0x7f) as u8]; + let mut v = v >> 7; + while v > 0 { + groups.push((v & 0x7f) as u8 | 0x80); + v >>= 7; + } + out.extend(groups.iter().rev()); +} + +/// WBMP type 0 (1 bit per pixel, rows padded to bytes, 1 = white). `white[i]` per pixel. +pub fn encode_wbmp(width: u32, height: u32, white: &[bool]) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + if width == 0 || height == 0 || white.len() != w * h { + return Err(CodecError::InvalidImage("WBMP size must match the pixel data".into())); + } + let mut out = vec![0u8, 0u8]; + put_multibyte(&mut out, width); + put_multibyte(&mut out, height); + let stride = w.div_ceil(8); + for y in 0..h { + let mut row = vec![0u8; stride]; + for x in 0..w { + if white[y * w + x] { + row[x / 8] |= 0x80 >> (x % 8); + } + } + out.extend(row); + } + Ok(out) +} + +/// Reads a WBMP type 0 image: (width, height, white per pixel). +pub fn decode_wbmp(bytes: &[u8]) -> Result<(u32, u32, Vec), CodecError> { + let bad = || CodecError::InvalidImage("not a type 0 WBMP".into()); + let mut i = 0usize; + let mut next = || -> Result { + let b = *bytes.get(i).ok_or_else(bad)?; + i += 1; + Ok(b) + }; + if next()? != 0 || next()? & 0x9f != 0 { + return Err(bad()); + } + let mut mb = || -> Result { + let mut v: u32 = 0; + for _ in 0..5 { + let b = next()?; + v = v.checked_mul(128).ok_or_else(bad)? | u32::from(b & 0x7f); + if b & 0x80 == 0 { + return Ok(v); + } + } + Err(bad()) + }; + let (w, h) = (mb()?, mb()?); + if w == 0 || h == 0 || u64::from(w) * u64::from(h) > 1 << 28 { + return Err(bad()); + } + let stride = (w as usize).div_ceil(8); + let data = bytes.get(i..i + stride * h as usize).ok_or_else(bad)?; + let mut px = Vec::with_capacity(w as usize * h as usize); + for y in 0..h as usize { + for x in 0..w as usize { + px.push(data[y * stride + x / 8] & (0x80 >> (x % 8)) != 0); + } + } + Ok((w, h, px)) +} + +/// JPEG from 8-bit RGB (`rgb.len() == 3·w·h`): quality 1..=100, optional progressive scans and +/// optimised Huffman tables (Save for Web's "Optimized"), optional ICC profile, density, EXIF and +/// XMP. EXIF that doesn't fit in one segment is left out (see [`jpeg_exif_fits`]). +#[allow(clippy::too_many_arguments)] +pub fn encode_jpeg_rgb8( + width: u32, + height: u32, + rgb: &[u8], + quality: u8, + progressive: bool, + optimized: bool, + icc: Option<&[u8]>, + dpi: Option, + exif: Option<&[u8]>, + xmp: Option<&str>, +) -> Result, CodecError> { + let e = |e: jpeg_encoder::EncodingError| CodecError::encode(Format::Jpeg, e); + let (Ok(w16), Ok(h16)) = (u16::try_from(width), u16::try_from(height)) else { + return Err(CodecError::encode(Format::Jpeg, "JPEG dimensions are limited to 65535")); + }; + if rgb.len() != width as usize * height as usize * 3 { + return Err(CodecError::InvalidImage("RGB data does not match the size".into())); + } + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, quality.clamp(1, 100)); + // Like Photoshop's Save for Web: 4:2:0 chroma up to quality 50, 4:4:4 from 51 (measured on + // Photoshop 2025's output; its Save As keeps 4:2:0 at every quality). + let subsampled = quality <= 50; + enc.set_progressive(progressive); + // jpeg-encoder 0.6 writes corrupt baseline scans when optimised Huffman tables meet 4:2:0 + // (both zune-jpeg and image-rs decode them green); progressive output is fine. + enc.set_optimized_huffman_tables(progressive || (optimized && !subsampled)); + enc.set_sampling_factor(if subsampled { jpeg_encoder::SamplingFactor::R_4_2_0 } else { jpeg_encoder::SamplingFactor::R_4_4_4 }); + if let Some(d) = dpi.filter(|d| *d >= 1.0) { + let v = d.round().min(65535.0) as u16; + enc.set_density(jpeg_encoder::Density::Inch { x: v, y: v }); + } + let ppi = dpi.filter(|d| *d >= 1.0).map(|d| (d, d)); + // Upright, without the source's thumbnail, at the JFIF resolution, as Save As writes it. + if let Some(seg) = exif.and_then(|x| crate::codecs::jpeg::exif_segment(x, ppi)) { + enc.add_app_segment(1, &seg).map_err(e)?; + } + if let Some(x) = xmp { + // Its `tiff:` resolution, if any, says what the JFIF density says (#1691). + let x = crate::resolution::export_xmp(x, ppi); + let mut seg = b"http://ns.adobe.com/xap/1.0/\0".to_vec(); + seg.extend_from_slice(x.as_bytes()); + enc.add_app_segment(1, &seg).map_err(e)?; + } + if let Some(icc) = icc { + enc.add_icc_profile(icc).map_err(e)?; + } + enc.encode(rgb, w16, h16, jpeg_encoder::ColorType::Rgb).map_err(e)?; + Ok(out) +} + +/// Whether [`encode_jpeg_rgb8`] can write `exif` at `dpi` (it must fit in one APP1 segment). +pub fn jpeg_exif_fits(exif: &[u8], dpi: Option) -> bool { + crate::codecs::jpeg::exif_segment(exif, dpi.filter(|d| *d >= 1.0).map(|d| (d, d))).is_some() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn decode_rgba(bytes: &[u8]) -> (u32, u32, Vec) { + let img = crate::decode(bytes).unwrap(); + let (w, h) = img.dimensions(); + (w, h, img.convert(crate::ChannelLayout::Rgba, crate::SampleType::U8).data().to_vec()) + } + + fn pattern(w: usize, h: usize, n: usize) -> Vec { + (0..w * h).map(|i| (((i % w) * 7 + (i / w) * 13 + (i * i) % 5) % n) as u8).collect() + } + + /// The luma sampling factors (h, v) of a baseline or progressive JPEG. + fn luma_sampling(bytes: &[u8]) -> Option<(u8, u8)> { + let i = bytes.windows(2).position(|w| w[0] == 0xFF && matches!(w[1], 0xC0..=0xC2))?; + let f = *bytes.get(i + 11)?; + Some((f >> 4, f & 15)) + } + + #[test] + fn jpeg_chroma_is_full_above_quality_50() { + let rgb: Vec = (0..16 * 16 * 3).map(|i| (i * 37 % 256) as u8).collect(); + for (q, want) in [(1, (2, 2)), (50, (2, 2)), (51, (1, 1)), (60, (1, 1)), (89, (1, 1)), (100, (1, 1))] { + for progressive in [false, true] { + let bytes = encode_jpeg_rgb8(16, 16, &rgb, q, progressive, false, None, None, None, None).unwrap(); + assert_eq!(luma_sampling(&bytes), Some(want), "quality {q} progressive {progressive}"); + } + } + } + + #[test] + fn gif_roundtrips_through_the_decoder() { + let pal: Vec<[u8; 3]> = (0..200).map(|i| [i as u8, (255 - i) as u8, (i * 3 % 256) as u8]).collect(); + for (w, h, interlaced) in [(1, 1, false), (37, 23, false), (37, 23, true), (300, 200, false)] { + let idx = pattern(w, h, pal.len()); + let bytes = encode_gif_indexed(w as u32, h as u32, &idx, &pal, None, interlaced).unwrap(); + let (dw, dh, px) = decode_rgba(&bytes); + assert_eq!((dw, dh), (w as u32, h as u32)); + for (i, k) in idx.iter().enumerate() { + assert_eq!(&px[i * 4..i * 4 + 3], &pal[*k as usize], "pixel {i} of {w}x{h} interlaced={interlaced}"); + } + } + } + + #[test] + fn gif_small_palettes_and_transparency() { + let pal = [[255, 255, 255], [0, 0, 0], [255, 0, 0]]; + let idx: Vec = (0..64 * 64).map(|i| (i % 3) as u8).collect(); + let bytes = encode_gif_indexed(64, 64, &idx, &pal, Some(2), false).unwrap(); + let (_, _, px) = decode_rgba(&bytes); + assert_eq!(px[3], 255); + assert_eq!(px[2 * 4 + 3], 0, "index 2 is transparent"); + // Two-colour image: minimum code size 2. + let bw: Vec = (0..1000).map(|i| u8::from(i % 7 == 0)).collect(); + let bytes = encode_gif_indexed(100, 10, &bw, &pal[..2], None, false).unwrap(); + let (_, _, px) = decode_rgba(&bytes); + assert_eq!(px[0], 0, "index 1 = black at x 0"); + assert!(encode_gif_indexed(2, 2, &[0, 1, 2, 3], &pal, None, false).is_err()); + } + + #[test] + fn gif_long_runs_hit_the_dictionary_limit() { + // Noise forces many codes and dictionary resets; flat areas make long strings. + let mut seed = 12345u32; + let idx: Vec = (0..512 * 512) + .map(|i| { + seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12345); + if (i / 512) % 64 < 32 { (seed >> 16) as u8 } else { 7 } + }) + .collect(); + let pal: Vec<[u8; 3]> = (0..256).map(|i| [i as u8, i as u8, 255 - i as u8]).collect(); + let bytes = encode_gif_indexed(512, 512, &idx, &pal, None, false).unwrap(); + let (_, _, px) = decode_rgba(&bytes); + for (i, k) in idx.iter().enumerate().step_by(97) { + assert_eq!(px[i * 4], *k); + } + } + + #[test] + fn wbmp_roundtrips() { + let px: Vec = (0..13 * 5).map(|i| i % 3 == 0).collect(); + let bytes = encode_wbmp(13, 5, &px).unwrap(); + assert_eq!(&bytes[..4], &[0, 0, 13, 5]); + assert_eq!(decode_wbmp(&bytes).unwrap(), (13, 5, px)); + let big = encode_wbmp(300, 1, &[true; 300]).unwrap(); + assert_eq!(&big[2..4], &[0x82, 0x2c], "300 as a multi-byte integer"); + assert!(decode_wbmp(&big[..10]).is_err()); + assert!(decode_wbmp(&[1, 0, 1, 1, 0]).is_err()); + } + + #[test] + fn progressive_jpeg_decodes() { + let rgb: Vec = (0..64 * 48 * 3).map(|i| (i % 251) as u8).collect(); + let base = encode_jpeg_rgb8(64, 48, &rgb, 80, false, false, None, Some(72.0), None, Some("")).unwrap(); + let prog = encode_jpeg_rgb8(64, 48, &rgb, 80, true, true, None, None, None, None).unwrap(); + assert_ne!(base, prog); + assert_eq!(crate::decode(&base).unwrap().meta.xmp.as_deref(), Some("")); + // SOF2 marks a progressive frame. + assert!(prog.windows(2).any(|w| w == [0xFF, 0xC2])); + assert_eq!(crate::decode(&prog).unwrap().dimensions(), (64, 48)); + } + + /// #1691: the XMP resolution of a Save for Web JPEG says what its JFIF density says. + #[test] + fn web_jpeg_xmp_resolution_follows_the_density() { + let rgb = vec![100u8; 8 * 8 * 3]; + let xmp = r#""#; + let out = encode_jpeg_rgb8(8, 8, &rgb, 80, false, false, None, Some(72.0), None, Some(xmp)).unwrap(); + let img = crate::decode(&out).unwrap(); + assert_eq!(img.meta.dpi, Some((72.0, 72.0))); + assert_eq!(img.meta.xmp.as_deref().and_then(crate::xmp_resolution), Some((72.0, 72.0))); + } +} + +#[cfg(test)] +mod subsampling_tests { + use super::*; + + /// jpeg-encoder 0.6 writes broken scans for baseline 4:2:0 with optimised Huffman tables; + /// `encode_jpeg_rgb8` must avoid that combination. + #[test] + fn every_option_combination_decodes_to_the_source() { + let (w, h) = (64u32, 48u32); + let rgb: Vec = (0..w * h).flat_map(|i| [((i % w) * 4) as u8, 76, ((i / w) * 5) as u8]).collect(); + for q in [30, 60, 95] { + for (prog, opt) in [(false, false), (false, true), (true, false), (true, true)] { + let bytes = encode_jpeg_rgb8(w, h, &rgb, q, prog, opt, None, None, None, None).unwrap(); + let ours = crate::decode(&bytes).unwrap().convert(crate::ChannelLayout::Rgb, crate::SampleType::U8); + let err = ours.data().iter().zip(&rgb).map(|(a, b)| i32::from(*a).abs_diff(i32::from(*b))).max().unwrap(); + assert!(err < 24, "q{q} progressive={prog} optimized={opt}: max error {err}"); + } + } + } +} + +#[cfg(test)] +mod anim_tests { + use super::*; + + #[test] + fn animated_gif_has_header_and_frames() { + let frames: Vec = (0..3) + .map(|i| GifFrame { indices: vec![i as u8; 4], palette: vec![[0, 0, 0], [255, 0, 0], [0, 255, 0]], transparent: None, delay_cs: 4 }) + .collect(); + let g = encode_gif_animated(2, 2, &frames, true).unwrap(); + assert_eq!(&g[..6], b"GIF89a"); + assert_eq!(*g.last().unwrap(), 0x3B); + // Three image descriptors (0x2C) for three frames. + assert_eq!(g.iter().filter(|&&b| b == 0x2C).count(), 3); + // NETSCAPE loop extension present. + assert!(g.windows(11).any(|w| w == b"NETSCAPE2.0")); + // First frame still decodes. + let img = crate::decode(&g).unwrap(); + assert_eq!(img.dimensions(), (2, 2)); + } + + #[test] + fn transparent_frames_are_cleared_before_the_next() { + let frame = |transparent| GifFrame { indices: vec![0, 1, 1, 1], palette: vec![[0, 0, 0], [255, 0, 0]], transparent, delay_cs: 4 }; + // The Graphic Control Extension's packed byte follows its 0x21 0xF9 0x04 introducer. + let packed = |g: &[u8]| g.windows(4).find(|w| w[..3] == [0x21, 0xF9, 0x04]).map(|w| w[3]).unwrap(); + let opaque = encode_gif_animated(2, 2, &[frame(None)], false).unwrap(); + assert_eq!(packed(&opaque), 0x04, "disposal 1, no transparency"); + let clear = encode_gif_animated(2, 2, &[frame(Some(0))], false).unwrap(); + assert_eq!(packed(&clear), 0x09, "disposal 2 with the transparency flag"); + let px = crate::decode(&clear).unwrap().to_rgba8(); + assert_eq!((px[3], &px[4..8]), (0, &[255, 0, 0, 255][..]), "index 0 transparent, index 1 red"); + } + + #[test] + fn rejects_bad_frames() { + assert!(encode_gif_animated(2, 2, &[], false).is_err()); + let bad = GifFrame { indices: vec![0; 3], palette: vec![[0, 0, 0]], transparent: None, delay_cs: 1 }; + assert!(encode_gif_animated(2, 2, &[bad], false).is_err()); + } +} diff --git a/crates/image-codecs/tests/it/camera_raw.rs b/crates/image-codecs/tests/it/camera_raw.rs new file mode 100644 index 000000000..a650f3506 --- /dev/null +++ b/crates/image-codecs/tests/it/camera_raw.rs @@ -0,0 +1,134 @@ +//! Camera raw files are TIFF-structured (TIFF/EP, DNG, CR2, NEF, ARW…) but +//! hold sensor data this crate does not develop. Opening one must say so, +//! not fail with a TIFF decoder detail or silently return the embedded +//! preview. The files here are tiny synthetic TIFFs with the structural +//! markers from the public TIFF/EP and DNG specifications. + +use craft_image_codecs::*; + +const SHORT: u16 = 3; +const LONG: u16 = 4; +const BYTE4: u16 = 1; // BYTE, written with count 4 (the value's 4 bytes inline) +const CFA: u32 = 32803; // TIFF/EP PhotometricInterpretation: colour filter array +const LINEAR_RAW: u32 = 34892; // DNG PhotometricInterpretation: LinearRaw + +/// A little-endian TIFF with the given IFDs (each a list of +/// `(tag, type, value)` entries with count 1), chained in order. Every IFD +/// gets 2×2 8-bit strip data. `header8` replaces bytes 8..12 (CR2 puts +/// "CR" + version there and starts IFD0 at 16). +fn tiff(ifds: &[Vec<(u16, u16, u32)>], header8: Option<[u8; 4]>) -> Vec { + let mut b = b"II*\0".to_vec(); + let first = if header8.is_some() { 16u32 } else { 8 }; + b.extend_from_slice(&first.to_le_bytes()); + if let Some(h) = header8 { + b.extend_from_slice(&h); + b.extend_from_slice(&0u32.to_le_bytes()); + } + // Layout: IFDs back to back, then one shared 12-byte pixel strip. + let sizes: Vec = ifds.iter().map(|e| 2 + 12 * (e.len() as u32 + 1) + 4).collect(); + let strip = first + sizes.iter().sum::(); + let mut at = first; + for (i, entries) in ifds.iter().enumerate() { + let mut entries = entries.clone(); + entries.push((273, LONG, strip)); // StripOffsets + entries.sort_by_key(|e| e.0); + b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, ty, v) in entries { + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + let count: u32 = if ty == BYTE4 { 4 } else { 1 }; + b.extend_from_slice(&count.to_le_bytes()); + if ty == SHORT { + b.extend_from_slice(&(v as u16).to_le_bytes()); + b.extend_from_slice(&[0, 0]); + } else { + b.extend_from_slice(&v.to_le_bytes()); + } + } + at += sizes[i]; + let next = if i + 1 < ifds.len() { at } else { 0 }; + b.extend_from_slice(&next.to_le_bytes()); + } + b.extend_from_slice(&[128; 12]); + b +} + +/// A 2×2 image IFD with the given photometric interpretation. +fn image_ifd(photometric: u32, samples: u32) -> Vec<(u16, u16, u32)> { + vec![ + (256, SHORT, 2), // ImageWidth + (257, SHORT, 2), // ImageLength + (258, SHORT, 8), // BitsPerSample + (259, SHORT, 1), // Compression: none + (262, SHORT, photometric), // PhotometricInterpretation + (277, SHORT, samples), // SamplesPerPixel + (278, SHORT, 2), // RowsPerStrip + (279, LONG, 4 * samples), // StripByteCounts + ] +} + +fn assert_camera_raw(bytes: &[u8]) { + assert_eq!(detect(bytes), Some(Format::Tiff)); + match decode(bytes) { + Err(CodecError::Unsupported { reason, .. }) => { + assert!(reason.contains("camera raw"), "unexpected reason: {reason}"); + } + Err(e) => panic!("expected a camera raw error, got: {e}"), + Ok(img) => panic!("expected a camera raw error, decoded {:?}", img.dimensions()), + } +} + +#[test] +fn cfa_image_is_reported_as_camera_raw() { + // TIFF/EP-style raw (NEF/ARW-like): the main image is a CFA. + assert_camera_raw(&tiff(&[image_ifd(CFA, 1)], None)); +} + +#[test] +fn linear_raw_image_is_reported_as_camera_raw() { + assert_camera_raw(&tiff(&[image_ifd(LINEAR_RAW, 3)], None)); +} + +#[test] +fn raw_behind_a_preview_is_reported_as_camera_raw() { + // IFD0 is a small RGB preview, the CFA raw is a SubIFD (tag 330): decoding + // must not quietly return the preview as the photo. + let mut ifd0 = image_ifd(2, 3); + let ifd0_size = 2 + 12 * (ifd0.len() as u32 + 2) + 4; + ifd0.push((330, LONG, 8 + ifd0_size)); // SubIFDs → the next IFD + let mut b = tiff(&[ifd0, image_ifd(CFA, 1)], None); + // Unchain the raw IFD from IFD0's next-IFD pointer so it is reachable only + // as a SubIFD. + let next_at = 8 + ifd0_size as usize - 4; + b[next_at..next_at + 4].copy_from_slice(&0u32.to_le_bytes()); + assert_camera_raw(&b); +} + +#[test] +fn dng_is_reported_as_camera_raw() { + let mut ifd0 = image_ifd(2, 3); + ifd0.push((50706, BYTE4, 0x0000_0401)); // DNGVersion 1.4.0.0 + assert_camera_raw(&tiff(&[ifd0], None)); +} + +#[test] +fn cr2_is_reported_as_camera_raw() { + // CR2: TIFF header + "CR", major 2, minor 0 at byte 8; IFD0 at 16. + assert_camera_raw(&tiff(&[image_ifd(6, 3)], Some(*b"CR\x02\0"))); +} + +#[test] +fn ordinary_tiffs_still_decode() { + let rgb = decode(&tiff(&[image_ifd(2, 3)], None)).unwrap(); + assert_eq!(rgb.dimensions(), (2, 2)); + let gray = decode(&tiff(&[image_ifd(1, 1)], None)).unwrap(); + assert_eq!(gray.dimensions(), (2, 2)); +} + +#[test] +fn truncated_raw_markers_do_not_panic() { + let b = tiff(&[image_ifd(CFA, 1)], Some(*b"CR\x02\0")); + for n in 0..b.len() { + let _ = decode(&b[..n]); + } +} diff --git a/crates/image-codecs/tests/it/common/mod.rs b/crates/image-codecs/tests/it/common/mod.rs new file mode 100644 index 000000000..6b03752f3 --- /dev/null +++ b/crates/image-codecs/tests/it/common/mod.rs @@ -0,0 +1,122 @@ +//! Shared helpers for integration tests: deterministic PRNG, synthetic +//! image generators, comparison metrics. +#![allow(dead_code)] + +pub mod tiffgen; + +use craft_image_codecs::*; + +/// SplitMix64 — tiny deterministic PRNG. +pub struct Rng(u64); + +impl Rng { + pub fn new(seed: u64) -> Self { + Rng(seed) + } + pub fn next_u64(&mut self) -> u64 { + self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = self.0; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + /// Uniform in [0, 1). + pub fn f32(&mut self) -> f32 { + (self.next_u64() >> 40) as f32 / (1u64 << 24) as f32 + } + pub fn bytes(&mut self, n: usize) -> Vec { + (0..n).map(|_| self.next_u64() as u8).collect() + } +} + +/// Gradient + noise image with values in [0, 1]. `noise` is the amplitude +/// of uniform noise added to the gradient. Alpha is a separate gradient +/// that includes fully transparent and fully opaque regions. +pub fn synth(w: u32, h: u32, layout: ChannelLayout, sample: SampleType, seed: u64, noise: f32) -> Image { + let mut rng = Rng::new(seed); + let nc = layout.channels(); + let mut v = Vec::with_capacity((w * h) as usize * nc); + for y in 0..h { + for x in 0..w { + let fx = x as f32 / (w.max(2) - 1) as f32; + let fy = y as f32 / (h.max(2) - 1) as f32; + for c in 0..nc { + let is_alpha = layout.has_alpha() && c == nc - 1; + let base = if is_alpha { + ((fx + fy) * 0.75 - 0.25).clamp(0.0, 1.0) + } else { + match c % 4 { + 0 => fx, + 1 => fy, + 2 => 1.0 - fx * 0.5 - fy * 0.5, + _ => (fx * fy).sqrt(), + } + }; + let n = (rng.f32() - 0.5) * noise; + v.push((base + n).clamp(0.0, 1.0)); + } + } + } + Image::from_normalized(w, h, layout, sample, &v).unwrap() +} + +/// Default test image: 37x23 with moderate noise. +pub fn test_image(layout: ChannelLayout, sample: SampleType) -> Image { + synth(37, 23, layout, sample, 0xC0DEC, 0.2) +} + +/// PSNR in dB over normalized samples (clamped to [0,1]). +pub fn psnr(a: &Image, b: &Image) -> f64 { + assert_eq!(a.dimensions(), b.dimensions()); + assert_eq!(a.layout(), b.layout()); + let (av, bv) = (a.to_normalized(), b.to_normalized()); + let mse: f64 = av + .iter() + .zip(&bv) + .map(|(x, y)| { + let d = (x.clamp(0.0, 1.0) - y.clamp(0.0, 1.0)) as f64; + d * d + }) + .sum::() + / av.len() as f64; + if mse == 0.0 { f64::INFINITY } else { 10.0 * (1.0 / mse).log10() } +} + +pub fn max_abs_diff(a: &Image, b: &Image) -> f32 { + a.to_normalized().iter().zip(b.to_normalized()).map(|(x, y)| (x - y).abs()).fold(0.0, f32::max) +} + +/// Formats that can be written in this build. +pub fn writable_formats() -> Vec { + Format::ALL.into_iter().filter(|f| caps(*f).write).collect() +} + +/// Formats that can be written *and* read in this build. +pub fn rw_formats() -> Vec { + Format::ALL.into_iter().filter(|f| caps(*f).write && caps(*f).read).collect() +} + +/// A minimal valid little-endian TIFF-structured EXIF blob (one IFD with +/// an Orientation entry). +pub fn sample_exif() -> Vec { + let mut v = b"II*\0\x08\0\0\0".to_vec(); + v.extend_from_slice(&1u16.to_le_bytes()); // 1 entry + v.extend_from_slice(&0x0112u16.to_le_bytes()); // Orientation + v.extend_from_slice(&3u16.to_le_bytes()); // SHORT + v.extend_from_slice(&1u32.to_le_bytes()); + v.extend_from_slice(&1u32.to_le_bytes()); + v.extend_from_slice(&0u32.to_le_bytes()); // next IFD + v +} + +/// Fake ICC profile of `len` bytes (header-ish prefix + deterministic noise). +pub fn sample_icc(len: usize) -> Vec { + let mut v = Rng::new(len as u64).bytes(len); + v[..4].copy_from_slice(&(len as u32).to_be_bytes()); + if len >= 40 { + v[36..40].copy_from_slice(b"acsp"); + } + v +} + +pub const SAMPLE_XMP: &str = ""; diff --git a/crates/image-codecs/tests/it/common/tiffgen.rs b/crates/image-codecs/tests/it/common/tiffgen.rs new file mode 100644 index 000000000..84f0e36d2 --- /dev/null +++ b/crates/image-codecs/tests/it/common/tiffgen.rs @@ -0,0 +1,327 @@ +//! A tiny hand-rolled TIFF / BigTIFF writer for tests: any byte order, strips or tiles, any +//! planar configuration, several IFDs in a chain plus free-standing ones (SubIFDs), and access +//! to every offset so tests can corrupt the structure precisely. +#![allow(dead_code, clippy::too_many_arguments)] + +/// A tag value: a number or a reference to another directory's offset (for SubIFDs). +#[derive(Clone, Copy, Debug)] +pub enum V { + N(u64), + Ifd(usize), +} + +/// TIFF field types used by the tests. +pub const BYTE: u16 = 1; +pub const ASCII: u16 = 2; +pub const SHORT: u16 = 3; +pub const LONG: u16 = 4; +pub const RATIONAL: u16 = 5; +pub const UNDEFINED: u16 = 7; +pub const IFD: u16 = 13; +pub const LONG8: u16 = 16; +pub const IFD8: u16 = 18; + +fn type_size(ty: u16) -> usize { + match ty { + BYTE | ASCII | UNDEFINED | 6 => 1, + SHORT | 8 => 2, + LONG | IFD | 9 | 11 => 4, + RATIONAL | 10 | 12 | LONG8 | 17 | IFD8 => 8, + _ => 1, + } +} + +#[derive(Clone, Debug, Default)] +pub struct Dir { + /// `(tag, type, values)`; strip / tile offsets and byte counts are added from `chunks`. + pub entries: Vec<(u16, u16, Vec)>, + /// Pixel data, one blob per strip or tile. + pub chunks: Vec>, + /// Tiles (322/323 must be in `entries`) instead of strips. + pub tiled: bool, +} + +impl Dir { + pub fn tag(mut self, tag: u16, ty: u16, values: &[u64]) -> Self { + self.entries.push((tag, ty, values.iter().map(|&v| V::N(v)).collect())); + self + } + pub fn bytes(mut self, tag: u16, ty: u16, data: &[u8]) -> Self { + self.entries.push((tag, ty, data.iter().map(|&v| V::N(u64::from(v))).collect())); + self + } + pub fn sub_ifds(mut self, ty: u16, dirs: &[usize]) -> Self { + self.entries.push((330, ty, dirs.iter().map(|&d| V::Ifd(d)).collect())); + self + } +} + +/// A basic directory: `w`×`h`, `spp` samples of `bits` bits, the given photometric, chunky, +/// uncompressed, one strip per `rows` rows holding `data` (already in file byte order). +pub fn strips(w: u32, h: u32, bits: u16, spp: u16, photometric: u16, rows: u32, data: &[u8]) -> Dir { + let row = (w as usize * bits as usize * spp as usize).div_ceil(8); + let chunks = data.chunks((row * rows as usize).max(1)).map(<[u8]>::to_vec).collect(); + Dir { entries: base(w, h, bits, spp, photometric), chunks, tiled: false }.tag(278, LONG, &[u64::from(rows)]) +} + +pub fn base(w: u32, h: u32, bits: u16, spp: u16, photometric: u16) -> Vec<(u16, u16, Vec)> { + vec![ + (256, LONG, vec![V::N(u64::from(w))]), + (257, LONG, vec![V::N(u64::from(h))]), + (258, SHORT, vec![V::N(u64::from(bits)); spp as usize]), + (259, SHORT, vec![V::N(1)]), + (262, SHORT, vec![V::N(u64::from(photometric))]), + (277, SHORT, vec![V::N(u64::from(spp))]), + ] +} + +/// Where things ended up in a built file. +#[derive(Debug, Default)] +pub struct Built { + pub bytes: Vec, + /// Offset of each directory (`dirs` order). + pub ifd_at: Vec, + /// Offset of each directory's next-IFD pointer field. + pub next_at: Vec, + /// Offset of each chunk's data, per directory. + pub chunk_at: Vec>, +} + +/// Builds a file. `chain` lists the directories of the main IFD chain in order; directories not +/// in it are only reachable through SubIFD references. +pub fn build(little: bool, big: bool, dirs: &[Dir], chain: &[usize]) -> Built { + let e16 = |v: u16| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let e32 = |v: u32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let e64 = |v: u64| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let enc = |ty: u16, v: u64| -> Vec { + match type_size(ty) { + 1 => vec![v as u8], + 2 => e16(v as u16), + 4 => e32(v as u32), + _ => e64(v), + } + }; + let header = if big { 16 } else { 8 }; + let entry_len = if big { 20 } else { 12 }; + let inline = if big { 8 } else { 4 }; + let off_ty = if big { LONG8 } else { LONG }; + + // Complete entry lists (with chunk offsets / byte counts), sorted by tag. + let mut full: Vec)>> = Vec::new(); + for d in dirs { + let mut e = d.entries.clone(); + let (ot, ct) = if d.tiled { (324, 325) } else { (273, 279) }; + if !d.chunks.is_empty() || !e.iter().any(|x| x.0 == ot) { + e.retain(|x| x.0 != ot && x.0 != ct); + e.push((ot, off_ty, vec![V::N(0); d.chunks.len()])); + e.push((ct, off_ty, d.chunks.iter().map(|c| V::N(c.len() as u64)).collect())); + } + e.sort_by_key(|x| x.0); + full.push(e); + } + + // Pass 1: positions. Layout: header, then per directory its chunks, its out-of-line values + // and its table. + let mut pos = header as u64; + let mut ifd_at = Vec::new(); + let mut chunk_at = Vec::new(); + for (d, e) in dirs.iter().zip(&full) { + let mut ca = Vec::new(); + for c in &d.chunks { + ca.push(pos); + pos += c.len() as u64; + } + chunk_at.push(ca); + for (_, ty, vals) in e { + let n = type_size(*ty) * vals.len(); + if n > inline { + pos += n as u64; + } + } + pos += pos % 2; // word alignment + ifd_at.push(pos); + pos += (if big { 8 } else { 2 }) + (e.len() * entry_len) as u64 + if big { 8 } else { 4 }; + } + + // Pass 2: bytes. + let mut f = Vec::new(); + f.extend_from_slice(if little { b"II" } else { b"MM" }); + let first = chain.first().map_or(0, |&i| ifd_at[i]); + if big { + f.extend(e16(43)); + f.extend(e16(8)); + f.extend(e16(0)); + f.extend(e64(first)); + } else { + f.extend(e16(42)); + f.extend(e32(first as u32)); + } + let mut next_at = vec![0; dirs.len()]; + for (k, (d, e)) in dirs.iter().zip(&full).enumerate() { + for c in &d.chunks { + f.extend_from_slice(c); + } + let (ot, _) = if d.tiled { (324, 325) } else { (273, 279) }; + let resolve = |tag: u16, i: usize, v: V| -> u64 { + if tag == ot && !d.chunks.is_empty() { + return chunk_at[k][i]; + } + match v { + V::N(n) => n, + V::Ifd(j) => ifd_at[j], + } + }; + let mut table = Vec::new(); + table.extend(if big { e64(e.len() as u64) } else { e16(e.len() as u16) }); + for (tag, ty, vals) in e { + let mut data = Vec::new(); + for (i, v) in vals.iter().enumerate() { + data.extend(enc(*ty, resolve(*tag, i, *v))); + } + table.extend(e16(*tag)); + table.extend(e16(*ty)); + table.extend(if big { e64(vals.len() as u64) } else { e32(vals.len() as u32) }); + if data.len() <= inline { + data.resize(inline, 0); + table.extend(data); + } else { + let at = f.len() as u64; + f.extend(data); + table.extend(if big { e64(at) } else { e32(at as u32) }); + } + } + if f.len() % 2 == 1 { + f.push(0); + } + assert_eq!(f.len() as u64, ifd_at[k]); + let next = chain.iter().position(|&c| c == k).and_then(|p| chain.get(p + 1)).map_or(0, |&n| ifd_at[n]); + f.extend(table); + next_at[k] = f.len() as u64; + f.extend(if big { e64(next) } else { e32(next as u32) }); + } + Built { bytes: f, ifd_at, next_at, chunk_at } +} + +/// Overwrites the next-IFD pointer of directory `k` (for cycles and dangling pointers). +pub fn set_next(b: &mut Built, little: bool, big: bool, k: usize, to: u64) { + let at = b.next_at[k] as usize; + let v = if big { + if little { to.to_le_bytes().to_vec() } else { to.to_be_bytes().to_vec() } + } else if little { + (to as u32).to_le_bytes().to_vec() + } else { + (to as u32).to_be_bytes().to_vec() + }; + b.bytes[at..at + v.len()].copy_from_slice(&v); +} + +/// Locates entry `tag` of directory `k`: `(entry position, type, count, value position)`. +pub fn find_entry(b: &Built, little: bool, big: bool, k: usize, tag: u16) -> Option<(usize, u16, u64, usize)> { + let f = &b.bytes; + let rd = |at: usize, n: usize| -> u64 { + let s = &f[at..at + n]; + let mut v = 0u64; + for i in 0..n { + let byte = if little { s[n - 1 - i] } else { s[i] }; + v = (v << 8) | u64::from(byte); + } + v + }; + let at = b.ifd_at[k] as usize; + let (n, first, entry, inline) = if big { (rd(at, 8), at + 8, 20, 8) } else { (rd(at, 2), at + 2, 12, 4) }; + for i in 0..n as usize { + let e = first + i * entry; + if rd(e, 2) as u16 != tag { + continue; + } + let ty = rd(e + 2, 2) as u16; + let count = if big { rd(e + 4, 8) } else { rd(e + 4, 4) }; + let field = e + if big { 12 } else { 8 }; + let value_at = if count as usize * type_size(ty) <= inline { field } else { rd(field, inline) as usize }; + return Some((e, ty, count, value_at)); + } + None +} + +/// Overwrites value `i` of entry `tag` in directory `k` (in its own type and width). +pub fn patch_value(b: &mut Built, little: bool, big: bool, k: usize, tag: u16, i: usize, value: u64) { + let (_, ty, _, at) = find_entry(b, little, big, k, tag).expect("tag present"); + let n = type_size(ty); + let bytes = if little { value.to_le_bytes() } else { value.to_be_bytes() }; + let v = if little { bytes[..n].to_vec() } else { bytes[8 - n..].to_vec() }; + let at = at + i * n; + b.bytes[at..at + n].copy_from_slice(&v); +} + +/// Overwrites the count field of entry `tag` in directory `k`. +pub fn patch_count(b: &mut Built, little: bool, big: bool, k: usize, tag: u16, count: u64) { + let (e, ..) = find_entry(b, little, big, k, tag).expect("tag present"); + let v = if big { + if little { count.to_le_bytes().to_vec() } else { count.to_be_bytes().to_vec() } + } else if little { + (count as u32).to_le_bytes().to_vec() + } else { + (count as u32).to_be_bytes().to_vec() + }; + b.bytes[e + 4..e + 4 + v.len()].copy_from_slice(&v); +} + +/// Samples to file bytes in the given order. +pub fn u16s(v: &[u16], little: bool) -> Vec { + v.iter().flat_map(|x| if little { x.to_le_bytes() } else { x.to_be_bytes() }).collect() +} +pub fn f32s(v: &[f32], little: bool) -> Vec { + v.iter().flat_map(|x| if little { x.to_le_bytes() } else { x.to_be_bytes() }).collect() +} + +/// Splits an interleaved `w`×`h` image of `spp` samples (`bps` bytes each) into tiles of +/// `tw`×`th`, padded with zeros; chunky (one blob per tile) or planar (all tiles of plane 0, +/// then plane 1, …). +pub fn tile_split(px: &[u8], w: usize, h: usize, spp: usize, bps: usize, tw: usize, th: usize, planar: bool) -> Vec> { + let across = w.div_ceil(tw); + let down = h.div_ceil(th); + let planes = if planar { spp } else { 1 }; + let per = if planar { 1 } else { spp }; + let mut out = Vec::new(); + for p in 0..planes { + for ty in 0..down { + for tx in 0..across { + let mut t = vec![0u8; tw * th * per * bps]; + for y in 0..th { + for x in 0..tw { + let (ix, iy) = (tx * tw + x, ty * th + y); + if ix >= w || iy >= h { + continue; + } + for s in 0..per { + let src = ((iy * w + ix) * spp + if planar { p } else { s }) * bps; + let dst = ((y * tw + x) * per + s) * bps; + t[dst..dst + bps].copy_from_slice(&px[src..src + bps]); + } + } + } + out.push(t); + } + } + } + out +} + +/// Splits an interleaved image into planar strips of `rows` rows: plane 0's strips, then +/// plane 1's, …. +pub fn planar_strips(px: &[u8], w: usize, h: usize, spp: usize, bps: usize, rows: usize) -> Vec> { + let mut out = Vec::new(); + for p in 0..spp { + for y0 in (0..h).step_by(rows) { + let mut s = Vec::new(); + for y in y0..(y0 + rows).min(h) { + for x in 0..w { + let src = ((y * w + x) * spp + p) * bps; + s.extend_from_slice(&px[src..src + bps]); + } + } + out.push(s); + } + } + out +} diff --git a/crates/image-codecs/tests/it/dds.rs b/crates/image-codecs/tests/it/dds.rs new file mode 100644 index 000000000..4f766a54c --- /dev/null +++ b/crates/image-codecs/tests/it/dds.rs @@ -0,0 +1,410 @@ +use craft_image_codecs::{ + ChannelLayout, CodecError, DecodeOptions, Format, Limits, SampleType, caps, decode, decode_as, decode_as_with, detect, encode, from_extension, +}; + +const DDPF_ALPHAPIXELS: u32 = 0x1; +const DDPF_FOURCC: u32 = 0x4; +const DDPF_RGB: u32 = 0x40; +const DDPF_LUMINANCE: u32 = 0x2_0000; +const DDSD_PITCH: u32 = 0x8; + +/// A DDS_HEADER for a single-level 2D texture, after the `DDS ` signature. +fn header(width: u32, height: u32, pf_flags: u32, fourcc: &[u8; 4], bits: u32, masks: [u32; 4]) -> Vec { + let mut b = vec![0u8; 128]; + b[0..4].copy_from_slice(b"DDS "); + let mut put = |at: usize, v: u32| b[at..at + 4].copy_from_slice(&v.to_le_bytes()); + put(4, 124); + put(8, 0x1007); // CAPS | HEIGHT | WIDTH | PIXELFORMAT + put(12, height); + put(16, width); + put(76, 32); + put(80, pf_flags); + put(88, bits); + for (i, m) in masks.into_iter().enumerate() { + put(92 + 4 * i, m); + } + put(108, 0x1000); // DDSCAPS_TEXTURE + b[84..88].copy_from_slice(fourcc); + b +} + +fn compressed(width: u32, height: u32, fourcc: &[u8; 4]) -> Vec { + header(width, height, DDPF_FOURCC, fourcc, 0, [0; 4]) +} + +fn set_pitch(b: &mut [u8], pitch: u32) { + b[8..12].copy_from_slice(&(0x1007 | DDSD_PITCH).to_le_bytes()); + b[20..24].copy_from_slice(&pitch.to_le_bytes()); +} + +/// The DX10 extension header after a `DX10` FourCC: format, dimension, misc flags, array size. +fn dx10(width: u32, height: u32, dxgi: u32, dimension: u32, misc: u32, array: u32) -> Vec { + let mut b = compressed(width, height, b"DX10"); + for v in [dxgi, dimension, misc, array, 0] { + b.extend(v.to_le_bytes()); + } + b +} + +/// BC1 colour endpoints as RGB565, then one 2-bit index per pixel, row by row. +fn bc1_block(c0: u16, c1: u16, rows: [u8; 4]) -> Vec { + let mut b = Vec::new(); + b.extend(c0.to_le_bytes()); + b.extend(c1.to_le_bytes()); + b.extend(rows); + b +} + +const RED: u16 = 0xf800; +const GREEN: u16 = 0x07e0; +const BLUE: u16 = 0x001f; +const BLACK: u16 = 0x0000; +const WHITE: u16 = 0xffff; + +/// Indices 0, 1 on the top row and 2, 3 below them, for the top-left 2x2 pixels. +const INDICES_0123: [u8; 4] = [0x04, 0x0e, 0, 0]; + +#[test] +fn dds_is_readable_and_writable() { + assert_eq!(from_extension("texture.DDS"), Some(Format::Dds)); + assert!(caps(Format::Dds).read && caps(Format::Dds).write); + let img = craft_image_codecs::Image::from_u8(1, 1, ChannelLayout::Rgba, vec![1, 2, 3, 0]).unwrap(); + let b = encode(&img, Format::Dds, &Default::default()).unwrap(); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 0]); +} + +#[test] +fn decodes_bc1_four_colour_blocks() { + // c0 > c1: four opaque colours, the two in between at thirds. + let mut b = compressed(2, 2, b"DXT1"); + b.extend(bc1_block(RED, BLUE, INDICES_0123)); + assert_eq!(detect(&b), Some(Format::Dds)); + let im = decode(&b).unwrap(); + assert_eq!((im.dimensions(), im.layout(), im.sample_type()), ((2, 2), ChannelLayout::Rgba, SampleType::U8)); + assert_eq!(im.data(), &[255, 0, 0, 255, 0, 0, 255, 255, 170, 0, 85, 255, 85, 0, 170, 255]); +} + +#[test] +fn decodes_bc1_one_bit_alpha_blocks() { + // c0 <= c1: three colours (the third halfway) and index 3 is transparent black. + let mut b = compressed(2, 2, b"DXT1"); + b.extend(bc1_block(BLUE, RED, INDICES_0123)); + assert_eq!(decode(&b).unwrap().data(), &[0, 0, 255, 255, 255, 0, 0, 255, 128, 0, 128, 255, 0, 0, 0, 0]); +} + +#[test] +fn decodes_bc2_explicit_alpha_with_four_colour_blocks() { + let mut b = compressed(2, 2, b"DXT3"); + // Four bits of alpha per pixel: 15, 0 on the top row, 8, 1 below. + b.extend([0x0f, 0x00, 0x18, 0x00, 0, 0, 0, 0]); + // c0 < c1 would mean transparency in BC1; BC2 colour blocks always have four colours. + b.extend(bc1_block(BLACK, WHITE, [0x04, 0x0b, 0, 0])); + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgba); + assert_eq!(im.data(), &[0, 0, 0, 255, 255, 255, 255, 0, 170, 170, 170, 136, 85, 85, 85, 17]); +} + +#[test] +fn decodes_bc3_interpolated_alpha() { + // a0 > a1: eight alpha values, six interpolated at sevenths. Indices 0, 1 / 2, 7. + let mut b = compressed(2, 2, b"DXT5"); + b.extend([255, 0, 0x08, 0xa0, 0x03, 0, 0, 0]); + b.extend(bc1_block(BLACK, WHITE, [0x04, 0x0b, 0, 0])); + assert_eq!(decode(&b).unwrap().data(), &[0, 0, 0, 255, 255, 255, 255, 0, 170, 170, 170, 219, 85, 85, 85, 36]); + + // a0 <= a1: six values, four interpolated at fifths, then 0 and 255. Indices 2, 5 / 6, 7. + let mut b = compressed(2, 2, b"DXT5"); + let bits: u64 = 2 | 5 << 3 | 6 << 12 | 7 << 15; + b.extend([0, 255]); + b.extend(&bits.to_le_bytes()[..6]); + b.extend(bc1_block(GREEN, GREEN, [0; 4])); + assert_eq!(decode(&b).unwrap().data(), &[0, 255, 0, 51, 0, 255, 0, 204, 0, 255, 0, 0, 0, 255, 0, 255]); +} + +#[test] +fn bc_textures_need_not_be_a_multiple_of_four() { + // 5x3 is two blocks wide and one high; the right block's first column is x = 4. + let mut b = compressed(5, 3, b"DXT1"); + b.extend(bc1_block(RED, RED, [0; 4])); + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + let im = decode(&b).unwrap(); + assert_eq!(im.dimensions(), (5, 3)); + let px = |x: usize, y: usize| &im.data()[(y * 5 + x) * 4..(y * 5 + x + 1) * 4]; + assert_eq!((px(3, 2), px(4, 0), px(4, 2)), (&[255, 0, 0, 255][..], &[0, 0, 255, 255][..], &[0, 0, 255, 255][..])); +} + +#[test] +fn decodes_uncompressed_rgba_and_bgra_masks() { + let rgba = [0xff, 0xff00, 0xff_0000, 0xff00_0000]; + let mut b = header(2, 1, DDPF_RGB | DDPF_ALPHAPIXELS, &[0; 4], 32, rgba); + b.extend([1, 2, 3, 4, 250, 251, 252, 253]); + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgba); + assert_eq!(im.data(), &[1, 2, 3, 4, 250, 251, 252, 253]); + + let bgra = [0xff_0000, 0xff00, 0xff, 0xff00_0000]; + let mut b = header(2, 1, DDPF_RGB | DDPF_ALPHAPIXELS, &[0; 4], 32, bgra); + b.extend([3, 2, 1, 4, 252, 251, 250, 253]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4, 250, 251, 252, 253]); + + // An alpha mask without DDPF_ALPHAPIXELS is not alpha: X8R8G8B8 opens as RGB. + let mut b = header(1, 1, DDPF_RGB, &[0; 4], 32, bgra); + b.extend([3, 2, 1, 0]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::Rgb, &[1u8, 2, 3][..])); +} + +#[test] +fn decodes_packed_masks_and_luminance() { + // R5G6B5: each field scaled to 0..255; 3 of 31 is 25. + let mut b = header(4, 1, DDPF_RGB, &[0; 4], 16, [0xf800, 0x07e0, 0x001f, 0]); + for v in [RED, GREEN, BLUE, 3 << 11] { + b.extend(v.to_le_bytes()); + } + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgb); + assert_eq!(im.data(), &[255, 0, 0, 0, 255, 0, 0, 0, 255, 25, 0, 0]); + + // 24-bit rows are packed three bytes a pixel, without padding. + let mut b = header(2, 2, DDPF_RGB, &[0; 4], 24, [0xff_0000, 0xff00, 0xff, 0]); + b.extend([3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); + + let mut b = header(2, 1, DDPF_LUMINANCE | DDPF_ALPHAPIXELS, &[0; 4], 16, [0xff, 0, 0, 0xff00]); + b.extend([10, 20, 30, 40]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::GrayA, &[10u8, 20, 30, 40][..])); +} + +#[test] +fn decodes_padded_rgb_rows() { + // DWORD-aligned 24-bit rows: the fourth byte is padding, not the next pixel. + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + set_pitch(&mut b, 4); + b.extend([255, 0, 0, 99, 0, 255, 0, 88]); + let im = decode(&b).unwrap(); + assert_eq!((im.dimensions(), im.layout()), ((1, 2), ChannelLayout::Rgb)); + assert_eq!(im.data(), &[255, 0, 0, 0, 255, 0]); + + // Padding can contain whole pixels; only the declared width is decoded. + let mut b = header(2, 2, DDPF_RGB, &[0; 4], 16, [0xf800, 0x07e0, 0x001f, 0]); + set_pitch(&mut b, 8); + for v in [RED, GREEN, WHITE, WHITE, BLUE, BLACK, WHITE, WHITE] { + b.extend(v.to_le_bytes()); + } + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0]); +} + +#[test] +fn decodes_padded_luminance_rows() { + let mut b = header(2, 2, DDPF_LUMINANCE, &[0; 4], 8, [0xff, 0, 0, 0]); + set_pitch(&mut b, 4); + b.extend([10, 20, 99, 98, 30, 40, 97, 96]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::Gray, &[10u8, 20, 30, 40][..])); + + let mut b = header(1, 2, DDPF_LUMINANCE | DDPF_ALPHAPIXELS, &[0; 4], 16, [0xff, 0, 0, 0xff00]); + set_pitch(&mut b, 4); + b.extend([10, 20, 99, 98, 30, 40, 97, 96]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::GrayA, &[10u8, 20, 30, 40][..])); +} + +#[test] +fn decodes_padded_dx10_rows() { + let mut b = dx10(1, 2, 28, 3, 0, 1); // R8G8B8A8_UNORM + set_pitch(&mut b, 8); + b.extend([1, 2, 3, 4, 99, 98, 97, 96, 5, 6, 7, 8, 95, 94, 93, 92]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4, 5, 6, 7, 8]); +} + +#[test] +fn ignores_pitch_without_the_pitch_flag() { + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + b[20..24].copy_from_slice(&u32::MAX.to_le_bytes()); + b.extend([255, 0, 0, 0, 255, 0]); + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0]); + + set_pitch(&mut b, 3); // An explicitly tightly packed row is also valid. + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0]); +} + +#[test] +fn compressed_linear_size_is_not_a_row_pitch() { + let mut b = compressed(4, 8, b"DXT1"); + b[8..12].copy_from_slice(&(0x1007u32 | 0x8_0000).to_le_bytes()); // DDSD_LINEARSIZE + b[20..24].copy_from_slice(&16u32.to_le_bytes()); // Two 8-byte block rows. + b.extend(bc1_block(RED, RED, [0; 4])); + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + let im = decode(&b).unwrap(); + assert!(im.data()[..64].as_chunks::<4>().0.iter().all(|p| p == &[255, 0, 0, 255])); + assert!(im.data()[64..].as_chunks::<4>().0.iter().all(|p| p == &[0, 0, 255, 255])); +} + +#[test] +fn rejects_pitch_smaller_than_a_pixel_row() { + for pitch in [0, 1, 2] { + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + set_pitch(&mut b, pitch); + b.extend([255, 0, 0, 0, 255, 0]); + assert!(matches!(decode(&b), Err(CodecError::Malformed { .. })), "pitch {pitch}"); + } +} + +#[test] +fn rejects_truncated_padded_rows() { + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + set_pitch(&mut b, 4); + b.extend([255, 0, 0, 99, 0, 255, 0, 88]); + assert!(decode(&b).is_ok()); + for n in 128..b.len() { + assert!(matches!(decode(&b[..n]), Err(CodecError::Malformed { .. })), "decoded {n} of {} bytes", b.len()); + } +} + +#[test] +fn rejects_huge_pitch_without_allocating() { + let mut b = header(1, 2, DDPF_LUMINANCE, &[0; 4], 8, [0xff, 0, 0, 0]); + set_pitch(&mut b, u32::MAX); + b.extend([10, 20]); + // On 32-bit targets pitch * height overflows; on 64-bit the payload is too short. + assert!(matches!(decode(&b), Err(CodecError::Malformed { .. }))); +} + +#[test] +fn decodes_dx10_headers() { + let mut b = dx10(1, 1, 28, 3, 0, 1); // R8G8B8A8_UNORM + b.extend([1, 2, 3, 4]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4]); + + let mut b = dx10(1, 1, 91, 3, 0, 1); // B8G8R8A8_UNORM_SRGB + b.extend([3, 2, 1, 4]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4]); + + let mut b = dx10(2, 2, 71, 3, 0, 1); // BC1_UNORM + b.extend(bc1_block(BLUE, RED, INDICES_0123)); + assert_eq!(decode(&b).unwrap().data(), &[0, 0, 255, 255, 255, 0, 0, 255, 128, 0, 128, 255, 0, 0, 0, 0]); +} + +#[test] +fn only_the_top_mip_level_is_read() { + let mut b = compressed(4, 4, b"DXT1"); + b[28..32].copy_from_slice(&3u32.to_le_bytes()); + b.extend(bc1_block(RED, RED, [0; 4])); + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + b.extend(bc1_block(GREEN, GREEN, [0; 4])); + let im = decode(&b).unwrap(); + assert_eq!(im.dimensions(), (4, 4)); + assert!(im.data().chunks(4).all(|p| p == [255, 0, 0, 255])); +} + +#[test] +fn refuses_cubemaps_arrays_and_volumes() { + let unsupported = |b: &[u8], what: &str| match decode_as(Format::Dds, b) { + Err(CodecError::Unsupported { reason, .. }) => assert!(reason.contains(what), "{reason}"), + other => panic!("{what}: {other:?}"), + }; + let mut cube = compressed(4, 4, b"DXT1"); + cube[112..116].copy_from_slice(&0xfe00u32.to_le_bytes()); // DDSCAPS2_CUBEMAP and all faces + cube.extend([0; 8 * 6]); + unsupported(&cube, "cubemap"); + let mut volume = compressed(4, 4, b"DXT1"); + volume[8..12].copy_from_slice(&(0x1007u32 | 0x80_0000).to_le_bytes()); // DDSD_DEPTH + volume[24..28].copy_from_slice(&4u32.to_le_bytes()); + volume[112..116].copy_from_slice(&0x20_0000u32.to_le_bytes()); // DDSCAPS2_VOLUME + volume.extend([0; 8 * 4]); + unsupported(&volume, "volume"); + unsupported(&dx10(4, 4, 71, 3, 0x4, 1), "cubemap"); + unsupported(&dx10(4, 4, 71, 3, 0, 6), "array"); + unsupported(&dx10(4, 4, 71, 4, 0, 1), "2D"); + unsupported(&dx10(4, 4, 98, 3, 0, 1), "98"); // BC7_UNORM + unsupported(&compressed(4, 4, b"ATI2"), "ATI2"); + unsupported(&compressed(4, 4, b"DXT2"), "premultiplied"); +} + +/// Face declarations matter even when the general cube flag is absent; padding does not. +#[test] +fn source_probe_distinguishes_extra_faces_from_single_images_and_padding() { + for caps in [0u32, 0x400, 0xc00, 0xfc00] { + let mut b = compressed(4, 4, b"DXT1"); + b[112..116].copy_from_slice(&caps.to_le_bytes()); + b.extend(bc1_block(RED, RED, [0; 4])); + for _ in 1..caps.count_ones() { + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + } + assert!(decode(&b).unwrap().data().chunks(4).all(|p| p == [255, 0, 0, 255])); + assert_eq!(craft_image_codecs::has_unretained_source_data(&b).unwrap(), caps.count_ones() > 1); + } + // A FourCC-looking unused union and trailing bytes are not a DX10 declaration. + let mut single = header(1, 1, DDPF_RGB, b"DX10", 24, [0xff, 0xff00, 0xff_0000, 0]); + single.extend([1, 2, 3]); + single.extend([0xff; 64]); + assert_eq!(decode(&single).unwrap().data(), &[1, 2, 3]); + for mips in [0u32, 1] { + single[28..32].copy_from_slice(&mips.to_le_bytes()); + assert!(!craft_image_codecs::has_unretained_source_data(&single).unwrap()); + } +} + +/// The metadata probe follows the same DX10 discriminator as preview decoding. +#[test] +fn source_probe_reads_dx10_array_and_cube_declarations() { + for (array, misc) in [(0, 0), (1, 0), (2, 0), (1, 4)] { + let mut b = dx10(1, 1, 28, 3, misc, array); + b.extend([1, 2, 3, 4]); + let multiple = array > 1 || misc == 4; + assert_eq!(craft_image_codecs::has_unretained_source_data(&b).unwrap(), multiple); + if multiple { + assert!(matches!(decode(&b), Err(CodecError::Unsupported { .. }))); + } else { + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4]); + } + } +} + +#[test] +fn rejects_truncated_files_at_every_length_without_panicking() { + let mut b = dx10(5, 3, 77, 3, 0, 1); // BC3_UNORM: two 16-byte blocks + b.extend([0x5a; 32]); + assert!(decode(&b).is_ok()); + for n in 0..b.len() { + assert!(decode_as(Format::Dds, &b[..n]).is_err(), "decoded {n} of {} bytes", b.len()); + } + let mut b = header(3, 2, DDPF_RGB, &[0; 4], 24, [0xff_0000, 0xff00, 0xff, 0]); + b.extend([7; 18]); + assert!(decode(&b).is_ok()); + assert!(decode_as(Format::Dds, &b[..b.len() - 1]).is_err(), "one byte short"); +} + +#[test] +fn rejects_bogus_sizes_and_pixel_formats() { + // Huge dimensions are refused before anything is allocated. + let mut huge = compressed(u32::MAX, u32::MAX, b"DXT1"); + huge.extend([0; 8]); + assert!(matches!(decode_as(Format::Dds, &huge), Err(CodecError::LimitExceeded(_)))); + let opts = DecodeOptions { limits: Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; + let mut tight = header(64, 64, DDPF_RGB, &[0; 4], 32, [0xff, 0xff00, 0xff_0000, 0]); + tight.extend(vec![0; 64 * 64 * 4]); + assert!(matches!(decode_as_with(Format::Dds, &tight, &opts), Err(CodecError::LimitExceeded(_)))); + assert!(decode_as(Format::Dds, &compressed(0, 4, b"DXT1")).is_err(), "zero width"); + + let mut bad_size = compressed(4, 4, b"DXT1"); + bad_size[4..8].copy_from_slice(&100u32.to_le_bytes()); + bad_size.extend([0; 8]); + assert!(matches!(decode_as(Format::Dds, &bad_size), Err(CodecError::Malformed { .. })), "header size must be 124"); + + for (bits, masks) in [ + (12, [0xf00, 0xf0, 0xf, 0]), // not a whole number of bytes + (16, [0xff_0000, 0xff00, 0xff, 0]), // a mask wider than the pixel + (32, [0xf0f, 0xf0f0, 0xf_0000, 0]), // a mask with a gap + (32, [0, 0, 0, 0]), // no colour at all + (0, [0xff, 0xff00, 0xff_0000, 0]), // zero bits per pixel + ] { + let mut b = header(1, 1, DDPF_RGB, &[0; 4], bits, masks); + b.extend([0; 4]); + assert!(decode_as(Format::Dds, &b).is_err(), "{bits} bits, masks {masks:x?}"); + } + let mut no_format = header(1, 1, 0, &[0; 4], 32, [0xff, 0xff00, 0xff_0000, 0]); + no_format.extend([0; 4]); + assert!(matches!(decode_as(Format::Dds, &no_format), Err(CodecError::Unsupported { .. }))); +} diff --git a/crates/image-codecs/tests/it/dds_write.rs b/crates/image-codecs/tests/it/dds_write.rs new file mode 100644 index 000000000..4f66d3679 --- /dev/null +++ b/crates/image-codecs/tests/it/dds_write.rs @@ -0,0 +1,375 @@ +//! Writing DDS textures: every storage read back through the reader, the header fields, sizes +//! that aren't a multiple of four, the mip chain, and the block-compression error on ramps. + +use craft_image_codecs::{ChannelLayout, DdsCompression, DdsOptions, EncodeOptions, FidelityWarning, Format, Image, decode, encode, fidelity_warnings_with}; + +const DDSD_PITCH: u32 = 0x8; +const DDSD_MIPMAPCOUNT: u32 = 0x2_0000; +const DDSD_LINEARSIZE: u32 = 0x8_0000; +const REQUIRED: u32 = 0x1007; // CAPS | HEIGHT | WIDTH | PIXELFORMAT +const DDSCAPS_COMPLEX: u32 = 0x8; +const DDSCAPS_TEXTURE: u32 = 0x1000; +const DDSCAPS_MIPMAP: u32 = 0x40_0000; +const DDPF_ALPHAPIXELS: u32 = 0x1; +const DDPF_FOURCC: u32 = 0x4; +const DDPF_RGB: u32 = 0x40; +const DDPF_LUMINANCE: u32 = 0x2_0000; + +const BLOCKS: [DdsCompression; 3] = [DdsCompression::Bc1, DdsCompression::Bc2, DdsCompression::Bc3]; + +fn opts(compression: DdsCompression, mipmaps: bool) -> EncodeOptions { + EncodeOptions { dds: DdsOptions { compression, mipmaps }, ..Default::default() } +} + +fn le32(b: &[u8], at: usize) -> u32 { + u32::from_le_bytes(b[at..at + 4].try_into().unwrap()) +} + +/// A ramp along one line through RGB (red up, green up half as fast, blue down) across the +/// texture, and alpha falling from opaque down it: every block's colours lie on a line, which +/// is what a colour block stores. +fn ramp(w: u32, h: u32, layout: ChannelLayout) -> Image { + let ch = layout.channels(); + let at = |v: u32, n: u32| (v * 255 / (n - 1).max(1)) as u8; + let mut data = Vec::new(); + for y in 0..h { + for x in 0..w { + let r = at(x, w); + let rgba = [r, 64 + r / 2, 255 - r, 255 - at(y, h)]; + match ch { + 1 => data.push(rgba[0]), + 2 => data.extend([rgba[0], rgba[3]]), + _ => data.extend(&rgba[..ch]), + } + } + } + Image::from_u8(w, h, layout, data).unwrap() +} + +/// The same RGBA image, fully opaque. +fn opaque(img: &Image) -> Image { + let data = img.data().chunks(4).flat_map(|p| [p[0], p[1], p[2], 255]).collect(); + Image::from_u8(img.width(), img.height(), ChannelLayout::Rgba, data).unwrap() +} + +/// Largest difference in channel `c` (of four) between RGBA images. +fn max_diff_channel(a: &Image, b: &Image, c: usize) -> u8 { + a.data().chunks(4).zip(b.data().chunks(4)).map(|(x, y)| x[c].abs_diff(y[c])).max().unwrap_or(0) +} + +/// Largest colour difference between RGBA images where `b` is visible (BC1 turns texels under +/// half opacity into transparent black). +fn max_colour_diff(a: &Image, b: &Image) -> u8 { + a.data().chunks(4).zip(b.data().chunks(4)).filter(|(_, y)| y[3] != 0).flat_map(|(x, y)| (0..3).map(|c| x[c].abs_diff(y[c]))).max().unwrap_or(0) +} + +/// A 2D gradient: red across, green down and blue along the diagonal, opaque. +fn gradient(w: u32, h: u32) -> Image { + let data = (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x * 4).min(255) as u8, (y * 4).min(255) as u8, ((x + y) * 2).min(255) as u8, 255])).collect(); + Image::from_u8(w, h, ChannelLayout::Rgba, data).unwrap() +} + +/// PSNR of the colour (not the alpha) of two RGBA images. +fn psnr(a: &Image, b: &Image) -> f64 { + let rgb = |i: &Image| i.data().chunks(4).flat_map(|p| [p[0], p[1], p[2]]).map(f64::from).collect::>(); + let (a, b) = (rgb(a), rgb(b)); + let mse = a.iter().zip(&b).map(|(x, y)| (x - y).powi(2)).sum::() / a.len() as f64; + if mse == 0.0 { f64::INFINITY } else { 10.0 * (255.0f64.powi(2) / mse).log10() } +} + +/// Bytes of one level as a reader computes them from the header's pixel format. +fn level_bytes(file: &[u8], w: usize, h: usize) -> usize { + if le32(file, 80) & DDPF_FOURCC != 0 { + let block = if &file[84..88] == b"DXT1" { 8 } else { 16 }; + w.div_ceil(4) * h.div_ceil(4) * block + } else { + w * h * le32(file, 88) as usize / 8 + } +} + +/// Mip level `i` of a written texture, cut out as a single-level texture and decoded. +fn level(file: &[u8], i: u32) -> Image { + let (w, h) = (le32(file, 16) as usize, le32(file, 12) as usize); + let size = |i: u32| ((w >> i).max(1), (h >> i).max(1)); + let start = 128 + (0..i).map(|j| level_bytes(file, size(j).0, size(j).1)).sum::(); + let (lw, lh) = size(i); + let mut b = file[..128].to_vec(); + b[12..16].copy_from_slice(&(lh as u32).to_le_bytes()); + b[16..20].copy_from_slice(&(lw as u32).to_le_bytes()); + b[28..32].copy_from_slice(&1u32.to_le_bytes()); + // The pitch field is a row stride for uncompressed textures and the level size for BC. + let pitch = if le32(file, 80) & DDPF_FOURCC != 0 { level_bytes(file, lw, lh) } else { lw * le32(file, 88) as usize / 8 }; + b[20..24].copy_from_slice(&(pitch as u32).to_le_bytes()); + b.extend(&file[start..start + level_bytes(file, lw, lh)]); + decode(&b).unwrap() +} + +#[test] +fn uncompressed_textures_round_trip_every_layout_exactly() { + for (layout, pf_flags, bits, masks) in [ + (ChannelLayout::Gray, DDPF_LUMINANCE, 8, [0xff, 0, 0, 0]), + (ChannelLayout::GrayA, DDPF_LUMINANCE | DDPF_ALPHAPIXELS, 16, [0xff, 0, 0, 0xff00]), + (ChannelLayout::Rgb, DDPF_RGB, 32, [0xff_0000, 0xff00, 0xff, 0]), + (ChannelLayout::Rgba, DDPF_RGB | DDPF_ALPHAPIXELS, 32, [0xff_0000, 0xff00, 0xff, 0xff00_0000]), + ] { + for (w, h) in [(1, 1), (5, 3), (17, 9)] { + let img = ramp(w, h, layout); + let b = encode(&img, Format::Dds, &EncodeOptions::default()).unwrap(); + let back = decode(&b).unwrap(); + assert_eq!(back.layout(), layout, "{layout:?} {w}x{h}"); + assert_eq!(back.data(), img.data(), "{layout:?} {w}x{h}"); + + assert_eq!((&b[..4], le32(&b, 4), le32(&b, 76)), (&b"DDS "[..], 124, 32)); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_PITCH, "pitch, not linear size, and no mip count"); + assert_eq!((le32(&b, 12), le32(&b, 16)), (h, w)); + assert_eq!(le32(&b, 20), w * bits / 8, "pitch of {layout:?} {w}x{h}"); + assert_eq!((le32(&b, 28), le32(&b, 108)), (1, DDSCAPS_TEXTURE)); + assert_eq!((le32(&b, 80), le32(&b, 88)), (pf_flags, bits)); + assert_eq!([le32(&b, 92), le32(&b, 96), le32(&b, 100), le32(&b, 104)], masks); + assert_eq!(b.len(), 128 + (w * h * bits / 8) as usize); + } + } +} + +#[test] +fn block_compressed_textures_round_trip_through_the_reader() { + for (compression, fourcc, block) in [(DdsCompression::Bc1, b"DXT1", 8), (DdsCompression::Bc2, b"DXT3", 16), (DdsCompression::Bc3, b"DXT5", 16)] { + // Not multiples of four, and the 1×1 and 2×2 textures smaller than one block. + for (w, h) in [(1, 1), (2, 2), (4, 4), (5, 3), (7, 13), (33, 17)] { + let img = ramp(w, h, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(compression, false)).unwrap(); + let linear = w.div_ceil(4) * h.div_ceil(4) * block; + assert_eq!(&b[84..88], fourcc); + assert_eq!((le32(&b, 80), le32(&b, 88)), (DDPF_FOURCC, 0)); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_LINEARSIZE, "linear size, not pitch"); + assert_eq!(le32(&b, 20), linear, "{compression:?} {w}x{h}: the top level's size"); + assert_eq!(b.len(), 128 + linear as usize); + + let back = decode(&b).unwrap(); + assert_eq!((back.dimensions(), back.layout()), ((w, h), ChannelLayout::Rgba)); + // Steep ramps across tiny textures put three colours where a block interpolates at + // thirds, so this bound is loose; it still catches swapped channels or misplaced + // blocks. `block_compression_error_on_gradients_is_bounded` is the tight one. A BC1 + // block with transparent texels has only three colours, so BC1's colours are + // checked on the opaque ramp. + let (src, back_colour) = if compression == DdsCompression::Bc1 { + let o = opaque(&img); + let back = decode(&encode(&o, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + (o, back) + } else { + (img.clone(), back.clone()) + }; + let colour = max_colour_diff(&src, &back_colour); + assert!(colour <= 24, "{compression:?} {w}x{h}: colour off by {colour}"); + let alpha = max_diff_channel(&img, &back, 3); + match compression { + DdsCompression::Bc1 => assert!(back.data().chunks(4).zip(img.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + _ => assert!(alpha <= 8, "{compression:?} {w}x{h}: alpha off by {alpha}"), + } + } + } +} + +#[test] +fn gray_and_rgb_sources_are_written_as_rgba_blocks() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb] { + for compression in BLOCKS { + let img = ramp(8, 8, layout); + let back = decode(&encode(&img, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + let want = img.convert(ChannelLayout::Rgba, img.sample_type()); + assert_eq!(back.layout(), ChannelLayout::Rgba); + let colour = max_colour_diff(&want, &back); + assert!(colour <= 8, "{layout:?} {compression:?}: colour off by {colour}"); + } + } +} + +#[test] +fn block_compression_error_on_gradients_is_bounded() { + let (line, plane) = (ramp(64, 64, ChannelLayout::Rgba), gradient(64, 64)); + for compression in BLOCKS { + let roundtrip = |img: &Image| decode(&encode(img, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + let (line_back, plane_back) = (roundtrip(&line), roundtrip(&plane)); + let (line_err, plane_err) = (max_colour_diff(&line, &line_back), max_colour_diff(&plane, &plane_back)); + let p = psnr(&plane, &plane_back); + let alpha = max_diff_channel(&line, &line_back, 3); + // Colours on a line only lose the RGB565 rounding of the endpoints (up to 4 levels in + // red and blue). A gradient in two directions is not on a line: the spread off a + // block's best line alone limits this one to about 40 dB, and RGB565 costs a little + // more (the encoder measures 38.6 dB and 9 levels at most). + assert!(line_err <= 5, "{compression:?}: colour ramp off by {line_err}"); + assert!(plane_err <= 10, "{compression:?}: 2D gradient off by {plane_err}"); + assert!(p >= 38.0, "{compression:?}: 2D gradient PSNR {p:.1} dB"); + match compression { + DdsCompression::Bc1 => assert!(line_back.data().chunks(4).zip(line.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + // Four bits: within half a step of 17. + DdsCompression::Bc2 => assert!(alpha <= 8, "BC2 alpha off by {alpha}"), + // Eight values across a block's few levels of alpha. + _ => assert!(alpha <= 2, "BC3 alpha off by {alpha}"), + } + } +} + +#[test] +fn solid_and_cutout_textures_keep_their_values() { + // RGB565-exact colours and BC2's 4-bit alpha steps come back exactly. + let solid = Image::from_u8(6, 5, ChannelLayout::Rgba, [255, 0, 255, 170].repeat(30)).unwrap(); + for compression in BLOCKS { + let back = decode(&encode(&solid, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + let want = if compression == DdsCompression::Bc1 { [255, 0, 255, 255] } else { [255, 0, 255, 170] }; + assert!(back.data().chunks(4).all(|p| p == want), "{compression:?}: {:?}", &back.data()[..4]); + } + // A cutout: BC1's transparent texels decode as transparent black, the rest stay opaque. + let cutout: Vec = (0..36).flat_map(|i| if (i / 6 + i % 6) % 2 == 0 { [200, 40, 40, 255] } else { [10, 10, 10, 0] }).collect(); + let cutout = Image::from_u8(6, 6, ChannelLayout::Rgba, cutout).unwrap(); + for compression in BLOCKS { + let back = decode(&encode(&cutout, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + assert_eq!(max_diff_channel(&cutout, &back, 3), 0, "{compression:?}"); + if compression == DdsCompression::Bc1 { + assert!(back.data().chunks(4).filter(|p| p[3] == 0).all(|p| p == [0, 0, 0, 0])); + } + } +} + +#[test] +fn mipmaps_write_the_full_chain_box_filtered() { + // 16×8 halves to 8×4, 4×2, 2×1 and 1×1: five levels. + let img = ramp(16, 8, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(DdsCompression::None, true)).unwrap(); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_PITCH | DDSD_MIPMAPCOUNT); + assert_eq!(le32(&b, 28), 5); + assert_eq!(le32(&b, 108), DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP); + assert_eq!(b.len(), 128 + 4 * (16 * 8 + 8 * 4 + 4 * 2 + 2 + 1)); + assert_eq!(decode(&b).unwrap().data(), img.data(), "the reader still reads the top level"); + let mut above = img.clone(); + for i in 1..5 { + let got = level(&b, i); + assert_eq!(got.dimensions(), ((16 >> i).max(1), (8 >> i).max(1))); + let want = box_filter(&above); + assert_eq!(got.data(), want.data(), "level {i}"); + above = got; + } + // 1×1 has a single level. + let one = encode(&ramp(1, 1, ChannelLayout::Rgba), Format::Dds, &opts(DdsCompression::None, true)).unwrap(); + assert_eq!((le32(&one, 28), le32(&one, 8) & DDSD_MIPMAPCOUNT), (1, 0)); +} + +/// The documented filter, written independently: each pixel averages the 2×2 pixels it covers, +/// and the last column and row of an odd-sized level take in the leftover pixel too. +fn box_filter(src: &Image) -> Image { + let (w, h) = (src.width() as usize, src.height() as usize); + let (nw, nh) = ((w / 2).max(1), (h / 2).max(1)); + let ch = src.layout().channels(); + let span = |i: usize, n: usize, size: usize| (2 * i, if i + 1 == n { size } else { 2 * i + 2 }); + let mut out = Vec::new(); + for y in 0..nh { + for x in 0..nw { + let ((x0, x1), (y0, y1)) = (span(x, nw, w), span(y, nh, h)); + for c in 0..ch { + let values: Vec = + (y0..y1).flat_map(|sy| (x0..x1).map(move |sx| (sy, sx))).map(|(sy, sx)| u32::from(src.data()[(sy * w + sx) * ch + c])).collect(); + let n = values.len() as u32; + out.push(((values.iter().sum::() + n / 2) / n) as u8); + } + } + } + Image::from_u8(nw as u32, nh as u32, src.layout(), out).unwrap() +} + +#[test] +fn odd_sized_mip_chains_keep_every_pixel() { + // 5×3 → 2×1 → 1×1. The 2×1 level's right pixel averages columns 2–4 of all three rows. + let img = ramp(5, 3, ChannelLayout::Gray); + let b = encode(&img, Format::Dds, &opts(DdsCompression::None, true)).unwrap(); + assert_eq!(le32(&b, 28), 3); + assert_eq!(b.len(), 128 + 5 * 3 + 2 + 1); + let second = level(&b, 1); + assert_eq!(second.data(), box_filter(&img).data()); + let px = |x: usize, y: usize| u32::from(img.data()[y * 5 + x]); + let right: u32 = (0..3).flat_map(|y| (2..5).map(move |x| (x, y))).map(|(x, y)| px(x, y)).sum(); + assert_eq!(u32::from(second.data()[1]), (right + 4) / 9); +} + +#[test] +fn block_compressed_mip_chains_have_a_block_per_small_level() { + for compression in BLOCKS { + let block = if compression == DdsCompression::Bc1 { 8 } else { 16 }; + let img = ramp(12, 6, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(compression, true)).unwrap(); + // 12×6, 6×3, 3×1, 1×1: 3×2, 2×1, 1 and 1 blocks. + assert_eq!(le32(&b, 28), 4); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_LINEARSIZE | DDSD_MIPMAPCOUNT); + assert_eq!(le32(&b, 20), 6 * block); + assert_eq!(b.len(), 128 + (6 + 2 + 1 + 1) * block as usize); + for i in 0..4 { + let got = level(&b, i); + assert_eq!(got.dimensions(), ((12 >> i).max(1), (6 >> i).max(1)), "{compression:?} level {i}"); + } + } +} + +#[test] +fn strips_and_tiny_textures_encode_with_every_option() { + for (w, h) in [(1, 1), (1, 9), (9, 1), (2, 3), (3, 2)] { + for compression in DdsCompression::ALL { + for mipmaps in [false, true] { + let img = ramp(w, h, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(compression, mipmaps)).unwrap_or_else(|e| panic!("{w}x{h} {compression:?} {mipmaps}: {e}")); + assert_eq!(decode(&b).unwrap().dimensions(), (w, h)); + } + } + } +} + +#[test] +fn fidelity_warnings_follow_the_compression() { + let translucent = ramp(8, 8, ChannelLayout::Rgba); + let warns = |c| fidelity_warnings_with(&translucent, Format::Dds, &opts(c, false)); + assert!(warns(DdsCompression::None).is_empty(), "uncompressed RGBA loses nothing"); + for c in BLOCKS { + assert!(warns(c).contains(&FidelityWarning::LossyCompression), "{c:?}"); + } + assert!(warns(DdsCompression::Bc1).contains(&FidelityWarning::AlphaBinarized)); + assert!(!warns(DdsCompression::Bc3).contains(&FidelityWarning::AlphaBinarized)); +} + +#[test] +fn compression_names_round_trip() { + for c in DdsCompression::ALL { + assert_eq!(DdsCompression::from_name(c.name()), Some(c)); + } + assert_eq!(DdsCompression::from_name("DXT5"), Some(DdsCompression::Bc3)); + assert_eq!(DdsCompression::from_name("dxt1"), Some(DdsCompression::Bc1)); + for bad in ["", "bc7", "dxt2", "uncompressed "] { + assert_eq!(DdsCompression::from_name(bad), None, "{bad:?}"); + } +} + +/// Encode timings on a synthetic ~30 MP texture (6336×4752). Ignored by default; run in release: +/// +/// ```sh +/// cargo test --release -p craft-image-codecs --test it -- dds_write:: --ignored --nocapture +/// ``` +#[test] +#[ignore] +fn dds_encode_timings_on_30_mp() { + let (w, h) = (6336u32, 4752u32); + let mut s = 0x1234_5678u32; + let mut data = Vec::with_capacity(w as usize * h as usize * 4); + for y in 0..h { + for x in 0..w { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + data.extend([(x / 25 + y / 19) as u8, (x / 31) as u8 ^ (s as u8 & 3), (y / 23) as u8, (255 - x / 25) as u8]); + } + } + let img = Image::from_u8(w, h, ChannelLayout::Rgba, data).unwrap(); + for compression in DdsCompression::ALL { + for mipmaps in [false, true] { + let t = std::time::Instant::now(); + let b = encode(&img, Format::Dds, &opts(compression, mipmaps)).unwrap(); + eprintln!("{compression:?} mipmaps={mipmaps}: {:.0} ms, {} MB", t.elapsed().as_secs_f64() * 1e3, b.len() >> 20); + } + } +} diff --git a/crates/image-codecs/tests/it/decode_warnings.rs b/crates/image-codecs/tests/it/decode_warnings.rs new file mode 100644 index 000000000..5995de931 --- /dev/null +++ b/crates/image-codecs/tests/it/decode_warnings.rs @@ -0,0 +1,511 @@ +//! Decode warnings: a file that holds more than was decoded (#523: animations, multi-page +//! TIFF) or less than it should (#518: truncated JPEG) says so; complete single images don't. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +const W: u32 = 24; +const H: u32 = 16; + +fn solid(rgb: [u8; 3]) -> Image { + Image::from_u8(W, H, ChannelLayout::Rgb, rgb.repeat((W * H) as usize)).unwrap() +} + +const COLOURS: [[u8; 3]; 3] = [[200, 30, 10], [10, 200, 30], [30, 10, 200]]; + +// --------------------------------------------------------------------------- +// #518: truncated JPEG +// --------------------------------------------------------------------------- + +fn jpeg(layout: ChannelLayout, progressive: bool) -> Vec { + let img = synth(64, 48, layout, SampleType::U8, 3, 0.3); + let ct = match layout { + ChannelLayout::Gray => jpeg_encoder::ColorType::Luma, + ChannelLayout::Cmyk => jpeg_encoder::ColorType::Cmyk, + _ => jpeg_encoder::ColorType::Rgb, + }; + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, 90); + enc.set_progressive(progressive); + enc.encode(img.data(), 64, 48, ct).unwrap(); + out +} + +/// Offset of the first byte of entropy-coded data (just past the first SOS header). +fn scan_start(b: &[u8]) -> usize { + let sos = b.windows(2).position(|w| w == [0xFF, 0xDA]).unwrap(); + sos + 2 + u16::from_be_bytes([b[sos + 2], b[sos + 3]]) as usize +} + +const TRUNCATED: DecodeWarning = DecodeWarning::Truncated { format: Format::Jpeg }; + +#[test] +fn truncated_jpeg_decodes_with_a_warning() { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Cmyk] { + for progressive in [false, true] { + let full = jpeg(layout, progressive); + assert_eq!(decode(&full).unwrap().warnings, [], "{layout:?} progressive {progressive}"); + let half = decode(&full[..full.len() / 2]).unwrap(); + assert_eq!(half.warnings, [TRUNCATED], "{layout:?} progressive {progressive}"); + assert_eq!(half.dimensions(), (64, 48)); + // One byte short: the end-of-image marker is incomplete. + assert_eq!(decode(&full[..full.len() - 1]).unwrap().warnings, [TRUNCATED]); + // A few bytes of scan data still decode, with the warning. + let start = scan_start(&full); + assert_eq!(decode(&full[..start + 8]).unwrap().warnings, [TRUNCATED]); + } + } +} + +#[test] +fn truncated_jpeg_message_names_the_problem() { + let s = TRUNCATED.to_string(); + assert!(s.starts_with("JPEG data ends early") && s.contains("truncated"), "{s}"); +} + +#[test] +fn jpeg_without_any_scan_data_is_an_error() { + let full = jpeg(ChannelLayout::Rgb, false); + let start = scan_start(&full); + for cut in [start - 3, start - 1, start] { + let e = decode(&full[..cut]).unwrap_err().to_string(); + assert!(e.contains("before any image data"), "cut {cut}: {e}"); + } +} + +#[test] +fn data_after_the_end_of_image_is_not_truncation() { + let mut b = jpeg(ChannelLayout::Rgb, false); + b.extend_from_slice(b"\0\0trailing bytes \xFF\xD8\xFF"); + assert_eq!(decode(&b).unwrap().warnings, []); +} + +#[test] +fn an_end_marker_inside_metadata_does_not_hide_truncation() { + // An EXIF block holding FF D9 (as an embedded thumbnail does) must be skipped as a segment. + let mut img = synth(64, 48, ChannelLayout::Rgb, SampleType::U8, 3, 0.3); + let mut exif = sample_exif(); + exif.extend_from_slice(&[0xFF, 0xD8, 0x00, 0xFF, 0xD9]); + img.meta.exif = Some(exif); + let full = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&full).unwrap().warnings, []); + assert_eq!(decode(&full[..full.len() * 2 / 3]).unwrap().warnings, [TRUNCATED]); +} + +#[test] +fn restart_markers_and_fill_bytes_are_scan_data() { + // Restart markers (FF D0–D7) and fill bytes (FF FF) inside a scan don't end it. + let full = jpeg(ChannelLayout::Rgb, false); + let start = scan_start(&full); + let mut b = full[..start].to_vec(); + b.extend_from_slice(&[0x12, 0xFF, 0x00, 0x34, 0xFF, 0xD0, 0x56, 0xFF, 0xFF, 0xD7, 0x78]); + assert_eq!(decode(&b).unwrap().warnings, [TRUNCATED]); + b.extend_from_slice(&[0xFF, 0xFF, 0xD9]); + assert_eq!(decode(&b).unwrap().warnings, []); +} + +// --------------------------------------------------------------------------- +// #523: only the first frame or page is decoded +// --------------------------------------------------------------------------- + +fn more_frames(n: u32) -> Vec { + vec![DecodeWarning::MoreFrames { total: Some(n) }] +} + +fn gif(frames: usize) -> Vec { + let mut out = Vec::new(); + let mut enc = image::codecs::gif::GifEncoder::new(&mut out); + let frames = COLOURS.iter().take(frames).map(|c| image::Frame::new(image::RgbaImage::from_pixel(W, H, image::Rgba([c[0], c[1], c[2], 255])))); + enc.encode_frames(frames).unwrap(); + drop(enc); + out +} + +#[test] +fn animated_gif_warns_and_keeps_the_first_frame() { + let one = decode(&gif(1)).unwrap(); + assert_eq!(one.warnings, []); + for n in [2, 3] { + let img = decode(&gif(n)).unwrap(); + assert_eq!(img.warnings, more_frames(n as u32)); + assert_eq!(img.to_rgba8(), one.to_rgba8(), "{n} frames: first frame pixels"); + } + // A single-frame GIF written by our own encoder. + let ours = encode(&solid(COLOURS[0]), Format::Gif, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&ours).unwrap().warnings, []); +} + +#[test] +fn gif_without_a_trailer_warns_without_a_total() { + let b = gif(3); + assert_eq!(b.last(), Some(&0x3B)); + let img = decode(&b[..b.len() - 1]).unwrap(); + assert_eq!(img.warnings, [DecodeWarning::MoreFrames { total: None }]); + assert_eq!(img.warnings[0].to_string(), "only the first frame of the animation was imported"); +} + +/// An APNG of `frames` solid frames at 8 or 16 bits; with `separate_default` the IDAT image is +/// an extra default image outside the animation. +fn apng(frames: u32, sixteen: bool, separate_default: bool) -> Vec { + let mut out = Vec::new(); + let mut enc = png::Encoder::new(&mut out, W, H); + enc.set_color(png::ColorType::Rgb); + enc.set_depth(if sixteen { png::BitDepth::Sixteen } else { png::BitDepth::Eight }); + let images = frames + u32::from(separate_default); + if frames > 0 { + enc.set_animated(frames, 0).unwrap(); + enc.set_sep_def_img(separate_default).unwrap(); + } + let mut w = enc.write_header().unwrap(); + for c in COLOURS.iter().cycle().take(images.max(1) as usize) { + let px: Vec = if sixteen { c.iter().flat_map(|&v| [v, v]).collect::>().repeat((W * H) as usize) } else { c.repeat((W * H) as usize) }; + w.write_image_data(&px).unwrap(); + } + w.finish().unwrap(); + out +} + +#[test] +fn apng_warns_and_keeps_the_first_frame() { + for sixteen in [false, true] { + let still = decode(&apng(0, sixteen, false)).unwrap(); + assert_eq!(still.warnings, []); + assert_eq!(decode(&apng(1, sixteen, false)).unwrap().warnings, [], "a one-frame APNG is a still"); + let img = decode(&apng(3, sixteen, false)).unwrap(); + assert_eq!(img.warnings, more_frames(3), "16-bit {sixteen}"); + assert_eq!((img.sample_type(), img.data()), (still.sample_type(), still.data()), "16-bit {sixteen}: first frame pixels"); + // Default image + 2 animation frames: three images, the default one kept. + let img = decode(&apng(2, sixteen, true)).unwrap(); + assert_eq!(img.warnings, more_frames(3)); + assert_eq!(img.data(), still.data()); + } +} + +fn chunk(id: &[u8; 4], data: &[u8]) -> Vec { + let mut c = id.to_vec(); + c.extend_from_slice(&(data.len() as u32).to_le_bytes()); + c.extend_from_slice(data); + if data.len() % 2 == 1 { + c.push(0); + } + c +} + +fn u24(v: u32) -> [u8; 3] { + let b = v.to_le_bytes(); + [b[0], b[1], b[2]] +} + +/// An animated WebP: each frame is a full-canvas lossless still wrapped in an ANMF chunk. +fn animated_webp(frames: &[Image]) -> Vec { + let mut body = b"WEBP".to_vec(); + let mut vp8x = vec![0x02, 0, 0, 0]; // animation flag + vp8x.extend_from_slice(&u24(W - 1)); + vp8x.extend_from_slice(&u24(H - 1)); + body.extend(chunk(b"VP8X", &vp8x)); + body.extend(chunk(b"ANIM", &[0, 0, 0, 0, 0, 0])); + for f in frames { + let still = encode(f, Format::WebP, &EncodeOptions::default()).unwrap(); + let at = still.windows(4).position(|w| w == b"VP8L").unwrap(); + let len = u32::from_le_bytes(still[at + 4..at + 8].try_into().unwrap()) as usize; + let mut anmf = vec![0; 6]; // frame x, y + anmf.extend_from_slice(&u24(W - 1)); + anmf.extend_from_slice(&u24(H - 1)); + anmf.extend_from_slice(&u24(100)); // duration + anmf.push(0); + anmf.extend(chunk(b"VP8L", &still[at + 8..at + 8 + len])); + body.extend(chunk(b"ANMF", &anmf)); + } + let mut out = b"RIFF".to_vec(); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend(body); + out +} + +#[test] +fn animated_webp_warns_and_keeps_the_first_frame() { + let frames: Vec = COLOURS.iter().map(|&c| solid(c)).collect(); + let still = decode(&encode(&frames[0], Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(still.warnings, []); + assert_eq!(decode(&animated_webp(&frames[..1])).unwrap().warnings, [], "a one-frame animation"); + for n in [2, 3] { + let img = decode(&animated_webp(&frames[..n])).unwrap(); + assert_eq!(img.warnings, more_frames(n as u32)); + // The animation decoder composites the frame onto its canvas: within one level. + let (a, b) = (img.to_rgba8(), still.to_rgba8()); + assert!(a.len() == b.len() && a.iter().zip(&b).all(|(x, y)| x.abs_diff(*y) <= 1), "{n} frames: first frame pixels"); + } +} + +/// A TIFF with one IFD per entry: `(NewSubfileType, sample type)`. +fn tiff(ifds: &[(u32, SampleType)]) -> Vec { + use tiff::encoder::{TiffEncoder, colortype}; + use tiff::tags::Tag; + let mut out = std::io::Cursor::new(Vec::new()); + let mut enc = TiffEncoder::new(&mut out).unwrap(); + let n = (W * H) as usize; + // Each page is a flat grey at its own level (`k`), written at `sample` depth. + for (k, &(kind, sample)) in ifds.iter().enumerate() { + macro_rules! page { + ($ct:ty, $data:expr) => {{ + let mut im = enc.new_image::<$ct>(W, H).unwrap(); + if kind != 0 { + im.encoder().write_tag(Tag::NewSubfileType, kind).unwrap(); + } + im.write_data($data).unwrap(); + }}; + } + let v = 40 * k as u8 + 10; + match sample { + SampleType::U8 => page!(colortype::Gray8, &vec![v; n]), + SampleType::U16 => page!(colortype::Gray16, &vec![u16::from(v) * 257; n]), + _ => page!(colortype::Gray32Float, &vec![f32::from(v) / 255.0; n]), + } + } + out.into_inner() +} + +#[test] +fn multi_page_tiff_warns_and_keeps_the_first_page() { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let one = decode(&tiff(&[(0, sample)])).unwrap(); + assert_eq!(one.warnings, [], "{sample:?}"); + let img = decode(&tiff(&[(0, sample), (2, sample)])).unwrap(); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(2) }], "{sample:?}"); + assert_eq!(img.data(), one.data(), "{sample:?}: first page pixels"); + let img = decode(&tiff(&[(0, sample), (0, sample), (0, sample)])).unwrap(); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(3) }], "{sample:?}"); + assert_eq!(img.warnings[0].to_string(), "only the first of 3 pages was imported"); + } +} + +#[test] +fn tiff_thumbnails_and_masks_are_not_pages() { + let u8 = SampleType::U8; + // A reduced-resolution copy (bit 0) or a transparency mask (bit 2) is part of the page. + assert_eq!(decode(&tiff(&[(0, u8), (1, u8)])).unwrap().warnings, []); + assert_eq!(decode(&tiff(&[(0, u8), (4, u8)])).unwrap().warnings, []); + assert_eq!(decode(&tiff(&[(0, u8), (1, u8), (0, u8)])).unwrap().warnings, [DecodeWarning::MorePages { total: Some(2) }]); +} + +#[test] +fn single_images_carry_no_warnings() { + for f in rw_formats() { + let img = synth(9, 7, ChannelLayout::Rgba, SampleType::U8, 5, 0.2); + let Ok(bytes) = encode(&img, f, &EncodeOptions::default()) else { continue }; + assert_eq!(decode(&bytes).unwrap().warnings, [], "{f:?}"); + } +} + +#[test] +fn warnings_survive_orientation() { + let mut img = synth(8, 4, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.warnings.push(DecodeWarning::MorePages { total: None }); + for o in 1..=8 { + assert_eq!(img.clone().oriented(o).unwrap().warnings, img.warnings, "orientation {o}"); + } + assert_eq!(img.convert(ChannelLayout::Rgba, SampleType::U16).warnings, img.warnings); +} + +/// A flagless depth still declares slices the flat reader and writer cannot retain. +#[test] +fn dds_depth_probe_refuses_unretained_slices_without_volume_flags() { + let single = encode(&solid(COLOURS[0]), Format::Dds, &Default::default()).unwrap(); + let expected = decode(&single).unwrap(); + let mut second = single[128..].to_vec(); + second[0] ^= 0xff; + for depth in [0u32, 1, 2, 3] { + let mut bytes = single.clone(); + bytes[24..28].copy_from_slice(&depth.to_le_bytes()); + for _ in 1..depth { + bytes.extend_from_slice(&second); + } + let flags = u32::from_le_bytes(bytes[8..12].try_into().unwrap()); + let caps = u32::from_le_bytes(bytes[112..116].try_into().unwrap()); + assert_eq!(flags & 0x0080_0000, 0); + assert_eq!(caps & 0x0020_0000, 0); + // Decoding remains permissive for preview; only source replacement is refused. + assert_eq!(decode(&bytes).unwrap().data(), expected.data()); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), depth > 1); + } +} + +/// Header-only inspection protects unretained source payloads while allowing stills and thumbnails. +#[test] +fn additional_images_probe_uses_container_structure_without_decoding_pixels() { + use craft_image_codecs::has_unretained_source_data; + let frames: Vec<_> = COLOURS.iter().take(2).map(|&c| solid(c)).collect(); + for (bytes, multiple) in [ + (gif(1), false), + (gif(2), true), + (apng(0, false, false), false), + (apng(1, false, false), false), + (apng(1, false, true), true), + (apng(2, true, false), true), + (animated_webp(&frames[..1]), false), + (animated_webp(&frames), true), + (tiff(&[(0, SampleType::U8)]), false), + (tiff(&[(0, SampleType::U8), (2, SampleType::U8)]), true), + (tiff(&[(0, SampleType::U8), (1, SampleType::U8)]), false), + (tiff(&[(4, SampleType::U8)]), false), + (tiff(&[(0, SampleType::U8), (4, SampleType::U8)]), true), + (tiff(&[(4, SampleType::U8), (0, SampleType::U8)]), true), + (tiff(&[(0, SampleType::U8), (1, SampleType::U8), (4, SampleType::U8)]), true), + ] { + assert_eq!(has_unretained_source_data(&bytes).unwrap(), multiple); + } + // A GIF whose blocks break off before the trailer after one frame may hide later frames: + // the probe refuses it instead of reporting a still. + let one = gif(1); + assert!(has_unretained_source_data(&one[..one.len() - 1]).is_err(), "trailer cut off"); + let two = gif(2); + let mut broken = gif(1); + broken.pop(); + broken.push(0x99); + broken.extend_from_slice(&two[two.len() - 10..]); + assert!(has_unretained_source_data(&broken).is_err(), "junk block after the first frame"); + assert!(has_unretained_source_data(&two[..two.len() - 1]).unwrap(), "two frames seen, trailer cut off"); + let image = solid(COLOURS[0]); + for mipmaps in [false, true] { + let mut options = EncodeOptions::default(); + options.dds.mipmaps = mipmaps; + let bytes = encode(&image, Format::Dds, &options).unwrap(); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), mipmaps); + if mipmaps { + let mut flagless = bytes; + let flags = u32::from_le_bytes(flagless[8..12].try_into().unwrap()); + flagless[8..12].copy_from_slice(&(flags & !0x0002_0000).to_le_bytes()); + decode(&flagless).unwrap(); + // Microsoft DDS_HEADER: readers cannot rely on DDSD_MIPMAPCOUNT being set. + assert!(has_unretained_source_data(&flagless).unwrap()); + } + } + let dpx = encode(&image, Format::Dpx, &Default::default()).unwrap(); + for little in [false, true] { + let mut bytes = dpx.clone(); + if little { + bytes[..4].copy_from_slice(b"XPDS"); + for at in [4, 772, 776, 780, 808, 812] { + bytes[at..at + 4].reverse(); + } + for at in [768, 770, 804, 806] { + bytes[at..at + 2].reverse(); + } + } + assert!(!has_unretained_source_data(&bytes).unwrap()); + for count in [2u16, 8] { + bytes[770..772].copy_from_slice(&if little { count.to_le_bytes() } else { count.to_be_bytes() }); + assert!(has_unretained_source_data(&bytes).unwrap()); + } + bytes[770..772].fill(0); + assert!(has_unretained_source_data(&bytes).is_err()); + assert!(has_unretained_source_data(&bytes[..771]).is_err()); + } + let one = encode(&image, Format::Ico, &Default::default()).unwrap(); + assert!(!has_unretained_source_data(&one).unwrap()); + let mut multiple = one[..6].to_vec(); + multiple[4..6].copy_from_slice(&2u16.to_le_bytes()); + let mut entry = one[6..22].to_vec(); + let offset = u32::from_le_bytes(entry[12..16].try_into().unwrap()) + 16; + entry[12..16].copy_from_slice(&offset.to_le_bytes()); + multiple.extend_from_slice(&entry); + multiple.extend_from_slice(&entry); + multiple.extend_from_slice(&one[22..]); + decode(&multiple).unwrap(); + assert!(has_unretained_source_data(&multiple).unwrap()); + assert!(has_unretained_source_data(b"\x89PNG\r\n\x1a\n").is_err()); +} + +/// FITS RGB planes form one image; other cubes and later image HDUs would be discarded. +#[test] +fn fits_additional_images_probe_distinguishes_color_planes_and_hdus() { + let hdu = |primary: bool, axes: &[usize]| { + let mut cards = vec![ + if primary { "SIMPLE = T".to_string() } else { "XTENSION= 'IMAGE'".to_string() }, + "BITPIX = 8".into(), + format!("NAXIS = {}", axes.len() + 2), + "NAXIS1 = 1".into(), + "NAXIS2 = 1".into(), + ]; + cards.extend(axes.iter().enumerate().map(|(index, n)| format!("NAXIS{} = {n}", index + 3))); + cards.push("END".into()); + let mut bytes = Vec::new(); + for card in cards.drain(..) { + bytes.extend_from_slice(format!("{card:<80}").as_bytes()); + } + bytes.resize(bytes.len().div_ceil(2880) * 2880, b' '); + bytes.extend((0..axes.iter().product::()).map(|i| i as u8 + 1)); + bytes.resize(bytes.len().div_ceil(2880) * 2880, 0); + bytes + }; + for (axes, multiple) in [(vec![1], false), (vec![2], true), (vec![3], false), (vec![4], true), (vec![1, 3], true), (vec![3, 1], false)] { + let bytes = hdu(true, &axes); + let image = decode(&bytes).unwrap(); + assert_eq!(image.warnings.iter().any(|w| matches!(w, DecodeWarning::MorePages { .. })), multiple); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), multiple); + } + let mut bytes = hdu(true, &[1]); + bytes.extend(hdu(false, &[1])); + decode(&bytes).unwrap(); + assert!(has_unretained_source_data(&bytes).unwrap()); + let mut incomplete = hdu(true, &[1]); + incomplete.extend_from_slice(b"XTENSION= 'IMAGE'"); + assert!(has_unretained_source_data(&incomplete).is_err()); +} + +/// Tables and extension heaps are source data even though the flat image decoder skips them. +#[test] +fn fits_source_probe_preserves_nonimage_payload_and_allows_empty_extensions() { + let primary = encode(&solid(COLOURS[0]), Format::Fits, &Default::default()).unwrap(); + for (kind, rows, heap, unretained) in + [("BINTABLE", 1, 0, true), ("TABLE", 1, 0, true), ("OTHER", 1, 0, true), ("BINTABLE", 0, 4, true), ("BINTABLE", 0, 0, false)] + { + let mut bytes = primary.clone(); + for card in [ + format!("XTENSION= '{kind}'"), + "BITPIX = 8".into(), + "NAXIS = 2".into(), + "NAXIS1 = 4".into(), + format!("NAXIS2 = {rows}"), + format!("PCOUNT = {heap}"), + "GCOUNT = 1".into(), + "TFIELDS = 1".into(), + "TTYPE1 = 'DATA'".into(), + "TFORM1 = '4B'".into(), + "END".into(), + ] { + bytes.extend_from_slice(format!("{card:<80}").as_bytes()); + } + bytes.resize(bytes.len().div_ceil(2880) * 2880, b' '); + bytes.extend(std::iter::repeat_n(0xa5, rows * 4 + heap)); + bytes.resize(bytes.len().div_ceil(2880) * 2880, 0); + assert_eq!(decode(&bytes).unwrap().data(), decode(&primary).unwrap().data()); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), unretained, "{kind} rows={rows} heap={heap}"); + } + let mut heap = primary.clone(); + let end = heap.as_chunks::<80>().0.iter().position(|c| c.starts_with(b"END ")).unwrap() * 80; + heap[end..end + 160].fill(b' '); + heap[end..end + 11].copy_from_slice(b"PCOUNT = 1"); + heap[end + 80..end + 83].copy_from_slice(b"END"); + heap[2880 + (W * H * 3) as usize] = 0xa5; + decode(&heap).unwrap(); + assert!(has_unretained_source_data(&heap).unwrap()); + for data in [false, true] { + let mut bytes = Vec::new(); + for card in ["SIMPLE = T", "BITPIX = 8", if data { "NAXIS = 1" } else { "NAXIS = 0" }, "NAXIS1 = 2", "END"] { + bytes.extend_from_slice(format!("{card:<80}").as_bytes()); + } + bytes.resize(2880, b' '); + if data { + bytes.extend_from_slice(&[0x11, 0x22]); + bytes.resize(5760, 0); + } + let mut image = primary.clone(); + image[..80].fill(b' '); + image[..17].copy_from_slice(b"XTENSION= 'IMAGE'"); + bytes.extend(image); + decode(&bytes).unwrap(); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), data, "empty primary versus skipped one-dimensional payload"); + } +} diff --git a/crates/image-codecs/tests/it/deep_exr.rs b/crates/image-codecs/tests/it/deep_exr.rs new file mode 100644 index 000000000..a3c003f75 --- /dev/null +++ b/crates/image-codecs/tests/it/deep_exr.rs @@ -0,0 +1,992 @@ +//! Deep EXR: a synthetic generator builds deep chunks per the OpenEXR file layout +//! specification (NONE, RLE and ZIPS × scanline and tile), the oracle tests check the +//! compositing math against hand-computed values, and malformed inputs must error, never +//! panic. With the `corpus` feature, the OpenEXR project's deep test images (`corpus/exr/`, +//! pinned) are checked against the ID manifests their sidecars list. + +use craft_image_codecs::CodecError; +use craft_image_codecs::f16; +use craft_image_codecs::{Limits, decode, decode_deep_exr}; +use std::io::Write as _; + +// --------------------------------------------------------------------------------------------- +// A minimal deep-EXR writer, mirroring what the reference files show on disk: +// version 2 | deep bit (and the tile bit for tiles), one header with the mandatory +// attributes in alphabetical order, a chunk offset table, then deep chunks of +// [y | tile coords][u64 packed table][u64 packed data][u64 unpacked data][table][data]. +// The table holds u32 sample counts, cumulative within each row of the block; the sample +// data is grouped per row, then per channel, in pixel order (the layout the OpenEXR library +// writes). NONE stores raw; RLE and ZIPS separate even/odd bytes, apply the +128 predictor, +// then RLE packs runs and ZIPS deflates. + +fn attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { + out.extend_from_slice(name.as_bytes()); + out.push(0); + out.extend_from_slice(ty.as_bytes()); + out.push(0); + out.extend_from_slice(&(payload.len() as i32).to_le_bytes()); + out.extend_from_slice(payload); +} + +fn samples_to_differences(buf: &mut [u8]) { + if let Some(&first) = buf.first() { + let mut prev = i16::from(first); + for b in buf.iter_mut().skip(1) { + let v = i16::from(*b); + *b = (v - prev + 128) as u8; + prev = v; + } + } +} + +fn separate_bytes_fragments(buf: &mut [u8]) { + let copy = buf.to_vec(); + let half = copy.len().div_ceil(2); + for i in 0..copy.len() { + buf[if i % 2 == 0 { i / 2 } else { half + i / 2 }] = copy[i]; + } +} + +fn rle_bytes(src: &[u8]) -> Vec { + let mut out = Vec::new(); + let mut i = 0; + while i < src.len() { + let mut run = 1; + while i + run < src.len() && run < 128 && src[i + run] == src[i] { + run += 1; + } + // OpenEXR tokens: `n >= 0` repeats the next byte n + 1 times, `n < 0` copies -n bytes. + if run > 1 { + out.push((run - 1) as u8); + out.push(src[i]); + i += run; + } else { + let start = i; + i += 1; + while i < src.len() && (i - start) < 127 && src[i] != src[i - 1] { + i += 1; + } + let n = i - start; + out.push((-(n as i32)) as u8); + out.extend_from_slice(&src[start..start + n]); + } + } + out +} + +fn compress(kind: u8, raw: &[u8]) -> Vec { + match kind { + 1 => { + // Like ZIPS, RLE packs the byte-split, predicted stream. + let mut b = raw.to_vec(); + separate_bytes_fragments(&mut b); + samples_to_differences(&mut b); + let c = rle_bytes(&b); + if c.len() < raw.len() { c } else { raw.to_vec() } + } + 2 => { + let mut b = raw.to_vec(); + separate_bytes_fragments(&mut b); + samples_to_differences(&mut b); + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + // Writing to a Vec never fails; the encoder errors surface in `finish`. + #[allow(clippy::unwrap_used)] + z.write_all(&b).unwrap(); + #[allow(clippy::unwrap_used)] + let c = z.finish().unwrap(); + if c.len() < raw.len() { c } else { raw.to_vec() } + } + _ => raw.to_vec(), + } +} + +/// One channel: a name (the caller must pass channels in alphabetical order, as chlist +/// requires) and the sample type as in the chlist attribute (0 = u32, 1 = half, 2 = float). +struct Chan { + name: &'static str, + ty: i32, +} + +/// `counts[p]` samples per pixel; `sample(p, ci)` returns channel `ci`'s values for pixel +/// `p` (its length must be `counts[p]`). `tile` switches between deepscanline and deeptile. +#[allow(clippy::too_many_arguments)] +fn gen_deep( + w: usize, + h: usize, + channels: &[Chan], + compression: u8, + tile: Option<(usize, usize)>, + counts: &[u32], + sample: &dyn Fn(usize, usize) -> Vec, +) -> Vec { + gen_deep_tables(w, h, channels, compression, tile, counts, sample, false) +} + +/// [`gen_deep`]; with `full_tile_tables` every tile's count table is stored at the full tile +/// size, as the OpenEXR library writes edge tiles: the block's entries first (in rows of its +/// clipped width), then zeros. +#[allow(clippy::too_many_arguments)] +fn gen_deep_tables( + w: usize, + h: usize, + channels: &[Chan], + compression: u8, + tile: Option<(usize, usize)>, + counts: &[u32], + sample: &dyn Fn(usize, usize) -> Vec, + full_tile_tables: bool, +) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); + // Single-part deep files (deepscanline and deeptile alike) carry only the deep-data + // flag; the tiling lives in the `tiles` attribute and the `type` string. + out.extend_from_slice(&(2u32 | 0x800).to_le_bytes()); + + let mut chlist = Vec::new(); + for c in channels { + chlist.extend_from_slice(c.name.as_bytes()); + chlist.push(0); + chlist.extend_from_slice(&c.ty.to_le_bytes()); + chlist.push(0); // pLinear + chlist.extend_from_slice(&[0u8; 3]); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + } + chlist.push(0); + attr(&mut out, "channels", "chlist", &chlist); + attr(&mut out, "compression", "compression", &[compression]); + // box2i is inclusive: the data window of a w×h image at the origin ends at (w-1, h-1). + let dw: Vec = [0i32, 0, w as i32 - 1, h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut out, "dataWindow", "box2i", &dw); + attr(&mut out, "displayWindow", "box2i", &dw); + attr(&mut out, "lineOrder", "lineOrder", &[0]); + attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut out, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); + if let Some((tw, th)) = tile { + let mut td = Vec::new(); + td.extend_from_slice(&(tw as u32).to_le_bytes()); + td.extend_from_slice(&(th as u32).to_le_bytes()); + td.push(0); // one level, round down + attr(&mut out, "tiles", "tiledesc", &td); + } + attr(&mut out, "type", "string", if tile.is_some() { b"deeptile" } else { b"deepscanline" }); + attr(&mut out, "version", "int", &1i32.to_le_bytes()); + out.push(0); // end of the header + + // The blocks: scan lines (one line each) or the level-0 tiles. + struct Block { + pixels: Vec, + /// Pixels per row: the count table restarts and the sample data is grouped per row. + row_w: usize, + } + let blocks: Vec = match tile { + None => (0..h).map(|y| Block { pixels: (y * w..y * w + w).collect(), row_w: w }).collect(), + Some((tw, th)) => { + let tx = w.div_ceil(tw); + let ty = h.div_ceil(th); + let mut v = Vec::new(); + for by in 0..ty { + for bx in 0..tx { + let mut px = Vec::new(); + for yy in (by * th)..((by * th + th).min(h)) { + for xx in (bx * tw)..((bx * tw + tw).min(w)) { + px.push(yy * w + xx); + } + } + v.push(Block { pixels: px, row_w: (bx * tw + tw).min(w) - bx * tw }); + } + } + v + } + }; + + let table_len = |b: &Block| b.pixels.len() * 4; + let data_len = |b: &Block| -> usize { + let samples: u64 = b.pixels.iter().map(|&p| counts[p] as u64).sum(); + channels.iter().map(|c| samples as usize * if c.ty == 1 { 2 } else { 4 }).sum() + }; + + // Build every chunk, remember the offsets, then write the offset table first. + let mut chunks: Vec<(usize, Vec)> = Vec::new(); // (block index, framed chunk) + for (bi, b) in blocks.iter().enumerate() { + let mut c = Vec::new(); + match tile { + None => { + let y = (bi % h) as i32; + c.extend_from_slice(&y.to_le_bytes()); + } + Some((tw, _)) => { + let tx = w.div_ceil(tw); + let (bx, by) = (bi % tx, bi / tx); + c.extend_from_slice(&(bx as i32).to_le_bytes()); + c.extend_from_slice(&(by as i32).to_le_bytes()); + c.extend_from_slice(&0i32.to_le_bytes()); + c.extend_from_slice(&0i32.to_le_bytes()); + } + } + let mut table = Vec::with_capacity(b.pixels.len() * 4); + for row in b.pixels.chunks(b.row_w) { + let mut acc = 0u32; + for &p in row { + acc += counts[p]; + table.extend_from_slice(&acc.to_le_bytes()); + } + } + assert_eq!(table.len(), table_len(b)); + if let (true, Some((tw, th))) = (full_tile_tables, tile) { + table.resize(tw * th * 4, 0); + } + let mut data = Vec::new(); + for row in b.pixels.chunks(b.row_w) { + for (ci, ch) in channels.iter().enumerate() { + for &p in row { + for v in sample(p, ci) { + match ch.ty { + 1 => data.extend_from_slice(&f16::from_f32(v).to_le_bytes()), + 0 => data.extend_from_slice(&(v as u32).to_le_bytes()), + _ => data.extend_from_slice(&v.to_le_bytes()), + } + } + } + } + } + let ct = compress(compression, &table); + let cd = compress(compression, &data); + c.extend_from_slice(&(ct.len() as u64).to_le_bytes()); + c.extend_from_slice(&(cd.len() as u64).to_le_bytes()); + c.extend_from_slice(&(data.len() as u64).to_le_bytes()); + c.extend_from_slice(&ct); + c.extend_from_slice(&cd); + assert_eq!(data.len(), data_len(b)); + chunks.push((bi, c)); + } + let mut pos = out.len() + chunks.len() * 8; + let mut table = Vec::new(); + for (_, c) in &chunks { + table.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += c.len(); + } + out.extend_from_slice(&table); + for (_, c) in chunks { + out.extend_from_slice(&c); + } + out +} + +/// RGBA float channels in chlist (alphabetical) order, no Z (everything composites). +const RGBA_NO_Z: [Chan; 4] = [Chan { name: "A", ty: 2 }, Chan { name: "B", ty: 2 }, Chan { name: "G", ty: 2 }, Chan { name: "R", ty: 2 }]; + +/// RGBA(Z) float channels in chlist (alphabetical) order. +const RGBAZ: [Chan; 5] = + [Chan { name: "A", ty: 2 }, Chan { name: "B", ty: 2 }, Chan { name: "G", ty: 2 }, Chan { name: "R", ty: 2 }, Chan { name: "Z", ty: 2 }]; + +fn sample_rgba(p: usize, ci: usize) -> Vec { + // One sample per pixel: a small checkerboard-ish pattern with varying depth. Deep + // colours are premultiplied, so the stored RGB values include the 0.5 alpha. + let v = [0.25f32, 0.5, 0.75, 1.0][p % 4]; + match ci { + 0 => vec![0.5], // A + 1 => vec![0.125 * v], // B = straight 0.25v, premultiplied + 2 => vec![0.25 * v], // G = straight 0.5v, premultiplied + 3 => vec![0.5 * v], // R = straight v, premultiplied + _ => vec![10.0 + p as f32], // Z + } +} + +const COUNTS_ONE: &[u32] = &[1; 16]; // for a 4x4 image + +fn rgba_flat(bytes: &[u8]) -> Vec { + let img = decode(bytes).expect("decodes"); + let s = img.to_f32_samples().expect("f32"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgba); + assert_eq!(img.sample_type(), craft_image_codecs::SampleType::F32); + assert!(img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::DeepFlattened { .. }))); + s +} + +// --------------------------------------------------------------------------------------------- +// Oracles + +/// One sample per pixel: the flat image is the stored colour un-premultiplied. +#[test] +fn single_sample_matches_flat() { + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let px = rgba_flat(&bytes); + for p in 0..16 { + let v = [0.25f32, 0.5, 0.75, 1.0][p % 4]; + let (r, g, b, a) = (px[p * 4], px[p * 4 + 1], px[p * 4 + 2], px[p * 4 + 3]); + assert!((r - v).abs() < 1e-6, "r at {p}: {r}"); + assert!((g - 0.5 * v).abs() < 1e-6, "g at {p}: {g}"); + assert!((b - 0.25 * v).abs() < 1e-6, "b at {p}: {b}"); + assert!((a - 0.5).abs() < 1e-6, "a at {p}: {a}"); + } +} + +/// Two samples stored back-to-front: the front (smaller Z) covers the back. Hand-computed: +/// front a=0.5, premultiplied colour (0.5, 0.25, 0); back a=0.25, premultiplied (0.25, 0.25, 0.25). +/// Over (front first): a = 0.5 + (1−0.5)·0.25 = 0.625; c = (0.5 + 0.5·0.25, 0.25 + 0.5·0.25, +/// 0 + 0.5·0.25) = (0.625, 0.375, 0.125); straight = c / a = (1.0, 0.6, 0.2). +#[test] +fn z_order_composites_front_over_back() { + // pixel 0: samples [(z=9, back), (z=1, front)] — the file order is reversed on purpose. + let counts = vec![2u32; 16]; + let sample = |p: usize, ci: usize| -> Vec { + if p != 0 { + let v = sample_rgba(p, ci); + return vec![v[0], v[0]]; + } + match ci { + 0 => vec![0.25, 0.5], // A: back first + 1 => vec![0.25, 0.0], // B + 2 => vec![0.25, 0.25], // G + 3 => vec![0.25, 0.5], // R + _ => vec![9.0, 1.0], // Z + } + }; + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let px = rgba_flat(&bytes); + let a = 0.5f32 + 0.5 * 0.25; + assert!((px[3] - a).abs() < 1e-6, "alpha {}", px[3]); + assert!((px[0] - 0.625 / a).abs() < 1e-6, "r {}", px[0]); + assert!((px[1] - 0.375 / a).abs() < 1e-6, "g {}", px[1]); + assert!((px[2] - 0.125 / a).abs() < 1e-6, "b {}", px[2]); +} + +/// A fully opaque front sample hides everything behind it. +#[test] +fn opaque_front_hides_back() { + let counts = vec![3u32; 16]; + let sample = |p: usize, ci: usize| -> Vec { + if p != 1 { + let v = sample_rgba(p, ci); + return vec![v[0], v[0], v[0]]; + } + match ci { + 0 => vec![1.0, 0.9, 1.0], // front opaque, middle partial, back opaque + 1 => vec![0.0, 1.0, 1.0], + 2 => vec![1.0, 1.0, 1.0], + 3 => vec![1.0, 0.0, 0.0], + _ => vec![1.0, 5.0, 9.0], + } + }; + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let px = rgba_flat(&bytes); + let (r, g, b, a) = (px[4], px[5], px[6], px[7]); + assert!((r - 1.0).abs() < 1e-6 && (g - 1.0).abs() < 1e-6 && (b - 0.0).abs() < 1e-6, "colour ({r},{g},{b})"); + assert!((a - 1.0).abs() < 1e-6, "alpha {a}"); +} + +/// Deep EXR is HDR: colour above 1.0 survives the composite (it used to be clamped), and a +/// tiled file whose edge tiles are narrower than the tile size decodes like the scan lines. +#[test] +fn hdr_colour_is_kept_and_edge_tiles_match_scanlines() { + let (w, h) = (7, 5); + let counts: Vec = (0..w * h).map(|p| (p as u32 * 3) % 4).collect(); + let sample = + |p: usize, ci: usize| -> Vec { (0..(p as u32 * 3) % 4).map(|k| [1.0, 0.25 * k as f32, 0.5, 2.0 + p as f32 / 8.0, 10.0 - k as f32][ci]).collect() }; + for compression in [0u8, 1, 2] { + let scan = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, None, &counts, &sample)); + let tiled = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, Some((3, 2)), &counts, &sample)); + assert_eq!(scan, tiled, "compression {compression}"); + // Pixel 1 has 3 opaque samples; the front one (k = 2, Z = 8) has R = 2.125. + assert!((scan[4] - 2.125).abs() < 1e-5, "compression {compression}: R {}", scan[4]); + } +} + +/// Pixels without samples are transparent black, and the structured counts say so. +#[test] +fn empty_pixels_are_transparent() { + let mut counts = vec![0u32; 16]; + counts[5] = 1; + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &|p, ci| { + if p == 5 { vec![[1.0f32, 0.5, 0.5, 0.5, 1.0][ci]] } else { Vec::new() } + }); + let px = rgba_flat(&bytes); + for p in 0..16 { + let base = p * 4; + if p == 5 { + assert!((px[base + 3] - 1.0).abs() < 1e-6); + } else { + assert!(px[base..base + 4].iter().all(|v| *v == 0.0), "pixel {p} not empty"); + } + } + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + assert_eq!(deep.total_samples(), 1); + assert_eq!(deep.sample_range(5).end, 1); +} + +/// RLE, ZIPS and NONE decode to identical pixels and identical structured samples. +#[test] +fn rle_and_zips_match_uncompressed() { + let counts: Vec = (0..16).map(|p| if p % 3 == 0 { 2 } else { 1 }).collect(); + let sample = |p: usize, ci: usize| -> Vec { + if counts[p] == 2 { + match ci { + 0 => vec![0.75, 0.25], + 1 => vec![0.1, 0.2], + 2 => vec![0.3, 0.4], + 3 => vec![0.5, 0.6], + _ => vec![7.0, 3.0], + } + } else { + sample_rgba(p, ci) + } + }; + let base = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let px = rgba_flat(&base); + let deep_base = decode_deep_exr(&base, &Limits::none()).expect("deep"); + for kind in [1u8, 2] { + let bytes = gen_deep(4, 4, &RGBAZ, kind, None, &counts, &sample); + let other = rgba_flat(&bytes); + for (i, (a, b)) in px.iter().zip(other.iter()).enumerate() { + assert!((a - b).abs() < 1e-6, "compression {kind}, sample {i}: {a} vs {b}"); + } + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + assert_eq!(deep.counts, deep_base.counts); + for (a, b) in deep.channels.iter().zip(deep_base.channels.iter()) { + assert_eq!(a.name, b.name); + assert_eq!(a.samples, b.samples, "channel {}", a.name); + } + } +} + +/// Deep tiles decode to the same pixels as deep scanlines of the same data. +#[test] +fn tiles_match_scanlines() { + let counts = vec![1u32, 0, 2, 1, 1, 1, 0, 1, 2, 1, 1, 0, 1, 1, 1, 1]; + let sample = |p: usize, ci: usize| -> Vec { + if counts[p] == 2 { + match ci { + 0 => vec![0.5, 0.5], + 1 => vec![0.2, 0.1], + 2 => vec![0.3, 0.3], + 3 => vec![0.4, 0.4], + _ => vec![2.0, 8.0], + } + } else if counts[p] > 0 { + vec![[0.5, 0.2, 0.4, 0.7, 4.0][ci]] + } else { + Vec::new() + } + }; + let sl = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let tiled = gen_deep(4, 4, &RGBAZ, 0, Some((2, 2)), &counts, &sample); + let a = rgba_flat(&sl); + let b = rgba_flat(&tiled); + for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() { + assert!((x - y).abs() < 1e-6, "tile sample {i}: {x} vs {y}"); + } +} + +/// Half-float channels come back as F16 samples with exact values. +#[test] +fn half_channels_round_trip() { + let channels = [Chan { name: "A", ty: 1 }, Chan { name: "B", ty: 1 }, Chan { name: "G", ty: 1 }, Chan { name: "R", ty: 1 }, Chan { name: "Z", ty: 1 }]; + let bytes = gen_deep(4, 4, &channels, 2, None, COUNTS_ONE, &|p, _ci| { + let v = f16::from_f32(0.5).to_f32(); + vec![v * (p as f32 + 1.0) / 4.0] + }); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + for c in &deep.channels { + assert_eq!(c.sample, craft_image_codecs::SampleType::F16, "{}", c.name); + } + let a = deep.channel("A").expect("A"); + assert_eq!(a.samples.len(), 16); +} + +/// Without an alpha channel the front-most sample wins, opaque; luminance maps to gray. +#[test] +fn without_alpha_front_sample_is_opaque() { + let channels = [Chan { name: "B", ty: 2 }, Chan { name: "G", ty: 2 }, Chan { name: "R", ty: 2 }, Chan { name: "Z", ty: 2 }]; + let counts = vec![2u32; 16]; + let bytes = gen_deep(4, 4, &channels, 0, None, &counts, &|p, ci| { + if p % 2 == 0 { + match ci { + 0 => vec![0.9, 0.1], + 1 => vec![0.2, 0.8], + 2 => vec![0.4, 0.6], + _ => vec![2.0, 6.0], + } + } else { + vec![[0.0, 0.0, 1.0, 1.0][ci], [0.0, 0.0, 1.0, 9.0][ci]] + } + }); + let img = decode(&bytes).expect("decodes"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgb); + let px = img.to_f32_samples().expect("f32"); + // Even pixels: the front sample (z=2) is (r 0.4, g 0.2, b 0.9). + assert!((px[0] - 0.4).abs() < 1e-6 && (px[1] - 0.2).abs() < 1e-6 && (px[2] - 0.9).abs() < 1e-6); +} + +/// Grayscale deep files (Y + Z) flatten to opaque gray, Y + A + Z keeps its alpha: the alpha +/// must land in the GrayA alpha slot, not a luminance copy (that was the bug). +#[test] +fn grayscale_deep_keeps_its_alpha() { + let channels = [Chan { name: "A", ty: 2 }, Chan { name: "Y", ty: 2 }, Chan { name: "Z", ty: 2 }]; + let bytes = gen_deep(4, 4, &channels, 0, None, COUNTS_ONE, &|p, ci| { + match ci { + 0 => vec![0.5], // A + 1 => vec![0.5], // Y, premultiplied: straight 1.0 × 0.5 + _ => vec![p as f32], // Z + } + }); + let img = decode(&bytes).expect("decodes"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::GrayA); + let px = img.to_f32_samples().expect("f32"); + for (p, s) in px.chunks(2).enumerate() { + let (y, a) = (s[0], s[1]); + assert!((a - 0.5).abs() < 1e-6, "pixel {p}: alpha {a}, want 0.5"); + assert!((y - 1.0).abs() < 1e-6, "pixel {p}: luminance {y}, want the un-premultiplied 1.0"); + } + + // Without an alpha channel the same file is opaque gray. + let channels = [Chan { name: "Y", ty: 2 }, Chan { name: "Z", ty: 2 }]; + let bytes = gen_deep(4, 4, &channels, 0, None, COUNTS_ONE, &|p, ci| match ci { + 0 => vec![0.4], + _ => vec![p as f32], + }); + let img = decode(&bytes).expect("decodes"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Gray); + let px = img.to_f32_samples().expect("f32"); + for (p, v) in px.iter().enumerate() { + assert!((v - 0.4).abs() < 1e-6, "pixel {p}: {v}, want 0.4"); + } +} + +/// decode_deep returns the counts and channels in file order. +#[test] +fn decode_deep_structure() { + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + assert_eq!((deep.width, deep.height), (4, 4)); + assert_eq!(deep.channels.iter().map(|c| c.name.as_str()).collect::>(), ["A", "B", "G", "R", "Z"]); + assert_eq!(deep.counts.len(), 17); + assert_eq!(deep.counts[0], 0); + assert_eq!(deep.total_samples(), 16); + for p in 0..16 { + assert_eq!(deep.sample_range(p).end - deep.sample_range(p).start, 1); + } + assert_eq!(deep.channel("Z").expect("Z").samples[5], 15.0); +} + +// --------------------------------------------------------------------------------------------- +// Hostile input: every damaged file errors, nothing panics. + +fn truncate_sweep(bytes: &[u8]) { + // Cut at every offset (cap the count for speed) — a cut file must error, not panic. + let step = (bytes.len() / 96).max(1); + let mut i = step; + while i < bytes.len() { + let cut = &bytes[..i]; + let _ = decode_deep_exr(cut, &Limits::none()); + let _ = decode(cut); + i += step; + } +} + +#[test] +fn truncated_files_error_never_panic() { + for kind in [0u8, 1, 2] { + let bytes = gen_deep(4, 4, &RGBAZ, kind, None, COUNTS_ONE, &sample_rgba); + truncate_sweep(&bytes); + } + let tiled = gen_deep(4, 4, &RGBAZ, 1, Some((2, 2)), COUNTS_ONE, &sample_rgba); + truncate_sweep(&tiled); +} + +#[test] +fn hostile_sizes_and_offsets_error() { + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + // A tiny sample-data budget rejects the file. + let tight = Limits { max_alloc: 64, ..Limits::none() }; + assert!(matches!(decode_deep_exr(&bytes, &tight), Err(CodecError::LimitExceeded(_)))); + // A non-deep file is unsupported for decode_deep. + let flat = craft_image_codecs::encode( + &craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgba, [128; 16].to_vec()).expect("img"), + craft_image_codecs::Format::OpenExr, + &Default::default(), + ) + .expect("encode"); + assert!(matches!(decode_deep_exr(&flat, &Limits::none()), Err(CodecError::Unsupported { .. }))); + // The last chunk's y coordinate destroyed: out of the data window. + let mut broken = bytes.clone(); + let header_end = header_end_of(&broken); + let last_off = u64::from_le_bytes(broken[header_end + 24..header_end + 32].try_into().expect("8")) as usize; + broken[last_off..last_off + 4].copy_from_slice(&i32::MAX.to_le_bytes()); + assert!(decode_deep_exr(&broken, &Limits::none()).is_err()); + assert!(decode(&broken).is_err()); + // Two chunks claiming the same block: duplicate offsets. + let mut dup = bytes.clone(); + let header_end = header_end_of(&dup); + let first: [u8; 8] = dup[header_end..header_end + 8].try_into().expect("8"); + dup[header_end + 8..header_end + 16].copy_from_slice(&first); + assert!(matches!(decode_deep_exr(&dup, &Limits::none()), Err(CodecError::Malformed { .. }))); + // An offset pointing past the file. + let mut far = bytes.clone(); + far[header_end..header_end + 8].copy_from_slice(&u64::MAX.to_le_bytes()); + assert!(matches!(decode_deep_exr(&far, &Limits::none()), Err(CodecError::Malformed { .. }))); +} + +/// #4958: a deep tile's count table may be stored at the full tile size, and that size comes +/// from the header alone: nothing ties it to the data window. One 1024×1024 tile over a 4×4 +/// image, its table stored at that size, makes a compressed file of tens of KB whose table +/// inflates to 4 MiB, plus as much again for the copy that undoes the byte split. Under a +/// budget smaller than both the table must be refused before it is allocated; with room for +/// both it decodes. Tables stored at the clipped size, and the full-size edge tables of +/// ordinary tiles, still decode under a small budget. +#[test] +fn deep_tile_count_table_is_admitted_against_max_alloc() { + let budget = |max_alloc: u64| Limits { max_alloc, ..Limits::none() }; + let tight = budget(1 << 20); + let table: u64 = 1024 * 1024 * 4; // the full tile's count table, in bytes + let scan = decode_deep_exr(&gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba), &Limits::none()).expect("scan lines"); + for compression in [0u8, 1, 2] { + let big = gen_deep_tables(4, 4, &RGBAZ, compression, Some((1024, 1024)), COUNTS_ONE, &sample_rgba, true); + assert_eq!(decode_deep_exr(&big, &Limits::none()).expect("full-size table, no limits"), scan, "compression {compression}"); + let got = decode_deep_exr(&big, &tight); + assert!( + matches!(got, Err(CodecError::LimitExceeded(_))), + "compression {compression}: a 4 MiB count table under a 1 MiB budget gave {:?}", + got.map(|d| d.total_samples()) + ); + let flat = craft_image_codecs::decode_with(&big, &craft_image_codecs::DecodeOptions { limits: tight, ..Default::default() }); + assert!(matches!(flat, Err(CodecError::LimitExceeded(_))), "compression {compression}: the flat decode gave {:?}", flat.map(|i| i.width())); + // The same tile with its table stored at the clipped size fits the small budget. + let clipped = gen_deep(4, 4, &RGBAZ, compression, Some((1024, 1024)), COUNTS_ONE, &sample_rgba); + assert_eq!(decode_deep_exr(&clipped, &tight).expect("clipped table, 1 MiB budget"), scan, "compression {compression}"); + if compression != 0 { + assert!(big.len() < 1 << 20, "compression {compression}: the file is {} bytes", big.len()); + } + // The table and its scratch copy are admitted together: 8 MiB. + assert_eq!(decode_deep_exr(&big, &budget(2 * table)).expect("room for the table and its copy"), scan, "compression {compression}"); + let got = decode_deep_exr(&big, &budget(2 * table - 1)); + assert!(matches!(got, Err(CodecError::LimitExceeded(_))), "compression {compression}: one byte short gave {:?}", got.map(|d| d.total_samples())); + } + // Ordinary tiles: full-size edge tables decode under the small budget, like clipped ones. + let counts: Vec = (0..35).map(|p| (p * 3) % 4).collect(); + let sample = |p: usize, ci: usize| -> Vec { (0..counts[p]).map(|k| [0.5, 0.25, 0.125 * k as f32, 1.0, 10.0 - k as f32][ci]).collect() }; + for compression in [0u8, 1, 2] { + let full = gen_deep_tables(7, 5, &RGBAZ, compression, Some((3, 2)), &counts, &sample, true); + let clipped = gen_deep(7, 5, &RGBAZ, compression, Some((3, 2)), &counts, &sample); + assert_eq!( + decode_deep_exr(&full, &tight).expect("full-size edge tables"), + decode_deep_exr(&clipped, &tight).expect("clipped edge tables"), + "compression {compression}" + ); + } +} + +/// #4958, the edges of that admission. A tile of the largest size exr accepts (2^30 - 2 a side, +/// a 2^62 byte count table) decodes from a clipped table under every budget, including no limits, +/// where its full size passes the check and is only tried as the data arrives. Each tile is +/// admitted on its own, because its table is freed before the next chunk: three 16×16 tiles over +/// a 48×1 image are all edge tiles, so every full-size table reaches the check (an interior tile's +/// full size equals its clipped size, so it uses the clipped check). They need 2 KiB each for +/// the table and its copy, 6 KiB in all, and decode under a budget of 4096 bytes. Half and uint +/// channels decode after full-size tables like float ones. +#[test] +fn deep_tile_table_admission_edges() { + let budget = |max_alloc: u64| Limits { max_alloc, ..Limits::none() }; + let scan = decode_deep_exr(&gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba), &Limits::none()).expect("scan lines"); + let side = (1 << 30) - 2; + for compression in [0u8, 1, 2] { + let bytes = gen_deep(4, 4, &RGBAZ, compression, Some((side, side)), COUNTS_ONE, &sample_rgba); + for limits in [Limits::none(), Limits::default(), budget(1 << 20)] { + assert_eq!(decode_deep_exr(&bytes, &limits).expect("the largest tile, clipped table"), scan, "compression {compression}, {limits:?}"); + } + } + let counts: Vec = (0..48).map(|p| (p % 3) as u32).collect(); + let half: [Chan; 5] = RGBAZ.map(|c| Chan { ty: 1, ..c }); + let [a, b, g, r, z] = RGBAZ; + let with_id = [a, b, g, r, z, Chan { name: "id", ty: 0 }]; + for channels in [&RGBAZ[..], &half[..], &with_id[..]] { + let sample = |p: usize, ci: usize| -> Vec { (0..counts[p]).map(|k| [0.5, 0.25, 0.125, 1.0, 2.0 + k as f32, 7.0][ci]).collect() }; + for compression in [0u8, 1, 2] { + let full = gen_deep_tables(48, 1, channels, compression, Some((16, 16)), &counts, &sample, true); + let clipped = gen_deep(48, 1, channels, compression, Some((16, 16)), &counts, &sample); + let what = format!("{} channels, compression {compression}", channels.len()); + let want = decode_deep_exr(&clipped, &Limits::none()).expect("clipped tables"); + assert_eq!(decode_deep_exr(&full, &budget(4096)).expect("full-size tables, admitted one at a time"), want, "{what}"); + } + } +} + +/// A one-channel data-window check admits only four bytes per pixel, but a compressed count +/// table and its scratch copy need eight. Cover scanlines, interior tiles and the clipped fallback; +/// the existing multichannel cases have enough pixel allowance to hide this gap. +#[test] +fn clipped_count_tables_include_scratch_in_the_budget() { + let channels = [Chan { name: "Y", ty: 2 }]; + let counts = vec![0u32; 64]; + let sample = |_: usize, _: usize| Vec::new(); + let budget = |max_alloc: u64| Limits { max_alloc, ..Limits::none() }; + for tile in [None, Some((64, 1)), Some((128, 2))] { + for compression in [0u8, 1, 2] { + let bytes = gen_deep(64, 1, &channels, compression, tile, &counts, &sample); + let what = format!("tile {tile:?}, compression {compression}"); + let want = decode_deep_exr(&bytes, &Limits::none()).expect("unlimited decode"); + let required = if compression == 0 { 256 } else { 512 }; + assert_eq!(decode_deep_exr(&bytes, &budget(required)).expect("table and any scratch fit exactly"), want, "{what}"); + assert!(matches!(decode_deep_exr(&bytes, &budget(required - 1)), Err(CodecError::LimitExceeded(_))), "{what}"); + let flat = craft_image_codecs::decode_with(&bytes, &craft_image_codecs::DecodeOptions { limits: budget(required - 1), ..Default::default() }); + assert!(matches!(flat, Err(CodecError::LimitExceeded(_))), "{what}"); + if compression == 0 { + assert_eq!(decode_deep_exr(&bytes, &budget(511)).expect("raw table needs no scratch"), want, "{what}"); + } + } + } +} + +/// RLE and ZIPS store incompressible tables raw too. Their actual payload, not the header's +/// compression tag, decides whether a scratch buffer is needed. +#[test] +fn raw_count_tables_need_no_scratch_even_with_compression() { + let channels = [Chan { name: "Y", ty: 2 }]; + let limits = Limits { max_alloc: 4, ..Limits::none() }; + for tile in [None, Some((1, 1)), Some((2, 2))] { + for compression in [0u8, 1, 2] { + let bytes = gen_deep(1, 1, &channels, compression, tile, &[0], &|_, _| Vec::new()); + let header_end = header_end_of(&bytes); + let chunk = u64::from_le_bytes(bytes[header_end..header_end + 8].try_into().expect("chunk offset")) as usize; + let table_size = chunk + if tile.is_some() { 16 } else { 4 }; + let packed = u64::from_le_bytes(bytes[table_size..table_size + 8].try_into().expect("table size")); + assert_eq!(packed, 4, "the four-byte count table is stored raw"); + let want = decode_deep_exr(&bytes, &Limits::none()).expect("unlimited decode"); + assert_eq!(decode_deep_exr(&bytes, &limits).expect("raw table fits"), want, "tile {tile:?}, compression {compression}"); + } + } +} + +/// Walk the attributes like the format prescribes: the offset table starts after the +/// header's terminating zero byte. +fn header_end_of(bytes: &[u8]) -> usize { + let mut p = 8usize; // magic + version + loop { + let Some(nul) = bytes[p..].iter().position(|&b| b == 0) else { panic!("unterminated attribute name") }; + if nul == 0 { + return p + 1; // the empty name terminates the header + } + p += nul + 1; + let Some(t) = bytes[p..].iter().position(|&b| b == 0) else { panic!("unterminated attribute type") }; + p += t + 1; + let size = i32::from_le_bytes(bytes[p..p + 4].try_into().expect("size")) as usize; + p += 4 + size; + } +} + +#[test] +fn inconsistent_count_table_errors() { + // The declared unpacked size disagrees with the count table. + let mut bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + // Find the first chunk: the offset table follows the header (find the "version" attr end). + let header_end = header_end_of(&bytes); + let first_chunk = u64::from_le_bytes(bytes[header_end..header_end + 8].try_into().expect("8")) as usize; + // Scanline chunk: y (4), three u64s (24); bump the unpacked size by one. + let at = first_chunk + 4 + 16; + let v = u64::from_le_bytes(bytes[at..at + 8].try_into().expect("8")); + bytes[at..at + 8].copy_from_slice(&(v + 1).to_le_bytes()); + assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed { .. }))); +} + +// --------------------------------------------------------------------------------------------- +// Real reference files: the deep test images of the OpenEXR repository, pinned in +// `xtask/src/corpus_pins.rs` and fetched into `corpus/exr/` by `cargo xtask corpus --exr` +// (cargo feature `corpus`; `cargo xtask test-corpus`). Their `.txt` sidecars upstream list the +// ID manifest of each file, an independent oracle for the u32 ID channels. + +/// One pinned file: name, total samples, most samples in one pixel, and per ID channel the +/// manifest IDs its sidecar lists. +#[cfg(feature = "corpus")] +type RealFile = (&'static str, u64, u64, &'static [(&'static str, &'static [u32])]); + +#[cfg(feature = "corpus")] +const MULTIVARIATE_IDS: &[u32] = &[ + 127035619, 147085119, 319332210, 409096490, 523461226, 678492822, 683859976, 1058109025, 1193275200, 1503246798, 1552124883, 1613501244, 1672208113, + 1687665471, 1741895317, 1841370478, 1863854195, 1865183722, 2056745377, 2202049807, 2303547305, 2369798380, 2433329986, 2439705590, 2505186565, 2698909330, + 2862333315, 2898564507, 2979371527, 3007860936, 3182339638, 3236172513, 3497437340, 3626859046, 3656820528, 3693645783, 3739501011, 3936563811, 4048943790, +]; + +#[cfg(feature = "corpus")] +const REAL_FILES: &[RealFile] = &[ + ( + "11.deep.exr", + 6056, + 2, + &[("id", &[523461226, 683859976, 1552124883, 2202049807, 2303547305, 2433329986, 2698909330, 3182339638, 3236172513, 3626859046, 3739501011])], + ), + ( + "42.deep.exr", + 67605, + 8, + &[ + ("modelid", &[147148503, 1252544961, 1917926828, 3170049167, 3338960663, 3411631724]), + ("materialid", &[489905694, 507819813, 2340321182, 3683581971, 3905155331, 3947898621, 4179825792]), + ], + ), + // 64-bit IDs split over `.id0`/`.id1`: only the sample counts are checked. + ("64.deep.exr", 64543, 6, &[]), + ("multivariate.deep.exr", 64543, 6, &[("id", MULTIVARIATE_IDS)]), + // The same samples as multivariate; its sidecar has no manifest. + ("objectid.deep.exr", 64543, 6, &[]), +]; + +#[cfg(feature = "corpus")] +#[test] +fn real_deep_files_decode() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/exr"); + for &(name, total, max_per_pixel, ids) in REAL_FILES { + let path = dir.join(name); + let bytes = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e} (run `cargo xtask corpus --exr`)", path.display())); + let deep = decode_deep_exr(&bytes, &Limits::none()).unwrap_or_else(|e| panic!("{name}: {e}")); + assert_eq!((deep.width, deep.height), (256, 256), "{name}"); + assert_eq!(deep.total_samples(), total, "{name}"); + assert_eq!(deep.counts.len(), 256 * 256 + 1, "{name}"); + assert!(deep.counts.windows(2).all(|w| w[0] <= w[1]), "{name}: counts must be cumulative"); + assert_eq!(deep.counts.windows(2).map(|w| w[1] - w[0]).max(), Some(max_per_pixel), "{name}"); + for c in &deep.channels { + assert_eq!(c.samples.len() as u64, total, "{name}: channel {}", c.name); + } + // Every decoded ID is one the sidecar lists, and every listed ID occurs. Samples hold + // u32 IDs as f32 (exact only to 2^24), so compare at f32 precision. + for &(channel, manifest) in ids { + let c = deep.channel(channel).unwrap_or_else(|| panic!("{name}: no channel {channel}")); + let want: std::collections::BTreeSet = manifest.iter().map(|&id| (id as f32).to_bits()).collect(); + let got: std::collections::BTreeSet = c.samples.iter().map(|v| v.to_bits()).collect(); + assert_eq!(got, want, "{name}: channel {channel}"); + } + // The flat decode composites the same samples. + let img = decode(&bytes).unwrap_or_else(|e| panic!("{name}: {e}")); + assert_eq!((img.width(), img.height()), (deep.width, deep.height), "{name}"); + assert_eq!(img.to_f32_samples().expect("f32").len(), 256 * 256 * 4, "{name}"); + assert!(img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::DeepFlattened { .. })), "{name}"); + } +} + +/// `gen_deep` for a cropped render: the data window sits at `(dwx, dwy)` inside a larger +/// display window of `sw × sh` (the stage the renderer composed onto). Scanline chunk Y +/// coordinates are absolute display rows, so they carry the data-window offset. +#[allow(clippy::too_many_arguments)] +fn gen_deep_cropped( + w: usize, + h: usize, + dwx: i32, + dwy: i32, + sw: usize, + sh: usize, + channels: &[Chan], + counts: &[u32], + sample: &dyn Fn(usize, usize) -> Vec, +) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); + out.extend_from_slice(&(2u32 | 0x800).to_le_bytes()); + + let mut chlist = Vec::new(); + for c in channels { + chlist.extend_from_slice(c.name.as_bytes()); + chlist.push(0); + chlist.extend_from_slice(&c.ty.to_le_bytes()); + chlist.push(0); + chlist.extend_from_slice(&[0u8; 3]); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + } + chlist.push(0); + attr(&mut out, "channels", "chlist", &chlist); + attr(&mut out, "compression", "compression", &[0u8]); + // box2i is inclusive: the windows end at position + size - 1. + let dw: Vec = [dwx, dwy, dwx + w as i32 - 1, dwy + h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut out, "dataWindow", "box2i", &dw); + let disp: Vec = [0i32, 0, sw as i32 - 1, sh as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut out, "displayWindow", "box2i", &disp); + attr(&mut out, "lineOrder", "lineOrder", &[0]); + attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut out, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); + attr(&mut out, "type", "string", b"deepscanline"); + attr(&mut out, "version", "int", &1i32.to_le_bytes()); + out.push(0); + + // One scanline block per data row; the pixel indexing stays data-relative. + let mut chunks: Vec> = Vec::new(); + for y in 0..h { + let pixels: Vec = (y * w..y * w + w).collect(); + let mut c = Vec::new(); + c.extend_from_slice(&(dwy + y as i32).to_le_bytes()); + let mut table = Vec::with_capacity(pixels.len() * 4); + let mut acc = 0u32; + for &p in &pixels { + acc += counts[p]; + table.extend_from_slice(&acc.to_le_bytes()); + } + let mut data = Vec::new(); + for (ci, _) in channels.iter().enumerate() { + for &p in &pixels { + for v in sample(p, ci) { + data.extend_from_slice(&v.to_le_bytes()); + } + } + } + c.extend_from_slice(&(table.len() as u64).to_le_bytes()); + c.extend_from_slice(&(data.len() as u64).to_le_bytes()); + c.extend_from_slice(&(data.len() as u64).to_le_bytes()); + c.extend_from_slice(&table); + c.extend_from_slice(&data); + chunks.push(c); + } + let mut pos = out.len() + chunks.len() * 8; + let mut table = Vec::new(); + for c in &chunks { + table.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += c.len(); + } + out.extend_from_slice(&table); + for c in chunks { + out.extend_from_slice(&c); + } + out +} + +/// A cropped render opens on the full display-window stage: MY27-style renders carry a +/// `display_window` of the full frame (e.g. 4000×3000) while the deep data window is the +/// render pass's crop inside it. The image must open at stage size with the data at its +/// crop offset, transparent elsewhere — not cropped to the data window. +#[test] +fn cropped_deep_data_opens_on_the_display_stage() { + let (w, h, dwx, dwy, sw, sh) = (24usize, 16usize, 8i32, 6i32, 48usize, 32usize); + let counts = vec![1u32; w * h]; + let sample = |p: usize, ci: usize| -> Vec { + // chlist order (RGBA_NO_Z): A, B, G, R. + let x = p % w; + let y = p / w; + match ci { + 0 => vec![1.0], // A + 1 => vec![0.5], // B + 2 => vec![y as f32 / h as f32], // G + _ => vec![x as f32 / w as f32], // R + } + }; + let bytes = gen_deep_cropped(w, h, dwx, dwy, sw, sh, &RGBA_NO_Z, &counts, &sample); + let img = decode(&bytes).expect("cropped deep EXR decodes"); + assert_eq!((img.width(), img.height()), (sw as u32, sh as u32), "opens on the display window stage"); + assert!(img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::DisplayWindowStage { origin, .. } if *origin == (dwx, dwy)))); + + // The data sits at its crop offset: the first data pixel's green channel carries its + // data-row fraction, everything outside the data region stays transparent black. + let s = img.to_f32_samples().expect("f32"); + let nc = s.len() / (sw * sh); + let px = |x: usize, y: usize| -> Vec { s[y * sw * nc + x * nc..y * sw * nc + x * nc + nc].to_vec() }; + let first = px(dwx as usize, dwy as usize); + assert!( + (first[0] - 0.0).abs() < 1e-5 && (first[1] - 0.0).abs() < 1e-5 && (first[2] - 0.5).abs() < 1e-5 && (first[3] - 1.0).abs() < 1e-5, + "first data pixel at the offset: {first:?}" + ); + let last = px(dwx as usize + w - 1, dwy as usize + h - 1); + assert!( + (last[0] - (w - 1) as f32 / w as f32).abs() < 1e-5 && (last[1] - (h - 1) as f32 / h as f32).abs() < 1e-5, + "last data pixel keeps its coordinates: {last:?}" + ); + let outside = px(0, 0); + assert_eq!(outside, vec![0.0, 0.0, 0.0, 0.0], "outside the data region: transparent"); +} diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs new file mode 100644 index 000000000..640772c83 --- /dev/null +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -0,0 +1,309 @@ +//! Magic-number detection, extensions, and the capability table +//! (including the symmetric read/write guarantee). + +use crate::common; +use common::*; +use craft_image_codecs::*; + +// --------------------------------------------------------------------------- +// Symmetric guarantee +// --------------------------------------------------------------------------- + +#[test] +fn every_enabled_format_is_symmetric_or_whitelisted() { + for f in Format::ALL { + let c = caps(f); + if !(c.read || c.write) { + continue; + } + if c.read && c.write { + continue; + } + let exc = ASYMMETRIC_EXCEPTIONS.iter().find(|(g, _)| *g == f); + let (_, reason) = exc.unwrap_or_else(|| panic!("{f:?} is enabled but not symmetric (read={}, write={})", c.read, c.write)); + assert!(reason.len() > 20, "{f:?} exception needs a real reason"); + } +} + +#[test] +fn exceptions_are_documented_and_unique() { + for (i, (f, reason)) in ASYMMETRIC_EXCEPTIONS.iter().enumerate() { + assert!(!reason.is_empty()); + assert!(ASYMMETRIC_EXCEPTIONS[i + 1..].iter().all(|(g, _)| g != f)); + } +} + +#[test] +fn never_write_only_without_exception() { + // Stronger form: no format may be writable-but-unreadable unless listed. + for f in writable_formats() { + if !caps(f).read { + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(g, _)| *g == f), "{f:?}"); + } + } +} + +#[test] +fn default_build_formats_all_symmetric() { + let expected = [ + Format::Png, + Format::Jpeg, + Format::Tiff, + Format::WebP, + Format::Gif, + Format::Bmp, + Format::Tga, + Format::Ico, + Format::Pnm, + Format::Qoi, + Format::OpenExr, + Format::Hdr, + Format::Fits, + Format::Dpx, + Format::Cineon, + Format::Iff, + ]; + for f in expected { + assert!(caps(f).read && caps(f).write, "{f:?}"); + } +} + +#[test] +fn avif_never_readable() { + assert!(!caps(Format::Avif).read); + assert_eq!(caps(Format::Avif).write, cfg!(feature = "avif")); + assert!(matches!(decode_as(Format::Avif, b"\0\0\0\x1cftypavif"), Err(CodecError::Unsupported { .. }))); +} + +#[test] +fn heif_is_detected_always_and_readable_only_with_the_feature() { + let header = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"; + assert_eq!(detect(header), Some(Format::Heif)); + assert_eq!(from_extension("IMG_0001.HEIC"), Some(Format::Heif)); + assert_eq!(caps(Format::Heif).read, cfg!(feature = "heif")); + assert!(!caps(Format::Heif).write); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Heif)); + // Either way a bare header is an error, never a panic. + assert!(decode_as(Format::Heif, header).is_err()); +} + +#[test] +fn detect_heif_brands() { + assert_eq!(detect(b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"), Some(Format::Heif)); + assert_eq!(detect(b"\0\0\0\x18ftypmif1\0\0\0\0mif1heic"), Some(Format::Heif), "generic major brand"); + assert_eq!(detect(b"\0\0\0\x18ftypheix\0\0\0\0mif1heix"), Some(Format::Heif), "10-bit"); + assert_eq!(detect(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"), Some(Format::Heif), "image sequence"); + assert_eq!(detect(b"\0\0\0\x18ftypmif1\0\0\0\0mif1avif"), Some(Format::Avif), "AVIF wins over the generic brand"); + assert_eq!(detect(b"\0\0\0\x18ftypisom\0\0\0\0isommp41"), None, "mp4 is not HEIF"); + assert_eq!(detect(b"\0\0\0\x14ftypqt \0\0\0\0qt "), None, "QuickTime is not HEIF"); +} + +#[test] +fn caps_are_internally_consistent() { + for f in Format::ALL { + let c = caps(f); + assert!(!c.depths.is_empty() && !c.layouts.is_empty(), "{f:?}"); + assert_eq!(c.alpha, c.layouts.iter().any(|l| l.has_alpha()), "{f:?} alpha flag vs layouts"); + // DICOM's registered media type is `application/dicom`; every other format is an image. + let mime_ok = f.mime_type().starts_with("image/") || (f == Format::Dicom && f.mime_type() == "application/dicom"); + assert!(!f.extensions().is_empty() && !f.name().is_empty() && mime_ok, "{f:?}"); + } +} + +#[test] +fn caps_function_matches_method() { + for f in Format::ALL { + assert_eq!(caps(f), f.caps()); + } +} + +// --------------------------------------------------------------------------- +// Detection +// --------------------------------------------------------------------------- + +#[test] +fn detect_encoded_output_of_every_format() { + for f in writable_formats() { + let img = test_image(ChannelLayout::Rgba, SampleType::U8); + let bytes = encode(&img, f, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(f)); + } +} + +#[test] +fn detect_magic_png() { + assert_eq!(detect(b"\x89PNG\r\n\x1a\n\0\0"), Some(Format::Png)); +} +#[test] +fn detect_magic_jpeg() { + assert_eq!(detect(&[0xFF, 0xD8, 0xFF, 0xE0]), Some(Format::Jpeg)); + assert_eq!(detect(&[0xFF, 0xD8]), None); +} +#[test] +fn detect_magic_tiff_both_endians_and_bigtiff() { + assert_eq!(detect(b"II*\0\x08\0\0\0"), Some(Format::Tiff)); + assert_eq!(detect(b"MM\0*\0\0\0\x08"), Some(Format::Tiff)); + assert_eq!(detect(b"II+\0\x08\0\0\0"), Some(Format::Tiff)); + assert_eq!(detect(b"MM\0+\0\x08\0\0"), Some(Format::Tiff)); +} +#[test] +fn detect_magic_webp() { + assert_eq!(detect(b"RIFF\x10\0\0\0WEBPVP8L"), Some(Format::WebP)); + assert_eq!(detect(b"RIFF\x10\0\0\0WAVEfmt "), None); +} +#[test] +fn detect_magic_gif() { + assert_eq!(detect(b"GIF87a\x01\0"), Some(Format::Gif)); + assert_eq!(detect(b"GIF89a\x01\0"), Some(Format::Gif)); + assert_eq!(detect(b"GIF90a\x01\0"), None); +} +#[test] +fn detect_magic_bmp() { + let mut b = b"BM".to_vec(); + b.resize(54, 0); + assert_eq!(detect(&b), Some(Format::Bmp)); + assert_eq!(detect(b"BM"), None); +} +#[test] +fn detect_magic_ico() { + assert_eq!(detect(&[0, 0, 1, 0, 1, 0, 16, 16]), Some(Format::Ico)); + assert_eq!(detect(&[0, 0, 1, 0, 0, 0]), None, "zero images"); +} +#[test] +fn detect_magic_pnm_family() { + for m in [&b"P1\n"[..], b"P2 ", b"P3\t", b"P4\n", b"P5\r", b"P6\n", b"P7\n", b"PF\n", b"Pf\n"] { + assert_eq!(detect(m), Some(Format::Pnm), "{:?}", std::str::from_utf8(m)); + } + assert_eq!(detect(b"P8\n"), None); + assert_eq!(detect(b"P6x"), None); +} +#[test] +fn detect_magic_qoi() { + assert_eq!(detect(b"qoif\0\0\0\x01\0\0\0\x01\x03\0"), Some(Format::Qoi)); +} +#[test] +fn detect_magic_exr() { + assert_eq!(detect(&[0x76, 0x2F, 0x31, 0x01, 2, 0, 0, 0]), Some(Format::OpenExr)); +} +#[test] +fn detect_magic_hdr() { + assert_eq!(detect(b"#?RADIANCE\n"), Some(Format::Hdr)); + assert_eq!(detect(b"#?RGBE\n"), Some(Format::Hdr)); +} +#[test] +fn detect_magic_cineon() { + let mut b = vec![0u8; 208]; + b[0..4].copy_from_slice(&[0x80, 0x2A, 0x5F, 0xD7]); + assert_eq!(detect(&b), Some(Format::Cineon)); + assert_eq!(from_extension("scan.CIN"), Some(Format::Cineon)); + assert!(caps(Format::Cineon).read && caps(Format::Cineon).write, "Cineon is read and written"); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Cineon)); + // A bare magic is an error, never a panic. + assert!(decode_as(Format::Cineon, &b).is_err()); +} + +#[test] +fn detect_magic_iff() { + // FORM + a four-byte length + the bitmap type. + assert_eq!(detect(b"FORM\0\0\0\x14ILBM"), Some(Format::Iff)); + assert_eq!(detect(b"FORM\0\0\0\x14PBM "), Some(Format::Iff)); + assert_eq!(from_extension("art.LBM"), Some(Format::Iff)); + assert_eq!(from_extension("art.ilbm"), Some(Format::Iff)); + assert!(caps(Format::Iff).read && caps(Format::Iff).write, "IFF is read and written"); + assert!(caps(Format::Iff).alpha); + assert!(caps(Format::Iff).layouts.contains(&craft_image_codecs::ChannelLayout::Rgba)); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Iff)); + assert_eq!(Format::Iff.max_dimensions(), Some((65535, 65535))); + // A bare FORM without a bitmap type is not IFF. + assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); + assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); +} +#[test] +fn detect_magic_avif() { + assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); + assert_eq!(detect(b"\0\0\0\x1cftypavis\0\0\0\0avismsf1miaf"), Some(Format::Avif)); + assert_eq!(detect(b"\0\0\0\x18ftypmif1\0\0\0\0mif1avif"), Some(Format::Avif), "compatible brand"); + assert_eq!(detect(b"\0\0\0\x18ftypisom\0\0\0\0isommp41"), None, "mp4 is not avif"); +} +#[test] +fn detect_magic_dicom() { + let mut b = vec![0u8; 128]; + b.extend_from_slice(b"DICM"); + assert_eq!(detect(&b), Some(Format::Dicom)); + assert_eq!(from_extension("scan.DCM"), Some(Format::Dicom)); + assert_eq!(from_extension("scan.dc3"), Some(Format::Dicom)); + assert!(caps(Format::Dicom).read && !caps(Format::Dicom).write); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Dicom)); + // A bare magic is an error, never a panic. + assert!(decode_as(Format::Dicom, &b).is_err()); +} + +#[test] +fn detect_tga_heuristic() { + // Uncompressed true-colour 2x2, 24 bpp. + let mut b = vec![0u8, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 24, 0]; + b.extend_from_slice(&[0; 12]); + assert_eq!(detect(&b), Some(Format::Tga)); + let mut bad = b.clone(); + bad[2] = 7; // invalid image type + assert_eq!(detect(&bad), None); + let mut bad = b.clone(); + bad[16] = 13; // invalid depth + assert_eq!(detect(&bad), None); +} +#[test] +fn detect_negatives() { + assert_eq!(detect(b""), None); + assert_eq!(detect(b"x"), None); + assert_eq!(detect(b"hello world, this is not an image"), None); + assert_eq!(detect(b"%PDF-1.7\n"), None); + assert_eq!(detect(b"PK\x03\x04"), None); + assert_eq!(detect(&[0u8; 64]), None); +} +#[test] +fn detect_random_bytes_rarely_matches_tga() { + let mut rng = Rng::new(5); + let hits = (0..2000).filter(|_| detect(&rng.bytes(64)) == Some(Format::Tga)).count(); + assert!(hits < 10, "{hits} false TGA positives"); +} +#[test] +fn decode_unknown_is_error() { + assert!(matches!(decode(b"garbage garbage"), Err(CodecError::UnknownFormat))); +} + +// --------------------------------------------------------------------------- +// Extensions +// --------------------------------------------------------------------------- + +#[test] +fn extensions_map_back() { + for f in Format::ALL { + for e in f.extensions() { + assert_eq!(from_extension(e), Some(f), "{e}"); + assert_eq!(from_extension(&e.to_uppercase()), Some(f)); + assert_eq!(from_extension(&format!(".{e}")), Some(f)); + assert_eq!(from_extension(&format!("dir/file.name.{e}")), Some(f)); + } + } +} +#[test] +fn extension_specific_cases() { + assert_eq!(from_extension("JPG"), Some(Format::Jpeg)); + assert_eq!(from_extension("tif"), Some(Format::Tiff)); + assert_eq!(from_extension("pfm"), Some(Format::Pnm)); + assert_eq!(from_extension("pam"), Some(Format::Pnm)); + assert_eq!(from_extension("FIT"), Some(Format::Fits)); + assert_eq!(from_extension("m31.fits"), Some(Format::Fits)); + assert_eq!(from_extension("exr"), Some(Format::OpenExr)); + assert_eq!(from_extension("psd"), None); + assert_eq!(from_extension(""), None); + assert_eq!(from_extension("png.bak"), None); +} +#[test] +fn extensions_unique_across_formats() { + let mut all: Vec<&str> = Format::ALL.iter().flat_map(|f| f.extensions().iter().copied()).collect(); + let n = all.len(); + all.sort(); + all.dedup(); + assert_eq!(all.len(), n); +} diff --git a/crates/image-codecs/tests/it/dpx.rs b/crates/image-codecs/tests/it/dpx.rs new file mode 100644 index 000000000..4d4835cc7 --- /dev/null +++ b/crates/image-codecs/tests/it/dpx.rs @@ -0,0 +1,521 @@ +//! DPX (SMPTE 268M) read and write, against fixtures packed here independently of the codec: +//! every descriptor, bit depth and packing it reads, both byte orders, the header fields kept +//! through a round trip, the bit-depth choice on export and the files it refuses (#1049). + +use craft_image_codecs::*; + +const HEADER: usize = 2048; +const ELEMENT0: usize = 780; + +/// A DPX file built field by field. +#[derive(Clone)] +struct Fixture { + big: bool, + width: u32, + height: u32, + descriptor: u8, + bits: u8, + packing: u16, + encoding: u16, + signed: bool, + transfer: u8, + colorimetric: u8, + orientation: u16, + elements: u16, + strings: Vec<((usize, usize), &'static str)>, + frame_position: u32, + frame_rate: f32, + time_code: u32, + /// Code values, interleaved, top line first. + codes: Vec, +} + +impl Fixture { + fn new(descriptor: u8, bits: u8, packing: u16, width: u32, height: u32) -> Self { + let channels = channels(descriptor); + let max = (1u32 << bits) - 1; + // Spread over the whole range, ends included. + let n = (width * height) as usize * channels; + let codes = (0..n) + .map(|i| { + if i == 0 { + 0 + } else if i == 1 { + max as u16 + } else { + ((i as u64 * 2654435761) % (u64::from(max) + 1)) as u16 + } + }) + .collect(); + Fixture { + big: true, + width, + height, + descriptor, + bits, + packing, + encoding: 0, + signed: false, + transfer: 255, + colorimetric: 255, + orientation: 0, + elements: 1, + strings: Vec::new(), + frame_position: u32::MAX, + frame_rate: f32::from_bits(u32::MAX), + time_code: u32::MAX, + codes, + } + } + + fn u16(&self, v: u16) -> [u8; 2] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + + fn u32(&self, v: u32) -> [u8; 4] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + + fn line(&self, codes: &[u16]) -> Vec { + let mut out = Vec::new(); + match (self.bits, self.packing) { + (8, _) => out.extend(codes.iter().map(|&c| c as u8)), + (16, _) => out.extend(codes.iter().flat_map(|&c| self.u16(c))), + (10, 1 | 2) => { + let base = if self.packing == 1 { 22 } else { 20 }; + for chunk in codes.chunks(3) { + let w = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c) << (base - 10 * i as u32))); + out.extend(self.u32(w)); + } + } + (12, 1) => out.extend(codes.iter().flat_map(|&c| self.u16(c << 4))), + _ => { + // One bit stream, high bits first, cut into 32-bit words. + let mut bits: Vec = codes.iter().flat_map(|&c| (0..self.bits).rev().map(move |b| (c >> b) & 1 == 1)).collect(); + bits.resize(bits.len().div_ceil(32) * 32, false); + for word in bits.chunks(32) { + out.extend(self.u32(word.iter().fold(0u32, |w, &b| (w << 1) | u32::from(b)))); + } + } + } + out.resize(out.len().div_ceil(4) * 4, 0); + out + } + + fn bytes(&self) -> Vec { + let mut b = vec![0u8; HEADER]; + let mut put = |at: usize, v: &[u8]| b[at..at + v.len()].copy_from_slice(v); + put(0, if self.big { b"SDPX" } else { b"XPDS" }); + put(4, &self.u32(HEADER as u32)); + put(8, b"V2.0"); + put(24, &self.u32(1664)); + put(28, &self.u32(384)); + put(768, &self.u16(self.orientation)); + put(770, &self.u16(self.elements)); + put(772, &self.u32(self.width)); + put(776, &self.u32(self.height)); + for el in 0..usize::from(self.elements.min(8)) { + let e = ELEMENT0 + el * 72; + put(e, &self.u32(u32::from(self.signed))); + put(e + 20, &[self.descriptor, self.transfer, self.colorimetric, self.bits]); + put(e + 24, &self.u16(self.packing)); + put(e + 26, &self.u16(self.encoding)); + put(e + 28, &self.u32(HEADER as u32)); + } + for &((at, len), s) in &self.strings { + assert!(s.len() < len); + put(at, s.as_bytes()); + } + put(1712, &self.u32(self.frame_position)); + put(1724, &self.u32(self.frame_rate.to_bits())); + put(1920, &self.u32(self.time_code)); + let per_line = self.width as usize * channels(self.descriptor); + for line in self.codes.chunks(per_line) { + b.extend(self.line(line)); + } + b + } +} + +fn channels(descriptor: u8) -> usize { + match descriptor { + 6 => 1, + 50 => 3, + _ => 4, + } +} + +fn layout(descriptor: u8) -> ChannelLayout { + match descriptor { + 6 => ChannelLayout::Gray, + 50 => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + } +} + +/// The 16-bit sample a code value opens as: `round(c × 65535 / (2^b − 1))`. +fn expected(c: u16, bits: u8) -> u16 { + (f64::from(c) * 65535.0 / f64::from((1u32 << bits) - 1)).round() as u16 +} + +/// Reads the code values of a big-endian file we wrote (packing 0 at 8/16 bits, 1 at 10/12). +fn written_codes(b: &[u8]) -> (u8, u16, Vec) { + let be16 = |at: usize| u16::from_be_bytes([b[at], b[at + 1]]); + let be32 = |at: usize| u32::from_be_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]); + let (w, h) = (be32(772) as usize, be32(776) as usize); + let nc = channels(b[ELEMENT0 + 20]); + let (bits, packing) = (b[ELEMENT0 + 23], be16(ELEMENT0 + 24)); + let off = be32(ELEMENT0 + 28) as usize; + let per_line = w * nc; + let stride = match bits { + 10 => per_line.div_ceil(3) * 4, + _ => (per_line * if bits == 8 { 1 } else { 2 }).div_ceil(4) * 4, + }; + let mut codes = Vec::new(); + for y in 0..h { + let line = &b[off + y * stride..]; + for i in 0..per_line { + codes.push(match bits { + 8 => u16::from(line[i]), + 10 => ((be32_at(line, i / 3 * 4) >> (22 - 10 * (i % 3) as u32)) & 0x3FF) as u16, + 12 => u16::from_be_bytes([line[i * 2], line[i * 2 + 1]]) >> 4, + _ => u16::from_be_bytes([line[i * 2], line[i * 2 + 1]]), + }); + } + } + (bits, packing, codes) +} + +fn be32_at(b: &[u8], at: usize) -> u32 { + u32::from_be_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]) +} + +fn opts(bits: Option) -> EncodeOptions { + EncodeOptions { dpx_bits: bits, ..EncodeOptions::default() } +} + +const COMBOS: [(u8, u16); 7] = [(8, 0), (10, 0), (10, 1), (10, 2), (12, 0), (12, 1), (16, 0)]; + +#[test] +fn every_descriptor_depth_and_packing_opens_with_its_model_and_values() { + for descriptor in [6, 50, 51] { + for (bits, packing) in COMBOS { + // Widths that leave partly filled words at the end of a line. + let fx = Fixture::new(descriptor, bits, packing, 5, 3); + let img = decode(&fx.bytes()).unwrap_or_else(|e| panic!("{descriptor} {bits}/{packing}: {e}")); + let what = format!("descriptor {descriptor}, {bits} bits, packing {packing}"); + assert_eq!(detect(&fx.bytes()), Some(Format::Dpx)); + assert_eq!((img.width(), img.height()), (5, 3), "{what}"); + assert_eq!(img.layout(), layout(descriptor), "{what}"); + assert_eq!(img.layout().has_alpha(), descriptor == 51, "{what}"); + match bits { + 8 => assert_eq!(img.as_u8().unwrap(), fx.codes.iter().map(|&c| c as u8).collect::>(), "{what}"), + 16 => assert_eq!(img.to_u16_samples().unwrap(), fx.codes, "{what}"), + _ => assert_eq!(img.to_u16_samples().unwrap(), fx.codes.iter().map(|&c| expected(c, bits)).collect::>(), "{what}"), + } + } + } +} + +#[test] +fn big_and_little_endian_files_open_identically() { + for (bits, packing) in COMBOS { + let be = Fixture::new(50, bits, packing, 4, 2); + let le = Fixture { big: false, ..be.clone() }; + assert_eq!(&le.bytes()[..4], b"XPDS"); + assert_eq!(decode(&be.bytes()).unwrap(), decode(&le.bytes()).unwrap(), "{bits} bits, packing {packing}"); + } +} + +#[test] +fn ten_bit_code_value_opens_as_the_rounded_16_bit_value() { + let mut fx = Fixture::new(6, 10, 1, 1, 1); + fx.codes = vec![512]; + assert_eq!(decode(&fx.bytes()).unwrap().to_u16_samples().unwrap(), vec![(512.0f64 * 65535.0 / 1023.0).round() as u16]); +} + +#[test] +fn ten_and_twelve_bit_frames_written_back_at_their_depth_keep_their_code_values() { + for (bits, choice) in [(10, DpxBits::Bits10), (12, DpxBits::Bits12)] { + for packing in [0, 1] { + let fx = Fixture::new(50, bits, packing, 7, 3); + let out = encode(&decode(&fx.bytes()).unwrap(), Format::Dpx, &opts(Some(choice))).unwrap(); + let (wbits, wpacking, codes) = written_codes(&out); + assert_eq!((wbits, wpacking), (bits, 1)); + assert_eq!(codes, fx.codes, "{bits} bits, packing {packing}"); + } + } +} + +#[test] +fn sixteen_bit_frames_round_trip_losslessly_and_files_start_with_sdpx() { + let fx = Fixture { big: false, ..Fixture::new(51, 16, 0, 6, 4) }; + let img = decode(&fx.bytes()).unwrap(); + let out = encode(&img, Format::Dpx, &EncodeOptions::default()).unwrap(); + assert_eq!(&out[..4], b"SDPX"); + let (bits, packing, codes) = written_codes(&out); + assert_eq!((bits, packing, codes), (16, 0, fx.codes)); + assert_eq!(decode(&out).unwrap().to_u16_samples(), img.to_u16_samples()); +} + +#[test] +fn log_frames_report_their_encoding_and_keep_the_plain_mapping() { + let mut fx = Fixture::new(50, 10, 1, 3, 2); + (fx.transfer, fx.colorimetric) = (3, 1); + let img = decode(&fx.bytes()).unwrap(); + assert_eq!(img.warnings, vec![DecodeWarning::DpxEncoding { transfer: 3, colorimetric: 1 }]); + let note = img.warnings[0].to_string(); + assert!(note.contains("logarithmic (3)") && note.contains("printing density (1)"), "{note}"); + assert_eq!(img.to_u16_samples().unwrap(), fx.codes.iter().map(|&c| expected(c, 10)).collect::>()); +} + +#[test] +fn only_the_first_of_several_image_elements_is_imported() { + let mut fx = Fixture::new(6, 8, 0, 2, 2); + fx.elements = 2; + let img = decode(&fx.bytes()).unwrap(); + assert_eq!(img.warnings.iter().map(ToString::to_string).collect::>(), ["only the first of 2 image elements was imported"]); +} + +#[test] +fn listed_header_fields_survive_a_round_trip() { + let mut fx = Fixture::new(50, 10, 1, 2, 2); + fx.strings = vec![((160, 100), "Resolve 20.1"), ((260, 200), "Reel 7"), ((460, 200), "(c) 2026 Studio"), ((136, 24), "2026:10:12:09:30:00UTC")]; + (fx.transfer, fx.colorimetric, fx.frame_position, fx.frame_rate, fx.time_code) = (3, 1, 86400, 23.976, 0x0102_0304); + let img = decode(&fx.bytes()).unwrap(); + for (k, v) in [ + ("DPX:Creator", "Resolve 20.1"), + ("DPX:Project", "Reel 7"), + ("DPX:Copyright", "(c) 2026 Studio"), + ("DPX:CreationTime", "2026:10:12:09:30:00UTC"), + ("DPX:Transfer", "3"), + ("DPX:Colorimetric", "1"), + ("DPX:FramePosition", "86400"), + ("DPX:FrameRate", "23.976"), + ("DPX:TimeCode", "01:02:03:04"), + ] { + assert!(img.meta.text.contains(&(k.into(), v.into())), "{k} = {v} in {:?}", img.meta.text); + } + let out = encode(&img, Format::Dpx, &opts(Some(DpxBits::Bits10))).unwrap(); + let src = fx.bytes(); + // Field by field: the strings, the element's encoding codes, film and TV fields. + for (at, len) in [(160, 100), (260, 200), (460, 200), (136, 24), (ELEMENT0 + 21, 2), (1712, 4), (1724, 4), (1920, 4)] { + assert_eq!(out[at..at + len], src[at..at + len], "field at {at}"); + } + assert_eq!(decode(&out).unwrap().meta.text, img.meta.text); +} + +#[test] +fn chosen_bit_depth_sets_the_header_and_packing() { + let img = Image::from_u16(5, 2, ChannelLayout::Rgb, &(0..30u16).map(|i| i * 2000).collect::>()).unwrap(); + for (choice, bits, packing) in [(DpxBits::Bits8, 8, 0), (DpxBits::Bits10, 10, 1), (DpxBits::Bits12, 12, 1), (DpxBits::Bits16, 16, 0)] { + let out = encode(&img, Format::Dpx, &opts(Some(choice))).unwrap(); + assert_eq!((out[ELEMENT0 + 23], u16::from_be_bytes([out[ELEMENT0 + 24], out[ELEMENT0 + 25]])), (bits, packing), "{choice:?}"); + let back = decode(&out).unwrap(); + assert_eq!((back.width(), back.height(), back.layout()), (5, 2, ChannelLayout::Rgb)); + } + // Without a choice the depth follows the image. + let gray8 = Image::from_u8(3, 1, ChannelLayout::Gray, vec![0, 128, 255]).unwrap(); + assert_eq!(encode(&gray8, Format::Dpx, &EncodeOptions::default()).unwrap()[ELEMENT0 + 23], 8); + assert_eq!(encode(&img, Format::Dpx, &EncodeOptions::default()).unwrap()[ELEMENT0 + 23], 16); + let float = img.convert(ChannelLayout::Rgb, SampleType::F32); + assert_eq!(encode(&float, Format::Dpx, &EncodeOptions::default()).unwrap()[ELEMENT0 + 23], 16); + assert!(fidelity_warnings(&float, Format::Dpx).contains(&FidelityWarning::DepthReduced { from: SampleType::F32, to: SampleType::U16 })); +} + +/// The depth warnings of `img` written as `format` with `o`. +fn depth_warnings(img: &Image, format: Format, o: &EncodeOptions) -> Vec { + fidelity_warnings_with(img, format, o) + .into_iter() + .filter(|w| matches!(w, FidelityWarning::DepthReduced { .. } | FidelityWarning::BitsReduced { .. })) + .collect() +} + +#[test] +fn saving_sixteen_bits_at_ten_or_twelve_warns_with_the_stored_bits() { + let img = Image::from_u16(5, 2, ChannelLayout::Rgb, &(0..30u16).map(|i| i * 2000 + 1).collect::>()).unwrap(); + for (choice, bits) in [(DpxBits::Bits10, 10), (DpxBits::Bits12, 12)] { + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(choice))), [FidelityWarning::BitsReduced { from: SampleType::U16, bits }]); + assert_eq!(FidelityWarning::BitsReduced { from: SampleType::U16, bits }.to_string(), format!("16-bit will be reduced to {bits}-bit")); + // Float samples name the stored bits too, not the 16-bit words they are packed in. + let float = img.convert(ChannelLayout::Rgb, SampleType::F32); + assert_eq!(depth_warnings(&float, Format::Dpx, &opts(Some(choice))), [FidelityWarning::BitsReduced { from: SampleType::F32, bits }]); + // 8-bit samples widen into 10 and 12 bits without loss. + let eight = img.convert(ChannelLayout::Rgb, SampleType::U8); + assert_eq!(depth_warnings(&eight, Format::Dpx, &opts(Some(choice))), []); + } + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits16))), []); + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(None)), []); + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits8))), [FidelityWarning::DepthReduced { from: SampleType::U16, to: SampleType::U8 }]); + assert_eq!(depth_warnings(&img, Format::Cineon, &EncodeOptions::default()), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); +} + +#[test] +fn a_frame_opened_at_ten_or_twelve_bits_saves_back_without_a_warning() { + let img = Image::from_u16(5, 2, ChannelLayout::Rgb, &(0..30u16).map(|i| i * 2000 + 1).collect::>()).unwrap(); + for (choice, bits) in [(DpxBits::Bits10, 10), (DpxBits::Bits12, 12)] { + let opened = decode(&encode(&img, Format::Dpx, &opts(Some(choice))).unwrap()).unwrap(); + assert_eq!(depth_warnings(&opened, Format::Dpx, &opts(Some(choice))), [], "{bits}-bit"); + assert_eq!(decode(&encode(&opened, Format::Dpx, &opts(Some(choice))).unwrap()).unwrap(), opened, "{bits}-bit"); + } + // A 12-bit frame saved at 10 bits does lose its two low bits. + let twelve = decode(&encode(&img, Format::Dpx, &opts(Some(DpxBits::Bits12))).unwrap()).unwrap(); + assert_eq!(depth_warnings(&twelve, Format::Dpx, &opts(Some(DpxBits::Bits10))), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); + let cineon = decode(&cineon_bytes(3, 4, 2, &cineon_codes(24))).unwrap(); + assert_eq!(depth_warnings(&cineon, Format::Cineon, &EncodeOptions::default()), []); + assert_eq!(depth_warnings(&cineon, Format::Dpx, &opts(Some(DpxBits::Bits10))), []); +} + +#[test] +fn gray_with_alpha_is_written_as_luma_with_a_warning() { + let img = Image::from_u8(2, 1, ChannelLayout::GrayA, vec![10, 128, 200, 255]).unwrap(); + let out = encode(&img, Format::Dpx, &EncodeOptions::default()).unwrap(); + assert_eq!(out[ELEMENT0 + 20], 6); + assert_eq!(decode(&out).unwrap().as_u8().unwrap(), &[10, 200]); + assert!(fidelity_warnings(&img, Format::Dpx).contains(&FidelityWarning::AlphaDiscarded)); +} + +#[test] +fn only_free_form_text_is_reported_as_dropped() { + let mut img = Image::from_u8(1, 1, ChannelLayout::Rgb, vec![1, 2, 3]).unwrap(); + img.meta.text = vec![("DPX:Project".into(), "Reel 7".into())]; + assert!(!fidelity_warnings(&img, Format::Dpx).contains(&FidelityWarning::TextDropped)); + img.meta.text.push(("Comment".into(), "from a PNG".into())); + assert!(fidelity_warnings(&img, Format::Dpx).contains(&FidelityWarning::TextDropped)); +} + +#[test] +fn stored_orientation_is_turned_upright() { + // Orientation 4: the stored lines are the image's columns, left to right. + let mut fx = Fixture::new(6, 8, 0, 3, 1); + fx.codes = vec![1, 2, 3]; + fx.orientation = 4; + let img = decode(&fx.bytes()).unwrap(); + assert_eq!((img.width(), img.height(), img.as_u8().unwrap()), (1, 3, &[1u8, 2, 3][..])); +} + +#[test] +fn unsupported_and_damaged_files_error_instead_of_panicking() { + let unsupported = |bytes: Vec, what: &str| { + assert!(matches!(decode(&bytes), Err(CodecError::Unsupported { format: Format::Dpx, .. })), "{what}"); + }; + unsupported(Fixture { descriptor: 100, ..Fixture::new(50, 8, 0, 2, 2) }.bytes(), "ABGR descriptor"); + unsupported(Fixture { encoding: 1, ..Fixture::new(50, 10, 1, 2, 2) }.bytes(), "run-length encoding"); + unsupported(Fixture { signed: true, ..Fixture::new(50, 16, 0, 2, 2) }.bytes(), "signed samples"); + let mut float = Fixture::new(50, 16, 0, 2, 2).bytes(); + float[ELEMENT0 + 23] = 32; + unsupported(float, "32-bit float"); + let mut method_b = Fixture::new(50, 12, 1, 2, 2).bytes(); + method_b[ELEMENT0 + 24..ELEMENT0 + 26].copy_from_slice(&2u16.to_be_bytes()); + unsupported(method_b, "12-bit method B"); + + let good = Fixture::new(51, 10, 1, 9, 9).bytes(); + let mut no_elements = good.clone(); + no_elements[770..772].copy_from_slice(&0u16.to_be_bytes()); + let mut past_end = good.clone(); + past_end[ELEMENT0 + 28..ELEMENT0 + 32].copy_from_slice(&u32::MAX.wrapping_sub(1).to_be_bytes()); + let mut huge = good.clone(); + huge[772..780].copy_from_slice(&[0xFF; 8]); + for (what, bytes) in [ + ("header cut short", good[..900].to_vec()), + ("truncated element", good[..good.len() - 1].to_vec()), + ("no image elements", no_elements), + ("data offset past the end", past_end), + ("absurd size", huge), + ("magic alone", b"SDPX".to_vec()), + ] { + assert!(decode_as(Format::Dpx, &bytes).is_err(), "{what}"); + } +} + +// ---------------------------------------------------------------- Cineon (Kodak .cin) + +/// A Cineon 4.5 file built field by field: the 1024-byte generic header, a 1024-byte film header, +/// then `codes` (interleaved, top line first) three 10-bit values to a big-endian word, left +/// justified, each line on a word boundary. +fn cineon_bytes(channels: u8, width: u32, height: u32, codes: &[u16]) -> Vec { + let mut b = vec![0u8; 2048]; + b[0..4].copy_from_slice(&0x802A_5FD7u32.to_be_bytes()); + b[4..8].copy_from_slice(&2048u32.to_be_bytes()); + b[193] = channels; + b[198] = 10; + b[200..204].copy_from_slice(&width.to_be_bytes()); + b[204..208].copy_from_slice(&height.to_be_bytes()); + for line in codes.chunks(width as usize * usize::from(channels)) { + for chunk in line.chunks(3) { + let w = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c) << (22 - 10 * i as u32))); + b.extend(w.to_be_bytes()); + } + } + b +} + +/// Code values spread over 0..=1023, ends included. +fn cineon_codes(n: usize) -> Vec { + (0..n).map(|i| if i == 1 { 1023 } else { ((i as u64 * 2654435761) % 1024) as u16 }).collect() +} + +/// Reads back what we wrote: (channels, width, height, code values), from the header's own fields. +fn cineon_written(b: &[u8]) -> (u8, u32, u32, Vec) { + let off = be32_at(b, 4) as usize; + let (nc, w, h) = (b[193], be32_at(b, 200), be32_at(b, 204)); + let per_line = w as usize * usize::from(nc); + let stride = per_line.div_ceil(3) * 4; + let mut codes = Vec::new(); + for y in 0..h as usize { + let line = &b[off + y * stride..]; + codes.extend((0..per_line).map(|i| ((be32_at(line, i / 3 * 4) >> (22 - 10 * (i % 3) as u32)) & 0x3FF) as u16)); + } + (nc, w, h, codes) +} + +#[test] +fn cineon_frames_saved_as_cineon_keep_their_code_values_and_reopen_identically() { + // Widths that leave partly filled words at the end of each line. + for (channels, layout) in [(1u8, ChannelLayout::Gray), (3, ChannelLayout::Rgb)] { + let codes = cineon_codes(5 * 3 * usize::from(channels)); + let img = decode(&cineon_bytes(channels, 5, 3, &codes)).unwrap(); + assert_eq!(img.layout(), layout); + let out = encode(&img, Format::Cineon, &EncodeOptions::default()).unwrap_or_else(|e| panic!("{layout:?}: {e}")); + assert_eq!(detect(&out), Some(Format::Cineon), "{layout:?}"); + assert_eq!(cineon_written(&out), (channels, 5, 3, codes), "{layout:?}"); + assert_eq!(decode(&out).unwrap(), img, "{layout:?}"); + } +} + +#[test] +fn cineon_header_follows_the_kodak_layout() { + let img = decode(&cineon_bytes(3, 4, 2, &cineon_codes(24))).unwrap(); + let out = encode(&img, Format::Cineon, &EncodeOptions::default()).unwrap(); + let f32_at = |at: usize| f32::from_bits(be32_at(&out, at)); + // Generic and film header lengths, user data, total size, version. + assert_eq!((be32_at(&out, 4), be32_at(&out, 8), be32_at(&out, 12), be32_at(&out, 16)), (2048, 1024, 1024, 0)); + assert_eq!(be32_at(&out, 20) as usize, out.len()); + assert_eq!(&out[24..29], b"V4.5\0"); + // Upright, three channels, each the universal metric's red, green, blue at 10 bits. + assert_eq!((out[192], out[193]), (0, 3)); + for (i, colour) in [1u8, 2, 3].into_iter().enumerate() { + let c = 196 + 28 * i; + assert_eq!(&out[c..c + 3], &[0, colour, 10], "channel {i}"); + assert_eq!((be32_at(&out, c + 4), be32_at(&out, c + 8)), (4, 2), "channel {i}"); + // Minimum and maximum data value and quantity, all four 32-bit floats. + assert_eq!((f32_at(c + 12), f32_at(c + 16), f32_at(c + 20), f32_at(c + 24)), (0.0, 0.0, 1023.0, 2.048), "channel {i}"); + } + // Pixel interleave, 32-bit words left justified, unsigned, positive, no padding. + assert_eq!(&out[680..692], &[0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + // Two lines of twelve samples: four words each. + assert_eq!(out.len(), 2048 + 2 * 16); +} + +#[test] +fn any_image_saves_as_ten_bit_cineon() { + // 16-bit samples land on the nearest 10-bit code; colour with alpha drops it, gray stays gray. + let wide = Image::from_u16(3, 1, ChannelLayout::Rgba, &[0, 32, 65535, 65535, 30000, 40000, 50000, 0, 1, 2, 3, 4]).unwrap(); + let out = encode(&wide, Format::Cineon, &EncodeOptions::default()).unwrap(); + let to_code = |v: u16| (f64::from(v) * 1023.0 / 65535.0).round() as u16; + let want: Vec = [0u16, 32, 65535, 30000, 40000, 50000, 1, 2, 3].into_iter().map(to_code).collect(); + assert_eq!(cineon_written(&out), (3, 3, 1, want)); + assert!(fidelity_warnings(&wide, Format::Cineon).contains(&FidelityWarning::AlphaDiscarded)); + let gray8 = Image::from_u8(2, 1, ChannelLayout::Gray, vec![0, 255]).unwrap(); + assert_eq!(cineon_written(&encode(&gray8, Format::Cineon, &EncodeOptions::default()).unwrap()), (1, 2, 1, vec![0, 1023])); +} diff --git a/crates/image-codecs/tests/it/exr_cryptomatte.rs b/crates/image-codecs/tests/it/exr_cryptomatte.rs new file mode 100644 index 000000000..80c843492 --- /dev/null +++ b/crates/image-codecs/tests/it/exr_cryptomatte.rs @@ -0,0 +1,271 @@ +//! Cryptomatte (specification 1.2): the hash function is checked against the official +//! test vectors from the reference repository, synthetic files carry ID/coverage streams +//! with header attributes and a manifest, and the info/decode tests check the listing, +//! the manifest round trip and the per-pixel pairs. Malformed inputs must error or +//! degrade, never panic. + +use exr::meta::attribute::{AttributeValue, Text}; +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, WritableImage, +}; +use craft_image_codecs::{Limits, cryptomatte_id, cryptomatte_key, cryptomatte_layers, cryptomatte_preview_color, decode_cryptomatte}; + +// ------------------------------------------------------------------------------------------- +// The official hash test vectors (cryptomatte_utilities_tests.py, CryptoHashing). + +#[test] +// The values are quoted verbatim from the reference test suite; their extra digits are +// intentional documentation, not a precision mistake. +#[allow(clippy::excessive_precision)] +fn mm3_hash_matches_the_official_vectors() { + // The values are the f32 IDs the reference implementation produces (single precision). + let vectors: &[(&str, f64)] = &[ + ("hello", 6.0705627102400005616e-17), + ("cube", -4.08461912519e+15), + ("sphere", 2.79018604383e+15), + ("plane", 3.66557617593e-11), + ("равнина", -1.3192631212399999468e-25), + ("mädchen", 6.2361298211599995797e+25), + ]; + for (name, want) in vectors { + let got = cryptomatte_id(name); + assert_eq!(got.to_bits(), (*want as f32).to_bits(), "ID bits of {name:?}"); + } +} + +#[test] +fn every_id_is_a_finite_normal_float() { + // The exponent fix keeps IDs away from denormals, infinity and NaN, whatever the name. + for name in ["", "a", "hello", "CryptoAsset", "very long object name with ünïcödé"] { + let id = cryptomatte_id(name); + assert!(id.is_finite() && id.abs() >= f32::MIN_POSITIVE, "{name:?} -> {id}"); + } +} + +#[test] +fn key_is_seven_hex_digits_of_the_id() { + let key = cryptomatte_key("CryptoAsset"); + assert_eq!(key.len(), 7); + assert!(key.chars().all(|c| c.is_ascii_hexdigit())); + let full = format!("{:08x}", cryptomatte_id("CryptoAsset").to_bits()); + assert_eq!(key, full[..7]); +} + +#[test] +fn preview_color_shifts_the_id_bits() { + let id = cryptomatte_id("cube"); + let [r, g, b] = cryptomatte_preview_color(id); + assert_eq!((r, g, b), (0.0, f32::from_bits(id.to_bits() << 8), f32::from_bits(id.to_bits() << 16))); +} + +// ------------------------------------------------------------------------------------------- +// Synthetic Cryptomatte files. + +fn text(s: &str) -> Text { + Text::new_or_panic(s) +} + +/// A 4x4 EXR with one Cryptomatte layer `crypto_asset` of two streams: pixel (x, y) holds +/// `cube` (coverage 0.6) and `sphere` (coverage 0.25) where the streams' values say so. +fn cryptomatte_file(name: &str) -> Vec { + let (w, h) = (4usize, 4usize); + let cube = cryptomatte_id("cube"); + let sphere = cryptomatte_id("sphere"); + // Stream 00: cube everywhere at 0.6; stream 01: sphere only in the first column. + let s0: Vec = vec![cube; w * h]; + let s0c: Vec = vec![0.6; w * h]; + let s1: Vec = (0..w * h).map(|p| if p % w == 0 { sphere } else { 0.0 }).collect(); + let s1c: Vec = (0..w * h).map(|p| if p % w == 0 { 0.25 } else { 0.0 }).collect(); + let mk = |n: String, v: Vec| AnyChannel::new(n.as_str(), FlatSamples::F32(v)); + let list = vec![mk(format!("{name}00.red"), s0), mk(format!("{name}00.green"), s0c), mk(format!("{name}01.red"), s1), mk(format!("{name}01.green"), s1c)]; + let key = cryptomatte_key("CryptoAsset"); // attributes always use the canonical name + let manifest = format!("{{\"cube\":\"{:08x}\",\"sphere\":\"{:08x}\"}}", cube.to_bits(), sphere.to_bits()); + let mut attrs = LayerAttributes::named(name); + attrs.other.insert(text(&format!("cryptomatte/{key}/name")), AttributeValue::Text(text("CryptoAsset"))); + attrs.other.insert(text(&format!("cryptomatte/{key}/hash")), AttributeValue::Text(text(&key))); + attrs.other.insert(text(&format!("cryptomatte/{key}/conversion")), AttributeValue::Text(text("uint32_to_float32"))); + attrs.other.insert(text(&format!("cryptomatte/{key}/manifest")), AttributeValue::Text(text(&manifest))); + let layer = Layer::new( + (w, h), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic cryptomatte exr writes"); + out.into_inner() +} + +/// The same layer, but with the short `.r`/`.g` channel suffixes Nuke also matches. +fn cryptomatte_file_short_suffix() -> Vec { + let (w, h) = (4usize, 4usize); + let id = cryptomatte_id("cube"); + let mk = |n: String, v: Vec| AnyChannel::new(n.as_str(), FlatSamples::F32(v)); + let list = vec![mk("crypto_asset00.r".into(), vec![id; w * h]), mk("crypto_asset00.g".into(), vec![1.0; w * h])]; + let key = cryptomatte_key("CryptoAsset"); + let mut attrs = LayerAttributes::named("crypto_asset"); + attrs.other.insert(text(&format!("cryptomatte/{key}/name")), AttributeValue::Text(text("CryptoAsset"))); + let layer = Layer::new( + (w, h), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic cryptomatte exr writes"); + out.into_inner() +} + +#[test] +fn layers_list_name_streams_and_manifest() { + let bytes = cryptomatte_file("crypto_asset"); + let found = cryptomatte_layers(&bytes, &Limits::default()).unwrap(); + assert_eq!(found.len(), 1); + let layer = &found[0]; + assert_eq!(layer.part, 0); + assert_eq!(layer.name, "CryptoAsset"); + assert_eq!(layer.key, cryptomatte_key("CryptoAsset")); + assert_eq!(layer.conversion.as_deref(), Some("uint32_to_float32")); + assert_eq!(layer.channels, ["crypto_asset00", "crypto_asset01"]); + // The manifest maps names to their hash IDs, hex bits -> float value. + assert_eq!(layer.manifest.len(), 2); + let cube = cryptomatte_id("cube"); + let sphere = cryptomatte_id("sphere"); + assert!(layer.manifest.contains(&(cube, "cube".to_string()))); + assert!(layer.manifest.contains(&(sphere, "sphere".to_string()))); + assert_eq!(layer.name_of(cube), Some("cube")); +} + +#[test] +fn decode_gives_the_pairs_per_pixel_sorted_by_coverage() { + let bytes = cryptomatte_file("crypto_asset"); + let buf = decode_cryptomatte(&bytes, "CryptoAsset", &Limits::default()).unwrap(); + assert_eq!((buf.width, buf.height), (4, 4)); + let cube = cryptomatte_id("cube"); + let sphere = cryptomatte_id("sphere"); + for y in 0..4 { + // First column: cube (0.6) over sphere (0.25), sorted by coverage. + let both = buf.at(0, y); + assert_eq!(both, &[(cube, 0.6), (sphere, 0.25)], "column 0, row {y}"); + // Other columns: cube only. + for x in 1..4 { + assert_eq!(buf.at(x, y), &[(cube, 0.6)], "({x}, {y})"); + } + } +} + +#[test] +fn short_channel_suffixes_decode_too() { + let bytes = cryptomatte_file_short_suffix(); + let buf = decode_cryptomatte(&bytes, "CryptoAsset", &Limits::default()).unwrap(); + let cube = cryptomatte_id("cube"); + assert_eq!(buf.at(3, 3), &[(cube, 1.0)]); +} + +/// A 4x4 layer `crypto` of one RGBA set `crypto00.///` with the given suffixes: +/// rank 0 (red/green) holds `cube` at 0.6 everywhere, rank 1 (blue/alpha) holds `sphere` +/// at 0.4 everywhere, the second (ID, coverage) pair the specification stores per set. +fn cryptomatte_file_rgba(suffixes: [&str; 4]) -> Vec { + let (w, h) = (4usize, 4usize); + let [r, g, b, a] = suffixes; + let mk = |s: &str, v: f32| AnyChannel::new(format!("crypto00.{s}").as_str(), FlatSamples::F32(vec![v; w * h])); + let list = vec![mk(r, cryptomatte_id("cube")), mk(g, 0.6), mk(b, cryptomatte_id("sphere")), mk(a, 0.4)]; + let key = cryptomatte_key("crypto"); + let mut attrs = LayerAttributes::named("crypto"); + attrs.other.insert(text(&format!("cryptomatte/{key}/name")), AttributeValue::Text(text("crypto"))); + let layer = Layer::new( + (w, h), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic cryptomatte exr writes"); + out.into_inner() +} + +#[test] +fn blue_alpha_rank_of_a_set_decodes_too() { + // Each RGBA set holds two ranks: (red, green) and (blue, alpha). Both must come back. + let bytes = cryptomatte_file_rgba(["red", "green", "blue", "alpha"]); + let found = cryptomatte_layers(&bytes, &Limits::default()).unwrap(); + assert_eq!(found.len(), 1); + assert_eq!(found[0].channels, ["crypto00"], "one set, listed once"); + let buf = decode_cryptomatte(&bytes, "crypto", &Limits::default()).unwrap(); + let (cube, sphere) = (cryptomatte_id("cube"), cryptomatte_id("sphere")); + for y in 0..4 { + for x in 0..4 { + assert_eq!(buf.at(x, y), &[(cube, 0.6), (sphere, 0.4)], "({x}, {y})"); + } + } +} + +#[test] +fn uppercase_channel_suffixes_decode_both_ranks() { + // Blender and others write `.R/.G/.B/.A`; suffixes match regardless of case. + for suffixes in [["R", "G", "B", "A"], ["Red", "Green", "Blue", "Alpha"], ["r", "g", "b", "a"]] { + let bytes = cryptomatte_file_rgba(suffixes); + let buf = decode_cryptomatte(&bytes, "crypto", &Limits::default()).unwrap(); + let (cube, sphere) = (cryptomatte_id("cube"), cryptomatte_id("sphere")); + assert_eq!(buf.at(2, 1), &[(cube, 0.6), (sphere, 0.4)], "{suffixes:?}"); + } +} + +#[test] +fn unknown_layer_name_is_unsupported() { + let bytes = cryptomatte_file("crypto_asset"); + let e = decode_cryptomatte(&bytes, "CryptoMaterial", &Limits::default()).unwrap_err(); + assert!(e.to_string().contains("no Cryptomatte layer"), "{e}"); +} + +#[test] +fn plain_exr_has_no_cryptomatte_layers() { + // A normal RGB file (the multi-part fixture style) lists nothing. + let (w, h) = (2usize, 2usize); + let mk = |n: &str, v: f32| AnyChannel::new(n, FlatSamples::F32(vec![v; w * h])); + let list = vec![mk("R", 0.5), mk("G", 0.5), mk("B", 0.5)]; + let layer = Layer::new( + (w, h), + LayerAttributes::named("beauty"), + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic exr writes"); + assert!(cryptomatte_layers(&out.into_inner(), &Limits::default()).unwrap().is_empty()); +} + +#[test] +fn a_broken_manifest_degrades_to_empty_not_a_panic() { + let mut bytes = cryptomatte_file("crypto_asset"); + // Corrupt the embedded manifest text (keep the attribute length: overwrite with '#'). + let needle = b"{\"cube\":"; + let at = bytes.windows(needle.len()).position(|w| w == needle).expect("manifest json"); + for b in &mut bytes[at..at + 8] { + *b = b'#'; + } + let found = cryptomatte_layers(&bytes, &Limits::default()).unwrap(); + assert_eq!(found.len(), 1); + assert!(found[0].manifest.is_empty(), "a broken manifest yields no entries"); + // The samples still decode. + let buf = decode_cryptomatte(&bytes, "CryptoAsset", &Limits::default()).unwrap(); + assert_eq!(buf.at(1, 0).len(), 1); +} + +#[test] +fn hostile_input_never_panics() { + let bytes = cryptomatte_file("crypto_asset"); + for end in (8..bytes.len()).step_by(7) { + let cut = &bytes[..end]; + let _ = cryptomatte_layers(cut, &Limits::default()); + let _ = decode_cryptomatte(cut, "CryptoAsset", &Limits::default()); + let _ = decode_cryptomatte(cut, "nothing", &Limits::default()); + } + // Tiny limits refuse before allocating. + let e = cryptomatte_layers(&bytes, &Limits { max_alloc: 8, ..Limits::default() }).unwrap_err(); + assert!(e.to_string().contains("exceed"), "{e}"); +} diff --git a/crates/image-codecs/tests/it/exr_multipart.rs b/crates/image-codecs/tests/it/exr_multipart.rs new file mode 100644 index 000000000..246b3d9ed --- /dev/null +++ b/crates/image-codecs/tests/it/exr_multipart.rs @@ -0,0 +1,334 @@ +//! Multi-part EXR (a Maya/Arnold render writes one part per AOV): the `exr` crate +//! writes synthetic files with several parts, the info/decode tests check the part +//! listing, the colour-ranked auto-pick and part-by-index decoding, and malformed +//! inputs must error, never panic. + +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Image, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, + WritableImage, +}; +use craft_image_codecs::f16; +use craft_image_codecs::{ + ChannelLayout, DecodeOptions, DecodeWarning, Format, Limits, SampleType, decode, decode_as_with, decode_exr_part, exr_info, probe_size, +}; + +fn layer_f32(name: &str, comp: Compression, chans: &[(&str, Vec)]) -> Layer> { + let n = chans.iter().find(|(_, v)| !v.is_empty()).map_or(0, |(_, v)| v.len()); + let list = chans + .iter() + .map(|(cn, v)| { + assert_eq!(v.len(), n, "channel lengths must match"); + AnyChannel::new(*cn, FlatSamples::F32(v.clone())) + }) + .collect::>(); + let h = (n as f32).sqrt().round() as usize; // tests use square images + Layer::new( + (h, h), + LayerAttributes::named(name), + Encoding { compression: comp, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ) +} + +/// Three parts in the order an AOV-heavy render might use: a depth-only `Z` part first +/// (which used to make the whole file fail with "no R/G/B or Y channels"), the `beauty` +/// RGBA part second and a `diffuse` RGB part third, each with its own compression. +fn aov_file() -> Vec { + let n = 16; + let z: Vec = (0..n).map(|i| i as f32).collect(); + let beauty: [Vec; 4] = std::array::from_fn(|c| { + (0..n) + .map(|i| match c { + 0 => i as f32 / 15.0, + 1 => 1.0 - i as f32 / 15.0, + 2 => 0.25, + _ => 1.0, + }) + .collect() + }); + let diffuse: [Vec; 3] = std::array::from_fn(|c| (0..n).map(|_| [0.3, 0.6, 0.9][c]).collect()); + let layers = vec![ + layer_f32("Z", Compression::Uncompressed, &[("Z", z)]), + layer_f32("beauty", Compression::ZIP1, &[("R", beauty[0].clone()), ("G", beauty[1].clone()), ("B", beauty[2].clone()), ("A", beauty[3].clone())]), + layer_f32("diffuse", Compression::RLE, &[("R", diffuse[0].clone()), ("G", diffuse[1].clone()), ("B", diffuse[2].clone())]), + ]; + write_image(layers) +} + +fn write_image(layers: Vec>>) -> Vec { + let size: (usize, usize) = layers.first().map_or((1, 1), |l| l.size.into()); + let bounds = IntegerBounds::new((0, 0), size); + let image = Image::from_layers(ImageAttributes::new(bounds), layers); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic exr writes"); + out.into_inner() +} + +fn stereo_file() -> Vec { + let n = 4; + let px = |k: f32| -> [Vec; 4] { std::array::from_fn(|c| (0..n).map(|i| if c == 0 { i as f32 / 3.0 * k } else { k }).collect()) }; + let left = px(0.1); + let right = px(0.9); + let with_view = |name: &str, view: &str, p: [Vec; 4]| { + let attrs = LayerAttributes { view_name: Some(view.into()), ..LayerAttributes::named(name) }; + let list: Vec<_> = ["R", "G", "B", "A"].iter().enumerate().map(|(c, cn)| AnyChannel::new(*cn, FlatSamples::F32(p[c].clone()))).collect(); + Layer::new( + (2, 2), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ) + }; + // The `exr` crate refuses duplicate part names when writing, so a stereo pair is + // emulated with distinct names that still carry the view attribute. + write_image(vec![with_view("beauty_left", "left", left), with_view("beauty_right", "right", right)]) +} + +fn near(a: f32, b: f32) -> bool { + (a - b).abs() < 1e-6 +} + +#[test] +fn info_lists_every_part() { + let parts = exr_info(&aov_file(), &Limits::default()).unwrap(); + assert_eq!(parts.len(), 3); + assert_eq!(parts.iter().map(|p| p.name.as_deref()).collect::>(), [Some("Z"), Some("beauty"), Some("diffuse")]); + let z = &parts[0]; + assert_eq!((z.width, z.height), (4, 4)); + assert!(!z.deep && !z.tiled); + assert_eq!(z.channels.len(), 1); + assert_eq!(z.channels[0].name, "Z"); + assert_eq!(z.channels[0].sample, SampleType::F32); + let beauty = &parts[1]; + // OpenEXR stores the channel list in the header alphabetically; the info shows the file as-is. + assert_eq!(beauty.channels.iter().map(|c| c.name.as_str()).collect::>(), ["A", "B", "G", "R"]); + assert_eq!(beauty.channels.iter().map(|c| c.sample).collect::>(), [SampleType::F32; 4]); +} + +#[test] +fn auto_pick_skips_the_depth_part() { + let img = decode(&aov_file()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert!(img.warnings.contains(&DecodeWarning::MoreParts { total: Some(3) }), "{:?}", img.warnings); + for i in 0..16 { + let (x, y) = (i % 4, i / 4); + assert!(near(img.get(x, y, 0), i as f32 / 15.0), "pixel {i}"); + assert!(near(img.get(x, y, 1), 1.0 - i as f32 / 15.0), "pixel {i}"); + assert!(near(img.get(x, y, 2), 0.25), "pixel {i}"); + assert!(near(img.get(x, y, 3), 1.0), "pixel {i}"); + } +} + +#[test] +fn parts_decode_by_index() { + let bytes = aov_file(); + // The depth part opens as a grayscale image instead of failing the file. + let z = decode_exr_part(&bytes, 0, &DecodeOptions::default()).unwrap(); + assert_eq!(z.layout(), ChannelLayout::Gray); + for i in 0..16 { + assert!(near(z.get(i % 4, i / 4, 0), i as f32), "pixel {i}"); + } + let diffuse = decode_exr_part(&bytes, 2, &DecodeOptions::default()).unwrap(); + assert_eq!(diffuse.layout(), ChannelLayout::Rgb); + assert!(near(diffuse.get(1, 2, 2), 0.9)); + // An explicit part choice does not warn about the parts left out. + assert!(!diffuse.warnings.contains(&DecodeWarning::MoreParts { total: Some(3) })); +} + +#[test] +fn part_index_out_of_range_errors() { + let bytes = aov_file(); + let e = decode_exr_part(&bytes, 3, &DecodeOptions::default()).unwrap_err().to_string(); + assert!(e.contains("out of range") && e.contains("3 parts"), "{e}"); +} + +#[test] +fn views_are_listed_and_left_wins_the_auto_pick() { + let bytes = stereo_file(); + let parts = exr_info(&bytes, &Limits::default()).unwrap(); + assert_eq!(parts.iter().map(|p| p.view.as_deref()).collect::>(), [Some("left"), Some("right")]); + let img = decode(&bytes).unwrap(); + assert!(near(img.get(0, 0, 0), 0.0) && near(img.get(3, 0, 0), 0.1), "the left view is the first part"); + assert!(near(img.get(1, 1, 1), 0.1), "left's G is 0.1 everywhere"); + let right = decode_exr_part(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert!(near(right.get(1, 1, 1), 0.9), "right's G is 0.9 everywhere"); +} + +#[test] +fn single_part_files_do_not_warn() { + let n = 4; + let v: Vec = (0..n).map(|i| i as f32 / 3.0).collect(); + let bytes = write_image(vec![layer_f32("main", Compression::ZIP1, &[("Y", v)])]); + let img = decode(&bytes).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert!(img.warnings.is_empty(), "{:?}", img.warnings); +} + +#[test] +fn half_float_parts_stay_half() { + let n = 4; + let chans: [Vec; 3] = std::array::from_fn(|c| (0..n).map(|_| f16::from_f32([0.5, 0.25, 1.0][c])).collect()); + let list: Vec<_> = ["R", "G", "B"].iter().enumerate().map(|(c, cn)| AnyChannel::new(*cn, FlatSamples::F16(chans[c].clone()))).collect(); + let layer = Layer::new( + (2, 2), + LayerAttributes::named("half"), + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let bytes = write_image(vec![layer]); + let img = decode_exr_part(&bytes, 0, &DecodeOptions::default()).unwrap(); + assert_eq!(img.sample_type(), SampleType::F16); + assert!(near(img.get(1, 1, 0), 0.5)); +} + +#[test] +fn truncated_files_error_never_panic() { + let bytes = aov_file(); + for end in (8..bytes.len()).step_by(5) { + let cut = &bytes[..end]; + let _ = exr_info(cut, &Limits::default()); + let _ = decode_as_with(Format::OpenExr, cut, &DecodeOptions::default()); + let _ = decode_exr_part(cut, 1, &DecodeOptions::default()); + } +} + +#[test] +fn parts_are_capped_together() { + // Each part fits alone (largest 256 bytes) but the three together (512) do not. + let limits = Limits { max_alloc: 300, ..Limits::default() }; + let e = exr_info(&aov_file(), &limits).unwrap_err().to_string(); + assert!(e.contains("across 3 parts"), "{e}"); +} + +/// Two ZIP1 parts of 4x4 RGBA; every chunk of part 0 is then overwritten with garbage, so +/// inflating it fails. +fn file_with_corrupt_first_part() -> Vec { + let px = |k: f32| -> Vec<(&'static str, Vec)> { ["R", "G", "B", "A"].iter().map(|c| (*c, vec![k; 16])).collect() }; + let mut bytes = write_image(vec![layer_f32("first", Compression::ZIP1, &px(0.25)), layer_f32("second", Compression::ZIP1, &px(0.75))]); + let mut cursor = std::io::Cursor::new(&bytes[..]); + exr::meta::MetaData::read_from_buffered(&mut cursor, false).unwrap(); + let table = cursor.position() as usize; + // Part 0 owns the first four of the eight chunks (one scan line per ZIP1 block). + for i in 0..4 { + let at = table + i * 8; + let offset = u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap()) as usize; + let size = u32::from_le_bytes(bytes[offset + 8..offset + 12].try_into().unwrap()) as usize; + bytes[offset + 12..offset + 12 + size].fill(0xA5); + } + bytes +} + +#[test] +fn decoding_one_part_does_not_decode_the_others() { + let bytes = file_with_corrupt_first_part(); + // Part 0 is unreadable ... + assert!(decode_exr_part(&bytes, 0, &DecodeOptions::default()).is_err()); + // ... but asking for part 1 must not touch it. + let img = decode_exr_part(&bytes, 1, &DecodeOptions::default()).expect("part 1 decodes on its own"); + assert_eq!((img.width(), img.height()), (4, 4)); + assert!(img.to_normalized().iter().all(|v| near(*v, 0.75)), "wrong pixels"); +} + +fn channel_group_file(channels: &[(&str, f32)]) -> Vec { + let channels: Vec<_> = channels.iter().map(|(name, value)| (*name, vec![*value; 4])).collect(); + write_image(vec![layer_f32("groups", Compression::ZIP1, &channels)]) +} + +fn assert_channel_group(bytes: &[u8], layout: ChannelLayout, expected: &[f32]) { + assert_eq!(exr_info(bytes, &Limits::default()).unwrap()[0].color_rank(), expected.len() as u8); + assert_eq!(probe_size(bytes).unwrap(), (2, 2)); + for img in [decode(bytes).unwrap(), decode_exr_part(bytes, 0, &DecodeOptions::default()).unwrap()] { + assert_eq!(img.layout(), layout); + assert_eq!(img.to_normalized(), expected.repeat(4)); + } +} + +#[test] +fn flat_decode_keeps_rgb_channels_in_one_group() { + // A partial group sorts before the nested beauty layer, but cannot donate its R. + let bytes = channel_group_file(&[("aaa.R", 9.0), ("beauty.diffuse.R", 0.25), ("beauty.diffuse.G", 0.5), ("beauty.diffuse.B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); +} + +#[test] +fn flat_decode_does_not_borrow_another_groups_alpha() { + for alpha in ["A", "aaa.A", "beauty.other.A"] { + let bytes = channel_group_file(&[(alpha, 0.125), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); + let bytes = channel_group_file(&[(alpha, 0.125), ("beauty.Y", 0.5)]); + assert_channel_group(&bytes, ChannelLayout::Gray, &[0.5]); + } +} + +#[test] +fn flat_decode_rejects_rgb_split_between_groups() { + for channels in [ + [("aaa.R", 0.25), ("bbb.G", 0.5), ("ccc.B", 2.0)], + [("beauty.R", 0.25), ("beauty.diffuse.G", 0.5), ("beauty.diffuse.B", 2.0)], + [(".R", 0.25), ("G", 0.5), ("B", 2.0)], + ] { + let bytes = channel_group_file(&channels); + assert_eq!(exr_info(&bytes, &Limits::default()).unwrap()[0].color_rank(), 0); + assert!(probe_size(&bytes).is_err()); + assert!(decode(&bytes).is_err()); + assert!(decode_exr_part(&bytes, 0, &DecodeOptions::default()).is_err()); + } +} + +#[test] +fn auto_pick_and_probe_skip_incoherent_rgb_parts() { + let bytes = write_image(vec![ + layer_f32("split", Compression::ZIP1, &[("aaa.R", vec![9.0; 4]), ("bbb.G", vec![8.0; 4]), ("ccc.B", vec![7.0; 4])]), + layer_f32("luminance", Compression::ZIP1, &[("Y", vec![0.5; 16]), ("A", vec![0.75; 16])]), + ]); + let parts = exr_info(&bytes, &Limits::default()).unwrap(); + assert_eq!(parts.iter().map(|part| part.color_rank()).collect::>(), [0, 2]); + assert_eq!(probe_size(&bytes).unwrap(), (4, 4)); + let img = decode(&bytes).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (4, 4, ChannelLayout::GrayA)); + assert_eq!(img.to_f32_samples().unwrap(), [0.5, 0.75].repeat(16)); +} + +#[test] +fn flat_decode_prefers_the_highest_ranked_complete_group() { + let bytes = + channel_group_file(&[("aaa.R", 9.0), ("aaa.G", 8.0), ("aaa.B", 7.0), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0), ("beauty.A", 0.75)]); + assert_channel_group(&bytes, ChannelLayout::Rgba, &[0.25, 0.5, 2.0, 0.75]); + let bytes = channel_group_file(&[("aaa.Y", 9.0), ("beauty.Y", 0.5), ("beauty.A", 0.75)]); + assert_channel_group(&bytes, ChannelLayout::GrayA, &[0.5, 0.75]); +} + +#[test] +fn flat_decode_breaks_group_ties_by_prefix() { + let bytes = channel_group_file(&[("z.R", 9.0), ("z.G", 8.0), ("z.B", 7.0), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); + // Even a prefix that sorts before the unprefixed channel names loses a rank tie. + let bytes = channel_group_file(&[("0.R", 9.0), ("0.G", 8.0), ("0.B", 7.0), ("R", 0.25), ("G", 0.5), ("B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); +} + +#[test] +fn flat_decode_preserves_luminance_and_lone_channel_fallbacks() { + for prefix in ["", "beauty."] { + let y = format!("{prefix}Y"); + let a = format!("{prefix}A"); + assert_channel_group(&channel_group_file(&[(&y, 2.0), (&a, 0.75)]), ChannelLayout::GrayA, &[2.0, 0.75]); + assert_channel_group(&channel_group_file(&[(&y, 2.0)]), ChannelLayout::Gray, &[2.0]); + } + for name in ["depth.Z", "A", "beauty.A"] { + // A lone alpha is one grayscale channel, not the same samples duplicated as YA. + assert_channel_group(&channel_group_file(&[(name, 9.0)]), ChannelLayout::Gray, &[9.0]); + } +} + +#[test] +fn half_float_group_does_not_use_unrelated_float_channels() { + let mut layer = layer_f32("mixed", Compression::ZIP1, &[("aaa.R", vec![9.0; 4]), ("aaa.A", vec![0.125; 4])]); + for (name, value) in [("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0), ("beauty.A", 0.75)] { + layer.channel_data.list.push(AnyChannel::new(name, FlatSamples::F16(vec![f16::from_f32(value); 4]))); + } + layer.channel_data = AnyChannels::sort(layer.channel_data.list); + let bytes = write_image(vec![layer]); + assert_channel_group(&bytes, ChannelLayout::Rgba, &[0.25, 0.5, 2.0, 0.75]); + assert_eq!(decode(&bytes).unwrap().sample_type(), SampleType::F16); +} diff --git a/crates/image-codecs/tests/it/exr_stream.rs b/crates/image-codecs/tests/it/exr_stream.rs new file mode 100644 index 000000000..5d6ee4158 --- /dev/null +++ b/crates/image-codecs/tests/it/exr_stream.rs @@ -0,0 +1,161 @@ +use craft_image_codecs::{exr_stream::Decoder, *}; +use std::io::Cursor; + +fn fixture(layout: ChannelLayout, sample: SampleType, compression: ExrCompression) -> Vec { + let n = 31 * 35 * layout.channels(); + let values: Vec = (0..n) + .map(|i| match i % 5 { + 0 => -0.25, + 1 => 3.5, + 2 => 0.0, + 3 => 0.0001, + _ => 0.5, + }) + .collect(); + let image = Image::from_f32(31, 35, layout, &values).unwrap().convert(layout, sample); + encode(&image, Format::OpenExr, &EncodeOptions { exr_compression: compression, ..Default::default() }).unwrap() +} + +#[test] +fn scanline_blocks_match_whole_decoder_at_each_layout_depth_and_compression() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for sample in [SampleType::F16, SampleType::F32] { + for compression in [ExrCompression::None, ExrCompression::Rle, ExrCompression::Zip1, ExrCompression::Zip16, ExrCompression::Piz] { + let bytes = fixture(layout, sample, compression); + let reference = decode(&bytes).unwrap(); + let target = if layout.is_gray() { ChannelLayout::GrayA } else { ChannelLayout::Rgba }; + let reference = reference.convert(target, SampleType::F32); + let mut stream = Decoder::new(Cursor::new(&bytes), &|| false).unwrap(); + assert_eq!(stream.info().all_half, sample == SampleType::F16); + let row = 31 * target.channels() * 4; + let mut actual = vec![0; reference.data().len()]; + let mut blocks = 0; + while let Some(band) = stream.next_band(&|| false).unwrap() { + actual[band.y as usize * row..(band.y + band.height) as usize * row].copy_from_slice(&band.data); + blocks += 1; + } + assert_eq!(blocks, stream.info().blocks); + assert_eq!(actual, reference.data(), "{layout:?}/{sample:?}/{compression:?}"); + } + } + } +} + +fn change_window(bytes: &mut [u8], width: i32, height: i32) { + let needle = b"dataWindow\0box2i\0\x10\0\0\0"; + let at = bytes.windows(needle.len()).position(|v| v == needle).unwrap() + needle.len(); + bytes[at + 8..at + 12].copy_from_slice(&(width - 1).to_le_bytes()); + bytes[at + 12..at + 16].copy_from_slice(&(height - 1).to_le_bytes()); +} + +#[test] +fn enormous_header_is_not_a_whole_image_allocation() { + let mut bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + change_window(&mut bytes, 65536, 32768); + assert!(matches!(decode(&bytes), Err(CodecError::LimitExceeded(ref message)) if message.contains("2147483648 pixels"))); + // Streaming gets as far as the now-truncated offset table, without a pixel allocation. + assert!(matches!(Decoder::new(Cursor::new(bytes), &|| false), Err(CodecError::Malformed { .. }))); +} + +#[test] +fn cancellation_truncation_duplicate_offsets_and_forged_lengths_fail() { + let bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + assert!(Decoder::new(Cursor::new(&bytes), &|| true).is_err()); + let mut decoder = Decoder::new(Cursor::new(&bytes), &|| false).unwrap(); + assert!(decoder.next_band(&|| true).is_err()); + for end in [0, 8, bytes.len() / 2] { + assert!(Decoder::new(Cursor::new(&bytes[..end]), &|| false).and_then(|mut r| r.next_band(&|| false)).is_err()); + } + // Locate the offset table by finding the validated header end. + let mut input = Cursor::new(&bytes); + let _ = exr::meta::MetaData::read_from_buffered(&mut input, false).unwrap(); + let at = input.position() as usize; + let offset = u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap()) as usize; + let mut corrupt = bytes.clone(); + corrupt[offset + 4..offset + 8].copy_from_slice(&i32::MAX.to_le_bytes()); + assert!(Decoder::new(Cursor::new(corrupt), &|| false).unwrap().next_band(&|| false).is_err()); + let mut duplicate = bytes.clone(); + duplicate[at + 8..at + 16].copy_from_slice(&bytes[at..at + 8]); + let mut decoder = Decoder::new(Cursor::new(duplicate), &|| false).unwrap(); + assert!(decoder.next_band(&|| false).unwrap().is_some()); + assert!(decoder.next_band(&|| false).is_err()); +} + +#[test] +fn hostile_metadata_is_bounded_and_mutated_inputs_never_panic() { + let bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + let mut large = bytes.clone(); + let needle = b"channels\0chlist\0"; + let length = bytes.windows(needle.len()).position(|v| v == needle).unwrap() + needle.len(); + large[length..length + 4].copy_from_slice(&i32::MAX.to_le_bytes()); + assert!(matches!(Decoder::new(Cursor::new(large), &|| false), Err(CodecError::LimitExceeded(_)))); + let mut many = bytes[..8].to_vec(); + for _ in 0..4097 { + many.extend_from_slice(b"z\0string\0\0\0\0\0"); + } + many.push(0); + assert!(matches!(Decoder::new(Cursor::new(many), &|| false), Err(CodecError::LimitExceeded(_)))); + // Small deterministic mutations cover headers, offsets and compressed payloads. + for i in 0..256 { + let mut corrupt = bytes.clone(); + let at = i * (bytes.len() - 1) / 255; + corrupt[at] ^= 0xff; + assert!( + std::panic::catch_unwind(|| { + if let Ok(mut decoder) = Decoder::new(Cursor::new(corrupt), &|| false) { + while let Ok(Some(_)) = decoder.next_band(&|| false) {} + } + }) + .is_ok(), + "mutation at byte {at}" + ); + } +} + +#[test] +fn mixed_samples_and_offset_data_window_keep_coordinates_and_values() { + use exr::prelude::{AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, WritableImage}; + let channels = AnyChannels::sort( + vec![ + AnyChannel::new("R", FlatSamples::F16(vec![f16::from_f32(-0.25); 31 * 35])), + AnyChannel::new("G", FlatSamples::F32(vec![3.5; 31 * 35])), + AnyChannel::new("B", FlatSamples::U32(vec![42; 31 * 35])), + ] + .into_iter() + .collect(), + ); + let attributes = LayerAttributes { layer_position: exr::math::Vec2(-15, 40), ..Default::default() }; + let layer = + Layer::new((31, 35), attributes, Encoding { compression: Compression::ZIP16, blocks: Blocks::ScanLines, line_order: LineOrder::Decreasing }, channels); + let mut bytes = Cursor::new(Vec::new()); + exr::image::Image::from_layer(layer).write().to_buffered(&mut bytes).unwrap(); + let mut decoder = Decoder::new(bytes, &|| false).unwrap(); + assert!(!decoder.info().all_half); + let mut rows = 0; + while let Some(band) = decoder.next_band(&|| false).unwrap() { + assert_eq!(band.y, rows); + rows += band.height; + for p in band.data.as_chunks::<16>().0 { + let v: Vec = p.as_chunks::<4>().0.iter().map(|s| f32::from_le_bytes(*s)).collect(); + assert_eq!(v, [-0.25, 3.5, 42.0, 1.0]); + } + } + assert_eq!(rows, 35); +} + +#[test] +fn unsupported_variants_stay_on_the_guarded_legacy_path() { + let bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + for flag in [0x200_u32, 0x800, 0x1000] { + let mut changed = bytes.clone(); + changed[4..8].copy_from_slice(&(2 | flag).to_le_bytes()); + assert!(matches!(Decoder::new(Cursor::new(changed), &|| false), Err(CodecError::Unsupported { .. }))); + } + let mut custom = bytes[..8].to_vec(); + custom.extend_from_slice(b"chromaticities\0chromaticities\0\x20\0\0\0"); + for v in [0.64_f32, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] { + custom.extend_from_slice(&v.to_le_bytes()); + } + custom.extend_from_slice(&bytes[8..]); + assert!(matches!(Decoder::new(Cursor::new(custom), &|| false), Err(CodecError::Unsupported { .. }))); +} diff --git a/crates/image-codecs/tests/it/fidelity.rs b/crates/image-codecs/tests/it/fidelity.rs new file mode 100644 index 000000000..37c7aba0c --- /dev/null +++ b/crates/image-codecs/tests/it/fidelity.rs @@ -0,0 +1,243 @@ +//! `fidelity_warnings` cases, and consistency with what `encode` does. + +use crate::common; +use FidelityWarning as W; +use common::*; +use craft_image_codecs::*; + +fn warns(img: &Image, f: Format) -> Vec { + fidelity_warnings(img, f) +} + +#[test] +fn png_u8_rgb_is_clean() { + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::U8), Format::Png).is_empty()); +} +#[test] +fn png_u16_rgba_is_clean() { + assert!(warns(&test_image(ChannelLayout::Rgba, SampleType::U16), Format::Png).is_empty()); +} +#[test] +fn tiff_is_clean_for_all_integer_and_f32_layouts() { + for l in ChannelLayout::ALL { + for s in [SampleType::U8, SampleType::U16, SampleType::F32, SampleType::F16] { + let w = warns(&test_image(l, s), Format::Tiff); + if l.is_lab() && s.is_float() { + // CIELab has no float form: it is written with 16 bits, and says so. + assert_eq!(w, vec![FidelityWarning::DepthReduced { from: s, to: SampleType::U16 }], "{l:?} {s:?}"); + } else { + assert!(w.is_empty(), "{l:?} {s:?}"); + } + } + } +} +#[test] +fn sixteen_bit_to_jpeg_reduces() { + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::U16), Format::Jpeg); + assert!(w.contains(&W::DepthReduced { from: SampleType::U16, to: SampleType::U8 })); + assert!(w.contains(&W::LossyCompression)); +} +#[test] +fn sixteen_bit_message_text() { + let w = W::DepthReduced { from: SampleType::U16, to: SampleType::U8 }; + assert_eq!(w.to_string(), "16-bit will be reduced to 8-bit"); +} +#[test] +fn alpha_to_jpeg_discarded() { + let w = warns(&test_image(ChannelLayout::Rgba, SampleType::U8), Format::Jpeg); + assert!(w.contains(&W::AlphaDiscarded)); +} +#[test] +fn opaque_alpha_not_reported() { + let mut img = test_image(ChannelLayout::Rgba, SampleType::U8); + for px in img.data_mut().chunks_mut(4) { + px[3] = 255; + } + assert!(!warns(&img, Format::Jpeg).contains(&W::AlphaDiscarded)); +} +#[test] +fn alpha_to_hdr_discarded() { + assert!(warns(&test_image(ChannelLayout::Rgba, SampleType::F32), Format::Hdr).contains(&W::AlphaDiscarded)); +} +#[test] +fn cmyk_to_png_converted() { + let w = warns(&test_image(ChannelLayout::Cmyk, SampleType::U8), Format::Png); + assert!(w.contains(&W::CmykConverted { to: ChannelLayout::Rgb })); +} +#[test] +fn cmyka_to_webp_converted_keeps_alpha() { + let w = warns(&test_image(ChannelLayout::CmykA, SampleType::U8), Format::WebP); + assert!(w.contains(&W::CmykConverted { to: ChannelLayout::Rgba })); + assert!(!w.contains(&W::AlphaDiscarded)); +} +#[test] +fn cmyk_to_jpeg_and_tiff_not_converted() { + for f in [Format::Jpeg, Format::Tiff, Format::Pnm] { + let w = warns(&test_image(ChannelLayout::Cmyk, SampleType::U8), f); + assert!(!w.iter().any(|w| matches!(w, W::CmykConverted { .. })), "{f:?}"); + } +} +#[test] +fn icc_dropped_for_bmp() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(100))); + assert!(warns(&img, Format::Bmp).contains(&W::IccDropped)); + assert!(!warns(&img, Format::Png).contains(&W::IccDropped)); +} +#[test] +fn icc_warning_suppressed_when_not_embedding() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(100))); + let opts = EncodeOptions { embed_icc: false, ..Default::default() }; + assert!(!fidelity_warnings_with(&img, Format::Bmp, &opts).contains(&W::IccDropped)); +} +#[test] +fn exif_xmp_dpi_text_dropped_for_qoi() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta = + Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + let w = warns(&img, Format::Qoi); + for x in [W::ExifDropped, W::XmpDropped, W::DpiDropped, W::TextDropped] { + assert!(w.contains(&x), "{x:?}"); + } +} +/// WebP keeps the resolution in EXIF (#2399); only free-form text has nowhere to go. +#[test] +fn webp_drops_text_only() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta = + Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + let w = warns(&img, Format::WebP); + assert_eq!(w, vec![W::TextDropped]); +} +#[test] +fn float_to_png_reduces_and_clips() { + let vals: Vec = (0..12).map(|i| i as f32 * 0.3 - 0.5).collect(); + let img = Image::from_f32(2, 2, ChannelLayout::Rgb, &vals).unwrap(); + let w = warns(&img, Format::Png); + assert!(w.contains(&W::DepthReduced { from: SampleType::F32, to: SampleType::U16 })); + assert!(w.contains(&W::RangeClipped)); +} +#[test] +fn in_range_float_not_clipped() { + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::F32), Format::Png); + assert!(!w.contains(&W::RangeClipped)); +} +#[test] +fn hdr_float_to_fits_clips() { + let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap(); + assert!(warns(&img, Format::Fits).contains(&W::RangeClipped)); + assert!(!warns(&test_image(ChannelLayout::Gray, SampleType::F32), Format::Fits).contains(&W::RangeClipped)); +} +#[test] +fn float_to_exr_and_tiff_is_clean() { + for f in [Format::OpenExr, Format::Tiff, Format::Pnm] { + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::F32), f).is_empty(), "{f:?}"); + } +} +#[test] +fn f16_to_exr_is_clean() { + assert!(warns(&test_image(ChannelLayout::Rgba, SampleType::F16), Format::OpenExr).is_empty()); +} +#[test] +fn integer_to_exr_is_lossless() { + for s in [SampleType::U8, SampleType::U16] { + assert!(warns(&test_image(ChannelLayout::Rgb, s), Format::OpenExr).is_empty(), "{s:?}"); + } +} +#[test] +fn float_pnm_drops_alpha() { + let w = warns(&test_image(ChannelLayout::Rgba, SampleType::F32), Format::Pnm); + assert!(w.contains(&W::AlphaDiscarded)); +} +#[test] +fn gif_quantizes_and_binarizes() { + let w = warns(&test_image(ChannelLayout::Rgba, SampleType::U8), Format::Gif); + assert!(w.contains(&W::PaletteQuantized)); + assert!(w.contains(&W::AlphaBinarized)); + assert!(!w.contains(&W::LossyCompression)); +} +#[test] +fn iff_binarizes_alpha_and_reduces_16_bit() { + assert_eq!(warns(&test_image(ChannelLayout::Rgba, SampleType::U8), Format::Iff), vec![W::AlphaBinarized]); + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::U16), Format::Iff); + assert!(w.contains(&W::DepthReduced { from: SampleType::U16, to: SampleType::U8 })); + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::U8), Format::Iff).is_empty()); +} +#[test] +fn hdr_is_lossy() { + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::F32), Format::Hdr).contains(&W::LossyCompression)); +} +#[test] +fn ico_oversize_is_fatal() { + let img = Image::new(512, 16, ChannelLayout::Rgba, SampleType::U8).unwrap(); + let w = warns(&img, Format::Ico); + assert!(w.iter().any(|w| w.is_fatal())); + assert!(w.contains(&W::DimensionsExceeded { max_width: 256, max_height: 256 })); +} +#[test] +fn avif_default_build_write_unsupported() { + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::U8), Format::Avif); + if cfg!(feature = "avif") { + assert!(w.contains(&W::LossyCompression)); + } else { + assert_eq!(w, vec![W::WriteUnsupported { format: Format::Avif }]); + assert!(w[0].is_fatal()); + } +} +#[test] +fn every_warning_has_a_message() { + let all = [ + W::WriteUnsupported { format: Format::Avif }, + W::DepthReduced { from: SampleType::F32, to: SampleType::F16 }, + W::BitsReduced { from: SampleType::U16, bits: 10 }, + W::RangeClipped, + W::AlphaDiscarded, + W::AlphaBinarized, + W::CmykConverted { to: ChannelLayout::Rgb }, + W::LabConverted { to: ChannelLayout::Rgb }, + W::ColorToGray, + W::PaletteQuantized, + W::LossyCompression, + W::IccDropped, + W::ExifDropped, + W::XmpDropped, + W::DpiDropped, + W::TextDropped, + W::DimensionsExceeded { max_width: 1, max_height: 1 }, + ]; + for w in all { + assert!(w.to_string().len() > 5); + } +} + +/// The warnings must agree with what actually comes back from the file. +#[test] +fn warnings_match_actual_roundtrip_behaviour() { + for f in rw_formats() { + for l in ChannelLayout::ALL { + for s in SampleType::ALL { + let img = test_image(l, s); + let w = warns(&img, f); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + let depth_reduced = w.iter().any(|w| match w { + W::DepthReduced { .. } => true, + W::BitsReduced { from, .. } => from.is_float(), + _ => false, + }); + let lost_depth = back.sample_type() != s && back.sample_type() != SampleType::F32 && s != SampleType::U8; + assert_eq!(depth_reduced, lost_depth, "{f:?} {l:?} {s:?} {w:?} -> {:?}", back.sample_type()); + // Cineon stores 10 bits: 16-bit samples come back changed exactly when warned. + if f == Format::Cineon && s == SampleType::U16 && back.sample_type() == s { + let bits_reduced = w.iter().any(|w| matches!(w, W::BitsReduced { .. })); + let changed = back.to_u16_samples() != img.convert(back.layout(), s).to_u16_samples(); + assert_eq!(bits_reduced, changed, "{f:?} {l:?} {w:?}"); + } + let alpha_lost = l.has_alpha() && !back.layout().has_alpha(); + assert_eq!(w.contains(&W::AlphaDiscarded), alpha_lost, "{f:?} {l:?} {s:?}"); + let cmyk_lost = l.is_cmyk() && !back.layout().is_cmyk(); + assert_eq!(w.iter().any(|w| matches!(w, W::CmykConverted { .. })), cmyk_lost, "{f:?} {l:?} {s:?}"); + let lab_lost = l.is_lab() && !back.layout().is_lab(); + assert_eq!(w.iter().any(|w| matches!(w, W::LabConverted { .. })), lab_lost, "{f:?} {l:?} {s:?}"); + } + } + } +} diff --git a/crates/image-codecs/tests/it/gif_frames.rs b/crates/image-codecs/tests/it/gif_frames.rs new file mode 100644 index 000000000..df7b29839 --- /dev/null +++ b/crates/image-codecs/tests/it/gif_frames.rs @@ -0,0 +1,168 @@ +//! `decode_gif_frames`: every frame of an animated GIF, composited, with its delay. + +use craft_image_codecs::{AnimationFrame, CodecError, DecodeOptions, DecodeWarning, Format, Limits, decode, decode_gif_frames}; + +const RED: [u8; 4] = [255, 0, 0, 255]; +const GREEN: [u8; 4] = [0, 255, 0, 255]; +const BLUE: [u8; 4] = [0, 0, 255, 255]; +const CLEAR: [u8; 4] = [0, 0, 0, 0]; + +fn frames(bytes: &[u8]) -> Vec { + decode_gif_frames(bytes, &DecodeOptions::default()).unwrap() +} + +fn pixels(f: &AnimationFrame) -> Vec { + f.image.to_rgba8() +} + +/// Solid full-screen frames written by the `image` crate's encoder, `delay_ms` apart. +fn solid_gif(colours: &[[u8; 4]], w: u32, h: u32, delay_ms: u32) -> Vec { + let mut out = Vec::new(); + let mut enc = image::codecs::gif::GifEncoder::new(&mut out); + let list = colours.iter().map(|c| { + let buf = image::RgbaImage::from_pixel(w, h, image::Rgba(*c)); + image::Frame::from_parts(buf, 0, 0, image::Delay::from_numer_denom_ms(delay_ms, 1)) + }); + enc.encode_frames(list).unwrap(); + drop(enc); + out +} + +/// A palette-index image LZW-packed with a 2-bit minimum code size. Few enough pixels that the +/// code size never grows past 3 bits. +fn lzw2(indices: &[u8]) -> Vec { + let mut codes = vec![4u8]; + codes.extend_from_slice(indices); + codes.push(5); + let (mut acc, mut nbits, mut out) = (0u32, 0, Vec::new()); + for c in codes { + acc |= u32::from(c) << nbits; + nbits += 3; + while nbits >= 8 { + out.push((acc & 0xFF) as u8); + acc >>= 8; + nbits -= 8; + } + } + if nbits > 0 { + out.push(acc as u8); + } + out +} + +/// One image of [`raw_gif`]: `(x, y, width, height, disposal method, palette indices)`. +type Part<'a> = (u16, u16, u16, u16, u8, &'a [u8]); + +/// A hand-built GIF89a: a `w`x`h` screen with the 4-colour global palette red, green, blue, +/// black, and one image per [`Part`]. Frames are 10 cs apart. +fn raw_gif(w: u16, h: u16, parts: &[Part]) -> Vec { + let mut b = b"GIF89a".to_vec(); + b.extend_from_slice(&w.to_le_bytes()); + b.extend_from_slice(&h.to_le_bytes()); + b.extend_from_slice(&[0b1000_0001, 0, 0]); + b.extend_from_slice(&[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0]); + for &(x, y, fw, fh, disposal, indices) in parts { + // Graphic control extension: disposal method, 10 cs delay, no transparency. + b.extend_from_slice(&[0x21, 0xF9, 4, disposal << 2, 10, 0, 0, 0]); + b.push(0x2C); + for v in [x, y, fw, fh] { + b.extend_from_slice(&v.to_le_bytes()); + } + b.extend_from_slice(&[0, 2]); + let data = lzw2(indices); + b.push(data.len() as u8); + b.extend_from_slice(&data); + b.push(0); + } + b.push(0x3B); + b +} + +#[test] +fn a_still_gif_gives_one_frame_equal_to_decode() { + let still = solid_gif(&[RED], 3, 2, 100); + let all = frames(&still); + assert_eq!(all.len(), 1); + assert_eq!((all[0].image.width(), all[0].image.height()), (3, 2)); + assert_eq!(pixels(&all[0]), decode(&still).unwrap().to_rgba8()); + assert!(all[0].image.warnings.is_empty()); +} + +#[test] +fn every_frame_comes_back_in_order_with_its_delay() { + let all = frames(&solid_gif(&[RED, GREEN, BLUE], 2, 2, 120)); + assert_eq!(all.len(), 3); + for (f, want) in all.iter().zip([RED, GREEN, BLUE]) { + assert_eq!(pixels(f), want.repeat(4)); + assert_eq!(f.delay_ms, 120); + } +} + +#[test] +fn a_smaller_frame_is_drawn_over_the_previous_one() { + // Frame 2 is one pixel at x = 1, kept ("do not dispose", 1): the screen is red, blue. + let all = frames(&raw_gif(2, 1, &[(0, 0, 2, 1, 1, &[0, 0]), (1, 0, 1, 1, 1, &[2])])); + assert_eq!(all.len(), 2); + assert_eq!(pixels(&all[0]), [RED, RED].concat()); + assert_eq!(pixels(&all[1]), [RED, BLUE].concat()); + assert_eq!(all[1].delay_ms, 100); +} + +#[test] +fn disposal_to_background_clears_the_previous_frame() { + // Frame 1 (disposal 2, restore to background) covers the screen; frame 2 draws one pixel. + let all = frames(&raw_gif(2, 1, &[(0, 0, 2, 1, 2, &[1, 1]), (1, 0, 1, 1, 1, &[2])])); + assert_eq!(pixels(&all[0]), [GREEN, GREEN].concat()); + assert_eq!(pixels(&all[1]), [CLEAR, BLUE].concat()); +} + +#[test] +fn disposal_to_previous_restores_what_was_there() { + // Frame 1 is kept; frame 2 (disposal 3, restore previous) paints one pixel over it, so frame + // 3, which draws elsewhere, sees frame 1 again. + let all = frames(&raw_gif(2, 1, &[(0, 0, 2, 1, 1, &[0, 0]), (0, 0, 1, 1, 3, &[1]), (1, 0, 1, 1, 1, &[2])])); + assert_eq!(pixels(&all[1]), [GREEN, RED].concat()); + assert_eq!(pixels(&all[2]), [RED, BLUE].concat()); +} + +#[test] +fn a_damaged_later_frame_keeps_the_frames_before_it() { + let good = solid_gif(&[RED, GREEN, BLUE], 8, 8, 100); + // Cut inside the last frame's data, so frames 1 and 2 are intact. + let cut = &good[..good.len() - 12]; + let all = frames(cut); + assert!((1..3).contains(&all.len()), "{} frames", all.len()); + assert_eq!(all[0].image.warnings, [DecodeWarning::Truncated { format: Format::Gif }]); + assert!(all[1..].iter().all(|f| f.image.warnings.is_empty())); +} + +#[test] +fn the_first_frame_must_decode() { + assert!(decode_gif_frames(b"", &DecodeOptions::default()).is_err()); + assert!(decode_gif_frames(b"GIF89a", &DecodeOptions::default()).is_err()); + assert!(decode_gif_frames(&[0xFF; 64], &DecodeOptions::default()).is_err()); +} + +#[test] +fn every_prefix_of_a_gif_returns() { + let g = solid_gif(&[RED, GREEN, BLUE], 5, 4, 100); + for n in 0..g.len() { + let _ = decode_gif_frames(&g[..n], &DecodeOptions::default()); + } +} + +#[test] +fn dimension_limits_apply() { + let g = solid_gif(&[RED], 4, 4, 100); + let opts = DecodeOptions { limits: Limits { max_width: 3, ..Limits::default() }, ..Default::default() }; + assert!(matches!(decode_gif_frames(&g, &opts), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn the_byte_budget_covers_all_frames_together() { + // One 4x4 RGBA frame is 64 bytes: two frames fit in 128, three do not. + let limits = Limits { max_alloc: 128, ..Limits::default() }; + let opts = DecodeOptions { limits, ..Default::default() }; + assert_eq!(decode_gif_frames(&solid_gif(&[RED, GREEN], 4, 4, 100), &opts).unwrap().len(), 2); + assert!(matches!(decode_gif_frames(&solid_gif(&[RED, GREEN, BLUE], 4, 4, 100), &opts), Err(CodecError::LimitExceeded(_)))); +} diff --git a/crates/image-codecs/tests/it/heif.rs b/crates/image-codecs/tests/it/heif.rs new file mode 100644 index 000000000..c5cd36c23 --- /dev/null +++ b/crates/image-codecs/tests/it/heif.rs @@ -0,0 +1,194 @@ +//! HEIF/HEIC decoding against Apple's own encoder and decoder (features `corpus` and `heif`; +//! run with `cargo xtask test-corpus`). The fixtures are fetched into `corpus/heif/` by +//! `cargo xtask corpus --heif` (pins: `xtask/src/corpus_pins.rs`); a missing corpus fails. +//! Most come from heic-rs (`heic-rs/`, MIT OR Apache-2.0): encoded by macOS `sips` from synthetic +//! PNGs, each `.ref.png` being Apple's decode of the `.heic` next to it, the ground truth we +//! compare with. The 10-bit one is pillow-heif's (`pillow-heif/`, BSD-3-Clause), with the source +//! it was encoded from. + +#![cfg(all(feature = "corpus", feature = "heif"))] + +use crate::common; +use common::*; +use craft_image_codecs::*; +use std::sync::LazyLock; + +/// A file of `corpus/heif/`. +fn corpus(rel: &str) -> Vec { + let p = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/heif").join(rel); + std::fs::read(&p).unwrap_or_else(|e| panic!("{}: {e}: run `cargo xtask corpus --all`", p.display())) +} + +static FILES: LazyLock<[Vec; 4]> = + LazyLock::new(|| ["heic-rs/checker-64.heic", "heic-rs/rgb-strips-96.heic", "heic-rs/checker-1024.heic", "heic-rs/with-exif.heic"].map(corpus)); + +fn checker_64() -> &'static [u8] { + &FILES[0] +} +fn strips_96() -> &'static [u8] { + &FILES[1] +} +/// iPhone-style grid of 512x512 tiles (2x2). +fn checker_grid() -> &'static [u8] { + &FILES[2] +} +fn with_exif() -> &'static [u8] { + &FILES[3] +} + +fn reference(png: &[u8]) -> Image { + decode_as(Format::Png, png).unwrap().convert(ChannelLayout::Rgb, SampleType::U8) +} + +/// `bytes` with the first `irot` box set to `angle` × 90° anticlockwise (every fixture has one, at 0). +fn with_irot(bytes: &[u8], angle: u8) -> Vec { + let at = bytes.windows(4).position(|w| w == b"irot").unwrap() + 4; + let mut b = bytes.to_vec(); + b[at] = angle; + b +} + +/// `bytes` with the big-endian EXIF Orientation entry (SHORT, count 1) set to `o`. +fn with_exif_orientation(bytes: &[u8], o: u8) -> Vec { + let at = bytes.windows(8).position(|w| w == [0x01, 0x12, 0, 3, 0, 0, 0, 1]).unwrap() + 9; + let mut b = bytes.to_vec(); + b[at] = o; + b +} + +fn keep() -> DecodeOptions { + DecodeOptions { keep_orientation: true, ..Default::default() } +} + +#[test] +fn heif_is_read_only_and_listed() { + let c = caps(Format::Heif); + assert!(c.read && !c.write); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Heif)); + for ext in ["heic", "HEIC", "photo.heif", "IMG_0001.HIF"] { + assert_eq!(from_extension(ext), Some(Format::Heif), "{ext}"); + } + let img = decode(checker_64()).unwrap(); + assert!(matches!(encode(&img, Format::Heif, &EncodeOptions::default()), Err(CodecError::Unsupported { .. }))); + assert_eq!(fidelity_warnings(&img, Format::Heif), vec![FidelityWarning::WriteUnsupported { format: Format::Heif }]); +} + +#[test] +fn detect_heif_brands() { + for f in [checker_64(), strips_96(), checker_grid(), with_exif()] { + assert_eq!(detect(f), Some(Format::Heif)); + } +} + +#[test] +fn single_picture_matches_apple_decode() { + for (heic, png) in [(checker_64(), "heic-rs/checker-64.ref.png"), (strips_96(), "heic-rs/rgb-strips-96.ref.png")] { + let img = decode(heic).unwrap(); + let want = reference(&corpus(png)); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U8)); + assert_eq!(img.dimensions(), want.dimensions()); + assert!(max_abs_diff(&img, &want) <= 3.0 / 255.0, "max diff {}", max_abs_diff(&img, &want) * 255.0); + assert!(psnr(&img, &want) > 45.0); + } +} + +#[test] +fn grid_tiled_photo_matches_apple_decode() { + // iPhone photos are grids of 512x512 HEVC tiles: here 2x2. + let img = decode(checker_grid()).unwrap(); + let want = reference(&corpus("heic-rs/checker-1024.ref.png")); + assert_eq!(img.dimensions(), (1024, 1024)); + assert!(max_abs_diff(&img, &want) <= 3.0 / 255.0); +} + +#[test] +fn rgb_strips_keep_their_colours() { + // Three vertical strips, pure red, green and blue: catches swapped channels or matrices. + let img = decode(strips_96()).unwrap(); + for (x, c) in [(16, 0), (48, 1), (80, 2)] { + for ch in 0..3 { + let v = img.get(x, 16, ch); + assert!(if ch == c { v > 0.9 } else { v < 0.1 }, "strip at x={x} channel {ch} = {v}"); + } + } +} + +#[test] +fn container_rotation_is_applied_once() { + // irot 1 = 90° anticlockwise: the red strip (left) ends at the bottom, blue (right) on top. + let rotated = with_irot(strips_96(), 1); + let img = decode(&rotated).unwrap(); + assert_eq!(img.dimensions(), (32, 96)); + assert!(img.get(16, 8, 2) > 0.9 && img.get(16, 8, 0) < 0.1, "blue on top"); + assert!(img.get(16, 88, 0) > 0.9 && img.get(16, 88, 2) < 0.1, "red at the bottom"); + // keep_orientation hands back the pixels as coded. + assert_eq!(decode_with(&rotated, &keep()).unwrap().dimensions(), (96, 32)); +} + +#[test] +fn exif_and_xmp_survive_and_exif_orientation_is_not_applied_twice() { + let img = decode(with_exif()).unwrap(); + assert_eq!(img.dimensions(), (2048, 1536)); + let exif = img.meta.exif.as_deref().unwrap(); + assert!(exif.starts_with(b"MM\0*"), "TIFF-structured EXIF, without the HEIF offset header"); + assert!(img.meta.xmp.as_deref().unwrap().contains("x:xmpmeta")); + + // HEIF's EXIF Orientation repeats what irot says; the container is the authority. + let tagged = with_exif_orientation(with_exif(), 6); + let img = decode(&tagged).unwrap(); + assert_eq!(img.dimensions(), (2048, 1536), "not turned by the EXIF tag"); + assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1, "rewritten so exports don't turn it either"); + let raw = decode_with(&tagged, &keep()).unwrap(); + assert_eq!(exif_orientation(raw.meta.exif.as_deref().unwrap()), 6, "kept verbatim when asked"); +} + +#[test] +fn limits_are_checked_before_decoding() { + let tight = |limits| DecodeOptions { limits, ..Default::default() }; + let small = Limits { max_width: 512, ..Default::default() }; + assert!(matches!(decode_with(checker_grid(), &tight(small)), Err(CodecError::LimitExceeded(_)))); + let few = Limits { max_pixels: 1000, ..Default::default() }; + assert!(matches!(decode_with(checker_64(), &tight(few)), Err(CodecError::LimitExceeded(_)))); + let bytes = Limits { max_alloc: 64 * 64 * 3 - 1, ..Default::default() }; + assert!(matches!(decode_with(checker_64(), &tight(bytes)), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn unsupported_and_broken_files_are_clear_errors() { + // An ftyp and nothing else: a HEIF image sequence would have a moov here instead of a meta. + let r = decode(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"); + assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("sequence")), "{r:?}"); + assert!(decode_as(Format::Heif, b"").is_err()); + assert!(decode_as(Format::Heif, &checker_grid()[..checker_grid().len() / 2]).is_err(), "half a file"); +} + +#[test] +fn every_truncation_errors_or_decodes_never_panics() { + for bytes in [checker_64(), strips_96(), checker_grid()] { + for cut in 0..bytes.len() { + let _ = decode_as(Format::Heif, &bytes[..cut]); + } + } +} + +#[test] +fn ten_bit_with_alpha_decodes_to_16_bit_rgba() { + // pillow-heif's fixture: a 16-bit RGBA gradient (the .src.png) encoded as 10-bit HEIF by libheif/x265. + let img = decode(&corpus("pillow-heif/heif/RGBA_10__29x100.heif")).unwrap(); + let src = decode_as(Format::Png, &corpus("pillow-heif/non_heif/RGBA_16__29x100.png")).unwrap(); + assert_eq!((img.dimensions(), img.layout(), img.sample_type()), ((29, 100), ChannelLayout::Rgba, SampleType::U16)); + // Depth is kept, not 8 bits stretched: most samples are not multiples of 257. + let samples = img.to_u16_samples().unwrap(); + assert!(samples.iter().filter(|v| *v % 257 != 0).count() > samples.len() / 2); + // The alpha plane is coded on its own and comes back almost exactly. + for y in 0..100 { + for x in 0..29 { + assert!((img.get(x, y, 3) - src.get(x, y, 3)).abs() < 0.002, "alpha at {x},{y}"); + } + } + // Colour went through 4:2:0 chroma on a 29-pixel-wide saturated gradient, which costs about + // 26 dB at any depth (the 8-bit encode of the same source scores the same, as does a second + // decoder); a decoder bug (range, matrix, bit shift) lands far below. + let rgb = |i: &Image| i.convert(ChannelLayout::Rgb, SampleType::U16); + assert!(psnr(&rgb(&img), &rgb(&src)) > 24.0, "{}", psnr(&rgb(&img), &rgb(&src))); +} diff --git a/crates/image-codecs/tests/it/limits.rs b/crates/image-codecs/tests/it/limits.rs new file mode 100644 index 000000000..69e510151 --- /dev/null +++ b/crates/image-codecs/tests/it/limits.rs @@ -0,0 +1,670 @@ +//! Decompression-bomb guards. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn opts(l: Limits) -> DecodeOptions { + DecodeOptions { limits: l, ..Default::default() } +} + +fn is_limit(r: Result) -> bool { + matches!(r, Err(CodecError::LimitExceeded(_))) +} + +fn encoded(f: Format) -> Vec { + let img = synth(64, 48, ChannelLayout::Rgba, SampleType::U8, 1, 0.1); + encode(&img, f, &EncodeOptions::default()).unwrap() +} + +#[test] +fn max_pixels_enforced_for_every_format() { + for f in rw_formats() { + let b = encoded(f); + let l = Limits { max_pixels: 1000, ..Limits::default() }; + assert!(is_limit(decode_with(&b, &opts(l))), "{f:?}"); + let l = Limits { max_pixels: 64 * 48, ..Limits::default() }; + assert!(decode_with(&b, &opts(l)).is_ok(), "{f:?} exact limit should pass"); + } +} + +#[test] +fn max_width_enforced_for_every_format() { + for f in rw_formats() { + let l = Limits { max_width: 63, ..Limits::default() }; + assert!(is_limit(decode_with(&encoded(f), &opts(l))), "{f:?}"); + } +} + +#[test] +fn max_height_enforced_for_every_format() { + for f in rw_formats() { + let l = Limits { max_height: 47, ..Limits::default() }; + assert!(is_limit(decode_with(&encoded(f), &opts(l))), "{f:?}"); + } +} + +#[test] +fn max_alloc_enforced_for_every_format() { + for f in rw_formats() { + let l = Limits { max_alloc: 64 * 48, ..Limits::default() }; + assert!(decode_with(&encoded(f), &opts(l)).is_err(), "{f:?}"); + } +} + +#[test] +fn limits_none_accepts() { + for f in rw_formats() { + assert!(decode_with(&encoded(f), &opts(Limits::none())).is_ok(), "{f:?}"); + } +} + +fn png_chunk(ty: &[u8; 4], data: &[u8]) -> Vec { + let mut v = (data.len() as u32).to_be_bytes().to_vec(); + v.extend_from_slice(ty); + v.extend_from_slice(data); + let mut crc_in = ty.to_vec(); + crc_in.extend_from_slice(data); + v.extend_from_slice(&crc32(&crc_in).to_be_bytes()); + v +} + +fn crc32(d: &[u8]) -> u32 { + let mut c = 0xFFFF_FFFFu32; + for &b in d { + c ^= b as u32; + for _ in 0..8 { + c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 }; + } + } + !c +} + +#[test] +fn png_bomb_header_rejected_before_alloc() { + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&100_000u32.to_be_bytes()); + ihdr.extend_from_slice(&100_000u32.to_be_bytes()); + ihdr.extend_from_slice(&[16, 6, 0, 0, 0]); + let mut b = b"\x89PNG\r\n\x1a\n".to_vec(); + b.extend(png_chunk(b"IHDR", &ihdr)); + b.extend(png_chunk(b"IDAT", &[0x78, 0x9C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01])); + b.extend(png_chunk(b"IEND", &[])); + assert!(is_limit(decode(&b))); +} + +#[test] +fn png_header_at_the_default_limit_needs_a_plausible_file_size() { + // 32768 x 32768 x RGBA16 is exactly the default max_alloc: it passes the dimension limits, + // but a ~100 byte file cannot hold it (zlib tops out near 1032:1), so it is refused before + // the 8 GiB output buffer is allocated. + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&32768u32.to_be_bytes()); + ihdr.extend_from_slice(&32768u32.to_be_bytes()); + ihdr.extend_from_slice(&[16, 6, 0, 0, 0]); + let mut b = b"\x89PNG\r\n\x1a\n".to_vec(); + b.extend(png_chunk(b"IHDR", &ihdr)); + b.extend(png_chunk(b"IDAT", &[0x78, 0x9C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01])); + b.extend(png_chunk(b"IEND", &[])); + assert!(is_limit(decode(&b))); +} + +#[test] +fn png_highly_compressible_image_still_decodes() { + let img = Image::from_u8(3000, 3000, ChannelLayout::Rgba, vec![255u8; 3000 * 3000 * 4]).unwrap(); + let b = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(b.len() < 100_000, "{}", b.len()); + assert_eq!(decode(&b).unwrap().width(), 3000); +} + +const PNG_TEXT_BUDGET: usize = 64 << 10; + +fn png_text_decode(bytes: &[u8], max_alloc: usize) -> Result { + decode_with(bytes, &opts(Limits { max_alloc: max_alloc as u64, ..Limits::default() })) +} + +fn zlib(text: &[u8]) -> Vec { + use std::io::Write; + let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + encoder.write_all(text).unwrap(); + encoder.finish().unwrap() +} + +// zTXt and iTXt payloads supplied here are already compressed. +fn png_text_stream(ty: &[u8; 4], keyword: &[u8], payload: &[u8]) -> Vec { + let mut data = keyword.to_vec(); + data.push(0); + match ty { + b"tEXt" => {} + b"zTXt" => data.push(0), + b"iTXt" => data.extend_from_slice(&[1, 0, 0, 0]), + _ => panic!("unexpected text chunk"), + } + data.extend_from_slice(payload); + png_chunk(ty, &data) +} + +fn png_text(ty: &[u8; 4], keyword: &[u8], text: &[u8]) -> Vec { + if ty == b"tEXt" { png_text_stream(ty, keyword, text) } else { png_text_stream(ty, keyword, &zlib(text)) } +} + +fn png_with_text(before_idat: &[Vec], after_idat: &[Vec]) -> Vec { + let mut image = Vec::new(); + { + let mut encoder = png::Encoder::new(&mut image, 1, 1); + encoder.set_color(png::ColorType::Grayscale); + encoder.set_depth(png::BitDepth::Eight); + let mut writer = encoder.write_header().unwrap(); + writer.write_image_data(&[127]).unwrap(); + writer.finish().unwrap(); + } + let mut at = 8; + let mut idat = None; + let iend = loop { + let len = u32::from_be_bytes(image[at..at + 4].try_into().unwrap()) as usize; + let ty = &image[at + 4..at + 8]; + if ty == b"IDAT" && idat.is_none() { + idat = Some(at); + } + if ty == b"IEND" { + break at; + } + at += len + 12; + }; + let idat = idat.unwrap(); + let mut output = image[..idat].to_vec(); + for chunk in before_idat { + output.extend_from_slice(chunk); + } + output.extend_from_slice(&image[idat..iend]); + for chunk in after_idat { + output.extend_from_slice(chunk); + } + output.extend_from_slice(&image[iend..]); + output +} + +#[test] +fn png_compressed_text_bombs_obey_limits_before_and_after_idat() { + let bomb = vec![b'A'; PNG_TEXT_BUDGET * 8]; + for ty in [b"zTXt", b"iTXt"] { + for after_idat in [false, true] { + let chunks = [png_text(ty, b"Comment", &bomb)]; + let bytes = if after_idat { png_with_text(&[], &chunks) } else { png_with_text(&chunks, &[]) }; + assert!(bytes.len() < PNG_TEXT_BUDGET / 2, "fixture must fit comfortably within the compressed-input budget"); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET)), "{ty:?}, after IDAT={after_idat}"); + } + } +} + +#[test] +fn png_text_chunks_share_an_aggregate_budget() { + for text_len in [(PNG_TEXT_BUDGET - 3) / 3, PNG_TEXT_BUDGET / 3] { + let text = vec![b'A'; text_len]; + let before = [png_text(b"tEXt", b"A", &text), png_text(b"zTXt", b"B", &text)]; + let after = [png_text(b"iTXt", b"C", &text)]; + let result = png_text_decode(&png_with_text(&before, &after), PNG_TEXT_BUDGET); + if 3 * (text_len + 1) <= PNG_TEXT_BUDGET { + let image = result.unwrap(); + assert_eq!(image.meta.text.len(), 3); + assert!(image.meta.text.iter().all(|(_, value)| value.len() == text_len)); + } else { + assert!(is_limit(result), "chunks fitting individually must still share a budget"); + } + } +} + +#[test] +fn png_text_accepts_the_exact_utf8_budget() { + for ty in [b"tEXt", b"zTXt", b"iTXt"] { + let text = vec![b'A'; PNG_TEXT_BUDGET - 1]; + let bytes = png_with_text(&[png_text(ty, b"K", &text)], &[]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("K".into(), String::from_utf8(text).unwrap())]); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - 1)), "{ty:?}: keyword and text both count"); + } +} + +#[test] +fn png_plain_itxt_obeys_the_exact_budget_and_validates_fields() { + let mut payload = b"K\0\0\0en\0Keyword\0".to_vec(); + payload.extend_from_slice(&vec![b'A'; PNG_TEXT_BUDGET - 1]); + let bytes = png_with_text(&[], &[png_chunk(b"iTXt", &payload)]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("K".into(), "A".repeat(PNG_TEXT_BUDGET - 1))]); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - 1))); + + let invalid_fields: &[&[u8]] = &[ + b"K\0\0\0\0\0\xFF", // Invalid text UTF-8. + b"K\0\0\0\xFF\0\0text", // Non-ASCII language tag. + b"K\0\0\0\0\xFF\0text", // Invalid translated-keyword UTF-8. + b"K\0\x02\0\0\0text", // Unknown compression flag. + b"K\0\x01\x01\0\0text", // Unknown compression method for compressed text. + b"K\0\0", // Missing compression method. + b"K\0\0\0\0text", // Missing translated-keyword separator. + ]; + for fields in invalid_fields { + let bytes = png_with_text(&[png_chunk(b"iTXt", fields)], &[png_text(b"tEXt", b"Good", b"retained")]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("Good".into(), "retained".into())]); + } +} + +#[test] +fn png_latin1_expansion_counts_keyword_and_text_utf8_bytes() { + let text_len = (PNG_TEXT_BUDGET - 2) / 2; + for ty in [b"tEXt", b"zTXt", b"iTXt"] { + let text = if ty == b"iTXt" { "é".repeat(text_len).into_bytes() } else { vec![0xE9; text_len] }; + let bytes = png_with_text(&[], &[png_text(ty, &[0xE9], &text)]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("é".into(), "é".repeat(text_len))]); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - 1)), "{ty:?}: Latin-1 expands when stored as UTF-8"); + } +} + +#[test] +fn png_duplicate_xmp_chunks_each_consume_budget() { + const KEYWORD: &[u8] = b"XML:com.adobe.xmp"; + let text_len = (PNG_TEXT_BUDGET - 2 * KEYWORD.len()) / 2; + for extra in [0, 1] { + let first = png_text(b"iTXt", KEYWORD, &vec![b'A'; text_len + extra]); + let last_text = vec![b'B'; text_len]; + let last = png_text(b"iTXt", KEYWORD, &last_text); + let result = png_text_decode(&png_with_text(&[first], &[last]), PNG_TEXT_BUDGET); + if extra == 0 { + let image = result.unwrap(); + assert_eq!(image.meta.xmp, Some(String::from_utf8(last_text).unwrap())); + assert!(image.meta.text.is_empty()); + } else { + assert!(is_limit(result), "replacing an XMP packet must not reset its budget charge"); + } + } +} + +#[test] +fn png_xmp_in_all_text_chunk_types_is_preserved() { + const XMP: &str = ""; + for ty in [b"tEXt", b"zTXt", b"iTXt"] { + for after_idat in [false, true] { + let chunks = [png_text(ty, b"XML:com.adobe.xmp", XMP.as_bytes())]; + let bytes = if after_idat { png_with_text(&[], &chunks) } else { png_with_text(&chunks, &[]) }; + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.xmp.as_deref(), Some(XMP), "{ty:?}, after IDAT: {after_idat}"); + assert!(image.meta.text.is_empty(), "XMP must not also be retained as ordinary text"); + let saved = encode(&image, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&saved).unwrap().meta.xmp, image.meta.xmp); + } + } +} + +#[test] +fn png_mixed_xmp_chunk_types_use_last_packet_and_share_budget() { + for first_ty in [b"tEXt", b"zTXt", b"iTXt"] { + for last_ty in [b"tEXt", b"zTXt", b"iTXt"] { + let first = png_text(first_ty, b"XML:com.adobe.xmp", b"first"); + let last = png_text(last_ty, b"XML:com.adobe.xmp", b"last"); + let bytes = png_with_text(&[first], &[last]); + let budget = 2 * b"XML:com.adobe.xmp".len() + b"first".len() + b"last".len(); + // Pixel decoding needs a larger limit, so check the shared budget with larger packets. + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.xmp.as_deref(), Some("last")); + assert!(image.meta.text.is_empty()); + let first = png_text(first_ty, b"XML:com.adobe.xmp", &vec![b'A'; PNG_TEXT_BUDGET - budget]); + let last = png_text(last_ty, b"XML:com.adobe.xmp", b"last"); + let bytes = png_with_text(&[first], &[last]); + assert!(png_text_decode(&bytes, PNG_TEXT_BUDGET - b"first".len()).is_ok()); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - b"first".len() - 1))); + } + } +} + +#[test] +fn png_corrupt_text_is_still_skipped() { + let valid = zlib(b"discard"); + let mut bad_header = valid.clone(); + bad_header[0] = 0; + let truncated = valid[..valid.len() - 4].to_vec(); + for ty in [b"zTXt", b"iTXt"] { + for stream in [&bad_header, &truncated] { + let before = [png_text_stream(ty, b"Bad", stream)]; + let after = [png_text(b"tEXt", b"Good", b"retained")]; + let image = png_text_decode(&png_with_text(&before, &after), PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("Good".into(), "retained".into())]); + } + } + let invalid_utf8 = png_text(b"iTXt", b"Bad", &[0xFF]); + let image = png_text_decode(&png_with_text(&[], &[invalid_utf8]), PNG_TEXT_BUDGET).unwrap(); + assert!(image.meta.text.is_empty()); +} + +#[test] +fn png_text_with_bad_crc_and_text_after_iend_are_ignored() { + let bomb = vec![b'A'; PNG_TEXT_BUDGET * 8]; + for ty in [b"zTXt", b"iTXt"] { + let valid = png_text(ty, b"Comment", &bomb); + let mut bad_crc = valid.clone(); + *bad_crc.last_mut().unwrap() ^= 1; + for after_idat in [false, true] { + let bytes = if after_idat { png_with_text(&[], &[bad_crc.clone()]) } else { png_with_text(&[bad_crc.clone()], &[]) }; + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert!(image.meta.text.is_empty()); + } + let mut bytes = png_with_text(&[], &[]); + bytes.extend_from_slice(&valid); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert!(image.meta.text.is_empty()); + } +} + +#[test] +fn png_malformed_chunk_tails_remain_tolerated_after_pixels() { + let base = png_with_text(&[png_text(b"tEXt", b"Good", b"retained")], &[png_chunk(b"vpAg", &[])]); + let iend = base.len() - 12; + let tails: &[&[u8]] = &[&[], b"\x00\x00\x00", b"\x00\x00\x00\x10iTXtK\0\0", b"\xFF\xFF\xFF\xFFiTXt"]; + for tail in tails { + let mut bytes = base[..iend].to_vec(); + bytes.extend_from_slice(tail); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.dimensions(), (1, 1)); + assert_eq!(image.meta.text, vec![("Good".into(), "retained".into())]); + } +} + +#[test] +fn png_invalid_optional_text_does_not_spend_an_exhausted_budget() { + let full = png_text(b"tEXt", b"K", &vec![b'A'; PNG_TEXT_BUDGET - 1]); + let invalid = [png_chunk(b"iTXt", b"Bad\0\0\0\0\0\xFF"), png_text_stream(b"zTXt", b"Bad", b"invalid")]; + let image = png_text_decode(&png_with_text(&[full], &invalid), PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text.len(), 1); + assert_eq!(image.meta.text[0].1.len(), PNG_TEXT_BUDGET - 1); +} + +#[test] +fn pnm_bomb_header_rejected() { + assert!(is_limit(decode(b"P6\n200000 200000\n255\n\0\0\0"))); + assert!(is_limit(decode(b"P7\nWIDTH 99999\nHEIGHT 99999\nDEPTH 4\nMAXVAL 65535\nTUPLTYPE RGB_ALPHA\nENDHDR\n"))); + assert!(is_limit(decode(b"PF\n100000 100000\n-1.0\n"))); +} + +#[test] +fn pnm_header_alone_reserves_nothing_without_limits() { + // #1110: ASCII PGM/PPM reserved four bytes per declared sample before reading any. A + // 2³¹ × 2³¹ header asked for more than isize::MAX ("capacity overflow"), and PBM reserved + // 2⁶² bytes before finding the raster missing (an abort). + let none = opts(Limits::none()); + let side = 1u32 << 31; + for f in [format!("P1\n{side} {side}\n"), format!("P2\n{side} {side}\n255\n"), format!("P3\n{side} {side}\n255\n"), format!("P4\n{side} {side}\n")] { + assert!(decode_with(f.as_bytes(), &none).is_err(), "{f:?}"); + } +} + +#[test] +fn hdr_header_sizes_that_overflow_u64_are_errors_without_limits() { + let b = b"#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y 2147483648 +X 2147483648\n"; + assert!(decode_with(b, &opts(Limits::none())).is_err()); +} + +#[test] +fn pnm_header_sizes_that_overflow_usize_are_errors_without_limits() { + let m = u32::MAX; + let none = opts(Limits::none()); + for f in [ + format!("P3\n{m} {m}\n255\n"), + format!("P6\n{m} {m}\n255\n"), + format!("P5\n{m} {m}\n65535\n"), + format!("PF\n{m} {m}\n-1.0\n"), + format!("P7\nWIDTH {m}\nHEIGHT {m}\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n"), + ] { + assert!(decode_with(f.as_bytes(), &none).is_err(), "{f:?}"); + } +} + +#[test] +fn ascii_pnm_with_the_tightest_layout_still_decodes() { + // The capacity bound for #1110 must not cut off files with no slack: PBM digits need no + // separator, and the last ASCII sample needs no trailing whitespace. + assert_eq!(decode(b"P1\n3 1\n010").unwrap().data(), &[255, 0, 255]); + assert_eq!(decode(b"P2\n3 1\n255\n1 2 3").unwrap().data(), &[1, 2, 3]); + assert_eq!(decode(b"P3\n1 1\n255\n9 8 7").unwrap().data(), &[9, 8, 7]); +} + +#[test] +fn jpeg_bomb_header_rejected() { + // SOI + SOF0 claiming 65535x65535x3 with no scan data. + let b = [0xFF, 0xD8, 0xFF, 0xC0, 0x00, 0x11, 8, 0xFF, 0xFF, 0xFF, 0xFF, 3, 1, 0x11, 0, 2, 0x11, 0, 3, 0x11, 0, 0xFF, 0xD9]; + let l = Limits { max_pixels: 1 << 24, ..Limits::default() }; + assert!(is_limit(decode_with(&b, &opts(l)))); +} + +#[test] +fn tiff_bomb_header_rejected() { + // Minimal little-endian TIFF claiming 1_000_000 x 1_000_000 8-bit gray. + let mut b = b"II*\0\x08\0\0\0".to_vec(); + let entries: [(u16, u16, u32, u32); 6] = [(256, 4, 1, 1_000_000), (257, 4, 1, 1_000_000), (258, 3, 1, 8), (262, 3, 1, 1), (273, 4, 1, 0), (279, 4, 1, 0)]; + b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, ty, count, val) in entries { + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + b.extend_from_slice(&count.to_le_bytes()); + b.extend_from_slice(&val.to_le_bytes()); + } + b.extend_from_slice(&0u32.to_le_bytes()); + assert!(is_limit(decode(&b))); +} + +#[test] +fn limits_check_api() { + let l = Limits::default(); + assert!(l.check(100, 100, ChannelLayout::Rgba, SampleType::F32).is_ok()); + assert!(matches!(l.check(0, 1, ChannelLayout::Gray, SampleType::U8), Err(CodecError::InvalidImage(_)))); + assert!(matches!(l.check(u32::MAX, 1, ChannelLayout::Gray, SampleType::U8), Err(CodecError::LimitExceeded(_)))); + let l = Limits { max_alloc: 399, ..Limits::default() }; + assert!(l.check(10, 10, ChannelLayout::Rgba, SampleType::U8).is_err()); + assert!(l.check(10, 10, ChannelLayout::Rgb, SampleType::U8).is_ok()); +} + +/// #8597: a small file whose header declares about 3 GB, inside the default limits, is refused +/// by the size-against-file check before the decoder allocates its output. +fn refused_as_implausible(f: Format, b: &[u8]) { + match decode(b) { + Err(CodecError::LimitExceeded(m)) => assert!(m.contains("cannot hold"), "{f:?}: {m}"), + other => panic!("{f:?}: expected an implausible-size refusal, got {:?}", other.map(|i| (i.width(), i.height()))), + } +} + +fn find(b: &[u8], needle: &[u8]) -> usize { + b.windows(needle.len()).position(|w| w == needle).unwrap() +} + +#[test] +fn jpeg_header_declaring_gigabytes_needs_a_plausible_file_size() { + let mut b = encoded(Format::Jpeg); + // SOF0: marker, length, precision, then height and width (big-endian). + let sof = find(&b, &[0xFF, 0xC0]); + b[sof + 5..sof + 7].copy_from_slice(&16000u16.to_be_bytes()); + b[sof + 7..sof + 9].copy_from_slice(&65000u16.to_be_bytes()); + // The decoder's own scan-size guard refuses it (one refusal path for truncated and complete + // files alike), before the backend allocates the frame. + match decode(&b) { + Err(e) => assert!(e.to_string().contains("too short for the frame"), "{e}"), + Ok(i) => panic!("decoded a {}×{} frame from {} bytes", i.width(), i.height(), b.len()), + } +} + +#[test] +fn image_crate_headers_declaring_gigabytes_need_a_plausible_file_size() { + let side = 30000u32; + let mut bmp = encoded(Format::Bmp); + bmp[18..22].copy_from_slice(&(side as i32).to_le_bytes()); + bmp[22..26].copy_from_slice(&(side as i32).to_le_bytes()); + refused_as_implausible(Format::Bmp, &bmp); + let mut qoi = encoded(Format::Qoi); + // The qoi crate refuses more than 400 M pixels itself, so 20000 x 20000 (1.6 GB). + qoi[4..8].copy_from_slice(&20000u32.to_be_bytes()); + qoi[8..12].copy_from_slice(&20000u32.to_be_bytes()); + refused_as_implausible(Format::Qoi, &qoi); + let mut gif = encoded(Format::Gif); + gif[6..8].copy_from_slice(&(side as u16).to_le_bytes()); + gif[8..10].copy_from_slice(&(side as u16).to_le_bytes()); + refused_as_implausible(Format::Gif, &gif); + // The frame too. Descriptor: separator, left, top, width, height. + let frame = find(&gif, &[0x2C, 0, 0, 0, 0, 64, 0, 48, 0]); + gif[frame + 5..frame + 7].copy_from_slice(&(side as u16).to_le_bytes()); + gif[frame + 7..frame + 9].copy_from_slice(&(side as u16).to_le_bytes()); + refused_as_implausible(Format::Gif, &gif); +} + +/// GIF89a with a two-colour global table and one frame at the top left holding no pixels or +/// one pixel (LZW minimum code size 2: clear, index 0, end of information). +fn tiny_gif(screen: (u16, u16), frame: (u16, u16)) -> Vec { + let mut b = b"GIF89a".to_vec(); + for v in [screen.0, screen.1] { + b.extend_from_slice(&v.to_le_bytes()); + } + b.extend_from_slice(&[0x80, 0, 0, 0, 0, 0, 255, 255, 255, 0x2C, 0, 0, 0, 0]); + for v in [frame.0, frame.1] { + b.extend_from_slice(&v.to_le_bytes()); + } + b.extend_from_slice(&[0, 2, 2, 0x44, 0x01, 0, 0x3B]); + b +} + +#[test] +fn a_gif_frame_much_smaller_than_its_screen_still_decodes() { + let img = decode(&tiny_gif((4000, 4000), (1, 1))).unwrap(); + assert_eq!((img.width(), img.height()), (4000, 4000)); +} + +#[test] +fn a_tiny_gif_frame_does_not_admit_a_gigabyte_screen() { + refused_as_implausible(Format::Gif, &tiny_gif((30000, 30000), (1, 1))); + // 324 MB of screen: past the 256 MiB that needs no data. + refused_as_implausible(Format::Gif, &tiny_gif((9000, 9000), (1, 1))); + refused_as_implausible(Format::Gif, &tiny_gif((30000, 30000), (0, 0))); + // A frame bigger than its screen is decoded at its own size. + refused_as_implausible(Format::Gif, &tiny_gif((100, 100), (40000, 40000))); +} + +#[test] +fn an_rle_bmp_ending_early_still_decodes() { + // 4000x4000 RLE8 whose only data is the end-of-bitmap escape: every pixel is left at index 0. + let data = 14 + 40 + 1024; + let mut b = b"BM".to_vec(); + for v in [data + 2, 0, data, 40, 4000, 4000] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.extend_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&8u16.to_le_bytes()); + for v in [1, 2, 0, 0, 0, 0] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.resize(data, 0); + b.extend_from_slice(&[0, 1]); + let img = decode(&b).unwrap(); + assert_eq!((img.width(), img.height()), (4000, 4000)); +} + +#[test] +fn an_rle_bmp_canvas_past_the_unbacked_allowance_needs_data() { + // 30000x30000 RLE8 with only the end-of-bitmap escape: 2.7 GB of RGB from about 1 KB. + let data = 14 + 40 + 1024; + let mut b = b"BM".to_vec(); + for v in [data + 2, 0, data, 40, 30000, 30000] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.extend_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&8u16.to_le_bytes()); + for v in [1, 2, 0, 0, 0, 0] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.resize(data, 0); + b.extend_from_slice(&[0, 1]); + refused_as_implausible(Format::Bmp, &b); +} + +#[test] +fn animated_gif_frames_need_a_plausible_file_size_too() { + let frames = |b: &[u8]| decode_gif_frames(b, &DecodeOptions::default()); + assert_eq!(frames(&tiny_gif((4000, 4000), (1, 1))).unwrap().len(), 1); + for gif in [tiny_gif((30000, 30000), (1, 1)), tiny_gif((100, 100), (40000, 40000))] { + match frames(&gif) { + Err(CodecError::LimitExceeded(m)) => assert!(m.contains("cannot hold"), "{m}"), + other => panic!("expected an implausible-size refusal, got {:?}", other.map(|f| f.len())), + } + } +} + +#[test] +fn pcx_header_declaring_gigabytes_needs_a_plausible_file_size() { + // 24-bit (three 8-bit planes) 65535 x 16384, with one row of data. + let mut b = vec![0u8; 128]; + b[0] = 0x0a; + b[1] = 5; + b[2] = 1; + b[3] = 8; + b[8..10].copy_from_slice(&65534u16.to_le_bytes()); + b[10..12].copy_from_slice(&16383u16.to_le_bytes()); + b[65] = 3; + b[66..68].copy_from_slice(&65535u16.to_le_bytes()); + b[68..70].copy_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&[0xff, 0]); + // PCX's own minimum-packet check refuses it before the output is allocated. + match decode(&b) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains("too short"), "{message}"), + other => panic!("expected a too-short refusal, got {:?}", other.map(|i| (i.width(), i.height()))), + } +} + +#[test] +fn xpm_rows_shorter_than_the_header_declares_are_refused_before_allocating() { + let b = b"/* XPM */\nstatic char *x[] = {\n\"30000 30000 1 1\",\n\". c #000000\",\n\".\",\n};\n".to_vec(); + match decode(&b) { + Err(CodecError::Malformed { .. }) => {} + other => panic!("expected a malformed-row error, got {:?}", other.map(|i| (i.width(), i.height()))), + } +} + +#[test] +fn exr_header_declaring_gigabytes_needs_a_plausible_file_size() { + let mut b = encoded(Format::OpenExr); + // dataWindow: name, type, size, then xMin, yMin, xMax, yMax (little-endian i32). + let at = find(&b, b"dataWindow\0box2i\0") + b"dataWindow\0box2i\0".len() + 4; + b[at + 8..at + 12].copy_from_slice(&15999i32.to_le_bytes()); + b[at + 12..at + 16].copy_from_slice(&15999i32.to_le_bytes()); + refused_as_implausible(Format::OpenExr, &b); +} + +#[test] +fn a_dwab_exr_gets_a_looser_file_size_bound() { + // DWA passes 2^16:1 (a flat 8K frame with 12 alpha-scheme mattes is about 30 KB), so a DWAB + // part may declare 4 times more than a ZIP part from the same file, and no more. + let img = synth(1, 1, ChannelLayout::Rgba, SampleType::U8, 1, 0.1); + let mut b = encode(&img, Format::OpenExr, &EncodeOptions::default()).unwrap(); + let at = find(&b, b"dataWindow\0box2i\0") + b"dataWindow\0box2i\0".len() + 4; + let c = find(&b, b"compression\0compression\0") + b"compression\0compression\0".len() + 4; + // RGBA as f32: 16 bytes a pixel against a file of a few hundred bytes. + let size = |b: &mut Vec, side: i32| { + b[at + 8..at + 12].copy_from_slice(&(side - 1).to_le_bytes()); + b[at + 12..at + 16].copy_from_slice(&(side - 1).to_le_bytes()); + }; + let refused = |b: &[u8]| matches!(exr_info(b, &Limits::default()), Err(CodecError::LimitExceeded(m)) if m.contains("cannot hold")); + size(&mut b, 2048); + b[c] = 3; // ZIP + assert!(refused(&b), "{} bytes", b.len()); + for dwa in [8, 9] { + b[c] = dwa; // DWAA, DWAB + let parts = exr_info(&b, &Limits::default()).unwrap(); + assert_eq!((parts[0].width, parts[0].height), (2048, 2048)); + } + size(&mut b, 4096); + assert!(refused(&b), "{} bytes", b.len()); +} + +#[test] +fn highly_compressible_jpeg_and_gif_still_decode() { + let img = Image::from_u8(3000, 3000, ChannelLayout::Rgba, vec![255u8; 3000 * 3000 * 4]).unwrap(); + for f in [Format::Jpeg, Format::Gif, Format::Qoi, Format::OpenExr] { + let b = encode(&img, f, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&b).unwrap().width(), 3000, "{f:?}"); + } +} diff --git a/crates/image-codecs/tests/it/main.rs b/crates/image-codecs/tests/it/main.rs new file mode 100644 index 000000000..f465a0cbb --- /dev/null +++ b/crates/image-codecs/tests/it/main.rs @@ -0,0 +1,39 @@ +//! The crate's integration tests, linked into one test binary. + +mod camera_raw; +mod common; +mod dds; +mod dds_write; +mod decode_warnings; +mod deep_exr; +mod detect_caps; +mod dpx; +mod exr_cryptomatte; +mod exr_multipart; +mod exr_stream; +mod fidelity; +mod gif_frames; +mod heif; +mod limits; +mod malformed; +mod metadata; +mod orientation; +mod pcx; +mod png_jpeg; +mod pnm_headers; +mod resolution; +mod roundtrip; +mod source_settings; +mod tga; +mod tiff_bands; +mod tiff_bench; +mod tiff_exif; +mod tiff_lab; +mod tiff_pages; +mod tiff_photoshop; +mod tiff_pyramid; +mod tiff_regressions; +mod tiff_ycbcr; +mod webp_frames; +mod webp_lossy; +mod xpm; diff --git a/crates/image-codecs/tests/it/malformed.rs b/crates/image-codecs/tests/it/malformed.rs new file mode 100644 index 000000000..ec06109e0 --- /dev/null +++ b/crates/image-codecs/tests/it/malformed.rs @@ -0,0 +1,270 @@ +//! Malformed input must produce errors, never panics or huge allocations. + +use crate::common; +use common::*; +use craft_image_codecs::*; +use proptest::prelude::*; + +fn tight() -> DecodeOptions { + DecodeOptions { limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 64 << 20 }, ..Default::default() } +} + +fn samples() -> Vec<(Format, Vec)> { + let mut v = Vec::new(); + for f in rw_formats() { + for (l, s) in [(ChannelLayout::Rgba, SampleType::U8), (ChannelLayout::Gray, SampleType::U16), (ChannelLayout::Rgb, SampleType::F32)] { + let mut img = synth(19, 13, l, s, 11, 0.3); + img.icc = Some(sample_icc(300)); + img.meta.exif = Some(sample_exif()); + img.meta.xmp = Some(SAMPLE_XMP.into()); + v.push((f, encode(&img, f, &EncodeOptions::default()).unwrap())); + } + } + // Read-only formats: Apple-encoded HEIC from `corpus/heif` (`cargo xtask corpus --heif`), a + // single picture and a grid of tiles. + #[cfg(all(feature = "corpus", feature = "heif"))] + for name in ["rgb-strips-96.heic", "checker-1024.heic"] { + let p = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/heif/heic-rs").join(name); + let bytes = std::fs::read(&p).unwrap_or_else(|e| panic!("{}: {e}: run `cargo xtask corpus --all`", p.display())); + v.push((Format::Heif, bytes)); + } + // Extra variants: interlaced PNG, ASCII PNM, CMYK JPEG. + let img = synth(19, 13, ChannelLayout::Rgb, SampleType::U8, 1, 0.3); + v.push((Format::Png, encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap())); + v.push((Format::Pnm, b"P3\n2 2\n255\n0 1 2 3 4 5 6 7 8 9 10 11\n".to_vec())); + v.push((Format::Jpeg, encode(&synth(19, 13, ChannelLayout::Cmyk, SampleType::U8, 1, 0.3), Format::Jpeg, &EncodeOptions::default()).unwrap())); + v +} + +fn no_panic(f: Format, bytes: &[u8]) { + let _ = decode_with(bytes, &tight()); + let _ = decode_as_with(f, bytes, &tight()); +} + +#[test] +fn truncated_at_many_offsets() { + for (f, bytes) in samples() { + let n = bytes.len(); + let step = (n / 97).max(1); + let mut cuts: Vec = (0..n.min(80)).collect(); + cuts.extend((0..n).step_by(step)); + cuts.extend(n.saturating_sub(40)..n); + for cut in cuts { + no_panic(f, &bytes[..cut]); + } + } +} + +#[test] +fn truncated_files_mostly_error() { + // Chopping off the second half must not silently succeed for formats + // with length-checked rasters. + for (f, bytes) in samples() { + if matches!(f, Format::Png | Format::Pnm | Format::Fits | Format::Dpx | Format::Qoi | Format::OpenExr | Format::Tiff | Format::Bmp) { + let r = decode_as_with(f, &bytes[..bytes.len() / 2], &tight()); + assert!(r.is_err(), "{f:?} decoded half a file"); + } + // JPEG decodes leniently (the missing part grey) but never silently (#518). + if f == Format::Jpeg { + match decode_as_with(f, &bytes[..bytes.len() / 2], &tight()) { + Ok(img) => assert_eq!(img.warnings, [DecodeWarning::Truncated { format: f }]), + Err(e) => assert!(e.to_string().contains("before any image data"), "{e}"), + } + } + } +} + +#[test] +fn bit_flips_never_panic() { + let mut rng = common::Rng::new(42); + for (f, bytes) in samples() { + for _ in 0..150 { + let mut b = bytes.clone(); + let flips = 1 + (rng.next_u64() % 4) as usize; + for _ in 0..flips { + let i = (rng.next_u64() as usize) % b.len(); + b[i] ^= 1 << (rng.next_u64() % 8); + } + no_panic(f, &b); + } + } +} + +#[test] +fn byte_overwrites_in_header_never_panic() { + for (f, bytes) in samples() { + for i in 0..bytes.len().min(64) { + for v in [0x00, 0x7F, 0xFF] { + let mut b = bytes.clone(); + b[i] = v; + no_panic(f, &b); + } + } + } +} + +#[test] +fn empty_input_errors_for_every_format() { + for f in Format::ALL { + assert!(decode_as(f, &[]).is_err(), "{f:?}"); + } +} + +#[test] +fn pnm_specific_malformations() { + let cases: &[&[u8]] = &[ + b"P6\n", + b"P6\n2 2\n", + b"P6\n2 2\n255", + b"P6\n2 2\n0\n", + b"P6\n2 2\n70000\n", + b"P6\n-2 2\n255\n", + b"P6\n2 2\n255\n\x01\x02", + b"P5\n2 2\n65535\n\x00\x00\x00", + b"P2\n2 2\n10\n1 2 3 11\n", + b"P1\n2 2\n0 1 2 0\n", + b"P4\n9 2\n\x00", + b"P7\nWIDTH 2\nHEIGHT 2\nDEPTH 9\nMAXVAL 255\nENDHDR\n", + b"P7\nWIDTH 2\nHEIGHT 2\nMAXVAL 255\nENDHDR\n", + b"P7\nWIDTH 2\nBOGUS 3\nENDHDR\n", + b"PF\n2 2\n0.0\n", + b"PF\n2 2\nnan\n", + b"PF\n2 2\n-1.0\n\0\0\0\0", + b"P6 4294967295 4294967295 255\n", + ]; + for c in cases { + assert!(decode(c).is_err(), "{:?}", String::from_utf8_lossy(c)); + } +} + +#[test] +fn pnm_valid_variants() { + let img = decode(b"P1\n# comment\n3 2\n1 0 1\n0 1 0\n").unwrap(); + assert_eq!(img.data(), &[0, 255, 0, 255, 0, 255]); + let img = decode(b"P1\n3 2\n101010").unwrap(); + assert_eq!(img.data(), &[0, 255, 0, 255, 0, 255]); + let img = decode(b"P4\n3 2\n\xA0\x40").unwrap(); + assert_eq!(img.data(), &[0, 255, 0, 255, 0, 255]); + let img = decode(b"P2\n2 1\n15\n0 15\n").unwrap(); + assert_eq!(img.data(), &[0, 255]); + let img = decode(b"P2\n2 1\n1023\n0 1023\n").unwrap(); + assert_eq!(img.sample_type(), SampleType::U16); + assert_eq!(img.to_u16_samples().unwrap(), vec![0, 65535]); + let img = decode(b"P3\n1 1\n255\n1 2 3\n").unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[1u8, 2, 3][..])); + let img = decode(b"P7\nWIDTH 1\nHEIGHT 1\nDEPTH 2\nMAXVAL 255\nTUPLTYPE GRAYSCALE_ALPHA\nENDHDR\n\x10\x20").unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::GrayA, &[0x10u8, 0x20][..])); + let mut pfm = b"PF\n1 2\n1.0\n".to_vec(); // big-endian, bottom-up + for v in [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0] { + pfm.extend_from_slice(&v.to_be_bytes()); + } + let img = decode(&pfm).unwrap(); + assert_eq!(img.to_f32_samples().unwrap(), vec![4.0, 5.0, 6.0, 1.0, 2.0, 3.0]); +} + +#[test] +fn jpeg_marker_garbage() { + let cases: &[&[u8]] = &[ + &[0xFF, 0xD8, 0xFF], + &[0xFF, 0xD8, 0xFF, 0xE0, 0x00], + &[0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x01], + &[0xFF, 0xD8, 0xFF, 0xE2, 0x00, 0x10, b'I', b'C', b'C', b'_', b'P', b'R', b'O', b'F', b'I', b'L', b'E', 0, 1, 1], + &[0xFF, 0xD8, 0xFF, 0xC0, 0x00, 0x0B, 8, 0xFF, 0xFF, 0xFF, 0xFF, 3, 0, 0, 0], + &[0xFF, 0xD8, 0xFF, 0xD9], + ]; + for c in cases { + assert!(decode(c).is_err()); + } +} + +proptest! { + #![proptest_config(ProptestConfig { cases: 400, .. ProptestConfig::default() })] + + #[test] + fn random_bytes_never_panic(data in proptest::collection::vec(any::(), 0..2048)) { + let _ = decode_with(&data, &tight()); + } + + #[test] + fn random_bytes_with_magic_never_panic(idx in 0usize..14, data in proptest::collection::vec(any::(), 0..1024)) { + let magics: [&[u8]; 14] = [ + b"\x89PNG\r\n\x1a\n", &[0xFF, 0xD8, 0xFF], b"II*\0", b"RIFF\0\0\0\0WEBP", b"GIF89a", b"BM", + &[0, 0, 1, 0, 1, 0], b"P6\n", b"qoif", &[0x76, 0x2F, 0x31, 0x01], b"#?RADIANCE\n", b"P7\n", b"PF\n", + b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", + ]; + let mut b = magics[idx].to_vec(); + b.extend_from_slice(&data); + let _ = decode_with(&b, &tight()); + } + + #[test] + fn random_bytes_each_format_never_panic(idx in 0usize..Format::ALL.len(), data in proptest::collection::vec(any::(), 0..512)) { + let _ = decode_as_with(Format::ALL[idx], &data, &tight()); + } + + #[test] + fn mutated_valid_files_never_panic(which in 0usize..40, pos in any::(), val in any::(), cut in any::()) { + let s = samples_cached(); + let (f, bytes) = &s[which % s.len()]; + let mut b = bytes.clone(); + let p = pos % b.len(); + b[p] = val; + let c = cut % (b.len() + 1); + let _ = decode_as_with(*f, &b[..c.max(p + 1).min(b.len())], &tight()); + } +} + +fn samples_cached() -> &'static [(Format, Vec)] { + static S: std::sync::OnceLock)>> = std::sync::OnceLock::new(); + S.get_or_init(samples) +} + +/// A little-endian TIFF with one 1x1 8-bit grey strip and the given extra IFD entries +/// (tag, type, count, value), sorted into place. +fn tiny_tiff(extra: &[(u16, u16, u32, u32)]) -> Vec { + const SHORT: u16 = 3; + const LONG: u16 = 4; + let mut entries = vec![ + (256, SHORT, 1, 1), // ImageWidth + (257, SHORT, 1, 1), // ImageLength + (258, SHORT, 1, 8), // BitsPerSample + (259, SHORT, 1, 1), // Compression: none + (262, SHORT, 1, 1), // Photometric: BlackIsZero + (273, LONG, 1, 0), // StripOffsets (patched below) + (277, SHORT, 1, 1), // SamplesPerPixel + (278, SHORT, 1, 1), // RowsPerStrip + (279, LONG, 1, 1), // StripByteCounts + ]; + entries.extend_from_slice(extra); + entries.sort_by_key(|e| e.0); + let ifd_len = 2 + 12 * entries.len() + 4; + let strip = 8 + ifd_len as u32; + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, ty, count, value) in entries { + let value = if tag == 273 { strip } else { value }; + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + b.extend_from_slice(&count.to_le_bytes()); + b.extend_from_slice(&value.to_le_bytes()); + } + b.extend_from_slice(&0u32.to_le_bytes()); + b.push(0x80); + b +} + +#[test] +fn tiff_fixture_decodes() { + let img = decode_as_with(Format::Tiff, &tiny_tiff(&[]), &tight()).unwrap(); + assert_eq!((img.width(), img.height()), (1, 1)); +} + +/// Regression: an empty SampleFormat tag made the tiff 0.10 decoder index an empty list +/// (`index out of bounds: the len is 0`). Found by mutation fuzzing; fixed by tiff 0.11. +#[test] +fn tiff_empty_sample_format_is_an_error() { + let bytes = tiny_tiff(&[(339, 3, 0, 0)]); + assert!(decode_as_with(Format::Tiff, &bytes, &tight()).is_err()); + assert!(decode_with(&bytes, &tight()).is_err()); +} diff --git a/crates/image-codecs/tests/it/metadata.rs b/crates/image-codecs/tests/it/metadata.rs new file mode 100644 index 000000000..d6d292557 --- /dev/null +++ b/crates/image-codecs/tests/it/metadata.rs @@ -0,0 +1,340 @@ +//! ICC / EXIF / XMP / DPI / text preservation, per format, per caps. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn rich_image(layout: ChannelLayout) -> Image { + let mut img = test_image(layout, SampleType::U8); + img.icc = Some(sample_icc(3000)); + img.meta = Metadata { + exif: Some(sample_exif()), + xmp: Some(SAMPLE_XMP.to_owned()), + dpi: Some((300.0, 150.0)), + text: vec![("Description".into(), "synthetic test image".into()), ("Software".into(), "photocraft".into())], + ..Default::default() + }; + img +} + +fn check_format(format: Format) { + let c = caps(format); + if !(c.read && c.write) { + return; + } + let layout = if c.layouts.contains(&ChannelLayout::Rgb) { ChannelLayout::Rgb } else { c.layouts[0] }; + let img = rich_image(layout); + let bytes = encode(&img, format, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap(); + let warnings = fidelity_warnings(&img, format); + + assert_eq!(back.icc.is_some(), c.icc, "{format:?} icc presence"); + if c.icc { + assert_eq!(back.icc, img.icc, "{format:?} icc bytes"); + } else { + assert!(warnings.contains(&FidelityWarning::IccDropped), "{format:?}"); + } + if c.exif && format == Format::Tiff { + // TIFF writes the block's directories as its own and rebuilds a block from them on + // reading (tests/it/tiff_exif.rs); IFD0's orientation here is the image's. + assert!(!warnings.contains(&FidelityWarning::ExifDropped), "{format:?}"); + } else if c.exif && format == Format::WebP { + // WebP's resolution lives in EXIF, so the block comes back with resolution tags added. + let back_exif = back.meta.exif.as_deref().unwrap_or_else(|| panic!("{format:?} lost exif")); + assert_eq!(craft_image_codecs::resolution::exif_resolution(back_exif).map(|(x, y)| (x.round(), y.round())), Some((300.0, 150.0))); + // The rest of the block survives too: the Orientation tag (0x0112, little-endian). + assert!(back_exif.windows(2).any(|w| w == b"\x12\x01"), "{format:?} lost Orientation"); + } else if c.exif { + assert_eq!(back.meta.exif, img.meta.exif, "{format:?} exif"); + } else { + assert!(warnings.contains(&FidelityWarning::ExifDropped), "{format:?}"); + } + if c.xmp { + assert_eq!(back.meta.xmp, img.meta.xmp, "{format:?} xmp"); + } else { + assert!(back.meta.xmp.is_none()); + assert!(warnings.contains(&FidelityWarning::XmpDropped), "{format:?}"); + } + if c.dpi { + let (x, y) = back.meta.dpi.unwrap_or_else(|| panic!("{format:?} lost dpi")); + assert!((x - 300.0).abs() < 0.05 && (y - 150.0).abs() < 0.05, "{format:?} dpi {x},{y}"); + } else { + assert!(warnings.contains(&FidelityWarning::DpiDropped), "{format:?}"); + } + if c.text { + for kv in &img.meta.text { + assert!(back.meta.text.contains(kv), "{format:?} lost text {kv:?}: {:?}", back.meta.text); + } + } else { + assert!(back.meta.text.is_empty()); + assert!(warnings.contains(&FidelityWarning::TextDropped), "{format:?}"); + } +} + +#[test] +fn png_metadata() { + check_format(Format::Png); +} +#[test] +fn jpeg_metadata() { + check_format(Format::Jpeg); +} +#[test] +fn tiff_metadata() { + check_format(Format::Tiff); +} +#[test] +fn webp_metadata() { + check_format(Format::WebP); +} +#[test] +fn gif_metadata() { + check_format(Format::Gif); +} +#[test] +fn bmp_metadata() { + check_format(Format::Bmp); +} +#[test] +fn tga_metadata() { + check_format(Format::Tga); +} +#[test] +fn ico_metadata() { + check_format(Format::Ico); +} +#[test] +fn fits_metadata() { + check_format(Format::Fits); +} +#[test] +fn pnm_metadata() { + check_format(Format::Pnm); +} +#[test] +fn qoi_metadata() { + check_format(Format::Qoi); +} +#[test] +fn exr_metadata() { + check_format(Format::OpenExr); +} +#[test] +fn hdr_metadata() { + check_format(Format::Hdr); +} + +#[test] +fn metadata_on_16bit_png_and_tiff() { + for f in [Format::Png, Format::Tiff] { + let mut img = test_image(ChannelLayout::Rgba, SampleType::U16); + img.icc = Some(sample_icc(512)); + img.meta.dpi = Some((72.0, 72.0)); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.icc, img.icc); + assert_eq!(back.sample_type(), SampleType::U16); + } +} + +#[test] +fn icc_on_cmyk_tiff_and_jpeg() { + for f in [Format::Tiff, Format::Jpeg] { + let img = test_image(ChannelLayout::Cmyk, SampleType::U8).with_icc(Some(sample_icc(900))); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Cmyk, "{f:?}"); + assert_eq!(back.icc, img.icc, "{f:?}"); + } +} + +#[test] +fn cmyk_icc_not_written_to_rgb_format() { + let img = test_image(ChannelLayout::Cmyk, SampleType::U8).with_icc(Some(sample_icc(900))); + let back = decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.icc.is_none(), "a CMYK profile must not be attached to converted RGB data"); + assert!(fidelity_warnings(&img, Format::Png).contains(&FidelityWarning::IccDropped)); +} + +#[test] +fn jpeg_multi_segment_icc() { + // > 65519 bytes forces several APP2 segments. + for len in [65519, 65520, 150_000, 300_001] { + let img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(len))); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let app2 = bytes.windows(14).filter(|w| w[0] == 0xFF && w[1] == 0xE2 && &w[4..14] == b"ICC_PROFIL").count(); + assert_eq!(app2, len.div_ceil(65519), "segments for {len}"); + assert_eq!(decode(&bytes).unwrap().icc.map(|v| v.len()), Some(len)); + // Oracle: the image crate reads the same profile. + use image::ImageDecoder; + let mut d = image::codecs::jpeg::JpegDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + assert_eq!(d.icc_profile().unwrap(), img.icc); + } +} + +/// A layered document's XMP can list every document ever placed in it and the text of every type +/// layer: far more than the 64 KB one JPEG segment holds. The JPEG is still written, with that +/// bookkeeping left out of the XMP; metadata too large even then is dropped with a warning. +#[test] +fn jpeg_with_oversized_metadata_still_encodes() { + let ancestors: String = (0..3000).map(|i| format!("xmp.did:{i:032x}")).collect(); + let texts: String = (0..400) + .map(|i| format!("Layer {i}Words on layer {i}")) + .collect(); + let xmp = format!( + "\n{ancestors}{texts}" + ); + assert!(xmp.len() > 120_000, "{}", xmp.len()); + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut img = test_image(ChannelLayout::Rgb, sample); + img.meta.xmp = Some(xmp.clone()); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap().meta.xmp.unwrap(); + assert!(back.contains("xmp:CreatorTool=\"synthetic\"") && back.ends_with(""), "{sample:?}"); + assert!(!back.contains("DocumentAncestors>") && !back.contains("TextLayers"), "{sample:?}"); + assert!(back.contains(""), "{sample:?}: a longer name is not the element"); + assert!(!fidelity_warnings(&img, Format::Jpeg).iter().any(|w| matches!(w, FidelityWarning::MetadataTooLarge { .. }))); + // Formats without a segment limit keep the packet whole. + assert_eq!(decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap().meta.xmp.as_deref(), Some(xmp.as_str())); + } + // Still too large: the JPEG is written without it. + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + let big = format!("", "x".repeat(70_000)); + let mut exif = sample_exif(); + exif.resize(70_000, 0); + img.meta.xmp = Some(big.clone()); + img.meta.exif = Some(exif); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.meta.xmp.is_none() && back.meta.exif.is_none()); + let w = fidelity_warnings(&img, Format::Jpeg); + assert!(w.contains(&FidelityWarning::MetadataTooLarge { what: "XMP", bytes: big.len() }), "{w:?}"); + assert!(w.contains(&FidelityWarning::MetadataTooLarge { what: "EXIF", bytes: 70_000 }), "{w:?}"); +} + +#[test] +fn embed_flags_disable_metadata() { + let img = rich_image(ChannelLayout::Rgb); + let opts = EncodeOptions { embed_icc: false, embed_metadata: false, ..Default::default() }; + for f in [Format::Png, Format::Jpeg, Format::Tiff, Format::WebP] { + let back = decode(&encode(&img, f, &opts).unwrap()).unwrap(); + assert!(back.icc.is_none(), "{f:?}"); + assert!(back.meta.exif.is_none() && back.meta.xmp.is_none(), "{f:?}"); + assert!(back.meta.text.is_empty(), "{f:?}"); + assert!(fidelity_warnings_with(&img, Format::Bmp, &opts).iter().all(|w| !matches!(w, FidelityWarning::IccDropped | FidelityWarning::ExifDropped))); + } +} + +#[test] +fn png_unicode_text_uses_itxt() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta.text = vec![("Title".into(), "Ünïcödé ☃ 雪".into()), ("Author".into(), "latin1 ok é".into())]; + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(bytes.windows(4).any(|w| w == b"iTXt")); + assert!(bytes.windows(4).any(|w| w == b"tEXt")); + let mut back = decode(&bytes).unwrap().meta.text; + let mut want = img.meta.text.clone(); + back.sort(); + want.sort(); + assert_eq!(back, want); +} + +#[test] +fn png_xmp_in_itxt_keyword() { + let mut img = test_image(ChannelLayout::Gray, SampleType::U8); + img.meta.xmp = Some(SAMPLE_XMP.into()); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(bytes.windows(17).any(|w| w == b"XML:com.adobe.xmp")); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.xmp.as_deref(), Some(SAMPLE_XMP)); + assert!(back.meta.text.is_empty(), "XMP must not leak into text"); +} + +#[test] +fn png_exif_chunk_written() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta.exif = Some(sample_exif()); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(bytes.windows(4).any(|w| w == b"eXIf")); + assert!(!bytes.windows(4).any(|w| w == b"iCCP")); +} + +#[test] +fn jpeg_exif_readable_by_oracle() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta.exif = Some(sample_exif()); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + use image::ImageDecoder; + let mut d = image::codecs::jpeg::JpegDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + let exif = d.exif_metadata().unwrap().unwrap(); + assert!(exif.ends_with(&sample_exif())); +} + +#[test] +fn jpeg_dpi_in_jfif() { + let mut img = test_image(ChannelLayout::Gray, SampleType::U8); + img.meta.dpi = Some((96.0, 200.0)); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.meta.dpi, Some((96.0, 200.0))); +} + +#[test] +fn tiff_icc_readable_by_oracle() { + let img = test_image(ChannelLayout::Rgb, SampleType::U16).with_icc(Some(sample_icc(1234))); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + use image::ImageDecoder; + let mut d = image::codecs::tiff::TiffDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + assert_eq!(d.icc_profile().unwrap(), img.icc); +} + +#[test] +fn png_icc_readable_by_oracle() { + let img = test_image(ChannelLayout::Rgba, SampleType::U8).with_icc(Some(sample_icc(777))); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + use image::ImageDecoder; + let mut d = image::codecs::png::PngDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + assert_eq!(d.icc_profile().unwrap(), img.icc); +} + +#[test] +fn metadata_survives_depth_conversion() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::F32).with_icc(Some(sample_icc(300))); + img.meta.xmp = Some(SAMPLE_XMP.into()); + let back = decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.icc, img.icc); + assert_eq!(back.meta.xmp, img.meta.xmp); +} + +/// #2399: a WebP export keeps the resolution even when the image has no EXIF (WebP has no +/// resolution field of its own, so a minimal EXIF block carries it), lossless and lossy alike, +/// and no longer warns that it is dropped. +#[test] +fn webp_keeps_dpi_without_source_exif() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta = Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }; + assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + for lossless in [true, false] { + let opts = EncodeOptions { webp_lossless: lossless, ..Default::default() }; + let back = decode(&encode(&img, Format::WebP, &opts).unwrap()).unwrap(); + let (x, y) = back.meta.dpi.unwrap_or_else(|| panic!("lossless {lossless}: dpi lost")); + assert!((x - 300.0).abs() < 0.05 && (y - 150.0).abs() < 0.05, "lossless {lossless}: {x},{y}"); + } + // EXIF that can't be parsed is written as it is, so the resolution is reported as dropped. + img.meta = Metadata { exif: Some(b"Exif\0\0garbage".to_vec()), dpi: Some((300.0, 150.0)), ..Default::default() }; + assert!(fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.meta.exif.is_some()); + // ...unless the XMP written beside it records the resolution, which readers look at first. + img.meta.xmp = Some(r#""#.into()); + assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + let (x, y) = back.meta.dpi.expect("dpi from XMP"); + assert!((x - 300.0).abs() < 0.05 && (y - 150.0).abs() < 0.05, "{x},{y}"); + // The default 72 ppi needs no EXIF (a WebP without one opens at 72), so a plain export stays + // a simple-format file; nor does an image without a resolution. + img.meta = Metadata { dpi: Some((72.0, 72.0)), ..Default::default() }; + assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + let bytes = encode(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).unwrap(); + assert_eq!(bytes.get(12..16), Some(&b"VP8 "[..])); + // Without a resolution or EXIF there is still no EXIF chunk. + img.meta = Metadata::default(); + let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.meta.exif.is_none()); +} diff --git a/crates/image-codecs/tests/it/orientation.rs b/crates/image-codecs/tests/it/orientation.rs new file mode 100644 index 000000000..c278f36f6 --- /dev/null +++ b/crates/image-codecs/tests/it/orientation.rs @@ -0,0 +1,670 @@ +//! EXIF / TIFF orientation (#285): files are opened upright, and every export +//! writes Orientation = 1 so the upright pixels are never rotated twice. + +use crate::common; +use common::*; +use craft_image_codecs::*; +use proptest::prelude::{any, prop_assert, prop_assert_eq, proptest}; + +/// A minimal EXIF (TIFF-structured) block: a filler tag, then Orientation. +fn exif(o: u32, big: bool, long: bool) -> Vec { + let u16b = |v: u16| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let mut v = if big { b"MM\0*".to_vec() } else { b"II*\0".to_vec() }; + v.extend_from_slice(&u32b(8)); + v.extend_from_slice(&u16b(2)); + // ImageDescription-ish filler (ASCII, 4 bytes inline). + v.extend_from_slice(&u16b(0x010E)); + v.extend_from_slice(&u16b(2)); + v.extend_from_slice(&u32b(4)); + v.extend_from_slice(b"abc\0"); + v.extend_from_slice(&u16b(0x0112)); + if long { + v.extend_from_slice(&u16b(4)); + v.extend_from_slice(&u32b(1)); + v.extend_from_slice(&u32b(o)); + } else { + v.extend_from_slice(&u16b(3)); + v.extend_from_slice(&u32b(1)); + v.extend_from_slice(&u16b(o as u16)); + v.extend_from_slice(&[0, 0]); + } + v.extend_from_slice(&u32b(0)); + v +} + +/// The "upright" test picture: `w`×`h` gray values, distinct per pixel. +fn upright(w: usize, h: usize, value: impl Fn(usize, usize) -> u8) -> Vec { + (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| value(x, y)).collect() +} + +/// What a camera stores for orientation `o` when the scene is `up` (`w`×`h`): the inverse +/// of the turn a viewer applies. Returns the stored pixels and their size. +fn stored(up: &[u8], w: usize, h: usize, o: u16) -> (Vec, usize, usize) { + let (sw, sh) = if o >= 5 { (h, w) } else { (w, h) }; + let mut s = vec![0u8; up.len()]; + for y in 0..h { + for x in 0..w { + // Where upright pixel (x, y) sits in the stored image. + let (sx, sy) = match o { + 1 => (x, y), + 2 => (w - 1 - x, y), // mirrored horizontally + 3 => (w - 1 - x, h - 1 - y), // rotated 180° + 4 => (x, h - 1 - y), // mirrored vertically + 5 => (y, x), // transposed + 6 => (y, w - 1 - x), // viewer turns it 90° clockwise + 7 => (h - 1 - y, w - 1 - x), // transverse + 8 => (h - 1 - y, x), // viewer turns it 90° counter-clockwise + _ => unreachable!(), + }; + s[sy * sw + sx] = up[y * w + x]; + } + } + (s, sw, sh) +} + +/// Inserts a JPEG APP1 EXIF segment right after SOI. +fn jpeg_with_exif(jpeg: &[u8], exif: &[u8]) -> Vec { + let mut seg = b"Exif\0\0".to_vec(); + seg.extend_from_slice(exif); + let mut out = jpeg[..2].to_vec(); + out.extend_from_slice(&[0xFF, 0xE1]); + out.extend_from_slice(&((seg.len() + 2) as u16).to_be_bytes()); + out.extend_from_slice(&seg); + out.extend_from_slice(&jpeg[2..]); + out +} + +/// Inserts a PNG eXIf chunk before the first IDAT. +fn png_with_exif(png: &[u8], exif: &[u8]) -> Vec { + let at = png.windows(4).position(|w| w == b"IDAT").unwrap() - 4; + let mut chunk = (exif.len() as u32).to_be_bytes().to_vec(); + let mut body = b"eXIf".to_vec(); + body.extend_from_slice(exif); + let mut crc = flate2::Crc::new(); + crc.update(&body); + chunk.extend_from_slice(&body); + chunk.extend_from_slice(&crc.sum().to_be_bytes()); + let mut out = png[..at].to_vec(); + out.extend_from_slice(&chunk); + out.extend_from_slice(&png[at..]); + out +} + +fn tiff_with_orientation(data: &[u8], w: u32, h: u32, o: u16) -> Vec { + use tiff::encoder::{TiffEncoder, colortype::Gray8}; + let mut cursor = std::io::Cursor::new(Vec::new()); + let mut enc = TiffEncoder::new(&mut cursor).unwrap(); + let mut im = enc.new_image::(w, h).unwrap(); + im.encoder().write_tag(tiff::tags::Tag::Orientation, o).unwrap(); + im.write_data(data).unwrap(); + cursor.into_inner() +} + +fn gray(w: usize, h: usize, px: Vec) -> Image { + Image::from_u8(w as u32, h as u32, ChannelLayout::Gray, px).unwrap() +} + +/// Exact pixels: distinct values per pixel. +fn exact_scene(w: usize, h: usize) -> Vec { + upright(w, h, |x, y| (y * w + x) as u8 * 3 + 1) +} + +#[test] +fn reads_orientation_in_every_encoding() { + for o in 1..=8 { + for big in [false, true] { + for long in [false, true] { + let e = exif(o, big, long); + assert_eq!(exif_orientation(&e), o as u16, "o={o} big={big} long={long}"); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend_from_slice(&e); + assert_eq!(exif_orientation(&prefixed), o as u16); + let fixed = upright_exif(&prefixed); + assert_eq!(exif_orientation(&fixed), 1); + assert_eq!(fixed.len(), prefixed.len(), "rewritten in place"); + assert_eq!(&fixed[..6], b"Exif\0\0"); + // Rewritten in the entry's own type and width: exactly `exif(1, ..)`. + assert_eq!(&fixed[6..], &exif(1, big, long)[..], "o={o} big={big} long={long}"); + } + } + } + // Already upright, no tag, or garbage: borrowed unchanged. + assert!(matches!(upright_exif(&exif(1, false, false)), std::borrow::Cow::Borrowed(_))); + assert!(matches!(upright_exif(&sample_exif()), std::borrow::Cow::Borrowed(_))); + assert!(matches!(upright_exif(b"nonsense"), std::borrow::Cow::Borrowed(_))); +} + +#[test] +fn malformed_or_out_of_range_orientation_reads_as_1() { + for bad in [0, 9, 255, 0xFFFF, 0x1_0006] { + assert_eq!(exif_orientation(&exif(bad, false, true)), 1, "{bad}"); + } + let good = exif(6, false, false); + assert_eq!(exif_orientation(&good), 6); + for n in 0..good.len() { + // Any cut, even one that keeps the Orientation entry but loses the next-IFD pointer. + assert_eq!(exif_orientation(&good[..n]), 1, "truncated at {n}"); + assert!(matches!(upright_exif(&good[..n]), std::borrow::Cow::Borrowed(_)), "truncated at {n}"); + } + // IFD offset past the end, a huge entry count, a wrong field type. + let mut far = good.clone(); + far[4..8].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(exif_orientation(&far), 1); + let mut many = good.clone(); + many[8..10].copy_from_slice(&u16::MAX.to_le_bytes()); + assert_eq!(exif_orientation(&many), 1, "an entry count past the end is a truncated table"); + let mut typed = good.clone(); + typed[24..26].copy_from_slice(&2u16.to_le_bytes()); // ASCII + assert_eq!(exif_orientation(&typed), 1); + assert_eq!(exif_orientation(&[]), 1); + assert_eq!(exif_orientation(b"II*\0"), 1); +} + +proptest! { + #[test] + fn random_exif_never_panics(bytes in proptest::collection::vec(any::(), 0..256), le in any::()) { + let mut b = if le { b"II*\0\x08\0\0\0".to_vec() } else { b"MM\0*\0\0\0\x08".to_vec() }; + b.extend_from_slice(&bytes); + let o = exif_orientation(&b); + prop_assert!((1..=8).contains(&o)); + prop_assert_eq!(exif_orientation(&upright_exif(&b)), 1); + let _ = exif_orientation(&bytes); + let _ = upright_exif(&bytes); + } + + #[test] + fn random_xmp_never_panics(s in ".{0,64}", tail in ".{0,16}") { + let x = format!("{s}tiff:Orientation{tail}"); + let _ = upright_xmp(&x); + } +} + +#[test] +fn xmp_orientation_is_rewritten() { + let attr = r#""#; + assert_eq!(upright_xmp(attr), r#""#); + let elem = "83"; + assert_eq!(upright_xmp(elem), "11"); + assert_eq!(upright_xmp("tiff:Orientation='12'"), "tiff:Orientation='1'"); + for same in [r#"tiff:Orientation="1""#, "no orientation", "tiff:Orientation", "tiff:Orientation=\"\"", "tiff:Orientation: 6"] { + assert!(matches!(upright_xmp(same), std::borrow::Cow::Borrowed(_)), "{same}"); + } +} + +#[test] +fn png_opens_upright_for_every_orientation() { + let (w, h) = (5, 3); + let up = exact_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let png = encode(&gray(sw, sh, s.clone()), Format::Png, &EncodeOptions::default()).unwrap(); + let file = png_with_exif(&png, &exif(u32::from(o), o % 2 == 0, false)); + let img = decode(&file).unwrap(); + assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}"); + assert_eq!(img.data(), &up[..], "o={o}"); + assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1, "o={o}: metadata says upright"); + // Decoded in steps and finished by `finish_png_decode`, it is the same image. + let stepped = PngDecodeRows::new(file.as_slice(), &Limits::default()).unwrap().step(&mut || true).unwrap().unwrap(); + assert_eq!(finish_png_decode(stepped, &DecodeOptions::default()).unwrap(), img, "o={o}: stepped"); + // Opting out keeps the stored pixels and the tag. + let raw = decode_with(&file, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!((raw.dimensions(), raw.data()), ((sw as u32, sh as u32), &s[..]), "o={o}"); + assert_eq!(exif_orientation(raw.meta.exif.as_deref().unwrap()), o); + } +} + +/// An image turned upright a band of rows at a time is the image `oriented` gives, for every +/// orientation, pixel size and shape, and takes a call per band. +#[test] +fn orient_steps_equal_oriented() { + for o in 1..=8u16 { + for (layout, sample) in [ + (ChannelLayout::Gray, SampleType::U8), + (ChannelLayout::GrayA, SampleType::U16), + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U16), + (ChannelLayout::Rgba, SampleType::F32), + ] { + for (w, h) in [(1u32, 1u32), (7, 5), (130, 66), (65, 200)] { + let len = w as usize * h as usize * layout.channels() * sample.bytes(); + let data: Vec = (0..len).map(|i| (i.wrapping_mul(2_654_435_761) >> 11) as u8).collect(); + let mut img = Image::from_raw(w, h, layout, sample, data).unwrap(); + img.meta.dpi = Some((100.0, 200.0)); + img.meta.exif = Some(exif(u32::from(o), false, false)); + let want = img.clone().oriented(o).unwrap(); + let mut steps = OrientSteps::new(img, o).unwrap(); + let mut calls = 0; + let got = loop { + calls += 1; + assert!(steps.progress() <= 1.0); + if let Some(img) = steps.step(&mut || false).unwrap() { + break img; + } + }; + assert_eq!(got, want, "o={o} {layout:?} {sample:?} {w}x{h}"); + let rows = if o >= 5 { w } else { h } as usize; + let expected = if (2..=8).contains(&o) { rows.div_ceil(64) } else { 1 }; + assert_eq!(calls, expected, "o={o} {w}x{h}: one call per band of 64 rows"); + } + } + } +} + +/// A PNG turned upright in bands by `begin_png_finish` is the image `decode` gives, for every +/// orientation, and a band of a tall image is not the whole of it. +#[test] +fn png_finishes_upright_in_bands_for_every_orientation() { + let (w, h) = (70, 150); + let up = upright(w, h, |x, y| (x * 7 + y * 13) as u8); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let png = encode(&gray(sw, sh, s), Format::Png, &EncodeOptions::default()).unwrap(); + let file = png_with_exif(&png, &exif(u32::from(o), o % 2 == 0, false)); + let want = decode(&file).unwrap(); + let img = PngDecodeRows::new(file.as_slice(), &Limits::default()).unwrap().step(&mut || true).unwrap().unwrap(); + let got = match begin_png_finish(img, &DecodeOptions::default()).unwrap() { + PngFinish::Done(img) => { + assert_eq!(o, 1); + *img + } + PngFinish::Orient(mut steps) => { + assert!(o > 1); + let mut calls = 0; + loop { + calls += 1; + if let Some(img) = steps.step(&mut || false).unwrap() { + assert!(calls > 1, "o={o}: more than one band"); + break img; + } + } + } + }; + assert_eq!(got, want, "o={o}"); + } +} + +/// Extra samples (a TIFF's alpha and spot planes) turn with the image, so a channel stays +/// aligned with the pixels for every orientation. +#[test] +fn extra_samples_turn_with_the_image() { + let (w, h) = (4, 7); + let up = exact_scene(w, h); + let up_extra: Vec = up.iter().map(|v| 255 - v).collect(); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let (s_extra, _, _) = stored(&up_extra, w, h, o); + let mut img = gray(sw, sh, s); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: s_extra }]; + let turned = img.oriented(o).unwrap(); + assert_eq!(turned.data(), &up[..], "o={o}"); + assert_eq!(turned.meta.extra_samples.len(), 1, "o={o}"); + assert_eq!(turned.meta.extra_samples[0].data, up_extra, "o={o}"); + } +} + +#[test] +fn tiff_opens_upright_for_every_orientation() { + let (w, h) = (4, 7); + let up = exact_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let img = decode(&tiff_with_orientation(&s, sw as u32, sh as u32, o)).unwrap(); + assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}"); + assert_eq!(img.data(), &up[..], "o={o}"); + } +} + +#[test] +fn webp_opens_upright() { + let (w, h) = (6, 2); + let up = exact_scene(w, h); + for o in [3, 6, 8] { + let (s, sw, sh) = stored(&up, w, h, o); + let mut out = Vec::new(); + let mut enc = image_webp::WebPEncoder::new(&mut out); + enc.set_exif_metadata(exif(u32::from(o), false, false)); + enc.encode(&s, sw as u32, sh as u32, image_webp::ColorType::L8).unwrap(); + // WebP has no gray mode: it comes back as RGB. + let img = decode(&out).unwrap().convert(ChannelLayout::Gray, SampleType::U8); + assert_eq!((img.dimensions(), img.data()), ((w as u32, h as u32), &up[..]), "o={o}"); + assert_eq!(probe_size(&out).unwrap(), (w as u32, h as u32), "the header probe turns upright too, o={o}"); + } +} + +/// 8×8-block scene so JPEG keeps each block's value: block (bx, by) is distinct. +fn block_scene(w: usize, h: usize) -> Vec { + upright(w, h, |x, y| (20 + 25 * (x / 8 + (w / 8) * (y / 8))) as u8) +} + +fn assert_blocks(img: &Image, up: &[u8], w: usize, h: usize, what: &str) { + assert_eq!(img.dimensions(), (w as u32, h as u32), "{what}"); + let d = img.data(); + for by in 0..h / 8 { + for bx in 0..w / 8 { + let i = (by * 8 + 4) * w + bx * 8 + 4; + assert!((i32::from(d[i]) - i32::from(up[i])).abs() <= 4, "{what}: block ({bx},{by}) is {} not {}", d[i], up[i]); + } + } +} + +#[test] +fn jpeg_opens_upright_for_every_orientation() { + let (w, h) = (32, 16); + let up = block_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let jpeg = encode(&gray(sw, sh, s), Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let file = jpeg_with_exif(&jpeg, &exif(u32::from(o), o > 4, o == 7)); + let img = decode(&file).unwrap(); + assert_blocks(&img, &up, w, h, &format!("o={o}")); + assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1); + } +} + +#[test] +fn rgb_jpeg_with_dpi_swaps_resolution_on_a_quarter_turn() { + let img = Image::from_u8(16, 8, ChannelLayout::Rgb, vec![128; 16 * 8 * 3]).unwrap().with_meta(Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }); + let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let back = decode(&jpeg_with_exif(&jpeg, &exif(6, false, false))).unwrap(); + assert_eq!(back.dimensions(), (8, 16)); + assert_eq!(back.meta.dpi, Some((150.0, 300.0))); +} + +#[test] +fn export_writes_orientation_1_and_keeps_pixels() { + let (w, h) = (32, 16); + let up = block_scene(w, h); + let (s, sw, sh) = stored(&up, w, h, 6); + let jpeg = encode(&gray(sw, sh, s), Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let opened = decode(&jpeg_with_exif(&jpeg, &exif(6, false, false))).unwrap(); + assert_blocks(&opened, &up, w, h, "opened"); + + // Even an image whose metadata still claims 6 (e.g. from a PSD) is written as 1. + let mut stale = opened.clone(); + stale.meta.exif = Some(exif(6, true, false)); + stale.meta.xmp = Some(r#""#.into()); + for format in [Format::Png, Format::Jpeg, Format::WebP, Format::Tiff] { + let bytes = encode(&stale, format, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let raw = decode_with(&bytes, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + if let Some(e) = &raw.meta.exif { + assert_eq!(exif_orientation(e), 1, "{format:?}"); + } + if let Some(x) = &raw.meta.xmp { + assert!(x.contains(r#"tiff:Orientation="1""#), "{format:?}: {x}"); + } + assert_eq!(exif_orientation(&bytes), 1, "{format:?} file"); + // Pixels as shown, both with and without applying orientation on reopen. + let back = decode(&bytes).unwrap(); + assert_eq!(back.dimensions(), raw.dimensions(), "{format:?}"); + if format == Format::Jpeg { + assert_blocks(&back, &up, w, h, "jpeg reopen"); + } else { + assert_eq!(back.convert(ChannelLayout::Gray, SampleType::U8).data(), opened.data(), "{format:?} lossless"); + } + } +} + +#[test] +fn round_trip_open_save_reopen_is_stable() { + let (w, h) = (24, 16); + let up = block_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let jpeg = encode(&gray(sw, sh, s), Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let first = decode(&jpeg_with_exif(&jpeg, &exif(u32::from(o), false, false))).unwrap(); + let saved = encode(&first, Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let second = decode(&saved).unwrap(); + assert_blocks(&second, &up, w, h, &format!("reopened o={o}")); + assert_eq!(exif_orientation(second.meta.exif.as_deref().unwrap()), 1); + } +} + +#[test] +fn malformed_exif_in_a_jpeg_opens_as_stored() { + let img = gray(16, 8, vec![90; 128]); + let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let mut rng = Rng::new(285); + let mut cases = vec![Vec::new(), b"II*\0".to_vec(), b"MM\0*\xff\xff\xff\xff".to_vec(), exif(6, false, false)[..20].to_vec()]; + cases.extend((0..64).map(|i| { + let mut b = if i % 2 == 0 { b"II*\0\x08\0\0\0".to_vec() } else { b"MM\0*\0\0\0\x08".to_vec() }; + b.extend(rng.bytes(i * 7)); + b + })); + for e in cases { + let back = decode(&jpeg_with_exif(&jpeg, &e)).unwrap(); + let o = exif_orientation(&e); + let want = if o >= 5 { (8, 16) } else { (16, 8) }; + assert_eq!(back.dimensions(), want, "{e:?}"); + } +} + +#[test] +fn every_layout_and_depth_orients_and_inverts() { + let inverse = |o: u16| match o { + 6 => 8, + 8 => 6, + o => o, + }; + for layout in ChannelLayout::ALL { + for sample in SampleType::ALL { + let img = test_image(layout, sample); + for o in 0..=9u16 { + let turned = img.clone().oriented(o).unwrap(); + if (2..=8).contains(&o) && o >= 5 { + assert_eq!(turned.dimensions(), (img.height(), img.width())); + } else { + assert_eq!(turned.dimensions(), img.dimensions()); + } + let back = turned.oriented(inverse(o)).unwrap(); + assert_eq!(back.data(), img.data(), "{layout:?} {sample:?} o={o}"); + } + } + } +} + +/// #285 performance budget: turning a 24 MP photo upright on open adds ≤ 50 ms +/// (release build: `cargo test --release -p craft-image-codecs --test it -- orientation:: --ignored`). +#[test] +#[ignore = "timing; run in release"] +fn rotating_24_mp_is_fast() { + let (w, h) = (6000u32, 4000u32); + let img = Image::from_u8(w, h, ChannelLayout::Rgb, (0..w as usize * h as usize * 3).map(|i| i as u8).collect()).unwrap(); + for o in [6u16, 3, 8] { + let mut best = f64::MAX; + for _ in 0..5 { + let c = img.clone(); + let t = std::time::Instant::now(); + let r = c.oriented(o).unwrap(); + best = best.min(t.elapsed().as_secs_f64() * 1000.0); + std::hint::black_box(r); + } + eprintln!("orientation {o}: 24 MP RGB8 in {best:.1} ms"); + assert!(best <= 50.0, "orientation {o} took {best:.1} ms"); + } +} + +/// A TIFF-structured EXIF block: IFD0 at offset 8 holding `entries` +/// (`tag, type, count, 4-byte value field`), a zero next-IFD pointer, then `tail`. +fn ifd(big: bool, entries: &[(u16, u16, u32, [u8; 4])], tail: &[u8]) -> Vec { + let u16b = |v: u16| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let mut v = if big { b"MM\0*".to_vec() } else { b"II*\0".to_vec() }; + v.extend_from_slice(&u32b(8)); + v.extend_from_slice(&u16b(entries.len() as u16)); + for &(tag, ty, count, value) in entries { + v.extend_from_slice(&u16b(tag)); + v.extend_from_slice(&u16b(ty)); + v.extend_from_slice(&u32b(count)); + v.extend_from_slice(&value); + } + v.extend_from_slice(&u32b(0)); + v.extend_from_slice(tail); + v +} + +/// The 4-byte value field of a SHORT. +fn short(v: u16, big: bool) -> [u8; 4] { + let b = if big { v.to_be_bytes() } else { v.to_le_bytes() }; + [b[0], b[1], 0, 0] +} + +#[test] +fn duplicate_orientation_entries_first_wins() { + for big in [false, true] { + let o = |v| (0x0112, 3, 1, short(v, big)); + assert_eq!(exif_orientation(&ifd(big, &[o(6), o(3)], &[])), 6); + assert_eq!(exif_orientation(&ifd(big, &[o(3), o(6)], &[])), 3); + assert_eq!(exif_orientation(&ifd(big, &[o(1), o(8)], &[])), 1); + // A malformed first entry is ignored as a whole: a later good one does not count. + let bad_first = ifd(big, &[(0x0112, 3, 2, short(6, big)), o(8)], &[]); + assert_eq!(exif_orientation(&bad_first), 1); + let long = |v: u32| (0x0112, 4, 1, if big { v.to_be_bytes() } else { v.to_le_bytes() }); + let long_first = ifd(big, &[long(6), o(8)], &[]); + assert_eq!(exif_orientation(&long_first), 6, "a LONG first entry counts"); + assert_eq!(&upright_exif(&long_first)[..], &ifd(big, &[long(1), o(1)], &[])[..], "each in its own width"); + let rational_first = ifd(big, &[(0x0112, 5, 1, short(6, big)), o(8)], &[]); + assert_eq!(exif_orientation(&rational_first), 1); + // Upright: every well-formed duplicate becomes 1, so no reader rotates twice. + let dup = ifd(big, &[o(6), o(3)], &[]); + let fixed = upright_exif(&dup); + assert_eq!(fixed.len(), dup.len()); + assert_eq!(&fixed[..], &ifd(big, &[o(1), o(1)], &[])[..]); + // Only a later duplicate disagrees: it is rewritten too. + assert_eq!(&upright_exif(&ifd(big, &[o(1), o(8)], &[]))[..], &ifd(big, &[o(1), o(1)], &[])[..]); + assert!(matches!(upright_exif(&ifd(big, &[o(1), o(1)], &[])), std::borrow::Cow::Borrowed(_))); + } +} + +#[test] +fn strict_ifd_parsing_ignores_the_tag() { + for big in [false, true] { + let o6 = (0x0112, 3, 1, short(6, big)); + let good = ifd(big, &[o6], &[]); + assert_eq!(exif_orientation(&good), 6); + // Truncated before (or inside) the next-IFD pointer. + for cut in 1..=4 { + assert_eq!(exif_orientation(&good[..good.len() - cut]), 1, "cut {cut}"); + } + // IFD offsets inside the 8-byte header. + for off in 0..8u32 { + let mut b = good.clone(); + b[4..8].copy_from_slice(&if big { off.to_be_bytes() } else { off.to_le_bytes() }); + assert_eq!(exif_orientation(&b), 1, "offset {off}"); + assert!(matches!(upright_exif(&b), std::borrow::Cow::Borrowed(_))); + } + // Count must be exactly 1, type must be SHORT or LONG. + let long6 = if big { 6u32.to_be_bytes() } else { 6u32.to_le_bytes() }; + for count in [0, 2, 3, u32::MAX] { + assert_eq!(exif_orientation(&ifd(big, &[(0x0112, 3, count, short(6, big))], &[])), 1, "count {count}"); + assert_eq!(exif_orientation(&ifd(big, &[(0x0112, 4, count, long6)], &[])), 1, "LONG count {count}"); + } + for ty in [1, 2, 5, 6, 7, 8, 9, 0, 0xFFFF] { + let b = ifd(big, &[(0x0112, ty, 1, short(6, big))], &[]); + assert_eq!(exif_orientation(&b), 1, "type {ty}"); + assert!(matches!(upright_exif(&b), std::borrow::Cow::Borrowed(_)), "type {ty}"); + } + } +} + +#[test] +fn upright_exif_keeps_every_other_byte() { + for big in [false, true] { + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + // IFD0: Make (12-byte ASCII stored after the IFD), Orientation 6, Software (inline ASCII), + // ExifIFD pointer to a sub-IFD with its own entries. + let ifd_end = 8 + 2 + 4 * 12 + 4; + let make_at = ifd_end as u32; + let sub_at = make_at + 12; + let mut tail = b"PhotoCraftCo".to_vec(); + // Sub-IFD: one DateTimeOriginal-ish SHORT entry, then next-IFD 0. + tail.extend_from_slice(&if big { 1u16.to_be_bytes() } else { 1u16.to_le_bytes() }); + tail.extend_from_slice(&if big { 0x9000u16.to_be_bytes() } else { 0x9000u16.to_le_bytes() }); + tail.extend_from_slice(&if big { 7u16.to_be_bytes() } else { 7u16.to_le_bytes() }); + tail.extend_from_slice(&u32b(4)); + tail.extend_from_slice(b"0232"); + tail.extend_from_slice(&u32b(0)); + let e = ifd(big, &[(0x010F, 2, 12, u32b(make_at)), (0x0112, 3, 1, short(6, big)), (0x0131, 2, 4, *b"PC1\0"), (0x8769, 4, 1, u32b(sub_at))], &tail); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend_from_slice(&e); + let fixed = upright_exif(&prefixed).into_owned(); + assert_eq!(exif_orientation(&fixed), 1); + assert_eq!(fixed.len(), prefixed.len()); + // Only the Orientation value (2 bytes in the second entry) differs. + let value = 6 + 8 + 2 + 12 + 8; + let changed: Vec = (0..fixed.len()).filter(|&i| fixed[i] != prefixed[i]).collect(); + assert_eq!(changed, vec![if big { value + 1 } else { value }], "big={big}"); + assert_eq!(&fixed[..value], &prefixed[..value]); + assert_eq!(&fixed[value + 2..], &prefixed[value + 2..]); + // The offset-based Make string and the inline Software tag are intact. + assert_eq!(&fixed[6 + make_at as usize..][..12], b"PhotoCraftCo"); + assert!(fixed.windows(4).any(|w| w == b"PC1\0")); + } +} + +#[test] +fn limits_apply_to_the_upright_size() { + // Stored 32×16 landscape; Orientation 6 makes it a 16×32 portrait. + let jpeg = encode(&gray(32, 16, vec![90; 32 * 16]), Format::Jpeg, &EncodeOptions::default()).unwrap(); + let tall = jpeg_with_exif(&jpeg, &exif(6, false, false)); + let limits = Limits { max_width: 32, max_height: 16, ..Limits::default() }; + let opts = DecodeOptions { limits, ..Default::default() }; + assert!(matches!(decode_with(&tall, &opts), Err(CodecError::LimitExceeded(_))), "the upright portrait is too tall"); + // As stored it fits, and a 180° turn keeps the size. + let kept = decode_with(&tall, &DecodeOptions { keep_orientation: true, ..opts.clone() }).unwrap(); + assert_eq!(kept.dimensions(), (32, 16)); + let flipped = decode_with(&jpeg_with_exif(&jpeg, &exif(3, false, false)), &opts).unwrap(); + assert_eq!(flipped.dimensions(), (32, 16)); + // The same holds for a TIFF, whose orientation lives in its own IFD0. + let tiff = tiff_with_orientation(&[7; 32 * 16], 32, 16, 8); + assert!(matches!(decode_with(&tiff, &opts), Err(CodecError::LimitExceeded(_)))); + assert_eq!(decode_with(&tiff, &DecodeOptions::default()).unwrap().dimensions(), (16, 32)); +} + +#[test] +fn long_orientation_reads_and_rewrites_in_both_byte_orders() { + for big in [false, true] { + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let long = |v: u32| (0x0112u16, 4u16, 1u32, u32b(v)); + for o in 1..=8u32 { + let e = ifd(big, &[(0x0131, 2, 4, *b"PC1\0"), long(o)], b"tail"); + assert_eq!(exif_orientation(&e), o as u16, "o={o} big={big}"); + let fixed = upright_exif(&e); + assert_eq!(exif_orientation(&fixed), 1); + // Still a LONG, all 4 value bytes set to 1 in the file's byte order; nothing else moved. + assert_eq!(&fixed[..], &ifd(big, &[(0x0131, 2, 4, *b"PC1\0"), long(1)], b"tail")[..], "o={o} big={big}"); + } + // Out of range, including values that only fit in a LONG, reads as 1 and is left alone. + for bad in [0, 9, 0x1_0006, u32::MAX] { + let e = ifd(big, &[long(bad)], &[]); + assert_eq!(exif_orientation(&e), 1, "{bad}"); + } + // A LONG 6 in a JPEG opens upright. + let jpeg = encode(&gray(32, 16, vec![90; 32 * 16]), Format::Jpeg, &EncodeOptions::default()).unwrap(); + let img = decode(&jpeg_with_exif(&jpeg, &ifd(big, &[long(6)], &[]))).unwrap(); + assert_eq!(img.dimensions(), (16, 32)); + let kept = img.meta.exif.as_deref().unwrap(); + assert_eq!(kept.strip_prefix(b"Exif\0\0").unwrap_or(kept), &ifd(big, &[long(1)], &[])[..]); + } +} + +/// `probe_size` reads the size `decode` returns from the header alone, for every format it +/// covers, and a file cut short inside its header is refused rather than given a size. +#[test] +fn probe_size_matches_decode() { + let img = synth(7, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.0); + let probed = [Format::WebP, Format::Gif, Format::Bmp, Format::Tga, Format::Qoi, Format::Hdr, Format::Ico, Format::OpenExr]; + assert!(probed.iter().all(|f| rw_formats().contains(f))); + for format in rw_formats() { + let bytes = encode(&img, format, &EncodeOptions::default()).unwrap(); + let decoded = decode(&bytes).unwrap().dimensions(); + match probe_size(&bytes) { + Ok(size) => assert_eq!(size, decoded, "{format:?}"), + Err(_) => assert!(!probed.contains(&format), "{format:?} should be probed"), + } + if probed.contains(&format) { + assert!(probe_size(&bytes[..10]).is_err(), "{format:?} cut inside its header"); + } + } + assert!(probe_size(&[]).is_err()); +} diff --git a/crates/image-codecs/tests/it/pcx.rs b/crates/image-codecs/tests/it/pcx.rs new file mode 100644 index 000000000..b112a3af1 --- /dev/null +++ b/crates/image-codecs/tests/it/pcx.rs @@ -0,0 +1,137 @@ +use craft_image_codecs::{ChannelLayout, CodecError, Format, SampleType, caps, decode, decode_as, detect, encode, from_extension}; + +fn header(bits: u8, planes: u8, width: u16, height: u16, stride: u16) -> Vec { + let mut b = vec![0u8; 128]; + b[0] = 0x0a; // ZSoft + b[1] = 5; // PC Paintbrush 3.0+ + b[2] = 1; // RLE + b[3] = bits; + b[8..10].copy_from_slice(&(width - 1).to_le_bytes()); + b[10..12].copy_from_slice(&(height - 1).to_le_bytes()); + b[12..14].copy_from_slice(&300u16.to_le_bytes()); + b[14..16].copy_from_slice(&150u16.to_le_bytes()); + b[65] = planes; + b[66..68].copy_from_slice(&stride.to_le_bytes()); + b[68..70].copy_from_slice(&1u16.to_le_bytes()); + b +} + +fn rle_row(out: &mut Vec, row: &[u8]) { + for &value in row { + if value >= 0xc0 { + out.extend_from_slice(&[0xc1, value]); + } else { + out.push(value); + } + } +} + +#[test] +fn pcx_is_readable_but_not_writable() { + assert_eq!(from_extension("archive.PCX"), Some(Format::Pcx)); + assert!(caps(Format::Pcx).read && !caps(Format::Pcx).write); + let img = craft_image_codecs::Image::from_u8(1, 1, ChannelLayout::Rgb, vec![0, 0, 0]).unwrap(); + assert!(matches!(encode(&img, Format::Pcx, &Default::default()), Err(CodecError::Unsupported { .. }))); +} + +#[test] +fn decodes_three_plane_rgb_and_keeps_dpi() { + let mut b = header(8, 3, 2, 1, 2); + for row in [&[255, 0][..], &[0, 255][..], &[0, 0][..]] { + rle_row(&mut b, row); + } + assert_eq!(detect(&b), Some(Format::Pcx)); + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgb); + assert_eq!(im.sample_type(), SampleType::U8); + assert_eq!(im.dimensions(), (2, 1)); + assert_eq!(im.data(), &[255, 0, 0, 0, 255, 0]); + assert_eq!(im.meta.dpi, Some((300.0, 150.0))); +} + +#[test] +fn decodes_256_colour_vga_palette() { + let mut b = header(8, 1, 2, 1, 2); + rle_row(&mut b, &[1, 2]); + b.push(0x0c); + let mut palette = vec![0; 256 * 3]; + palette[3..6].copy_from_slice(&[255, 0, 0]); + palette[6..9].copy_from_slice(&[0, 255, 0]); + b.extend(palette); + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0]); +} + +#[test] +fn decodes_4bit_and_monochrome_palettes() { + let mut four = header(4, 1, 2, 1, 2); + four[16 + 3..16 + 6].copy_from_slice(&[255, 0, 0]); + four[16 + 6..16 + 9].copy_from_slice(&[0, 0, 255]); + rle_row(&mut four, &[0x12, 0]); + assert_eq!(decode(&four).unwrap().data(), &[255, 0, 0, 0, 0, 255]); + + let mut one = header(1, 1, 2, 1, 2); + one[16 + 3..16 + 6].copy_from_slice(&[255, 0, 0]); + rle_row(&mut one, &[0x80, 0]); + assert_eq!(decode(&one).unwrap().data(), &[255, 0, 0, 0, 0, 0]); +} + +#[test] +fn rejects_truncated_and_overshooting_rle_without_panicking() { + let mut b = header(8, 3, 2, 1, 2); + assert!(decode_as(Format::Pcx, &b).is_err(), "header alone is truncated"); + b.extend([0xc7, 0xff]); // a seven-byte run cannot cross a six-byte scanline + assert!(decode_as(Format::Pcx, &b).is_err()); + let mut b = header(8, 3, 2, 1, 2); + b.extend([0xc1]); // missing run value + assert!(decode_as(Format::Pcx, &b).is_err()); + let mut b = header(8, 3, 2, 1, 2); + b[66..68].copy_from_slice(&0u16.to_le_bytes()); + assert_eq!(detect(&b), None, "invalid stride is not a PCX signature"); + + // A solid image with stride of the form 63k + 1 (64, 127) encodes as k full 63-byte runs + // plus a 1-byte literal (2k + 1 bytes per plane row), which is valid and must decode. + for width in [64u16, 127] { + let mut solid = header(8, 3, width, 1, width); + for _plane in 0..3 { + let mut rem = usize::from(width); + while rem >= 63 { + solid.extend_from_slice(&[0xff, 0x00]); // 63-byte run of 0x00 + rem -= 63; + } + if rem == 1 { + solid.push(0x00); // 1-byte literal 0x00 + } + } + let im = decode(&solid).unwrap(); + assert_eq!(im.dimensions(), (u32::from(width), 1)); + assert!(im.data().iter().all(|&v| v == 0)); + } +} + +#[test] +fn refuses_headers_that_exceed_allocation_budget() { + let mut b = header(8, 3, 3000, 3000, 3000); + // The default decoder rejects the allocation before reading any scanline. + let opts = craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; + assert!(matches!(craft_image_codecs::decode_as_with(Format::Pcx, &b, &opts), Err(CodecError::LimitExceeded(_)))); + b[2] = 0; + assert_eq!(detect(&b), None, "unsupported uncompressed encoding"); +} + +#[test] +fn decodes_rgb_runs_across_planes() { + let mut b = header(8, 3, 2, 1, 2); + b.extend([0xc4, 0xff, 0xc2, 0]); + let im = decode(&b).unwrap(); + assert_eq!(im.data(), &[255, 255, 0, 255, 255, 0]); + let png = encode(&im, Format::Png, &Default::default()).unwrap(); + assert_eq!(decode(&png).unwrap().data(), im.data()); +} + +#[test] +fn decodes_four_plane_runs_across_planes() { + let mut b = header(1, 4, 2, 1, 2); + b[16 + 15 * 3..16 + 16 * 3].copy_from_slice(&[255, 255, 0]); + b.extend([0xc8, 0xff]); + assert_eq!(decode(&b).unwrap().data(), &[255, 255, 0, 255, 255, 0]); +} diff --git a/crates/image-codecs/tests/it/png_jpeg.rs b/crates/image-codecs/tests/it/png_jpeg.rs new file mode 100644 index 000000000..e98b0ae09 --- /dev/null +++ b/crates/image-codecs/tests/it/png_jpeg.rs @@ -0,0 +1,646 @@ +//! Format-specific behaviour: Adam7 PNG, JPEG colour models, oracle +//! cross-checks against the `image` crate, and the optional PngSuite corpus. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +// --------------------------------------------------------------------------- +// PNG Adam7 +// --------------------------------------------------------------------------- + +fn interlaced(img: &Image) -> Vec { + encode(img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap() +} + +fn check_adam7(w: u32, h: u32, layout: ChannelLayout, sample: SampleType) { + let img = synth(w, h, layout, sample, (w * 31 + h) as u64, 0.5); + let a = interlaced(&img); + let b = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(a[28], 1, "IHDR interlace flag"); + assert_eq!(b[28], 0); + let da = decode(&a).unwrap(); + let db = decode(&b).unwrap(); + assert_eq!(da, db, "{w}x{h} {layout:?} {sample:?}: interlaced != progressive"); + assert_eq!(da.data(), img.data()); + // Oracle: the image crate agrees. + let o = image::load_from_memory_with_format(&a, image::ImageFormat::Png).unwrap(); + let ours = da.convert(ChannelLayout::Rgba, SampleType::U16).to_u16_samples().unwrap(); + assert_eq!(o.into_rgba16().into_raw(), ours); +} + +#[test] +fn adam7_small_sizes_all_layouts() { + for (w, h) in [(1, 1), (2, 2), (3, 3), (5, 1), (1, 9), (7, 5), (8, 8), (9, 17), (33, 31)] { + for l in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + check_adam7(w, h, l, SampleType::U8); + } + } +} + +#[test] +fn adam7_16bit() { + for (w, h) in [(1, 1), (4, 3), (13, 11), (64, 40)] { + for l in [ChannelLayout::Gray, ChannelLayout::Rgba] { + check_adam7(w, h, l, SampleType::U16); + } + } +} + +#[test] +fn adam7_with_metadata() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(400))); + img.meta.text.push(("Comment".into(), "interlaced".into())); + let back = decode(&interlaced(&img)).unwrap(); + assert_eq!(back.icc, img.icc); + assert_eq!(back.meta.text, img.meta.text); +} + +#[test] +fn adam7_compressions() { + let img = test_image(ChannelLayout::Rgba, SampleType::U8); + for c in [PngCompression::None, PngCompression::Fast, PngCompression::Best] { + let b = encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, png_compression: c, ..Default::default() }).unwrap(); + assert_eq!(decode(&b).unwrap().data(), img.data()); + } +} + +#[test] +fn png_palette_and_low_bit_depth_expand() { + // Build a 2-bit gray PNG and a palette PNG with tRNS using the png crate. + let mut out = Vec::new(); + { + let mut e = png::Encoder::new(&mut out, 4, 1); + e.set_color(png::ColorType::Grayscale); + e.set_depth(png::BitDepth::Two); + let mut w = e.write_header().unwrap(); + w.write_image_data(&[0b00_01_10_11]).unwrap(); + } + let img = decode(&out).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Gray, &[0u8, 85, 170, 255][..])); + + let mut out = Vec::new(); + { + let mut e = png::Encoder::new(&mut out, 2, 1); + e.set_color(png::ColorType::Indexed); + e.set_depth(png::BitDepth::Eight); + e.set_palette(vec![255, 0, 0, 0, 0, 255]); + e.set_trns(vec![128]); + let mut w = e.write_header().unwrap(); + w.write_image_data(&[0, 1]).unwrap(); + } + let img = decode(&out).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgba, &[255u8, 0, 0, 128, 0, 0, 255, 255][..])); +} + +// --------------------------------------------------------------------------- +// JPEG colour models +// --------------------------------------------------------------------------- + +#[test] +fn jpeg_grayscale_decodes_as_gray() { + let img = synth(40, 30, ChannelLayout::Gray, SampleType::U8, 2, 0.02); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Gray); + assert!(psnr(&img, &back) > 35.0); +} + +#[test] +fn jpeg_cmyk_roundtrip_with_adobe_marker() { + let img = synth(40, 30, ChannelLayout::Cmyk, SampleType::U8, 3, 0.02); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 95, ..Default::default() }).unwrap(); + assert!(bytes.windows(5).any(|w| w == b"Adobe"), "APP14 Adobe marker"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Cmyk); + assert!(psnr(&img, &back) > 35.0, "{}", psnr(&img, &back)); +} + +#[test] +fn jpeg_ycck_decodes_to_cmyk() { + let img = synth(40, 32, ChannelLayout::Cmyk, SampleType::U8, 4, 0.02); + let mut bytes = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut bytes, 95); + enc.set_sampling_factor(jpeg_encoder::SamplingFactor::R_4_4_4); + enc.encode(img.data(), 40, 32, jpeg_encoder::ColorType::CmykAsYcck).unwrap(); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Cmyk); + assert!(psnr(&img, &back) > 32.0, "{}", psnr(&img, &back)); +} + +#[test] +fn jpeg_cmyk_flat_colour_exact_ish() { + let img = Image::from_u8(16, 16, ChannelLayout::Cmyk, [10u8, 100, 200, 50].repeat(256)).unwrap(); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + for px in back.data().chunks(4) { + for (a, b) in px.iter().zip([10u8, 100, 200, 50]) { + assert!((*a as i32 - b as i32).abs() <= 2); + } + } +} + +#[test] +fn jpeg_rgb_matches_oracle_decoder() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let ours = decode(&bytes).unwrap(); + let theirs = image::load_from_memory_with_format(&bytes, image::ImageFormat::Jpeg).unwrap().into_rgb8(); + let max = ours.data().iter().zip(theirs.as_raw()).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max <= 1, "max diff {max}"); +} + +#[test] +fn jpeg_progressive_from_other_encoder_decodes() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let mut bytes = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut bytes, 90); + enc.set_progressive(true); + enc.encode(img.data(), img.width() as u16, img.height() as u16, jpeg_encoder::ColorType::Rgb).unwrap(); + let back = decode(&bytes).unwrap(); + assert!(psnr(&img, &back) > 25.0); +} + +// --------------------------------------------------------------------------- +// Oracle: files we write are readable by the image crate +// --------------------------------------------------------------------------- + +#[test] +fn png_output_matches_oracle_all_layouts() { + for l in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for s in [SampleType::U8, SampleType::U16] { + let img = test_image(l, s); + let b = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let o = image::load_from_memory(&b).unwrap().into_rgba16().into_raw(); + assert_eq!(o, img.to_rgba16(), "{l:?} {s:?}"); + } + } +} + +#[test] +fn tiff_output_matches_oracle() { + for l in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for s in [SampleType::U8, SampleType::U16] { + let img = test_image(l, s); + let b = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let o = image::load_from_memory(&b).unwrap().into_rgba16().into_raw(); + assert_eq!(o, img.to_rgba16(), "{l:?} {s:?}"); + } + } +} + +#[test] +fn tiff_f32_output_matches_oracle() { + let img = test_image(ChannelLayout::Rgba, SampleType::F32); + let b = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::None, ..Default::default() }).unwrap(); + let o = image::load_from_memory(&b).unwrap().into_rgba32f().into_raw(); + assert_eq!(o, img.to_f32_samples().unwrap()); +} + +// --------------------------------------------------------------------------- +// PNG decoded in steps (PngDecodeRows) +// --------------------------------------------------------------------------- + +/// Decodes `bytes` with [`PngDecodeRows`], going on for `per_call` rows per call (0: unlimited); +/// returns the image and how many calls it took. +fn decode_in_steps(bytes: &[u8], per_call: usize) -> Result<(Image, usize), CodecError> { + let mut rows = PngDecodeRows::new(bytes, &Limits::default())?; + let mut calls = 0; + loop { + calls += 1; + let mut left = per_call; + let mut more = || { + left = left.wrapping_sub(1); + per_call == 0 || left > 0 + }; + if let Some(img) = rows.step(&mut more)? { + return Ok((img, calls)); + } + assert!(calls < 100_000, "the decoder never finished"); + } +} + +fn assert_steps_equal_decode(bytes: &[u8], what: &str) { + let whole = decode(bytes); + for per_call in [1, 7, 0] { + // `decode` also takes the resolution from EXIF or XMP and turns the image upright; + // `finish_png_decode` does that for a stepped decode, as the stepped import does. + let stepped = decode_in_steps(bytes, per_call).and_then(|(img, calls)| Ok((finish_png_decode(img, &DecodeOptions::default())?, calls))); + match (&whole, stepped) { + (Ok(a), Ok((b, _))) => assert_eq!(a, &b, "{what}, {per_call} rows per call"), + (Err(_), Err(_)) => {} + (a, b) => panic!("{what}, {per_call} rows per call: decode {:?}, steps {:?}", a.as_ref().map(|_| ()), b.map(|_| ())), + } + } +} + +#[test] +fn png_rows_equal_decode_in_every_layout_and_depth() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for sample in [SampleType::U8, SampleType::U16] { + let mut img = synth(41, 29, layout, sample, 77, 0.3).with_icc(Some(sample_icc(400))); + img.meta.text.push(("Comment".into(), "stepped".into())); + img.meta.dpi = Some((300.0, 300.0)); + for interlaced in [false, true] { + let bytes = encode(&img, Format::Png, &EncodeOptions { png_interlaced: interlaced, ..Default::default() }).unwrap(); + assert_steps_equal_decode(&bytes, &format!("{layout:?} {sample:?} interlaced={interlaced}")); + } + } + } +} + +#[test] +fn png_rows_equal_decode_for_palette_and_transparency() { + let pal = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [9, 9, 9]]; + let idx: Vec = (0..50 * 20).map(|i| (i % 4) as u8).collect(); + let bytes = encode_png_indexed(50, 20, &idx, &pal, Some(3), None, None).unwrap(); + assert_steps_equal_decode(&bytes, "palette with a transparent entry"); +} + +#[test] +fn png_rows_take_one_call_per_row_and_report_progress() { + let bytes = encode(&synth(16, 40, ChannelLayout::Rgb, SampleType::U8, 5, 0.2), Format::Png, &EncodeOptions::default()).unwrap(); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + assert_eq!(rows.size(), (16, 40)); + assert_eq!(rows.progress(), 0.0); + let mut seen = Vec::new(); + let img = loop { + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + seen.push(rows.progress()); + }; + assert_eq!(seen.len(), 39, "one call per row but the last, which also finishes the file"); + assert!(seen.windows(2).all(|w| w[0] < w[1])); + assert_eq!(rows.progress(), 1.0); + assert_eq!(img, decode(&bytes).unwrap()); + // A decoder that has finished says so instead of decoding again. + assert!(rows.step(&mut || true).is_err()); +} + +#[test] +fn png_rows_share_their_bytes_without_copying_them() { + let bytes = std::sync::Arc::new(encode(&synth(20, 20, ChannelLayout::Rgba, SampleType::U8, 3, 0.2), Format::Png, &EncodeOptions::default()).unwrap()); + let mut rows = PngDecodeRows::new(SharedBytes(bytes.clone()), &Limits::default()).unwrap(); + let img = rows.step(&mut || true).unwrap().unwrap(); + assert_eq!(img, decode(&bytes).unwrap()); + drop(rows); + assert_eq!(std::sync::Arc::strong_count(&bytes), 1, "the decoder let go of the bytes"); +} + +/// A CRC-valid PNG chunk. +fn png_chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut crc = flate2::Crc::new(); + crc.update(kind); + crc.update(data); + let mut out = u32::try_from(data.len()).unwrap().to_be_bytes().to_vec(); + out.extend_from_slice(kind); + out.extend_from_slice(data); + out.extend_from_slice(&crc.sum().to_be_bytes()); + out +} + +fn zlib(text: &[u8]) -> Vec { + use std::io::Write; + let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + encoder.write_all(text).unwrap(); + encoder.finish().unwrap() +} + +/// A `zTXt` chunk (`deflated`) or a compressed `iTXt` one (`!deflated`) with `text` as its value. +fn png_text_chunk(kind: &[u8; 4], keyword: &[u8], text: &[u8]) -> Vec { + let mut data = keyword.to_vec(); + data.push(0); + if kind == b"zTXt" { + data.push(0); + } else { + data.extend_from_slice(&[1, 0, 0, 0]); + } + data.extend_from_slice(&zlib(text)); + png_chunk(kind, &data) +} + +/// `png` with `chunks` between the image data and `IEND` (the text-after-IDAT case). +fn with_chunks_before_iend(png: &[u8], chunks: &[Vec]) -> Vec { + let mut at = 8; + loop { + let len = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize; + if &png[at + 4..at + 8] == b"IEND" { + let mut out = png[..at].to_vec(); + for c in chunks { + out.extend_from_slice(c); + } + out.extend_from_slice(&png[at..]); + return out; + } + at += len + 12; + } +} + +#[test] +fn png_rows_read_compressed_text_in_bounded_batches() { + let img = synth(19, 23, ChannelLayout::Rgba, SampleType::U8, 9, 0.4); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let text = "A".repeat(150_000); + for kind in [b"zTXt", b"iTXt"] { + let bytes = with_chunks_before_iend(&base, &[png_text_chunk(kind, b"Comment", text.as_bytes())]); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + // An empty budget: every step reads one bounded batch and stops. + let stepped = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 100_000, "the decoder never finished"); + }; + assert_eq!(stepped, decode(&bytes).unwrap(), "{kind:?}"); + assert_eq!(stepped.meta.text, vec![("Comment".into(), text.clone())], "{kind:?}: the whole value is kept"); + assert!(calls > 20, "{kind:?}: {calls} bounded calls, not one blocking pass"); + assert_steps_equal_decode(&bytes, &format!("compressed {kind:?} text")); + } +} + +/// An Adam7 image whose compressed text needs many metadata yields: the pixels are decoded +/// once, before the first yield, and later steps only read text. +#[test] +fn png_rows_finish_adam7_text_in_bounded_batches() { + let img = synth(19, 23, ChannelLayout::Rgba, SampleType::U8, 9, 0.4); + let base = encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap(); + let text = "A".repeat(150_000); + for kind in [b"zTXt", b"iTXt"] { + let bytes = with_chunks_before_iend(&base, &[png_text_chunk(kind, b"Comment", text.as_bytes())]); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + let stepped = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 100_000, "the decoder never finished"); + }; + assert_eq!(stepped, decode(&bytes).unwrap(), "{kind:?}"); + assert_eq!(stepped.meta.text, vec![("Comment".into(), text.clone())], "{kind:?}: the whole value is kept"); + assert!(calls > 20, "{kind:?}: {calls} bounded calls after the one Adam7 pass"); + } +} + +/// An uncompressed `iTXt` chunk (`value` is its text, so its CRC covers the whole value). +fn png_itxt_uncompressed(keyword: &[u8], value: &[u8]) -> Vec { + let mut data = keyword.to_vec(); + data.push(0); + data.extend_from_slice(&[0, 0, 0, 0]); + data.extend_from_slice(value); + png_chunk(b"iTXt", &data) +} + +/// The CRC of a large text chunk takes bounded batches too, before any of its value is read. +#[test] +fn png_rows_read_a_large_text_crc_in_bounded_batches() { + let img = synth(5, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.2); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let text: String = (0..300_000u32).map(|i| char::from(b'a' + (i % 26) as u8)).collect(); + let bytes = with_chunks_before_iend(&base, &[png_itxt_uncompressed(b"Comment", text.as_bytes())]); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + let stepped = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 100_000, "the decoder never finished"); + }; + assert_eq!(stepped.meta.text, vec![("Comment".into(), text)]); + assert!(calls > 60, "{calls} calls: the CRC and the value each take bounded batches"); +} + +/// A text chunk whose CRC does not match is still dropped after its bounded CRC batches. +#[test] +fn png_rows_drop_a_corrupt_text_crc() { + let img = synth(5, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.2); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut chunk = png_itxt_uncompressed(b"Comment", b"not accepted"); + let at = chunk.len() - 5; + chunk[at] ^= 0xFF; + let bytes = with_chunks_before_iend(&base, &[chunk]); + let stepped = decode_in_steps(&bytes, 0).unwrap().0; + assert!(stepped.meta.text.is_empty(), "a broken CRC drops the text"); + assert_eq!(stepped, decode(&bytes).unwrap(), "the one-pass decode drops it too"); +} + +/// An uncompressed iTXt value that is not UTF-8 is dropped without charging its bytes against +/// the budget, even when it is larger than all of it: the previous reader checked UTF-8 first. +#[test] +fn png_rows_drop_invalid_uncompressed_text_before_charging_the_budget() { + let img = synth(4, 4, ChannelLayout::Rgb, SampleType::U8, 3, 0.2); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut value = vec![b'A'; 12_000]; + value[11_999] = 0xFF; + let bytes = with_chunks_before_iend(&base, &[png_itxt_uncompressed(b"Comment", &value)]); + let opts = DecodeOptions { limits: Limits { max_alloc: 200, ..Default::default() }, ..Default::default() }; + let mut rows = PngDecodeRows::new(bytes.as_slice(), &opts.limits).unwrap(); + let stepped = rows.step(&mut || true).unwrap().unwrap(); + let stepped = finish_png_decode(stepped, &opts).unwrap(); + assert!(stepped.meta.text.is_empty(), "invalid UTF-8 is skipped, not a budget failure"); + let whole = decode_with(&bytes, &opts).unwrap(); + assert!(whole.meta.text.is_empty(), "the one-pass decode skips it too"); +} + +#[test] +fn png_rows_fail_like_decode_on_damaged_files() { + let img = synth(64, 64, ChannelLayout::Rgb, SampleType::U8, 11, 1.0); + for interlaced in [false, true] { + let bytes = encode(&img, Format::Png, &EncodeOptions { png_interlaced: interlaced, ..Default::default() }).unwrap(); + // Cuts inside the signature, the IHDR chunk or the next chunk header leave no complete image. + for cut in [8, 20, 40] { + let truncated = bytes.get(..cut).unwrap(); + assert!(decode(truncated).is_err(), "cut at {cut} interlaced={interlaced}"); + assert!(decode_in_steps(truncated, 1).is_err(), "cut at {cut} interlaced={interlaced}"); + } + for cut in [bytes.len() / 3, bytes.len() / 2, bytes.len() - 20] { + assert_steps_equal_decode(bytes.get(..cut).unwrap(), &format!("cut at {cut} interlaced={interlaced}")); + } + // A flipped byte in the middle of the compressed data. + let mut bad = bytes.clone(); + let mid = bad.len() / 2; + bad[mid] ^= 0xFF; + assert_steps_equal_decode(&bad, "a damaged byte"); + } + assert!(decode_in_steps(&[], 1).is_err()); + assert!(decode_in_steps(b"not a png at all, just some text bytes", 1).is_err()); +} + +// --------------------------------------------------------------------------- +// PngSuite corpus (repo-root corpus/pngsuite/*.png; feature `corpus`, fetched by +// `cargo xtask corpus --all`; missing = failure) +// --------------------------------------------------------------------------- + +#[cfg(feature = "corpus")] +#[test] +fn pngsuite_corpus_matches_oracle() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/pngsuite"); + let Ok(entries) = std::fs::read_dir(&dir) else { + panic!("{} is missing: run `cargo xtask corpus --all`", dir.display()); + }; + let mut checked = 0; + for e in entries.flatten() { + let p = e.path(); + if p.extension().and_then(|s| s.to_str()) != Some("png") { + continue; + } + let bytes = std::fs::read(&p).unwrap(); + let oracle = image::load_from_memory_with_format(&bytes, image::ImageFormat::Png); + let ours = decode(&bytes); + match (ours, oracle) { + (Ok(a), Ok(b)) => { + assert_eq!(a.to_rgba16(), b.into_rgba16().into_raw(), "{}", p.display()); + checked += 1; + } + (Err(e), Ok(_)) => panic!("{}: we failed but oracle decoded: {e}", p.display()), + _ => {} + } + } + eprintln!("pngsuite: {checked} files compared"); + assert!(checked > 100, "{}: only {checked} PNGs compared: run `cargo xtask corpus --all`", dir.display()); +} + +/// The preflight charges the decoded layout's bytes per pixel: a 1000×1000 Gray8 PNG (1 MB) fits a +/// 1.5 MB budget, which the old worst-case RGBA16 preflight (8 MB) refused. +#[test] +fn png_preflight_uses_the_output_layout() { + let img = synth(1000, 1000, ChannelLayout::Gray, SampleType::U8, 7, 0.5); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let opts = DecodeOptions { limits: Limits { max_alloc: 1_500_000, ..Limits::none() }, ..Default::default() }; + let got = decode_with(&bytes, &opts).expect("a Gray8 image is charged 1 byte per pixel"); + assert_eq!(got.data(), img.data()); + let tight = DecodeOptions { limits: Limits { max_alloc: 900_000, ..Limits::none() }, ..Default::default() }; + assert!(decode_with(&bytes, &tight).is_err(), "the actual pixel budget still applies"); +} + +#[cfg(feature = "corpus")] +#[test] +fn png_rows_equal_decode_on_the_pngsuite_corpus() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/pngsuite"); + let Ok(entries) = std::fs::read_dir(&dir) else { + panic!("{} is missing: run `cargo xtask corpus --all`", dir.display()); + }; + let mut checked = 0; + for e in entries.flatten() { + let p = e.path(); + if p.extension().and_then(|s| s.to_str()) != Some("png") { + continue; + } + let bytes = std::fs::read(&p).unwrap(); + assert_steps_equal_decode(&bytes, &p.display().to_string()); + checked += 1; + } + assert!(checked > 100, "{}: only {checked} PNGs compared: run `cargo xtask corpus --all`", dir.display()); +} + +/// Row-by-row decoding gives `decode`'s pixels and metadata for every PngSuite file it takes (all +/// but the interlaced ones), and fails where `decode` fails. +#[cfg(feature = "corpus")] +#[test] +fn pngsuite_corpus_rows_match_the_whole_decode() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/pngsuite"); + let Ok(entries) = std::fs::read_dir(&dir) else { + panic!("{} is missing: run `cargo xtask corpus --all`", dir.display()); + }; + let opts = DecodeOptions { keep_orientation: true, ..Default::default() }; + let (mut streamed, mut refused, mut skipped) = (0, 0, 0); + for e in entries.flatten() { + let p = e.path(); + if p.extension().and_then(|s| s.to_str()) != Some("png") { + continue; + } + let bytes = std::fs::read(&p).unwrap(); + // The importer only streams files `detect` takes for PNG. + if detect(&bytes) != Some(Format::Png) { + skipped += 1; + continue; + } + let whole = decode_with(&bytes, &opts); + let (mut data, mut header) = (Vec::new(), None); + let rows = decode_png_rows::( + &bytes, + &opts, + 256, + &mut |h| { + header = Some(*h); + Ok(true) + }, + &mut |_, r| { + data.extend_from_slice(r); + Ok(()) + }, + ); + match (whole, rows) { + (Ok(img), Ok(Some(x))) => { + let h = header.unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (h.width, h.height, h.layout, h.sample), "{}", p.display()); + assert!(img.data() == data, "{}: pixels", p.display()); + assert_eq!( + (&img.icc, &img.meta.exif, &img.meta.text, &img.meta.xmp, &img.warnings), + (&x.icc, &x.meta.exif, &x.meta.text, &x.meta.xmp, &x.warnings), + "{}", + p.display() + ); + assert_eq!(img.meta.dpi, x.meta.dpi, "{}", p.display()); + streamed += 1; + } + (Ok(_), Ok(None)) => refused += 1, + (Err(a), Err(b)) => assert_eq!(a.to_string(), b.to_string(), "{}", p.display()), + (a, b) => panic!("{}: whole {:?}, rows {:?}", p.display(), a.map(|_| ()), b.map(|r| r.map(|_| ()))), + } + } + eprintln!("pngsuite: {streamed} streamed, {refused} refused (interlaced), {skipped} skipped (not detected as PNG)"); + assert!(streamed > 100, "only {streamed} PNGs streamed: run `cargo xtask corpus --all`"); +} + +/// Streaming a PNG's rows a group at a time gives the pixels and metadata `decode` gives, holds +/// only a group at a time, and stops between rows when asked. +#[test] +fn step_stream_gives_the_pixels_and_extras_of_decode() { + for (layout, sample) in [(ChannelLayout::Gray, SampleType::U8), (ChannelLayout::Rgb, SampleType::U16), (ChannelLayout::Rgba, SampleType::U8)] { + let (w, h) = (23u32, 19u32); + let len = w as usize * h as usize * layout.channels() * sample.bytes(); + let mut img = Image::from_raw(w, h, layout, sample, (0..len).map(|i| (i.wrapping_mul(2_654_435_761) >> 11) as u8).collect()).unwrap(); + img.meta.dpi = Some((144.0, 144.0)); + img.meta.text.push(("Comment".into(), "streamed".into())); + img.icc = Some(vec![7; 40]); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let want = decode(&bytes).unwrap(); + for group in [0u32, 1, 4, 19, 64] { + let mut dec = PngDecodeRows::new_streaming(bytes.as_slice(), &Limits::default()).unwrap(); + assert!(dec.streamable()); + assert_eq!(dec.header(), PngHeader { width: w, height: h, layout, sample }); + let (mut data, mut next, mut calls) = (Vec::new(), 0u32, 0); + let extras = loop { + calls += 1; + let mut sink = |y0: u32, rows: &[u8]| -> Result<(), CodecError> { + assert_eq!(y0, next, "{layout:?} group {group}"); + assert!(rows.len() <= group.max(1) as usize * dec_line(w, layout, sample), "a group at most"); + next += (rows.len() / dec_line(w, layout, sample)) as u32; + data.extend_from_slice(rows); + Ok(()) + }; + if let Some(extras) = dec.step_stream(group, &mut || false, &mut sink).unwrap() { + break extras; + } + assert!(calls < 10_000); + }; + assert!(calls > 1, "{layout:?}: stopped between rows"); + assert_eq!(data, want.data(), "{layout:?} group {group}: pixels"); + assert_eq!((extras.icc.as_deref(), extras.meta.dpi, &extras.meta.text), (want.icc.as_deref(), want.meta.dpi, &want.meta.text)); + assert_eq!(extras.orientation, 1); + assert!(dec.step_stream::(group, &mut || true, &mut |_, _| Ok(())).is_err(), "spent"); + } + } + // An interlaced file doesn't stream. + let img = Image::from_raw(9, 9, ChannelLayout::Gray, SampleType::U8, vec![1; 81]).unwrap(); + let bytes = encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap(); + assert!(!PngDecodeRows::new_streaming(bytes.as_slice(), &Limits::default()).unwrap().streamable()); +} + +/// Bytes per row of `w` pixels. +fn dec_line(w: u32, layout: ChannelLayout, sample: SampleType) -> usize { + w as usize * layout.channels() * sample.bytes() +} diff --git a/crates/image-codecs/tests/it/pnm_headers.rs b/crates/image-codecs/tests/it/pnm_headers.rs new file mode 100644 index 000000000..363e941ad --- /dev/null +++ b/crates/image-codecs/tests/it/pnm_headers.rs @@ -0,0 +1,82 @@ +use craft_image_codecs::{ChannelLayout, CodecError, Format, SampleType, decode, decode_as}; + +const PAM_GRAY: &[u8] = b"P7\nWIDTH 1\nHEIGHT 1\nDEPTH 1\nMAXVAL 255\nENDHDR"; +const ENDINGS: &[&[u8]] = &[b"\n", b" \n", b"\t \n", b"\r\n", b" \t\r\n"]; + +#[test] +fn pam_consumes_the_complete_endhdr_line() { + for ending in ENDINGS { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(ending); + bytes.push(0x80); + let img = decode(&bytes).unwrap(); + assert_eq!(img.dimensions(), (1, 1)); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &[0x80], "ENDHDR ending {ending:?}"); + } +} + +#[test] +fn pam_preserves_every_possible_first_raster_byte() { + // In particular, LF, CR, space, tab and '#' must not be skipped as header syntax. + for ending in ENDINGS { + for first in 0..=255u8 { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(ending); + bytes.push(first); + assert_eq!(decode(&bytes).unwrap().data(), &[first], "ENDHDR ending {ending:?}, raster byte {first}"); + } + } +} + +#[test] +fn pam_crlf_header_preserves_u16_samples_and_alpha() { + for ending in ENDINGS { + let mut bytes = b"P7\r\nWIDTH 1\r\nHEIGHT 1\r\nDEPTH 2\r\nMAXVAL 65535\r\nTUPLTYPE GRAYSCALE_ALPHA\r\nENDHDR".to_vec(); + bytes.extend_from_slice(ending); + bytes.extend_from_slice(b" \n#\r"); + let img = decode(&bytes).unwrap(); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.sample_type(), SampleType::U16); + assert_eq!(img.to_u16_samples().unwrap(), vec![0x200a, 0x230d], "ENDHDR ending {ending:?}"); + } +} + +#[test] +fn pam_rejects_unterminated_or_invalid_endhdr_lines() { + for suffix in [b"".as_slice(), b" ", b"\r", b" \t\r", b" \x80", b" \x80\n\x7f", b"#comment\n\x7f"] { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(suffix); + assert!(matches!(decode_as(Format::Pnm, &bytes), Err(CodecError::Malformed { .. })), "ENDHDR suffix {suffix:?}"); + } +} + +#[test] +fn pam_truncation_never_uses_header_whitespace_as_pixels() { + for ending in ENDINGS { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(ending); + bytes.push(0x80); + for cut in 0..bytes.len() { + assert!(decode_as(Format::Pnm, &bytes[..cut]).is_err(), "ENDHDR ending {ending:?}, truncated at {cut}"); + } + } +} + +#[test] +fn binary_pnm_keeps_its_single_whitespace_separator() { + for separator in *b" \t\r\n" { + for first in *b" \t\r\n#" { + for (header, channels) in [(b"P5\n1 1\n255".as_slice(), 1), (b"P6\n1 1\n255", 3)] { + let mut bytes = header.to_vec(); + bytes.push(separator); + bytes.extend(std::iter::repeat_n(first, channels)); + assert_eq!(decode(&bytes).unwrap().data(), vec![first; channels]); + } + let mut bytes = b"P4\n8 1".to_vec(); + bytes.extend_from_slice(&[separator, first]); + let expected: Vec = (0..8).map(|bit| if first & (0x80 >> bit) == 0 { 255 } else { 0 }).collect(); + assert_eq!(decode(&bytes).unwrap().data(), expected); + } + } +} diff --git a/crates/image-codecs/tests/it/resolution.rs b/crates/image-codecs/tests/it/resolution.rs new file mode 100644 index 000000000..fe2c51c8e --- /dev/null +++ b/crates/image-codecs/tests/it/resolution.rs @@ -0,0 +1,546 @@ +//! Resolution recorded in metadata (#1691): a camera JPEG keeps its resolution in EXIF only, and +//! Photoshop opens it at that resolution; Photoshop's own APP13 ResolutionInfo and the XMP +//! `tiff:` properties outrank EXIF, and the JFIF density comes last. Exports write the image's +//! resolution into JFIF, EXIF and XMP alike. Every file here is built by hand. + +use std::borrow::Cow; + +use craft_image_codecs::resolution::{exif_with_resolution, xmp_with_resolution}; +use craft_image_codecs::*; +use proptest::prelude::{any, proptest}; + +const X: u16 = 282; +const Y: u16 = 283; +const UNIT: u16 = 296; +const SHORT: u16 = 3; +const LONG: u16 = 4; +const RATIONAL: u16 = 5; +const ASCII: u16 = 2; + +/// One IFD0 entry: tag, type, count and the value bytes (in the block's byte order). +struct E(u16, u16, u32, Vec); + +struct Exif { + big: bool, +} + +impl Exif { + fn u16(&self, v: u16) -> [u8; 2] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + fn u32(&self, v: u32) -> [u8; 4] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + fn rational(&self, n: u32, d: u32) -> Vec { + [self.u32(n), self.u32(d)].concat() + } + fn short(&self, v: u16) -> Vec { + self.u16(v).to_vec() + } + fn x(&self, n: u32, d: u32) -> E { + E(X, RATIONAL, 1, self.rational(n, d)) + } + fn y(&self, n: u32, d: u32) -> E { + E(Y, RATIONAL, 1, self.rational(n, d)) + } + fn unit(&self, u: u16) -> E { + E(UNIT, SHORT, 1, self.short(u)) + } + fn make(&self) -> E { + E(0x010F, ASCII, 6, b"NIKON\0".to_vec()) + } + fn orientation(&self, o: u16) -> E { + E(0x0112, SHORT, 1, self.short(o)) + } + + /// A TIFF-structured EXIF block: header, IFD0 at 8, then the out-of-line values. + fn build(&self, entries: &[E]) -> Vec { + let mut v = if self.big { b"MM\0*".to_vec() } else { b"II*\0".to_vec() }; + v.extend_from_slice(&self.u32(8)); + v.extend_from_slice(&self.u16(entries.len() as u16)); + let mut data_at = 8 + 2 + entries.len() * 12 + 4; + let mut data = Vec::new(); + for E(tag, ty, count, value) in entries { + v.extend_from_slice(&self.u16(*tag)); + v.extend_from_slice(&self.u16(*ty)); + v.extend_from_slice(&self.u32(*count)); + if value.len() <= 4 { + let mut inline = value.clone(); + inline.resize(4, 0); + v.extend_from_slice(&inline); + } else { + v.extend_from_slice(&self.u32(data_at as u32)); + data.extend_from_slice(value); + data_at += value.len(); + } + } + v.extend_from_slice(&self.u32(0)); + v.extend_from_slice(&data); + v + } +} + +const LE: Exif = Exif { big: false }; +const BE: Exif = Exif { big: true }; + +/// A D3200-style block: Make, XResolution 300/1, YResolution 300/1, ResolutionUnit 2. +fn camera_exif(e: &Exif) -> Vec { + e.build(&[e.make(), e.x(300, 1), e.y(300, 1), e.unit(2)]) +} + +fn segment(marker: u8, payload: &[u8]) -> Vec { + let mut s = vec![0xFF, marker]; + s.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes()); + s.extend_from_slice(payload); + s +} + +fn jfif(unit: u8, x: u16, y: u16) -> Vec { + let mut p = b"JFIF\0\x01\x02".to_vec(); + p.push(unit); + p.extend_from_slice(&x.to_be_bytes()); + p.extend_from_slice(&y.to_be_bytes()); + p.extend_from_slice(&[0, 0]); + segment(0xE0, &p) +} + +fn exif_app1(exif: &[u8]) -> Vec { + segment(0xE1, &[b"Exif\0\0".as_slice(), exif].concat()) +} + +fn xmp_app1(xmp: &str) -> Vec { + segment(0xE1, &[b"http://ns.adobe.com/xap/1.0/\0".as_slice(), xmp.as_bytes()].concat()) +} + +/// An 8BIM resource block holding ResolutionInfo at `ppi` (display unit inch), after an +/// unrelated resource with an odd-length name and an odd-sized body. +fn irb(ppi: f64) -> Vec { + let mut b = b"8BIM".to_vec(); + b.extend_from_slice(&0x0404u16.to_be_bytes()); + b.extend_from_slice(&[3, b'a', b'b', b'c']); + b.extend_from_slice(&3u32.to_be_bytes()); + b.extend_from_slice(&[1, 2, 3, 0]); + b.extend_from_slice(b"8BIM"); + b.extend_from_slice(&0x03EDu16.to_be_bytes()); + b.extend_from_slice(&[0, 0]); + b.extend_from_slice(&16u32.to_be_bytes()); + let fixed = ((ppi * 65536.0).round() as u32).to_be_bytes(); + for _ in 0..2 { + b.extend_from_slice(&fixed); + b.extend_from_slice(&1u16.to_be_bytes()); + b.extend_from_slice(&2u16.to_be_bytes()); + } + b +} + +fn app13(resources: &[u8]) -> Vec { + segment(0xED, &[b"Photoshop 3.0\0".as_slice(), resources].concat()) +} + +/// A 16×8 baseline JPEG whose APPn segments are exactly `segments` (the encoder's own JFIF +/// header is taken out). +fn jpeg_with(segments: &[Vec]) -> Vec { + let img = Image::from_raw(16, 8, ChannelLayout::Gray, SampleType::U8, vec![128; 128]).unwrap(); + let base = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let mut i = 2; + while (0xE0..=0xEF).contains(&base[i + 1]) { + i += 2 + u16::from_be_bytes([base[i + 2], base[i + 3]]) as usize; + } + let mut out = base[..2].to_vec(); + for s in segments { + out.extend_from_slice(s); + } + out.extend_from_slice(&base[i..]); + out +} + +fn dpi_of(bytes: &[u8]) -> Option<(f32, f32)> { + decode(bytes).unwrap().meta.dpi +} + +fn near(got: Option<(f32, f32)>, want: f32) { + let (x, y) = got.unwrap_or_else(|| panic!("no resolution, wanted {want}")); + assert!((x - want).abs() < 0.01 && (y - want).abs() < 0.01, "got {x}×{y}, wanted {want}"); +} + +/// The APPn payloads of a JPEG: (marker, payload). +fn app_segments(b: &[u8]) -> Vec<(u8, Vec)> { + let mut out = Vec::new(); + let mut i = 2; + while i + 4 <= b.len() && b[i] == 0xFF && b[i + 1] != 0xDA { + let len = u16::from_be_bytes([b[i + 2], b[i + 3]]) as usize; + if (0xE0..=0xEF).contains(&b[i + 1]) { + out.push((b[i + 1], b[i + 4..i + 2 + len].to_vec())); + } + i += 2 + len; + } + out +} + +/// (unit, x, y) of a JPEG's JFIF header. +fn jfif_density(b: &[u8]) -> (u8, u16, u16) { + let (_, p) = app_segments(b).into_iter().find(|(m, p)| *m == 0xE0 && p.starts_with(b"JFIF\0")).unwrap(); + (p[7], u16::from_be_bytes([p[8], p[9]]), u16::from_be_bytes([p[10], p[11]])) +} + +fn exif_of(b: &[u8]) -> Vec { + let (_, p) = app_segments(b).into_iter().find(|(m, p)| *m == 0xE1 && p.starts_with(b"Exif\0\0")).unwrap(); + p[6..].to_vec() +} + +#[test] +fn exif_only_camera_jpeg_opens_at_its_exif_resolution() { + for e in [&LE, &BE] { + near(dpi_of(&jpeg_with(&[exif_app1(&camera_exif(e))])), 300.0); + // ResolutionUnit absent: inch, the EXIF default. + near(dpi_of(&jpeg_with(&[exif_app1(&e.build(&[e.x(300, 1), e.y(300, 1)]))])), 300.0); + // Non-unit denominators. + near(dpi_of(&jpeg_with(&[exif_app1(&e.build(&[e.x(3_000_000, 10_000), e.y(1200, 4), e.unit(2)]))])), 300.0); + } +} + +#[test] +fn exif_resolution_in_centimetres_is_converted() { + for e in [&LE, &BE] { + let exif = e.build(&[e.x(11811, 100), e.y(11811, 100), e.unit(3)]); + near(exif_resolution(&exif), 299.9994); + near(dpi_of(&jpeg_with(&[exif_app1(&exif)])), 299.9994); + } +} + +#[test] +fn jfif_aspect_ratio_only_with_exif_uses_exif() { + near(dpi_of(&jpeg_with(&[jfif(0, 1, 1), exif_app1(&camera_exif(&LE))])), 300.0); +} + +#[test] +fn exif_outranks_a_default_jfif_density() { + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&camera_exif(&LE))])), 300.0); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&camera_exif(&BE))])), 300.0); + // JFIF alone still counts, in either unit. + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72)])), 72.0); + near(dpi_of(&jpeg_with(&[jfif(2, 100, 100)])), 254.0); + assert_eq!(dpi_of(&jpeg_with(&[jfif(0, 1, 1)])), None); +} + +#[test] +fn photoshop_resolution_info_wins_then_xmp_then_exif() { + let xmp = r#""#; + let exif = exif_app1(&camera_exif(&LE)); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif.clone(), xmp_app1(xmp), app13(&irb(240.0))])), 240.0); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif.clone(), xmp_app1(xmp)])), 200.0); + // A fractional Photoshop resolution survives the 16.16 fixed point. + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif, app13(&irb(299.5))])), 299.5); + near(photoshop_resolution(&irb(150.0)), 150.0); + // Element form, centimetres. + let elem = "100/11003"; + near(xmp_resolution(elem), 254.0); +} + +#[test] +fn unusable_exif_falls_back_to_jfif() { + let e = &LE; + let bad = [ + // No absolute unit. + e.build(&[e.x(300, 1), e.y(300, 1), e.unit(1)]), + // Unknown unit, malformed unit entry. + e.build(&[e.x(300, 1), e.y(300, 1), e.unit(9)]), + e.build(&[e.x(300, 1), e.y(300, 1), E(UNIT, LONG, 1, e.u32(2).to_vec())]), + // Zero denominator, zero value, wrong type, wrong count. + e.build(&[e.x(300, 0), e.y(300, 1)]), + e.build(&[e.x(0, 1), e.y(300, 1)]), + e.build(&[E(X, SHORT, 1, e.short(300)), e.y(300, 1)]), + e.build(&[E(X, RATIONAL, 2, [e.rational(300, 1), e.rational(300, 1)].concat()), e.y(300, 1)]), + // Only one axis. + e.build(&[e.x(300, 1)]), + ]; + for exif in &bad { + assert_eq!(exif_resolution(exif), None); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(exif)])), 72.0); + } + // A value offset pointing past the end. + let mut far = camera_exif(e); + let at = 8 + 2 + 12 + 8; // XResolution entry's value field + far[at..at + 4].copy_from_slice(&e.u32(60_000)); + assert_eq!(exif_resolution(&far), None); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&far)])), 72.0); +} + +#[test] +fn truncated_metadata_never_panics_and_falls_back() { + let exif = camera_exif(&BE); + for n in 0..exif.len() { + let _ = exif_resolution(&exif[..n]); + let _ = exif_with_resolution(&exif[..n], (240.0, 240.0)); + let dpi = dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&exif[..n])])); + assert!(dpi == Some((72.0, 72.0)) || dpi == Some((300.0, 300.0)), "{n}: {dpi:?}"); + } + let r = irb(300.0); + for n in 0..r.len() { + assert_eq!(photoshop_resolution(&r[..n]), None, "{n}"); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), app13(&r[..n])])), 72.0); + } + for x in ["tiff:XResolution=", "tiff:XResolution=\"", "tiff:XResolution=\"\" tiff:YResolution=\"5/0\"", "", "9<"] { + assert_eq!(xmp_resolution(x), None, "{x}"); + let _ = xmp_with_resolution(x, (1.0, 1.0)); + } + + // Property-like text repeated inside the packet must not trigger a full-suffix rescan + // per occurrence (it used to be quadratic). With the cursor advancing past each located + // value, this completes well under a second even with many decoys; a timeout here would + // indicate the quadratic scan returned. A clean packet still extracts correctly. + let start = std::time::Instant::now(); + let decoys = " tiff:ResolutionUnit>".repeat(50_000); + let _ = xmp_resolution(&decoys); + assert!(start.elapsed() < std::time::Duration::from_secs(2), "xmp scan took too long: {:?}", start.elapsed()); + let clean = "300/1300/12"; + near(xmp_resolution(clean), 300.0); +} + +proptest! { + #[test] + fn hostile_bytes_never_panic(bytes in proptest::collection::vec(any::(), 0..256), ppi in 0.0f32..2e6) { + let _ = exif_resolution(&bytes); + let _ = exif_with_resolution(&bytes, (ppi, ppi)); + let _ = photoshop_resolution(&bytes); + let s = String::from_utf8_lossy(&bytes); + let _ = xmp_resolution(&s); + let _ = xmp_with_resolution(&s, (ppi, ppi)); + } + + #[test] + fn mutated_exif_never_panics(i in 0usize..80, v in any::(), ppi in 1.0f32..10_000.0) { + let mut exif = camera_exif(&LE); + if let Some(b) = exif.get_mut(i) { *b = v; } + let _ = exif_resolution(&exif); + let fixed = exif_with_resolution(&exif, (ppi, ppi)); + let _ = exif_resolution(&fixed); + let _ = exif_orientation(&fixed); + } +} + +#[test] +fn rewriting_keeps_every_other_byte() { + for e in [&LE, &BE] { + let src = e.build(&[e.make(), e.orientation(6), e.x(300, 1), e.y(300, 1), e.unit(3)]); + let out = exif_with_resolution(&src, (240.0, 240.0)).into_owned(); + assert_eq!(out.len(), src.len()); + near(exif_resolution(&out), 240.0); + let want = e.build(&[e.make(), e.orientation(6), e.x(240, 1), e.y(240, 1), e.unit(2)]); + assert_eq!(out, want); + // Fractions keep three decimals. + near(exif_resolution(&exif_with_resolution(&src, (299.5, 72.25))).map(|(x, _)| (x, x)), 299.5); + // Nothing to change: borrowed. + assert!(matches!(exif_with_resolution(&camera_exif(e), (300.0, 300.0)), Cow::Borrowed(_))); + let no_res = e.build(&[e.make()]); + assert!(matches!(exif_with_resolution(&no_res, (240.0, 240.0)), Cow::Borrowed(_))); + // With the JPEG prefix. + let prefixed = [b"Exif\0\0".as_slice(), &src].concat(); + assert_eq!(exif_with_resolution(&prefixed, (240.0, 240.0)).as_ref(), [b"Exif\0\0".as_slice(), &want].concat()); + } +} + +#[test] +fn entries_that_cannot_be_rewritten_are_removed() { + for e in [&LE, &BE] { + // XResolution as a SHORT and a LONG unit: both go, the rest stays readable. + let src = e.build(&[e.make(), E(X, SHORT, 1, e.short(300)), e.y(300, 1), E(UNIT, LONG, 1, e.u32(2).to_vec()), e.orientation(6)]); + let out = exif_with_resolution(&src, (240.0, 240.0)).into_owned(); + assert_eq!(out.len(), src.len()); + let count = if e.big { u16::from_be_bytes([out[8], out[9]]) } else { u16::from_le_bytes([out[8], out[9]]) }; + assert_eq!(count, 3); + assert_eq!(exif_orientation(&out), 6); + // Only YResolution is left (240): no X, so no resolution at all, and no stale 300. + assert_eq!(exif_resolution(&out), None); + let tags: Vec = (0..3) + .map(|k| { + let at = 10 + k * 12; + if e.big { u16::from_be_bytes([out[at], out[at + 1]]) } else { u16::from_le_bytes([out[at], out[at + 1]]) } + }) + .collect(); + assert_eq!(tags, vec![0x010F, Y, 0x0112]); + // The next-IFD pointer followed the table, the freed entries are zero. + assert_eq!(&out[10 + 36..10 + 40], &[0, 0, 0, 0]); + assert!(out[10 + 40..10 + 64].iter().all(|&b| b == 0)); + // The Make string (out of line) did not move. + assert!(out.windows(6).any(|w| w == b"NIKON\0")); + } +} + +#[test] +fn xmp_resolution_is_rewritten() { + let attr = r#""#; + assert_eq!( + xmp_with_resolution(attr, (240.0, 240.0)), + r#""# + ); + let elem = "300/1300/1"; + assert_eq!(xmp_with_resolution(elem, (72.5, 72.5)), "72500/100072500/1000"); + for same in [r#"tiff:XResolution="240" tiff:YResolution="240/1""#, ""] { + assert!(matches!(xmp_with_resolution(same, (240.0, 240.0)), Cow::Borrowed(_)), "{same}"); + } +} + +#[test] +fn jpeg_export_writes_one_resolution_everywhere() { + for e in [&LE, &BE] { + let xmp = r#""#; + let src = jpeg_with(&[jfif(1, 72, 72), exif_app1(&camera_exif(e)), xmp_app1(xmp)]); + let mut img = decode(&src).unwrap(); + near(img.meta.dpi, 300.0); + img.meta.dpi = Some((240.0, 240.0)); + let out = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!(jfif_density(&out), (1, 240, 240)); + near(exif_resolution(&exif_of(&out)), 240.0); + let (_, x) = app_segments(&out).into_iter().find(|(m, p)| *m == 0xE1 && p.starts_with(b"http://ns.adobe.com/xap/1.0/\0")).unwrap(); + near(xmp_resolution(std::str::from_utf8(&x).unwrap()), 240.0); + near(dpi_of(&out), 240.0); + // The rest of the EXIF came along. + assert!(exif_of(&out).windows(6).any(|w| w == b"NIKON\0")); + } +} + +#[test] +fn other_formats_take_the_exif_resolution_when_they_have_none() { + let mut img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + img.meta.exif = Some(camera_exif(&LE)); + for fmt in [Format::WebP, Format::Png] { + // Written at 240 with the 300 ppi EXIF: the EXIF follows the image. + img.meta.dpi = Some((240.0, 240.0)); + let out = encode(&img, fmt, &EncodeOptions::default()).unwrap(); + let back = decode(&out).unwrap(); + near(back.meta.dpi, 240.0); + near(back.meta.exif.as_deref().and_then(exif_resolution), 240.0); + // Written without a resolution: the EXIF one is read back. + img.meta.dpi = None; + let out = encode(&img, fmt, &EncodeOptions::default()).unwrap(); + near(decode(&out).unwrap().meta.dpi, 300.0); + } +} + +fn crc32(data: &[u8]) -> u32 { + let mut c = !0u32; + for &b in data { + c ^= u32::from(b); + for _ in 0..8 { + c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 }; + } + } + !c +} + +#[test] +fn png_phys_outranks_its_exif() { + // eXIf says 300 ppi, then a pHYs chunk of 150 ppi (5906 px/m) goes in after IHDR. + let mut img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + img.meta.exif = Some(camera_exif(&LE)); + let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + near(decode(&png).unwrap().meta.dpi, 300.0); + near(decode(&with_phys(&png, 5906)).unwrap().meta.dpi, 150.0); +} + +/// `png` with a `pHYs` chunk of `ppm` pixels per metre after IHDR. +fn with_phys(png: &[u8], ppm: u32) -> Vec { + let mut body = b"pHYs".to_vec(); + body.extend_from_slice(&ppm.to_be_bytes()); + body.extend_from_slice(&ppm.to_be_bytes()); + body.push(1); + let mut chunk = 9u32.to_be_bytes().to_vec(); + chunk.extend_from_slice(&body); + chunk.extend_from_slice(&crc32(&body).to_be_bytes()); + let ihdr_end = 8 + 8 + 13 + 4; + [&png[..ihdr_end], &chunk, &png[ihdr_end..]].concat() +} + +#[test] +fn png_phys_of_a_whole_ppi_reads_back_whole() { + // pHYs holds whole pixels per metre: 300 ppi is stored as 11811 (299.9994 ppi), 72 as 2835 + // (72.009). The whole number that rounds to the stored value is read, as Photoshop does; a + // value no whole ppi rounds to stays as it is. + let img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + let png = encode(&img, Format::Png, &EncodeOptions { embed_metadata: false, ..Default::default() }).unwrap(); + for (ppm, want) in [(11811, 300.0), (2835, 72.0), (3937, 100.0), (11812, 300.0248)] { + let (x, y) = decode(&with_phys(&png, ppm)).unwrap().meta.dpi.unwrap(); + assert!((x - want).abs() < 1e-4 && (y - want).abs() < 1e-4, "{ppm} px/m: {x} × {y}, wanted {want}"); + } + // And a 300 ppi PNG written by the encoder opens at exactly 300. + let mut img = img; + img.meta.dpi = Some((300.0, 300.0)); + let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&png).unwrap().meta.dpi, Some((300.0, 300.0))); +} + +/// `camera_exif` plus IFD1, the embedded thumbnail a camera (or the last editor) wrote: +/// JPEGInterchangeFormat / JPEGInterchangeFormatLength naming a fake JPEG after the IFD. +fn exif_with_thumbnail(e: &Exif) -> Vec { + let mut v = camera_exif(e); + let ifd1 = v.len() as u32; + // IFD0 (4 entries) ends with its next-IFD pointer. + v[8 + 2 + 4 * 12..8 + 2 + 4 * 12 + 4].copy_from_slice(&e.u32(ifd1)); + let thumb = b"\xFF\xD8stale thumbnail\xFF\xD9"; + let thumb_at = ifd1 + 2 + 2 * 12 + 4; + v.extend_from_slice(&e.u16(2)); + for (tag, value) in [(0x0201u16, thumb_at), (0x0202, thumb.len() as u32)] { + v.extend_from_slice(&e.u16(tag)); + v.extend_from_slice(&e.u16(LONG)); + v.extend_from_slice(&e.u32(1)); + v.extend_from_slice(&e.u32(value)); + } + v.extend_from_slice(&e.u32(0)); + v.extend_from_slice(thumb); + v +} + +fn next_ifd(e: &Exif, exif: &[u8]) -> u32 { + let at = 8 + 2 + 4 * 12; + let b: [u8; 4] = exif[at..at + 4].try_into().unwrap(); + if e.big { u32::from_be_bytes(b) } else { u32::from_le_bytes(b) } +} + +/// #2147 / #2373: an export must not carry the source's EXIF thumbnail (IFD1). Finder, Android +/// and upload pickers show it instead of the image, so an edited photo kept its old thumbnail. +#[test] +fn exports_drop_the_source_exif_thumbnail() { + for e in [&LE, &BE] { + let exif = exif_with_thumbnail(e); + assert_ne!(next_ifd(e, &exif), 0); + for ppi in [None, Some((300.0, 300.0)), Some((240.0, 240.0))] { + let out = export_exif(&exif, ppi); + assert_eq!(next_ifd(e, &out), 0, "IFD1 is unlinked ({ppi:?})"); + assert_eq!(out.len(), exif.len(), "nothing moves"); + assert!(out.windows(6).any(|w| w == b"NIKON\0"), "the rest of the EXIF is kept"); + near(exif_resolution(&out), ppi.map_or(300.0, |p| p.0)); + } + // Only the pointer changes. + let out = export_exif(&exif, None); + let differing: Vec = (0..exif.len()).filter(|&i| exif[i] != out[i]).collect(); + assert!(differing.iter().all(|&i| (8 + 2 + 4 * 12..8 + 2 + 4 * 12 + 4).contains(&i)), "{differing:?}"); + // With a JPEG-style prefix too. + let prefixed = [b"Exif\0\0".as_slice(), &exif].concat(); + assert_eq!(next_ifd(e, &export_exif(&prefixed, None)[6..]), 0); + // An EXIF without a thumbnail comes through untouched. + assert!(matches!(export_exif(&camera_exif(e), None), Cow::Borrowed(_))); + // Through every encoder that writes EXIF. + let mut img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + img.meta.exif = Some(exif.clone()); + let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!(next_ifd(e, &exif_of(&jpeg)), 0, "JPEG"); + for format in [Format::Png, Format::WebP] { + let back = decode(&encode(&img, format, &EncodeOptions::default()).unwrap()).unwrap(); + let got = back.meta.exif.unwrap_or_else(|| panic!("{format:?} kept no EXIF")); + let body = got.strip_prefix(b"Exif\0\0").unwrap_or(&got); + assert_eq!(next_ifd(e, body), 0, "{format:?}"); + } + } +} + +#[test] +fn thumbnail_removal_never_panics_on_truncated_exif() { + for e in [&LE, &BE] { + let exif = exif_with_thumbnail(e); + for n in 0..exif.len() { + let _ = export_exif(&exif[..n], None); + let _ = export_exif(&exif[..n], Some((300.0, 300.0))); + } + } +} diff --git a/crates/image-codecs/tests/it/roundtrip.rs b/crates/image-codecs/tests/it/roundtrip.rs new file mode 100644 index 000000000..c9b179bc8 --- /dev/null +++ b/crates/image-codecs/tests/it/roundtrip.rs @@ -0,0 +1,348 @@ +//! Round-trip every writable format × every sample type × every layout. +//! Lossless formats must reproduce the (planned) conversion of the input +//! exactly; lossy formats must stay above a PSNR threshold. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +/// Minimum PSNR (dB) for lossy formats at default options. +fn psnr_threshold(format: Format) -> f64 { + match format { + Format::Jpeg => 30.0, + Format::Gif => 20.0, + Format::Hdr => 40.0, + Format::Avif => 28.0, + _ => f64::INFINITY, + } +} + +fn check_roundtrip(format: Format, layout: ChannelLayout, sample: SampleType) { + let c = caps(format); + if !c.write { + return; + } + // GIF quantizes; keep its input smooth so the threshold is meaningful. + let img = if format == Format::Gif { + synth(37, 23, layout, sample, 7, 0.0) + } else if c.lossy { + synth(37, 23, layout, sample, 7, 0.02) + } else { + test_image(layout, sample) + }; + let opts = EncodeOptions::default(); + let bytes = encode(&img, format, &opts).unwrap_or_else(|e| panic!("{format:?} {layout:?} {sample:?}: {e}")); + assert_eq!(detect(&bytes), Some(format), "detect after encode"); + if !c.read { + return; + } + let back = decode(&bytes).unwrap_or_else(|e| panic!("{format:?} {layout:?} {sample:?} decode: {e}")); + assert_eq!(back.dimensions(), img.dimensions()); + + // Native storage must be preserved when the format declares support. + let float_pnm_special = format == Format::Pnm && sample.is_float(); + if c.layouts.contains(&layout) && c.depths.contains(&sample) && !float_pnm_special { + assert_eq!(back.layout(), layout, "{format:?} must keep layout"); + assert_eq!(back.sample_type(), sample, "{format:?} must keep sample type"); + } + if c.depths.contains(&sample) && !float_pnm_special { + assert_eq!(back.sample_type(), sample, "{format:?} must keep supported depth"); + } + // Integer sources never lose precision in formats offering that depth. + if sample == SampleType::U16 && c.depths.contains(&SampleType::U16) { + assert!(matches!(back.sample_type(), SampleType::U16 | SampleType::F32)); + } + + let expected = img.convert(back.layout(), back.sample_type()); + let mut expected_cmp = expected.clone(); + if format == Format::Gif && layout.has_alpha() { + // GIF alpha is binary: compare colour only on opaque pixels. + let ch = back.layout().channels(); + let mut e = expected.to_normalized(); + let b = back.to_normalized(); + for (pe, pb) in e.chunks_mut(ch).zip(b.chunks(ch)) { + if pb[ch - 1] == 0.0 { + pe.copy_from_slice(pb); + } + pe[ch - 1] = pb[ch - 1]; + } + expected_cmp = Image::from_normalized(img.width(), img.height(), back.layout(), back.sample_type(), &e).unwrap(); + } + + if c.lossy { + let p = psnr(&expected_cmp, &back); + assert!(p >= psnr_threshold(format), "{format:?} {layout:?} {sample:?}: PSNR {p:.2} dB"); + } else { + assert_eq!( + expected_cmp.data(), + back.data(), + "{format:?} {layout:?} {sample:?}: lossless round-trip mismatch (max diff {})", + max_abs_diff(&expected_cmp, &back) + ); + } +} + +macro_rules! cases { + ($fmt:expr) => { + use super::*; + cases!(@ $fmt; + gray_u8: Gray, U8; gray_u16: Gray, U16; gray_f16: Gray, F16; gray_f32: Gray, F32; + graya_u8: GrayA, U8; graya_u16: GrayA, U16; graya_f16: GrayA, F16; graya_f32: GrayA, F32; + rgb_u8: Rgb, U8; rgb_u16: Rgb, U16; rgb_f16: Rgb, F16; rgb_f32: Rgb, F32; + rgba_u8: Rgba, U8; rgba_u16: Rgba, U16; rgba_f16: Rgba, F16; rgba_f32: Rgba, F32; + cmyk_u8: Cmyk, U8; cmyk_u16: Cmyk, U16; cmyk_f16: Cmyk, F16; cmyk_f32: Cmyk, F32; + cmyka_u8: CmykA, U8; cmyka_u16: CmykA, U16; cmyka_f16: CmykA, F16; cmyka_f32: CmykA, F32 + ); + }; + (@ $fmt:expr; $($name:ident : $l:ident, $s:ident);*) => { + $( + #[test] + fn $name() { + check_roundtrip($fmt, ChannelLayout::$l, SampleType::$s); + } + )* + }; +} + +mod png { + cases!(Format::Png); +} +mod jpeg { + cases!(Format::Jpeg); +} +mod tiff { + cases!(Format::Tiff); +} +mod webp { + cases!(Format::WebP); +} +mod gif { + cases!(Format::Gif); +} +mod bmp { + cases!(Format::Bmp); +} +mod tga { + cases!(Format::Tga); +} +mod ico { + cases!(Format::Ico); +} +mod pnm { + cases!(Format::Pnm); +} +mod fits { + cases!(Format::Fits); +} +mod dpx { + cases!(Format::Dpx); +} +mod dds { + cases!(Format::Dds); +} +mod qoi { + cases!(Format::Qoi); +} +mod exr { + cases!(Format::OpenExr); +} +mod hdr { + cases!(Format::Hdr); +} + +// --------------------------------------------------------------------------- +// Targeted round-trips beyond the matrix +// --------------------------------------------------------------------------- + +/// Float formats must keep HDR values (> 1, and negative where allowed). +#[test] +fn hdr_values_survive_float_formats() { + let vals: Vec = (0..16 * 8 * 3).map(|i| (i as f32 - 40.0) * 0.37).collect(); + let img = Image::from_f32(16, 8, ChannelLayout::Rgb, &vals).unwrap(); + for f in [Format::Tiff, Format::OpenExr, Format::Pnm] { + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.to_f32_samples().unwrap(), vals, "{f:?}"); + } +} + +#[test] +fn hdr_format_keeps_bright_values_approximately() { + let vals: Vec = (0..16 * 8 * 3).map(|i| 0.01 + i as f32 * 0.25).collect(); + let img = Image::from_f32(16, 8, ChannelLayout::Rgb, &vals).unwrap(); + let back = decode(&encode(&img, Format::Hdr, &EncodeOptions::default()).unwrap()).unwrap(); + // RGBE shares one exponent per pixel: error is relative to the brightest channel. + let got = back.to_f32_samples().unwrap(); + for (pa, pb) in vals.chunks(3).zip(got.chunks(3)) { + let m = pa.iter().copied().fold(0.0, f32::max); + for (a, b) in pa.iter().zip(pb) { + assert!((a - b).abs() <= m * 0.01, "{a} vs {b}"); + } + } +} + +#[test] +fn f16_exr_is_bit_exact() { + let vals: Vec = (0..20 * 10 * 4).map(|i| f16::from_f32(i as f32 * 0.013 - 1.0)).collect(); + let img = Image::from_f16(20, 10, ChannelLayout::Rgba, &vals).unwrap(); + for comp in [ExrCompression::None, ExrCompression::Rle, ExrCompression::Zip1, ExrCompression::Zip16, ExrCompression::Piz] { + let opts = EncodeOptions { exr_compression: comp, ..Default::default() }; + let back = decode(&encode(&img, Format::OpenExr, &opts).unwrap()).unwrap(); + assert_eq!(back.to_f16_samples().unwrap(), vals, "{comp:?}"); + } +} + +#[test] +fn tiff_all_compressions_lossless() { + for layout in ChannelLayout::ALL { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let img = test_image(layout, sample); + for comp in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { + let opts = EncodeOptions { tiff_compression: comp, ..Default::default() }; + let back = decode(&encode(&img, Format::Tiff, &opts).unwrap()).unwrap(); + if layout.is_lab() && sample != SampleType::U8 { + // CIELab holds a* and b* as 256·a at 16 bits and has no float form: 16-bit and + // float Lab come back within one 16-bit step (the PSD's 16-bit Lab does the same). + let (a, b) = (img.to_normalized(), back.convert(layout, sample).to_normalized()); + let worst = a.iter().zip(&b).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max); + assert!(worst <= 1.5 / 65535.0, "{layout:?} {sample:?} {comp:?}: {worst}"); + continue; + } + assert_eq!(back, img.clone().with_meta(back.meta.clone()), "{layout:?} {sample:?} {comp:?}"); + } + } + } +} + +#[test] +fn compressed_tiff_does_not_keep_the_uncompressed_reservation() { + // The encoder reserves the uncompressed size up front for an uncompressed file (#6607). A + // smooth image compresses to a fraction of it, and no returned file holds much spare room. + let (w, h) = (512u32, 512u32); + let px: Vec = (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x / 2) as u8, (y / 2) as u8, 128])).collect(); + let img = Image::from_u8(w, h, ChannelLayout::Rgb, px).unwrap(); + for comp in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate] { + let v = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: comp, ..Default::default() }).unwrap(); + if !matches!(comp, TiffCompression::None) { + assert!(v.len() < img.data().len() / 2, "{comp:?}: the image should compress well ({} bytes)", v.len()); + } + assert!(v.capacity() - v.len() <= v.len() / 8, "{comp:?}: {} bytes kept for a {} byte file", v.capacity(), v.len()); + assert_eq!(decode(&v).unwrap().data(), img.data(), "{comp:?}"); + } +} + +#[test] +fn png_all_compressions_lossless() { + let img = test_image(ChannelLayout::Rgba, SampleType::U16); + for comp in [PngCompression::None, PngCompression::Fast, PngCompression::Default, PngCompression::Best] { + let opts = EncodeOptions { png_compression: comp, ..Default::default() }; + let back = decode(&encode(&img, Format::Png, &opts).unwrap()).unwrap(); + assert_eq!(back.data(), img.data(), "{comp:?}"); + } +} + +#[test] +fn jpeg_quality_affects_size_and_error() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let lo = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 20, ..Default::default() }).unwrap(); + let hi = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 98, ..Default::default() }).unwrap(); + assert!(lo.len() < hi.len()); + let plo = psnr(&img, &decode(&lo).unwrap()); + let phi = psnr(&img, &decode(&hi).unwrap()); + assert!(phi > plo, "{phi} <= {plo}"); +} + +#[test] +fn jpeg_444_beats_420_on_chroma_detail() { + let img = synth(64, 64, ChannelLayout::Rgb, SampleType::U8, 3, 0.5); + let a = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_chroma_subsampling: false, jpeg_quality: 95, ..Default::default() }).unwrap(); + let b = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_chroma_subsampling: true, jpeg_quality: 95, ..Default::default() }).unwrap(); + assert!(psnr(&img, &decode(&a).unwrap()) > psnr(&img, &decode(&b).unwrap())); +} + +/// JPEG Options › Format Options: each scan layout decodes back to the same image, Progressive +/// writes a progressive frame (SOF2), and Baseline Optimized fits its Huffman tables (smaller at +/// 4:4:4; at 4:2:0 it keeps the standard tables, see `JpegFormat::Optimized`). +#[test] +fn jpeg_format_options_round_trip() { + let img = synth(64, 48, ChannelLayout::Rgb, SampleType::U8, 3, 0.5); + let sof = |b: &[u8], m: u8| b.windows(2).any(|w| w == [0xFF, m]); + for subsampled in [false, true] { + let enc = |f| { + encode(&img, Format::Jpeg, &EncodeOptions { jpeg_format: f, jpeg_chroma_subsampling: subsampled, jpeg_quality: 90, ..Default::default() }).unwrap() + }; + let (base, opt, prog) = (enc(JpegFormat::Baseline), enc(JpegFormat::Optimized), enc(JpegFormat::Progressive)); + // The scan layout only changes how the same quantized coefficients are stored, so every + // format decodes as faithfully as Baseline. + let reference = psnr(&img, &decode(&base).unwrap()); + for (name, b) in [("optimized", &opt), ("progressive", &prog)] { + let back = decode(b).unwrap(); + assert_eq!(back.dimensions(), img.dimensions(), "{name}"); + let p = psnr(&img, &back); + assert!((p - reference).abs() < 0.5, "{name}, subsampled {subsampled}: {p} vs baseline {reference}"); + } + assert!(sof(&base, 0xC0) && !sof(&base, 0xC2)); + assert!(sof(&prog, 0xC2), "progressive frame"); + if subsampled { + assert_eq!(opt, base, "4:2:0 keeps the standard tables"); + } else { + assert!(opt.len() < base.len(), "fitted tables are smaller: {} vs {}", opt.len(), base.len()); + } + } + // Gray is 4:4:4 too, so Baseline Optimized shrinks it. + let gray = synth(64, 48, ChannelLayout::Gray, SampleType::U8, 3, 0.5); + let enc = |f| encode(&gray, Format::Jpeg, &EncodeOptions { jpeg_format: f, ..Default::default() }).unwrap(); + let (base, opt) = (enc(JpegFormat::Baseline), enc(JpegFormat::Optimized)); + assert!(opt.len() < base.len(), "gray: {} vs {}", opt.len(), base.len()); + assert!((psnr(&gray, &decode(&opt).unwrap()) - psnr(&gray, &decode(&base).unwrap())).abs() < 0.5); + assert_eq!(JpegFormat::parse("progressive"), Some(JpegFormat::Progressive)); + assert_eq!(JpegFormat::parse(JpegFormat::Optimized.name()), Some(JpegFormat::Optimized)); + for bad in ["Progressive", "", "standard"] { + assert_eq!(JpegFormat::parse(bad), None, "{bad}"); + } +} + +#[test] +fn webp_lossy_request_is_a_lossy_file() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let bytes = encode(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).unwrap(); + assert_eq!(&bytes[12..16], b"VP8 ", "a bare lossy frame"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.dimensions(), img.dimensions()); + assert!( + fidelity_warnings_with(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).contains(&FidelityWarning::LossyCompression) + ); +} + +#[test] +fn one_pixel_images_roundtrip() { + for f in rw_formats() { + let img = synth(1, 1, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.dimensions(), (1, 1), "{f:?}"); + } +} + +#[test] +fn ico_rejects_oversize() { + let img = Image::new(300, 10, ChannelLayout::Rgba, SampleType::U8).unwrap(); + assert!(matches!(encode(&img, Format::Ico, &EncodeOptions::default()), Err(CodecError::Encode { .. }))); + let img = Image::new(256, 256, ChannelLayout::Rgba, SampleType::U8).unwrap(); + assert!(encode(&img, Format::Ico, &EncodeOptions::default()).is_ok()); +} + +#[test] +fn empty_image_rejected() { + let img = Image::new(0, 5, ChannelLayout::Rgb, SampleType::U8).unwrap(); + for f in writable_formats() { + assert!(encode(&img, f, &EncodeOptions::default()).is_err(), "{f:?}"); + } +} + +#[test] +fn larger_image_png_tiff_exact() { + let img = synth(513, 257, ChannelLayout::Rgba, SampleType::U16, 99, 1.0); + for f in [Format::Png, Format::Tiff, Format::Pnm] { + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.data(), img.data(), "{f:?}"); + } +} diff --git a/crates/image-codecs/tests/it/source_settings.rs b/crates/image-codecs/tests/it/source_settings.rs new file mode 100644 index 000000000..07b20471a --- /dev/null +++ b/crates/image-codecs/tests/it/source_settings.rs @@ -0,0 +1,111 @@ +//! Reading back the settings a lossy file was saved with: a JPEG's quality from its +//! quantisation tables, a WebP's lossy or lossless encoding (and its quality). + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn photo(w: u32, h: u32, layout: ChannelLayout) -> Image { + synth(w, h, layout, SampleType::U8, 5, 0.03) +} + +#[test] +fn jpeg_quality_is_estimated_within_two() { + for layout in [ChannelLayout::Rgb, ChannelLayout::Gray] { + let img = photo(40, 24, layout); + for q in [1u8, 5, 10, 25, 40, 50, 60, 70, 75, 80, 85, 90, 95, 98, 100] { + let bytes = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: q, ..Default::default() }).unwrap(); + let est = jpeg_quality(&bytes).unwrap_or_else(|| panic!("{layout:?} q{q}: no estimate")); + assert!(est.abs_diff(q) <= 2, "{layout:?}: saved at {q}, estimated {est}"); + } + } +} + +#[test] +fn jpeg_quality_of_hand_built_tables() { + // A minimal JPEG prefix: SOI, one DQT holding the Annex K luminance table (quality 50) in + // zig-zag order, then SOS. + let luma: [u8; 64] = [ + 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, 72, 92, 78, + 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, + ]; + let mut b = vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x00]; + b.extend_from_slice(&luma); + b.extend_from_slice(&[0xFF, 0xDA, 0, 2]); + assert_eq!(jpeg_quality(&b), Some(50)); +} + +#[test] +fn hostile_jpeg_tables_give_no_estimate_and_never_panic() { + let img = photo(16, 16, ChannelLayout::Rgb); + let good = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 70, ..Default::default() }).unwrap(); + // Every truncation of a real file. + for n in 0..good.len().min(800) { + let _ = jpeg_quality(&good[..n]); + } + let cases: Vec> = vec![ + vec![], + vec![0xFF], + vec![0xFF, 0xD8], + // A DQT whose length runs past the end. + vec![0xFF, 0xD8, 0xFF, 0xDB, 0xFF, 0xFF, 0x00, 1, 2, 3], + // A DQT of length 0 and 1 (less than its own length field). + vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x00], + vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x01], + // A table cut short, a bad precision, a slot past 3, a 16-bit table cut mid-entry. + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 12, 0x00], vec![9; 9]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x70], vec![9; 64]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x0F], vec![9; 64]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 68, 0x10], vec![0xFF; 65]].concat(), + // All-zero and all-0xFF tables. + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x00], vec![0; 64]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 131, 0x10], vec![0xFF; 128]].concat(), + vec![0xFF; 64], + ]; + for c in &cases { + let _ = jpeg_quality(c); + } + assert_eq!(jpeg_quality(&[0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x00]), None); + assert_eq!(jpeg_quality(b"not a jpeg at all"), None); + let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(jpeg_quality(&png), None); +} + +#[test] +fn webp_lossless_and_lossy_are_told_apart() { + let img = photo(33, 21, ChannelLayout::Rgb); + let lossless = encode(&img, Format::WebP, &EncodeOptions::default()).unwrap(); + assert_eq!(webp_encoding(&lossless), Some(WebpEncoding::Lossless)); + for q in [10u8, 30, 50, 75, 80, 90, 100] { + let bytes = encode(&img, Format::WebP, &EncodeOptions { webp_lossless: false, webp_quality: q, ..Default::default() }).unwrap(); + let Some(WebpEncoding::Lossy { quality: Some(est) }) = webp_encoding(&bytes) else { panic!("q{q}: {:?}", webp_encoding(&bytes)) }; + assert!(est.abs_diff(q) <= 2, "lossy WebP saved at {q}, estimated {est}"); + } + // With alpha and metadata (the extended container). + let rgba = photo(20, 20, ChannelLayout::Rgba); + let bytes = encode(&rgba, Format::WebP, &EncodeOptions { webp_lossless: false, webp_quality: 60, ..Default::default() }).unwrap(); + assert!(matches!(webp_encoding(&bytes), Some(WebpEncoding::Lossy { quality: Some(58..=62) })), "{:?}", webp_encoding(&bytes)); +} + +#[test] +fn hostile_webp_never_panics() { + let img = photo(24, 24, ChannelLayout::Rgb); + for opts in [EncodeOptions::default(), EncodeOptions { webp_lossless: false, ..Default::default() }] { + let good = encode(&img, Format::WebP, &opts).unwrap(); + for n in 0..good.len().min(400) { + let _ = webp_encoding(&good[..n]); + } + let mut flipped = good.clone(); + for i in 12..flipped.len().min(64) { + flipped[i] ^= 0xA5; + let _ = webp_encoding(&flipped); + } + } + let mut huge = b"RIFF\0\0\0\0WEBPVP8 ".to_vec(); + huge.extend_from_slice(&u32::MAX.to_le_bytes()); + let _ = webp_encoding(&huge); + let mut anmf = b"RIFF\0\0\0\0WEBPANMF".to_vec(); + anmf.extend_from_slice(&3u32.to_le_bytes()); + let _ = webp_encoding(&anmf); + assert_eq!(webp_encoding(b"RIFF\0\0\0\0WAVE"), None); +} diff --git a/crates/image-codecs/tests/it/tga.rs b/crates/image-codecs/tests/it/tga.rs new file mode 100644 index 000000000..489e2eed1 --- /dev/null +++ b/crates/image-codecs/tests/it/tga.rs @@ -0,0 +1,91 @@ +//! Targa writing (#6382): run-length packets end with their scanline, rows are stored bottom-up, +//! and the file reads back exactly. + +use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image, decode_as, encode}; + +/// The packets of a run-length encoded Targa body, as (pixels, is a run), from just after the +/// 18-byte header (no image ID or colour map) up to `pixels` pixels. +fn packets(file: &[u8], bpp: usize, pixels: usize) -> Vec<(usize, bool)> { + let mut at = 18; + let mut seen = 0; + let mut out = Vec::new(); + while seen < pixels { + let head = file[at]; + let count = usize::from(head & 0x7f) + 1; + let run = head & 0x80 != 0; + at += 1 + if run { bpp } else { count * bpp }; + seen += count; + out.push((count, run)); + } + out +} + +/// A `w`×`h` image whose rows mix long runs (wider than a packet), short ones and noise, and +/// whose last pixel equals the next row's first, so a packet would run on if it could. +fn sample(w: u32, h: u32, layout: ChannelLayout) -> Image { + let n = layout.channels(); + let mut data = Vec::new(); + for y in 0..h { + for x in 0..w { + let v = match x { + _ if x < 150 => 40, + _ if x % 3 == 0 => (x * 7 + y * 13) as u8, + _ => 200, + }; + let px = [v, v.wrapping_add(y as u8), v ^ 0x5a, 255 - v]; + data.extend_from_slice(&px[..n]); + } + } + Image::from_u8(w, h, layout, data).unwrap() +} + +#[test] +fn rle_packets_end_with_their_scanline() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let (w, h) = (300u32, 4u32); + let file = encode(&sample(w, h, layout), Format::Tga, &EncodeOptions::default()).unwrap(); + let bpp = layout.channels(); + let mut col = 0; + for (count, _) in packets(&file, bpp, (w * h) as usize) { + assert!(count <= 128, "{layout:?}: a packet holds at most 128 pixels"); + assert!(col + count <= w as usize, "{layout:?}: a packet runs from column {col} past the row's end"); + col = (col + count) % w as usize; + } + // A solid image is one run per row (three packets of 128, 128 and 44), not one long run. + let solid = Image::from_u8(w, h, layout, vec![90; (w * h) as usize * bpp]).unwrap(); + let file = encode(&solid, Format::Tga, &EncodeOptions::default()).unwrap(); + let p = packets(&file, bpp, (w * h) as usize); + assert_eq!(p.len(), 3 * h as usize, "{layout:?}"); + assert!(p.iter().all(|&(_, run)| run)); + } +} + +#[test] +fn the_file_is_bottom_up_rle_with_its_alpha_bits_and_the_tga2_footer() { + let mut data = vec![0u8; 2 * 2 * 4]; + // Top row red, bottom row blue, opaque. + for (i, px) in data.as_chunks_mut::<4>().0.iter_mut().enumerate() { + *px = if i < 2 { [255, 0, 0, 255] } else { [0, 0, 255, 255] }; + } + let file = encode(&Image::from_u8(2, 2, ChannelLayout::Rgba, data).unwrap(), Format::Tga, &EncodeOptions::default()).unwrap(); + assert_eq!(file[2], 10, "run-length true colour"); + assert_eq!((file[16], file[17]), (32, 8), "32 bits per pixel, 8 alpha bits, bottom-left origin"); + // The first stored row is the bottom one: a run of two blue pixels, in BGRA. + assert_eq!(&file[18..23], &[0x81, 255, 0, 0, 255]); + assert!(file.ends_with(b"TRUEVISION-XFILE.\0")); + let gray = encode(&Image::from_u8(2, 1, ChannelLayout::Gray, vec![1, 2]).unwrap(), Format::Tga, &EncodeOptions::default()).unwrap(); + assert_eq!((gray[2], gray[16], gray[17]), (11, 8, 0), "run-length grayscale, no alpha bits"); +} + +#[test] +fn every_layout_reads_back_exactly() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for (w, h) in [(1, 1), (300, 4), (129, 3), (5, 130)] { + let img = sample(w, h, layout); + let file = encode(&img, Format::Tga, &EncodeOptions::default()).unwrap(); + let back = decode_as(Format::Tga, &file).unwrap(); + assert_eq!((back.dimensions(), back.layout()), ((w, h), layout), "{layout:?} {w}x{h}"); + assert_eq!(back.data(), img.data(), "{layout:?} {w}x{h}"); + } + } +} diff --git a/crates/image-codecs/tests/it/tiff_bands.rs b/crates/image-codecs/tests/it/tiff_bands.rs new file mode 100644 index 000000000..a449ae2b7 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_bands.rs @@ -0,0 +1,367 @@ +//! `TiffBands` (#8169): decoding a TIFF a group of bands at a time gives exactly the image the +//! whole-image decoder does, for every layout the decoder reads, every orientation, and groups of +//! any size; and it opens images whose single buffer the whole-image decoder refuses. + +use crate::common::tiffgen::*; +use craft_image_codecs::*; + +/// A deterministic byte pattern. +fn noise(n: usize, seed: u32) -> Vec { + let mut x = seed.wrapping_mul(2_654_435_761).max(1); + (0..n) + .map(|_| { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + (x >> 24) as u8 + }) + .collect() +} + +/// The image `TiffBands` decodes, assembled from its groups. +fn banded(bytes: &[u8], page: Option, limits: &Limits, upright: bool) -> Result { + let mut t = TiffBands::open(bytes, page, limits)?; + if upright { + t = t.upright(); + } + let (w, h) = t.dimensions(); + let mut out = Image::new(w, h, t.layout(), t.sample_type())?; + let bpp = t.layout().channels() * t.sample_type().bytes(); + let mut groups = 0; + t.decode(|x, y, g: &Image| -> Result<(), CodecError> { + groups += 1; + assert_eq!((g.layout(), g.sample_type()), (out.layout(), out.sample_type())); + let (gw, gh) = g.dimensions(); + assert!(x + gw <= w && y + gh <= h, "group {gw}x{gh} at {x},{y} outside {w}x{h}"); + let row = gw as usize * bpp; + for r in 0..gh as usize { + let dst = ((y as usize + r) * w as usize + x as usize) * bpp; + out.data_mut()[dst..dst + row].copy_from_slice(&g.data()[r * row..(r + 1) * row]); + } + Ok(()) + })?; + assert!(groups >= 1); + out.icc = t.icc.clone(); + out.meta = t.meta.clone(); + out.warnings = t.warnings.clone(); + Ok(out) +} + +/// The whole-image decode of the same page. +fn whole(bytes: &[u8], page: Option, upright: bool) -> Image { + let opts = DecodeOptions { keep_orientation: !upright, ..Default::default() }; + match page { + Some(p) => decode_tiff_page(bytes, p, &opts).unwrap(), + None => decode_with(bytes, &opts).unwrap(), + } +} + +/// Limits whose `max_alloc` makes each group `bands` bands of `band_bytes`. +fn grouped(band_bytes: usize, bands: usize) -> Limits { + Limits { max_alloc: (band_bytes * bands) as u64, ..Limits::banded() } +} + +fn assert_same(bytes: &[u8], band_bytes: usize, what: &str) { + for upright in [false, true] { + let want = whole(bytes, None, upright); + for limits in [Limits::banded(), grouped(band_bytes, 1), grouped(band_bytes, 2), grouped(band_bytes, 3)] { + let got = banded(bytes, None, &limits, upright).unwrap_or_else(|e| panic!("{what}: {e}")); + assert_eq!(got.dimensions(), want.dimensions(), "{what}"); + assert_eq!((got.layout(), got.sample_type()), (want.layout(), want.sample_type()), "{what}"); + assert!(got.data() == want.data(), "{what}: pixels differ (max_alloc {})", limits.max_alloc); + assert_eq!(got.icc, want.icc, "{what}"); + assert_eq!(got.meta, want.meta, "{what}"); + assert_eq!(got.warnings, want.warnings, "{what}"); + } + } +} + +/// `n / 1` as a little-endian RATIONAL value. +fn rational(n: u64) -> u64 { + n | 1 << 32 +} + +const W: u32 = 37; +const H: u32 = 29; + +#[test] +fn every_layout_matches_the_whole_image_decoder() { + let n = (W * H) as usize; + let rows = 4u32; + let row = |spp: usize, bps: usize| W as usize * spp * bps; + let cases: Vec<(&str, Dir, usize)> = vec![ + ("rgb8 strips", strips(W, H, 8, 3, 2, rows, &noise(n * 3, 1)), row(3, 1) * rows as usize), + ("gray8 WhiteIsZero", strips(W, H, 8, 1, 0, rows, &noise(n, 2)), row(1, 1) * rows as usize), + ("gray16 big-endian", strips(W, H, 16, 1, 1, rows, &noise(n * 2, 3)), row(1, 2) * rows as usize), + ("cmyk8", strips(W, H, 8, 4, 5, rows, &noise(n * 4, 4)), row(4, 1) * rows as usize), + ("cielab8", strips(W, H, 8, 3, 8, rows, &noise(n * 3, 5)), row(3, 1) * rows as usize), + ("rgba16 associated alpha", strips(W, H, 16, 4, 2, rows, &noise(n * 8, 6)).tag(338, SHORT, &[1]), row(4, 2) * rows as usize), + ( + "gray f32", + strips(W, H, 32, 1, 1, rows, &f32s(&noise(n, 7).iter().map(|&b| f32::from(b) / 255.0).collect::>(), true)).tag(339, SHORT, &[3]), + row(1, 4) * rows as usize, + ), + ( + "palette 4-bit", + strips(W, H, 4, 1, 3, rows, &noise((W as usize).div_ceil(2) * H as usize, 8)).tag(320, SHORT, &(0..48u64).map(|i| i * 1365).collect::>()), + row(3, 1) * rows as usize, + ), + ("gray 1-bit", strips(W, H, 1, 1, 1, rows, &noise((W as usize).div_ceil(8) * H as usize, 9)), row(1, 1) * rows as usize), + ]; + for (what, dir, band) in cases { + // Both byte orders: the generator writes multi-byte samples as given, so the + // big-endian files hold different values; each is compared against its own decode. + for little in [true, false] { + assert_same(&build(little, false, std::slice::from_ref(&dir), &[0]).bytes, band, what); + } + } +} + +#[test] +fn tiles_and_planar_data_match() { + let (w, h) = (W as usize, H as usize); + let px = noise(w * h * 3 * 2, 11); + let tiled = |planar: bool| { + let mut d = Dir { entries: base(W, H, 16, 3, 2), chunks: tile_split(&px, w, h, 3, 2, 16, 16, planar), tiled: true }.tag(322, SHORT, &[16]).tag( + 323, + SHORT, + &[16], + ); + if planar { + d = d.tag(284, SHORT, &[2]); + } + d + }; + let band = w * 3 * 2 * 16; + assert_same(&build(true, false, &[tiled(false)], &[0]).bytes, band, "chunky tiles"); + assert_same(&build(true, true, &[tiled(true)], &[0]).bytes, band, "planar tiles, BigTIFF"); + let planar = Dir { entries: base(W, H, 16, 3, 2), chunks: planar_strips(&px, w, h, 3, 2, 5), tiled: false }.tag(278, LONG, &[5]).tag(284, SHORT, &[2]); + assert_same(&build(true, false, &[planar], &[0]).bytes, w * 3 * 2 * 5, "planar strips"); +} + +#[test] +fn every_orientation_matches() { + let n = (W * H) as usize; + for o in 1..=8u64 { + let dir = strips(W, H, 8, 3, 2, 3, &noise(n * 3, 20 + o as u32)) + .tag(274, SHORT, &[o]) + .tag(282, RATIONAL, &[rational(300)]) + .tag(283, RATIONAL, &[rational(150)]) + .tag(296, SHORT, &[2]); + let bytes = build(true, false, &[dir], &[0]).bytes; + assert_same(&bytes, W as usize * 3 * 3, &format!("orientation {o}")); + let t = TiffBands::open(&bytes, None, &Limits::banded()).unwrap(); + assert_eq!(t.orientation(), o as u16); + let up = t.upright(); + let swapped = o >= 5; + assert_eq!(up.dimensions(), if swapped { (H, W) } else { (W, H) }, "orientation {o}"); + assert_eq!(up.meta.dpi, Some(if swapped { (150.0, 300.0) } else { (300.0, 150.0) }), "orientation {o}"); + } +} + +#[test] +fn cut_off_data_matches_and_warns() { + let n = (W * H) as usize; + let built = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 30))], &[0]); + // The last four of eight strips point past the end of the file: their data is missing. + let mut built = built; + let past = built.bytes.len() as u64 + 4096; + for i in 4..8 { + patch_value(&mut built, true, false, 0, 273, i, past); + } + assert_same(&built.bytes, W as usize * 3 * 4, "cut off"); + let img = banded(&built.bytes, None, &Limits::banded(), true).unwrap(); + assert!(img.warnings.contains(&DecodeWarning::Truncated { format: Format::Tiff })); +} + +#[test] +fn a_strip_cut_off_partway_decodes_as_zeros() { + // An uncompressed strip read straight into its output rows, whose data starts in the file + // and ends past it: the bytes read before the end must not survive (#8536 review). + let n = (W * H) as usize; + let mut built = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 31))], &[0]); + let strip = W as usize * 3 * 4; + let near_end = built.bytes.len() as u64 - 100; + patch_value(&mut built, true, false, 0, 273, 6, near_end); + let img = banded(&built.bytes, None, &Limits::banded(), false).unwrap(); + assert!(img.warnings.contains(&DecodeWarning::Truncated { format: Format::Tiff })); + assert!(img.data()[6 * strip..7 * strip].iter().all(|&v| v == 0), "partial strip bytes kept"); + assert!(img.data()[..6 * strip] == whole(&built.bytes, None, false).data()[..6 * strip]); +} + +#[test] +fn other_pages_and_the_more_pages_warning_match() { + let n = (W * H) as usize; + let a = strips(W, H, 8, 1, 1, 5, &noise(n, 40)); + let b = strips(W, H, 8, 3, 2, 5, &noise(n * 3, 41)); + let bytes = build(true, false, &[a, b], &[0, 1]).bytes; + for page in [None, Some(0), Some(1)] { + let want = whole(&bytes, page, false); + let got = banded(&bytes, page, &Limits::banded(), false).unwrap(); + assert!(got.data() == want.data(), "page {page:?}"); + assert_eq!(got.warnings, want.warnings, "page {page:?}"); + } + assert!(matches!(TiffBands::open(&bytes, Some(9), &Limits::banded()), Err(CodecError::InvalidImage(_)))); +} + +#[test] +fn a_cut_off_page_of_several_warns_in_the_whole_image_order() { + // Truncated before MorePages, as the whole-image decode reports them (#8536 review). + let n = (W * H) as usize; + let a = strips(W, H, 8, 3, 2, 4, &noise(n * 3, 42)); + let b = strips(W, H, 8, 1, 1, 5, &noise(n, 43)); + let mut built = build(true, false, &[a, b], &[0, 1]); + let past = built.bytes.len() as u64 + 4096; + for i in 4..8 { + patch_value(&mut built, true, false, 0, 273, i, past); + } + let want = whole(&built.bytes, None, false); + assert!(matches!(want.warnings[..], [DecodeWarning::Truncated { .. }, DecodeWarning::MorePages { .. }]), "{:?}", want.warnings); + let got = banded(&built.bytes, None, &Limits::banded(), false).unwrap(); + assert_eq!(got.warnings, want.warnings); +} + +#[test] +fn group_bytes_is_the_largest_group_handed_over() { + let n = (W * H) as usize; + // Eight strips of four rows, two to a group; one strip holding the whole image. + let bytes = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 44))], &[0]).bytes; + let band = W as usize * 3 * 4; + let mut t = TiffBands::open(&bytes, None, &grouped(band, 2)).unwrap(); + assert_eq!(t.group_bytes().unwrap(), 2 * band); + let mut largest = 0; + t.decode(|_, _, g: &Image| -> Result<(), CodecError> { + largest = largest.max(g.data().len()); + Ok(()) + }) + .unwrap(); + assert_eq!(largest, 2 * band); + let single = build(true, false, &[strips(W, H, 8, 3, 2, H, &noise(n * 3, 45))], &[0]).bytes; + let t = TiffBands::open(&single, None, &Limits::banded()).unwrap(); + assert_eq!(t.group_bytes().unwrap(), n * 3, "a single strip is one group: the whole image"); +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn opens_an_image_over_the_whole_image_budget() { + // The whole-image decoder needs the image in one buffer; the banded one only a band (on + // 64-bit targets: 32-bit ones still check the whole image). + let n = (W * H) as usize; + let bytes = build(true, false, &[strips(W, H, 8, 3, 2, 2, &noise(n * 3, 50))], &[0]).bytes; + let band = W as u64 * 3 * 2; + let tight = Limits { max_alloc: band * 3, ..Limits::banded() }; + let refused = decode_with(&bytes, &DecodeOptions { limits: tight, keep_orientation: false }); + assert!(matches!(refused, Err(CodecError::LimitExceeded(_))), "{refused:?}"); + let got = banded(&bytes, None, &tight, true).unwrap(); + assert!(got.data() == whole(&bytes, None, true).data()); +} + +#[test] +fn size_limits_still_apply() { + let n = (W * H) as usize; + let bytes = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 60))], &[0]).bytes; + let few_pixels = Limits { max_pixels: u64::from(W * H) - 1, ..Limits::banded() }; + assert!(matches!(TiffBands::open(&bytes, None, &few_pixels), Err(CodecError::LimitExceeded(_)))); + let narrow = Limits { max_width: W - 1, ..Limits::banded() }; + assert!(matches!(TiffBands::open(&bytes, None, &narrow), Err(CodecError::LimitExceeded(_)))); + // One strip is more than `max_alloc`: refused before decoding, with the image size. + let tiny = Limits { max_alloc: 10, ..Limits::banded() }; + match TiffBands::open(&bytes, None, &tiny) { + Err(CodecError::LimitExceeded(m)) => assert!(m.contains(&format!("{W}x{H}")), "{m}"), + other => panic!("{:?}", other.map(|t| t.dimensions())), + } +} + +#[test] +fn banded_limits_allow_the_largest_document_size() { + let l = Limits::banded(); + if cfg!(target_pointer_width = "64") { + assert_eq!((l.max_width, l.max_height), (300_000, 300_000)); + assert!(l.max_pixels >= 300_000 * 300_000); + // The banner from #8456 / #8169: 136 ft × 26 ft at 72 ppi. + assert!(117_504u64 * 22_464 > Limits::default().max_pixels); + assert!(117_504u64 * 22_464 <= l.max_pixels); + } else { + assert_eq!(l, Limits::default()); + } + assert_eq!(l.max_alloc, Limits::default().max_alloc); +} + +#[test] +fn a_hostile_header_is_refused_before_allocating() { + // 300000² declared in a file of a few kilobytes: one 270 GB strip, which neither the + // decompression-bomb guard nor the per-group budget lets through. + let mut built = build(true, true, &[strips(1000, 4, 8, 3, 2, 4, &noise(12_000, 70))], &[0]); + for tag in [256, 257, 278] { + patch_value(&mut built, true, true, 0, tag, 0, 300_000); + } + assert!(matches!(TiffBands::open(&built.bytes, None, &Limits::banded()), Err(CodecError::Malformed { .. } | CodecError::LimitExceeded(_)))); + // Past the 300000 px a side documents allow. + let mut wide = build(true, true, &[strips(1000, 4, 8, 3, 2, 4, &noise(12_000, 71))], &[0]); + patch_value(&mut wide, true, true, 0, 256, 0, 300_001); + assert!(matches!(TiffBands::open(&wide.bytes, None, &Limits::banded()), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn padding_does_not_let_missing_chunks_past_the_bomb_guard() { + // 20000² RGB declared as Deflate (1.2 GB of samples), every strip past the end of the file, + // and 400 KB of padding: the file is large enough for that size at Deflate's ratio, but its + // strips hold nothing. Refused before anything is allocated, by both decoders (#8536 review). + let (w, h) = (20_000, 20_000); + let mut dir = Dir { entries: base(w, h, 8, 3, 2), chunks: vec![vec![0]; 20], tiled: false }.tag(278, LONG, &[1000]); + dir.entries.retain(|e| e.0 != 259); + let dir = dir.tag(259, SHORT, &[8]).bytes(65_000, UNDEFINED, &vec![0; 400_000]); + let mut built = build(true, false, &[dir], &[0]); + let past = built.bytes.len() as u64 + 4096; + for i in 0..20 { + patch_value(&mut built, true, false, 0, 273, i, past); + } + match TiffBands::open(&built.bytes, None, &Limits::banded()) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains("hold 0 bytes"), "{message}"), + other => panic!("{:?}", other.map(|t| t.dimensions())), + } + assert!(matches!(decode(&built.bytes), Err(CodecError::Malformed { .. }))); +} + +/// An all-white CCITT Group 4 strip: one V0 code (a 1 bit) per row, then the EOFB. +fn blank_g4(h: u32) -> Vec { + let mut bits = vec![true; h as usize]; + for _ in 0..2 { + bits.extend([false; 11]); + bits.push(true); + } + bits.chunks(8).map(|c| c.iter().enumerate().fold(0u8, |a, (i, &b)| a | (u8::from(b) << (7 - i)))).collect() +} + +#[test] +fn a_blank_fax_page_wider_than_4096_px_passes_the_bomb_guard() { + // A blank 600 dpi page (A4 landscape, A3) codes each row in one bit, past 4096:1: it must + // open, with or without the metadata and other pages that used to raise the bound. + for (w, h) in [(7016u32, 4961u32), (7016, 9921)] { + let mut d = Dir { entries: base(w, h, 1, 1, 0), chunks: vec![blank_g4(h)], tiled: false }.tag(278, LONG, &[u64::from(h)]); + d.entries.retain(|e| e.0 != 259); + let d = d.tag(259, SHORT, &[4]); + let pages: Vec = (0..3).map(|_| d.clone()).collect(); + for (what, bytes) in + [("one page", build(true, false, std::slice::from_ref(&d), &[0]).bytes), ("three pages", build(true, false, &pages, &[0, 1, 2]).bytes)] + { + let img = decode(&bytes).unwrap_or_else(|e| panic!("{w}x{h} {what}: {e}")); + assert_eq!(img.dimensions(), (w, h), "{what}"); + assert!(img.data().iter().all(|&v| v == 255), "{w}x{h} {what}: not white"); + } + } + let mut d = Dir { entries: base(7016, 4961, 1, 1, 0), chunks: vec![blank_g4(4961)], tiled: false }.tag(278, LONG, &[4961]); + d.entries.retain(|e| e.0 != 259); + let bytes = build(true, false, &[d.tag(259, SHORT, &[4])], &[0]).bytes; + assert!(banded(&bytes, None, &Limits::banded(), true).unwrap().data() == decode(&bytes).unwrap().data()); + // Still a bound: 100 bytes of Group 4 can't hold that page (4096:1 for Group 3, which is not decoded). + for compression in [4, 3] { + let mut d = Dir { entries: base(7016, 4961, 1, 1, 0), chunks: vec![vec![0x80; 100]], tiled: false }.tag(278, LONG, &[4961]); + d.entries.retain(|e| e.0 != 259); + let bytes = build(true, false, &[d.tag(259, SHORT, &[compression])], &[0]).bytes; + match decode(&bytes) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains("hold 100 bytes"), "{message}"), + other => panic!("compression {compression}: {:?}", other.map(|i| i.dimensions())), + } + } +} diff --git a/crates/image-codecs/tests/it/tiff_bench.rs b/crates/image-codecs/tests/it/tiff_bench.rs new file mode 100644 index 000000000..55cff55ca --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_bench.rs @@ -0,0 +1,124 @@ +//! TIFF decode timings on a synthetic ~30 MP image (6336×4752). Ignored by default; run in +//! release: +//! +//! ```sh +//! cargo test --release -p craft-image-codecs --test it -- tiff_bench:: --ignored --nocapture +//! ``` + +use crate::common; +use common::tiffgen::*; +use craft_image_codecs::*; +use std::time::Instant; + +const W: u32 = 6336; +const H: u32 = 4752; + +fn pixels(spp: usize) -> Vec { + // A smooth gradient with a little texture: compresses like a photo, not like noise. + let mut v = Vec::with_capacity(W as usize * H as usize * spp); + let mut s = 0x1234_5678u32; + for y in 0..H { + for x in 0..W { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + for c in 0..spp { + let g = (x * (c as u32 + 1) / 25 + y / 19) as u8; + v.push(g.wrapping_add((s >> (8 * c)) as u8 & 3)); + } + } + } + v +} + +/// The decoder before FILE-215-8, for a same-run comparison: the `tiff` crate decodes the whole +/// image into one buffer (`read_image`, first plane only), which is then copied into an `Image`. +fn legacy(bytes: &[u8]) -> Result { + use tiff::decoder::{Decoder, DecodingResult, Limits as TLimits}; + let mut l = TLimits::default(); + l.decoding_buffer_size = 8 << 30; + l.intermediate_buffer_size = 1 << 30; + let mut dec = Decoder::new(std::io::Cursor::new(bytes)).map_err(|e| e.to_string())?.with_limits(l); + let (w, h) = dec.dimensions().map_err(|e| e.to_string())?; + let layout = match dec.colortype().map_err(|e| e.to_string())? { + tiff::ColorType::RGB(_) => ChannelLayout::Rgb, + tiff::ColorType::Gray(_) => ChannelLayout::Gray, + c => return Err(format!("{c:?}")), + }; + match dec.read_image().map_err(|e| e.to_string())? { + DecodingResult::U8(v) => Image::from_u8(w, h, layout, v), + DecodingResult::U16(v) => Image::from_u16(w, h, layout, &v), + _ => return Err("sample type".into()), + } + .map_err(|e| e.to_string()) +} + +fn best_of(f: impl Fn() -> Result) -> Result { + let mut best = f64::MAX; + for _ in 0..7 { + let t = Instant::now(); + f()?; + best = best.min(t.elapsed().as_secs_f64() * 1000.0); + } + Ok(best) +} + +fn time(name: &str, bytes: &[u8]) { + let new = best_of(|| decode(bytes).map_err(|e| e.to_string())); + let old = best_of(|| legacy(bytes)); + let show = |r: &Result| match r { + Ok(ms) => format!("{ms:>7.1} ms"), + Err(e) => format!("error ({})", e.chars().take(48).collect::()), + }; + println!("{name:<34} new {} before {} ({:.1} MB file)", show(&new), show(&old), bytes.len() as f64 / 1e6); +} + +#[test] +#[ignore = "benchmark: run in release with --ignored --nocapture"] +fn decode_30mp() { + let rgb = pixels(3); + let img = Image::from_u8(W, H, ChannelLayout::Rgb, rgb.clone()).unwrap(); + for (name, c) in + [("strips, none", TiffCompression::None), ("strips, LZW + predictor", TiffCompression::Lzw), ("strips, Deflate + predictor", TiffCompression::Deflate)] + { + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); + time(&format!("RGB 8 {name}"), &bytes); + } + let img16 = img.convert(ChannelLayout::Rgb, SampleType::U16); + let bytes = encode(&img16, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::Deflate, ..Default::default() }).unwrap(); + time("RGB 16 strips, Deflate + predictor", &bytes); + let bytes = encode(&img16, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::None, ..Default::default() }).unwrap(); + time("RGB 16 strips, none", &bytes); + + // Hand-built layouts the encoder does not write: one big strip, tiles, planar. + let le = cfg!(target_endian = "little"); + let one = build(le, false, &[strips(W, H, 8, 3, 2, H, &rgb)], &[0]); + time("RGB 8 one strip, none", &one.bytes); + let tiles = tile_split(&rgb, W as usize, H as usize, 3, 1, 256, 256, false); + let mut d = Dir { entries: base(W, H, 8, 3, 2), chunks: tiles, tiled: true }; + d = d.tag(322, LONG, &[256]).tag(323, LONG, &[256]); + time("RGB 8 tiles 256, none", &build(le, false, &[d], &[0]).bytes); + let planes = planar_strips(&rgb, W as usize, H as usize, 3, 1, 64); + let mut d = Dir { entries: base(W, H, 8, 3, 2), chunks: planes, tiled: false }; + d = d.tag(278, LONG, &[64]).tag(284, SHORT, &[2]); + time("RGB 8 planar strips, none", &build(le, false, &[d], &[0]).bytes); + let d = strips(W, H, 8, 3, 2, 64, &rgb); + time("RGB 8 BigTIFF strips, none", &build(le, true, &[d], &[0]).bytes); +} + +/// Save Image Pyramid's cost on top of a plain encode (#8827): RGB/8 and RGBA/16. +#[test] +#[ignore = "benchmark: run in release with --ignored --nocapture"] +fn encode_with_pyramid() { + for (layout, sample, spp) in [(ChannelLayout::Rgb, SampleType::U8, 3), (ChannelLayout::Rgba, SampleType::U16, 4)] { + let raw = pixels(spp); + let data = if sample == SampleType::U16 { raw.iter().flat_map(|&v| (u16::from(v) * 257).to_ne_bytes()).collect() } else { raw }; + let img = Image::from_raw(W, H, layout, sample, data).unwrap(); + for pyramid in [false, true] { + let opts = EncodeOptions { tiff_pyramid: pyramid, ..Default::default() }; + let t = Instant::now(); + let bytes = encode(&img, Format::Tiff, &opts).unwrap(); + println!("{layout:?} {sample:?} pyramid {pyramid}: {:.0} ms, {} MB", t.elapsed().as_secs_f64() * 1000.0, bytes.len() >> 20); + } + } +} diff --git a/crates/image-codecs/tests/it/tiff_exif.rs b/crates/image-codecs/tests/it/tiff_exif.rs new file mode 100644 index 000000000..a1ad5c542 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_exif.rs @@ -0,0 +1,262 @@ +//! EXIF in TIFF: an EXIF block's Exif and GPS directories and IFD0's descriptive tags are +//! written as the TIFF's own directories (IFD0 pointing to them, as cameras and Photoshop write +//! them) and read back into a block. + +use craft_image_codecs::*; + +/// A classic TIFF stream builder for EXIF blocks: directories of (tag, type, count, value bytes). +struct Block { + le: bool, +} + +type Entry = (u16, u16, u32, Vec); + +impl Block { + fn u16(&self, v: u16) -> [u8; 2] { + if self.le { v.to_le_bytes() } else { v.to_be_bytes() } + } + fn u32(&self, v: u32) -> [u8; 4] { + if self.le { v.to_le_bytes() } else { v.to_be_bytes() } + } + fn rational(&self, n: u32, d: u32) -> Vec { + [self.u32(n), self.u32(d)].concat() + } + /// IFD0 (with pointers to `exif` and `gps` appended) followed by the two directories. + fn build(&self, mut ifd0: Vec, exif: Vec, gps: Vec) -> Vec { + let size = |d: &[Entry]| 2 + 12 * d.len() + 4 + d.iter().filter(|e| e.3.len() > 4).map(|e| e.3.len().next_multiple_of(2)).sum::(); + ifd0.push((34665, 4, 1, vec![0; 4])); + ifd0.push((34853, 4, 1, vec![0; 4])); + let at_exif = 8 + size(&ifd0); + let at_gps = at_exif + size(&exif); + let n = ifd0.len(); + ifd0[n - 2].3 = self.u32(at_exif as u32).to_vec(); + ifd0[n - 1].3 = self.u32(at_gps as u32).to_vec(); + let mut out = if self.le { b"II".to_vec() } else { b"MM".to_vec() }; + out.extend(self.u16(42)); + out.extend(self.u32(8)); + for d in [ifd0, exif, gps] { + let start = out.len(); + let mut extra = start + 2 + 12 * d.len() + 4; + let mut values = Vec::new(); + out.extend(self.u16(d.len() as u16)); + for (tag, ty, count, v) in &d { + out.extend(self.u16(*tag)); + out.extend(self.u16(*ty)); + out.extend(self.u32(*count)); + if v.len() <= 4 { + let mut inline = v.clone(); + inline.resize(4, 0); + out.extend(inline); + } else { + out.extend(self.u32(extra as u32)); + values.extend(v); + if v.len() % 2 == 1 { + values.push(0); + } + extra += v.len().next_multiple_of(2); + } + } + out.extend(self.u32(0)); + out.extend(values); + } + out + } +} + +/// The entries (tag, type, count, value bytes in the block's order) of IFD0 and of the +/// directories its tags `34665` and `34853` point to. +fn dirs(b: &[u8]) -> (bool, Vec, Vec, Vec) { + let le = &b[..2] == b"II"; + let u16 = |at: usize| if le { u16::from_le_bytes([b[at], b[at + 1]]) } else { u16::from_be_bytes([b[at], b[at + 1]]) }; + let u32 = |at: usize| { + let s = [b[at], b[at + 1], b[at + 2], b[at + 3]]; + if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) } + }; + let size = |ty: u16| match ty { + 3 | 8 => 2, + 4 | 9 | 11 | 13 => 4, + 5 | 10 | 12 => 8, + _ => 1, + }; + let read = |at: usize| -> Vec { + (0..usize::from(u16(at))) + .map(|i| { + let e = at + 2 + 12 * i; + let (tag, ty, count) = (u16(e), u16(e + 2), u32(e + 4)); + let len = count as usize * size(ty); + let at = if len <= 4 { e + 8 } else { u32(e + 8) as usize }; + (tag, ty, count, b[at..at + len].to_vec()) + }) + .collect() + }; + let ifd0 = read(u32(4) as usize); + let sub = |tag: u16| ifd0.iter().find(|e| e.0 == tag).map(|e| read(u32_of(&e.3, le) as usize)).unwrap_or_default(); + let (exif, gps) = (sub(34665), sub(34853)); + (le, ifd0.into_iter().filter(|e| !matches!(e.0, 34665 | 34853)).collect(), exif, gps) +} + +fn u32_of(v: &[u8], le: bool) -> u32 { + let s = [v[0], v[1], v[2], v[3]]; + if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) } +} + +/// The entries of `d` with their values in little-endian order, sorted by tag. +fn normalized(d: &[Entry], le: bool) -> Vec<(u16, u16, u32, Vec)> { + let swap = |ty: u16, v: &[u8]| -> Vec { + let n = match ty { + 3 | 8 => 2, + 4 | 5 | 9 | 10 | 11 | 13 => 4, + 12 => 8, + _ => 1, + }; + if le || n == 1 { v.to_vec() } else { v.chunks(n).flat_map(|c| c.iter().rev().copied()).collect() } + }; + let mut v: Vec<_> = d.iter().map(|e| (e.0, e.1, e.2, swap(e.1, &e.3))).collect(); + v.sort_by_key(|e| e.0); + v +} + +fn camera_block(le: bool) -> Vec { + let b = Block { le }; + let ascii = |s: &str| [s.as_bytes(), b"\0"].concat(); + b.build( + vec![(271, 2, 4, ascii("Cam")), (272, 2, 9, ascii("Model X1")), (315, 2, 16, ascii("A. Photographer")), (274, 3, 1, b.u16(1).to_vec())], + vec![ + (33434, 5, 1, b.rational(1, 125)), + (34855, 3, 1, b.u16(200).to_vec()), + (36867, 2, 20, ascii("2026:10:13 09:30:00")), + (37386, 5, 1, b.rational(50, 1)), + (37500, 7, 6, b"maker!".to_vec()), + ], + vec![(1, 2, 2, ascii("N")), (2, 5, 3, [b.rational(51, 1), b.rational(30, 1), b.rational(0, 1)].concat())], + ) +} + +#[test] +fn exif_round_trips_through_a_tiff_in_either_byte_order() { + for le in [true, false] { + let block = camera_block(le); + let mut img = Image::from_raw(4, 3, ChannelLayout::Rgb, SampleType::U8, vec![90; 36]).unwrap(); + img.meta.exif = Some(block.clone()); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert!(fidelity_warnings(&img, Format::Tiff).iter().all(|w| *w != FidelityWarning::ExifDropped)); + let back = decode(&tiff).unwrap().meta.exif.expect("EXIF read back"); + let (ble, b0, bexif, bgps) = dirs(&back); + let (ole, o0, oexif, ogps) = dirs(&block); + assert_eq!(normalized(&bexif, ble), normalized(&oexif, ole), "Exif directory (le {le})"); + assert_eq!(normalized(&bgps, ble), normalized(&ogps, ole), "GPS directory (le {le})"); + // IFD0's descriptive tags come along; its orientation is the image's own. + let descriptive: Vec<_> = o0.into_iter().filter(|e| e.0 != 274).collect(); + assert_eq!(normalized(&b0, ble), normalized(&descriptive, ole), "IFD0 (le {le})"); + } +} + +#[test] +fn a_tiff_without_exif_directories_reads_no_exif() { + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.text = vec![("Artist".into(), "A. Photographer".into())]; + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&tiff).unwrap().meta.exif, None); +} + +#[test] +fn no_exif_is_written_without_embed_metadata() { + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.exif = Some(camera_block(true)); + let tiff = encode(&img, Format::Tiff, &EncodeOptions { embed_metadata: false, ..Default::default() }).unwrap(); + assert_eq!(decode(&tiff).unwrap().meta.exif, None); +} + +// --------------------------------------------------------------------------- +// Bounded copying: an entry's value only has to lie inside the file, so a hostile file can have +// many entries point at the same bytes. +// --------------------------------------------------------------------------- + +/// A valid 1x1 gray TIFF whose Exif directory has `entries` UNDEFINED entries of `count` bytes +/// that all point at the same `count` bytes: the file is about `count` bytes long and asks to +/// copy `entries * count`. +fn overlapping_tiff(entries: usize, count: u32) -> Vec { + let entry = |tag: u16, ty: u16, count: u32, value: u32| { + [tag.to_le_bytes().to_vec(), ty.to_le_bytes().to_vec(), count.to_le_bytes().to_vec(), value.to_le_bytes().to_vec()].concat() + }; + let at_exif = 8 + 2 + 12 * 10 + 4; + let data_at = at_exif + 2 + 12 * entries + 4; + let mut out = b"II".to_vec(); + out.extend(42u16.to_le_bytes()); + out.extend(8u32.to_le_bytes()); + out.extend(10u16.to_le_bytes()); + for (tag, ty, value) in + [(256, 3, 1), (257, 3, 1), (258, 3, 8), (259, 3, 1), (262, 3, 1), (273, 4, 0), (277, 3, 1), (278, 3, 1), (279, 4, 1), (34665, 4, at_exif as u32)] + { + out.extend(entry(tag, ty, 1, value)); + } + out.extend(0u32.to_le_bytes()); + out.extend((entries as u16).to_le_bytes()); + for i in 0..entries { + out.extend(entry(i as u16 + 1, 7, count, data_at as u32)); + } + out.extend(0u32.to_le_bytes()); + out.extend(vec![0x5A; count as usize]); + out +} + +#[test] +fn overlapping_field_values_are_bounded() { + // About 62 KB on disk; copied entry by entry it would be 1,000 x 50,000 bytes. + let tiff = overlapping_tiff(1000, 50_000); + assert!(tiff.len() < 64 * 1024); + let img = decode(&tiff).expect("the image itself is fine"); + assert_eq!(img.dimensions(), (1, 1)); + let exif = img.meta.exif.expect("the first entries are kept"); + assert!(exif.len() <= 2 * tiff.len(), "{} bytes of EXIF from a {} byte file", exif.len(), tiff.len()); +} + +#[test] +fn overlapping_field_values_in_an_exif_block_are_bounded_on_export() { + // The same block as an image's EXIF: the writer reads it with the same code. + let block = overlapping_tiff(1000, 50_000); + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.exif = Some(block.clone()); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert!(tiff.len() <= 2 * block.len(), "a {} byte block wrote a {} byte file", block.len(), tiff.len()); +} + +/// An EXIF block with a maker note of `len` bytes, an exposure time and a GPS latitude reference. +fn block_with_maker_note(le: bool, len: usize) -> Vec { + let b = Block { le }; + b.build( + vec![(271, 2, 4, b"Cam\0".to_vec())], + vec![(33434, 5, 1, b.rational(1, 125)), (37500, 7, len as u32, (0..len).map(|i| (i * 7) as u8).collect())], + vec![(1, 2, 2, b"N\0".to_vec())], + ) +} + +#[test] +fn large_maker_notes_still_survive() { + // Nikon maker notes run from 100 KB to nearly 1 MB. + for len in [200 * 1024, 1000 * 1024] { + let block = block_with_maker_note(true, len); + let mut img = Image::from_raw(4, 3, ChannelLayout::Rgb, SampleType::U8, vec![90; 36]).unwrap(); + img.meta.exif = Some(block.clone()); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let back = decode(&tiff).unwrap().meta.exif.expect("EXIF read back"); + let (ble, _, bexif, bgps) = dirs(&back); + let (ole, _, oexif, ogps) = dirs(&block); + assert_eq!(normalized(&bexif, ble), normalized(&oexif, ole), "{len} byte maker note"); + assert_eq!(normalized(&bgps, ble), normalized(&ogps, ole)); + } +} + +#[test] +fn a_field_beyond_the_cap_is_dropped_and_the_rest_kept() { + let block = block_with_maker_note(false, 6 * 1024 * 1024); + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.exif = Some(block); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert!(tiff.len() < 1024 * 1024, "{} bytes", tiff.len()); + let back = decode(&tiff).unwrap().meta.exif.expect("EXIF read back"); + let (_, ifd0, exif, gps) = dirs(&back); + assert_eq!(exif.iter().map(|e| e.0).collect::>(), [33434], "the exposure time stays, the 6 MiB maker note goes"); + assert_eq!(gps.iter().map(|e| e.0).collect::>(), [1]); + assert_eq!(ifd0.iter().map(|e| e.0).collect::>(), [271]); +} diff --git a/crates/image-codecs/tests/it/tiff_lab.rs b/crates/image-codecs/tests/it/tiff_lab.rs new file mode 100644 index 000000000..b36435ef4 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_lab.rs @@ -0,0 +1,109 @@ +//! CIE L*a*b* TIFFs: CIELab (PhotometricInterpretation 8) as Photoshop writes it, with a* and b* +//! signed, and ICCLab (9) with them offset, both read into the Lab layout (a* and b* as +//! `(a + 128) / 255`). + +use craft_image_codecs::*; + +/// One Lab pixel in the codec's normalized form. +fn lab(l: f32, a: f32, b: f32) -> [f32; 3] { + [l / 100.0, (a + 128.0) / 255.0, (b + 128.0) / 255.0] +} + +fn image(sample: SampleType) -> Image { + let px: Vec = [lab(50.0, 50.0, -30.0), lab(100.0, 0.0, 0.0)].concat(); + Image::from_normalized(2, 1, ChannelLayout::Lab, sample, &px).unwrap() +} + +fn uncompressed(img: &Image) -> Vec { + encode(img, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::None, ..Default::default() }).unwrap() +} + +fn find(hay: &[u8], needle: &[u8]) -> Option { + hay.windows(needle.len()).position(|w| w == needle) +} + +#[test] +fn eight_bit_cielab_holds_a_and_b_signed() { + let file = uncompressed(&image(SampleType::U8)); + // L* 50 → 128, a* 50, b* −30 as a signed byte; then white with neutral a* and b*. + assert!(find(&file, &[128, 50, (-30i8) as u8, 255, 0, 0]).is_some(), "CIELab samples"); + let back = decode(&file).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (ChannelLayout::Lab, SampleType::U8)); + assert_eq!(back.data(), image(SampleType::U8).data()); +} + +#[test] +fn sixteen_bit_cielab_holds_256_a() { + let file = uncompressed(&image(SampleType::U16)); + let le = &file[..2] == b"II"; + let u16b = |v: u16| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + // a* 50 → 12800, b* −30 → −7680, white's a* and b* 0. + let want: Vec = [32768u16, 12800, (-7680i16) as u16, 65535, 0, 0].iter().flat_map(|v| u16b(*v)).collect(); + assert!(find(&file, &want).is_some(), "CIELab samples"); + let back = decode(&file).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (ChannelLayout::Lab, SampleType::U16)); + let (a, b) = (image(SampleType::U16).to_normalized(), back.to_normalized()); + assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() <= 1.5 / 65535.0), "{a:?} {b:?}"); +} + +#[test] +fn icclab_reads_with_offset_a_and_b() { + // The CIELab file turned into ICCLab: PhotometricInterpretation 9, a* and b* + 128. + let mut file = uncompressed(&image(SampleType::U8)); + let at = find(&file, &[128, 50, (-30i8) as u8, 255, 0, 0]).unwrap(); + for i in [1, 2, 4, 5] { + file[at + i] ^= 0x80; + } + let le = &file[..2] == b"II"; + let entry: Vec = if le { [262u16.to_le_bytes(), 3u16.to_le_bytes()].concat() } else { [262u16.to_be_bytes(), 3u16.to_be_bytes()].concat() }; + let e = find(&file, &entry).expect("PhotometricInterpretation entry"); + let nine = if le { 9u16.to_le_bytes() } else { 9u16.to_be_bytes() }; + file[e + 8..e + 10].copy_from_slice(&nine); + let back = decode(&file).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Lab); + assert_eq!(back.data(), image(SampleType::U8).data()); +} + +#[test] +fn lab_converts_to_rgb_for_other_formats() { + // White and a saturated orange-red; PNG has no Lab, so the encoder plan converts to RGB. + let img = image(SampleType::U8); + let png = decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(png.layout().is_rgb()); + let p = png.to_rgba8(); + assert_eq!(&p[4..7], &[255, 255, 255], "white"); + assert!(p[0] > 180 && p[1] < 120 && p[2] > 140, "L 50, a 50, b −30 is a magenta-ish purple: {:?}", &p[..3]); +} + +#[test] +fn lab_keeps_extra_samples() { + // A spot plate next to Lab: written as CIELab plus one unspecified extra sample, and read + // back as Lab with the spot (not as LabA). + for (layout, sample) in [(ChannelLayout::Lab, SampleType::U8), (ChannelLayout::LabA, SampleType::U16)] { + let px: Vec = + [lab(50.0, 50.0, -30.0), lab(100.0, 0.0, 0.0)].iter().flat_map(|p| p.iter().copied().chain(layout.has_alpha().then_some(0.5))).collect(); + let mut img = Image::from_normalized(2, 1, layout, sample, &px).unwrap(); + let spot: Vec = (0..2 * sample.bytes()).map(|i| (i * 37 + 5) as u8).collect(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: spot.clone() }]; + let file = uncompressed(&img); + if layout == ChannelLayout::Lab { + assert!(find(&file, &[128, 50, (-30i8) as u8, spot[0], 255, 0, 0, spot[1]]).is_some(), "CIELab samples with the spot"); + } + let back = decode(&file).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (layout, sample)); + let (a, b) = (img.to_normalized(), back.to_normalized()); + assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() <= 1.5 / 65535.0), "{layout:?}: {a:?} {b:?}"); + assert_eq!(back.meta.extra_samples, vec![ExtraSample { kind: 0, data: spot }], "{layout:?}"); + } +} + +#[test] +fn float_lab_writes_its_extra_samples_with_16_bits() { + let px: Vec = [lab(50.0, 50.0, -30.0), lab(100.0, 0.0, 0.0)].concat(); + let mut img = Image::from_normalized(2, 1, ChannelLayout::Lab, SampleType::F32, &px).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: [0.25f32, 1.0].iter().flat_map(|v| v.to_ne_bytes()).collect() }]; + let back = decode(&uncompressed(&img)).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (ChannelLayout::Lab, SampleType::U16)); + let spot: Vec = [16384u16, 65535].iter().flat_map(|v| v.to_ne_bytes()).collect(); + assert_eq!(back.meta.extra_samples, vec![ExtraSample { kind: 0, data: spot }]); +} diff --git a/crates/image-codecs/tests/it/tiff_pages.rs b/crates/image-codecs/tests/it/tiff_pages.rs new file mode 100644 index 000000000..fbe4ffb8b --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_pages.rs @@ -0,0 +1,841 @@ +//! Large-file TIFF support (FILE-215-8): BigTIFF in both byte orders, every IFD of a multi-page +//! file and its SubIFDs, banded decoding of strips and tiles in either planar configuration, the +//! sample formats and interpretations around them, and hostile structures (cycles, counts and +//! offsets past the end, absurd sizes) that must give `Ok` or a clean `Err`, never a panic. +//! Every file here is hand-built by `common::tiffgen`. + +use crate::common; +use common::Rng; +use common::tiffgen::*; +use craft_image_codecs::*; + +const ORDERS: [(bool, bool); 4] = [(true, false), (false, false), (true, true), (false, true)]; + +fn ne16(v: &[u16]) -> Vec { + v.iter().flat_map(|x| x.to_ne_bytes()).collect() +} +fn ne32f(v: &[f32]) -> Vec { + v.iter().flat_map(|x| x.to_ne_bytes()).collect() +} + +/// A test image's samples in each storage: 8-bit, 16-bit and 32-bit float. +struct Truth { + u8s: Vec, + u16s: Vec, + f32s: Vec, +} + +fn truth(n: usize, seed: u64) -> Truth { + let mut r = Rng::new(seed); + let u16s: Vec = (0..n).map(|_| r.next_u64() as u16).collect(); + Truth { u8s: u16s.iter().map(|v| (v >> 8) as u8).collect(), f32s: u16s.iter().map(|&v| f32::from(v) / 65535.0).collect(), u16s } +} + +/// `(bits, sample format, file bytes in this order, expected native bytes, sample type)`. +type Depth = (u16, u16, Vec, Vec, SampleType); + +fn depths(t: &Truth, little: bool) -> Vec { + vec![ + (8, 1, t.u8s.clone(), t.u8s.clone(), SampleType::U8), + (16, 1, u16s(&t.u16s, little), ne16(&t.u16s), SampleType::U16), + (32, 3, f32s(&t.f32s, little), ne32f(&t.f32s), SampleType::F32), + ] +} + +fn decode_ok(bytes: &[u8]) -> Image { + decode(bytes).unwrap_or_else(|e| panic!("decode failed: {e}")) +} + +#[test] +fn bigtiff_and_classic_in_both_byte_orders_at_every_depth() { + let (w, h) = (7u32, 5u32); + for spp in [1u16, 3, 4] { + let t = truth((w * h) as usize * spp as usize, u64::from(spp)); + for (little, big) in ORDERS { + for (bits, format, file, want, sample) in depths(&t, little) { + let photometric = if spp == 1 { 1 } else { 2 }; + let mut d = strips(w, h, bits, spp, photometric, 2, &file).tag(339, SHORT, &vec![u64::from(format); spp as usize]); + if spp == 4 { + d = d.tag(338, SHORT, &[2]); + } + let b = build(little, big, &[d], &[0]); + let img = decode_ok(&b.bytes); + assert_eq!((img.dimensions(), img.sample_type()), ((w, h), sample), "little {little} big {big} bits {bits} spp {spp}"); + assert_eq!(img.layout().channels(), spp as usize); + assert_eq!(img.data(), &want[..], "little {little} big {big} bits {bits} spp {spp}"); + assert_eq!(img.warnings, []); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.big_tiff, info.little_endian, info.pages.len(), info.complete), (big, little, 1, true)); + } + } + } +} + +#[test] +fn tiles_with_padding_in_both_planar_configurations() { + let (w, h) = (37usize, 29usize); + for spp in [1usize, 3, 4] { + let t = truth(w * h * spp, 9 + spp as u64); + for (little, big) in ORDERS { + for (bits, format, file, want, _) in depths(&t, little) { + let bps = bits as usize / 8; + for planar in [false, true] { + let mut d = Dir { + entries: base(w as u32, h as u32, bits, spp as u16, if spp == 1 { 1 } else { 2 }), + chunks: tile_split(&file, w, h, spp, bps, 16, 16, planar), + tiled: true, + }; + d = d.tag(322, SHORT, &[16]).tag(323, SHORT, &[16]).tag(339, SHORT, &[u64::from(format)]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + if spp == 4 { + d = d.tag(338, SHORT, &[2]); + } + let b = build(little, big, &[d], &[0]); + let img = decode_ok(&b.bytes); + assert_eq!(img.data(), &want[..], "tiles: little {little} big {big} bits {bits} spp {spp} planar {planar}"); + let info = tiff_info(&b.bytes).unwrap(); + assert!(info.pages[0].tiled && info.pages[0].planar == planar); + } + } + } + } +} + +#[test] +fn planar_strips_at_every_depth() { + let (w, h) = (11usize, 9usize); + for spp in [3usize, 4] { + let t = truth(w * h * spp, 3); + for (little, big) in ORDERS { + for (bits, format, file, want, _) in depths(&t, little) { + for rows in [1usize, 4, 9] { + let mut d = Dir { + entries: base(w as u32, h as u32, bits, spp as u16, 2), + chunks: planar_strips(&file, w, h, spp, bits as usize / 8, rows), + tiled: false, + }; + d = d.tag(278, LONG, &[rows as u64]).tag(284, SHORT, &[2]).tag(339, SHORT, &[u64::from(format)]); + if spp == 4 { + d = d.tag(338, SHORT, &[2]); + } + let img = decode_ok(&build(little, big, &[d], &[0]).bytes); + assert_eq!(img.data(), &want[..], "planar strips: little {little} big {big} bits {bits} spp {spp} rows {rows}"); + } + } + } + } +} + +/// Extra samples that are not the alpha (ExtraSamples 0: a spot colour or an extra channel) are +/// not part of the image: they come back in `meta.extra_samples`, one plane each. +#[test] +fn extra_samples_that_are_not_alpha_are_kept_apart() { + let (w, h) = (5usize, 3usize); + let t = truth(w * h * 5, 4); + let rgb_of = |spp: usize, keep: usize| -> Vec { t.u8s[..w * h * spp].chunks(spp).flat_map(|p| p[..keep].to_vec()).collect() }; + let extra = |spp: usize, at: usize| vec![ExtraSample { kind: 0, data: t.u8s[..w * h * spp].chunks(spp).map(|p| p[at]).collect() }]; + for planar in [false, true] { + // RGB + one unspecified extra sample: RGB, and the sample as an extra plane. + let px = &t.u8s[..w * h * 4]; + let chunks = if planar { planar_strips(px, w, h, 4, 1, 2) } else { px.chunks(w * 4 * 2).map(<[u8]>::to_vec).collect() }; + let mut d = Dir { entries: base(w as u32, h as u32, 8, 4, 2), chunks, tiled: false }.tag(278, LONG, &[2]).tag(338, SHORT, &[0]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::Rgb); + assert_eq!(img.data(), &rgb_of(4, 3)[..], "planar {planar}"); + assert_eq!(img.meta.extra_samples, extra(4, 3), "planar {planar}"); + // RGB + alpha + an unspecified extra sample: RGBA and one extra plane. + let px = &t.u8s[..w * h * 5]; + let chunks = if planar { planar_strips(px, w, h, 5, 1, 2) } else { px.chunks(w * 5 * 2).map(<[u8]>::to_vec).collect() }; + let mut d = Dir { entries: base(w as u32, h as u32, 8, 5, 2), chunks, tiled: false }.tag(278, LONG, &[2]).tag(338, SHORT, &[2, 0]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let img = decode_ok(&build(false, true, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert_eq!(img.data(), &rgb_of(5, 4)[..], "planar {planar}"); + assert_eq!(img.meta.extra_samples, extra(5, 4), "planar {planar}"); + } + // Gray + alpha + extra: gray and alpha; gray + an unspecified extra: gray. Each keeps the + // extra sample as a plane. + let px = &t.u8s[..w * h * 3]; + let d = strips(w as u32, h as u32, 8, 3, 1, 3, px).tag(338, SHORT, &[2, 0]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.data(), &rgb_of(3, 2)[..]); + assert_eq!(img.meta.extra_samples, extra(3, 2)); + let px = &t.u8s[..w * h * 2]; + let d = strips(w as u32, h as u32, 8, 2, 1, 3, px).tag(338, SHORT, &[0]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &rgb_of(2, 1)[..]); + assert_eq!(img.meta.extra_samples, extra(2, 1)); +} + +/// A file may hold more extra samples than the `MAX_EXTRA_SAMPLES` a decode keeps (up to 64 +/// samples a pixel open): the first ones open, and a warning counts those left out, so nothing +/// is lost without notice. +#[test] +fn extra_samples_past_the_cap_are_reported() { + let (w, h) = (3usize, 2usize); + const EXTRAS: usize = MAX_EXTRA_SAMPLES + 4; + let spp = 3 + EXTRAS; + let px: Vec = (0..w * h * spp).map(|i| (i % spp) as u8).collect(); + let d = strips(w as u32, h as u32, 8, spp as u16, 2, h as u32, &px).tag(338, SHORT, &[0; EXTRAS]); + let bytes = build(true, false, &[d], &[0]).bytes; + let img = decode_ok(&bytes); + assert_eq!(img.layout(), ChannelLayout::Rgb); + assert_eq!(img.meta.extra_samples.len(), MAX_EXTRA_SAMPLES); + assert_eq!(img.meta.extra_samples.last().map(|e| e.data[0]), Some((3 + MAX_EXTRA_SAMPLES - 1) as u8)); + assert_eq!(img.warnings, vec![DecodeWarning::ExtraSamplesOverLimit { format: Format::Tiff, count: 4 }]); + assert!(has_unretained_source_data(&bytes).unwrap()); + let text = img.warnings[0].to_string(); + assert!(text.contains("4 extra TIFF file channels") && text.contains("past the first 16"), "{text}"); + // At the cap exactly, nothing is left out. + let spp = 3 + MAX_EXTRA_SAMPLES; + let px: Vec = (0..w * h * spp).map(|i| (i % spp) as u8).collect(); + let d = strips(w as u32, h as u32, 8, spp as u16, 2, h as u32, &px).tag(338, SHORT, &[0; MAX_EXTRA_SAMPLES]); + let bytes = build(true, false, &[d], &[0]).bytes; + let img = decode_ok(&bytes); + assert_eq!(img.meta.extra_samples.len(), MAX_EXTRA_SAMPLES); + assert!(img.warnings.is_empty(), "{:?}", img.warnings); + assert!(!has_unretained_source_data(&bytes).unwrap()); +} + +/// The guard follows decoder classification, including non-first, repeated or missing alpha tags. +#[test] +fn source_sample_cap_matches_the_decoder_at_layout_and_alpha_boundaries() { + for (photo, color) in [(1u16, 1usize), (2, 3), (5, 4), (8, 3), (9, 3)] { + for extras in [MAX_EXTRA_SAMPLES - 1, MAX_EXTRA_SAMPLES, MAX_EXTRA_SAMPLES + 1] { + for kind in 0..7 { + let alpha = matches!(kind, 1..=3); + let spp = color + extras + usize::from(alpha); + let px: Vec<_> = (0..spp).map(|i| i as u8 + 1).collect(); + let mut kinds = vec![0; spp - color]; + if alpha { + kinds[if kind == 2 { 1 } else { 0 }] = 2; + } + if kind == 3 { + kinds[1] = 1; + } + if kind == 4 { + kinds.push(2); + } + let mut dir = strips(1, 1, 8, spp as u16, photo, 1, &px); + if kind != 5 { + dir = dir.tag(338, SHORT, &kinds); + } + if kind == 6 { + dir = dir.tag(338, SHORT, &vec![2; spp - color]); + } + let bytes = build(true, false, &[dir], &[0]).bytes; + let image = decode(&bytes); + let guarded = has_unretained_source_data(&bytes); + match image { + Ok(image) => { + let dropped = image.warnings.iter().any(|w| matches!(w, DecodeWarning::ExtraSamplesOverLimit { .. })); + assert_eq!(guarded.unwrap(), dropped, "photo={photo} extras={extras} alpha tags={kind}"); + if kind <= 3 { + assert_eq!(dropped, extras > MAX_EXTRA_SAMPLES); + } + } + Err(_) if kind >= 4 => assert!(guarded.is_err(), "unsupported tags remain refused"), + Err(error) => panic!("supported photo={photo} extras={extras} tags={kind}: {error}"), + } + } + } + } +} + +/// Independent compressed zero rows satisfy the decoder's file-size safety bound. Inspection +/// follows import's default budget without allocating or decoding the declared whole image. +#[cfg(target_pointer_width = "64")] +#[test] +fn source_sample_guard_follows_whole_aux_and_banded_plain_budget_routes() { + for (size, bits, extras, guarded) in [(16384u32, 8u16, MAX_EXTRA_SAMPLES, true), (32768, 32, 0, false)] { + let samples = 3 + extras; + let entries = if extras == 0 { 11u16 } else { 12 }; + let bits_at = 8 + 2 + usize::from(entries) * 12 + 4; + let kinds_at = bits_at + samples * 2; + let offsets_at = kinds_at + extras * 2; + let counts_at = offsets_at + size as usize * 4; + let data_at = counts_at + size as usize * 4; + let sample = if bits == 32 { SampleType::F32 } else { SampleType::U8 }; + let mut image = Image::from_raw(size, 1, ChannelLayout::Rgb, sample, vec![0; size as usize * 3 * sample.bytes()]).unwrap(); + image.meta.extra_samples = (0..extras).map(|_| ExtraSample { kind: 0, data: vec![0; size as usize * sample.bytes()] }).collect(); + let encoded = encode(&image, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::Deflate, ..Default::default() }).unwrap(); + decode_ok(&encoded); + assert_eq!(&encoded[..2], b"II"); + let first = u32::from_le_bytes(encoded[4..8].try_into().unwrap()) as usize; + let n = u16::from_le_bytes(encoded[first..first + 2].try_into().unwrap()) as usize; + let field = |tag: u16| { + let at = (0..n).map(|i| first + 2 + i * 12).find(|&at| u16::from_le_bytes(encoded[at..at + 2].try_into().unwrap()) == tag).unwrap(); + assert_eq!(u32::from_le_bytes(encoded[at + 4..at + 8].try_into().unwrap()), 1); + u32::from_le_bytes(encoded[at + 8..at + 12].try_into().unwrap()) as usize + }; + let row = &encoded[field(273)..field(273) + field(279)]; + let mut tags = vec![ + (256u16, 4u16, 1u32, size), + (257, 4, 1, size), + (258, 3, samples as u32, bits_at as u32), + (259, 3, 1, field(259) as u32), + (262, 3, 1, 2), + (273, 4, size, offsets_at as u32), + (277, 3, 1, samples as u32), + (278, 4, 1, 1), + (279, 4, size, counts_at as u32), + (284, 3, 1, 1), + (339, 3, 1, if bits == 32 { 3 } else { 1 }), + ]; + if extras > 0 { + tags.push((338, 3, extras as u32, kinds_at as u32)); + } + tags.sort_unstable_by_key(|t| t.0); + let mut bytes = b"II\x2a\0\x08\0\0\0".to_vec(); + bytes.extend_from_slice(&entries.to_le_bytes()); + for (tag, ty, count, value) in tags { + bytes.extend_from_slice(&tag.to_le_bytes()); + bytes.extend_from_slice(&ty.to_le_bytes()); + bytes.extend_from_slice(&count.to_le_bytes()); + bytes.extend_from_slice(&value.to_le_bytes()); + } + bytes.extend_from_slice(&0u32.to_le_bytes()); + for _ in 0..samples { + bytes.extend_from_slice(&bits.to_le_bytes()); + } + for _ in 0..extras { + bytes.extend_from_slice(&0u16.to_le_bytes()); + } + for index in 0..size { + bytes.extend_from_slice(&((data_at + index as usize * row.len()) as u32).to_le_bytes()); + } + for _ in 0..size { + bytes.extend_from_slice(&(row.len() as u32).to_le_bytes()); + } + assert_eq!(bytes.len(), data_at); + for _ in 0..size { + bytes.extend_from_slice(row); + } + assert!(bytes.len() < 32 << 20); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), guarded); + } +} + +#[test] +fn alpha_after_an_unspecified_extra_sample_is_kept() { + // #1349: TIFF 6.0 lets alpha be any extra sample; ExtraSamples [0, 2] puts it second. + let (w, h) = (5usize, 3usize); + for (photometric, color, layout) in [(2u16, 3usize, ChannelLayout::Rgba), (5, 4, ChannelLayout::CmykA), (1, 1, ChannelLayout::GrayA)] { + let spp = color + 2; + let t = truth(w * h * spp, 13 + spp as u64); + for (little, big) in ORDERS { + for (bits, format, file, want_all, sample) in depths(&t, little) { + let bps = bits as usize / 8; + // The colour samples, then the second extra sample (the alpha). + let want: Vec = want_all.chunks(spp * bps).flat_map(|p| [&p[..color * bps], &p[(color + 1) * bps..]].concat()).collect(); + for planar in [false, true] { + let chunks = if planar { planar_strips(&file, w, h, spp, bps, 2) } else { file.chunks(w * spp * bps * 2).map(<[u8]>::to_vec).collect() }; + let mut d = Dir { entries: base(w as u32, h as u32, bits, spp as u16, photometric), chunks, tiled: false } + .tag(278, LONG, &[2]) + .tag(339, SHORT, &vec![u64::from(format); spp]) + .tag(338, SHORT, &[0, 2]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let img = decode_ok(&build(little, big, &[d], &[0]).bytes); + let what = format!("photometric {photometric} little {little} big {big} bits {bits} planar {planar}"); + assert_eq!((img.layout(), img.sample_type()), (layout, sample), "{what}"); + assert_eq!(img.data(), &want[..], "{what}"); + assert_eq!(img.warnings, [], "{what}"); + } + } + } + } + // The issue's file: 2x2 RGB, every pixel 200 100 50, then 17 (unspecified), then alpha 128. + let px: Vec = [200u8, 100, 50, 17, 128].repeat(4); + let d = strips(2, 2, 8, 5, 2, 2, &px).tag(338, SHORT, &[0, 2]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.data(), &[200u8, 100, 50, 128].repeat(4)[..]); + // Associated alpha in the same position is made straight. + let px: Vec = [100u8, 50, 25, 17, 128].repeat(4); + let d = strips(2, 2, 8, 5, 2, 2, &px).tag(338, SHORT, &[0, 1]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.data(), &[199u8, 100, 50, 128].repeat(4)[..]); +} + +#[test] +fn associated_alpha_is_made_straight() { + // Premultiplied: half-transparent mid gray is stored as 64 with alpha 128. + let px = [64u8, 128, 200, 255, 0, 0]; + let d = strips(3, 1, 8, 2, 1, 1, &px).tag(338, SHORT, &[1]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.data(), &[128, 128, 200, 255, 0, 0]); + let px16 = u16s(&[16384, 32768], false); + let d = strips(1, 1, 16, 2, 1, 1, &px16).tag(338, SHORT, &[1]); + let img = decode_ok(&build(false, false, &[d], &[0]).bytes); + assert_eq!(img.to_u16_samples().unwrap(), [32768, 32768]); +} + +#[test] +fn white_is_zero_opens_with_the_right_tones() { + // #691: 00 FF 00 in a WhiteIsZero file is white, black, white. + let b = build(true, false, &[strips(3, 1, 8, 1, 0, 1, &[0x00, 0xff, 0x00])], &[0]); + let ours = decode_ok(&b.bytes); + let oracle = image::load_from_memory_with_format(&b.bytes, image::ImageFormat::Tiff).unwrap().into_luma8(); + assert_eq!(oracle.as_raw(), &[255u8, 0, 255]); + assert_eq!(ours.data(), &oracle.as_raw()[..]); + // The BlackIsZero control keeps the stored bytes. + let b = build(true, false, &[strips(3, 1, 8, 1, 1, 1, &[0x00, 0xff, 0x00])], &[0]); + assert_eq!(decode_ok(&b.bytes).data(), &[0, 255, 0]); + // 16-bit, in both byte orders and BigTIFF. + for (little, big) in ORDERS { + let b = build(little, big, &[strips(2, 1, 16, 1, 0, 1, &u16s(&[0, 65535], little))], &[0]); + assert_eq!(decode_ok(&b.bytes).to_u16_samples().unwrap(), [65535, 0]); + } + // 1-bit (a fax-style scan): bit set = black. + let b = build(true, false, &[strips(8, 1, 1, 1, 0, 1, &[0b1010_0000])], &[0]); + assert_eq!(decode_ok(&b.bytes).data(), &[0, 255, 0, 255, 255, 255, 255, 255]); + // With alpha: the gray inverts, the alpha does not. + let b = build(true, false, &[strips(2, 1, 8, 2, 0, 1, &[0, 255, 255, 10]).tag(338, SHORT, &[2])], &[0]); + let img = decode_ok(&b.bytes); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.data(), &[255, 255, 0, 10]); + // 32-bit float. + let b = build(true, false, &[strips(2, 1, 32, 1, 0, 1, &f32s(&[0.0, 0.25], true)).tag(339, SHORT, &[3])], &[0]); + assert_eq!(decode_ok(&b.bytes).to_f32_samples().unwrap(), [1.0, 0.75]); +} + +#[test] +fn bigtiff_orientation_is_applied_like_classic_tiff() { + // #693: the same 2×1 gray image with Orientation 6, classic and BigTIFF, both byte orders. + for (little, big) in ORDERS { + let d = strips(2, 1, 8, 1, 1, 1, &[10, 200]).tag(274, SHORT, &[6]); + let b = build(little, big, &[d], &[0]); + assert_eq!(exif_orientation(&b.bytes), 6, "little {little} big {big}"); + assert_eq!(tiff_orientation(&b.bytes), 6); + let img = decode_ok(&b.bytes); + assert_eq!(img.dimensions(), (1, 2), "little {little} big {big}"); + assert_eq!(img.data(), &[10, 200]); + let kept = decode_with(&b.bytes, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(kept.dimensions(), (2, 1)); + } +} + +#[test] +fn palette_images_open_as_rgb() { + // Two 8-bit entries used: 0 = red, 1 = (16, 32, 48); the map has 256 entries per channel. + let mut map = vec![0u64; 768]; + map[0] = 0xffff; + map[1] = 16 * 257; + map[256 + 1] = 32 * 257; + map[512 + 1] = 48 * 257; + for (little, big) in ORDERS { + let d = strips(3, 1, 8, 1, 3, 1, &[0, 1, 0]).tag(320, SHORT, &map); + let img = decode_ok(&build(little, big, &[d], &[0]).bytes); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U8)); + assert_eq!(img.data(), &[255, 0, 0, 16, 32, 48, 255, 0, 0]); + } + // 4-bit indices. + let mut map = vec![0u64; 48]; + map[15] = 0xffff; + map[16 + 15] = 0x8000; + let d = strips(2, 1, 4, 1, 3, 1, &[0xf0]).tag(320, SHORT, &map); + assert_eq!(decode_ok(&build(true, false, &[d], &[0]).bytes).data(), &[255, 128, 0, 0, 0, 0]); + // A missing or short colour map is an error. + let d = strips(2, 1, 8, 1, 3, 1, &[0, 1]); + assert!(decode(&build(true, false, &[d], &[0]).bytes).is_err()); + let d = strips(2, 1, 8, 1, 3, 1, &[0, 1]).tag(320, SHORT, &[1, 2, 3]); + assert!(decode(&build(true, false, &[d], &[0]).bytes).is_err()); +} + +#[test] +fn wide_and_odd_sample_formats() { + for (little, big) in ORDERS { + let e = |v: u32| if little { v.to_le_bytes() } else { v.to_be_bytes() }; + // 32-bit unsigned keeps its top 16 bits. + let px: Vec = [0x1234_5678u32, 0xffff_0000].iter().flat_map(|&v| e(v)).collect(); + let img = decode_ok(&build(little, big, &[strips(2, 1, 32, 1, 1, 1, &px)], &[0]).bytes); + assert_eq!(img.to_u16_samples().unwrap(), [0x1234, 0xffff]); + // 64-bit float becomes 32-bit float. + let px: Vec = [0.5f64, 2.0].iter().flat_map(|v| if little { v.to_le_bytes() } else { v.to_be_bytes() }).collect(); + let img = decode_ok(&build(little, big, &[strips(2, 1, 64, 1, 1, 1, &px).tag(339, SHORT, &[3])], &[0]).bytes); + assert_eq!(img.to_f32_samples().unwrap(), [0.5, 2.0]); + // 16-bit float stays half. + let px: Vec = [f16::from_f32(0.25), f16::ONE].iter().flat_map(|v| if little { v.to_le_bytes() } else { v.to_be_bytes() }).collect(); + let img = decode_ok(&build(little, big, &[strips(2, 1, 16, 1, 1, 1, &px).tag(339, SHORT, &[3])], &[0]).bytes); + assert_eq!(img.sample_type(), SampleType::F16); + assert_eq!(img.to_normalized(), [0.25, 1.0]); + } + // 2- and 4-bit gray, rows padded to a byte, in several strips. + let img = decode_ok(&build(true, false, &[strips(3, 2, 2, 1, 1, 1, &[0b0001_1011, 0b1110_0100])], &[0]).bytes); + assert_eq!(img.data(), &[0, 85, 170, 255, 170, 85]); + let img = decode_ok(&build(false, true, &[strips(3, 2, 4, 1, 1, 1, &[0x0f, 0x80, 0x12, 0x30])], &[0]).bytes); + assert_eq!(img.data(), &[0, 255, 136, 17, 34, 51]); + // Signed integers are unsupported, not a crash. + let d = strips(2, 1, 16, 1, 1, 1, &[0, 0, 0, 0]).tag(339, SHORT, &[2]); + assert!(matches!(decode(&build(true, false, &[d], &[0]).bytes), Err(CodecError::Unsupported { .. }))); +} + +/// A flat gray page of `w`×`h` at `level`, with an optional NewSubfileType. +fn gray_page(w: u32, h: u32, level: u8, kind: Option) -> Dir { + let d = strips(w, h, 8, 1, 1, h, &vec![level; (w * h) as usize]); + match kind { + Some(k) => d.tag(254, LONG, &[k]), + None => d, + } +} + +#[test] +fn every_ifd_and_subifd_is_listed_and_decodable() { + for (little, big) in ORDERS { + let sub_ty = if big { IFD8 } else { IFD }; + let dirs = vec![ + gray_page(4, 3, 1, Some(1)), // 0: thumbnail first + gray_page(9, 7, 2, None).sub_ifds(sub_ty, &[4, 5]), // 1: page A, two SubIFDs + gray_page(9, 7, 3, Some(4)), // 2: its transparency mask + gray_page(5, 5, 4, Some(2)), // 3: page B (multi-page bit) + gray_page(3, 2, 5, Some(1)), // 4: SubIFD: reduced copy of A + gray_page(2, 1, 6, Some(1)), // 5: SubIFD: smaller copy + ]; + let b = build(little, big, &dirs, &[0, 1, 2, 3]); + let info = tiff_info(&b.bytes).unwrap(); + assert!(info.complete); + let summary: Vec<(u32, Option, TiffPageKind)> = info.pages.iter().map(|p| (p.width, p.parent, p.kind)).collect(); + use TiffPageKind::*; + assert_eq!( + summary, + [(4, None, ReducedResolution), (9, None, Page), (3, Some(1), ReducedResolution), (2, Some(1), ReducedResolution), (9, None, Mask), (5, None, Page)] + ); + assert_eq!(info.pages[1].ifd_offset, b.ifd_at[1]); + assert_eq!((info.default_page(), info.page_count()), (Some(1), 2)); + // Like Photoshop: the first full-resolution page opens; the thumbnail is skipped. + let img = decode_ok(&b.bytes); + assert_eq!((img.dimensions(), img.data()[0]), ((9, 7), 2)); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(2) }]); + // Every directory decodes on request. + for (i, (w, level)) in [(4, 1u8), (9, 2), (3, 5), (2, 6), (9, 3), (5, 4)].into_iter().enumerate() { + let img = decode_tiff_page(&b.bytes, i, &DecodeOptions::default()).unwrap(); + assert_eq!((img.width(), img.data()[0]), (w, level), "page {i} little {little} big {big}"); + } + assert!(matches!(decode_tiff_page(&b.bytes, 6, &DecodeOptions::default()), Err(CodecError::InvalidImage(_)))); + } +} + +#[test] +fn a_file_of_only_thumbnails_still_opens() { + let b = build(true, false, &[gray_page(4, 3, 7, Some(1))], &[0]); + assert_eq!(tiff_info(&b.bytes).unwrap().default_page(), Some(0)); + assert_eq!(decode_ok(&b.bytes).data()[0], 7); +} + +#[test] +fn cyclic_chains_and_subifds_terminate() { + for (little, big) in ORDERS { + // IFD1 points back to IFD0. + let mut b = build(little, big, &[gray_page(2, 2, 1, None), gray_page(2, 2, 2, None)], &[0, 1]); + let first = b.ifd_at[0]; + set_next(&mut b, little, big, 1, first); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (2, false)); + let img = decode_ok(&b.bytes); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: None }]); + // A directory pointing at itself. + let mut b = build(little, big, &[gray_page(2, 2, 1, None)], &[0]); + let first = b.ifd_at[0]; + set_next(&mut b, little, big, 0, first); + assert_eq!(tiff_info(&b.bytes).unwrap().pages.len(), 1); + assert_eq!(decode_ok(&b.bytes).warnings, []); + // A SubIFD listing its own parent, and one listing itself. + let sub_ty = if big { IFD8 } else { IFD }; + let b = build(little, big, &[gray_page(2, 2, 1, None).sub_ifds(sub_ty, &[1]), gray_page(1, 1, 2, Some(1)).sub_ifds(sub_ty, &[0, 1])], &[0]); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (2, false)); + decode_ok(&b.bytes); + // A next pointer past the end, or into the header. + for to in [b.bytes.len() as u64 + 100, 3, u64::MAX] { + let mut b = build(little, big, &[gray_page(2, 2, 1, None), gray_page(2, 2, 2, None)], &[0, 1]); + set_next(&mut b, little, big, 1, to); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (2, false)); + decode_ok(&b.bytes); + } + } +} + +#[test] +fn the_chain_walk_is_capped() { + // 10 001 tiny directories: the walk stops at 10 000 and says the list is incomplete. + let dirs: Vec = (0..10_001).map(|_| gray_page(1, 1, 9, None)).collect(); + let chain: Vec = (0..dirs.len()).collect(); + let b = build(true, false, &dirs, &chain); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (10_000, false)); + assert_eq!(decode_ok(&b.bytes).warnings, [DecodeWarning::MorePages { total: None }]); +} + +/// A partial child walk remains usable for decoding, but cannot authorize destructive saves. +#[test] +fn incomplete_subifd_lists_and_depth_are_reported() { + for (little, big) in ORDERS { + let ty = if big { IFD8 } else { IFD }; + for depth in [4usize, 5] { + let dirs: Vec<_> = (0..=depth) + .map(|i| { + let dir = gray_page( + 2, + 2, + i as u8, + Some(if i == 0 { + 0 + } else if i == depth { + 4 + } else { + 1 + }), + ); + if i < depth { dir.sub_ifds(ty, &[i + 1]) } else { dir } + }) + .collect(); + let b = build(little, big, &dirs, &[0]); + assert_eq!(tiff_info(&b.bytes).unwrap().complete, depth == 4); + decode_ok(&b.bytes); + if depth == 4 { + assert!(has_unretained_source_data(&b.bytes).unwrap()); + } else { + assert!(has_unretained_source_data(&b.bytes).is_err()); + } + } + for count in [0usize, 1, 256, 257] { + let b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, ty, &vec![0; count])], &[0]); + assert_eq!(tiff_info(&b.bytes).unwrap().complete, count <= 256, "null child slots {count}"); + decode_ok(&b.bytes); + } + for invalid in [SHORT, u16::MAX] { + let b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, invalid, &[0])], &[0]); + assert!(!tiff_info(&b.bytes).unwrap().complete); + assert!(has_unretained_source_data(&b.bytes).is_err()); + decode_ok(&b.bytes); + } + let b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, ty, &[0]).tag(330, ty, &[0])], &[0]); + assert!(!tiff_info(&b.bytes).unwrap().complete, "duplicate child declarations"); + let mut b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, ty, &[0, 0])], &[0]); + let (entry, ..) = find_entry(&b, little, big, 0, 330).unwrap(); + let offset = b.bytes.len() as u64 - 1; + let value = if little { offset.to_le_bytes() } else { offset.to_be_bytes() }; + let (field, width) = if big { (entry + 12, 8) } else { (entry + 8, 4) }; + b.bytes[field..field + width].copy_from_slice(if little { &value[..width] } else { &value[8 - width..] }); + assert!(!tiff_info(&b.bytes).unwrap().complete, "truncated child list"); + assert!(has_unretained_source_data(&b.bytes).is_err()); + decode_ok(&b.bytes); + } +} + +#[test] +fn image_data_cut_off_decodes_with_a_warning() { + let (w, h) = (4u32, 8u32); + let px: Vec = (0..32).collect(); + for (little, big) in ORDERS { + // The last strip starts past the end of the file: its rows stay empty. + let mut b = build(little, big, &[strips(w, h, 8, 1, 1, 2, &px)], &[0]); + let len = b.bytes.len() as u64; + patch_value(&mut b, little, big, 0, 273, 3, len + 1000); + let img = decode_ok(&b.bytes); + assert_eq!(img.warnings, [DecodeWarning::Truncated { format: Format::Tiff }]); + assert_eq!(&img.data()[..24], &px[..24]); + assert_eq!(&img.data()[24..], &[0; 8]); + // A strip running into the end of the file. + let mut b = build(little, big, &[strips(w, h, 8, 1, 1, 2, &px)], &[0]); + let len = b.bytes.len() as u64; + patch_value(&mut b, little, big, 0, 273, 1, len - 3); + assert_eq!(decode_ok(&b.bytes).warnings, [DecodeWarning::Truncated { format: Format::Tiff }]); + // Every strip missing: nothing to show, an error. + let mut b = build(little, big, &[strips(w, h, 8, 1, 1, 2, &px)], &[0]); + let len = b.bytes.len() as u64; + for i in 0..4 { + patch_value(&mut b, little, big, 0, 273, i, len + 7); + } + assert!(decode(&b.bytes).is_err()); + } +} + +#[test] +fn hostile_sizes_and_counts_are_refused_without_allocating() { + let tight = DecodeOptions { limits: Limits { max_alloc: 64 << 20, ..Limits::default() }, ..Default::default() }; + for (little, big) in ORDERS { + let px = vec![7u8; 16]; + let fresh = || build(little, big, &[strips(4, 4, 8, 1, 1, 1, &px)], &[0]); + // Huge declared dimensions: over the limits, or far more than the file can hold. + for (wv, hv) in [(1u64 << 31, 4u64), (4, 1 << 31), (u32::MAX as u64, u32::MAX as u64), (200_000, 200_000), (30_000, 30_000)] { + let mut b = fresh(); + patch_value(&mut b, little, big, 0, 256, 0, wv); + patch_value(&mut b, little, big, 0, 257, 0, hv); + assert!(decode(&b.bytes).is_err(), "{wv}x{hv}"); + assert!(decode_with(&b.bytes, &DecodeOptions { limits: Limits::none(), ..Default::default() }).is_err(), "{wv}x{hv} without limits"); + } + // A strip-offset count far past the end of the file. + let mut b = fresh(); + patch_count(&mut b, little, big, 0, 273, 1 << 30); + assert!(matches!(decode(&b.bytes), Err(CodecError::Malformed { .. }))); + let mut b = fresh(); + patch_count(&mut b, little, big, 0, 258, u32::MAX as u64); + assert!(decode(&b.bytes).is_err()); + // Zero rows per strip, zero samples, absurd samples per pixel. + for (tag, v) in [(278u16, 0u64), (277, 0), (277, 60_000)] { + let mut b = fresh(); + patch_value(&mut b, little, big, 0, tag, 0, v); + assert!(decode(&b.bytes).is_err(), "tag {tag} = {v}"); + } + // Absurd tiles over a tiny image, chunky and planar. + for planar in [false, true] { + for (tw, th) in [(1u64 << 30, 16u64), (16, 1 << 30), (u32::MAX as u64, u32::MAX as u64)] { + let spp = if planar { 3 } else { 1 }; + let mut d = Dir { entries: base(4, 4, 8, spp, if planar { 2 } else { 1 }), chunks: vec![vec![1u8; 64]; spp as usize], tiled: true }; + d = d.tag(322, LONG, &[tw]).tag(323, LONG, &[th]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let b = build(little, big, &[d], &[0]); + let _ = decode_with(&b.bytes, &tight); + let _ = decode(&b.bytes); + } + } + } + // A BigTIFF directory claiming 2^60 entries. + let mut b = build(true, true, &[gray_page(2, 2, 1, None)], &[0]); + let at = b.ifd_at[0] as usize; + b.bytes[at..at + 8].copy_from_slice(&(1u64 << 60).to_le_bytes()); + assert!(tiff_info(&b.bytes).is_err()); + assert!(decode(&b.bytes).is_err()); +} + +#[test] +fn banded_decode_matches_the_encoder_on_larger_images() { + // Many strips decoded in parallel bands, at every depth and compression. + for (layout, sample) in [ + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U16), + (ChannelLayout::Gray, SampleType::F32), + (ChannelLayout::Cmyk, SampleType::U8), + ] { + let img = common::synth(613, 411, layout, sample, 3, 0.2); + for c in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); + let back = decode_ok(&bytes); + assert_eq!(back.data(), img.data(), "{layout:?} {sample:?} {c:?}"); + } + } + // Tiles across many bands. + let t = truth(300 * 260 * 3, 77); + let d = Dir { entries: base(300, 260, 16, 3, 2), chunks: tile_split(&u16s(&t.u16s, false), 300, 260, 3, 2, 64, 32, false), tiled: true } + .tag(322, SHORT, &[64]) + .tag(323, SHORT, &[32]); + assert_eq!(decode_ok(&build(false, true, &[d], &[0]).bytes).data(), &ne16(&t.u16s)[..]); +} + +#[test] +fn bigtiff_is_written_on_request_and_reads_back() { + let mut img = common::synth(23, 17, ChannelLayout::Rgba, SampleType::U16, 2, 0.3); + img.meta.photoshop_layers = Some(b"Adobe Photoshop Document Data Block\0MIB8ryaL".to_vec()); + img.meta.photoshop_resources = Some(b"8BIM\x04\x04\0\0\0\0\0\0".to_vec()); + for c in [TiffCompression::None, TiffCompression::Deflate, TiffCompression::Lzw] { + let classic = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); + let big = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, tiff_bigtiff: true, ..Default::default() }).unwrap(); + let version = |b: &[u8]| if b.starts_with(b"II") { u16::from_le_bytes([b[2], b[3]]) } else { u16::from_be_bytes([b[2], b[3]]) }; + assert_eq!((version(&classic), version(&big)), (42, 43)); + assert!(tiff_info(&big).unwrap().big_tiff); + let back = decode_ok(&big); + assert_eq!(back.data(), img.data(), "{c:?}"); + assert_eq!(back.meta.photoshop_layers, img.meta.photoshop_layers); + assert_eq!(back.meta.photoshop_resources, img.meta.photoshop_resources); + assert_eq!(tiff_photoshop_tags(&big), (img.meta.photoshop_resources.as_deref(), img.meta.photoshop_layers.as_deref())); + } +} + +/// Small files covering every structure: multi-IFD, SubIFDs, BigTIFF both orders, tiles, planar. +fn zoo() -> Vec> { + let mut v = Vec::new(); + let t = truth(6 * 5 * 4, 5); + for (little, big) in ORDERS { + let sub_ty = if big { IFD8 } else { IFD }; + let tiles = Dir { entries: base(6, 5, 8, 3, 2), chunks: tile_split(&t.u8s, 6, 5, 3, 1, 16, 16, true), tiled: true } + .tag(322, SHORT, &[16]) + .tag(323, SHORT, &[16]) + .tag(284, SHORT, &[2]); + let planar = Dir { entries: base(6, 5, 16, 4, 2), chunks: planar_strips(&u16s(&t.u16s, little), 6, 5, 4, 2, 2), tiled: false } + .tag(278, SHORT, &[2]) + .tag(284, SHORT, &[2]) + .tag(338, SHORT, &[1]); + let dirs = vec![gray_page(3, 2, 1, Some(1)), tiles.sub_ifds(sub_ty, &[3]), planar, gray_page(2, 2, 4, Some(1)).tag(274, SHORT, &[6])]; + v.push(build(little, big, &dirs, &[0, 1, 2]).bytes); + v.push(build(little, big, &[strips(5, 3, 1, 1, 0, 2, &[0xa5; 3])], &[0]).bytes); + } + v +} + +fn poke(bytes: &[u8]) { + let _ = decode(bytes); + let _ = decode_with(bytes, &DecodeOptions { limits: Limits { max_alloc: 16 << 20, ..Limits::default() }, ..Default::default() }); + let _ = exif_orientation(bytes); + let _ = tiff_orientation(bytes); + if let Ok(info) = tiff_info(bytes) { + for i in 0..info.pages.len().min(8) { + let _ = decode_tiff_page(bytes, i, &DecodeOptions::default()); + } + } +} + +#[test] +fn zoo_decodes_and_never_panics_when_cut_or_corrupted() { + for f in zoo() { + let info = tiff_info(&f).unwrap(); + for i in 0..info.pages.len() { + decode_tiff_page(&f, i, &DecodeOptions::default()).unwrap(); + } + for cut in 0..f.len() { + poke(&f[..cut]); + } + for i in 0..f.len() { + for flip in [0xffu8, 0x80, 0x01] { + let mut g = f.clone(); + g[i] ^= flip; + poke(&g); + } + } + let mut r = Rng::new(f.len() as u64); + for _ in 0..300 { + let mut g = f.clone(); + for _ in 0..4 { + let i = (r.next_u64() as usize) % g.len(); + g[i] = r.next_u64() as u8; + } + poke(&g); + } + } +} + +/// A longer random-mutation run over the zoo (ignored by default; `-- --ignored`, with +/// `TIFF_FUZZ_ITERS` to change the count). +#[test] +#[ignore = "long fuzz run"] +fn zoo_long_random_mutations() { + let iters: usize = std::env::var("TIFF_FUZZ_ITERS").ok().and_then(|v| v.parse().ok()).unwrap_or(20_000); + let mut r = Rng::new(0x5eed); + let zoo = zoo(); + for k in 0..iters { + let mut g = zoo[k % zoo.len()].clone(); + for _ in 0..1 + r.next_u64() % 8 { + let i = (r.next_u64() as usize) % g.len(); + g[i] = match r.next_u64() % 4 { + 0 => 0, + 1 => 0xff, + _ => r.next_u64() as u8, + }; + } + poke(&g); + } +} diff --git a/crates/image-codecs/tests/it/tiff_photoshop.rs b/crates/image-codecs/tests/it/tiff_photoshop.rs new file mode 100644 index 000000000..698a4296d --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_photoshop.rs @@ -0,0 +1,436 @@ +//! Photoshop's private TIFF tags (34377 image resources, 37724 ImageSourceData) travel through +//! `Metadata` as opaque bytes: written when present, read from either byte order and from +//! BigTIFF, and never a panic or an allocation on malformed input. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn with_tags(layout: ChannelLayout, sample: SampleType) -> Image { + let mut img = synth(9, 7, layout, sample, 5, 0.3); + img.meta.photoshop_resources = Some(b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\x48\x00\x00\x00\x01\x00\x01\x00\x48\x00\x00\x00\x01\x00\x01".to_vec()); + let mut layers = b"Adobe Photoshop Document Data Block\0".to_vec(); + layers.extend_from_slice(b"MIB8ryaL"); + layers.extend_from_slice(&4u32.to_le_bytes()); + layers.extend_from_slice(&[0, 0, 0, 0]); + img.meta.photoshop_layers = Some(layers); + img +} + +#[test] +fn tags_round_trip_through_the_encoder() { + for (layout, sample) in [ + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U16), + (ChannelLayout::Gray, SampleType::F32), + (ChannelLayout::CmykA, SampleType::U8), + ] { + for compression in [TiffCompression::None, TiffCompression::Deflate, TiffCompression::Lzw, TiffCompression::PackBits] { + let img = with_tags(layout, sample); + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: compression, ..Default::default() }).unwrap(); + // The encoder writes the running target's byte order. + assert_eq!(bytes.starts_with(b"II"), tiff_writes_little_endian()); + let (res, layers) = tiff_photoshop_tags(&bytes); + assert_eq!(res, img.meta.photoshop_resources.as_deref()); + assert_eq!(layers, img.meta.photoshop_layers.as_deref()); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.photoshop_resources, img.meta.photoshop_resources, "{layout:?} {sample:?} {compression:?}"); + assert_eq!(back.meta.photoshop_layers, img.meta.photoshop_layers); + assert_eq!(back.data(), img.data()); + } + } +} + +#[test] +fn extra_samples_round_trip() { + for (layout, sample) in [ + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U8), + (ChannelLayout::Gray, SampleType::U16), + (ChannelLayout::Cmyk, SampleType::F32), + ] { + let mut img = synth(4, 3, layout, sample, 2, 0.4); + let n = 4 * 3; + let b = sample.bytes(); + let data: Vec = (0..n * b).map(|i| (i * 17) as u8).collect(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: data.clone() }]; + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), layout, "{layout:?} {sample:?}"); + assert_eq!(back.data(), img.data(), "{layout:?} {sample:?}"); + assert_eq!(back.meta.extra_samples.len(), 1, "{layout:?} {sample:?}"); + assert_eq!(back.meta.extra_samples[0].kind, 0, "{layout:?} {sample:?}"); + assert_eq!(back.meta.extra_samples[0].data, data, "{layout:?} {sample:?}"); + } +} + +/// Tall enough that the default strip size (about 1 MB) cannot hold every row, so a writer +/// that only fills the first strip, or predicts across the wrong sample count, cannot hide. +/// The spot plane is checked with the `tiff` crate, not PhotoCraft's decoder. +#[test] +fn multi_strip_spot_round_trip_and_dump() { + let w = 800u32; + let h = 400u32; + let n = (w as usize) * (h as usize); + for (layout, channels) in [(ChannelLayout::Rgb, 3usize), (ChannelLayout::Rgba, 4usize)] { + let mut px = vec![0u8; n * channels]; + let mut spot = vec![0u8; n]; + for y in 0..h as usize { + for x in 0..w as usize { + let i = y * w as usize + x; + px[i * channels] = (x % 251) as u8; + px[i * channels + 1] = (y % 251) as u8; + px[i * channels + 2] = 40; + if channels == 4 { + px[i * channels + 3] = if x > 20 && x < 780 && y > 10 && y + 10 < h as usize { 255 } else { 0 }; + } + spot[i] = ((x * 3 + y * 17) % 251) as u8; + } + } + let mut img = Image::from_u8(w, h, layout, px).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: spot.clone() }]; + for compression in [TiffCompression::None, TiffCompression::Deflate, TiffCompression::Lzw, TiffCompression::PackBits] { + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: compression, ..Default::default() }).unwrap(); + let label = format!("{layout:?} {compression:?}"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.extra_samples.len(), 1, "{label}"); + assert_eq!(back.meta.extra_samples[0].data, spot, "own decoder {label}"); + assert_eq!(back.data(), img.data(), "own decoder rgb {label}"); + let mut dec = tiff::decoder::Decoder::new(std::io::Cursor::new(bytes.as_slice())).unwrap(); + let spp: u16 = dec.get_tag_unsigned(tiff::tags::Tag::SamplesPerPixel).unwrap(); + let bps = dec.get_tag_u16_vec(tiff::tags::Tag::BitsPerSample).unwrap(); + let extra = dec.get_tag_u16_vec(tiff::tags::Tag::ExtraSamples).unwrap(); + let rows = dec.find_tag_unsigned::(tiff::tags::Tag::RowsPerStrip).unwrap(); + assert_eq!(bps.len(), spp as usize, "{label}"); + assert_eq!(spp as usize, channels + 1, "{label}"); + assert_eq!(*extra.last().unwrap(), 0, "{label} spot ExtraSamples {extra:?}"); + assert!(rows.is_some_and(|r| r < h) || compression == TiffCompression::PackBits, "{label} rows {rows:?}"); + let patched = min_is_black(&bytes); + let mut indep = tiff::decoder::Decoder::new(std::io::Cursor::new(patched.as_slice())).unwrap(); + let tiff::decoder::DecodingResult::U8(samples) = indep.read_image().unwrap() else { panic!("{label} not u8") }; + let spp = spp as usize; + assert_eq!(samples.len(), n * spp, "{label}"); + let mut bad = 0usize; + let mut first = None; + for (i, want) in spot.iter().enumerate() { + let got = samples.get(i * spp + spp - 1).copied(); + if got != Some(*want) { + bad += 1; + first.get_or_insert((i, got)); + } + } + assert_eq!(bad, 0, "{label} independent reader, first {first:?}"); + } + } +} + +/// Retag photometric interpretation 262 as MinIsBlack so `tiff`'s decoder returns every sample. +fn min_is_black(src: &[u8]) -> Vec { + let mut bytes = src.to_vec(); + let le = bytes.first().copied() == Some(b'I'); + let ru16 = |b: &[u8], i: usize| -> Option { + let s = b.get(i..i + 2)?; + Some(if le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + }; + let ru32 = |b: &[u8], i: usize| -> Option { + let s = b.get(i..i + 4)?; + Some(if le { u32::from_le_bytes(s.try_into().ok()?) } else { u32::from_be_bytes(s.try_into().ok()?) }) + }; + let Some(ifd) = ru32(&bytes, 4) else { return bytes }; + let Some(n) = ru16(&bytes, ifd as usize) else { return bytes }; + for e in 0..n as usize { + let at = ifd as usize + 2 + e * 12; + if ru16(&bytes, at) == Some(262) { + let value = if le { 1u16.to_le_bytes() } else { 1u16.to_be_bytes() }; + if let Some(slot) = bytes.get_mut(at + 8..at + 10) { + slot.copy_from_slice(&value); + } + break; + } + } + bytes +} + +#[test] +fn more_than_sixteen_extra_samples_is_refused() { + let mut img = synth(2, 2, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = (0..MAX_EXTRA_SAMPLES + 1).map(|_| ExtraSample { kind: 0, data: vec![0; 4] }).collect(); + assert!(encode(&img, Format::Tiff, &EncodeOptions::default()).is_err()); +} + +#[test] +fn sixteen_extra_samples_round_trip() { + for (layout, sample, base) in [ + (ChannelLayout::Rgb, SampleType::U8, 3usize), + (ChannelLayout::Rgba, SampleType::U8, 4), + (ChannelLayout::Gray, SampleType::U16, 1), + (ChannelLayout::Cmyk, SampleType::F32, 4), + ] { + let mut img = synth(2, 2, layout, sample, 1, 0.0); + let n = 2 * 2 * sample.bytes(); + img.meta.extra_samples = (0..MAX_EXTRA_SAMPLES).map(|k| ExtraSample { kind: 0, data: vec![k as u8; n] }).collect(); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), layout, "{layout:?} {sample:?} base {base}"); + assert_eq!(back.data(), img.data(), "{layout:?}"); + assert_eq!(back.meta.extra_samples.len(), MAX_EXTRA_SAMPLES, "{layout:?}"); + assert_eq!(back.meta.extra_samples[7].kind, 0, "{layout:?}"); + assert_eq!(back.meta.extra_samples[15].data, vec![15; n], "{layout:?}"); + } +} + +/// An extra plane holds exactly one sample per pixel of the image's sample type. A plane of any +/// other length is refused rather than written: the writer copies one sample per pixel, so a +/// longer plane would lose its tail and a shorter one would run out mid-image. +#[test] +fn wrong_sized_extra_planes_are_refused() { + let (w, h) = (2u32, 1u32); + for (label, plane) in [("too long", vec![10u8, 20, 30]), ("too short", vec![10u8]), ("empty", Vec::new())] { + let mut img = synth(w, h, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: plane.clone() }]; + let err = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap_err(); + assert!(matches!(err, CodecError::Encode { .. }), "{label}: {err}"); + assert!(err.to_string().contains("extra sample 0") && err.to_string().contains("exactly 2"), "{label}: {err}"); + } + // The exact length still writes, and comes back unchanged. + let plane = vec![10u8, 20]; + let mut img = synth(w, h, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: plane.clone() }]; + let back = decode(&encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.meta.extra_samples, vec![ExtraSample { kind: 0, data: plane }]); +} + +/// The sample-type conversion an encode performs must not launder a wrong-sized plane: float Lab +/// is stored as CIELab at 16 bits, and `Image::convert` leaves a plane whose length does not match +/// the image alone, so a 7- or 9-byte plane for two pixels would otherwise reach the writer +/// reinterpreted at the new width. +#[test] +fn a_wrong_sized_plane_is_refused_before_a_sample_type_conversion() { + let (w, h) = (2u32, 1u32); + // Two pixels at 32-bit float are 8 bytes; the file's 16-bit samples are 4. + for (label, len) in [("short", 7usize), ("long", 9)] { + let mut img = synth(w, h, ChannelLayout::Lab, SampleType::F32, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![0; len] }]; + let err = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap_err(); + assert!(matches!(err, CodecError::Encode { .. }), "{label}: {err}"); + assert!(err.to_string().contains("exactly 8"), "{label}: {err}"); + } +} + +/// Extra samples count against `max_alloc` with the image (their interleaved buffer and the +/// planes split from it): a budget that holds the RGB image but not sixteen extra planes opens +/// the image without them, with a warning, instead of allocating past it. +#[test] +fn extra_samples_count_against_the_allocation_limit() { + // Big enough for several strips (about 1 MB each): one strip of all 19 samples fits the budget + // of the image alone, as in real files. + let (w, h) = (1024u32, 1024u32); + let n = (w * h) as usize; + let mut img = synth(w, h, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = (0..MAX_EXTRA_SAMPLES).map(|k| ExtraSample { kind: 0, data: vec![k as u8; n] }).collect(); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let with = |max_alloc: u64| decode_with(&bytes, &DecodeOptions { limits: Limits { max_alloc, ..Limits::default() }, ..DecodeOptions::default() }); + // RGB is 3 bytes a pixel; the extra samples add 2 x 16 more. + let image_only = 3 * n as u64; + let with_extras = (3 + 2 * MAX_EXTRA_SAMPLES as u64) * n as u64; + let back = with(with_extras).expect("everything fits"); + assert_eq!(back.meta.extra_samples.len(), MAX_EXTRA_SAMPLES); + assert!(back.warnings.is_empty()); + // The image fits, its extra samples don't: it opens as before they were read, with a warning. + for max_alloc in [image_only, with_extras - 1] { + let only = with(max_alloc).expect("the image alone fits"); + assert!(only.meta.extra_samples.is_empty()); + assert_eq!(only.warnings, vec![DecodeWarning::ExtraSamplesDropped { format: Format::Tiff, count: MAX_EXTRA_SAMPLES as u32 }]); + assert_eq!(only.data(), back.data()); + } + assert!(matches!(with(image_only - 1), Err(CodecError::LimitExceeded(_))), "the image itself must still fit"); +} + +/// The value field of a little-endian classic TIFF's ExtraSamples entry (tag 338, SHORT) holding +/// two values, so they sit in the entry itself. +fn extra_samples_values(bytes: &mut [u8]) -> &mut [u8] { + assert_eq!(&bytes[..4], b"II*\0"); + let entry = [0x52, 0x01, 3, 0, 2, 0, 0, 0]; + let at = bytes.windows(8).position(|w| w == entry).expect("an ExtraSamples entry with two values"); + &mut bytes[at + 8..at + 12] +} + +/// ExtraSamples values above 2 are reserved (TIFF 6.0, p. 31): they read as 0 (unspecified) and +/// are written as 0, so a save never carries a reserved value. +#[test] +fn reserved_extra_sample_kinds_read_as_unspecified() { + let n = 4 * 4; + let mut img = synth(4, 4, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![7; n] }, ExtraSample { kind: 0, data: vec![9; n] }]; + let mut bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + // A file from another writer with reserved values 3 and 65535. + let values = extra_samples_values(&mut bytes); + assert_eq!(values, [0, 0, 0, 0]); + values.copy_from_slice(&[3, 0, 0xFF, 0xFF]); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.extra_samples.iter().map(|e| e.kind).collect::>(), [0, 0]); + assert_eq!(back.layout(), ChannelLayout::Rgb, "a reserved value is no alpha"); + assert_eq!(back.meta.extra_samples[0].data, vec![7; n]); + assert_eq!(back.meta.extra_samples[1].data, vec![9; n]); + + // An image that carries a reserved kind saves it as 0. + img.meta.extra_samples[0].kind = 3; + img.meta.extra_samples[1].kind = u16::MAX; + let mut bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(extra_samples_values(&mut bytes), [0, 0, 0, 0]); +} + +/// Converting an image's sample type converts its extra samples with it, so they keep matching +/// the image: a 16-bit float TIFF's spot planes survive the 32-bit float a layered import uses. +#[test] +fn extra_samples_follow_a_sample_type_conversion() { + let (w, h) = (3u32, 2u32); + let n = (w * h) as usize; + let values = [0.0f32, 0.25, 0.5, 0.75, 1.0, 0.125]; + let half = |v: f32| f16::from_f32(v).to_bits().to_ne_bytes(); + let rgb: Vec = (0..n * 3).flat_map(|_| half(0.5)).collect(); + let mut img = Image::from_raw(w, h, ChannelLayout::Rgb, SampleType::F16, rgb).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: values.iter().flat_map(|v| half(*v)).collect() }]; + + let wide = img.convert(ChannelLayout::Rgb, SampleType::F32); + let plane: Vec = wide.meta.extra_samples[0].data.as_chunks::<4>().0.iter().map(|c| f32::from_ne_bytes(*c)).collect(); + assert_eq!(plane, values); + + // To 8 bits, the plane quantizes exactly as a gray image of the same values does. + let narrow = wide.convert(ChannelLayout::Rgb, SampleType::U8); + let gray = Image::from_raw(w, h, ChannelLayout::Gray, SampleType::F32, wide.meta.extra_samples[0].data.clone()).unwrap(); + assert_eq!(narrow.meta.extra_samples[0].data, gray.convert(ChannelLayout::Gray, SampleType::U8).data()); + assert_eq!(narrow.meta.extra_samples[0].data.len(), n); +} + +#[test] +fn absent_and_empty_tags_are_not_written() { + let mut img = synth(4, 4, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(tiff_photoshop_tags(&bytes), (None, None)); + assert!(decode(&bytes).unwrap().meta.photoshop_layers.is_none()); + img.meta.photoshop_layers = Some(Vec::new()); + img.meta.photoshop_resources = Some(Vec::new()); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(tiff_photoshop_tags(&bytes), (None, None)); +} + +/// A minimal uncompressed 2×1 gray TIFF with a 37724 tag, in either byte order, classic or +/// BigTIFF: `payload` is stored inline when it fits the value field, at an offset otherwise. +fn hand_made(little: bool, big: bool, payload: &[u8]) -> Vec { + let w16 = |v: u16| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let w32 = |v: u32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let w64 = |v: u64| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let mut f = Vec::new(); + f.extend_from_slice(if little { b"II" } else { b"MM" }); + let header_len = if big { 16 } else { 8 }; + let pixels_at = header_len; + let ifd_at = pixels_at + 2; + f.extend(w16(if big { 43 } else { 42 })); + if big { + f.extend(w16(8)); + f.extend(w16(0)); + f.extend(w64(ifd_at as u64)); + } else { + f.extend(w32(ifd_at as u32)); + } + f.extend_from_slice(&[0x40, 0xc0]); + // Entries: width, length, bits, compression, photometric, strip offsets, samples, rows per + // strip, strip byte counts, 37724. + let tags: [(u16, u16, u64, u64); 10] = [ + (256, 3, 1, 2), + (257, 3, 1, 1), + (258, 3, 1, 8), + (259, 3, 1, 1), + (262, 3, 1, 1), + (273, 4, 1, pixels_at as u64), + (277, 3, 1, 1), + (278, 3, 1, 1), + (279, 4, 1, 2), + (37724, 7, payload.len() as u64, 0), + ]; + let entry_len = if big { 20 } else { 12 }; + let inline = if big { 8 } else { 4 }; + let count_len = if big { 8 } else { 2 }; + let next_len = if big { 8 } else { 4 }; + let data_at = ifd_at + count_len + tags.len() * entry_len + next_len; + let mut entries = Vec::new(); + for (tag, ty, count, value) in tags { + entries.extend(w16(tag)); + entries.extend(w16(ty)); + entries.extend(if big { w64(count) } else { w32(count as u32) }); + let mut v = Vec::new(); + if tag == 37724 { + if payload.len() <= inline { + v.extend_from_slice(payload); + } else { + v.extend(if big { w64(data_at as u64) } else { w32(data_at as u32) }); + } + } else if ty == 3 { + v.extend(w16(value as u16)); + } else { + v.extend(if big { w64(value) } else { w32(value as u32) }); + } + v.resize(inline, 0); + entries.extend(v); + } + f.extend(if big { w64(tags.len() as u64) } else { w16(tags.len() as u16) }); + f.extend(entries); + f.extend(if big { w64(0) } else { w32(0) }); + assert_eq!(f.len(), data_at); + if payload.len() > inline { + f.extend_from_slice(payload); + } + f +} + +#[test] +fn hand_made_files_in_every_layout() { + let long = b"Adobe Photoshop Document Data Block\0MIB8".to_vec(); + for little in [false, true] { + for big in [false, true] { + for payload in [&b"ab"[..], &b"abcd"[..], &b"abcdefgh"[..], &long[..]] { + let f = hand_made(little, big, payload); + let (res, layers) = tiff_photoshop_tags(&f); + assert_eq!(res, None); + assert_eq!(layers, Some(payload), "little {little} big {big} len {}", payload.len()); + let img = decode(&f).unwrap(); + assert_eq!(img.dimensions(), (2, 1)); + assert_eq!(img.meta.photoshop_layers.as_deref(), Some(payload)); + } + } + } +} + +#[test] +fn malformed_tags_read_as_absent_without_panicking() { + let long = vec![7u8; 40]; + for little in [false, true] { + for big in [false, true] { + let f = hand_made(little, big, &long); + // Truncated anywhere: never a panic; the payload is absent once it is cut. + for cut in 0..f.len() { + let (_, layers) = tiff_photoshop_tags(&f[..cut]); + if cut < f.len() { + assert!(layers.is_none_or(|l| l.len() == 40)); + } + let _ = decode(&f[..cut]); + } + // Bit flips in the directory. + for i in 0..f.len() - 40 { + let mut g = f.clone(); + g[i] ^= 0xff; + let _ = tiff_photoshop_tags(&g); + let _ = decode(&g); + } + } + } + assert_eq!(tiff_photoshop_tags(b""), (None, None)); + assert_eq!(tiff_photoshop_tags(b"II*\0"), (None, None)); + assert_eq!(tiff_photoshop_tags(b"MM\0*\xff\xff\xff\xff"), (None, None)); + // A count that overflows the file. + let mut f = hand_made(true, false, &long); + let at = f.windows(2).position(|w| w == 37724u16.to_le_bytes()).unwrap(); + f[at + 4..at + 8].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(tiff_photoshop_tags(&f).1, None); +} diff --git a/crates/image-codecs/tests/it/tiff_pyramid.rs b/crates/image-codecs/tests/it/tiff_pyramid.rs new file mode 100644 index 000000000..48c846944 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_pyramid.rs @@ -0,0 +1,184 @@ +//! TIFF "Save Image Pyramid" (#8827): reduced-resolution levels after the full image, each half +//! the size of the one before, that every reader skips when it opens the file. + +use craft_image_codecs::*; + +fn pyramid() -> EncodeOptions { + EncodeOptions { tiff_pyramid: true, ..Default::default() } +} + +/// An image of `layout`/`sample` where every sample is `v` (on the 0–1 scale). +fn solid(w: u32, h: u32, layout: ChannelLayout, sample: SampleType, v: f32) -> Image { + let n = w as usize * h as usize * layout.channels(); + let data: Vec = match sample { + SampleType::U8 => vec![(v * 255.0).round() as u8; n], + SampleType::U16 => (0..n).flat_map(|_| ((v * 65535.0).round() as u16).to_ne_bytes()).collect(), + SampleType::F32 => (0..n).flat_map(|_| v.to_ne_bytes()).collect(), + SampleType::F16 => (0..n).flat_map(|_| f16::from_f32(v).to_bits().to_ne_bytes()).collect(), + }; + Image::from_raw(w, h, layout, sample, data).unwrap() +} + +fn sizes(bytes: &[u8]) -> Vec<(TiffPageKind, u32, u32)> { + tiff_info(bytes).unwrap().pages.iter().map(|p| (p.kind, p.width, p.height)).collect() +} + +#[test] +fn levels_halve_down_to_the_minimum() { + use TiffPageKind::*; + let img = solid(300, 200, ChannelLayout::Rgb, SampleType::U8, 0.5); + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + // 300 → 150 → 75 → 38: the first level no longer than 64 px is the last. + assert_eq!(sizes(&bytes), [(Page, 300, 200), (ReducedResolution, 150, 100), (ReducedResolution, 75, 50), (ReducedResolution, 38, 25)]); + // Opening the file opens the full image, and the levels are not extra pages. + let back = decode(&bytes).unwrap(); + assert_eq!(back.dimensions(), (300, 200)); + assert_eq!(back.data(), img.data()); + assert!(!back.warnings.iter().any(|w| matches!(w, DecodeWarning::MorePages { .. })), "{:?}", back.warnings); +} + +#[test] +fn off_by_default_and_none_for_small_images() { + let img = solid(300, 200, ChannelLayout::Rgb, SampleType::U8, 0.5); + assert_eq!(sizes(&encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap()).len(), 1); + let small = solid(64, 10, ChannelLayout::Rgb, SampleType::U8, 0.5); + assert_eq!(sizes(&encode(&small, Format::Tiff, &pyramid()).unwrap()).len(), 1); + // A 1-px-wide strip still halves its long side. + let strip = solid(1, 200, ChannelLayout::Gray, SampleType::U8, 0.5); + let s = sizes(&encode(&strip, Format::Tiff, &pyramid()).unwrap()); + assert_eq!(s.iter().map(|&(_, w, h)| (w, h)).collect::>(), [(1, 200), (1, 100), (1, 50)]); +} + +#[test] +fn every_depth_layout_and_compression() { + let layouts = + [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba, ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab]; + for layout in layouts { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + for compression in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { + let img = solid(120, 70, layout, sample, 0.6); + let opts = EncodeOptions { tiff_compression: compression, ..pyramid() }; + let bytes = encode(&img, Format::Tiff, &opts).unwrap_or_else(|e| panic!("{layout:?} {sample:?} {compression:?}: {e}")); + let info = tiff_info(&bytes).unwrap(); + assert_eq!(info.pages.len(), 2, "{layout:?} {sample:?} {compression:?}"); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.dimensions(), (60, 35)); + assert_eq!(level.layout(), decode(&bytes).unwrap().layout(), "{layout:?} {sample:?}"); + // A solid image stays the same colour at every level. + let full = decode(&bytes).unwrap().convert(level.layout(), SampleType::F32); + let level = level.convert(level.layout(), SampleType::F32); + let (a, b) = (full.to_f32_samples().unwrap(), level.to_f32_samples().unwrap()); + for (c, v) in b.iter().take(level.layout().channels()).enumerate() { + assert!((v - a[c]).abs() < 1e-3, "{layout:?} {sample:?} channel {c}: {v} vs {}", a[c]); + } + } + } + } +} + +/// A 128×128 image of 2×2 `tile`s (top left, top right, bottom left, bottom right; alpha last), +/// and the first pixel of its first reduced level. +fn first_level_pixel(layout: ChannelLayout, tile: [&[u8]; 4]) -> Vec { + let mut data = Vec::new(); + for _ in 0..64 { + for x in 0..128 { + data.extend_from_slice(tile[x % 2]); + } + for x in 0..128 { + data.extend_from_slice(tile[2 + x % 2]); + } + } + let img = Image::from_raw(128, 128, layout, SampleType::U8, data).unwrap(); + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.dimensions(), (64, 64), "{layout:?}"); + assert_eq!(level.layout(), layout); + level.data()[..layout.channels()].to_vec() +} + +#[test] +fn colour_is_weighted_by_alpha() { + // One opaque pixel and three transparent ones of another colour: the level keeps the opaque + // colour at a quarter alpha. Unweighted, the transparent colour would bleed in (pink, not red). + assert_eq!(first_level_pixel(ChannelLayout::Rgba, [&[255, 0, 0, 255], &[255, 255, 255, 0], &[255, 255, 255, 0], &[255, 255, 255, 0]]), [255, 0, 0, 64]); + // Gray and CMYK reach the same path with other channel counts. + assert_eq!(first_level_pixel(ChannelLayout::GrayA, [&[40, 255], &[255, 0], &[255, 0], &[255, 0]]), [40, 64]); + assert_eq!(first_level_pixel(ChannelLayout::CmykA, [&[200, 10, 30, 90, 255], &[0; 5], &[0; 5], &[0; 5]]), [200, 10, 30, 90, 64]); +} + +#[test] +fn colour_is_weighted_between_translucent_pixels() { + // Two half-transparent pixels of different colours (red, blue) and two clear ones: the level + // averages the two by their alpha (purple), where copying either pixel would give red or blue. + let tile: [&[u8]; 4] = [&[255, 0, 0, 128], &[0, 0, 255, 128], &[0; 4], &[0; 4]]; + assert_eq!(first_level_pixel(ChannelLayout::Rgba, tile), [128, 0, 128, 64]); +} + +#[test] +fn layered_and_tagged_data_stay_on_the_full_image() { + let mut img = solid(200, 100, ChannelLayout::Rgb, SampleType::U8, 0.25); + img.icc = Some(photocraft_cms_free_profile()); + img.meta.photoshop_layers = Some(vec![1, 2, 3, 4]); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![9; 200 * 100] }]; + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let info = tiff_info(&bytes).unwrap(); + assert_eq!(info.pages.len(), 3); + // Only the full image carries the layers and the extra samples. + assert_eq!(info.pages[0].samples_per_pixel, 4); + assert!(info.pages[1..].iter().all(|p| p.samples_per_pixel == 3)); + assert_eq!(tiff_photoshop_tags(&bytes).1, Some(&[1u8, 2, 3, 4][..])); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.icc, img.icc, "levels keep the profile so they show the same colours"); + assert!(level.meta.extra_samples.is_empty()); +} + +/// Any bytes a decoder keeps as the ICC profile: the codec stores it without parsing. +fn photocraft_cms_free_profile() -> Vec { + (0..=255u8).collect() +} + +#[test] +fn levels_average_exactly_at_every_depth() { + // One 2×2 block of gray values 0, 0.25, 0.5, 1 averages to 0.4375; a 2×2 block with half + // transparent pixels keeps only the opaque colour and averages the alpha. + for sample in [SampleType::U16, SampleType::F32] { + let vals = [0.0f32, 0.25, 0.5, 1.0]; + let mut img = solid(130, 2, ChannelLayout::Gray, sample, 0.0); + let w = 130; + for (i, v) in vals.iter().enumerate() { + let (x, y) = (i % 2, i / 2); + let at = (y * w + x) * sample.bytes(); + let bytes: Vec = match sample { + SampleType::U16 => ((v * 65535.0).round() as u16).to_ne_bytes().to_vec(), + _ => v.to_ne_bytes().to_vec(), + }; + img.data_mut()[at..at + bytes.len()].copy_from_slice(&bytes); + } + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap().convert(ChannelLayout::Gray, SampleType::F32); + let v = level.to_f32_samples().unwrap()[0]; + assert!((v - 0.4375).abs() < 1e-4, "{sample:?}: {v}"); + } + let mut data = Vec::new(); + for _ in 0..2 { + for x in 0..130u32 { + // RGBA16: opaque blue on even columns, transparent red on odd. + let px: [u16; 4] = if x % 2 == 0 { [0, 0, 65535, 65535] } else { [65535, 0, 0, 0] }; + data.extend(px.iter().flat_map(|v| v.to_ne_bytes())); + } + } + let img = Image::from_raw(130, 2, ChannelLayout::Rgba, SampleType::U16, data).unwrap(); + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.to_u16_samples().unwrap()[..4], [0, 0, 65535, 32768]); +} + +#[test] +fn bigtiff_files_carry_the_pyramid_too() { + let img = solid(300, 200, ChannelLayout::Rgba, SampleType::U8, 0.5); + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_bigtiff: true, ..pyramid() }).unwrap(); + let info = tiff_info(&bytes).unwrap(); + assert!(info.big_tiff); + assert_eq!(info.pages.iter().map(|p| (p.kind, p.width)).collect::>().len(), 4); + assert_eq!(decode_tiff_page(&bytes, 3, &DecodeOptions::default()).unwrap().dimensions(), (38, 25)); +} diff --git a/crates/image-codecs/tests/it/tiff_regressions.rs b/crates/image-codecs/tests/it/tiff_regressions.rs new file mode 100644 index 000000000..2df89fbbb --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_regressions.rs @@ -0,0 +1,176 @@ +use crate::common; + +use common::tiffgen::*; +use craft_image_codecs::{Image, decode, exif_orientation, upright_exif}; + +fn classic_white_is_zero() -> Vec { + let width = 4u16; + let height = 1u16; + let pixels = [0u8, 64, 192, 255]; + let entries: [(u16, u16, u32); 9] = [ + (256, 3, u32::from(width)), + (257, 3, u32::from(height)), + (258, 3, 8), + (259, 3, 1), + (262, 3, 0), // WhiteIsZero + (273, 4, 122), // StripOffsets: immediately after IFD0. + (277, 3, 1), + (278, 3, u32::from(height)), + (279, 4, pixels.len() as u32), + ]; + let mut bytes = b"II*\0\x08\0\0\0".to_vec(); + bytes.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, field_type, value) in entries { + bytes.extend_from_slice(&tag.to_le_bytes()); + bytes.extend_from_slice(&field_type.to_le_bytes()); + bytes.extend_from_slice(&1u32.to_le_bytes()); + if field_type == 3 { + bytes.extend_from_slice(&(value as u16).to_le_bytes()); + bytes.extend_from_slice(&[0; 2]); + } else { + bytes.extend_from_slice(&value.to_le_bytes()); + } + } + bytes.extend_from_slice(&0u32.to_le_bytes()); + bytes.extend_from_slice(&pixels); + bytes +} + +fn big_tiff_gray(stored: &[u8], width: u16, height: u16, orientation: u16) -> Vec { + let mut entries = vec![ + (256u16, 3u16, 1u64, u64::from(width)), + (257, 3, 1, u64::from(height)), + (258, 3, 1, 8), + (259, 3, 1, 1), + (262, 3, 1, 1), // BlackIsZero + (273, 4, 1, 0), // StripOffsets, patched below. + (274, 3, 1, u64::from(orientation)), + (277, 3, 1, 1), + (278, 3, 1, u64::from(height)), + (279, 4, 1, stored.len() as u64), + ]; + entries.sort_by_key(|entry| entry.0); + let pixel_offset = 16u64 + 8 + entries.len() as u64 * 20 + 8; + if let Some(strip_offset) = entries.iter_mut().find(|entry| entry.0 == 273) { + strip_offset.3 = pixel_offset; + } + + let mut bytes = b"II+\0\x08\0\0\0\x10\0\0\0\0\0\0\0".to_vec(); + bytes.extend_from_slice(&(entries.len() as u64).to_le_bytes()); + for (tag, field_type, count, value) in entries { + bytes.extend_from_slice(&tag.to_le_bytes()); + bytes.extend_from_slice(&field_type.to_le_bytes()); + bytes.extend_from_slice(&count.to_le_bytes()); + bytes.extend_from_slice(&value.to_le_bytes()); + } + bytes.extend_from_slice(&0u64.to_le_bytes()); + bytes.extend_from_slice(stored); + bytes +} + +fn stored_for_orientation_6_or_8(orientation: u16) -> Vec { + match orientation { + 6 => vec![3, 6, 2, 5, 1, 4], + 8 => vec![4, 1, 5, 2, 6, 3], + _ => unreachable!(), + } +} + +#[test] +fn white_is_zero_is_normalized_once_by_the_tiff_decoder() { + let image = decode(&classic_white_is_zero()).expect("synthetic WhiteIsZero TIFF should decode"); + + assert_eq!(image.dimensions(), (4, 1)); + assert_eq!(image.data(), &[255, 191, 63, 0]); +} + +#[test] +fn big_tiff_orientation_6_and_8_are_applied_to_decoded_pixels() { + let upright = [1u8, 2, 3, 4, 5, 6]; + + for orientation in [6, 8] { + let bytes = big_tiff_gray(&stored_for_orientation_6_or_8(orientation), 2, 3, orientation); + let image: Image = decode(&bytes).expect("synthetic BigTIFF should decode"); + + assert_eq!(exif_orientation(&bytes), orientation); + assert_eq!(exif_orientation(&upright_exif(&bytes)), 1); + assert_eq!(image.dimensions(), (3, 2), "orientation {orientation}"); + assert_eq!(image.data(), &upright, "orientation {orientation}"); + } +} + +fn with_compression(mut dir: Dir, code: u64) -> Dir { + for entry in &mut dir.entries { + if entry.0 == 259 { + entry.2 = vec![V::N(code)]; + } + } + dir +} + +#[test] +fn jpeg_compressed_tiff_decodes() { + // A flat mid-grey 16x16 image as a baseline JPEG stored in one strip (Compression = 7). + let mut jpeg = Vec::new(); + let gray = vec![100u8; 16 * 16]; + jpeg_encoder::Encoder::new(&mut jpeg, 95).encode(&gray, 16, 16, jpeg_encoder::ColorType::Luma).expect("encode the JPEG strip"); + let mut dir = with_compression(strips(16, 16, 8, 1, 1, 16, &[0; 256]), 7); + dir.chunks = vec![jpeg]; + let bytes = build(true, false, &[dir], &[0]).bytes; + + let image = decode(&bytes).expect("a JPEG-compressed TIFF should decode"); + assert_eq!(image.dimensions(), (16, 16)); + assert!(image.data().iter().all(|&v| v.abs_diff(100) <= 4), "{:?}", image.data().get(..4)); +} + +#[test] +fn ccitt_group4_tiff_decodes() { + // 8x3 bilevel: a white row, then two black rows. Bits: V0 | H(white 0, black 8) | V0 V0 | EOFB. + let bits = "1".to_string() + "001" + "00110101" + "000101" + "11" + "000000000001" + "000000000001"; + let mut data = Vec::new(); + for chunk in bits.as_bytes().chunks(8) { + let mut byte = 0u8; + for (i, c) in chunk.iter().enumerate() { + if *c == b'1' { + byte |= 0x80 >> i; + } + } + data.push(byte); + } + let mut dir = with_compression(strips(8, 3, 1, 1, 0, 3, &[0; 3]), 4); + dir.chunks = vec![data]; + let bytes = build(true, false, &[dir], &[0]).bytes; + + let image = decode(&bytes).expect("a CCITT G4 TIFF should decode"); + assert_eq!(image.dimensions(), (8, 3)); + let d = image.data(); + assert_eq!(d.len(), 24, "{d:?}"); + assert!(d[..8].iter().all(|&v| v == d[0]), "{d:?}"); + assert!(d[8..].iter().all(|&v| v == d[8]), "{d:?}"); + assert_ne!(d[0], d[8], "{d:?}"); + assert_eq!(d[0], 255, "white row of a WhiteIsZero image"); +} + +/// #4484: a TIFF saved with the default options must open in software that only reads +/// baseline TIFF (embroidery and plotter tools, older readers). Like Photoshop's default, that +/// means uncompressed strips with no predictor, classic offsets and unassociated alpha. +#[test] +fn default_tiff_is_baseline() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, SampleType, encode}; + use std::io::Cursor; + use tiff::tags::Tag; + + for layout in [ChannelLayout::Rgb, ChannelLayout::Rgba] { + let img = common::synth(33, 21, layout, SampleType::U8, 7, 0.4); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).expect("encode"); + assert!(bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*"), "{layout:?}: not a classic TIFF"); + let mut dec = tiff::decoder::Decoder::new(Cursor::new(&bytes[..])).expect("open"); + assert_eq!(dec.get_tag_u32(Tag::Compression).ok(), Some(1), "{layout:?}: Compression"); + assert_eq!(dec.find_tag(Tag::Predictor).ok().flatten().and_then(|v| v.into_u16().ok()).unwrap_or(1), 1, "{layout:?}: Predictor"); + assert!(dec.find_tag(Tag::TileWidth).ok().flatten().is_none(), "{layout:?}: tiled"); + assert_eq!(dec.get_tag_u32(Tag::PlanarConfiguration).ok().unwrap_or(1), 1, "{layout:?}: planar"); + let extra = dec.find_tag(Tag::ExtraSamples).ok().flatten().and_then(|v| v.into_u16_vec().ok()); + assert_eq!(extra, if layout == ChannelLayout::Rgba { Some(vec![2]) } else { None }, "{layout:?}: ExtraSamples"); + assert_eq!(decode(&bytes).expect("decode").data(), img.data(), "{layout:?}: round trip"); + } +} diff --git a/crates/image-codecs/tests/it/tiff_ycbcr.rs b/crates/image-codecs/tests/it/tiff_ycbcr.rs new file mode 100644 index 000000000..15cac9410 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_ycbcr.rs @@ -0,0 +1,259 @@ +//! YCbCr TIFFs (PhotometricInterpretation 6), as scanners write them: JPEG-compressed with 2×2 +//! chroma subsampling, and uncompressed with ReferenceBlackWhite and YCbCrCoefficients (#10316). +//! Every file here is hand-built by `common::tiffgen`. + +use crate::common::sample_icc; +use crate::common::tiffgen::*; +use craft_image_codecs::*; + +const COMPRESSION: u16 = 259; +const SUBSAMPLING: u16 = 530; +const YCBCR_COEFFICIENTS: u16 = 529; +const REFERENCE_BLACK_WHITE: u16 = 532; +const ICC_PROFILE: u16 = 34675; + +/// A directory whose Compression is `c`. +fn compressed(mut d: Dir, c: u64) -> Dir { + d.entries.retain(|e| e.0 != COMPRESSION); + d.tag(COMPRESSION, SHORT, &[c]) +} + +/// A little-endian RATIONAL value for `tiffgen`. +fn rational(num: u32, den: u32) -> u64 { + u64::from(num) | (u64::from(den) << 32) +} + +/// The image's pixels as RGB bytes. +fn rgb(img: &Image) -> Vec { + img.to_rgba8().as_chunks::<4>().0.iter().flat_map(|[r, g, b, _]| [*r, *g, *b]).collect() +} + +/// A smooth colour test card, `w`×`h` RGB. +fn card(w: u32, h: u32) -> Vec { + let mut v = Vec::new(); + for y in 0..h { + for x in 0..w { + v.extend([(x * 255 / (w - 1)) as u8, (y * 255 / (h - 1)) as u8, (255 - x * 255 / (w - 1)) as u8]); + } + } + v +} + +/// A 4:2:0 baseline JPEG stream of `rgb`. +fn jpeg(rgb: &[u8], w: u32, h: u32) -> Vec { + let img = Image::from_raw(w, h, ChannelLayout::Rgb, SampleType::U8, rgb.to_vec()).unwrap(); + encode(&img, Format::Jpeg, &EncodeOptions { jpeg_chroma_subsampling: true, ..Default::default() }).unwrap() +} + +#[test] +fn jpeg_compressed_ycbcr_with_subsampled_chroma_reads_as_rgb() { + let (w, h) = (32, 24); + let stream = jpeg(&card(w, h), w, h); + // The stream really is subsampled: the luma component samples 2×2 in the frame header. + let sof = stream.windows(2).position(|m| m == [0xFF, 0xC0]).expect("baseline frame header"); + assert_eq!(stream[sof + 11], 0x22, "luma sampling factors"); + let dir = compressed(strips(w, h, 8, 3, 6, h, &stream), 7).tag(SUBSAMPLING, SHORT, &[2, 2]); + let file = build(true, false, &[dir], &[0]).bytes; + + let img = decode(&file).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (w, h, ChannelLayout::Rgb, SampleType::U8)); + // The oracle: the same JPEG stream decoded to RGB by an independent decoder. + let want = image::load_from_memory_with_format(&stream, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw(); + let worst = rgb(&img).iter().zip(&want).map(|(g, o)| g.abs_diff(*o)).max().unwrap(); + assert!(worst <= 2, "largest difference from the oracle: {worst}"); +} + +#[test] +fn uncompressed_ycbcr_uses_the_default_conversion() { + // Black, white, mid gray and JFIF's pure red, without subsampling. + let px = [0, 128, 128, 255, 128, 128, 128, 128, 128, 76, 85, 255]; + let dir = strips(4, 1, 8, 3, 6, 1, &px).tag(SUBSAMPLING, SHORT, &[1, 1]); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgb); + assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255, 128, 128, 128, 254, 0, 0]); +} + +#[test] +fn reference_black_white_sets_the_code_range() { + // Studio range: Y 16–235, chroma 128 ± 112. + let rbw = [rational(16, 1), rational(235, 1), rational(128, 1), rational(240, 1), rational(128, 1), rational(240, 1)]; + let dir = strips(2, 1, 8, 3, 6, 1, &[16, 128, 128, 235, 128, 128]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(REFERENCE_BLACK_WHITE, RATIONAL, &rbw); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255]); +} + +#[test] +fn weights_that_do_not_sum_to_one_keep_grays_neutral() { + // 0.25 each (sum 0.75): TIFF 6.0's full inverse would scale green's Y by (1 − Lr − Lb) / Lg + // = 2 and give mid gray G 255 (clamped). Like libtiff, green keeps Y, so gray stays gray, and + // the weights still set the chroma terms: R = Y + 1.5·Cr, B = Y + 1.5·Cb. + let weights = [rational(1, 4), rational(1, 4), rational(1, 4)]; + let dir = strips(2, 1, 8, 3, 6, 1, &[128, 128, 128, 128, 64, 192]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + // Green for the second pixel: 128 − (Lb·1.5/Lg)·Cb − (Lr·1.5/Lg)·Cr = 128 + 96 − 96. + assert_eq!(rgb(&img), [128, 128, 128, 224, 128, 32]); +} + +#[test] +fn ycbcr_coefficients_set_the_luma_weights() { + // Weights 0.25, 0.5, 0.25 with Cb −64 and Cr +64: R = Y + 1.5·Cr, B = Y + 1.5·Cb, and G keeps Y + // (BT.601 would give R 218 and B 15). + let weights = [rational(1, 4), rational(1, 2), rational(1, 4)]; + let dir = strips(1, 1, 8, 3, 6, 1, &[128, 64, 192]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [224, 128, 32]); +} + +#[test] +fn ycbcr_tags_with_the_wrong_count_use_the_defaults() { + // Four weights and seven black/white codes: both tags are malformed, so BT.601 and full range + // apply (the weights alone would give 224, 128, 32). + let weights = [rational(1, 4), rational(1, 2), rational(1, 4), rational(1, 4)]; + let rbw = [rational(16, 1), rational(235, 1), rational(128, 1), rational(240, 1), rational(128, 1), rational(240, 1), rational(0, 1)]; + let dir = strips(1, 1, 8, 3, 6, 1, &[128, 64, 192]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights).tag( + REFERENCE_BLACK_WHITE, + RATIONAL, + &rbw, + ); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [218, 104, 15]); +} + +#[test] +fn sixteen_bit_ycbcr_reads_as_rgb() { + let px: Vec = [65535u16, 32768, 32768, 0, 32768, 32768].iter().flat_map(|v| v.to_le_bytes()).collect(); + let dir = strips(2, 1, 16, 3, 6, 1, &px).tag(SUBSAMPLING, SHORT, &[1, 1]); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U16)); + let n = img.to_normalized(); + assert!(n[..3].iter().all(|&v| v == 1.0) && n[3..].iter().all(|&v| v == 0.0), "{n:?}"); +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn sixteen_bit_ycbcr_bands_match_whole_decode_with_a_missing_last_strip() { + // On 32-bit targets max_alloc also bounds the whole image, so a small allocation cap + // cannot force these tiny strips into separate output groups there. + let (w, h, rows) = (4u32, 5u32, 2u32); + let pixels = [[0u16, 32768, 32768], [65535, 32768, 32768], [19595, 21710, 65535], [32768, 40960, 24576]]; + let samples: Vec = (0..w * h).flat_map(|i| pixels[i as usize % pixels.len()]).collect(); + let weights = [rational(1, 4), rational(1, 2), rational(1, 4)]; + let icc = sample_icc(512); + for little in [true, false] { + for missing in [false, true] { + let dir = strips(w, h, 16, 3, 6, rows, &u16s(&samples, little)).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights).bytes( + ICC_PROFILE, + UNDEFINED, + &icc, + ); + let mut file = build(little, false, &[dir], &[0]); + if missing { + let past = file.bytes.len() as u64 + 4096; + patch_value(&mut file, little, false, 0, 273, 2, past); + } + let want = decode_with(&file.bytes, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + let limits = Limits { max_alloc: u64::from(w * rows * 3 * 2), ..Limits::banded() }; + let mut bands = TiffBands::open(&file.bytes, None, &limits).unwrap(); + assert_eq!((bands.dimensions(), bands.layout(), bands.sample_type()), ((w, h), ChannelLayout::Rgb, SampleType::U16)); + let mut got = Image::new(w, h, bands.layout(), bands.sample_type()).unwrap(); + let mut positions = Vec::new(); + bands + .decode(|x, y, group: &Image| -> Result<(), CodecError> { + positions.push((x, y, group.height())); + assert_eq!((x, group.width(), group.layout(), group.sample_type()), (0, w, ChannelLayout::Rgb, SampleType::U16)); + let at = y as usize * w as usize * 3 * 2; + got.data_mut()[at..at + group.data().len()].copy_from_slice(group.data()); + Ok(()) + }) + .unwrap(); + assert_eq!(positions, [(0, 0, 2), (0, 2, 2), (0, 4, 1)], "one output group per strip, including the short last strip"); + assert_eq!(got.data(), want.data(), "little endian: {little}, missing last strip: {missing}"); + assert_eq!(bands.icc, want.icc); + assert_eq!(bands.meta, want.meta); + assert_eq!(bands.warnings, want.warnings); + assert_eq!(bands.warnings.iter().any(|w| matches!(w, DecodeWarning::Truncated { .. })), missing); + } + } +} + +#[test] +fn unsupported_ycbcr_files_fail_cleanly() { + // Four samples (an extra sample next to YCbCr). + let four = strips(1, 1, 8, 4, 6, 1, &[128, 128, 128, 255]).tag(SUBSAMPLING, SHORT, &[1, 1]); + assert!(decode(&build(true, false, &[four], &[0]).bytes).is_err()); + // Subsampled chroma without JPEG compression (the default subsampling is 2×2). + let sub = strips(2, 2, 8, 3, 6, 2, &[128; 12]); + assert!(decode(&build(true, false, &[sub], &[0]).bytes).is_err()); + // A JPEG strip that is not JPEG data. + let junk = compressed(strips(16, 16, 8, 3, 6, 16, &[0x5A; 64]), 7).tag(SUBSAMPLING, SHORT, &[2, 2]); + assert!(decode(&build(true, false, &[junk], &[0]).bytes).is_err()); + // Coefficients and ReferenceBlackWhite that would divide by zero fall back to the defaults. + let bad = strips(1, 1, 8, 3, 6, 1, &[255, 128, 128]) + .tag(SUBSAMPLING, SHORT, &[1, 1]) + .tag(YCBCR_COEFFICIENTS, RATIONAL, &[rational(0, 1), rational(0, 0), rational(5, 1)]) + .tag(REFERENCE_BLACK_WHITE, RATIONAL, &[rational(7, 1); 6]); + let img = decode(&build(true, false, &[bad], &[0]).bytes).unwrap(); + assert_eq!(&img.to_rgba8()[..3], &[255, 255, 255]); +} + +#[test] +fn tiled_jpeg_ycbcr_keeps_its_icc_profile() { + // Two 16×16 tiles, each its own 4:2:0 JPEG stream, and an embedded profile, like a scanner's + // tiled output. + let (w, h, t) = (32u32, 16u32, 16u32); + let px = card(w, h); + let tile = |tx: u32| -> Vec { + (0..t) + .flat_map(|y| { + let at = ((y * w + tx * t) * 3) as usize; + px[at..at + (t * 3) as usize].to_vec() + }) + .collect() + }; + let streams = [jpeg(&tile(0), t, t), jpeg(&tile(1), t, t)]; + let icc = sample_icc(512); + let dir = Dir { entries: base(w, h, 8, 3, 6), chunks: streams.to_vec(), tiled: true }; + let dir = compressed(dir, 7).tag(322, SHORT, &[u64::from(t)]).tag(323, SHORT, &[u64::from(t)]).tag(SUBSAMPLING, SHORT, &[2, 2]).bytes( + ICC_PROFILE, + UNDEFINED, + &icc, + ); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (w, h, ChannelLayout::Rgb)); + assert_eq!(img.icc.as_deref(), Some(icc.as_slice()), "the embedded profile is kept"); + // The oracle: each tile's stream decoded by an independent decoder, placed side by side. + let tiles: Vec> = streams.iter().map(|s| image::load_from_memory_with_format(s, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw()).collect(); + let row = (t * 3) as usize; + let want: Vec = (0..t as usize).flat_map(|y| [&tiles[0][y * row..(y + 1) * row], &tiles[1][y * row..(y + 1) * row]].concat()).collect(); + let worst = rgb(&img).iter().zip(&want).map(|(g, o)| g.abs_diff(*o)).max().unwrap(); + assert!(worst <= 2, "largest difference from the oracle: {worst}"); +} + +#[test] +fn big_endian_sixteen_bit_ycbcr_reads_as_rgb() { + // White, black, and red as full-range 16-bit YCbCr (Y 0.299·65535, Cb 32768 − 0.1687·65535, Cr max). + let px = [65535u16, 32768, 32768, 0, 32768, 32768, 19595, 21710, 65535]; + let dir = strips(3, 1, 16, 3, 6, 1, &u16s(&px, false)).tag(SUBSAMPLING, SHORT, &[1, 1]); + let file = build(false, false, &[dir], &[0]).bytes; + assert_eq!(&file[..2], b"MM"); + let img = decode(&file).unwrap(); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U16)); + let n = img.to_normalized(); + let want = [1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + assert!(n.iter().zip(want).all(|(g, w)| (g - w).abs() < 2.0 / 255.0), "{n:?}"); +} + +#[test] +fn planar_ycbcr_reads_as_rgb_in_both_byte_orders() { + // The pixels of the default-conversion test, one plane per component. + let px = [0u8, 128, 128, 255, 128, 128, 128, 128, 128, 76, 85, 255]; + for little in [true, false] { + let dir = Dir { entries: base(4, 1, 8, 3, 6), chunks: planar_strips(&px, 4, 1, 3, 1, 1), tiled: false }.tag(278, LONG, &[1]).tag(284, SHORT, &[2]).tag( + SUBSAMPLING, + SHORT, + &[1, 1], + ); + let img = decode(&build(little, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255, 128, 128, 128, 254, 0, 0], "little endian: {little}"); + } +} diff --git a/crates/image-codecs/tests/it/webp_frames.rs b/crates/image-codecs/tests/it/webp_frames.rs new file mode 100644 index 000000000..c099ec981 --- /dev/null +++ b/crates/image-codecs/tests/it/webp_frames.rs @@ -0,0 +1,166 @@ +//! `decode_webp_frames`: every frame of an animated WebP, composited, with its duration (#1198). + +use craft_image_codecs::{ + AnimationFrame, ChannelLayout, CodecError, DecodeOptions, DecodeWarning, EncodeOptions, Format, Image, Limits, decode, decode_webp_frames, encode, + webp_is_animated, +}; + +const RED: [u8; 4] = [255, 0, 0, 255]; +const GREEN: [u8; 4] = [0, 255, 0, 255]; +const BLUE: [u8; 4] = [0, 0, 255, 255]; + +fn frames(bytes: &[u8]) -> Vec { + decode_webp_frames(bytes, &DecodeOptions::default()).unwrap() +} + +fn solid(c: [u8; 4], w: u32, h: u32) -> Image { + Image::from_u8(w, h, ChannelLayout::Rgba, c.repeat((w * h) as usize)).unwrap() +} + +fn chunk(id: &[u8; 4], data: &[u8]) -> Vec { + let mut c = id.to_vec(); + c.extend_from_slice(&(data.len() as u32).to_le_bytes()); + c.extend_from_slice(data); + if data.len() % 2 == 1 { + c.push(0); + } + c +} + +fn u24(v: u32) -> [u8; 3] { + let b = v.to_le_bytes(); + [b[0], b[1], b[2]] +} + +/// One frame of [`animated`]: the picture, where it sits on the canvas (even offsets, as WebP +/// stores them halved), its duration in milliseconds, and whether it is alpha-blended over the +/// canvas rather than replacing it. +struct Part { + image: Image, + at: [u32; 2], + duration_ms: u32, + blend: bool, +} + +fn part(c: [u8; 4], w: u32, h: u32, duration_ms: u32) -> Part { + Part { image: solid(c, w, h), at: [0, 0], duration_ms, blend: false } +} + +/// An animated WebP with a `w`x`h` canvas (transparent background) and one ANMF chunk per part, +/// each holding a lossless still from our own encoder. +fn animated(w: u32, h: u32, parts: &[Part]) -> Vec { + let mut body = b"WEBP".to_vec(); + let mut vp8x = vec![0x02 | 0x10, 0, 0, 0]; // animation and alpha flags + vp8x.extend_from_slice(&u24(w - 1)); + vp8x.extend_from_slice(&u24(h - 1)); + body.extend(chunk(b"VP8X", &vp8x)); + body.extend(chunk(b"ANIM", &[0, 0, 0, 0, 0, 0])); // background 0, loop forever + for p in parts { + let still = encode(&p.image, Format::WebP, &EncodeOptions::default()).unwrap(); + let at = still.windows(4).position(|w| w == b"VP8L").unwrap(); + let len = u32::from_le_bytes(still[at + 4..at + 8].try_into().unwrap()) as usize; + let mut anmf = Vec::new(); + anmf.extend_from_slice(&u24(p.at[0] / 2)); + anmf.extend_from_slice(&u24(p.at[1] / 2)); + anmf.extend_from_slice(&u24(p.image.width() - 1)); + anmf.extend_from_slice(&u24(p.image.height() - 1)); + anmf.extend_from_slice(&u24(p.duration_ms)); + anmf.push(if p.blend { 0 } else { 0x02 }); // bit 1: do not blend + anmf.extend(chunk(b"VP8L", &still[at + 8..at + 8 + len])); + body.extend(chunk(b"ANMF", &anmf)); + } + let mut out = b"RIFF".to_vec(); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend(body); + out +} + +fn pixel(f: &AnimationFrame, x: u32, y: u32) -> [u8; 4] { + let rgba = f.image.to_rgba8(); + let i = ((y * f.image.width() + x) * 4) as usize; + rgba[i..i + 4].try_into().unwrap() +} + +#[test] +fn a_still_webp_gives_one_frame_equal_to_decode() { + let still = encode(&solid(RED, 6, 4), Format::WebP, &EncodeOptions::default()).unwrap(); + assert!(!webp_is_animated(&still)); + let f = frames(&still); + assert_eq!(f.len(), 1); + assert_eq!(f[0].delay_ms, 0); + assert_eq!(f[0].image, decode(&still).unwrap()); +} + +#[test] +fn a_one_frame_animation_is_not_animated() { + let a = animated(6, 4, &[part(RED, 6, 4, 70)]); + assert!(!webp_is_animated(&a)); + assert_eq!(frames(&a).len(), 1); +} + +#[test] +fn every_frame_comes_back_in_order_with_its_duration() { + let a = animated(6, 4, &[part(RED, 6, 4, 40), part(GREEN, 6, 4, 120), part(BLUE, 6, 4, 7)]); + assert!(webp_is_animated(&a)); + let f = frames(&a); + assert_eq!(f.iter().map(|f| f.delay_ms).collect::>(), [40, 120, 7], "WebP durations are milliseconds"); + for (f, c) in f.iter().zip([RED, GREEN, BLUE]) { + assert_eq!((f.image.width(), f.image.height()), (6, 4)); + assert_eq!(pixel(f, 0, 0), c); + assert_eq!(pixel(f, 5, 3), c); + assert_eq!(f.image.warnings, []); + } +} + +#[test] +fn a_smaller_frame_is_drawn_over_the_previous_one() { + let small = Part { image: solid(BLUE, 2, 2), at: [2, 2], duration_ms: 50, blend: true }; + let f = frames(&animated(6, 4, &[part(RED, 6, 4, 50), small])); + assert_eq!(f.len(), 2); + // Alpha blending rounds: within one level. + let near = |a: [u8; 4], b: [u8; 4]| a.iter().zip(b).all(|(x, y)| x.abs_diff(y) <= 1); + assert!(near(pixel(&f[1], 0, 0), RED), "outside the second frame the first shows through"); + assert!(near(pixel(&f[1], 2, 2), BLUE)); + assert!(near(pixel(&f[1], 3, 3), BLUE)); + assert!(near(pixel(&f[1], 4, 2), RED)); +} + +#[test] +fn a_damaged_later_frame_keeps_the_frames_before_it() { + let mut a = animated(6, 4, &[part(RED, 6, 4, 50), part(GREEN, 6, 4, 50), part(BLUE, 6, 4, 50)]); + // Break the last frame's VP8L signature byte; the container around it stays whole. + let last = a.windows(4).rposition(|w| w == b"VP8L").unwrap(); + a[last + 8] = 0; + let f = frames(&a); + assert_eq!(f.len(), 2); + assert_eq!(pixel(&f[0], 0, 0), RED); + assert_eq!(pixel(&f[1], 0, 0), GREEN); + assert_eq!(f[0].image.warnings, [DecodeWarning::Truncated { format: Format::WebP }]); +} + +#[test] +fn every_prefix_of_an_animated_webp_returns() { + let a = animated(5, 4, &[part(RED, 5, 4, 50), part(GREEN, 5, 4, 50), part(BLUE, 5, 4, 50)]); + for n in 0..a.len() { + let _ = decode_webp_frames(&a[..n], &DecodeOptions::default()); + } +} + +#[test] +fn dimension_limits_apply() { + let a = animated(4, 4, &[part(RED, 4, 4, 50), part(GREEN, 4, 4, 50)]); + let opts = DecodeOptions { limits: Limits { max_width: 3, ..Limits::default() }, ..Default::default() }; + assert!(matches!(decode_webp_frames(&a, &opts), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn the_byte_budget_covers_all_frames_together() { + // One 4x4 RGBA frame is 64 bytes. The decoder's own canvas counts against the budget too, so + // two frames need more than 128; ten frames of the same size must not fit where two do. + let two = animated(4, 4, &[part(RED, 4, 4, 50), part(GREEN, 4, 4, 50)]); + let ten: Vec = (0..10).map(|i| part(if i % 2 == 0 { RED } else { BLUE }, 4, 4, 50)).collect(); + let ten = animated(4, 4, &ten); + let opts = DecodeOptions { limits: Limits { max_alloc: 256, ..Limits::default() }, ..Default::default() }; + assert_eq!(decode_webp_frames(&two, &opts).unwrap().len(), 2); + assert!(matches!(decode_webp_frames(&ten, &opts), Err(CodecError::LimitExceeded(_)))); +} diff --git a/crates/image-codecs/tests/it/webp_lossy.rs b/crates/image-codecs/tests/it/webp_lossy.rs new file mode 100644 index 000000000..2d0c4861c --- /dev/null +++ b/crates/image-codecs/tests/it/webp_lossy.rs @@ -0,0 +1,140 @@ +//! Lossy WebP (our VP8 encoder) checked against an independent decoder (`image-webp`): the +//! files decode, look like the input (PSNR by quality), get smaller as quality drops and are far +//! smaller than lossless on photographic content; alpha and metadata travel in the container; +//! odd sizes and the limits behave. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn lossy(q: u8) -> EncodeOptions { + EncodeOptions { webp_lossless: false, webp_quality: q, ..Default::default() } +} + +/// A smooth photograph-like image: gradients with a little grain. +fn photo(w: u32, h: u32, seed: u64) -> Image { + synth(w, h, ChannelLayout::Rgb, SampleType::U8, seed, 0.03) +} + +#[test] +fn decodes_with_the_right_size_and_looks_like_the_input() { + let img = photo(97, 61, 3); + for (q, min_psnr) in [(100u8, 40.0), (90, 36.0), (75, 33.0), (50, 30.0), (20, 26.0)] { + let bytes = encode(&img, Format::WebP, &lossy(q)).unwrap(); + let back = decode(&bytes).unwrap_or_else(|e| panic!("q{q}: {e}")); + assert_eq!(back.dimensions(), (97, 61)); + assert_eq!(back.layout(), ChannelLayout::Rgb); + let p = psnr(&img, &back); + assert!(p >= min_psnr, "quality {q}: PSNR {p:.1} dB below {min_psnr}"); + } +} + +#[test] +fn smaller_at_lower_quality_and_much_smaller_than_lossless_on_photos() { + let img = synth(256, 192, ChannelLayout::Rgb, SampleType::U8, 9, 0.08); + let lossless = encode(&img, Format::WebP, &EncodeOptions::default()).unwrap().len(); + let sizes: Vec = [95u8, 80, 60, 40, 20].iter().map(|&q| encode(&img, Format::WebP, &lossy(q)).unwrap().len()).collect(); + for w in sizes.windows(2) { + assert!(w[1] <= w[0], "sizes must not grow as quality drops: {sizes:?}"); + } + assert!(sizes[1] * 4 < lossless, "q80 {} vs lossless {lossless}", sizes[1]); + assert!(sizes[4] * 10 < lossless, "q20 {} vs lossless {lossless}", sizes[4]); +} + +#[test] +fn flat_areas_cost_almost_nothing() { + // A single colour: every macroblock is skipped; the file is dominated by the header. + let img = Image::from_u8(640, 480, ChannelLayout::Rgb, vec![200u8; 640 * 480 * 3]).unwrap(); + let bytes = encode(&img, Format::WebP, &lossy(80)).unwrap(); + assert!(bytes.len() < 1200, "{} bytes", bytes.len()); + let back = decode(&bytes).unwrap(); + assert!(max_abs_diff(&img, &back) <= 3.0 / 255.0, "flat colour reproduced within rounding"); +} + +#[test] +fn alpha_is_kept_exactly_and_metadata_travels() { + let mut img = synth(40, 30, ChannelLayout::Rgba, SampleType::U8, 5, 0.05); + img.icc = Some(sample_icc(400)); + img.meta.exif = Some(sample_exif()); + img.meta.xmp = Some(SAMPLE_XMP.to_owned()); + let bytes = encode(&img, Format::WebP, &lossy(85)).unwrap(); + assert_eq!(&bytes[12..16], b"VP8X", "extended container"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Rgba); + for (a, b) in img.data().as_chunks::<4>().0.iter().zip(back.data().as_chunks::<4>().0) { + assert_eq!(a[3], b[3], "alpha is stored losslessly"); + } + assert!(psnr(&img, &back) > 30.0); + assert_eq!(back.icc, img.icc); + assert!(back.meta.xmp.is_some()); + assert!(back.meta.exif.is_some()); + // Fully opaque alpha is dropped rather than stored. + let mut data = synth(40, 30, ChannelLayout::Rgba, SampleType::U8, 5, 0.0).data().to_vec(); + for px in data.as_chunks_mut::<4>().0 { + px[3] = 255; + } + let opaque = Image::from_u8(40, 30, ChannelLayout::Rgba, data).unwrap(); + let bytes = encode(&opaque, Format::WebP, &EncodeOptions { embed_icc: false, embed_metadata: false, ..lossy(85) }).unwrap(); + assert_eq!(&bytes[12..16], b"VP8 "); + assert_eq!(decode(&bytes).unwrap().layout(), ChannelLayout::Rgb); +} + +#[test] +fn gray_and_odd_sizes_and_tiny_images() { + for (w, h) in [(1, 1), (2, 3), (17, 9), (33, 1), (1, 47), (16, 16), (31, 15)] { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + // Little noise: at these sizes the 2 × 2 chroma average alone would otherwise dominate. + let img = synth(w, h, layout, SampleType::U8, u64::from(w * 100 + h), 0.02); + let bytes = encode(&img, Format::WebP, &lossy(80)).unwrap_or_else(|e| panic!("{w}x{h} {layout:?}: {e}")); + let back = decode(&bytes).unwrap_or_else(|e| panic!("{w}x{h} {layout:?}: {e}")); + assert_eq!(back.dimensions(), (w, h)); + // Below 8 px the 2 × 2 chroma average of random saturated pixels is the whole error. + if w >= 8 && h >= 8 { + let p = psnr(&img.convert(back.layout(), SampleType::U8), &back); + assert!(p > 24.0, "{w}x{h} {layout:?}: {p:.1} dB"); + } + } + } +} + +#[test] +fn sixteen_bit_input_is_quantised_to_eight() { + let img = synth(24, 24, ChannelLayout::Rgb, SampleType::U16, 2, 0.02); + let bytes = encode(&img, Format::WebP, &lossy(90)).unwrap(); + let back = decode(&bytes).unwrap(); + assert!(psnr(&img.convert(ChannelLayout::Rgb, SampleType::U8), &back) > 34.0); +} + +#[test] +fn dimension_limit_is_an_error_not_a_panic() { + let img = Image::from_u8(16384, 1, ChannelLayout::Rgb, vec![0u8; 16384 * 3]).unwrap(); + let e = encode(&img, Format::WebP, &lossy(50)).unwrap_err(); + assert!(matches!(e, CodecError::Encode { .. }), "{e}"); + let img = Image::from_u8(16383, 1, ChannelLayout::Rgb, vec![0u8; 16383 * 3]).unwrap(); + let bytes = encode(&img, Format::WebP, &lossy(50)).unwrap(); + assert_eq!(decode(&bytes).unwrap().dimensions(), (16383, 1)); +} + +#[test] +fn hard_edges_and_text_like_content_keep_their_structure() { + // Black text-like strokes on white: B_PRED sub-block modes keep edges sharp enough that the + // thresholded result matches the source almost everywhere. + let (w, h) = (128u32, 64u32); + let mut data = vec![255u8; (w * h * 3) as usize]; + for y in 0..h { + for x in 0..w { + let stroke = (x % 11 < 2 && y > 8 && y < 56) || (y % 13 < 2 && x > 4 && x < 124); + if stroke { + let i = ((y * w + x) * 3) as usize; + data[i..i + 3].fill(0); + } + } + } + let img = Image::from_u8(w, h, ChannelLayout::Rgb, data).unwrap(); + let bytes = encode(&img, Format::WebP, &lossy(90)).unwrap(); + let back = decode(&bytes).unwrap(); + let wrong = img.data().as_chunks::<3>().0.iter().zip(back.data().as_chunks::<3>().0).filter(|(a, b)| (a[0] < 128) != (b[0] < 128)).count(); + let total = (w * h) as usize; + assert!(wrong * 100 < total * 2, "{wrong} of {total} pixels flipped side of mid-gray"); + assert!(psnr(&img, &back) > 30.0, "{:.1}", psnr(&img, &back)); +} diff --git a/crates/image-codecs/tests/it/xpm.rs b/crates/image-codecs/tests/it/xpm.rs new file mode 100644 index 000000000..b499cea33 --- /dev/null +++ b/crates/image-codecs/tests/it/xpm.rs @@ -0,0 +1,123 @@ +use craft_image_codecs::{ChannelLayout, CodecError, Format, Image, caps, decode, decode_as, detect, encode, from_extension}; + +const SAMPLE: &[u8] = br#"/* XPM */ +static char *icon[] = { +"2 2 3 1", +". c #ff0000", +" c None", +"+ c #00ff00", +".+", +" +" +}; +"#; + +#[test] +fn xpm3_decodes_palette_transparency_and_spaces_in_keys() { + assert_eq!(detect(SAMPLE), Some(Format::Xpm)); + assert_eq!(from_extension("icon.XPM"), Some(Format::Xpm)); + let img = decode(SAMPLE).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert_eq!(img.dimensions(), (2, 2)); + assert_eq!(img.data(), &[255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 0, 0, 0, 255, 0, 255]); +} + +#[test] +fn rgb_precisions_and_x11_name() { + let xpm = br#"/* XPM */ +static char *i[] = { +"3 1 3 1", +"a c #f00", +"b c #ffff00000000", +"c c gray70", +"abc" +};"#; + let img = decode(xpm).unwrap(); + assert_eq!(img.data(), &[255, 0, 0, 255, 255, 0, 0, 255, 179, 179, 179, 255]); +} + +#[test] +fn rejects_truncation_unsupported_colours_and_unknown_keys() { + for bytes in [ + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c None\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c NotAColor\", \"a\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c #a\xC3\xA9\", \"a\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c #+f0000\", \"a\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c black\", \"z\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c black\", \"aa\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 2 1\", \"a c black\", \"a c white\", \"a\" };"[..], + ] { + assert!(decode_as(Format::Xpm, bytes).is_err(), "{:?}", bytes); + } + assert!(decode_as(Format::Xpm, b"/* XPM */\n\"4294967295 4294967295 1 1\"").is_err()); + assert!(decode_as(Format::Xpm, b"/* XPM */\n\"2 1 1 1\" \"a c black\" \"aa\" /* bad").is_err()); +} + +#[test] +fn x11_names_and_field_priority() { + let xpm = br#"/* XPM */ +static char *i[] = { +"4 1 4 1", +"a c LightGray", +"b c DarkBlue", +"c m black g white c light blue", +"d m black g4 red g green", +"abcd" +};"#; + let image = decode(xpm).unwrap(); + assert_eq!(image.data(), &[211, 211, 211, 255, 0, 0, 139, 255, 173, 216, 230, 255, 0, 255, 0, 255]); +} + +#[test] +fn named_x11_colors_use_x11_not_css_values() { + let bytes = br#"/* XPM */ +static char *icon[] = { +"6 1 6 1", +"a c gray", +"b c maroon", +"c c purple", +"d c LightGoldenrodYellow", +"e c DarkSlateGray4", +"f c Ghost White", +"abcdef" +};"#; + let image = decode(bytes).unwrap(); + assert_eq!(image.data(), &[190, 190, 190, 255, 176, 48, 96, 255, 160, 32, 240, 255, 250, 250, 210, 255, 82, 139, 139, 255, 248, 248, 255, 255,]); +} + +#[test] +fn xpm2_decodes_crlf_comments_spaces_and_multichar_keys() { + let bytes = b"! XPM2\r\n! exported by an icon tool\r\n2 2 3 2\r\n.a c #f00\r\n!_ c None\r\nzz c DarkBlue\r\n.a!_\r\nzz.a\r\n"; + assert_eq!(detect(bytes), Some(Format::Xpm)); + let image = decode(bytes).unwrap(); + assert_eq!(image.dimensions(), (2, 2)); + assert_eq!(image.data(), &[255, 0, 0, 255, 0, 0, 0, 0, 0, 0, 139, 255, 255, 0, 0, 255]); +} + +#[test] +fn xpm2_allows_exclamation_as_a_pixel_key_and_preserves_space_keys() { + let bytes = b"! XPM2\n2 1 2 1\n! c red\n c None\n! \n"; + let image = decode(bytes).unwrap(); + assert_eq!(image.data(), &[255, 0, 0, 255, 0, 0, 0, 0]); +} + +#[test] +fn xpm2_malformed_inputs_return_errors() { + for bytes in [ + &b"! XPM2\n1 1 1 1\na c black\n"[..], + &b"! XPM2\n1 1 1 1\na c black\nb\n"[..], + &b"! XPM2\n1 1 1 1\na c #a\xc3\xa9\na\n"[..], + &b"! XPM2 invalid\n1 1 1 1\na c black\na\n"[..], + &b"! XPM2\n100000 100000 1 1\na c red\na\n"[..], + ] { + assert!(decode_as(Format::Xpm, bytes).is_err(), "{bytes:?}"); + } + assert_ne!(detect(b"! XPM23\n1 1 1 1\na c black\na\n"), Some(Format::Xpm)); +} + +#[test] +fn xpm_is_import_only() { + assert!(caps(Format::Xpm).read); + assert!(!caps(Format::Xpm).write); + let image = Image::from_u8(1, 1, ChannelLayout::Rgba, vec![0, 0, 0, 0]).unwrap(); + assert!(matches!(encode(&image, Format::Xpm, &Default::default()), Err(CodecError::Unsupported { .. }))); +} diff --git a/crates/image-codecs/tests/jxl_export.rs b/crates/image-codecs/tests/jxl_export.rs new file mode 100644 index 000000000..61659f55a --- /dev/null +++ b/crates/image-codecs/tests/jxl_export.rs @@ -0,0 +1,110 @@ +//! Lossless JPEG XL export and honest export-only capability reporting. + +use craft_image_codecs::{ + ASYMMETRIC_EXCEPTIONS, ChannelLayout, CodecError, EncodeOptions, Format, Image, SampleType, caps, decode, detect, encode, from_extension, +}; + +#[test] +fn jpeg_xl_is_export_only_and_detects_both_signatures() { + let c = caps(Format::Jxl); + assert!(c.write && !c.read); + assert_eq!(c.depths, &[SampleType::U8, SampleType::U16]); + assert_eq!(c.layouts, &[ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba]); + assert!(c.alpha && !c.icc && !c.exif && !c.xmp && !c.dpi && !c.lossy); + assert_eq!(Format::Jxl.mime_type(), "image/jxl"); + assert_eq!(from_extension("image.JXL"), Some(Format::Jxl)); + assert_eq!(detect(&[0xff, 0x0a, 0, 0]), Some(Format::Jxl)); + assert_eq!(detect(b"\0\0\0\x0cJXL \r\n\x87\n"), Some(Format::Jxl)); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(format, reason)| *format == Format::Jxl && !reason.is_empty())); + let err = decode(&[0xff, 0x0a]); + assert!(matches!(err, Err(CodecError::Unsupported { format: Format::Jxl, .. })), "{err:?}"); +} + +#[test] +fn jpeg_xl_encoding_is_deterministic_and_converts_float_to_integer() { + let input = + Image::from_u8(3, 2, ChannelLayout::Rgba, vec![0, 255, 17, 127, 20, 40, 80, 255, 255, 0, 2, 0, 10, 20, 30, 64, 0, 0, 0, 1, 90, 80, 70, 128]).unwrap(); + let opts = EncodeOptions::default(); + let one = encode(&input, Format::Jxl, &opts).unwrap(); + let two = encode(&input, Format::Jxl, &opts).unwrap(); + assert_eq!(one, two); + assert_eq!(detect(&one), Some(Format::Jxl)); + let float = Image::from_f32(2, 2, ChannelLayout::Rgb, &[0.0, 0.5, 1.0, 0.25, 0.5, 0.75, 0.75, 0.5, 0.25, 1.0, 0.0, 0.5]).unwrap(); + let bytes = encode(&float, Format::Jxl, &opts).unwrap(); + assert_eq!(detect(&bytes), Some(Format::Jxl)); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn jpeg_xl_decodes_losslessly_with_independent_decoder() { + use jxl_oxide::JxlImage; + + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let channels = layout.channels(); + for sample in [SampleType::U8, SampleType::U16] { + let (w, h) = (7, 5); + let count = w as usize * h as usize * channels; + let expected = match sample { + SampleType::U8 => (0..count).map(|i| (((i * 37 + 19) % 256) as u16) * 257).collect::>(), + SampleType::U16 => (0..count).map(|i| ((i * 9973 + 27) % 65536) as u16).collect::>(), + _ => unreachable!(), + }; + let input = match sample { + SampleType::U8 => { + let data = expected.iter().map(|v| (v / 257) as u8).collect(); + Image::from_u8(w, h, layout, data).unwrap() + } + SampleType::U16 => Image::from_u16(w, h, layout, &expected).unwrap(), + _ => unreachable!(), + }; + // GrayA is losslessly widened to RGBA. Keep the oracle comparison + // against original gray and alpha samples, not only against a converted image. + let (output_channels, expected_output) = if layout == ChannelLayout::GrayA { + let expanded: Vec = expected.as_chunks::<2>().0.iter().flat_map(|[gray, alpha]| [*gray, *gray, *gray, *alpha]).collect(); + (4, expanded) + } else { + (channels, expected.clone()) + }; + let encoded = encode(&input, Format::Jxl, &EncodeOptions::default()).unwrap(); + let decoder = JxlImage::builder().read(&encoded[..]).unwrap(); + assert_eq!((decoder.width(), decoder.height()), (w, h)); + let render = decoder.render_frame(0).unwrap(); + let mut stream = render.stream(); + assert_eq!(stream.channels() as usize, output_channels, "{layout:?} {sample:?}"); + let mut output = vec![0u16; expected_output.len()]; + assert_eq!(stream.write_to_buffer(&mut output), expected_output.len()); + assert_eq!(output, expected_output, "{layout:?} {sample:?}"); + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn jpeg_xl_preserves_sixteen_bit_alpha_across_dimension_header_sizes() { + use jxl_oxide::JxlImage; + + // Exercise each size-header coding tier, and non-opaque alpha extremes. + for width in [2u32, 513, 8193] { + let height = 2u32; + let mut samples = Vec::with_capacity(width as usize * height as usize * 4); + for i in 0..(width as usize * height as usize) { + samples.extend_from_slice(&[(i as u16).wrapping_mul(127), 65535, (i as u16).wrapping_mul(9973), [0u16, 1, 32768, 65535][i % 4]]); + } + let input = Image::from_u16(width, height, ChannelLayout::Rgba, &samples).unwrap(); + let encoded = encode(&input, Format::Jxl, &EncodeOptions::default()).unwrap(); + let decoder = JxlImage::builder().read(&encoded[..]).unwrap(); + let render = decoder.render_frame(0).unwrap(); + let mut stream = render.stream(); + assert_eq!(stream.channels(), 4, "width={width}"); + let mut output = vec![0u16; samples.len()]; + assert_eq!(stream.write_to_buffer(&mut output), samples.len()); + assert_eq!(output, samples, "width={width}"); + } +} + +#[test] +fn jpeg_xl_rejects_one_pixel_images_without_panicking() { + // zune-jpegxl currently requires at least two pixels on each axis. + let image = Image::from_u8(1, 1, ChannelLayout::Rgb, vec![42, 100, 255]).unwrap(); + assert!(matches!(encode(&image, Format::Jxl, &EncodeOptions::default()), Err(CodecError::Encode { format: Format::Jxl, .. }))); +} diff --git a/crates/image-codecs/tests/pnm_peak_memory.rs b/crates/image-codecs/tests/pnm_peak_memory.rs new file mode 100644 index 000000000..33bf39b3c --- /dev/null +++ b/crates/image-codecs/tests/pnm_peak_memory.rs @@ -0,0 +1,36 @@ +//! #2938: `Limits::max_alloc` budgets the decoded pixel buffer, so the PNM decoder must not build a +//! wider scratch copy (it used to collect one `u32` per sample, four times an 8-bit image) next to it. +//! +//! This file holds a single test so it runs in its own process: it reads the process's peak RSS +//! (`VmHWM`, Linux only) and nothing else may allocate meanwhile. + +#[cfg(target_os = "linux")] +fn peak_rss_bytes() -> Option { + let status = std::fs::read_to_string("/proc/self/status").ok()?; + let line = status.lines().find(|l| l.starts_with("VmHWM:"))?; + let kib: u64 = line.trim_start_matches("VmHWM:").trim().trim_end_matches("kB").trim().parse().ok()?; + Some(kib * 1024) +} + +#[cfg(target_os = "linux")] +#[test] +fn pnm_decode_peak_memory_stays_near_the_output_size() { + use craft_image_codecs::*; + + const SIDE: u32 = 4096; // 16 MiB of 8-bit gray output + let out_bytes = (SIDE * SIDE) as u64; + let mut file = format!("P5\n{SIDE} {SIDE}\n255\n").into_bytes(); + file.extend((0..out_bytes).map(|i| (i % 251) as u8)); + // Writing 5 resets the peak RSS to the current RSS; skip where the kernel doesn't allow it. + if std::fs::write("/proc/self/clear_refs", "5").is_err() { + return; + } + let Some(before) = peak_rss_bytes() else { return }; + let img = decode(&file).unwrap(); + let after = peak_rss_bytes().unwrap(); + assert_eq!(img.data().len() as u64, out_bytes); + assert_eq!(img.data()[300], (300 % 251) as u8); + // The output buffer plus slack; a `u32`-per-sample scratch copy alone would be 4× it. + let grown = after.saturating_sub(before); + assert!(grown < 2 * out_bytes, "decoding a {out_bytes} byte image raised peak RSS by {grown} bytes"); +} diff --git a/crates/raster-codecs/Cargo.toml b/crates/raster-codecs/Cargo.toml index cadf9cbd0..e8d6bc3ba 100644 --- a/crates/raster-codecs/Cargo.toml +++ b/crates/raster-codecs/Cargo.toml @@ -11,8 +11,9 @@ repository.workspace = true workspace = true [dependencies] +craft-image-codecs = { workspace = true } thiserror = { workspace = true } -image = { version = "0.25", default-features = false, features = ["png", "jpeg", "bmp", "gif", "tiff", "webp"] } +image = { version = "0.25", default-features = false } # The DDS encoder compresses rows of blocks in parallel on native; wasm stays single-threaded. [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/raster-codecs/README.md b/crates/raster-codecs/README.md index b69ca2817..6735769ae 100644 --- a/crates/raster-codecs/README.md +++ b/crates/raster-codecs/README.md @@ -3,8 +3,10 @@ Small raster image formats for the Craft apps, from PhotoCraft's `photocraft-codecs`: one module per format, one implementation of each. Decoders take untrusted bytes and `Limits`, check the declared size before allocating, and never panic. No `unsafe`; it builds for -`wasm32-unknown-unknown`. Dependencies: `thiserror`, and `rayon` on native targets (the DDS -encoder compresses rows of blocks in parallel). +`wasm32-unknown-unknown`. The `common` RGBA image facade delegates PNG/JPEG/BMP/GIF/TIFF/WebP +decoding and PNG/JPEG encoding to [`craft-image-codecs`](../image-codecs/README.md), the shared +depth- and metadata-preserving codec implementation. Other dependencies are `thiserror`, and +`rayon` on native targets (the DDS encoder compresses rows of blocks in parallel). ```toml craft-raster-codecs = { git = "https://github.com/storytold/craft-libs", rev = "" } diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index da3c1c39d..ec61aeedd 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -4,9 +4,7 @@ //! the source was PNG, JPEG, BMP, GIF, TIFF, or WebP. Format sniffing and codec dispatch stay //! inside this crate. Callers choose their own stricter file and image limits. -use std::io::Cursor; - -use image::{ImageReader, Limits as ImageLimits, RgbaImage}; +use image::RgbaImage; use crate::CodecError; @@ -33,33 +31,24 @@ pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result limits.max_file_bytes { return Err(CodecError::LimitExceeded(format!("{} encoded bytes exceed max {}", bytes.len(), limits.max_file_bytes))); } - if is_heif(bytes) { - return Err(CodecError::unsupported("image", "HEIC/HEIF is not supported by the enabled codecs")); + let mut decode_limits = craft_image_codecs::Limits::default(); + decode_limits.max_width = limits.max_width; + decode_limits.max_height = limits.max_height; + decode_limits.max_pixels = limits.max_pixels; + decode_limits.max_alloc = limits.max_alloc_bytes; + let decoded = craft_image_codecs::decode_with(bytes, &craft_image_codecs::DecodeOptions { limits: decode_limits, ..Default::default() }) + .map_err(map_codec_error)?; + let output_bytes = u64::from(decoded.width()).saturating_mul(u64::from(decoded.height())).saturating_mul(4); + if output_bytes > limits.max_alloc_bytes { + return Err(CodecError::LimitExceeded(format!("{output_bytes} RGBA bytes exceed max {}", limits.max_alloc_bytes))); } - let reader = || -> Result>, CodecError> { - let mut reader = ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(|e| CodecError::malformed("image", e))?; - if reader.format().is_none() { - return Err(CodecError::unsupported("image", "unrecognised image format")); - } - let mut image_limits = ImageLimits::default(); - image_limits.max_image_width = Some(limits.max_width); - image_limits.max_image_height = Some(limits.max_height); - image_limits.max_alloc = Some(limits.max_alloc_bytes); - reader.limits(image_limits); - Ok(reader) - }; - let (width, height) = reader()?.into_dimensions().map_err(|e| CodecError::malformed("image", e))?; - check_dimensions(width, height, limits)?; - let decoded = reader()?.decode().map_err(|e| CodecError::malformed("image", e))?; - check_dimensions(decoded.width(), decoded.height(), limits)?; - Ok(decoded.to_rgba8()) + RgbaImage::from_raw(decoded.width(), decoded.height(), decoded.to_rgba8()) + .ok_or_else(|| CodecError::malformed("image", "decoded RGBA dimensions do not match its pixel buffer")) } /// Encode an RGBA image as PNG. pub fn encode_png(pixels: &RgbaImage) -> Result, CodecError> { - let mut out = Cursor::new(Vec::new()); - pixels.write_to(&mut out, image::ImageFormat::Png).map_err(|e| CodecError::encode("PNG", e))?; - Ok(out.into_inner()) + encode(pixels, craft_image_codecs::Format::Png, craft_image_codecs::EncodeOptions::default()) } /// Encode RGBA pixels as JPEG at the requested quality (1–100). @@ -67,30 +56,32 @@ pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecErro if !(1..=100).contains(&quality) { return Err(CodecError::InvalidImage(format!("JPEG quality {quality} is outside 1–100"))); } - let rgb = image::DynamicImage::ImageRgba8(pixels.clone()).to_rgb8(); - let mut out = Cursor::new(Vec::new()); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, quality).encode_image(&rgb).map_err(|e| CodecError::encode("JPEG", e))?; - Ok(out.into_inner()) + encode(pixels, craft_image_codecs::Format::Jpeg, craft_image_codecs::EncodeOptions { jpeg_quality: quality, ..Default::default() }) +} + +fn encode(pixels: &RgbaImage, format: craft_image_codecs::Format, options: craft_image_codecs::EncodeOptions) -> Result, CodecError> { + let image = craft_image_codecs::Image::from_u8(pixels.width(), pixels.height(), craft_image_codecs::ChannelLayout::Rgba, pixels.as_raw().clone()) + .map_err(map_codec_error)?; + craft_image_codecs::encode(&image, format, &options).map_err(map_codec_error) } /// Encode pixels according to a destination filename's extension (`png`, `jpg`, or `jpeg`). pub fn encode_for_path(path: impl AsRef, pixels: &RgbaImage) -> Result, CodecError> { let extension = path.as_ref().extension().and_then(std::ffi::OsStr::to_str).unwrap_or_default(); - match extension.to_ascii_lowercase().as_str() { - "png" => encode_png(pixels), - "jpg" | "jpeg" => encode_jpeg(pixels, 90), - other => Err(CodecError::unsupported("image", format!("cannot encode files with the .{other} extension"))), + match craft_image_codecs::from_extension(extension) { + Some(craft_image_codecs::Format::Png) => encode_png(pixels), + Some(craft_image_codecs::Format::Jpeg) => encode_jpeg(pixels, 90), + _ => Err(CodecError::unsupported("image", format!("cannot encode files with the .{extension} extension"))), } } -fn is_heif(bytes: &[u8]) -> bool { - bytes.get(4..8) == Some(b"ftyp") && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) -} - -fn check_dimensions(width: u32, height: u32, limits: DecodeLimits) -> Result<(), CodecError> { - let pixels = u64::from(width).checked_mul(u64::from(height)).ok_or_else(|| CodecError::LimitExceeded("image dimensions overflow".into()))?; - if width == 0 || height == 0 || width > limits.max_width || height > limits.max_height || pixels > limits.max_pixels { - return Err(CodecError::LimitExceeded(format!("image dimensions {width}x{height} exceed configured limits"))); +fn map_codec_error(error: craft_image_codecs::CodecError) -> CodecError { + match error { + craft_image_codecs::CodecError::UnknownFormat => CodecError::unsupported("image", "unrecognized image format"), + craft_image_codecs::CodecError::Unsupported { format, reason } => CodecError::unsupported(format.name(), reason), + craft_image_codecs::CodecError::Malformed { format, message } => CodecError::malformed(format.name(), message), + craft_image_codecs::CodecError::LimitExceeded(message) => CodecError::LimitExceeded(message), + craft_image_codecs::CodecError::Encode { format, message } => CodecError::Encode { format: format.name(), message }, + craft_image_codecs::CodecError::InvalidImage(message) => CodecError::InvalidImage(message), } - Ok(()) } From 3bdf0eaadd2c20402413ae76038928fb15ca9670 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:17:35 -0500 Subject: [PATCH 046/520] Format shared image codec crate --- crates/image-codecs/src/codecs/dds.rs | 5 +- crates/image-codecs/src/codecs/dds_bc.rs | 3 +- crates/image-codecs/src/codecs/deep_exr.rs | 55 ++++++++++-------- crates/image-codecs/src/codecs/dicom.rs | 16 +++++- crates/image-codecs/src/codecs/dpx.rs | 15 +++-- crates/image-codecs/src/codecs/exr.rs | 22 ++++++-- .../src/codecs/exr_cryptomatte.rs | 6 +- crates/image-codecs/src/codecs/fits.rs | 12 ++-- crates/image-codecs/src/codecs/heif.rs | 12 ++-- crates/image-codecs/src/codecs/iff.rs | 9 ++- crates/image-codecs/src/codecs/jxl.rs | 6 +- crates/image-codecs/src/codecs/png.rs | 56 +++++++++++++++---- crates/image-codecs/src/codecs/pnm.rs | 4 +- crates/image-codecs/src/codecs/tiff/decode.rs | 14 +++-- crates/image-codecs/src/codecs/tiff/encode.rs | 6 +- crates/image-codecs/src/codecs/tiff_exif.rs | 3 +- crates/image-codecs/src/codecs/via_image.rs | 4 +- crates/image-codecs/src/codecs/vp8.rs | 6 +- crates/image-codecs/src/codecs/vp8/tables.rs | 26 +++++---- crates/image-codecs/src/codecs/webp.rs | 3 +- crates/image-codecs/src/exr_stream.rs | 9 ++- crates/image-codecs/src/fidelity.rs | 4 +- crates/image-codecs/src/format.rs | 25 +++++++-- crates/image-codecs/src/image.rs | 47 ++++++++++++---- crates/image-codecs/src/lib.rs | 13 +++-- crates/image-codecs/src/orientation.rs | 6 +- crates/image-codecs/src/source_settings.rs | 10 +++- crates/image-codecs/tests/it/dds_write.rs | 15 +++-- .../image-codecs/tests/it/decode_warnings.rs | 3 +- crates/image-codecs/tests/it/deep_exr.rs | 29 +++++++--- crates/image-codecs/tests/it/dpx.rs | 10 +++- .../image-codecs/tests/it/exr_cryptomatte.rs | 12 +++- crates/image-codecs/tests/it/exr_multipart.rs | 25 ++++++--- crates/image-codecs/tests/it/exr_stream.rs | 8 ++- crates/image-codecs/tests/it/fidelity.rs | 18 ++++-- crates/image-codecs/tests/it/limits.rs | 7 ++- crates/image-codecs/tests/it/metadata.rs | 13 ++++- crates/image-codecs/tests/it/orientation.rs | 10 +++- crates/image-codecs/tests/it/pcx.rs | 3 +- crates/image-codecs/tests/it/png_jpeg.rs | 3 +- crates/image-codecs/tests/it/resolution.rs | 9 ++- crates/image-codecs/tests/it/roundtrip.rs | 6 +- .../image-codecs/tests/it/source_settings.rs | 4 +- crates/image-codecs/tests/it/tiff_bands.rs | 17 +++--- crates/image-codecs/tests/it/tiff_bench.rs | 8 ++- crates/image-codecs/tests/it/tiff_exif.rs | 15 ++++- crates/image-codecs/tests/it/tiff_pages.rs | 12 +++- .../image-codecs/tests/it/tiff_photoshop.rs | 3 +- crates/image-codecs/tests/it/tiff_pyramid.rs | 16 +++++- crates/image-codecs/tests/it/tiff_ycbcr.rs | 21 ++++--- crates/image-codecs/tests/it/webp_frames.rs | 4 +- crates/image-codecs/tests/jxl_export.rs | 3 +- 52 files changed, 474 insertions(+), 197 deletions(-) diff --git a/crates/image-codecs/src/codecs/dds.rs b/crates/image-codecs/src/codecs/dds.rs index 1d220e6ac..9ed4e3f35 100644 --- a/crates/image-codecs/src/codecs/dds.rs +++ b/crates/image-codecs/src/codecs/dds.rs @@ -437,8 +437,9 @@ fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Res }; // Uncompressed: the pitch of one row. Compressed: the whole top level (its "linear size"). let (size_flag, pitch_or_size) = if store.is_block() { (DDSD_LINEARSIZE, Some(top)) } else { (DDSD_PITCH, store.row_bytes(width as usize)) }; - let pitch_or_size = - pitch_or_size.and_then(|v| u32::try_from(v).ok()).ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?; + let pitch_or_size = pitch_or_size + .and_then(|v| u32::try_from(v).ok()) + .ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?; let mipmapped = levels > 1; put(4, 124); put(8, DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | size_flag | if mipmapped { DDSD_MIPMAPCOUNT } else { 0 }); diff --git a/crates/image-codecs/src/codecs/dds_bc.rs b/crates/image-codecs/src/codecs/dds_bc.rs index bbd55b4bf..df60e462c 100644 --- a/crates/image-codecs/src/codecs/dds_bc.rs +++ b/crates/image-codecs/src/codecs/dds_bc.rs @@ -252,7 +252,8 @@ fn least_squares(block: &Block, opaque: Mask, bytes: &[u8; 8]) -> Option<(u16, u /// between the extremes other than 0 and 255, plus exact 0 and 255) fits better. fn alpha_block(block: &Block, inside: Mask) -> [u8; 8] { let alphas = || block.iter().enumerate().filter(move |(t, _)| inside >> t & 1 != 0).map(|(_, texel)| texel[3]); - let range = |values: &mut dyn Iterator| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))); + let range = + |values: &mut dyn Iterator| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))); let (lo, hi) = range(&mut alphas()).unwrap_or((0, 0)); // Eight values need a0 > a1; equal ends fall to the six-value mode, which starts with a0 too. let eight = alpha_fit(block, inside, hi, lo); diff --git a/crates/image-codecs/src/codecs/deep_exr.rs b/crates/image-codecs/src/codecs/deep_exr.rs index 0652112bf..0cc69e261 100644 --- a/crates/image-codecs/src/codecs/deep_exr.rs +++ b/crates/image-codecs/src/codecs/deep_exr.rs @@ -53,7 +53,12 @@ fn read_exact<'a>(cur: &mut Cursor<&'a [u8]>, n: usize, what: &str) -> Result<&' fn i32(cur: &mut Cursor<&[u8]>) -> Result { let b = read_exact(cur, 4, "an i32")?; - Ok(i32::from_le_bytes([b.first().copied().unwrap_or(0), b.get(1).copied().unwrap_or(0), b.get(2).copied().unwrap_or(0), b.get(3).copied().unwrap_or(0)])) + Ok(i32::from_le_bytes([ + b.first().copied().unwrap_or(0), + b.get(1).copied().unwrap_or(0), + b.get(2).copied().unwrap_or(0), + b.get(3).copied().unwrap_or(0), + ])) } fn u64(cur: &mut Cursor<&[u8]>) -> Result { @@ -125,7 +130,8 @@ fn reserve_up_to(out: &mut Vec, extra: usize, limit: usize, what: &str) -> R } if need > out.capacity() { let target = out.capacity().saturating_mul(2).max(need).min(limit); - out.try_reserve_exact(target.saturating_sub(out.len())).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {target} bytes for {what}")))?; + out.try_reserve_exact(target.saturating_sub(out.len())) + .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {target} bytes for {what}")))?; } Ok(()) } @@ -344,30 +350,31 @@ pub(crate) fn decode_deep(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Res // Block coordinates → the block's pixel bounds and its slot. The per-pixel index is // built only after the chunk's declared payload sizes are validated below, so a // truncated or lying chunk cannot drive a large index allocation first. - let (block_rows, block_cols, slot, row_w, scanline_row): (std::ops::Range, std::ops::Range, usize, usize, bool) = match &header.blocks { - BlockDescription::ScanLines => { - let y = i32(&mut cur)?; - let row = match y.checked_sub(y0) { - Some(d) if d >= 0 && (d as usize) < h => d as usize, - _ => return Err(err(format!("scan line block y={y} is outside the data window"))), - }; - (row..row + 1, 0..w, row, w, true) - } - BlockDescription::Tiles(_) => { - let (tx, ty, lx, ly) = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); - if (lx, ly) != (0, 0) { - return Err(err(format!("deep tile at level ({lx},{ly}); only level 0 exists in a single-level file"))); + let (block_rows, block_cols, slot, row_w, scanline_row): (std::ops::Range, std::ops::Range, usize, usize, bool) = + match &header.blocks { + BlockDescription::ScanLines => { + let y = i32(&mut cur)?; + let row = match y.checked_sub(y0) { + Some(d) if d >= 0 && (d as usize) < h => d as usize, + _ => return Err(err(format!("scan line block y={y} is outside the data window"))), + }; + (row..row + 1, 0..w, row, w, true) } - if tx < 0 || ty < 0 || (tx as usize) >= tiles_x || (ty as usize) * tile_h >= h { - return Err(err(format!("tile ({tx},{ty}) is outside the data window"))); + BlockDescription::Tiles(_) => { + let (tx, ty, lx, ly) = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); + if (lx, ly) != (0, 0) { + return Err(err(format!("deep tile at level ({lx},{ly}); only level 0 exists in a single-level file"))); + } + if tx < 0 || ty < 0 || (tx as usize) >= tiles_x || (ty as usize) * tile_h >= h { + return Err(err(format!("tile ({tx},{ty}) is outside the data window"))); + } + let (bx, by) = ((tx as usize) * tile_w, (ty as usize) * tile_h); + let rows = (by + tile_h).min(h); + let cols = (bx + tile_w).min(w); + let slot = (ty as usize).saturating_mul(tiles_x) + tx as usize; + (by..rows, bx..cols, slot, cols - bx, false) } - let (bx, by) = ((tx as usize) * tile_w, (ty as usize) * tile_h); - let rows = (by + tile_h).min(h); - let cols = (bx + tile_w).min(w); - let slot = (ty as usize).saturating_mul(tiles_x) + tx as usize; - (by..rows, bx..cols, slot, cols - bx, false) - } - }; + }; let block_len = block_rows.len().saturating_mul(block_cols.len()); if seen.get(slot).copied().unwrap_or(true) { return Err(err(format!("two chunks cover block {slot}"))); diff --git a/crates/image-codecs/src/codecs/dicom.rs b/crates/image-codecs/src/codecs/dicom.rs index ea2674dae..912d8672c 100644 --- a/crates/image-codecs/src/codecs/dicom.rs +++ b/crates/image-codecs/src/codecs/dicom.rs @@ -254,7 +254,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { .get(pixel.start..pixel.start.checked_add(need).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?) .ok_or_else(|| bad("pixel data is truncated"))?; let out_channels = layout.channels(); - let out_len = pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let out_len = + pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; let mut out = Vec::new(); out.try_reserve_exact(out_len).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; // Raw sample of channel `ch` at pixel `p`, honouring the planar configuration. @@ -367,7 +368,18 @@ mod tests { /// A DICOM with the given transfer syntax UID and data-set encoding. #[allow(clippy::too_many_arguments)] - fn synth(ts: &str, explicit: bool, big: bool, planar: u16, rows: u16, cols: u16, samples: u16, bits: u16, photometric: &str, pixels: &[u8]) -> Vec { + fn synth( + ts: &str, + explicit: bool, + big: bool, + planar: u16, + rows: u16, + cols: u16, + samples: u16, + bits: u16, + photometric: &str, + pixels: &[u8], + ) -> Vec { let mut b = vec![0u8; 128]; b.extend_from_slice(b"DICM"); // One extra meta element before the syntax, so a reader that only looks at the last diff --git a/crates/image-codecs/src/codecs/dpx.rs b/crates/image-codecs/src/codecs/dpx.rs index 86e4938b0..39be0e255 100644 --- a/crates/image-codecs/src/codecs/dpx.rs +++ b/crates/image-codecs/src/codecs/dpx.rs @@ -382,7 +382,8 @@ pub(crate) fn decode_cineon(bytes: &[u8], limits: &Limits) -> Result Result, CodecErr return Err(enc("the image has the wrong number of samples".into())); } let data_len = line_bytes(per_line, 10, 1).and_then(|l| l.checked_mul(h as usize)).ok_or_else(|| enc("image too large".into()))?; - let total = - CINEON_HEADER_LEN.checked_add(data_len).and_then(|t| u32::try_from(t).ok()).ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?; + let total = CINEON_HEADER_LEN + .checked_add(data_len) + .and_then(|t| u32::try_from(t).ok()) + .ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?; let mut wr = Writer { h: vec![0; CINEON_HEADER_LEN] }; // File information. @@ -731,8 +734,10 @@ mod tests { /// A minimal header for `w`×`h` with element 0 set; image data at 2048. fn header(o: Order, w: u32, h: u32, descriptor: u8, bits: u8, packing: u16) -> Vec { let mut b = vec![0u8; HEADER_LEN]; - let put32 = |b: &mut Vec, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); - let put16 = |b: &mut Vec, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + let put32 = + |b: &mut Vec, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + let put16 = + |b: &mut Vec, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); b[..4].copy_from_slice(if o == Order::Big { b"SDPX" } else { b"XPDS" }); put32(&mut b, 4, HEADER_LEN as u32); put32(&mut b, 28, INDUSTRY_LEN); diff --git a/crates/image-codecs/src/codecs/exr.rs b/crates/image-codecs/src/codecs/exr.rs index 0d68d20ca..04019bcdc 100644 --- a/crates/image-codecs/src/codecs/exr.rs +++ b/crates/image-codecs/src/codecs/exr.rs @@ -8,7 +8,8 @@ use std::io::Cursor; use exr::meta::MetaData; use exr::prelude::{ - AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, ReadChannels, ReadLayers, WritableImage, read, + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, ReadChannels, ReadLayers, WritableImage, + read, }; use half::f16; @@ -71,8 +72,10 @@ pub(crate) fn check_previews(b: &[u8]) -> Result<(), CodecError> { _ => return Ok(()), }; if kind == b"preview" { - let declared = - u32_at(value).zip(u32_at(value + 4)).and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)).filter(|_| size >= 8); + let declared = u32_at(value) + .zip(u32_at(value + 4)) + .and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)) + .filter(|_| size >= 8); if declared.is_none_or(|n| n > (size - 8) as u64) { return Err(err("a preview attribute declares more pixels than it holds")); } @@ -201,7 +204,8 @@ fn layer_to_image(layer: &Layer>) -> Result = channels.iter().map(|c| c.name.to_string()).collect(); - let (layout, selected) = exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| CodecError::unsupported(F, "no R/G/B or Y channels"))?; + let (layout, selected) = + exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| CodecError::unsupported(F, "no R/G/B or Y channels"))?; let color: Vec<_> = selected.iter().map(|i| channels.get(*i).ok_or_else(|| err("selected channel is missing"))).collect::>()?; let all_f16 = color.iter().all(|c| matches!(c.sample_data, FlatSamples::F16(_))); let sample = if all_f16 { SampleType::F16 } else { SampleType::F32 }; @@ -273,7 +277,10 @@ pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32), CodecEr pub(crate) fn decode_part(bytes: &[u8], index: usize, limits: &Limits) -> Result { let meta = read_meta(bytes, limits)?; if meta.requirements.has_deep_data { - return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + return Err(CodecError::unsupported( + F, + "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)", + )); } if index >= meta.headers.len() { return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); @@ -287,7 +294,10 @@ pub(crate) fn decode_part(bytes: &[u8], index: usize, limits: &Limits) -> Result pub(crate) fn decode_channels(bytes: &[u8], index: usize, names: &[&str], limits: &Limits) -> Result { let meta = read_meta(bytes, limits)?; if meta.requirements.has_deep_data { - return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + return Err(CodecError::unsupported( + F, + "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)", + )); } if index >= meta.headers.len() { return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); diff --git a/crates/image-codecs/src/codecs/exr_cryptomatte.rs b/crates/image-codecs/src/codecs/exr_cryptomatte.rs index 62fd2e1e0..f942231da 100644 --- a/crates/image-codecs/src/codecs/exr_cryptomatte.rs +++ b/crates/image-codecs/src/codecs/exr_cryptomatte.rs @@ -231,8 +231,10 @@ pub(crate) fn decode(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result< // returned per-pixel vectors could exceed the caller's max_alloc budget. let ranks = layer.channels.len().saturating_mul(2); let pair_capacity = (ranks as u64).saturating_mul(std::mem::size_of::<(f32, f32)>() as u64); - let per_pixel = - (layer.channels.len().max(1) as u64).saturating_mul(16).saturating_add(std::mem::size_of::>() as u64).saturating_add(pair_capacity); + let per_pixel = (layer.channels.len().max(1) as u64) + .saturating_mul(16) + .saturating_add(std::mem::size_of::>() as u64) + .saturating_add(pair_capacity); limits.check_bytes(w32, h32, per_pixel)?; let ex_layer = super::exr::read_layer(bytes, layer.part)?; diff --git a/crates/image-codecs/src/codecs/fits.rs b/crates/image-codecs/src/codecs/fits.rs index ae6e94505..67d37ca3e 100644 --- a/crates/image-codecs/src/codecs/fits.rs +++ b/crates/image-codecs/src/codecs/fits.rs @@ -39,8 +39,8 @@ const MAX_HDUS: usize = 1000; /// Keywords that describe the data layout. They are written from the image, never copied from /// (or kept as) text metadata: after decoding they no longer describe the pixels. const STRUCTURAL: &[&str] = &[ - "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", "DATAMIN", - "DATAMAX", "ZIMAGE", + "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", + "DATAMIN", "DATAMAX", "ZIMAGE", ]; fn err(msg: impl std::fmt::Display) -> CodecError { @@ -192,7 +192,9 @@ fn axes(h: &Header) -> Result, CodecError> { if !(0..=999).contains(&n) { return Err(err(format!("invalid NAXIS {n}"))); } - (1..=n).map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))).collect() + (1..=n) + .map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))) + .collect() } /// Bytes in the data unit (before padding to a block). @@ -433,7 +435,9 @@ fn ascii(s: &str) -> String { /// A FITS literal (number or logical) that can be written unquoted. fn literal(v: &str) -> bool { - v == "T" || v == "F" || (v.replace(['D', 'd'], "E").parse::().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b))) + v == "T" + || v == "F" + || (v.replace(['D', 'd'], "E").parse::().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b))) } fn quoted(v: &str, room: usize) -> String { diff --git a/crates/image-codecs/src/codecs/heif.rs b/crates/image-codecs/src/codecs/heif.rs index 4fde27cf1..775891ed8 100644 --- a/crates/image-codecs/src/codecs/heif.rs +++ b/crates/image-codecs/src/codecs/heif.rs @@ -92,7 +92,10 @@ mod tests { let header = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"; assert_eq!(crate::detect(header), Some(F), "still recognised"); let r = crate::decode(header); - assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("isn't included in this build")), "{r:?}"); + assert!( + matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("isn't included in this build")), + "{r:?}" + ); assert!(!crate::caps(F).read); } @@ -109,9 +112,10 @@ mod tests { /// (`boxes.rs:130`, "slice index starts at 24 but ends at 16"). It must be an error, not a crash. #[cfg(feature = "heif")] const HEIC_RS_BOX_PANIC: [u8; 72] = [ - 0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50, 0x72, 0x6d, - 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70, 0x00, 0xa8, 0x00, + 0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50, + 0x72, 0x6d, 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70, + 0x00, 0xa8, 0x00, ]; #[cfg(feature = "heif")] diff --git a/crates/image-codecs/src/codecs/iff.rs b/crates/image-codecs/src/codecs/iff.rs index 4997817ca..e006b3c11 100644 --- a/crates/image-codecs/src/codecs/iff.rs +++ b/crates/image-codecs/src/codecs/iff.rs @@ -190,7 +190,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { for row in stored.chunks_mut(plane_row.max(1)).take(total_rows) { match h.compression { 0 => { - let src = body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?; + let src = + body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?; let n = row.len().min(src.len()); row[..n].copy_from_slice(&src[..n]); cursor += plane_row; @@ -203,7 +204,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { } let mut out = Vec::new(); - out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; + out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())) + .map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; let planes = usize::from(h.planes); for y in 0..hh { for x in 0..w { @@ -215,7 +217,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { let c = if h.planes == DEEP_PLANES { [index as u8, (index >> 8) as u8, (index >> 16) as u8] } else { palette(index) }; out.extend_from_slice(&c); if h.masking != 0 { - let opaque = if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) }; + let opaque = + if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) }; out.push(if opaque { 255 } else { 0 }); } } diff --git a/crates/image-codecs/src/codecs/jxl.rs b/crates/image-codecs/src/codecs/jxl.rs index 11591c930..39dd287a8 100644 --- a/crates/image-codecs/src/codecs/jxl.rs +++ b/crates/image-codecs/src/codecs/jxl.rs @@ -141,7 +141,11 @@ pub(crate) fn encode(image: &Image, plan: Plan) -> Result, CodecError> { let options = ZuneOptions::new(width as usize, height as usize, space, depth); let mut output = Vec::new(); JxlSimpleEncoder::new(image.data(), options).encode(&mut output).map_err(|e| CodecError::encode(F, e.to_string()))?; - if depth == BitDepth::Sixteen && image.layout() == ChannelLayout::Rgba { correct_16_bit_alpha_header(&output, width, height) } else { Ok(output) } + if depth == BitDepth::Sixteen && image.layout() == ChannelLayout::Rgba { + correct_16_bit_alpha_header(&output, width, height) + } else { + Ok(output) + } })) .unwrap_or_else(|_| Err(CodecError::encode(F, "the JPEG XL encoder failed"))) } diff --git a/crates/image-codecs/src/codecs/png.rs b/crates/image-codecs/src/codecs/png.rs index d1ff5140c..cf061f49b 100644 --- a/crates/image-codecs/src/codecs/png.rs +++ b/crates/image-codecs/src/codecs/png.rs @@ -355,7 +355,9 @@ impl> PngDecodeRows { let at = held.checked_mul(self.line).ok_or_else(|| err("image too large"))?; let end = at.checked_add(self.line).ok_or_else(|| err("image too large"))?; if self.buf.len() < end { - self.buf.try_reserve_exact(end - self.buf.len()).map_err(|_| CodecError::LimitExceeded("cannot allocate a group of rows".into()))?; + self.buf + .try_reserve_exact(end - self.buf.len()) + .map_err(|_| CodecError::LimitExceeded("cannot allocate a group of rows".into()))?; self.buf.resize(end, 0); } let row = self.buf.get_mut(at..end).ok_or_else(|| err("invalid PNG row group"))?; @@ -990,7 +992,14 @@ fn inflate_text(mut p: PendingText, bytes: &[u8]) -> Result, utf8: &mut Option, over: &mut bool, bytes: &[u8], latin1: bool, limit: usize) -> Result { +fn append_checked( + out: &mut Vec, + utf8: &mut Option, + over: &mut bool, + bytes: &[u8], + latin1: bool, + limit: usize, +) -> Result { if let Some(check) = utf8.as_mut() && !check.feed(bytes) { @@ -1112,8 +1121,11 @@ fn start( && x > 0.0 && y > 0.0 { - info.pixel_dims = - Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + info.pixel_dims = Some(png::PixelDimensions { + xppu: (x / METERS_PER_INCH).round() as u32, + yppu: (y / METERS_PER_INCH).round() as u32, + unit: png::Unit::Meter, + }); } } let mut encoder = png::Encoder::with_info(out, info).map_err(|e| CodecError::encode(F, e))?; @@ -1136,7 +1148,9 @@ fn start( res.map_err(|e| CodecError::encode(F, e))?; } if let Some(xmp) = &meta.xmp { - encoder.add_itxt_chunk(XMP_KEYWORD.into(), crate::resolution::export_xmp(xmp, meta.dpi).into_owned()).map_err(|e| CodecError::encode(F, e))?; + encoder + .add_itxt_chunk(XMP_KEYWORD.into(), crate::resolution::export_xmp(xmp, meta.dpi).into_owned()) + .map_err(|e| CodecError::encode(F, e))?; } } let mut writer = encoder.write_header().map_err(|e| CodecError::encode(F, e))?; @@ -1195,7 +1209,18 @@ impl PngRows { } let plan = crate::fidelity::plan(first, F, opts); let writer = start(out, (width, height), (plan.layout, plan.sample), first.icc.as_ref(), &first.meta, opts)?; - Ok(Self { writer, plan, width, height, rows: 0, level: opts.png_compression, pending: Vec::new(), prev: Vec::new(), adler: 1, started: false }) + Ok(Self { + writer, + plan, + width, + height, + rows: 0, + level: opts.png_compression, + pending: Vec::new(), + prev: Vec::new(), + adler: 1, + started: false, + }) } /// Encodes the next rows of the image: `band` is as wide as the image, in any layout and @@ -1235,7 +1260,8 @@ impl PngRows { let parts = deflate_rows(rows, prev, stride, bpp, per_band, deflate_level(self.level), finish, encode_threads()) .ok_or_else(|| CodecError::encode(F, "a band of rows could not be compressed"))?; let mut zlib = Vec::new(); - zlib.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6).map_err(|_| CodecError::encode(F, "not enough memory for PNG data"))?; + zlib.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6) + .map_err(|_| CodecError::encode(F, "not enough memory for PNG data"))?; if !self.started { zlib.extend_from_slice(&zlib_header(self.level)); self.started = true; @@ -1266,7 +1292,8 @@ impl PngRows { /// Build the zlib stream for an Adam7-interlaced image (filter type 0). fn adam7_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression) -> Result, CodecError> { // (x0, y0, dx, dy) per pass. - const PASSES: [(usize, usize, usize, usize); 7] = [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)]; + const PASSES: [(usize, usize, usize, usize); 7] = + [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)]; let mut raw = Vec::new(); raw.try_reserve_exact(data.len() + h * 7).map_err(|_| CodecError::encode(F, "not enough memory for PNG rows"))?; for (x0, y0, dx, dy) in PASSES { @@ -1553,8 +1580,11 @@ pub fn encode_indexed( && x > 0.0 && y > 0.0 { - info.pixel_dims = - Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + info.pixel_dims = Some(png::PixelDimensions { + xppu: (x / METERS_PER_INCH).round() as u32, + yppu: (y / METERS_PER_INCH).round() as u32, + unit: png::Unit::Meter, + }); } let mut out = Vec::new(); { @@ -1880,7 +1910,8 @@ mod parallel_tests { for (layout, sample, ch, bytes) in [(ChannelLayout::Rgb, SampleType::U8, 3usize, 1usize), (ChannelLayout::Rgba, SampleType::U16, 4, 2)] { let (w, h) = (1531u32, 977u32); let n = w as usize * h as usize * ch * bytes; - let px: Vec = (0..n).map(|i| ((i / 3) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect(); + let px: Vec = + (0..n).map(|i| ((i / 3) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect(); let img = Image::from_raw(w, h, layout, sample, px.clone()).unwrap(); assert!(n >= PARALLEL_MIN_BYTES || sample == SampleType::U8); for level in [PngCompression::Fast, PngCompression::Default] { @@ -2088,7 +2119,8 @@ mod rows_tests { #[test] fn metadata_yields_do_not_finalize_the_pixels_again() { let text = "A".repeat(20_000); - let img = image(8, 6, ChannelLayout::Rgb, SampleType::U8).with_meta(Metadata { text: vec![("Comment".into(), text.clone())], ..Default::default() }); + let img = image(8, 6, ChannelLayout::Rgb, SampleType::U8) + .with_meta(Metadata { text: vec![("Comment".into(), text.clone())], ..Default::default() }); let bytes = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); let mut calls = 0; diff --git a/crates/image-codecs/src/codecs/pnm.rs b/crates/image-codecs/src/codecs/pnm.rs index c16cccfd0..2931d8da6 100644 --- a/crates/image-codecs/src/codecs/pnm.rs +++ b/crates/image-codecs/src/codecs/pnm.rs @@ -338,7 +338,9 @@ pub(crate) fn encode(src: &Image, plan: Plan, _opts: &EncodeOptions) -> Result "CMYK", _ => "CMYK_ALPHA", }; - out.extend_from_slice(format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes()); + out.extend_from_slice( + format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes(), + ); } } if sample == SampleType::U8 { diff --git a/crates/image-codecs/src/codecs/tiff/decode.rs b/crates/image-codecs/src/codecs/tiff/decode.rs index d8e11c447..97efdd979 100644 --- a/crates/image-codecs/src/codecs/tiff/decode.rs +++ b/crates/image-codecs/src/codecs/tiff/decode.rs @@ -4,8 +4,8 @@ use super::bands::{Tally, convert_ab, decode_bands, extra_sample_indices, refill use super::tags::{F, TAG_IMAGE_SOURCE_DATA, TAG_PHOTOSHOP, TAG_XMP, TEXT_TAGS, photoshop_tags}; use super::ycbcr::YCbCr; use super::{ - ChannelLayout, ChunkType, CodecError, DecodeWarning, Decoder, ExifDirs, File, Ifd, Image, Limits, Metadata, Read, SampleType, Seek, SeekFrom, Tag, - camera_raw, err, ifd, io, map_err, tiff_exif, + ChannelLayout, ChunkType, CodecError, DecodeWarning, Decoder, ExifDirs, File, Ifd, Image, Limits, Metadata, Read, SampleType, Seek, SeekFrom, + Tag, camera_raw, err, ifd, io, map_err, tiff_exif, }; /// Decodes the page Photoshop would open: the first full-resolution directory. @@ -46,7 +46,10 @@ pub(crate) fn has_unretained_samples(bytes: &[u8], offset: u64) -> Result) -> Result<(File<'_>, Ifd, Option), CodecError> { if camera_raw(bytes) { - return Err(CodecError::unsupported(F, "camera raw files (such as CR2, NEF, ARW or DNG) are not flat images; decode them with photocraft-raw")); + return Err(CodecError::unsupported( + F, + "camera raw files (such as CR2, NEF, ARW or DNG) are not flat images; decode them with photocraft-raw", + )); } let file = File::parse(bytes).ok_or_else(|| err("not a TIFF or BigTIFF header"))?; let info = ifd::info_of(&file)?; @@ -262,7 +265,10 @@ fn scratch_bound(direct: bool, chunk_bytes: u64, band_rows: usize, h: u32, worke pub(crate) fn orientation(bytes: &[u8], page: Option) -> u16 { let Some(file) = File::parse(bytes) else { return 1 }; let Ok(info) = ifd::info_of(&file) else { return 1 }; - page.or_else(|| info.default_page()).and_then(|i| info.pages.get(i)).and_then(|p| file.ifd(p.ifd_offset)).map_or(1, |d| ifd::orientation_of(&file, &d)) + page.or_else(|| info.default_page()) + .and_then(|i| info.pages.get(i)) + .and_then(|p| file.ifd(p.ifd_offset)) + .map_or(1, |d| ifd::orientation_of(&file, &d)) } /// The page's metadata; the ICC profile goes to `icc`. diff --git a/crates/image-codecs/src/codecs/tiff/encode.rs b/crates/image-codecs/src/codecs/tiff/encode.rs index 18b02a603..47b814e37 100644 --- a/crates/image-codecs/src/codecs/tiff/encode.rs +++ b/crates/image-codecs/src/codecs/tiff/encode.rs @@ -111,7 +111,8 @@ macro_rules! write_extras { /// is an error, not an abort. pub(super) fn try_with_capacity(len: usize) -> io::Result> { let mut v = Vec::new(); - v.try_reserve_exact(len).map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, format!("cannot allocate {len} samples for the extra samples")))?; + v.try_reserve_exact(len) + .map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, format!("cannot allocate {len} samples for the extra samples")))?; Ok(v) } @@ -411,7 +412,8 @@ pub(super) fn half_size(img: &Image) -> Result { let ch = layout.channels(); let bps = sample.bytes(); let src_row = (w as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "image row size overflows"))?; - let out_row = (nw as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "pyramid row size overflows"))?; + let out_row = + (nw as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "pyramid row size overflows"))?; let len = out_row.checked_mul(nh as usize).ok_or_else(|| CodecError::encode(F, "pyramid level size overflows"))?; if img.data().len() < src_row.saturating_mul(h as usize) { return Err(CodecError::encode(F, "image buffer is shorter than its dimensions")); diff --git a/crates/image-codecs/src/codecs/tiff_exif.rs b/crates/image-codecs/src/codecs/tiff_exif.rs index 2e6885b6e..e0fb8c641 100644 --- a/crates/image-codecs/src/codecs/tiff_exif.rs +++ b/crates/image-codecs/src/codecs/tiff_exif.rs @@ -157,7 +157,8 @@ pub(crate) fn block(dirs: &ExifDirs) -> Vec { ifd0.push(pointer(TAG_GPS_IFD)); } // Layout: header, then each directory followed by its out-of-line values. - let size = |fs: &[Field]| 2 + 12 * fs.len() + 4 + fs.iter().filter(|f| f.value.len() > 4).map(|f| f.value.len().next_multiple_of(2)).sum::(); + let size = + |fs: &[Field]| 2 + 12 * fs.len() + 4 + fs.iter().filter(|f| f.value.len() > 4).map(|f| f.value.len().next_multiple_of(2)).sum::(); let at0 = 8; let at_exif = at0 + size(&ifd0); let at_gps = at_exif + if dirs.exif.is_empty() { 0 } else { size(&dirs.exif) }; diff --git a/crates/image-codecs/src/codecs/via_image.rs b/crates/image-codecs/src/codecs/via_image.rs index 7c02c614e..02efc0287 100644 --- a/crates/image-codecs/src/codecs/via_image.rs +++ b/crates/image-codecs/src/codecs/via_image.rs @@ -327,7 +327,9 @@ fn to_dynamic(img: &Image) -> Result { let bad = || CodecError::InvalidImage("buffer size mismatch".into()); Ok(match (img.layout(), img.sample_type()) { (ChannelLayout::Gray, SampleType::U8) => DynamicImage::ImageLuma8(image::GrayImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), - (ChannelLayout::GrayA, SampleType::U8) => DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::GrayA, SampleType::U8) => { + DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?) + } (ChannelLayout::Rgb, SampleType::U8) => DynamicImage::ImageRgb8(image::RgbImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), (ChannelLayout::Rgba, SampleType::U8) => DynamicImage::ImageRgba8(image::RgbaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), (ChannelLayout::Rgb, SampleType::F32) => { diff --git a/crates/image-codecs/src/codecs/vp8.rs b/crates/image-codecs/src/codecs/vp8.rs index a24fb727c..ee27f2e38 100644 --- a/crates/image-codecs/src/codecs/vp8.rs +++ b/crates/image-codecs/src/codecs/vp8.rs @@ -20,9 +20,9 @@ mod transform; use bool_encoder::BoolEncoder; use predict::{Plane, SubEdges, predict_block, predict_sub}; use tables::{ - AC_QLOOKUP, B_DC_PRED, B_HE_PRED, B_PRED, B_TM_PRED, B_VE_PRED, BMODE_TREE, CAT_BASE, COEFF_BANDS, COEFF_TREE, COEFF_UPDATE_PROBS, DC_PRED, DC_QLOOKUP, - DCT_0, DCT_1, DCT_2, DCT_3, DCT_4, DCT_CAT1, DCT_CAT2, DCT_CAT3, DCT_CAT4, DCT_CAT5, DCT_CAT6, DCT_EOB, DEFAULT_COEFF_PROBS, H_PRED, KF_BMODE_PROBS, - KF_UV_MODE_PROB, KF_YMODE_PROB, KF_YMODE_TREE, PCAT, TM_PRED, UV_MODE_TREE, V_PRED, ZIGZAG, + AC_QLOOKUP, B_DC_PRED, B_HE_PRED, B_PRED, B_TM_PRED, B_VE_PRED, BMODE_TREE, CAT_BASE, COEFF_BANDS, COEFF_TREE, COEFF_UPDATE_PROBS, DC_PRED, + DC_QLOOKUP, DCT_0, DCT_1, DCT_2, DCT_3, DCT_4, DCT_CAT1, DCT_CAT2, DCT_CAT3, DCT_CAT4, DCT_CAT5, DCT_CAT6, DCT_EOB, DEFAULT_COEFF_PROBS, H_PRED, + KF_BMODE_PROBS, KF_UV_MODE_PROB, KF_YMODE_PROB, KF_YMODE_TREE, PCAT, TM_PRED, UV_MODE_TREE, V_PRED, ZIGZAG, }; use transform::{fdct, fwht, idct, iwht}; diff --git a/crates/image-codecs/src/codecs/vp8/tables.rs b/crates/image-codecs/src/codecs/vp8/tables.rs index 261231e9e..0178286bd 100644 --- a/crates/image-codecs/src/codecs/vp8/tables.rs +++ b/crates/image-codecs/src/codecs/vp8/tables.rs @@ -98,8 +98,14 @@ pub const COEFF_TREE: Tree = &[ /// Extra-bit probabilities of the six value categories (RFC 6386 §13.2), most significant bit /// first; the category bases are 5, 7, 11, 19, 35 and 67. -pub const PCAT: [&[u8]; 6] = - [&[159], &[165, 145], &[173, 148, 140], &[176, 155, 140, 135], &[180, 157, 141, 134, 130], &[254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129]]; +pub const PCAT: [&[u8]; 6] = [ + &[159], + &[165, 145], + &[173, 148, 140], + &[176, 155, 140, 135], + &[180, 157, 141, 134, 130], + &[254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129], +]; pub const CAT_BASE: [i32; 6] = [5, 7, 11, 19, 35, 67]; /// Coefficient position → probability band (RFC 6386 §13.3). @@ -454,18 +460,18 @@ pub const COEFF_UPDATE_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [ /// DC dequantization factors by quantizer index (RFC 6386 §14.1). pub const DC_QLOOKUP: [i32; 128] = [ - 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, - 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, - 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, - 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, + 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, + 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, + 112, 114, 116, 118, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, ]; /// AC dequantization factors by quantizer index (RFC 6386 §14.1). pub const AC_QLOOKUP: [i32; 128] = [ - 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, - 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 181, 185, 189, 193, 197, - 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, + 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, + 177, 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, ]; /// Key-frame sub-block mode probabilities `[above mode][left mode][tree node]` (RFC 6386 §11.5). diff --git a/crates/image-codecs/src/codecs/webp.rs b/crates/image-codecs/src/codecs/webp.rs index 3f69d3c9c..ab2ffc95e 100644 --- a/crates/image-codecs/src/codecs/webp.rs +++ b/crates/image-codecs/src/codecs/webp.rs @@ -186,7 +186,8 @@ pub(crate) fn export_exif(img: &Image) -> Option> { pub(crate) fn keeps_dpi(img: &Image) -> bool { let bare_default = img.meta.exif.as_deref().is_none_or(<[u8]>::is_empty) && img.meta.dpi.is_none_or(is_default_ppi); // The XMP written beside it carries the resolution too, and readers look there first. - let in_xmp = img.meta.xmp.as_deref().is_some_and(|x| crate::resolution::xmp_resolution(&crate::resolution::export_xmp(x, img.meta.dpi)).is_some()); + let in_xmp = + img.meta.xmp.as_deref().is_some_and(|x| crate::resolution::xmp_resolution(&crate::resolution::export_xmp(x, img.meta.dpi)).is_some()); bare_default || in_xmp || export_exif(img).is_some_and(|e| crate::resolution::exif_resolution(&e).is_some()) } diff --git a/crates/image-codecs/src/exr_stream.rs b/crates/image-codecs/src/exr_stream.rs index 1ed8ba127..936814360 100644 --- a/crates/image-codecs/src/exr_stream.rs +++ b/crates/image-codecs/src/exr_stream.rs @@ -143,7 +143,8 @@ impl Decoder { } // The same channels the whole-file decoder shows, so both routes open the same image. let names: Vec = list.iter().map(|c| c.name.to_string()).collect(); - let (layout, picked) = crate::image::exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| unsupported("no R/G/B or Y channels"))?; + let (layout, picked) = + crate::image::exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| unsupported("no R/G/B or Y channels"))?; let gray = matches!(layout, crate::ChannelLayout::Gray | crate::ChannelLayout::GrayA); let mut selected: Vec> = picked.into_iter().map(Some).collect(); if !matches!(layout, crate::ChannelLayout::GrayA | crate::ChannelLayout::Rgba) { @@ -241,8 +242,10 @@ impl Decoder { } self.source.read_exact(bytes).map_err(invalid)?; } - let chunk = - Chunk { layer_index: 0, compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }) }; + let chunk = Chunk { + layer_index: 0, + compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }), + }; let block = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| UncompressedBlock::decompress_chunk(chunk, &self.meta, false))) .map_err(|_| invalid("invalid EXR compression block"))? .map_err(invalid)?; diff --git a/crates/image-codecs/src/fidelity.rs b/crates/image-codecs/src/fidelity.rs index 984f2ac45..12c285d7b 100644 --- a/crates/image-codecs/src/fidelity.rs +++ b/crates/image-codecs/src/fidelity.rs @@ -164,7 +164,9 @@ pub(crate) fn plan(image: &Image, format: Format, opts: &EncodeOptions) -> Plan match format { // PNM: integer data can use any layout (PAM); float goes to PFM // which only has gray and RGB. - Format::Pnm if sample.is_float() => Plan { layout: pick_layout(layout, &[ChannelLayout::Gray, ChannelLayout::Rgb], false), sample: SampleType::F32 }, + Format::Pnm if sample.is_float() => { + Plan { layout: pick_layout(layout, &[ChannelLayout::Gray, ChannelLayout::Rgb], false), sample: SampleType::F32 } + } // DPX has no luma + alpha descriptor: gray with alpha is written as luma, alpha dropped. // The sample type follows the chosen bits (U8 for 8, U16 for 10, 12 and 16). Format::Dpx => Plan { diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index 20a520e31..c77f9a09f 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -73,7 +73,10 @@ pub struct FormatCaps { /// Formats that are enabled but intentionally not symmetric, with the reason. /// The test-suite asserts that every other enabled format is read+write. pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[ - (Format::Xpm, "XPM is a legacy Unix icon interchange format. Reading existing XPM icons is supported; writing modern image assets should use PNG instead."), + ( + Format::Xpm, + "XPM is a legacy Unix icon interchange format. Reading existing XPM icons is supported; writing modern image assets should use PNG instead.", + ), ( Format::Dicom, "DICOM single-frame images are imported for medical review; this crate has no DICOM writer, so save an imported frame as TIFF or PNG instead.", @@ -257,11 +260,23 @@ pub fn caps(format: Format) -> FormatCaps { lossy: false, }; match format { - Format::Png => { - FormatCaps { depths: &[S::U8, S::U16], layouts: RGB_GRAY, icc: true, exif: true, xmp: true, dpi: true, text: true, animation: true, ..base } + Format::Png => FormatCaps { + depths: &[S::U8, S::U16], + layouts: RGB_GRAY, + icc: true, + exif: true, + xmp: true, + dpi: true, + text: true, + animation: true, + ..base + }, + Format::Jpeg => { + FormatCaps { layouts: &[L::Gray, L::Rgb, L::Cmyk], alpha: false, icc: true, exif: true, xmp: true, dpi: true, lossy: true, ..base } + } + Format::Tiff => { + FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: TIFF_LAYOUTS, icc: true, exif: true, xmp: true, dpi: true, text: true, ..base } } - Format::Jpeg => FormatCaps { layouts: &[L::Gray, L::Rgb, L::Cmyk], alpha: false, icc: true, exif: true, xmp: true, dpi: true, lossy: true, ..base }, - Format::Tiff => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: TIFF_LAYOUTS, icc: true, exif: true, xmp: true, dpi: true, text: true, ..base }, // Resolution travels in EXIF (WebP has no field of its own). Format::WebP => FormatCaps { icc: true, exif: true, xmp: true, dpi: true, animation: true, ..base }, Format::Gif => FormatCaps { layouts: &[L::Rgba], animation: true, lossy: true, ..base }, diff --git a/crates/image-codecs/src/image.rs b/crates/image-codecs/src/image.rs index fb3294948..dc555c42b 100644 --- a/crates/image-codecs/src/image.rs +++ b/crates/image-codecs/src/image.rs @@ -97,7 +97,10 @@ impl ChannelLayout { /// `true` if both layouts share a colour model (ignoring alpha). pub const fn same_model(self, other: Self) -> bool { - (self.is_gray() && other.is_gray()) || (self.is_rgb() && other.is_rgb()) || (self.is_cmyk() && other.is_cmyk()) || (self.is_lab() && other.is_lab()) + (self.is_gray() && other.is_gray()) + || (self.is_rgb() && other.is_rgb()) + || (self.is_cmyk() && other.is_cmyk()) + || (self.is_lab() && other.is_lab()) } } @@ -521,7 +524,10 @@ impl Image { pub fn from_raw(width: u32, height: u32, layout: ChannelLayout, sample: SampleType, data: Vec) -> Result { let expected = byte_len(width, height, layout, sample).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; if data.len() != expected { - return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{height} {layout:?} {sample:?}", data.len()))); + return Err(CodecError::InvalidImage(format!( + "buffer has {} bytes, expected {expected} for {width}x{height} {layout:?} {sample:?}", + data.len() + ))); } Ok(Image { width, height, layout, sample, data, icc: None, cicp: None, meta: Metadata::default(), warnings: Vec::new() }) } @@ -607,7 +613,8 @@ impl Image { /// Copy samples out as `f16` (only when the sample type is `F16`). pub fn to_f16_samples(&self) -> Option> { - (self.sample == SampleType::F16).then(|| self.data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]]))).collect()) + (self.sample == SampleType::F16) + .then(|| self.data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]]))).collect()) } /// Copy samples out as `f32` (only when the sample type is `F32`). @@ -688,7 +695,11 @@ impl Image { /// [`Image::convert`], borrowing `self` when it already has that layout and sample type /// (encoders use it so a full-size image isn't copied for nothing). pub fn converted(&self, layout: ChannelLayout, sample: SampleType) -> std::borrow::Cow<'_, Image> { - if layout == self.layout && sample == self.sample { std::borrow::Cow::Borrowed(self) } else { std::borrow::Cow::Owned(self.convert(layout, sample)) } + if layout == self.layout && sample == self.sample { + std::borrow::Cow::Borrowed(self) + } else { + std::borrow::Cow::Owned(self.convert(layout, sample)) + } } /// Interleaved RGBA, 8 bits per sample. @@ -742,10 +753,21 @@ fn convert_data(data: &[u8], from: ChannelLayout, from_sample: SampleType, layou /// [`Image::convert`] on borrowed pixels: `rows` rows of `width` pixels in `from` (layout, sample /// type) become the same pixels in `to`, bit for bit what the image conversion gives. Lets a /// caller convert a band of a decoded image without copying it into an [`Image`] first. -pub fn convert_samples(src: &[u8], width: u32, rows: u32, from: (ChannelLayout, SampleType), to: (ChannelLayout, SampleType)) -> Result, CodecError> { +pub fn convert_samples( + src: &[u8], + width: u32, + rows: u32, + from: (ChannelLayout, SampleType), + to: (ChannelLayout, SampleType), +) -> Result, CodecError> { let expected = byte_len(width, rows, from.0, from.1).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; if src.len() != expected { - return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{rows} {:?} {:?}", src.len(), from.0, from.1))); + return Err(CodecError::InvalidImage(format!( + "buffer has {} bytes, expected {expected} for {width}x{rows} {:?} {:?}", + src.len(), + from.0, + from.1 + ))); } Ok(convert_data(src, from.0, from.1, to.0, to.1)) } @@ -919,7 +941,9 @@ mod tests { let got = convert_samples(img.data(), 5, 2, (ChannelLayout::Rgb, sample), (layout, to)).unwrap(); assert_eq!(got, want.data(), "{sample:?} to {layout:?} {to:?}"); } - assert!(convert_samples(img.data().get(1..).unwrap_or_default(), 5, 2, (ChannelLayout::Rgb, sample), (ChannelLayout::Rgba, sample)).is_err()); + assert!( + convert_samples(img.data().get(1..).unwrap_or_default(), 5, 2, (ChannelLayout::Rgb, sample), (ChannelLayout::Rgba, sample)).is_err() + ); } } @@ -933,9 +957,12 @@ mod tests { // Every U8 and every U16 value through every depth pair (alpha goes through the same per-sample conversion; covered by the layout test). let all8: Vec = (0..=255).collect(); let all16: Vec = (0..=65535).collect(); - for (src, to) in - [(SampleType::U8, SampleType::U8), (SampleType::U8, SampleType::U16), (SampleType::U16, SampleType::U8), (SampleType::U16, SampleType::U16)] - { + for (src, to) in [ + (SampleType::U8, SampleType::U8), + (SampleType::U8, SampleType::U16), + (SampleType::U16, SampleType::U8), + (SampleType::U16, SampleType::U16), + ] { let img = if src == SampleType::U8 { Image::from_u8(256, 1, ChannelLayout::Gray, all8.clone()).unwrap() } else { diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index a5922d323..974ff205b 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -48,8 +48,8 @@ pub use crate::image::{ MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, }; pub use crate::options::{ - DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, - TiffCompression, + DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, + TIFF_PYRAMID_MIN, TiffCompression, }; pub use crate::orientation::{OrientSteps, exif_orientation, upright_exif, upright_xmp}; pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; @@ -284,7 +284,10 @@ pub fn has_unretained_source_data(bytes: &[u8]) -> Result { retained.and_then(|index| info.pages.get(index)).map_or(Ok(false), |page| tiff::has_unretained_samples(bytes, page.ifd_offset)) } Some(Format::Ico) => { - let count = bytes.get(4..6).and_then(|b| <[u8; 2]>::try_from(b).ok()).ok_or_else(|| CodecError::malformed(Format::Ico, "truncated image count"))?; + let count = bytes + .get(4..6) + .and_then(|b| <[u8; 2]>::try_from(b).ok()) + .ok_or_else(|| CodecError::malformed(Format::Ico, "truncated image count"))?; Ok(u16::from_le_bytes(count) > 1) } Some(Format::Dds) => dds::has_additional_images(bytes), @@ -416,6 +419,8 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jxl => jxl::encode(image, plan), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::encode(format, image, plan, opts), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => { + via_image::encode(format, image, plan, opts) + } } } diff --git a/crates/image-codecs/src/orientation.rs b/crates/image-codecs/src/orientation.rs index 5ad8be252..f77e6691f 100644 --- a/crates/image-codecs/src/orientation.rs +++ b/crates/image-codecs/src/orientation.rs @@ -177,8 +177,10 @@ pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> { let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else { return Cow::Borrowed(exif); }; - let values: Vec = - (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).filter_map(|e| orientation_value(order, body, e, big_tiff)).collect(); + let values: Vec = (0..count) + .filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)) + .filter_map(|e| orientation_value(order, body, e, big_tiff)) + .collect(); if values.iter().all(|v| v.read(order, body) == Some(1)) { return Cow::Borrowed(exif); } diff --git a/crates/image-codecs/src/source_settings.rs b/crates/image-codecs/src/source_settings.rs index ebd0421c3..c7bb51baf 100644 --- a/crates/image-codecs/src/source_settings.rs +++ b/crates/image-codecs/src/source_settings.rs @@ -40,8 +40,8 @@ const ANNEX_K_CHROMA: [u16; 64] = [ /// The natural-order index of the k-th coefficient in zig-zag order (T.81 Figure A.6): DQT /// stores its 64 values in this order. const ZIGZAG: [usize; 64] = [ - 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, - 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, + 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, ]; /// One quantisation table from a DQT segment, in natural order, with its precision. @@ -129,7 +129,11 @@ fn read_dqt(mut seg: &[u8], tables: &mut [Option
; 4]) { let Some(data) = rest.get(..n) else { return }; let mut values = [0u16; 64]; for (k, &natural) in ZIGZAG.iter().enumerate() { - let v = if wide { data.get(2 * k..2 * k + 2).map_or(0, |p| u16::from_be_bytes([p[0], p[1]])) } else { data.get(k).map_or(0, |&p| u16::from(p)) }; + let v = if wide { + data.get(2 * k..2 * k + 2).map_or(0, |p| u16::from_be_bytes([p[0], p[1]])) + } else { + data.get(k).map_or(0, |&p| u16::from(p)) + }; if let Some(dst) = values.get_mut(natural) { *dst = v; } diff --git a/crates/image-codecs/tests/it/dds_write.rs b/crates/image-codecs/tests/it/dds_write.rs index 4f66d3679..f9d9add8e 100644 --- a/crates/image-codecs/tests/it/dds_write.rs +++ b/crates/image-codecs/tests/it/dds_write.rs @@ -1,7 +1,9 @@ //! Writing DDS textures: every storage read back through the reader, the header fields, sizes //! that aren't a multiple of four, the mip chain, and the block-compression error on ramps. -use craft_image_codecs::{ChannelLayout, DdsCompression, DdsOptions, EncodeOptions, FidelityWarning, Format, Image, decode, encode, fidelity_warnings_with}; +use craft_image_codecs::{ + ChannelLayout, DdsCompression, DdsOptions, EncodeOptions, FidelityWarning, Format, Image, decode, encode, fidelity_warnings_with, +}; const DDSD_PITCH: u32 = 0x8; const DDSD_MIPMAPCOUNT: u32 = 0x2_0000; @@ -65,7 +67,8 @@ fn max_colour_diff(a: &Image, b: &Image) -> u8 { /// A 2D gradient: red across, green down and blue along the diagonal, opaque. fn gradient(w: u32, h: u32) -> Image { - let data = (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x * 4).min(255) as u8, (y * 4).min(255) as u8, ((x + y) * 2).min(255) as u8, 255])).collect(); + let data = + (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x * 4).min(255) as u8, (y * 4).min(255) as u8, ((x + y) * 2).min(255) as u8, 255])).collect(); Image::from_u8(w, h, ChannelLayout::Rgba, data).unwrap() } @@ -163,7 +166,9 @@ fn block_compressed_textures_round_trip_through_the_reader() { assert!(colour <= 24, "{compression:?} {w}x{h}: colour off by {colour}"); let alpha = max_diff_channel(&img, &back, 3); match compression { - DdsCompression::Bc1 => assert!(back.data().chunks(4).zip(img.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + DdsCompression::Bc1 => { + assert!(back.data().chunks(4).zip(img.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })) + } _ => assert!(alpha <= 8, "{compression:?} {w}x{h}: alpha off by {alpha}"), } } @@ -201,7 +206,9 @@ fn block_compression_error_on_gradients_is_bounded() { assert!(plane_err <= 10, "{compression:?}: 2D gradient off by {plane_err}"); assert!(p >= 38.0, "{compression:?}: 2D gradient PSNR {p:.1} dB"); match compression { - DdsCompression::Bc1 => assert!(line_back.data().chunks(4).zip(line.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + DdsCompression::Bc1 => { + assert!(line_back.data().chunks(4).zip(line.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })) + } // Four bits: within half a step of 17. DdsCompression::Bc2 => assert!(alpha <= 8, "BC2 alpha off by {alpha}"), // Eight values across a block's few levels of alpha. diff --git a/crates/image-codecs/tests/it/decode_warnings.rs b/crates/image-codecs/tests/it/decode_warnings.rs index 5995de931..908b102b3 100644 --- a/crates/image-codecs/tests/it/decode_warnings.rs +++ b/crates/image-codecs/tests/it/decode_warnings.rs @@ -159,7 +159,8 @@ fn apng(frames: u32, sixteen: bool, separate_default: bool) -> Vec { } let mut w = enc.write_header().unwrap(); for c in COLOURS.iter().cycle().take(images.max(1) as usize) { - let px: Vec = if sixteen { c.iter().flat_map(|&v| [v, v]).collect::>().repeat((W * H) as usize) } else { c.repeat((W * H) as usize) }; + let px: Vec = + if sixteen { c.iter().flat_map(|&v| [v, v]).collect::>().repeat((W * H) as usize) } else { c.repeat((W * H) as usize) }; w.write_image_data(&px).unwrap(); } w.finish().unwrap(); diff --git a/crates/image-codecs/tests/it/deep_exr.rs b/crates/image-codecs/tests/it/deep_exr.rs index a3c003f75..37fe315ad 100644 --- a/crates/image-codecs/tests/it/deep_exr.rs +++ b/crates/image-codecs/tests/it/deep_exr.rs @@ -383,8 +383,9 @@ fn opaque_front_hides_back() { fn hdr_colour_is_kept_and_edge_tiles_match_scanlines() { let (w, h) = (7, 5); let counts: Vec = (0..w * h).map(|p| (p as u32 * 3) % 4).collect(); - let sample = - |p: usize, ci: usize| -> Vec { (0..(p as u32 * 3) % 4).map(|k| [1.0, 0.25 * k as f32, 0.5, 2.0 + p as f32 / 8.0, 10.0 - k as f32][ci]).collect() }; + let sample = |p: usize, ci: usize| -> Vec { + (0..(p as u32 * 3) % 4).map(|k| [1.0, 0.25 * k as f32, 0.5, 2.0 + p as f32 / 8.0, 10.0 - k as f32][ci]).collect() + }; for compression in [0u8, 1, 2] { let scan = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, None, &counts, &sample)); let tiled = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, Some((3, 2)), &counts, &sample)); @@ -482,7 +483,8 @@ fn tiles_match_scanlines() { /// Half-float channels come back as F16 samples with exact values. #[test] fn half_channels_round_trip() { - let channels = [Chan { name: "A", ty: 1 }, Chan { name: "B", ty: 1 }, Chan { name: "G", ty: 1 }, Chan { name: "R", ty: 1 }, Chan { name: "Z", ty: 1 }]; + let channels = + [Chan { name: "A", ty: 1 }, Chan { name: "B", ty: 1 }, Chan { name: "G", ty: 1 }, Chan { name: "R", ty: 1 }, Chan { name: "Z", ty: 1 }]; let bytes = gen_deep(4, 4, &channels, 2, None, COUNTS_ONE, &|p, _ci| { let v = f16::from_f32(0.5).to_f32(); vec![v * (p as f32 + 1.0) / 4.0] @@ -661,7 +663,11 @@ fn deep_tile_count_table_is_admitted_against_max_alloc() { // The table and its scratch copy are admitted together: 8 MiB. assert_eq!(decode_deep_exr(&big, &budget(2 * table)).expect("room for the table and its copy"), scan, "compression {compression}"); let got = decode_deep_exr(&big, &budget(2 * table - 1)); - assert!(matches!(got, Err(CodecError::LimitExceeded(_))), "compression {compression}: one byte short gave {:?}", got.map(|d| d.total_samples())); + assert!( + matches!(got, Err(CodecError::LimitExceeded(_))), + "compression {compression}: one byte short gave {:?}", + got.map(|d| d.total_samples()) + ); } // Ordinary tiles: full-size edge tables decode under the small budget, like clipped ones. let counts: Vec = (0..35).map(|p| (p * 3) % 4).collect(); @@ -729,7 +735,8 @@ fn clipped_count_tables_include_scratch_in_the_budget() { let required = if compression == 0 { 256 } else { 512 }; assert_eq!(decode_deep_exr(&bytes, &budget(required)).expect("table and any scratch fit exactly"), want, "{what}"); assert!(matches!(decode_deep_exr(&bytes, &budget(required - 1)), Err(CodecError::LimitExceeded(_))), "{what}"); - let flat = craft_image_codecs::decode_with(&bytes, &craft_image_codecs::DecodeOptions { limits: budget(required - 1), ..Default::default() }); + let flat = + craft_image_codecs::decode_with(&bytes, &craft_image_codecs::DecodeOptions { limits: budget(required - 1), ..Default::default() }); assert!(matches!(flat, Err(CodecError::LimitExceeded(_))), "{what}"); if compression == 0 { assert_eq!(decode_deep_exr(&bytes, &budget(511)).expect("raw table needs no scratch"), want, "{what}"); @@ -802,9 +809,10 @@ type RealFile = (&'static str, u64, u64, &'static [(&'static str, &'static [u32] #[cfg(feature = "corpus")] const MULTIVARIATE_IDS: &[u32] = &[ - 127035619, 147085119, 319332210, 409096490, 523461226, 678492822, 683859976, 1058109025, 1193275200, 1503246798, 1552124883, 1613501244, 1672208113, - 1687665471, 1741895317, 1841370478, 1863854195, 1865183722, 2056745377, 2202049807, 2303547305, 2369798380, 2433329986, 2439705590, 2505186565, 2698909330, - 2862333315, 2898564507, 2979371527, 3007860936, 3182339638, 3236172513, 3497437340, 3626859046, 3656820528, 3693645783, 3739501011, 3936563811, 4048943790, + 127035619, 147085119, 319332210, 409096490, 523461226, 678492822, 683859976, 1058109025, 1193275200, 1503246798, 1552124883, 1613501244, + 1672208113, 1687665471, 1741895317, 1841370478, 1863854195, 1865183722, 2056745377, 2202049807, 2303547305, 2369798380, 2433329986, 2439705590, + 2505186565, 2698909330, 2862333315, 2898564507, 2979371527, 3007860936, 3182339638, 3236172513, 3497437340, 3626859046, 3656820528, 3693645783, + 3739501011, 3936563811, 4048943790, ]; #[cfg(feature = "corpus")] @@ -813,7 +821,10 @@ const REAL_FILES: &[RealFile] = &[ "11.deep.exr", 6056, 2, - &[("id", &[523461226, 683859976, 1552124883, 2202049807, 2303547305, 2433329986, 2698909330, 3182339638, 3236172513, 3626859046, 3739501011])], + &[( + "id", + &[523461226, 683859976, 1552124883, 2202049807, 2303547305, 2433329986, 2698909330, 3182339638, 3236172513, 3626859046, 3739501011], + )], ), ( "42.deep.exr", diff --git a/crates/image-codecs/tests/it/dpx.rs b/crates/image-codecs/tests/it/dpx.rs index 4d4835cc7..9b4d42e18 100644 --- a/crates/image-codecs/tests/it/dpx.rs +++ b/crates/image-codecs/tests/it/dpx.rs @@ -346,7 +346,10 @@ fn saving_sixteen_bits_at_ten_or_twelve_warns_with_the_stored_bits() { } assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits16))), []); assert_eq!(depth_warnings(&img, Format::Dpx, &opts(None)), []); - assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits8))), [FidelityWarning::DepthReduced { from: SampleType::U16, to: SampleType::U8 }]); + assert_eq!( + depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits8))), + [FidelityWarning::DepthReduced { from: SampleType::U16, to: SampleType::U8 }] + ); assert_eq!(depth_warnings(&img, Format::Cineon, &EncodeOptions::default()), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); } @@ -360,7 +363,10 @@ fn a_frame_opened_at_ten_or_twelve_bits_saves_back_without_a_warning() { } // A 12-bit frame saved at 10 bits does lose its two low bits. let twelve = decode(&encode(&img, Format::Dpx, &opts(Some(DpxBits::Bits12))).unwrap()).unwrap(); - assert_eq!(depth_warnings(&twelve, Format::Dpx, &opts(Some(DpxBits::Bits10))), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); + assert_eq!( + depth_warnings(&twelve, Format::Dpx, &opts(Some(DpxBits::Bits10))), + [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }] + ); let cineon = decode(&cineon_bytes(3, 4, 2, &cineon_codes(24))).unwrap(); assert_eq!(depth_warnings(&cineon, Format::Cineon, &EncodeOptions::default()), []); assert_eq!(depth_warnings(&cineon, Format::Dpx, &opts(Some(DpxBits::Bits10))), []); diff --git a/crates/image-codecs/tests/it/exr_cryptomatte.rs b/crates/image-codecs/tests/it/exr_cryptomatte.rs index 80c843492..dbf4f7010 100644 --- a/crates/image-codecs/tests/it/exr_cryptomatte.rs +++ b/crates/image-codecs/tests/it/exr_cryptomatte.rs @@ -4,11 +4,12 @@ //! the manifest round trip and the per-pixel pairs. Malformed inputs must error or //! degrade, never panic. +use craft_image_codecs::{Limits, cryptomatte_id, cryptomatte_key, cryptomatte_layers, cryptomatte_preview_color, decode_cryptomatte}; use exr::meta::attribute::{AttributeValue, Text}; use exr::prelude::{ - AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, WritableImage, + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, + WritableImage, }; -use craft_image_codecs::{Limits, cryptomatte_id, cryptomatte_key, cryptomatte_layers, cryptomatte_preview_color, decode_cryptomatte}; // ------------------------------------------------------------------------------------------- // The official hash test vectors (cryptomatte_utilities_tests.py, CryptoHashing). @@ -77,7 +78,12 @@ fn cryptomatte_file(name: &str) -> Vec { let s1: Vec = (0..w * h).map(|p| if p % w == 0 { sphere } else { 0.0 }).collect(); let s1c: Vec = (0..w * h).map(|p| if p % w == 0 { 0.25 } else { 0.0 }).collect(); let mk = |n: String, v: Vec| AnyChannel::new(n.as_str(), FlatSamples::F32(v)); - let list = vec![mk(format!("{name}00.red"), s0), mk(format!("{name}00.green"), s0c), mk(format!("{name}01.red"), s1), mk(format!("{name}01.green"), s1c)]; + let list = vec![ + mk(format!("{name}00.red"), s0), + mk(format!("{name}00.green"), s0c), + mk(format!("{name}01.red"), s1), + mk(format!("{name}01.green"), s1c), + ]; let key = cryptomatte_key("CryptoAsset"); // attributes always use the canonical name let manifest = format!("{{\"cube\":\"{:08x}\",\"sphere\":\"{:08x}\"}}", cube.to_bits(), sphere.to_bits()); let mut attrs = LayerAttributes::named(name); diff --git a/crates/image-codecs/tests/it/exr_multipart.rs b/crates/image-codecs/tests/it/exr_multipart.rs index 246b3d9ed..b98c045f7 100644 --- a/crates/image-codecs/tests/it/exr_multipart.rs +++ b/crates/image-codecs/tests/it/exr_multipart.rs @@ -3,14 +3,14 @@ //! listing, the colour-ranked auto-pick and part-by-index decoding, and malformed //! inputs must error, never panic. -use exr::prelude::{ - AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Image, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, - WritableImage, -}; use craft_image_codecs::f16; use craft_image_codecs::{ ChannelLayout, DecodeOptions, DecodeWarning, Format, Limits, SampleType, decode, decode_as_with, decode_exr_part, exr_info, probe_size, }; +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Image, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, + WritableImage, +}; fn layer_f32(name: &str, comp: Compression, chans: &[(&str, Vec)]) -> Layer> { let n = chans.iter().find(|(_, v)| !v.is_empty()).map_or(0, |(_, v)| v.len()); @@ -49,7 +49,11 @@ fn aov_file() -> Vec { let diffuse: [Vec; 3] = std::array::from_fn(|c| (0..n).map(|_| [0.3, 0.6, 0.9][c]).collect()); let layers = vec![ layer_f32("Z", Compression::Uncompressed, &[("Z", z)]), - layer_f32("beauty", Compression::ZIP1, &[("R", beauty[0].clone()), ("G", beauty[1].clone()), ("B", beauty[2].clone()), ("A", beauty[3].clone())]), + layer_f32( + "beauty", + Compression::ZIP1, + &[("R", beauty[0].clone()), ("G", beauty[1].clone()), ("B", beauty[2].clone()), ("A", beauty[3].clone())], + ), layer_f32("diffuse", Compression::RLE, &[("R", diffuse[0].clone()), ("G", diffuse[1].clone()), ("B", diffuse[2].clone())]), ]; write_image(layers) @@ -291,8 +295,15 @@ fn auto_pick_and_probe_skip_incoherent_rgb_parts() { #[test] fn flat_decode_prefers_the_highest_ranked_complete_group() { - let bytes = - channel_group_file(&[("aaa.R", 9.0), ("aaa.G", 8.0), ("aaa.B", 7.0), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0), ("beauty.A", 0.75)]); + let bytes = channel_group_file(&[ + ("aaa.R", 9.0), + ("aaa.G", 8.0), + ("aaa.B", 7.0), + ("beauty.R", 0.25), + ("beauty.G", 0.5), + ("beauty.B", 2.0), + ("beauty.A", 0.75), + ]); assert_channel_group(&bytes, ChannelLayout::Rgba, &[0.25, 0.5, 2.0, 0.75]); let bytes = channel_group_file(&[("aaa.Y", 9.0), ("beauty.Y", 0.5), ("beauty.A", 0.75)]); assert_channel_group(&bytes, ChannelLayout::GrayA, &[0.5, 0.75]); diff --git a/crates/image-codecs/tests/it/exr_stream.rs b/crates/image-codecs/tests/it/exr_stream.rs index 5d6ee4158..a3a6503f4 100644 --- a/crates/image-codecs/tests/it/exr_stream.rs +++ b/crates/image-codecs/tests/it/exr_stream.rs @@ -125,8 +125,12 @@ fn mixed_samples_and_offset_data_window_keep_coordinates_and_values() { .collect(), ); let attributes = LayerAttributes { layer_position: exr::math::Vec2(-15, 40), ..Default::default() }; - let layer = - Layer::new((31, 35), attributes, Encoding { compression: Compression::ZIP16, blocks: Blocks::ScanLines, line_order: LineOrder::Decreasing }, channels); + let layer = Layer::new( + (31, 35), + attributes, + Encoding { compression: Compression::ZIP16, blocks: Blocks::ScanLines, line_order: LineOrder::Decreasing }, + channels, + ); let mut bytes = Cursor::new(Vec::new()); exr::image::Image::from_layer(layer).write().to_buffered(&mut bytes).unwrap(); let mut decoder = Decoder::new(bytes, &|| false).unwrap(); diff --git a/crates/image-codecs/tests/it/fidelity.rs b/crates/image-codecs/tests/it/fidelity.rs index 37c7aba0c..98f362698 100644 --- a/crates/image-codecs/tests/it/fidelity.rs +++ b/crates/image-codecs/tests/it/fidelity.rs @@ -92,8 +92,13 @@ fn icc_warning_suppressed_when_not_embedding() { #[test] fn exif_xmp_dpi_text_dropped_for_qoi() { let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); - img.meta = - Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + img.meta = Metadata { + exif: Some(sample_exif()), + xmp: Some("x".into()), + dpi: Some((72.0, 72.0)), + text: vec![("a".into(), "b".into())], + ..Default::default() + }; let w = warns(&img, Format::Qoi); for x in [W::ExifDropped, W::XmpDropped, W::DpiDropped, W::TextDropped] { assert!(w.contains(&x), "{x:?}"); @@ -103,8 +108,13 @@ fn exif_xmp_dpi_text_dropped_for_qoi() { #[test] fn webp_drops_text_only() { let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); - img.meta = - Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + img.meta = Metadata { + exif: Some(sample_exif()), + xmp: Some("x".into()), + dpi: Some((72.0, 72.0)), + text: vec![("a".into(), "b".into())], + ..Default::default() + }; let w = warns(&img, Format::WebP); assert_eq!(w, vec![W::TextDropped]); } diff --git a/crates/image-codecs/tests/it/limits.rs b/crates/image-codecs/tests/it/limits.rs index 69e510151..e590aa568 100644 --- a/crates/image-codecs/tests/it/limits.rs +++ b/crates/image-codecs/tests/it/limits.rs @@ -393,7 +393,9 @@ fn pnm_header_alone_reserves_nothing_without_limits() { // 2⁶² bytes before finding the raster missing (an abort). let none = opts(Limits::none()); let side = 1u32 << 31; - for f in [format!("P1\n{side} {side}\n"), format!("P2\n{side} {side}\n255\n"), format!("P3\n{side} {side}\n255\n"), format!("P4\n{side} {side}\n")] { + for f in + [format!("P1\n{side} {side}\n"), format!("P2\n{side} {side}\n255\n"), format!("P3\n{side} {side}\n255\n"), format!("P4\n{side} {side}\n")] + { assert!(decode_with(f.as_bytes(), &none).is_err(), "{f:?}"); } } @@ -440,7 +442,8 @@ fn jpeg_bomb_header_rejected() { fn tiff_bomb_header_rejected() { // Minimal little-endian TIFF claiming 1_000_000 x 1_000_000 8-bit gray. let mut b = b"II*\0\x08\0\0\0".to_vec(); - let entries: [(u16, u16, u32, u32); 6] = [(256, 4, 1, 1_000_000), (257, 4, 1, 1_000_000), (258, 3, 1, 8), (262, 3, 1, 1), (273, 4, 1, 0), (279, 4, 1, 0)]; + let entries: [(u16, u16, u32, u32); 6] = + [(256, 4, 1, 1_000_000), (257, 4, 1, 1_000_000), (258, 3, 1, 8), (262, 3, 1, 1), (273, 4, 1, 0), (279, 4, 1, 0)]; b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); for (tag, ty, count, val) in entries { b.extend_from_slice(&tag.to_le_bytes()); diff --git a/crates/image-codecs/tests/it/metadata.rs b/crates/image-codecs/tests/it/metadata.rs index d6d292557..ed5855a3f 100644 --- a/crates/image-codecs/tests/it/metadata.rs +++ b/crates/image-codecs/tests/it/metadata.rs @@ -177,7 +177,11 @@ fn jpeg_multi_segment_icc() { fn jpeg_with_oversized_metadata_still_encodes() { let ancestors: String = (0..3000).map(|i| format!("xmp.did:{i:032x}")).collect(); let texts: String = (0..400) - .map(|i| format!("Layer {i}Words on layer {i}")) + .map(|i| { + format!( + "Layer {i}Words on layer {i}" + ) + }) .collect(); let xmp = format!( "\n{ancestors}{texts}" @@ -218,7 +222,9 @@ fn embed_flags_disable_metadata() { assert!(back.icc.is_none(), "{f:?}"); assert!(back.meta.exif.is_none() && back.meta.xmp.is_none(), "{f:?}"); assert!(back.meta.text.is_empty(), "{f:?}"); - assert!(fidelity_warnings_with(&img, Format::Bmp, &opts).iter().all(|w| !matches!(w, FidelityWarning::IccDropped | FidelityWarning::ExifDropped))); + assert!( + fidelity_warnings_with(&img, Format::Bmp, &opts).iter().all(|w| !matches!(w, FidelityWarning::IccDropped | FidelityWarning::ExifDropped)) + ); } } @@ -322,7 +328,8 @@ fn webp_keeps_dpi_without_source_exif() { let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); assert!(back.meta.exif.is_some()); // ...unless the XMP written beside it records the resolution, which readers look at first. - img.meta.xmp = Some(r#""#.into()); + img.meta.xmp = + Some(r#""#.into()); assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); let (x, y) = back.meta.dpi.expect("dpi from XMP"); diff --git a/crates/image-codecs/tests/it/orientation.rs b/crates/image-codecs/tests/it/orientation.rs index c278f36f6..d5d3ad90e 100644 --- a/crates/image-codecs/tests/it/orientation.rs +++ b/crates/image-codecs/tests/it/orientation.rs @@ -364,7 +364,9 @@ fn jpeg_opens_upright_for_every_orientation() { #[test] fn rgb_jpeg_with_dpi_swaps_resolution_on_a_quarter_turn() { - let img = Image::from_u8(16, 8, ChannelLayout::Rgb, vec![128; 16 * 8 * 3]).unwrap().with_meta(Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }); + let img = Image::from_u8(16, 8, ChannelLayout::Rgb, vec![128; 16 * 8 * 3]) + .unwrap() + .with_meta(Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }); let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); let back = decode(&jpeg_with_exif(&jpeg, &exif(6, false, false))).unwrap(); assert_eq!(back.dimensions(), (8, 16)); @@ -584,7 +586,11 @@ fn upright_exif_keeps_every_other_byte() { tail.extend_from_slice(&u32b(4)); tail.extend_from_slice(b"0232"); tail.extend_from_slice(&u32b(0)); - let e = ifd(big, &[(0x010F, 2, 12, u32b(make_at)), (0x0112, 3, 1, short(6, big)), (0x0131, 2, 4, *b"PC1\0"), (0x8769, 4, 1, u32b(sub_at))], &tail); + let e = ifd( + big, + &[(0x010F, 2, 12, u32b(make_at)), (0x0112, 3, 1, short(6, big)), (0x0131, 2, 4, *b"PC1\0"), (0x8769, 4, 1, u32b(sub_at))], + &tail, + ); let mut prefixed = b"Exif\0\0".to_vec(); prefixed.extend_from_slice(&e); let fixed = upright_exif(&prefixed).into_owned(); diff --git a/crates/image-codecs/tests/it/pcx.rs b/crates/image-codecs/tests/it/pcx.rs index b112a3af1..fbd0e7e57 100644 --- a/crates/image-codecs/tests/it/pcx.rs +++ b/crates/image-codecs/tests/it/pcx.rs @@ -112,7 +112,8 @@ fn rejects_truncated_and_overshooting_rle_without_panicking() { fn refuses_headers_that_exceed_allocation_budget() { let mut b = header(8, 3, 3000, 3000, 3000); // The default decoder rejects the allocation before reading any scanline. - let opts = craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; + let opts = + craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; assert!(matches!(craft_image_codecs::decode_as_with(Format::Pcx, &b, &opts), Err(CodecError::LimitExceeded(_)))); b[2] = 0; assert_eq!(detect(&b), None, "unsupported uncompressed encoding"); diff --git a/crates/image-codecs/tests/it/png_jpeg.rs b/crates/image-codecs/tests/it/png_jpeg.rs index e98b0ae09..0c7bc03d4 100644 --- a/crates/image-codecs/tests/it/png_jpeg.rs +++ b/crates/image-codecs/tests/it/png_jpeg.rs @@ -278,7 +278,8 @@ fn png_rows_take_one_call_per_row_and_report_progress() { #[test] fn png_rows_share_their_bytes_without_copying_them() { - let bytes = std::sync::Arc::new(encode(&synth(20, 20, ChannelLayout::Rgba, SampleType::U8, 3, 0.2), Format::Png, &EncodeOptions::default()).unwrap()); + let bytes = + std::sync::Arc::new(encode(&synth(20, 20, ChannelLayout::Rgba, SampleType::U8, 3, 0.2), Format::Png, &EncodeOptions::default()).unwrap()); let mut rows = PngDecodeRows::new(SharedBytes(bytes.clone()), &Limits::default()).unwrap(); let img = rows.step(&mut || true).unwrap().unwrap(); assert_eq!(img, decode(&bytes).unwrap()); diff --git a/crates/image-codecs/tests/it/resolution.rs b/crates/image-codecs/tests/it/resolution.rs index fe2c51c8e..eadecbaa3 100644 --- a/crates/image-codecs/tests/it/resolution.rs +++ b/crates/image-codecs/tests/it/resolution.rs @@ -280,7 +280,9 @@ fn truncated_metadata_never_panics_and_falls_back() { assert_eq!(photoshop_resolution(&r[..n]), None, "{n}"); near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), app13(&r[..n])])), 72.0); } - for x in ["tiff:XResolution=", "tiff:XResolution=\"", "tiff:XResolution=\"\" tiff:YResolution=\"5/0\"", "", "9<"] { + for x in + ["tiff:XResolution=", "tiff:XResolution=\"", "tiff:XResolution=\"\" tiff:YResolution=\"5/0\"", "", "9<"] + { assert_eq!(xmp_resolution(x), None, "{x}"); let _ = xmp_with_resolution(x, (1.0, 1.0)); } @@ -375,7 +377,10 @@ fn xmp_resolution_is_rewritten() { r#""# ); let elem = "300/1300/1"; - assert_eq!(xmp_with_resolution(elem, (72.5, 72.5)), "72500/100072500/1000"); + assert_eq!( + xmp_with_resolution(elem, (72.5, 72.5)), + "72500/100072500/1000" + ); for same in [r#"tiff:XResolution="240" tiff:YResolution="240/1""#, ""] { assert!(matches!(xmp_with_resolution(same, (240.0, 240.0)), Cow::Borrowed(_)), "{same}"); } diff --git a/crates/image-codecs/tests/it/roundtrip.rs b/crates/image-codecs/tests/it/roundtrip.rs index c9b179bc8..6d5d38bca 100644 --- a/crates/image-codecs/tests/it/roundtrip.rs +++ b/crates/image-codecs/tests/it/roundtrip.rs @@ -268,7 +268,8 @@ fn jpeg_format_options_round_trip() { let sof = |b: &[u8], m: u8| b.windows(2).any(|w| w == [0xFF, m]); for subsampled in [false, true] { let enc = |f| { - encode(&img, Format::Jpeg, &EncodeOptions { jpeg_format: f, jpeg_chroma_subsampling: subsampled, jpeg_quality: 90, ..Default::default() }).unwrap() + encode(&img, Format::Jpeg, &EncodeOptions { jpeg_format: f, jpeg_chroma_subsampling: subsampled, jpeg_quality: 90, ..Default::default() }) + .unwrap() }; let (base, opt, prog) = (enc(JpegFormat::Baseline), enc(JpegFormat::Optimized), enc(JpegFormat::Progressive)); // The scan layout only changes how the same quantized coefficients are stored, so every @@ -309,7 +310,8 @@ fn webp_lossy_request_is_a_lossy_file() { let back = decode(&bytes).unwrap(); assert_eq!(back.dimensions(), img.dimensions()); assert!( - fidelity_warnings_with(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).contains(&FidelityWarning::LossyCompression) + fidelity_warnings_with(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }) + .contains(&FidelityWarning::LossyCompression) ); } diff --git a/crates/image-codecs/tests/it/source_settings.rs b/crates/image-codecs/tests/it/source_settings.rs index 07b20471a..5923d562d 100644 --- a/crates/image-codecs/tests/it/source_settings.rs +++ b/crates/image-codecs/tests/it/source_settings.rs @@ -26,8 +26,8 @@ fn jpeg_quality_of_hand_built_tables() { // A minimal JPEG prefix: SOI, one DQT holding the Annex K luminance table (quality 50) in // zig-zag order, then SOS. let luma: [u8; 64] = [ - 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, 72, 92, 78, - 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, + 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, + 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, ]; let mut b = vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x00]; b.extend_from_slice(&luma); diff --git a/crates/image-codecs/tests/it/tiff_bands.rs b/crates/image-codecs/tests/it/tiff_bands.rs index a449ae2b7..fe61178dd 100644 --- a/crates/image-codecs/tests/it/tiff_bands.rs +++ b/crates/image-codecs/tests/it/tiff_bands.rs @@ -103,7 +103,11 @@ fn every_layout_matches_the_whole_image_decoder() { ), ( "palette 4-bit", - strips(W, H, 4, 1, 3, rows, &noise((W as usize).div_ceil(2) * H as usize, 8)).tag(320, SHORT, &(0..48u64).map(|i| i * 1365).collect::>()), + strips(W, H, 4, 1, 3, rows, &noise((W as usize).div_ceil(2) * H as usize, 8)).tag( + 320, + SHORT, + &(0..48u64).map(|i| i * 1365).collect::>(), + ), row(3, 1) * rows as usize, ), ("gray 1-bit", strips(W, H, 1, 1, 1, rows, &noise((W as usize).div_ceil(8) * H as usize, 9)), row(1, 1) * rows as usize), @@ -122,11 +126,9 @@ fn tiles_and_planar_data_match() { let (w, h) = (W as usize, H as usize); let px = noise(w * h * 3 * 2, 11); let tiled = |planar: bool| { - let mut d = Dir { entries: base(W, H, 16, 3, 2), chunks: tile_split(&px, w, h, 3, 2, 16, 16, planar), tiled: true }.tag(322, SHORT, &[16]).tag( - 323, - SHORT, - &[16], - ); + let mut d = Dir { entries: base(W, H, 16, 3, 2), chunks: tile_split(&px, w, h, 3, 2, 16, 16, planar), tiled: true } + .tag(322, SHORT, &[16]) + .tag(323, SHORT, &[16]); if planar { d = d.tag(284, SHORT, &[2]); } @@ -135,7 +137,8 @@ fn tiles_and_planar_data_match() { let band = w * 3 * 2 * 16; assert_same(&build(true, false, &[tiled(false)], &[0]).bytes, band, "chunky tiles"); assert_same(&build(true, true, &[tiled(true)], &[0]).bytes, band, "planar tiles, BigTIFF"); - let planar = Dir { entries: base(W, H, 16, 3, 2), chunks: planar_strips(&px, w, h, 3, 2, 5), tiled: false }.tag(278, LONG, &[5]).tag(284, SHORT, &[2]); + let planar = + Dir { entries: base(W, H, 16, 3, 2), chunks: planar_strips(&px, w, h, 3, 2, 5), tiled: false }.tag(278, LONG, &[5]).tag(284, SHORT, &[2]); assert_same(&build(true, false, &[planar], &[0]).bytes, w * 3 * 2 * 5, "planar strips"); } diff --git a/crates/image-codecs/tests/it/tiff_bench.rs b/crates/image-codecs/tests/it/tiff_bench.rs index 55cff55ca..99ed4f5d6 100644 --- a/crates/image-codecs/tests/it/tiff_bench.rs +++ b/crates/image-codecs/tests/it/tiff_bench.rs @@ -78,9 +78,11 @@ fn time(name: &str, bytes: &[u8]) { fn decode_30mp() { let rgb = pixels(3); let img = Image::from_u8(W, H, ChannelLayout::Rgb, rgb.clone()).unwrap(); - for (name, c) in - [("strips, none", TiffCompression::None), ("strips, LZW + predictor", TiffCompression::Lzw), ("strips, Deflate + predictor", TiffCompression::Deflate)] - { + for (name, c) in [ + ("strips, none", TiffCompression::None), + ("strips, LZW + predictor", TiffCompression::Lzw), + ("strips, Deflate + predictor", TiffCompression::Deflate), + ] { let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); time(&format!("RGB 8 {name}"), &bytes); } diff --git a/crates/image-codecs/tests/it/tiff_exif.rs b/crates/image-codecs/tests/it/tiff_exif.rs index a1ad5c542..6c08c091f 100644 --- a/crates/image-codecs/tests/it/tiff_exif.rs +++ b/crates/image-codecs/tests/it/tiff_exif.rs @@ -185,9 +185,18 @@ fn overlapping_tiff(entries: usize, count: u32) -> Vec { out.extend(42u16.to_le_bytes()); out.extend(8u32.to_le_bytes()); out.extend(10u16.to_le_bytes()); - for (tag, ty, value) in - [(256, 3, 1), (257, 3, 1), (258, 3, 8), (259, 3, 1), (262, 3, 1), (273, 4, 0), (277, 3, 1), (278, 3, 1), (279, 4, 1), (34665, 4, at_exif as u32)] - { + for (tag, ty, value) in [ + (256, 3, 1), + (257, 3, 1), + (258, 3, 8), + (259, 3, 1), + (262, 3, 1), + (273, 4, 0), + (277, 3, 1), + (278, 3, 1), + (279, 4, 1), + (34665, 4, at_exif as u32), + ] { out.extend(entry(tag, ty, 1, value)); } out.extend(0u32.to_le_bytes()); diff --git a/crates/image-codecs/tests/it/tiff_pages.rs b/crates/image-codecs/tests/it/tiff_pages.rs index fbe4ffb8b..0e589786f 100644 --- a/crates/image-codecs/tests/it/tiff_pages.rs +++ b/crates/image-codecs/tests/it/tiff_pages.rs @@ -338,7 +338,8 @@ fn alpha_after_an_unspecified_extra_sample_is_kept() { // The colour samples, then the second extra sample (the alpha). let want: Vec = want_all.chunks(spp * bps).flat_map(|p| [&p[..color * bps], &p[(color + 1) * bps..]].concat()).collect(); for planar in [false, true] { - let chunks = if planar { planar_strips(&file, w, h, spp, bps, 2) } else { file.chunks(w * spp * bps * 2).map(<[u8]>::to_vec).collect() }; + let chunks = + if planar { planar_strips(&file, w, h, spp, bps, 2) } else { file.chunks(w * spp * bps * 2).map(<[u8]>::to_vec).collect() }; let mut d = Dir { entries: base(w as u32, h as u32, bits, spp as u16, photometric), chunks, tiled: false } .tag(278, LONG, &[2]) .tag(339, SHORT, &vec![u64::from(format); spp]) @@ -509,7 +510,14 @@ fn every_ifd_and_subifd_is_listed_and_decodable() { use TiffPageKind::*; assert_eq!( summary, - [(4, None, ReducedResolution), (9, None, Page), (3, Some(1), ReducedResolution), (2, Some(1), ReducedResolution), (9, None, Mask), (5, None, Page)] + [ + (4, None, ReducedResolution), + (9, None, Page), + (3, Some(1), ReducedResolution), + (2, Some(1), ReducedResolution), + (9, None, Mask), + (5, None, Page) + ] ); assert_eq!(info.pages[1].ifd_offset, b.ifd_at[1]); assert_eq!((info.default_page(), info.page_count()), (Some(1), 2)); diff --git a/crates/image-codecs/tests/it/tiff_photoshop.rs b/crates/image-codecs/tests/it/tiff_photoshop.rs index 698a4296d..d413ccc34 100644 --- a/crates/image-codecs/tests/it/tiff_photoshop.rs +++ b/crates/image-codecs/tests/it/tiff_photoshop.rs @@ -8,7 +8,8 @@ use craft_image_codecs::*; fn with_tags(layout: ChannelLayout, sample: SampleType) -> Image { let mut img = synth(9, 7, layout, sample, 5, 0.3); - img.meta.photoshop_resources = Some(b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\x48\x00\x00\x00\x01\x00\x01\x00\x48\x00\x00\x00\x01\x00\x01".to_vec()); + img.meta.photoshop_resources = + Some(b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\x48\x00\x00\x00\x01\x00\x01\x00\x48\x00\x00\x00\x01\x00\x01".to_vec()); let mut layers = b"Adobe Photoshop Document Data Block\0".to_vec(); layers.extend_from_slice(b"MIB8ryaL"); layers.extend_from_slice(&4u32.to_le_bytes()); diff --git a/crates/image-codecs/tests/it/tiff_pyramid.rs b/crates/image-codecs/tests/it/tiff_pyramid.rs index 48c846944..adecd3fc7 100644 --- a/crates/image-codecs/tests/it/tiff_pyramid.rs +++ b/crates/image-codecs/tests/it/tiff_pyramid.rs @@ -51,8 +51,15 @@ fn off_by_default_and_none_for_small_images() { #[test] fn every_depth_layout_and_compression() { - let layouts = - [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba, ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab]; + let layouts = [ + ChannelLayout::Gray, + ChannelLayout::GrayA, + ChannelLayout::Rgb, + ChannelLayout::Rgba, + ChannelLayout::Cmyk, + ChannelLayout::CmykA, + ChannelLayout::Lab, + ]; for layout in layouts { for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { for compression in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { @@ -100,7 +107,10 @@ fn first_level_pixel(layout: ChannelLayout, tile: [&[u8]; 4]) -> Vec { fn colour_is_weighted_by_alpha() { // One opaque pixel and three transparent ones of another colour: the level keeps the opaque // colour at a quarter alpha. Unweighted, the transparent colour would bleed in (pink, not red). - assert_eq!(first_level_pixel(ChannelLayout::Rgba, [&[255, 0, 0, 255], &[255, 255, 255, 0], &[255, 255, 255, 0], &[255, 255, 255, 0]]), [255, 0, 0, 64]); + assert_eq!( + first_level_pixel(ChannelLayout::Rgba, [&[255, 0, 0, 255], &[255, 255, 255, 0], &[255, 255, 255, 0], &[255, 255, 255, 0]]), + [255, 0, 0, 64] + ); // Gray and CMYK reach the same path with other channel counts. assert_eq!(first_level_pixel(ChannelLayout::GrayA, [&[40, 255], &[255, 0], &[255, 0], &[255, 0]]), [40, 64]); assert_eq!(first_level_pixel(ChannelLayout::CmykA, [&[200, 10, 30, 90, 255], &[0; 5], &[0; 5], &[0; 5]]), [200, 10, 30, 90, 64]); diff --git a/crates/image-codecs/tests/it/tiff_ycbcr.rs b/crates/image-codecs/tests/it/tiff_ycbcr.rs index 15cac9410..0f456bf84 100644 --- a/crates/image-codecs/tests/it/tiff_ycbcr.rs +++ b/crates/image-codecs/tests/it/tiff_ycbcr.rs @@ -141,11 +141,10 @@ fn sixteen_bit_ycbcr_bands_match_whole_decode_with_a_missing_last_strip() { let icc = sample_icc(512); for little in [true, false] { for missing in [false, true] { - let dir = strips(w, h, 16, 3, 6, rows, &u16s(&samples, little)).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights).bytes( - ICC_PROFILE, - UNDEFINED, - &icc, - ); + let dir = strips(w, h, 16, 3, 6, rows, &u16s(&samples, little)) + .tag(SUBSAMPLING, SHORT, &[1, 1]) + .tag(YCBCR_COEFFICIENTS, RATIONAL, &weights) + .bytes(ICC_PROFILE, UNDEFINED, &icc); let mut file = build(little, false, &[dir], &[0]); if missing { let past = file.bytes.len() as u64 + 4096; @@ -222,7 +221,8 @@ fn tiled_jpeg_ycbcr_keeps_its_icc_profile() { assert_eq!((img.width(), img.height(), img.layout()), (w, h, ChannelLayout::Rgb)); assert_eq!(img.icc.as_deref(), Some(icc.as_slice()), "the embedded profile is kept"); // The oracle: each tile's stream decoded by an independent decoder, placed side by side. - let tiles: Vec> = streams.iter().map(|s| image::load_from_memory_with_format(s, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw()).collect(); + let tiles: Vec> = + streams.iter().map(|s| image::load_from_memory_with_format(s, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw()).collect(); let row = (t * 3) as usize; let want: Vec = (0..t as usize).flat_map(|y| [&tiles[0][y * row..(y + 1) * row], &tiles[1][y * row..(y + 1) * row]].concat()).collect(); let worst = rgb(&img).iter().zip(&want).map(|(g, o)| g.abs_diff(*o)).max().unwrap(); @@ -248,11 +248,10 @@ fn planar_ycbcr_reads_as_rgb_in_both_byte_orders() { // The pixels of the default-conversion test, one plane per component. let px = [0u8, 128, 128, 255, 128, 128, 128, 128, 128, 76, 85, 255]; for little in [true, false] { - let dir = Dir { entries: base(4, 1, 8, 3, 6), chunks: planar_strips(&px, 4, 1, 3, 1, 1), tiled: false }.tag(278, LONG, &[1]).tag(284, SHORT, &[2]).tag( - SUBSAMPLING, - SHORT, - &[1, 1], - ); + let dir = Dir { entries: base(4, 1, 8, 3, 6), chunks: planar_strips(&px, 4, 1, 3, 1, 1), tiled: false } + .tag(278, LONG, &[1]) + .tag(284, SHORT, &[2]) + .tag(SUBSAMPLING, SHORT, &[1, 1]); let img = decode(&build(little, false, &[dir], &[0]).bytes).unwrap(); assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255, 128, 128, 128, 254, 0, 0], "little endian: {little}"); } diff --git a/crates/image-codecs/tests/it/webp_frames.rs b/crates/image-codecs/tests/it/webp_frames.rs index c099ec981..2c8e54e98 100644 --- a/crates/image-codecs/tests/it/webp_frames.rs +++ b/crates/image-codecs/tests/it/webp_frames.rs @@ -1,8 +1,8 @@ //! `decode_webp_frames`: every frame of an animated WebP, composited, with its duration (#1198). use craft_image_codecs::{ - AnimationFrame, ChannelLayout, CodecError, DecodeOptions, DecodeWarning, EncodeOptions, Format, Image, Limits, decode, decode_webp_frames, encode, - webp_is_animated, + AnimationFrame, ChannelLayout, CodecError, DecodeOptions, DecodeWarning, EncodeOptions, Format, Image, Limits, decode, decode_webp_frames, + encode, webp_is_animated, }; const RED: [u8; 4] = [255, 0, 0, 255]; diff --git a/crates/image-codecs/tests/jxl_export.rs b/crates/image-codecs/tests/jxl_export.rs index 61659f55a..57a91143c 100644 --- a/crates/image-codecs/tests/jxl_export.rs +++ b/crates/image-codecs/tests/jxl_export.rs @@ -23,7 +23,8 @@ fn jpeg_xl_is_export_only_and_detects_both_signatures() { #[test] fn jpeg_xl_encoding_is_deterministic_and_converts_float_to_integer() { let input = - Image::from_u8(3, 2, ChannelLayout::Rgba, vec![0, 255, 17, 127, 20, 40, 80, 255, 255, 0, 2, 0, 10, 20, 30, 64, 0, 0, 0, 1, 90, 80, 70, 128]).unwrap(); + Image::from_u8(3, 2, ChannelLayout::Rgba, vec![0, 255, 17, 127, 20, 40, 80, 255, 255, 0, 2, 0, 10, 20, 30, 64, 0, 0, 0, 1, 90, 80, 70, 128]) + .unwrap(); let opts = EncodeOptions::default(); let one = encode(&input, Format::Jxl, &opts).unwrap(); let two = encode(&input, Format::Jxl, &opts).unwrap(); From 7c3a099ad8fee294ec2d5cba9d2241d099a29f11 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:21:44 +0000 Subject: [PATCH 047/520] Read native format JSON from streams --- crates/stencilcraft-format/src/lib.rs | 188 +++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 6 deletions(-) diff --git a/crates/stencilcraft-format/src/lib.rs b/crates/stencilcraft-format/src/lib.rs index 81881f6a9..3ff499124 100644 --- a/crates/stencilcraft-format/src/lib.rs +++ b/crates/stencilcraft-format/src/lib.rs @@ -18,10 +18,14 @@ //! [`DocumentFields`] owns the JSON field names, required and defaulted fields, and the //! flattening rule. Its values are JSON because the consuming app owns the document model, //! domain validation, and conversion between that model and the wire fields. The crate does not -//! open files or set a file-size policy; callers handle bounded file I/O. +//! open files or set a file-size policy; callers handle bounded file I/O. [`File::read_from`] +//! lets callers parse from a reader without first buffering source bytes in a string. -use serde::{Deserialize, Serialize}; -use serde_json::Value; +use serde::de::{self, IgnoredAny, MapAccess, SeqAccess, Visitor}; +use serde::{Deserialize, Deserializer, Serialize}; +use serde_json::{Map, Value}; +use std::fmt; +use std::io::Read; /// The extension, without the leading dot. pub const EXTENSION: &str = "stencilcraft"; @@ -90,8 +94,21 @@ impl File { /// Parses JSON, validates the format header and version, then reads the wire fields. pub fn read(text: &str) -> Result { - let value: Value = serde_json::from_str(text).map_err(Error::Json)?; - let header = value.get(HEADER_KEY).and_then(Value::as_object).ok_or(Error::NotStencilCraft)?; + let wire: RawWire = serde_json::from_str(text).map_err(Error::Json)?; + Self::from_wire(wire) + } + + /// Parses JSON from a reader, without first buffering the source bytes in a string. + /// + /// Parsed document fields are still materialized as JSON values. Callers that impose a + /// total file-size policy should enforce it on the reader before calling this method. + pub fn read_from(reader: impl Read) -> Result { + let wire: RawWire = serde_json::from_reader(reader).map_err(Error::Json)?; + Self::from_wire(wire) + } + + fn from_wire(wire: RawWire) -> Result { + let header = wire.fields.get(HEADER_KEY).and_then(Value::as_object).ok_or(Error::NotStencilCraft)?; if header.get("name").and_then(Value::as_str) != Some(FORMAT_NAME) { return Err(Error::NotStencilCraft); } @@ -101,7 +118,7 @@ impl File { if version != FORMAT_VERSION { return Err(Error::Version { found: version, supported: FORMAT_VERSION }); } - let raw: RawFile = serde_json::from_value(value).map_err(|source| Error::Document { source })?; + let raw: RawFile = serde_json::from_value(Value::Object(wire.fields)).map_err(|source| Error::Document { source })?; Ok(Self { document: raw.document }) } @@ -127,6 +144,132 @@ struct RawFile { document: DocumentFields, } +/// A top-level streaming visitor that stores only fields owned by this wire schema. +/// +/// Values are kept as `Value` because consumers own their document models. Unknown fields are +/// skipped, duplicate fields retain serde_json's last-value-wins behavior, and header checks run +/// only after the full JSON input has been parsed. +struct RawWire { + fields: Map, +} + +impl<'de> Deserialize<'de> for RawWire { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct RawWireVisitor; + + impl<'de> Visitor<'de> for RawWireVisitor { + type Value = RawWire; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a JSON value containing a StencilCraft document") + } + + fn visit_map(self, mut access: A) -> Result + where + A: MapAccess<'de>, + { + let mut fields = Map::new(); + while let Some(key) = access.next_key::()? { + if is_wire_field(&key) { + fields.insert(key, access.next_value()?); + } else { + let _ = access.next_value::()?; + } + } + Ok(RawWire { fields }) + } + + fn visit_seq(self, mut access: A) -> Result + where + A: SeqAccess<'de>, + { + while access.next_element::()?.is_some() {} + Ok(RawWire { fields: Map::new() }) + } + + fn visit_bool(self, _value: bool) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + + fn visit_i64(self, _value: i64) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + + fn visit_u64(self, _value: u64) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + + fn visit_f64(self, _value: f64) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + + fn visit_str(self, _value: &str) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + + fn visit_string(self, _value: String) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + + fn visit_unit(self) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + + fn visit_none(self) -> Result + where + E: de::Error, + { + Ok(RawWire { fields: Map::new() }) + } + } + + deserializer.deserialize_any(RawWireVisitor) + } +} + +fn is_wire_field(name: &str) -> bool { + matches!( + name, + HEADER_KEY + | "board" + | "kerf" + | "layers" + | "next-shape-id" + | "images" + | "board-setup" + | "pages" + | "guides" + | "grid" + | "snap" + | "groups" + | "meta" + ) +} + #[derive(Serialize)] struct HeaderRef<'a> { name: &'a str, @@ -161,6 +304,8 @@ pub enum Error { #[cfg(test)] mod tests { + use std::io::{self, Cursor, Read}; + use serde_json::{Value, json}; use super::{DocumentFields, Error, FORMAT_VERSION, File}; @@ -222,6 +367,37 @@ mod tests { assert!(matches!(File::read(r#"{"stencilcraft":{"name":"stencilcraft","version":2}}"#), Err(Error::Version { found: 2, supported: 1 }))); } + #[test] + fn reader_preserves_duplicate_key_and_error_precedence_behavior() -> Result<(), Error> { + let duplicate = r#"{"stencilcraft":{"name":"wrong","version":1},"stencilcraft":{"name":"stencilcraft","version":1},"board":{"first":true},"board":{"width_mm":100,"height_mm":150},"kerf":{},"layers":[]}"#; + let file = File::read_from(Cursor::new(duplicate))?; + assert_eq!(file.document.board, json!({"width_mm":100,"height_mm":150})); + + let malformed_after_bad_header = Cursor::new(br#"{"stencilcraft":{"name":"other","version":1},"board": "#); + assert!(matches!(File::read_from(malformed_after_bad_header), Err(Error::Json(_)))); + assert!(matches!(File::read_from(Cursor::new(b"[]")), Err(Error::NotStencilCraft))); + Ok(()) + } + + #[test] + fn reader_accepts_large_input_in_short_reads() -> Result<(), Error> { + struct ShortReads(R); + impl Read for ShortReads { + fn read(&mut self, buffer: &mut [u8]) -> io::Result { + let length = buffer.len().min(1024); + self.0.read(&mut buffer[..length]) + } + } + + let text = format!( + r#"{{"stencilcraft":{{"name":"stencilcraft","version":1}},"board":{{}},"kerf":{{}},"layers":[],"future-field":"{}"}}"#, + "x".repeat(8 * 1024 * 1024) + ); + let file = File::read_from(ShortReads(Cursor::new(text.as_bytes())))?; + assert_eq!(file.document.layers, json!([])); + Ok(()) + } + #[test] fn separates_json_errors_from_wire_schema_errors() { assert!(matches!(File::read("{"), Err(Error::Json(_)))); From 79c2003ac9c665bfd6a0dbc00c256d42cc3ad1e3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:29:48 -0500 Subject: [PATCH 048/520] Add shared file format detection --- Cargo.lock | 7 +++ Cargo.toml | 1 + crates/file-formats/Cargo.toml | 14 +++++ crates/file-formats/src/lib.rs | 93 ++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 crates/file-formats/Cargo.toml create mode 100644 crates/file-formats/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index cf0e0bd47..5e004a7ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1454,6 +1454,13 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-file-formats" +version = "0.1.0" +dependencies = [ + "craft-eps", +] + [[package]] name = "craft-flags" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 629bab64c..4af6f586a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ craft-docx = { path = "crates/docx" } craft-dxf = { path = "crates/dxf" } craft-drawingml = { path = "crates/drawingml" } craft-eps = { path = "crates/eps" } +craft-file-formats = { path = "crates/file-formats" } craft-doc = { path = "crates/doc" } craft-docpath = { path = "crates/docpath" } craft-dpx = { path = "crates/dpx" } diff --git a/crates/file-formats/Cargo.toml b/crates/file-formats/Cargo.toml new file mode 100644 index 000000000..0a5be5e76 --- /dev/null +++ b/crates/file-formats/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "craft-file-formats" +description = "Shared bounded file signature and extension resolution for Craft applications" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true + +[dependencies] +craft-eps = { workspace = true } + +[lints] +workspace = true diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs new file mode 100644 index 000000000..b51fc399c --- /dev/null +++ b/crates/file-formats/src/lib.rs @@ -0,0 +1,93 @@ +//! Bounded file kind detection and extension resolution shared by Craft applications. +//! +//! This crate identifies a format; it does not parse, validate, or decode the file. +//! Callers remain responsible for choosing which identified formats their product supports. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +/// A supported file family, independent of the application model that consumes it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Format { + Pdf, + Eps, + PostScript, + Illustrator, + Dxf, + Svg, + Raster, + Studio, + StencilCraft, +} + +/// Resolve a name and file bytes to a format. Strong signatures take precedence over +/// extension hints. Only format-specific bounded probes and extension tables live here. +pub fn detect(name: &str, bytes: &[u8]) -> Option { + if let Some(found) = craft_eps::sniff(bytes) { + return Some(match (found.kind, found.illustrator) { + (_, true) => Format::Illustrator, + (craft_eps::Kind::Pdf, _) => Format::Pdf, + (craft_eps::Kind::Eps, _) => Format::Eps, + (craft_eps::Kind::PostScript, _) => Format::PostScript, + }); + } + if raster_signature(bytes) { + return Some(Format::Raster); + } + if bytes.starts_with(b"silhouette04;") || bytes.starts_with(b"silhouette05;") { + return Some(Format::Studio); + } + if let Some(ext) = extension(name) { + if let Some(format) = from_extension(ext) { + return Some(format); + } + } + let head = bytes.get(..4096).unwrap_or(bytes); + let xml = head.trim_ascii_start(); + if xml.starts_with(b" Option { + match ext.trim_start_matches('.').to_ascii_lowercase().as_str() { + "pdf" => Some(Format::Pdf), + "eps" => Some(Format::Eps), + "ps" => Some(Format::PostScript), + "ai" => Some(Format::Illustrator), + "dxf" => Some(Format::Dxf), + "svg" | "svgz" => Some(Format::Svg), + "png" | "jpg" | "jpeg" | "bmp" | "gif" | "tif" | "tiff" | "webp" | "heic" | "heif" => Some(Format::Raster), + "studio" | "studio3" => Some(Format::Studio), + "stencilcraft" => Some(Format::StencilCraft), + _ => None, + } +} + +fn extension(name: &str) -> Option<&str> { + Some(name.rsplit(['/', '\\']).next()?.rsplit_once('.')?.1) +} + +fn dxf_ascii_prefix(bytes: &[u8]) -> bool { + let text = String::from_utf8_lossy(bytes); + let mut lines = text.lines().map(str::trim); + matches!((lines.next(), lines.next(), lines.next(), lines.next()), (Some("0"), Some("SECTION"), Some("2"), Some(section)) if matches!(section, "HEADER" | "CLASSES" | "TABLES" | "BLOCKS" | "ENTITIES" | "OBJECTS")) +} + +fn raster_signature(bytes: &[u8]) -> bool { + let head = bytes.get(..16).unwrap_or(bytes); + head.starts_with(b"\x89PNG\r\n\x1a\n") + || head.starts_with(b"\xff\xd8\xff") + || head.starts_with(b"BM") + || head.starts_with(b"GIF87a") + || head.starts_with(b"GIF89a") + || head.starts_with(b"II*\0") + || head.starts_with(b"MM\0*") + || head.starts_with(b"II+\0") + || head.starts_with(b"MM\0+") + || (head.starts_with(b"RIFF") && head.get(8..12) == Some(b"WEBP")) +} From 8d3c753b5e7422d4484f5550b6e84bc93f2ab280 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:34:11 -0500 Subject: [PATCH 049/520] Recognize G-code filename extensions --- crates/file-formats/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index b51fc399c..c5a2164de 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -15,6 +15,7 @@ pub enum Format { Dxf, Svg, Raster, + Gcode, Studio, StencilCraft, } @@ -64,6 +65,7 @@ pub fn from_extension(ext: &str) -> Option { "png" | "jpg" | "jpeg" | "bmp" | "gif" | "tif" | "tiff" | "webp" | "heic" | "heif" => Some(Format::Raster), "studio" | "studio3" => Some(Format::Studio), "stencilcraft" => Some(Format::StencilCraft), + "gcode" | "nc" => Some(Format::Gcode), _ => None, } } From c1a1b976d0decf496d132bf71eecaec0de198675 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:34:48 -0500 Subject: [PATCH 050/520] Resolve raster subformats centrally --- crates/file-formats/src/lib.rs | 51 ++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index c5a2164de..29bf92a3f 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -14,7 +14,13 @@ pub enum Format { Illustrator, Dxf, Svg, - Raster, + Png, + Jpeg, + Bmp, + Gif, + Tiff, + WebP, + Heif, Gcode, Studio, StencilCraft, @@ -31,8 +37,8 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option { (craft_eps::Kind::PostScript, _) => Format::PostScript, }); } - if raster_signature(bytes) { - return Some(Format::Raster); + if let Some(raster) = raster_signature(bytes) { + return Some(raster); } if bytes.starts_with(b"silhouette04;") || bytes.starts_with(b"silhouette05;") { return Some(Format::Studio); @@ -62,7 +68,13 @@ pub fn from_extension(ext: &str) -> Option { "ai" => Some(Format::Illustrator), "dxf" => Some(Format::Dxf), "svg" | "svgz" => Some(Format::Svg), - "png" | "jpg" | "jpeg" | "bmp" | "gif" | "tif" | "tiff" | "webp" | "heic" | "heif" => Some(Format::Raster), + "png" => Some(Format::Png), + "jpg" | "jpeg" => Some(Format::Jpeg), + "bmp" => Some(Format::Bmp), + "gif" => Some(Format::Gif), + "tif" | "tiff" => Some(Format::Tiff), + "webp" => Some(Format::WebP), + "heic" | "heif" => Some(Format::Heif), "studio" | "studio3" => Some(Format::Studio), "stencilcraft" => Some(Format::StencilCraft), "gcode" | "nc" => Some(Format::Gcode), @@ -80,16 +92,25 @@ fn dxf_ascii_prefix(bytes: &[u8]) -> bool { matches!((lines.next(), lines.next(), lines.next(), lines.next()), (Some("0"), Some("SECTION"), Some("2"), Some(section)) if matches!(section, "HEADER" | "CLASSES" | "TABLES" | "BLOCKS" | "ENTITIES" | "OBJECTS")) } -fn raster_signature(bytes: &[u8]) -> bool { +fn raster_signature(bytes: &[u8]) -> Option { let head = bytes.get(..16).unwrap_or(bytes); - head.starts_with(b"\x89PNG\r\n\x1a\n") - || head.starts_with(b"\xff\xd8\xff") - || head.starts_with(b"BM") - || head.starts_with(b"GIF87a") - || head.starts_with(b"GIF89a") - || head.starts_with(b"II*\0") - || head.starts_with(b"MM\0*") - || head.starts_with(b"II+\0") - || head.starts_with(b"MM\0+") - || (head.starts_with(b"RIFF") && head.get(8..12) == Some(b"WEBP")) + if head.starts_with(b"\x89PNG\r\n\x1a\n") { + return Some(Format::Png); + } + if head.starts_with(b"\xff\xd8\xff") { + return Some(Format::Jpeg); + } + if head.starts_with(b"BM") { + return Some(Format::Bmp); + } + if head.starts_with(b"GIF87a") || head.starts_with(b"GIF89a") { + return Some(Format::Gif); + } + if head.starts_with(b"II*\0") || head.starts_with(b"MM\0*") || head.starts_with(b"II+\0") || head.starts_with(b"MM\0+") { + return Some(Format::Tiff); + } + if head.starts_with(b"RIFF") && head.get(8..12) == Some(b"WEBP") { + return Some(Format::WebP); + } + None } From ba24fe2435adb3c81ad6057faf36365fbba736cf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:39:37 -0500 Subject: [PATCH 051/520] Move bounded EPS rectangle parsing to craft-eps --- crates/eps/src/lib.rs | 2 + crates/eps/src/rectangles.rs | 136 +++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 crates/eps/src/rectangles.rs diff --git a/crates/eps/src/lib.rs b/crates/eps/src/lib.rs index acffdfcf2..e48532349 100644 --- a/crates/eps/src/lib.rs +++ b/crates/eps/src/lib.rs @@ -19,10 +19,12 @@ pub mod ascii85; mod container; mod dsc; mod preview; +mod rectangles; pub use container::{DOS_HEADER, DOS_MAGIC, Sections, sections, wrap}; pub use dsc::{Comments, MAX_HEADER, MAX_TRAILER}; pub use preview::{Bitmap, MAX_EPSI, epsi}; +pub use rectangles::{MAX_SIMPLE_RECTANGLE_SAVE_DEPTH, MAX_SIMPLE_RECTANGLE_TOKENS, MAX_SIMPLE_RECTANGLES, Rectangle, simple_rectangles}; use std::fmt; diff --git a/crates/eps/src/rectangles.rs b/crates/eps/src/rectangles.rs new file mode 100644 index 000000000..d1c871762 --- /dev/null +++ b/crates/eps/src/rectangles.rs @@ -0,0 +1,136 @@ +//! A bounded subset of PostScript used for simple EPS previews. + +use std::collections::HashMap; + +/// A solid rectangle drawn by a simple EPS program. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Rectangle { + /// Left edge in PostScript points. + pub x: f64, + /// Bottom edge in PostScript points. + pub y: f64, + /// Width in PostScript points. + pub width: f64, + /// Height in PostScript points. + pub height: f64, + /// Straight sRGB RGBA. + pub color: [u8; 4], +} + +pub const MAX_SIMPLE_RECTANGLES: usize = 200_000; +pub const MAX_SIMPLE_RECTANGLE_TOKENS: usize = 2_000_000; +pub const MAX_SIMPLE_RECTANGLE_SAVE_DEPTH: usize = 64; + +/// Simple PostScript tokens without comments, with braces kept as individual tokens. +fn tokens(text: &str) -> impl Iterator { + text.split(['\n', '\r']).flat_map(|line| { + line.split('%').next().unwrap_or("").split_whitespace().flat_map(|word| word.split_inclusive(['{', '}'])).flat_map(|piece| { + let (body, brace) = match piece.char_indices().last() { + Some((at, '{' | '}')) => (piece.get(..at).unwrap_or(""), piece.get(at..)), + _ => (piece, None), + }; + [Some(body).filter(|body| !body.is_empty()), brace].into_iter().flatten() + }) + }) +} + +/// Read solid rectangle fills and their sRGB colours from the restricted PostScript subset used +/// by simple EPS previews. None means the program uses an unsupported operator or exceeds a +/// fixed work bound. The function never executes PostScript. +pub fn simple_rectangles(ps: &[u8]) -> Option> { + let text = std::str::from_utf8(ps).ok()?; + let mut aliases: HashMap = HashMap::new(); + let mut stack: Vec = Vec::new(); + let mut color = [0u8, 0, 0, 255]; + let mut saved: Vec<[u8; 4]> = Vec::new(); + let mut rectangles = Vec::new(); + let mut input = tokens(text); + let mut count = 0usize; + while let Some(token) = input.next() { + count += 1; + if count > MAX_SIMPLE_RECTANGLE_TOKENS { + return None; + } + if let Ok(value) = token.parse::() { + if !value.is_finite() { + return None; + } + stack.push(value); + continue; + } + if let Some(name) = token.strip_prefix('/') { + let mut body: Vec<&str> = Vec::new(); + while body.len() < 5 { + count += 1; + if count > MAX_SIMPLE_RECTANGLE_TOKENS { + return None; + } + match input.next() { + Some("def") | None => break, + Some(token) => body.push(token), + } + } + let op = match body.as_slice() { + ["{", op, "}"] | ["{", op, "}", _] => (*op).to_string(), + _ => return None, + }; + aliases.insert(name.to_string(), op); + continue; + } + let op = aliases.get(token).map_or(token, String::as_str); + let mut pop = |n: usize| -> Option> { (stack.len() >= n).then(|| stack.split_off(stack.len() - n)) }; + match op { + "setcmykcolor" => { + let values = pop(4)?; + let black = values[3].clamp(0.0, 1.0); + let channel = |value: f64| ((1.0 - value.clamp(0.0, 1.0)) * (1.0 - black) * 255.0).round() as u8; + color = [channel(values[0]), channel(values[1]), channel(values[2]), 255]; + } + "setrgbcolor" => { + let values = pop(3)?; + let channel = |value: f64| (value.clamp(0.0, 1.0) * 255.0).round() as u8; + color = [channel(values[0]), channel(values[1]), channel(values[2]), 255]; + } + "setgray" => { + let values = pop(1)?; + let gray = (values[0].clamp(0.0, 1.0) * 255.0).round() as u8; + color = [gray, gray, gray, 255]; + } + "rectfill" => { + let values = pop(4)?; + if rectangles.len() >= MAX_SIMPLE_RECTANGLES { + return None; + } + let x1 = values[0] + values[2]; + let y1 = values[1] + values[3]; + if !x1.is_finite() || !y1.is_finite() { + return None; + } + rectangles.push(Rectangle { + x: values[0].min(x1), + y: values[1].min(y1), + width: (x1 - values[0]).abs(), + height: (y1 - values[1]).abs(), + color, + }); + } + "gsave" | "save" => { + if saved.len() >= MAX_SIMPLE_RECTANGLE_SAVE_DEPTH { + return None; + } + saved.push(color); + } + "grestore" | "restore" => { + if let Some(previous) = saved.pop() { + color = previous; + } + } + "pop" => { + pop(1)?; + } + "bind" | "newpath" | "showpage" | "end" | "begin" => {} + _ => return None, + } + } + (!rectangles.is_empty()).then_some(rectangles) +} From d8cc88a7720755e0054038ebc0e152b2e0dcfa00 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:39:31 -0500 Subject: [PATCH 052/520] Add image document formats to shared registry --- crates/file-formats/src/lib.rs | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 29bf92a3f..9d3a3c438 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -21,6 +21,14 @@ pub enum Format { Tiff, WebP, Heif, + Psd, + Affinity, + Ora, + Kra, + Xcf, + Pdn, + Raw, + Pcraft, Gcode, Studio, StencilCraft, @@ -37,6 +45,18 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option { (craft_eps::Kind::PostScript, _) => Format::PostScript, }); } + if bytes.starts_with(b"8BPS") { + return Some(Format::Psd); + } + if bytes.starts_with(b"PDN3") { + return Some(Format::Pdn); + } + if bytes.starts_with(b"gimp xcf ") || gzipped_xcf(name, bytes) { + return Some(Format::Xcf); + } + if bytes.starts_with(b"\0\xffKA") { + return Some(Format::Affinity); + } if let Some(raster) = raster_signature(bytes) { return Some(raster); } @@ -75,6 +95,16 @@ pub fn from_extension(ext: &str) -> Option { "tif" | "tiff" => Some(Format::Tiff), "webp" => Some(Format::WebP), "heic" | "heif" => Some(Format::Heif), + "psd" | "psb" => Some(Format::Psd), + "afdesign" | "afphoto" | "afpub" => Some(Format::Affinity), + "ora" => Some(Format::Ora), + "kra" => Some(Format::Kra), + "xcf" => Some(Format::Xcf), + "pdn" => Some(Format::Pdn), + "dng" | "3fr" | "ari" | "arw" | "bay" | "cap" | "cr2" | "cr3" | "dcs" | "dcr" | "drf" | "eip" | "erf" | "fff" | "gpr" | "iiq" | "k25" + | "kdc" | "mdc" | "mef" | "mos" | "mrw" | "nef" | "nrw" | "obm" | "orf" | "pef" | "ptx" | "pxn" | "r3d" | "raf" | "raw" | "rwl" | "rw2" + | "rwz" | "sr2" | "srf" | "sti" | "x3f" => Some(Format::Raw), + "pcraft" => Some(Format::Pcraft), "studio" | "studio3" => Some(Format::Studio), "stencilcraft" => Some(Format::StencilCraft), "gcode" | "nc" => Some(Format::Gcode), @@ -86,6 +116,11 @@ fn extension(name: &str) -> Option<&str> { Some(name.rsplit(['/', '\\']).next()?.rsplit_once('.')?.1) } +fn gzipped_xcf(name: &str, bytes: &[u8]) -> bool { + let lower = name.rsplit(['/', '\\']).next().unwrap_or(name).to_ascii_lowercase(); + bytes.starts_with(&[0x1f, 0x8b]) && (lower.ends_with(".xcf.gz") || lower.ends_with(".xcfgz")) +} + fn dxf_ascii_prefix(bytes: &[u8]) -> bool { let text = String::from_utf8_lossy(bytes); let mut lines = text.lines().map(str::trim); From 8bac629c0fe569a5c2f5e79e62f09dbf4020805b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:46:33 -0500 Subject: [PATCH 053/520] Preserve CFA anchor in shared raw metadata --- crates/raw/src/arw.rs | 4 +++- crates/raw/src/dng.rs | 8 ++++++-- crates/raw/src/erf.rs | 4 +++- crates/raw/src/kodak.rs | 4 +++- crates/raw/src/lib.rs | 19 ++++++++++++++----- crates/raw/src/mef.rs | 4 +++- crates/raw/src/nef.rs | 4 +++- crates/raw/src/pef.rs | 4 +++- crates/raw/src/raf.rs | 4 +++- crates/raw/src/srw.rs | 4 +++- 10 files changed, 44 insertions(+), 15 deletions(-) diff --git a/crates/raw/src/arw.rs b/crates/raw/src/arw.rs index e95d9a0bc..639fc0032 100644 --- a/crates/raw/src/arw.rs +++ b/crates/raw/src/arw.rs @@ -721,7 +721,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result= (w * h * 2) as u64 { 14 } else { out_bits }; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => Cfa::bayer_static("RGGB"), }; let default_black = if scale_bits >= 14 { 512.0 } else { 128.0 }; diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index 04b0b8e66..87f23f81f 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -410,9 +410,13 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f // sensor origin (the crop to the active area shifts it back), so move the anchor out by the area's offset // (DNGs that LightCraft wrote before its writer followed the spec carry the sensor-origin pattern; they are marked // by a bare "LightCraft" Software tag and keep their original reading) - let at_origin = Cfa { width: cols, height: rows, pattern }; + let mut at_origin = Cfa { width: cols, height: rows, pattern, origin_x: 0, origin_y: 0 }; let legacy_writer = [raw, ifd0].iter().any(|i| i.string(t::SOFTWARE).as_deref().map(str::trim) == Some("LightCraft")); - Some(if legacy_writer { at_origin } else { at_origin.shifted(cols - active_area.x % cols, rows - active_area.y % rows) }) + if !legacy_writer { + at_origin.origin_x = active_area.x; + at_origin.origin_y = active_area.y; + } + Some(at_origin) } else { None }; diff --git a/crates/raw/src/erf.rs b/crates/raw/src/erf.rs index 1779f19ad..40190ca20 100644 --- a/crates/raw/src/erf.rs +++ b/crates/raw/src/erf.rs @@ -78,7 +78,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Erf raw without a 2x2 CFAPattern".into())), }; let mn = maker_note(bytes, &tiff); diff --git a/crates/raw/src/kodak.rs b/crates/raw/src/kodak.rs index 40e186d6c..bd517225c 100644 --- a/crates/raw/src/kodak.rs +++ b/crates/raw/src/kodak.rs @@ -68,7 +68,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Kodak raw without a 2x2 CFAPattern".into())), }; let curve: Vec = raw diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 016185675..9ba94db04 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -785,6 +785,11 @@ pub struct Cfa { pub height: usize, /// `width × height` colour indices, row-major, anchored at image pixel (0, 0). pub pattern: Vec, + /// The sensor-space coordinate where this periodic pattern is anchored. + #[serde(default)] + pub origin_x: usize, + #[serde(default)] + pub origin_y: usize, } impl Cfa { @@ -801,13 +806,13 @@ impl Cfa { }) }) .collect::>()?; - (p.len() == 4).then_some(Cfa { width: 2, height: 2, pattern: p }) + (p.len() == 4).then_some(Cfa { width: 2, height: 2, pattern: p, origin_x: 0, origin_y: 0 }) } /// A 2×2 Bayer layout named in code (`"RGGB"`, `"BGGR"`, `"GRBG"`, `"GBRG"`). Only for literal /// names (tested below); anything else falls back to RGGB instead of failing. #[cfg_attr(not(feature = "crw"), allow(dead_code))] pub(crate) fn bayer_static(s: &'static str) -> Cfa { - Cfa::bayer(s).unwrap_or(Cfa { width: 2, height: 2, pattern: vec![0, 1, 1, 2] }) + Cfa::bayer(s).unwrap_or(Cfa { width: 2, height: 2, pattern: vec![0, 1, 1, 2], origin_x: 0, origin_y: 0 }) } /// The Fujifilm X-Trans 6×6 layout (as commonly documented), anchored at (0, 0). pub fn xtrans() -> Cfa { @@ -822,16 +827,20 @@ impl Cfa { }) }) .collect(); - Cfa { width: 6, height: 6, pattern } + Cfa { width: 6, height: 6, pattern, origin_x: 0, origin_y: 0 } } #[inline] pub fn color_at(&self, x: usize, y: usize) -> u8 { - self.pattern[(y % self.height) * self.width + (x % self.width)] + let relative = |v: usize, origin: usize, period: usize| { + let (v, origin) = (v % period, origin % period); + if v >= origin { v - origin } else { period - (origin - v) } + }; + self.pattern[relative(y, self.origin_y, self.height) * self.width + relative(x, self.origin_x, self.width)] } /// The same pattern anchored at `(dx, dy)` of the current anchor (e.g. after cropping to an active area). pub fn shifted(&self, dx: usize, dy: usize) -> Cfa { let pattern = (0..self.height).flat_map(|y| (0..self.width).map(move |x| (x, y))).map(|(x, y)| self.color_at(x + dx, y + dy)).collect(); - Cfa { width: self.width, height: self.height, pattern } + Cfa { width: self.width, height: self.height, pattern, origin_x: 0, origin_y: 0 } } pub fn is_bayer(&self) -> bool { if self.width != 2 || self.height != 2 { diff --git a/crates/raw/src/mef.rs b/crates/raw/src/mef.rs index 90849a7bd..5cd1fb705 100644 --- a/crates/raw/src/mef.rs +++ b/crates/raw/src/mef.rs @@ -70,7 +70,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Mef raw without a 2x2 CFAPattern".into())), }; let preview = rgb_preview(&tiff, bytes); diff --git a/crates/raw/src/nef.rs b/crates/raw/src/nef.rs index d378d5cbe..bd880ebb8 100644 --- a/crates/raw/src/nef.rs +++ b/crates/raw/src/nef.rs @@ -281,7 +281,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result= 2 && v[0] > 0.1 && v[1] > 0.1 && v[0] < 10.0 && v[1] < 10.0) .map(|v| [v[0] as f32, 1.0, v[1] as f32]); let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => Cfa::bayer_static("RGGB"), }; let model = ifd0.string(t::MODEL).unwrap_or_default(); diff --git a/crates/raw/src/pef.rs b/crates/raw/src/pef.rs index d5f14bd9a..a33750db5 100644 --- a/crates/raw/src/pef.rs +++ b/crates/raw/src/pef.rs @@ -195,7 +195,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result dark_trimmed(samples, w, hgt, black_point.as_deref().map(|v| v.iter().sum::() / 4.0), white), }; let cfa = match (ifd0.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), ifd0.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } // Every body states its layout in the Exif `CFAPattern`, read from the first sample of the stored array, not // of the image area: the *ist D (origin 19, 11) and the 645D (80, 59) only fit unshifted. The 12-bit 10 MP // bodies and everything from the K-3 on say RGGB; the K-7 / K-5 / 645D generation says BGGR. Files without the diff --git a/crates/raw/src/raf.rs b/crates/raw/src/raf.rs index b92f7568f..f75fa160b 100644 --- a/crates/raw/src/raf.rs +++ b/crates/raw/src/raf.rs @@ -125,7 +125,9 @@ fn camera_name(b: &[u8]) -> String { /// The CFA: X-Trans from the (reversed) layout record, else RGGB. fn cfa(h: &Header) -> Cfa { match record(h, XTRANS_LAYOUT) { - Some(l) if l.len() == 36 && l.iter().all(|&c| c <= 2) => Cfa { width: 6, height: 6, pattern: l.iter().rev().copied().collect() }, + Some(l) if l.len() == 36 && l.iter().all(|&c| c <= 2) => { + Cfa { width: 6, height: 6, pattern: l.iter().rev().copied().collect(), origin_x: 0, origin_y: 0 } + } _ => Cfa::bayer_static("RGGB"), } } diff --git a/crates/raw/src/srw.rs b/crates/raw/src/srw.rs index 3946cbf79..c78ef1f6f 100644 --- a/crates/raw/src/srw.rs +++ b/crates/raw/src/srw.rs @@ -134,7 +134,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Samsung raw without a 2x2 CFAPattern".into())), }; // saturation levels and black levels (R, G1, G2, B), in the file's own words From eb379cf3d211cbe9f01d5e0688cf4f853bdf2c28 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:51:08 +0000 Subject: [PATCH 054/520] test(stencilcraft-format): cover scalar JSON roots --- crates/stencilcraft-format/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/stencilcraft-format/src/lib.rs b/crates/stencilcraft-format/src/lib.rs index 3ff499124..1395e1b8a 100644 --- a/crates/stencilcraft-format/src/lib.rs +++ b/crates/stencilcraft-format/src/lib.rs @@ -362,6 +362,13 @@ mod tests { } } + #[test] + fn scalar_roots_are_not_documents() { + for text in ["false", "-1", "1", "1.5", "\"scalar\"", "null"] { + assert!(matches!(File::read(text), Err(Error::NotStencilCraft))); + } + } + #[test] fn refuses_versions_other_than_the_current_version() { assert!(matches!(File::read(r#"{"stencilcraft":{"name":"stencilcraft","version":2}}"#), Err(Error::Version { found: 2, supported: 1 }))); From 4b8dde4c1258848d15a0def44c7901d7e966a240 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:52:28 +0000 Subject: [PATCH 055/520] test(stencilcraft-format): exercise reader scalar root variants --- crates/stencilcraft-format/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/stencilcraft-format/src/lib.rs b/crates/stencilcraft-format/src/lib.rs index 1395e1b8a..709bf7ac7 100644 --- a/crates/stencilcraft-format/src/lib.rs +++ b/crates/stencilcraft-format/src/lib.rs @@ -367,6 +367,7 @@ mod tests { for text in ["false", "-1", "1", "1.5", "\"scalar\"", "null"] { assert!(matches!(File::read(text), Err(Error::NotStencilCraft))); } + assert!(matches!(File::read_from(Cursor::new(br#""scalar""#)), Err(Error::NotStencilCraft))); } #[test] From 85145d8dfd52966eacd045683a86a3737883aeea Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:58:35 -0500 Subject: [PATCH 056/520] Expose shared raster source and tone inspection --- crates/image-codecs/src/codecs/via_image.rs | 2 +- crates/image-codecs/src/inspection.rs | 196 ++++++++++++++++++++ crates/image-codecs/src/lib.rs | 2 + 3 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 crates/image-codecs/src/inspection.rs diff --git a/crates/image-codecs/src/codecs/via_image.rs b/crates/image-codecs/src/codecs/via_image.rs index 02efc0287..9e5e47b85 100644 --- a/crates/image-codecs/src/codecs/via_image.rs +++ b/crates/image-codecs/src/codecs/via_image.rs @@ -83,7 +83,7 @@ pub(crate) fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palett } /// The `image` decoder for `f` with our limits, its header read and its size checked. -fn open<'a>(f: Format, bytes: &'a [u8], limits: &Limits) -> Result { +pub(crate) fn open<'a>(f: Format, bytes: &'a [u8], limits: &Limits) -> Result { let fmt = image_format(f).ok_or_else(|| CodecError::unsupported(f, "not handled by image backend"))?; let mut reader = image::ImageReader::with_format(Cursor::new(bytes), fmt); let mut il = image::Limits::default(); diff --git a/crates/image-codecs/src/inspection.rs b/crates/image-codecs/src/inspection.rs new file mode 100644 index 000000000..922cc5ce1 --- /dev/null +++ b/crates/image-codecs/src/inspection.rs @@ -0,0 +1,196 @@ +//! Bounded source-header and tone inspection for raster documents. + +use crate::error::CodecError; +use crate::{ChannelLayout, DecodeOptions, Format, Limits, SampleType, decode_with}; + +/// The source pixel model reported by an image container, before decode normalizes it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RasterColorType { + Bitmap, + Gray, + GrayAlpha, + Indexed, + Rgb, + Rgba, + Cmyk, + CmykAlpha, + Lab, + Other, +} + +/// Useful source facts, including palette entries when an indexed container exposes them. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RasterInspection { + pub width: u32, + pub height: u32, + pub channels: u8, + pub bits_per_sample: u8, + pub color_type: RasterColorType, + pub palette: Option>, + pub transparent_index: Option, +} + +/// A grayscale source suitable for single-swatch tinting. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ToneClass { + Bitmap, + Gray, +} + +/// Inspect PSD, PNG, JPEG, TIFF, GIF and BMP source headers without expanding pixel data. +pub fn inspect_raster(bytes: &[u8]) -> Result, CodecError> { + if bytes.starts_with(b"8BPS") { + if bytes.len() < 26 || !matches!(u16::from_be_bytes([bytes[4], bytes[5]]), 1 | 2) { + return Ok(None); + } + let u16_at = |i| u16::from_be_bytes([bytes[i], bytes[i + 1]]); + let height = u32::from_be_bytes(bytes[14..18].try_into().map_err(|_| CodecError::InvalidImage("short PSD header".into()))?); + let width = u32::from_be_bytes(bytes[18..22].try_into().map_err(|_| CodecError::InvalidImage("short PSD header".into()))?); + let color_type = match u16_at(24) { + 0 => RasterColorType::Bitmap, + 1 => RasterColorType::Gray, + 2 => RasterColorType::Indexed, + 3 => RasterColorType::Rgb, + 4 => RasterColorType::Cmyk, + 9 => RasterColorType::Lab, + _ => RasterColorType::Other, + }; + return Ok(Some(RasterInspection { + width, + height, + channels: u16_at(12).min(255) as u8, + bits_per_sample: u16_at(22).min(255) as u8, + color_type, + palette: None, + transparent_index: None, + })); + } + let Some(format) = crate::detect(bytes) else { return Ok(None) }; + let i = match format { + Format::Png => { + let rows = crate::PngDecodeRows::new(bytes, &Limits::default())?; + let h = rows.header(); + let depth = *bytes.get(24).ok_or_else(|| CodecError::malformed(format, "short PNG IHDR"))?; + let ct = *bytes.get(25).ok_or_else(|| CodecError::malformed(format, "short PNG IHDR"))?; + let color = match (ct, depth) { + (0, 1) => RasterColorType::Bitmap, + (0, _) => RasterColorType::Gray, + (2, _) => RasterColorType::Rgb, + (3, _) => RasterColorType::Indexed, + (4, _) => RasterColorType::GrayAlpha, + (6, _) => RasterColorType::Rgba, + _ => RasterColorType::Other, + }; + let palette = crate::png_palette(bytes)?.map(|p| (p.colors, p.transparent)); + let (palette, transparent_index) = palette.map_or((None, None), |(c, t)| (Some(c), t)); + RasterInspection { + width: h.width, + height: h.height, + channels: match ct { + 0 | 3 => 1, + 2 => 3, + 4 => 2, + 6 => 4, + _ => h.layout.channels().min(255) as u8, + }, + bits_per_sample: depth, + color_type: color, + palette, + transparent_index, + } + } + Format::Tiff => { + let info = crate::tiff_info(bytes)?; + let Some(p) = info.default_page().and_then(|n| info.pages.get(n)) else { return Ok(None) }; + let color = match (p.photometric, p.bits_per_sample) { + (Some(0 | 1), 1) => RasterColorType::Bitmap, + (Some(0 | 1), _) => RasterColorType::Gray, + (Some(2 | 6), _) => RasterColorType::Rgb, + (Some(3), _) => RasterColorType::Indexed, + (Some(5), _) => RasterColorType::Cmyk, + (Some(8), _) => RasterColorType::Lab, + _ => RasterColorType::Other, + }; + RasterInspection { + width: p.width, + height: p.height, + channels: p.samples_per_pixel.min(255) as u8, + bits_per_sample: p.bits_per_sample.min(255) as u8, + color_type: color, + palette: None, + transparent_index: None, + } + } + Format::Jpeg => { + let Some((w, h, channels)) = crate::codecs::jpeg::scan_metadata(bytes).frame else { return Ok(None) }; + let depth = craft_jpeg::frame(bytes).map_or(8, |f| f.precision); + let color = match channels { + 1 => RasterColorType::Gray, + 3 => RasterColorType::Rgb, + 4 => RasterColorType::Cmyk, + _ => RasterColorType::Other, + }; + RasterInspection { width: w, height: h, channels, bits_per_sample: depth, color_type: color, palette: None, transparent_index: None } + } + Format::Bmp | Format::Gif => { + use image::ImageDecoder; + let d = crate::codecs::via_image::open(format, bytes, &Limits::default())?; + let (width, height) = d.dimensions(); + let (channels, bits, color) = match d.original_color_type() { + image::ExtendedColorType::L1 => (1, 1, RasterColorType::Bitmap), + image::ExtendedColorType::L2 => (1, 2, RasterColorType::Gray), + image::ExtendedColorType::L4 => (1, 4, RasterColorType::Gray), + image::ExtendedColorType::L8 => (1, 8, RasterColorType::Gray), + image::ExtendedColorType::L16 => (1, 16, RasterColorType::Gray), + image::ExtendedColorType::La8 => (2, 8, RasterColorType::GrayAlpha), + image::ExtendedColorType::La16 => (2, 16, RasterColorType::GrayAlpha), + image::ExtendedColorType::Rgb8 => (3, 8, RasterColorType::Rgb), + image::ExtendedColorType::Rgb16 => (3, 16, RasterColorType::Rgb), + image::ExtendedColorType::Rgba8 => (4, 8, RasterColorType::Rgba), + image::ExtendedColorType::Rgba16 => (4, 16, RasterColorType::Rgba), + _ => (4, 8, RasterColorType::Other), + }; + let (palette, transparent_index) = if format == Format::Gif { + crate::gif_palette(bytes).map_or((None, None), |(p, t)| (Some(p), t)) + } else { + (crate::bmp_palette(bytes)?, None) + }; + let (channels, color) = if format == Format::Gif { (1, RasterColorType::Indexed) } else { (channels, color) }; + RasterInspection { width, height, channels, bits_per_sample: bits, color_type: color, palette, transparent_index } + } + _ => return Ok(None), + }; + Ok(Some(i)) +} + +/// Classify grayscale sources for single-swatch tinting. GIF checks visible decoded pixels and +/// exposes its source palette through [`inspect_raster`]; all other formats use source headers. +pub fn classify_tone(bytes: &[u8]) -> Option { + let i = inspect_raster(bytes).ok()??; + if i.color_type == RasterColorType::Indexed && bytes.starts_with(b"GIF8") { + i.palette.as_ref()?; + let decoded = decode_with(bytes, &DecodeOptions::default()).ok()?; + let rgba = decoded.convert(ChannelLayout::Rgba, SampleType::U8); + let mut opaque = true; + let mut two_level = true; + for p in rgba.data().chunks_exact(4) { + match p[3] { + 0 => opaque = false, + 255 if p[0] == p[1] && p[1] == p[2] => two_level &= p[0] == 0 || p[0] == 255, + _ => return None, + } + } + return if two_level { + Some(ToneClass::Bitmap) + } else if opaque { + Some(ToneClass::Gray) + } else { + None + }; + } + match i.color_type { + RasterColorType::Bitmap => Some(ToneClass::Bitmap), + RasterColorType::Gray => Some(ToneClass::Gray), + _ => None, + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 974ff205b..335692a47 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -22,6 +22,7 @@ pub mod exr_stream; mod fidelity; mod format; mod image; +mod inspection; mod lab; mod options; pub mod orientation; @@ -47,6 +48,7 @@ pub use crate::image::{ ChannelLayout, CryptomatteBuffer, CryptomatteLayer, DecodeWarning, DeepChannel, DeepImage, ExrChannelInfo, ExrPartInfo, ExtraSample, Image, MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, }; +pub use crate::inspection::{RasterColorType, RasterInspection, ToneClass, classify_tone, inspect_raster}; pub use crate::options::{ DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, TiffCompression, From 07a4cd0fc758198ae1fd6b91cb179ebafc4ed8f6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:44:30 +0000 Subject: [PATCH 057/520] #778: preserve consumer VP8 frame limits --- crates/video/src/vp8/decoder.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/video/src/vp8/decoder.rs b/crates/video/src/vp8/decoder.rs index 40dcd2fa9..d08b69f3a 100644 --- a/crates/video/src/vp8/decoder.rs +++ b/crates/video/src/vp8/decoder.rs @@ -115,6 +115,7 @@ pub struct Decoder { sign_bias: [bool; 4], color_space: u8, stats: DecodeStats, + max_luma_samples: usize, } impl Default for Decoder { @@ -125,6 +126,14 @@ impl Default for Decoder { impl Decoder { pub fn new() -> Decoder { + Self::with_max_luma_samples(MAX_LUMA_SAMPLES) + } + + /// Set the maximum padded luma samples retained for one frame. + /// + /// The default is 36 million. Consumers that historically supported a larger bound can + /// choose it explicitly while retaining the same checked allocation limit. + pub fn with_max_luma_samples(max_luma_samples: usize) -> Decoder { Decoder { ctx: EntropyContext::default(), seg: Segmentation::default(), @@ -138,6 +147,7 @@ impl Decoder { sign_bias: [false; 4], color_space: 0, stats: DecodeStats::default(), + max_luma_samples, } } @@ -178,7 +188,7 @@ impl Decoder { let (w, h) = (usize::from(tag.width), usize::from(tag.height)); let (mb_w, mb_h) = (w.div_ceil(16), h.div_ceil(16)); let samples = mb_w.checked_mul(16).and_then(|w| w.checked_mul(mb_h)).and_then(|h| h.checked_mul(16)); - ensure!(samples.is_some_and(|n| n <= MAX_LUMA_SAMPLES), "frame size {}x{} exceeds the decoder memory limit", tag.width, tag.height); + ensure!(samples.is_some_and(|n| n <= self.max_luma_samples), "frame size {}x{} exceeds the decoder memory limit", tag.width, tag.height); if w != self.width || h != self.height { self.width = w; self.height = h; @@ -1032,6 +1042,14 @@ mod segment_map_tests { assert_eq!((d.width, d.height), (8192, 4320)); } + #[test] + fn caller_may_preserve_a_larger_existing_frame_limit() { + let mut decoder = Decoder::with_max_luma_samples(1 << 26); + assert!(decoder.begin_key_frame(&key_tag(8192, 8192)).is_ok()); + let mut default = Decoder::new(); + assert!(default.begin_key_frame(&key_tag(8192, 8192)).is_err()); + } + #[test] fn tiny_inter_partition_is_rejected_as_truncated() { let mut d = Decoder::new(); From 21d7c1267e89bcec63579b6913f950c74a7c9e9b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:22:05 +0000 Subject: [PATCH 058/520] metafile: draw GDI+ percentage hatch styles --- crates/metafile/src/canvas.rs | 37 +++++++++++++++++++++++++++++----- crates/metafile/src/emfplus.rs | 8 +++++--- crates/metafile/src/tests.rs | 29 ++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/crates/metafile/src/canvas.rs b/crates/metafile/src/canvas.rs index b4ac445fc..51159bdf3 100644 --- a/crates/metafile/src/canvas.rs +++ b/crates/metafile/src/canvas.rs @@ -1295,13 +1295,35 @@ impl Canvas { } } -/// An 8×8 hatch tile (HS_HORIZONTAL, HS_VERTICAL, HS_FDIAGONAL, HS_BDIAGONAL, HS_CROSS, -/// HS_DIAGCROSS): one-pixel lines in `fg` over `bg`, or over nothing when the background is -/// transparent. None for an unknown style. +/// An 8×8 GDI hatch tile (HS_HORIZONTAL through HS_DIAGCROSS). None for an unknown GDI style. pub(crate) fn hatch_tile(style: u32, fg: [u8; 4], bg: Option<[u8; 4]>) -> Option> { - if style > 5 { + hatch_tile_inner(style, fg, bg, false) +} + +/// An 8×8 GDI+ tile for the six basic and twelve percentage HatchStyle values. Percentage styles +/// use a Bayer tile with the specified foreground coverage. Other GDI+ styles still use the +/// fallback in `emfplus::Painter::fill` until their patterns have been mapped. +pub(crate) fn emfplus_hatch_tile(style: u32, fg: [u8; 4], bg: Option<[u8; 4]>) -> Option> { + hatch_tile_inner(style, fg, bg, true) +} + +fn hatch_tile_inner(style: u32, fg: [u8; 4], bg: Option<[u8; 4]>, gdi_plus: bool) -> Option> { + if style > if gdi_plus { 17 } else { 5 } { return None; } + // An 8×8 Bayer matrix keeps percentage hatches evenly distributed in a small, repeatable + // tile. The twelve values are GDI+'s Percent05 through Percent90 styles. + const BAYER: [[u8; 8]; 8] = [ + [0, 48, 12, 60, 3, 51, 15, 63], + [32, 16, 44, 28, 35, 19, 47, 31], + [8, 56, 4, 52, 11, 59, 7, 55], + [40, 24, 36, 20, 43, 27, 39, 23], + [2, 50, 14, 62, 1, 49, 13, 61], + [34, 18, 46, 30, 33, 17, 45, 29], + [10, 58, 6, 54, 9, 57, 5, 53], + [42, 26, 38, 22, 41, 25, 37, 21], + ]; + const PERCENT: [u32; 12] = [5, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90]; let mut px = Vec::with_capacity(8 * 8 * 4); for y in 0..8u32 { for x in 0..8u32 { @@ -1312,7 +1334,12 @@ pub(crate) fn hatch_tile(style: u32, fg: [u8; 4], bg: Option<[u8; 4]>) -> Option 2 => f, 3 => b, 4 => h || v, - _ => f || b, + 5 => f || b, + 6..=17 if gdi_plus => { + let count = (PERCENT[(style - 6) as usize] * 64 + 50) / 100; + u32::from(BAYER[y as usize][x as usize]) < count + } + _ => return None, }; px.extend(if on { fg } else { bg.unwrap_or([0, 0, 0, 0]) }); } diff --git a/crates/metafile/src/emfplus.rs b/crates/metafile/src/emfplus.rs index 5c45721a7..af406aa30 100644 --- a/crates/metafile/src/emfplus.rs +++ b/crates/metafile/src/emfplus.rs @@ -27,7 +27,7 @@ use std::sync::Arc; use crate::bytes::Bytes; -use crate::canvas::{BS_NULL, BS_SOLID, Brush, Canvas, LogFont, PS_NULL, Pen, RawTile, Xform, hatch_tile, mul}; +use crate::canvas::{BS_NULL, BS_SOLID, Brush, Canvas, LogFont, PS_NULL, Pen, RawTile, Xform, emfplus_hatch_tile, mul}; use crate::canvas::{Raw, RawClip}; use crate::cliprect::{MAX_AREA_CLIP_WORK, clip_polygon_within, convex, flatten, ops_bbox, pts_bbox, rect_poly}; use crate::dib::Rgba; @@ -845,9 +845,11 @@ impl Painter { cv.set_brush(Brush::new(BS_SOLID, *c)); cv.shape(ops.to_vec(), true, false); } - Fill::Hatch { style, fg, bg } => match hatch_tile(*style, *fg, Some(*bg)) { + Fill::Hatch { style, fg, bg } => match emfplus_hatch_tile(*style, *fg, Some(*bg)) { Some(px) => cv.tile_shape(ops, Arc::new(RawTile { w: 8, h: 8, px, cell: cv.pattern_px })), - // GDI+ has many more hatch styles than GDI: those fill with the blend of both colours. + // Percentage styles are tiled above. The remaining GDI+ hatch styles have no tile + // mapping yet, so keep the existing blended-colour fallback instead of inventing + // a pattern that could disagree with the file. None => { cv.set_brush(Brush::new(BS_SOLID, lerp(*fg, *bg, 0.5))); cv.shape(ops.to_vec(), true, false); diff --git a/crates/metafile/src/tests.rs b/crates/metafile/src/tests.rs index 806c95662..1a2a30fc6 100644 --- a/crates/metafile/src/tests.rs +++ b/crates/metafile/src/tests.rs @@ -2237,10 +2237,35 @@ fn emfplus_linear_gradient_fills_with_a_shaded_tile() { #[test] fn emfplus_hatch_brush_fills_with_an_8x8_tile() { - let brush = cat(vec![h32(0xDBC0_1002), h32(1), h32(4), h32(0xFF00_0000), h32(0xFFFF_FFFF)]); + // GDI+ HatchStyle.Percent50 (12), as used by System.Drawing's HatchBrush. + let brush = cat(vec![h32(0xDBC0_1002), h32(1), h32(12), h32(0xFF00_0000), h32(0xFFFF_FFFF)]); let fill = prec(0x400A, 0, &cat(vec![h32(0), h32(1), f32s(&[0.0, 0.0, 16.0, 16.0])])); let p = plus_pic(vec![prec(0x4008, 0x0100, &brush), fill]); - assert!(matches!(&p.items[0], Item::Pattern { tile, .. } if tile.width == 8 && tile.height == 8)); + let tile = pattern_of(&p.items[0]); + assert_eq!((tile.width, tile.height), (8, 8)); + let foreground = tile.rgba.as_chunks::<4>().0.iter().filter(|px| **px == [0, 0, 0, 255]).count(); + assert_eq!(foreground, 32, "Percent50 uses equal foreground and background coverage"); +} + +#[test] +fn gdi_plus_percentage_hatch_styles_keep_the_documented_coverage() { + const PERCENT: [usize; 12] = [5, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90]; + let fg = [0, 0, 0, 255]; + let bg = [255, 255, 255, 255]; + for style in 0..=17 { + let tile = crate::canvas::emfplus_hatch_tile(style, fg, Some(bg)).expect("supported GDI+ hatch style"); + let foreground = tile.as_chunks::<4>().0.iter().filter(|px| **px == fg).count(); + assert!((1..64).contains(&foreground), "style {style} should mix both colours"); + if (6..=17).contains(&style) { + let percent = PERCENT[(style - 6) as usize]; + assert_eq!(foreground, (percent * 64 + 50) / 100, "percentage style {style}"); + } + } + for style in 18..=52 { + assert!(crate::canvas::emfplus_hatch_tile(style, fg, Some(bg)).is_none(), "style {style} has no confirmed tile mapping"); + } + assert!(crate::canvas::emfplus_hatch_tile(53, fg, Some(bg)).is_none(), "out-of-range values stay unsupported"); + assert!(crate::canvas::hatch_tile(9, fg, Some(bg)).is_none(), "GDI's six-style range is unchanged"); } #[test] From c6ae97474351732c9e7c2c07eae6abfce9bd9882 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:09:49 -0500 Subject: [PATCH 059/520] Expose shared decoded DNG JPEG XL previews --- crates/raw/src/lib.rs | 9 ++++- crates/raw/src/preview.rs | 85 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 9ba94db04..8c8dd227d 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -146,8 +146,8 @@ pub use opcodes::{Opcode, OpcodeLists}; #[cfg(feature = "arw")] pub use preview::embedded_preview_dynamic_range_optimized; pub use preview::{ - PREVIEW_PREFIX, PreviewColorSpace, PreviewRange, bounded_preview, embedded_preview, embedded_preview_color_space, embedded_preview_covering, - embedded_preview_from_range, + DecodedPreview, PREVIEW_PREFIX, PreviewColorSpace, PreviewRange, bounded_preview, embedded_jpeg_preview, embedded_jpeg_preview_covering, + embedded_jxl_preview, embedded_preview, embedded_preview_color_space, embedded_preview_covering, embedded_preview_from_range, }; #[cfg(feature = "dng")] pub use semantic::{SemanticMask, semantic_masks}; @@ -636,6 +636,11 @@ pub fn probe_info(bytes: &[u8]) -> Result { probe_info_with(bytes, &Limits::default()) } +/// Orientation of a supported raw, read through the same format parser as [`probe_info`]. +pub fn orientation(bytes: &[u8]) -> Option { + probe_info(bytes).ok().map(|info| info.orientation.to_exif()) +} + /// [`probe_info`] within `limits`: a file that declares more than they allow fails as [`decode_with`] would. pub fn probe_info_with(bytes: &[u8], limits: &Limits) -> Result { decode_in(bytes, Mode::Header, limits).map(RawImage::into_info) diff --git a/crates/raw/src/preview.rs b/crates/raw/src/preview.rs index 6917d2588..48fa4e4b1 100644 --- a/crates/raw/src/preview.rs +++ b/crates/raw/src/preview.rs @@ -12,6 +12,73 @@ use craft_tiff::{Ifd, Tiff, makernote}; pub enum PreviewColorSpace { Srgb, AdobeRgb, + ProPhoto, +} + +/// A decoded DNG JPEG XL preview. Pixel samples use the declared bit depth and colour space. +#[derive(Clone, Debug, PartialEq)] +pub struct DecodedPreview { + pub width: u32, + pub height: u32, + pub bits: u32, + pub rgb: Vec, + pub color_space: Option, +} + +/// Decode the largest usable rendered JPEG XL preview in a DNG, within `limits`. +#[cfg(feature = "jxl")] +pub fn embedded_jxl_preview(bytes: &[u8], limits: &crate::Limits) -> Option { + use craft_tiff::image::chunk_bytes; + use craft_tiff::tags as t; + let tiff = Tiff::parse(bytes).ok()?; + let ifd0 = tiff.ifds.first()?; + if !ifd0.contains(t::DNG_VERSION) { + return None; + } + let mut candidates: Vec<_> = tiff + .all_ifds() + .into_iter() + .filter_map(|ifd| { + if ifd.u32(t::NEW_SUBFILE_TYPE)? & 1 == 0 + || ifd.u16(t::COMPRESSION)? != t::compression::JPEG_XL + || ifd.u16(t::SAMPLES_PER_PIXEL)? != 3 + || !matches!(ifd.u16(t::PHOTOMETRIC), Some(t::photometric::RGB | t::photometric::YCBCR)) + { + return None; + } + let info = ifd.image().ok()?; + let chunk = info.single_chunk(bytes.len() as u64)?; + chunk_bytes(bytes, &chunk)?; + Some((u64::from(info.width) * u64::from(info.height), ifd, info)) + }) + .collect(); + candidates.sort_by_key(|(area, _, _)| std::cmp::Reverse(*area)); + for (_, ifd, info) in candidates { + let samples = usize::from(info.samples_per_pixel); + let pixels = usize::try_from(info.width).ok()?.checked_mul(usize::try_from(info.height).ok()?)?; + if pixels.checked_mul(samples)? > limits.samples() { + continue; + } + let Ok(crate::RawData::U16(rgb)) = crate::tiffraw::read_image(bytes, &info, tiff.order, crate::tiffraw::Packing::Msb, limits) else { + continue; + }; + if rgb.len() != pixels.checked_mul(3)? { + continue; + } + let color_space = match ifd.u16(50970).or_else(|| ifd0.u16(50970)) { + Some(2) => Some(PreviewColorSpace::Srgb), + Some(3) => Some(PreviewColorSpace::AdobeRgb), + Some(4) => Some(PreviewColorSpace::ProPhoto), + _ => None, + }; + return Some(DecodedPreview { width: info.width, height: info.height, bits: u32::from(info.bits()), rgb, color_space }); + } + None +} + +#[cfg(not(feature = "jxl"))] +pub fn embedded_jxl_preview(_: &[u8], _: &crate::Limits) -> Option { + None } /// The colour space the raw says its embedded JPEG is in, for a JPEG that declares none itself (no ICC profile, @@ -243,7 +310,17 @@ fn candidates<'a>(data: &'a [u8], ifd: &Ifd, base: u64, out: &mut Vec<&'a [u8]>, /// The largest embedded preview, if any: a JPEG, or (DNG 1.7) a JPEG XL file, or for a Hasselblad 3FR / FFF, which /// has no JPEG, its uncompressed RGB preview wrapped as a baseline TIFF. pub fn embedded_preview(bytes: &[u8]) -> Option> { - preview_covering(bytes, None) + preview_covering(bytes, None, false) +} + +/// Largest embedded JPEG preview, excluding DNG JPEG XL previews. +pub fn embedded_jpeg_preview(bytes: &[u8]) -> Option> { + preview_covering(bytes, None, true) +} + +/// Smallest embedded JPEG of the largest preview's shape whose long edge covers `min_edge`, or the largest JPEG. +pub fn embedded_jpeg_preview_covering(bytes: &[u8], min_edge: u32) -> Option> { + preview_covering(bytes, Some(min_edge), true) } /// The smallest embedded JPEG whose long edge is at least `min_edge` and that looks like a copy of the largest preview @@ -251,7 +328,7 @@ pub fn embedded_preview(bytes: &[u8]) -> Option> { /// grey mask or depth map), else [`embedded_preview`]. Only TIFF-based raws keep several: an A7R VI's 1616 × 1080 /// preview is 0.3 MB beside its 9984 × 6656 one (5.6 MB), so a stand-in up to 1616 px decodes about a twentieth of the data. pub fn embedded_preview_covering(bytes: &[u8], min_edge: u32) -> Option> { - preview_covering(bytes, Some(min_edge)) + preview_covering(bytes, Some(min_edge), false) } /// Of the DCT JPEGs in `found`, the one with the shortest long edge of at least `min_edge` with `largest`'s aspect @@ -273,7 +350,7 @@ fn smallest_covering<'a>(found: &[&'a [u8]], largest: &[u8], min_edge: u32) -> O .map(|(s, _)| s) } -fn preview_covering(bytes: &[u8], min_edge: Option) -> Option> { +fn preview_covering(bytes: &[u8], min_edge: Option, jpeg_only: bool) -> Option> { if bytes.starts_with(b"FUJIFILMCCD-RAW") { return raf_jpeg(bytes).filter(|j| is_dct_jpeg(j)).map(|j| trim_eoi(j).to_vec()); } @@ -326,7 +403,7 @@ fn preview_covering(bytes: &[u8], min_edge: Option) -> Option> { found.push(p); } bound_candidates(&mut found, bytes.len()); - let largest = found.iter().copied().filter(|s| is_dct_jpeg(s) || is_jxl(s)).max_by_key(|s| s.len()); + let largest = found.iter().copied().filter(|s| is_dct_jpeg(s) || (!jpeg_only && is_jxl(s))).max_by_key(|s| s.len()); let best = min_edge .zip(largest) .and_then(|(min, big)| smallest_covering(&found, big, min)) From a47b78256c6acf3d7fdc4bc7cfbc25c373eac522 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:15:30 -0500 Subject: [PATCH 060/520] Expose shared embedded ICC extraction --- crates/image-codecs/src/inspection.rs | 8 ++++++++ crates/image-codecs/src/lib.rs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/inspection.rs b/crates/image-codecs/src/inspection.rs index 922cc5ce1..e263eea3a 100644 --- a/crates/image-codecs/src/inspection.rs +++ b/crates/image-codecs/src/inspection.rs @@ -3,6 +3,14 @@ use crate::error::CodecError; use crate::{ChannelLayout, DecodeOptions, Format, Limits, SampleType, decode_with}; +/// Return an embedded ICC profile using the selected format decoder. +/// +/// This fully decodes the image; callers that already need the pixels should read `Image::meta.icc` +/// from that decode instead of invoking this helper separately. +pub fn embedded_icc(bytes: &[u8]) -> Result>, CodecError> { + Ok(decode_with(bytes, &DecodeOptions::default())?.icc) +} + /// The source pixel model reported by an image container, before decode normalizes it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum RasterColorType { diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 335692a47..865e59865 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -48,7 +48,7 @@ pub use crate::image::{ ChannelLayout, CryptomatteBuffer, CryptomatteLayer, DecodeWarning, DeepChannel, DeepImage, ExrChannelInfo, ExrPartInfo, ExtraSample, Image, MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, }; -pub use crate::inspection::{RasterColorType, RasterInspection, ToneClass, classify_tone, inspect_raster}; +pub use crate::inspection::{RasterColorType, RasterInspection, ToneClass, classify_tone, embedded_icc, inspect_raster}; pub use crate::options::{ DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, TiffCompression, From fc9ecb40e72bb3d539ae16d158b84ebed4c65024 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:22:58 -0500 Subject: [PATCH 061/520] Extend shared file-format registry --- Cargo.lock | 1 + crates/file-formats/Cargo.toml | 1 + crates/file-formats/src/lib.rs | 39 +++++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e004a7ce..87ef135e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1459,6 +1459,7 @@ name = "craft-file-formats" version = "0.1.0" dependencies = [ "craft-eps", + "craft-svg", ] [[package]] diff --git a/crates/file-formats/Cargo.toml b/crates/file-formats/Cargo.toml index 0a5be5e76..00d3b388a 100644 --- a/crates/file-formats/Cargo.toml +++ b/crates/file-formats/Cargo.toml @@ -9,6 +9,7 @@ rust-version.workspace = true [dependencies] craft-eps = { workspace = true } +craft-svg = { workspace = true } [lints] workspace = true diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 9d3a3c438..9df7fe16a 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -14,6 +14,8 @@ pub enum Format { Illustrator, Dxf, Svg, + /// Gzip-compressed SVG. Kept distinct so writers do not place plain SVG under `.svgz`. + Svgz, Png, Jpeg, Bmp, @@ -29,6 +31,13 @@ pub enum Format { Pdn, Raw, Pcraft, + Idml, + DesignCraft, + Docx, + Rtf, + Xlsx, + TaggedText, + Pptx, Gcode, Studio, StencilCraft, @@ -63,16 +72,29 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option { if bytes.starts_with(b"silhouette04;") || bytes.starts_with(b"silhouette05;") { return Some(Format::Studio); } + let tagged = bytes.strip_prefix(b"\xef\xbb\xbf").unwrap_or(bytes).trim_ascii_start(); + if tagged.starts_with(b" Option { "ps" => Some(Format::PostScript), "ai" => Some(Format::Illustrator), "dxf" => Some(Format::Dxf), - "svg" | "svgz" => Some(Format::Svg), + "svg" => Some(Format::Svg), + "svgz" => Some(Format::Svgz), "png" => Some(Format::Png), "jpg" | "jpeg" => Some(Format::Jpeg), "bmp" => Some(Format::Bmp), @@ -105,6 +128,12 @@ pub fn from_extension(ext: &str) -> Option { | "kdc" | "mdc" | "mef" | "mos" | "mrw" | "nef" | "nrw" | "obm" | "orf" | "pef" | "ptx" | "pxn" | "r3d" | "raf" | "raw" | "rwl" | "rw2" | "rwz" | "sr2" | "srf" | "sti" | "x3f" => Some(Format::Raw), "pcraft" => Some(Format::Pcraft), + "idml" => Some(Format::Idml), + "designcraft" | "dcdoc" => Some(Format::DesignCraft), + "docx" => Some(Format::Docx), + "rtf" => Some(Format::Rtf), + "xlsx" => Some(Format::Xlsx), + "pptx" => Some(Format::Pptx), "studio" | "studio3" => Some(Format::Studio), "stencilcraft" => Some(Format::StencilCraft), "gcode" | "nc" => Some(Format::Gcode), From fdfb18885507e60604d3188078e3a34df82128ec Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:24:36 -0500 Subject: [PATCH 062/520] Use array chunks for raster pixels --- crates/image-codecs/src/inspection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/image-codecs/src/inspection.rs b/crates/image-codecs/src/inspection.rs index e263eea3a..b5dc0c95b 100644 --- a/crates/image-codecs/src/inspection.rs +++ b/crates/image-codecs/src/inspection.rs @@ -181,7 +181,7 @@ pub fn classify_tone(bytes: &[u8]) -> Option { let rgba = decoded.convert(ChannelLayout::Rgba, SampleType::U8); let mut opaque = true; let mut two_level = true; - for p in rgba.data().chunks_exact(4) { + for p in rgba.data().as_chunks::<4>().0 { match p[3] { 0 => opaque = false, 255 if p[0] == p[1] && p[1] == p[2] => two_level &= p[0] == 0 || p[0] == 255, From 32529b94e8bfcc2febc08ef9d00dc97187e8e7e2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:24:40 -0500 Subject: [PATCH 063/520] Resolve plain text format extensions --- crates/file-formats/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 9df7fe16a..6029757de 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -38,6 +38,7 @@ pub enum Format { Xlsx, TaggedText, Pptx, + PlainText, Gcode, Studio, StencilCraft, @@ -134,6 +135,7 @@ pub fn from_extension(ext: &str) -> Option { "rtf" => Some(Format::Rtf), "xlsx" => Some(Format::Xlsx), "pptx" => Some(Format::Pptx), + "txt" | "text" | "md" => Some(Format::PlainText), "studio" | "studio3" => Some(Format::Studio), "stencilcraft" => Some(Format::StencilCraft), "gcode" | "nc" => Some(Format::Gcode), From 1f9c271f7d0924a50b84032dcde90b259ae3ccf9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:26:09 -0500 Subject: [PATCH 064/520] Recognize legacy Word extension in shared registry --- crates/file-formats/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 6029757de..3c6001673 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -34,6 +34,8 @@ pub enum Format { Idml, DesignCraft, Docx, + /// Legacy OLE Compound File Word document; identified by its extension only. + LegacyDoc, Rtf, Xlsx, TaggedText, @@ -132,6 +134,7 @@ pub fn from_extension(ext: &str) -> Option { "idml" => Some(Format::Idml), "designcraft" | "dcdoc" => Some(Format::DesignCraft), "docx" => Some(Format::Docx), + "doc" => Some(Format::LegacyDoc), "rtf" => Some(Format::Rtf), "xlsx" => Some(Format::Xlsx), "pptx" => Some(Format::Pptx), From 6ba1ca33ca21c33de55ef155141db08ca6048e8d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:27:11 -0500 Subject: [PATCH 065/520] Recognize InDesign files in shared registry --- crates/file-formats/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 3c6001673..1e45ad447 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -33,6 +33,7 @@ pub enum Format { Pcraft, Idml, DesignCraft, + Indd, Docx, /// Legacy OLE Compound File Word document; identified by its extension only. LegacyDoc, @@ -49,6 +50,9 @@ pub enum Format { /// Resolve a name and file bytes to a format. Strong signatures take precedence over /// extension hints. Only format-specific bounded probes and extension tables live here. pub fn detect(name: &str, bytes: &[u8]) -> Option { + if bytes.starts_with(&INDD_SIGNATURE) { + return Some(Format::Indd); + } if let Some(found) = craft_eps::sniff(bytes) { return Some(match (found.kind, found.illustrator) { (_, true) => Format::Illustrator, @@ -133,6 +137,7 @@ pub fn from_extension(ext: &str) -> Option { "pcraft" => Some(Format::Pcraft), "idml" => Some(Format::Idml), "designcraft" | "dcdoc" => Some(Format::DesignCraft), + "indd" | "indt" => Some(Format::Indd), "docx" => Some(Format::Docx), "doc" => Some(Format::LegacyDoc), "rtf" => Some(Format::Rtf), @@ -146,6 +151,8 @@ pub fn from_extension(ext: &str) -> Option { } } +const INDD_SIGNATURE: [u8; 16] = [0x06, 0x06, 0xED, 0xF5, 0xD8, 0x1D, 0x46, 0xE5, 0xBD, 0x31, 0xEF, 0xE7, 0xFE, 0x74, 0xB7, 0x1D]; + fn extension(name: &str) -> Option<&str> { Some(name.rsplit(['/', '\\']).next()?.rsplit_once('.')?.1) } From bdba99322ea1eb4807aab17ac4cffe53d62a4b89 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:28:04 -0500 Subject: [PATCH 066/520] Use canonical winprint repository URL --- Cargo.lock | 20 ++++++++++---------- crates/print/Cargo.toml | 5 ++--- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5ff17d87..3c0f9c7e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3034,7 +3034,7 @@ dependencies = [ "libc", "option-ext", "redox_users", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -3581,7 +3581,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -5804,7 +5804,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7118,7 +7118,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7688,7 +7688,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7757,7 +7757,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -8930,10 +8930,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix 1.1.5", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -10085,7 +10085,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -10355,7 +10355,7 @@ dependencies = [ [[package]] name = "winprint" version = "0.2.1" -source = "git+https://github.com/dotCooCoo/winprint.rs?rev=72951d7455ebcab569fbd9386c513d3d9f53fb02#72951d7455ebcab569fbd9386c513d3d9f53fb02" +source = "git+https://github.com/storytold/winprint.rs?rev=72951d7455ebcab569fbd9386c513d3d9f53fb02#72951d7455ebcab569fbd9386c513d3d9f53fb02" dependencies = [ "flate2", "fmt-derive", diff --git a/crates/print/Cargo.toml b/crates/print/Cargo.toml index 50fbb43d6..36b8813f2 100644 --- a/crates/print/Cargo.toml +++ b/crates/print/Cargo.toml @@ -35,9 +35,8 @@ log = "0.4" # ticket (winprint.rs#12, storytold/pdfcraft#3823), and reads whitespace-padded integers in # PrintCapabilities (winprint.rs#3, storytold/pdfcraft#2430). Pinned to an immutable revision; # review before bumping. The settings chosen in a driver's own dialog become a ticket through -# `PrintTicket::from_dev_mode`: until it is merged into storytold/winprint.rs, the pin is the -# fork commit that adds it on top of storytold/winprint.rs main (e90c444). -winprint = { git = "https://github.com/dotCooCoo/winprint.rs", rev = "72951d7455ebcab569fbd9386c513d3d9f53fb02", default-features = false } +# `PrintTicket::from_dev_mode`. +winprint = { git = "https://github.com/storytold/winprint.rs", rev = "72951d7455ebcab569fbd9386c513d3d9f53fb02", default-features = false } # GetDefaultPrinter (MIT). winsafe = { version = "0.0.29", default-features = false, features = ["winspool"] } # The job file's private folder. From 96c848798408370cba30b9930a89b6a73d1b9aa8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:30:54 -0500 Subject: [PATCH 067/520] Add shared InDesign conversion facade --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 2 ++ crates/indd/Cargo.toml | 14 ++++++++++++++ crates/indd/src/lib.rs | 16 ++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 crates/indd/Cargo.toml create mode 100644 crates/indd/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 174984c1d..03a7e15f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1638,6 +1638,13 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-indd" +version = "0.1.0" +dependencies = [ + "indd", +] + [[package]] name = "craft-jmap" version = "0.1.0" @@ -4738,6 +4745,11 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" +[[package]] +name = "indd" +version = "0.1.0" +source = "git+https://github.com/storytold/indd-utils?rev=7f3060e5bce78d21ea70e0d5eacb96edce15dfbe#7f3060e5bce78d21ea70e0d5eacb96edce15dfbe" + [[package]] name = "indexmap" version = "2.14.2" diff --git a/Cargo.toml b/Cargo.toml index 4735dba39..bc06a403b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,7 @@ craft-fig = { path = "crates/fig" } craft-fonts-build = { path = "crates/fonts-build" } craft-i18n = { path = "crates/i18n" } craft-idml = { path = "crates/idml" } +craft-indd = { path = "crates/indd" } craft-jmap = { path = "crates/jmap" } craft-ical = { path = "crates/ical" } craft-jpeg = { path = "crates/jpeg" } @@ -126,6 +127,7 @@ craft-vectordrawable = { path = "crates/vectordrawable" } craft-xls = { path = "crates/xls" } craft-wordmodel = { path = "crates/wordmodel" } craft-xlsx = { path = "crates/xlsx" } +indd = { git = "https://github.com/storytold/indd-utils", rev = "7f3060e5bce78d21ea70e0d5eacb96edce15dfbe" } craft-xcf = { path = "crates/xcf" } craft-xml = { path = "crates/xml" } craft-zip = { path = "crates/zip" } diff --git a/crates/indd/Cargo.toml b/crates/indd/Cargo.toml new file mode 100644 index 000000000..874ccb0d6 --- /dev/null +++ b/crates/indd/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "craft-indd" +description = "Shared InDesign document to IDML conversion facade for Craft applications" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true + +[dependencies] +indd = { workspace = true } + +[lints] +workspace = true diff --git a/crates/indd/src/lib.rs b/crates/indd/src/lib.rs new file mode 100644 index 000000000..2cce05c0d --- /dev/null +++ b/crates/indd/src/lib.rs @@ -0,0 +1,16 @@ +//! Shared InDesign document conversion boundary. +//! +//! InDesign's binary document model is converted to IDML here. Applications can import the +//! resulting package through [`craft_idml`] while retaining only their own model and UI mapping. + +pub use indd::{Conversion, Error, Preview, Warning}; + +/// Whether `bytes` begins with the fixed signature of an InDesign document. +pub fn is_indd(bytes: &[u8]) -> bool { + bytes.starts_with(&indd::header::SIGNATURE) +} + +/// Convert an InDesign document to IDML. `name` is the file name stored in the package. +pub fn convert(bytes: &[u8], name: &str) -> Result { + indd::convert(bytes, name) +} From 47bf5650b0d503b97bfb5e841746c4957897d11f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:31:26 -0500 Subject: [PATCH 068/520] Expose shared InDesign signature --- crates/indd/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/indd/src/lib.rs b/crates/indd/src/lib.rs index 2cce05c0d..657b417fc 100644 --- a/crates/indd/src/lib.rs +++ b/crates/indd/src/lib.rs @@ -3,11 +3,12 @@ //! InDesign's binary document model is converted to IDML here. Applications can import the //! resulting package through [`craft_idml`] while retaining only their own model and UI mapping. +pub use indd::header::SIGNATURE; pub use indd::{Conversion, Error, Preview, Warning}; /// Whether `bytes` begins with the fixed signature of an InDesign document. pub fn is_indd(bytes: &[u8]) -> bool { - bytes.starts_with(&indd::header::SIGNATURE) + bytes.starts_with(&SIGNATURE) } /// Convert an InDesign document to IDML. `name` is the file name stored in the package. From 1df8d98c852f66eb0987ac156e8cdba07dfde20f Mon Sep 17 00:00:00 2001 From: alehel Date: Thu, 12 Nov 2026 20:24:38 +0100 Subject: [PATCH 069/520] craft-video: MPEG-2 encoder, first step: I and P pictures (#1133) The start of MPEG-2 encoding in craft_video::mpeg2 (feature mpeg2-encode): progressive frame pictures, closed I/P GOPs, 4:2:0 Main Profile and 4:2:2 Profile, constant quantiser or single-pass average bitrate with adaptive quantisation, half-sample motion search. The encoder shares the decoder's code tables, IDCT, dequantisation and motion compensation, so the decoder outputs exactly the encoder's reconstruction. B pictures, a VBV guarantee, interlaced coding and DVD or broadcast presets are later steps of #1133. The mpeg2 feature is split into mpeg2-decode and mpeg2-encode; mpeg2 still turns on both. --- crates/video/Cargo.toml | 10 +- crates/video/README.md | 2 +- crates/video/src/lib.rs | 4 +- crates/video/src/mpeg2/README.md | 70 ++- crates/video/src/mpeg2/encode/bitwriter.rs | 92 +++ crates/video/src/mpeg2/encode/mod.rs | 624 +++++++++++++++++++++ crates/video/src/mpeg2/encode/motion.rs | 191 +++++++ crates/video/src/mpeg2/encode/picture.rs | 412 ++++++++++++++ crates/video/src/mpeg2/encode/rate.rs | 102 ++++ crates/video/src/mpeg2/encode/tables.rs | 264 +++++++++ crates/video/src/mpeg2/encode/tests.rs | 250 +++++++++ crates/video/src/mpeg2/encode/transform.rs | 122 ++++ crates/video/src/mpeg2/idct.rs | 2 +- crates/video/src/mpeg2/mod.rs | 8 +- crates/video/src/mpeg2/slice.rs | 11 +- crates/video/src/mpeg2/vlc.rs | 2 +- crates/video/tests/mpeg2/encode.rs | 206 +++++++ crates/video/tests/mpeg2/main.rs | 2 + 18 files changed, 2361 insertions(+), 13 deletions(-) create mode 100644 crates/video/src/mpeg2/encode/bitwriter.rs create mode 100644 crates/video/src/mpeg2/encode/mod.rs create mode 100644 crates/video/src/mpeg2/encode/motion.rs create mode 100644 crates/video/src/mpeg2/encode/picture.rs create mode 100644 crates/video/src/mpeg2/encode/rate.rs create mode 100644 crates/video/src/mpeg2/encode/tables.rs create mode 100644 crates/video/src/mpeg2/encode/tests.rs create mode 100644 crates/video/src/mpeg2/encode/transform.rs create mode 100644 crates/video/tests/mpeg2/encode.rs diff --git a/crates/video/Cargo.toml b/crates/video/Cargo.toml index 0e31f8e26..3b95faef6 100644 --- a/crates/video/Cargo.toml +++ b/crates/video/Cargo.toml @@ -25,8 +25,12 @@ apv-encode = ["craft-media/bitstream"] vp8 = ["vp8-decode", "vp8-encode"] vp8-decode = [] vp8-encode = [] -# MPEG-2 video (ITU-T H.262) and MPEG-1 video (ISO/IEC 11172-2): decoder. -mpeg2 = [] +# MPEG-2 video (ITU-T H.262) and MPEG-1 video (ISO/IEC 11172-2): decoder (`mpeg2-decode`) and +# MPEG-2 encoder (`mpeg2-encode`). The encoder reuses the decoder's code tables, IDCT and motion +# compensation, so it turns on the decoder too. +mpeg2 = ["mpeg2-decode", "mpeg2-encode"] +mpeg2-decode = [] +mpeg2-encode = ["mpeg2-decode"] # MPEG-4 Part 2 video (ISO/IEC 14496-2, the Xvid/DivX format): Simple profile encoder. mpeg4 = ["mpeg4-encode"] mpeg4-encode = ["craft-media/bitstream"] @@ -130,7 +134,7 @@ required-features = ["mpeg4-encode"] [[test]] name = "mpeg2" -required-features = ["mpeg2"] +required-features = ["mpeg2-decode"] [[test]] name = "prores" diff --git a/crates/video/README.md b/crates/video/README.md index 4aff9b8d0..74b8c6414 100644 --- a/crates/video/README.md +++ b/crates/video/README.md @@ -21,7 +21,7 @@ No codec feature is on by default. | `dnx` | [`dnx`](src/dnx/README.md) | Avid DNxHD / DNxHR (SMPTE ST 2019-1 VC-3): decoder and encoder | | `apv` | [`apv`](src/apv/README.md) | APV (Advanced Professional Video, RFC 9924): decoder (`apv-decode` alone) and encoder (`apv-encode` alone) | | `vp8` | `vp8` | VP8 (RFC 6386): decoder | -| `mpeg2` | [`mpeg2`](src/mpeg2/README.md) | MPEG-2 video (ITU-T H.262) and MPEG-1 video (ISO/IEC 11172-2): decoder | +| `mpeg2` | [`mpeg2`](src/mpeg2/README.md) | MPEG-2 video (ITU-T H.262) and MPEG-1 video (ISO/IEC 11172-2): decoder (`mpeg2-decode` alone); MPEG-2 encoder with I and P pictures (`mpeg2-encode`, which also turns on the decoder) | | `mpeg4` | [`mpeg4`](src/mpeg4/README.md) | MPEG-4 Part 2 video (ISO/IEC 14496-2, the Xvid/DivX format): Simple profile encoder (`mpeg4-encode`) | | `prores` | `prores` | Apple ProRes (SMPTE RDD 36): decoder and encoder | | `h264` | [`h264`](src/h264/decode/README.md) | ITU-T H.264 / AVC: decoder (`h264-decode` alone) and encoder (`h264-encode` alone) | diff --git a/crates/video/src/lib.rs b/crates/video/src/lib.rs index e8e9d8113..d9723a81d 100644 --- a/crates/video/src/lib.rs +++ b/crates/video/src/lib.rs @@ -8,7 +8,7 @@ //! | `dnx` | [`dnx`] | Avid DNxHD / DNxHR (SMPTE ST 2019-1): decoder and encoder | //! | `apv` | [`apv`] | APV (RFC 9924): decoder (`apv-decode`) and encoder (`apv-encode`) | //! | `vp8` | [`vp8`] | VP8 (RFC 6386): decoder (`vp8-decode`) and key-frame encoder (`vp8-encode`) | -//! | `mpeg2` | [`mpeg2`] | MPEG-2 video (ITU-T H.262) and MPEG-1 video: decoder | +//! | `mpeg2` | [`mpeg2`] | MPEG-2 video (ITU-T H.262) and MPEG-1 video: decoder (`mpeg2-decode`) and MPEG-2 I/P encoder (`mpeg2-encode`) | //! | `mpeg4` | [`mpeg4`] | MPEG-4 Part 2 video (ISO/IEC 14496-2, Xvid/DivX): Simple profile encoder (`mpeg4-encode`) | //! | `prores` | [`prores`] | Apple ProRes (SMPTE RDD 36): decoder and encoder | //! | `h264` | [`h264`] | ITU-T H.264 / AVC: decoder (`h264-decode`) and encoder (`h264-encode`) | @@ -35,7 +35,7 @@ pub mod dnx; #[cfg(any(feature = "vp8-decode", feature = "vp8-encode"))] pub mod vp8; -#[cfg(feature = "mpeg2")] +#[cfg(feature = "mpeg2-decode")] pub mod mpeg2; #[cfg(feature = "mpeg4-encode")] diff --git a/crates/video/src/mpeg2/README.md b/crates/video/src/mpeg2/README.md index 1c49d45cd..b68dc800b 100644 --- a/crates/video/src/mpeg2/README.md +++ b/crates/video/src/mpeg2/README.md @@ -1,6 +1,7 @@ -# MPEG-2 and MPEG-1 video decoder (`craft_video::mpeg2`, feature `mpeg2`) +# MPEG-2 and MPEG-1 video decoder, MPEG-2 encoder (`craft_video::mpeg2`, features `mpeg2-decode`, `mpeg2-encode`) -Clean-room MPEG-2 video and MPEG-1 video decoder in pure Rust. Depends only on +Clean-room MPEG-2 video and MPEG-1 video decoder, and the first step of an MPEG-2 video encoder +(I and P pictures, see "Encoder"), in pure Rust. Depends only on `thiserror` and (optional, feature `threads`) `rayon`. Builds for `wasm32-unknown-unknown`; no `unsafe`. No GPL/LGPL code (FFmpeg, libmpeg2, libavcodec) was consulted; FFmpeg is used only as an external fixture generator and test oracle. @@ -11,6 +12,8 @@ consulted; FFmpeg is used only as an external fixture generator and test oracle. |---|---|---| | ITU-T H.262 \| ISO/IEC 13818-2 | 02/2000 (second edition, with Amd. 1 and Cor. 1/2 as incorporated in the 2000 text) | sequence / GOP / picture headers and extensions, slices, macroblocks, VLC tables (Annex B), inverse quantisation and mismatch control (§7.4), IDCT (Annex A), motion vectors and prediction (§7.6) | | ISO/IEC 11172-2 | 1993 | MPEG-1 video: slices spanning rows, MPEG-1 escape codes, oddification, full-pel vectors, D pictures | +| ITU-T H.262 \| ISO/IEC 13818-2 §8 | 02/2000 | profile and level limits the encoder chooses from (Tables 8-8 to 8-13) | +| MPEG-2 Test Model 5 (ISO/IEC JTC1/SC29/WG11/N0400) | April 1993 | encoder: target bit allocation and adaptive quantisation by activity (§5.2) | | IEEE Std 1180 | 1990 | IDCT accuracy test procedure (run as a unit test) | | ISO/IEC TR 13818-5 (MPEG Software Simulation Group decoder behaviour) | 1997 | two decoder behaviours the normative text leaves open (see "Interpretations") | @@ -72,6 +75,58 @@ the synthetic-stream tests confirmed against FFmpeg: formula only when the prediction is out of range: a field vector's doubled vertical component predicting a following frame vector. +## Encoder (feature `mpeg2-encode`) + +The encoder is the first step of MPEG-2 encoding in this crate, tracked in +storytold/craft-libs#1133. It writes valid MPEG-2 elementary streams, but not yet DVD or +broadcast deliverables: those need the later steps listed below. + +```rust +use craft_video::mpeg2::{Encoder, EncoderConfig, RateControl, YuvFrame}; + +let mut cfg = EncoderConfig::new(720, 576, 25, 1); +cfg.rate = RateControl::Abr { kbps: 6000 }; // or RateControl::Quantiser(1..=31) +cfg.gop_size = 12; // one I picture, then P pictures +let mut enc = Encoder::new(cfg)?; +for (i, f) in frames.iter().enumerate() { + let frame = YuvFrame { y: &f.y, cb: &f.cb, cr: &f.cr, y_stride: 720, c_stride: 360 }; + for p in enc.encode(&frame, i as i64)? { /* p.data, p.pts, p.dts, p.keyframe, p.picture_type */ } +} +for p in enc.flush() { /* none yet: no B pictures */ } +es.extend_from_slice(enc.end_of_sequence()); +``` + +What it does: + +- Main Profile (4:2:0) at Low, Main, High-1440 and High Level, and 4:2:2 Profile at Main and + High Level. The lowest level that holds the size, frame rate and (average) bitrate is chosen; + sizes beyond High Level are refused. Any even picture size; edges are padded by repetition. +- Progressive frame pictures, I and P, in closed GOPs. Every GOP starts with a sequence header, + sequence extension and GOP header (with a time code), so every I picture is a random access + point. Packets come out in decoding order, which equals display order without B pictures. +- Forward DCT on the transpose of the decoder's IDCT basis; intra quantisation with a small + dead zone, non-intra quantisation by truncation; default or custom quantiser matrices + (sequence header); linear or non-linear quantiser scale; intra DC precision 8-10 bits; + intra VLC table B.15. +- P pictures: integer pattern search (steps 8, 4, 2, 1) from candidate vectors (left neighbour, + predictor, the previous picture's vectors), then half-sample refinement; cost SAD + λ·bits. + Each macroblock is coded intra, motion-compensated with or without residual, "no MC" with + residual, or skipped. The search range sets f_code (1-4). +- Adaptive quantisation by macroblock activity (Test Model 5 §5.2.3). +- Rate control: a constant quantiser, or single-pass average bitrate with Test Model 5's + target bit allocation at picture level. Streams are signalled as variable bitrate + (`vbv_delay` 0xFFFF, the level's maximum bitrate and VBV size in the sequence header); the VBV + buffer is not modelled. +- The reconstruction uses the decoder's own dequantisation, mismatch control, IDCT and motion + compensation, so this crate's decoder outputs exactly the encoder's reconstruction. +- Slices (one per macroblock row) are coded in parallel on rayon with `threads`; the stream is + identical either way. + +Later steps (#1133): B pictures and reordering; CBR and VBR with a VBV guarantee; DVD presets +(sizes, aspect, GOP length, 9.8 Mb/s, 23.976 soft telecine); interlaced coding (field/frame DCT, +field prediction, alternate scan) and 4:2:2 High Level CBR for XDCAM HD422. MPEG-2 essence in the +MXF writer and a program stream muxer belong to `craft-media`. + ## Accuracy (vs FFmpeg as an external oracle) MPEG-2's IDCT is not bit-exact by definition (any IEEE 1180-accurate IDCT conforms, and the @@ -131,3 +186,14 @@ fell to 77–150 fps). These are lower bounds. 3000 random mutations never panic. - `synth_tests.rs`, `tests/mpeg2/oracle.rs`: see above (single-threaded decode identical to the threaded one). +- `encode/`: every encoder code decodes to its value with the decoder's tables; motion vector + deltas survive the decoder's wrap for f_code 1-4; forward/inverse DCT and quantiser round trips; + rate control converges on its target. `encode/tests.rs`: our decoder outputs exactly the + encoder's reconstruction (intra only, a 30-picture P GOP, 4:2:2, linear and non-linear + quantisers, quantisers 1 and 31, DC precision 8 and 10, custom matrices, odd macroblock counts, + average bitrate); quality and size follow the quantiser; repeated pictures skip; levels and + aspect in the sequence header; bad configurations and short or inconsistent planes are refused. +- `tests/mpeg2/encode.rs`: FFmpeg decodes our streams of `testsrc2` (CIF and 576-line 4:2:2) + without errors and within the decoder suite's tolerance of our reconstruction, ffprobe reports + the profile, level, pixel format and I/P picture types, and average bitrate lands within 10 % of + 800 and 2500 kbit/s targets. diff --git a/crates/video/src/mpeg2/encode/bitwriter.rs b/crates/video/src/mpeg2/encode/bitwriter.rs new file mode 100644 index 000000000..75b363d47 --- /dev/null +++ b/crates/video/src/mpeg2/encode/bitwriter.rs @@ -0,0 +1,92 @@ +//! Big-endian bit writer for start-code units. + +pub(crate) struct BitWriter { + out: Vec, + acc: u64, + /// Bits held in `acc` (always below 8 between calls). + n: u32, +} + +impl BitWriter { + pub fn new() -> Self { + Self { out: Vec::new(), acc: 0, n: 0 } + } + + /// Append the low `bits` (0..=32) bits of `v`, most significant first. + #[inline(always)] + pub fn put(&mut self, v: u32, bits: u32) { + if bits == 0 { + return; + } + let mask = if bits == 32 { u32::MAX } else { (1u32 << bits) - 1 }; + self.acc = (self.acc << bits) | u64::from(v & mask); + self.n += bits; + while self.n >= 8 { + self.n -= 8; + self.out.push((self.acc >> self.n) as u8); + } + self.acc &= (1u64 << self.n) - 1; + } + + #[inline(always)] + pub fn bit(&mut self, b: bool) { + self.put(b as u32, 1); + } + + /// Pad with zero bits to a byte boundary. + pub fn align(&mut self) { + if self.n > 0 { + self.put(0, 8 - self.n); + } + } + + /// Byte-align and write the start code `00 00 01 code`. + pub fn start_code(&mut self, code: u8) { + self.align(); + self.out.extend_from_slice(&[0, 0, 1, code]); + } + + /// Bits written so far. + #[cfg(test)] + pub fn bits(&self) -> u64 { + self.out.len() as u64 * 8 + u64::from(self.n) + } + + /// The bytes, padded to a byte boundary. + pub fn finish(mut self) -> Vec { + self.align(); + self.out + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::mpeg2::bits::Bits; + + #[test] + fn round_trips_through_the_reader() { + let mut w = BitWriter::new(); + w.put(0b101, 3); + w.put(0x1234_5678, 32); + w.bit(true); + w.put(0x3FF, 10); + assert_eq!(w.bits(), 46); + let d = w.finish(); + assert_eq!(d.len(), 6); + let mut b = Bits::new(&d); + assert_eq!(b.read(3), 0b101); + assert_eq!(b.read(32), 0x1234_5678); + assert!(b.bit()); + assert_eq!(b.read(10), 0x3FF); + assert_eq!(b.read(2), 0); + } + + #[test] + fn start_codes_are_byte_aligned() { + let mut w = BitWriter::new(); + w.put(1, 1); + w.start_code(0xB3); + assert_eq!(w.finish(), vec![0x80, 0, 0, 1, 0xB3]); + } +} diff --git a/crates/video/src/mpeg2/encode/mod.rs b/crates/video/src/mpeg2/encode/mod.rs new file mode 100644 index 000000000..fcd1bd8d1 --- /dev/null +++ b/crates/video/src/mpeg2/encode/mod.rs @@ -0,0 +1,624 @@ +//! MPEG-2 video encoder (ITU-T H.262 | ISO/IEC 13818-2), feature `mpeg2-encode`. +//! +//! This is the first step of MPEG-2 encoding in this crate (storytold/craft-libs#1133): I and P +//! pictures, progressive frame pictures, 4:2:0 (Main Profile) and 4:2:2 (4:2:2 Profile), closed +//! GOPs, constant quantiser or single-pass average bitrate. B pictures, a VBV guarantee, +//! interlaced coding and DVD / broadcast presets come later. +//! +//! ```no_run +//! use craft_video::mpeg2::encode::{Encoder, EncoderConfig, RateControl, YuvFrame}; +//! +//! let mut cfg = EncoderConfig::new(720, 576, 25, 1); +//! cfg.rate = RateControl::Abr { kbps: 6000 }; +//! let mut enc = Encoder::new(cfg)?; +//! let (y, c) = (vec![128u8; 720 * 576], vec![128u8; 360 * 288]); +//! let mut es = Vec::new(); +//! for i in 0..50 { +//! let frame = YuvFrame { y: &y, cb: &c, cr: &c, y_stride: 720, c_stride: 360 }; +//! for p in enc.encode(&frame, i)? { +//! es.extend_from_slice(&p.data); +//! } +//! } +//! for p in enc.flush() { +//! es.extend_from_slice(&p.data); +//! } +//! es.extend_from_slice(enc.end_of_sequence()); +//! # Ok::<(), craft_video::mpeg2::encode::EncodeError>(()) +//! ``` + +mod bitwriter; +mod motion; +mod picture; +mod rate; +mod tables; +mod transform; + +#[cfg(test)] +mod tests; + +use crate::mpeg2::ChromaFormat; +use crate::mpeg2::headers::{DEFAULT_INTRA, PictureType, SCAN}; +use crate::mpeg2::mc::Frame; + +use bitwriter::BitWriter; +use picture::{PicCtx, RowOut, RowPlanes, encode_row, qcode_for, qscale}; +use rate::RateCtl; + +#[derive(Debug, thiserror::Error)] +pub enum EncodeError { + #[error("unsupported MPEG-2 encoder configuration: {0}")] + Config(String), + #[error("bad input frame: {0}")] + BadFrame(String), +} + +/// aspect_ratio_information (H.262 Table 6-3). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AspectRatio { + /// Square samples. + Square, + /// 4:3 display. + Dar4x3, + /// 16:9 display. + Dar16x9, + /// 2.21:1 display. + Dar221x100, +} + +impl AspectRatio { + fn code(self) -> u32 { + match self { + AspectRatio::Square => 1, + AspectRatio::Dar4x3 => 2, + AspectRatio::Dar16x9 => 3, + AspectRatio::Dar221x100 => 4, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum RateControl { + /// A constant quantiser_scale_code (1..=31; mapped through H.262 Table 7-6 with + /// `nonlinear_quant`). Adaptive quantisation still varies it per macroblock when enabled. + Quantiser(u8), + /// Single-pass average bitrate in kbit/s. The stream is signalled as variable bitrate and + /// is not checked against the VBV buffer. + Abr { kbps: u32 }, +} + +#[derive(Clone, Debug)] +pub struct EncoderConfig { + /// Picture size: even, at most 1920×1152 (4:2:0) or 1920×1088 (4:2:2). + pub width: u32, + pub height: u32, + /// Frame rate: one of H.262 Table 6-4 (23.976, 24, 25, 29.97, 30, 50, 59.94, 60). + pub fps_num: u32, + pub fps_den: u32, + /// 4:2:0 (Main Profile) or 4:2:2 (4:2:2 Profile). + pub chroma: ChromaFormat, + pub aspect: AspectRatio, + /// Pictures per GOP: one I picture, then P pictures. 1 codes every picture as I. 1..=1023. + pub gop_size: u32, + pub rate: RateControl, + /// q_scale_type: the non-linear quantiser scale (finer steps at low quantisers). + pub nonlinear_quant: bool, + /// Vary the quantiser per macroblock with its activity (finer in flat areas). + pub adaptive_quant: bool, + /// Intra DC precision in bits: 8, 9 or 10. + pub intra_dc_bits: u8, + /// Quantiser matrices (raster order, values 1..=255); `None`: the defaults. + pub intra_matrix: Option<[u8; 64]>, + pub non_intra_matrix: Option<[u8; 64]>, + /// Largest motion vector searched, in luma samples (1..=63). + pub search_range: u32, + /// Code slices on the rayon pool (with the `threads` feature). + pub threads: bool, +} + +impl EncoderConfig { + pub fn new(width: u32, height: u32, fps_num: u32, fps_den: u32) -> Self { + Self { + width, + height, + fps_num, + fps_den, + chroma: ChromaFormat::Yuv420, + aspect: AspectRatio::Square, + gop_size: 12, + rate: RateControl::Quantiser(4), + nonlinear_quant: true, + adaptive_quant: true, + intra_dc_bits: 9, + intra_matrix: None, + non_intra_matrix: None, + search_range: 31, + threads: cfg!(feature = "threads"), + } + } +} + +/// Planar 8-bit input: luma `width × height`, chroma `width/2 × height/2` (4:2:0) or +/// `width/2 × height` (4:2:2), rows `y_stride` / `c_stride` bytes apart. +#[derive(Clone, Copy, Debug)] +pub struct YuvFrame<'a> { + pub y: &'a [u8], + pub cb: &'a [u8], + pub cr: &'a [u8], + pub y_stride: usize, + pub c_stride: usize, +} + +/// One coded picture, in decoding order. +#[derive(Clone, Debug)] +pub struct Packet { + /// Elementary stream bytes: the sequence header and GOP header before every I picture, then + /// the picture. + pub data: Vec, + pub pts: i64, + /// Decoding timestamp; equal to `pts` while there are no B pictures. + pub dts: i64, + /// An I picture starting a closed GOP (random access point). + pub keyframe: bool, + pub picture_type: PictureType, + /// Average quantiser_scale of the picture's macroblocks. + pub quantiser_scale: f32, +} + +/// The encoder's reconstruction of a picture (what a decoder outputs), cropped. +#[derive(Clone, Debug)] +pub struct ReconFrame { + pub width: u32, + pub height: u32, + pub y: Vec, + pub cb: Vec, + pub cr: Vec, +} + +/// The sequence_end_code. +const SEQUENCE_END: [u8; 4] = [0, 0, 1, 0xB7]; + +/// A profile and level (H.262 Tables 8-8 to 8-13): limits and profile_and_level_indication. +#[derive(Clone, Copy, Debug)] +struct Level { + indication: u8, + max_w: u32, + max_h: u32, + max_fps: u32, + luma_rate: u64, + bit_rate: u64, + vbv_bits: u64, +} + +const MAIN_LEVELS: [Level; 4] = [ + Level { indication: 0x4A, max_w: 352, max_h: 288, max_fps: 30, luma_rate: 3_041_280, bit_rate: 4_000_000, vbv_bits: 475_136 }, + Level { indication: 0x48, max_w: 720, max_h: 576, max_fps: 30, luma_rate: 10_368_000, bit_rate: 15_000_000, vbv_bits: 1_835_008 }, + Level { indication: 0x46, max_w: 1440, max_h: 1152, max_fps: 60, luma_rate: 47_001_600, bit_rate: 60_000_000, vbv_bits: 7_340_032 }, + Level { indication: 0x44, max_w: 1920, max_h: 1152, max_fps: 60, luma_rate: 62_668_800, bit_rate: 80_000_000, vbv_bits: 9_781_248 }, +]; + +const PROFILE_422_LEVELS: [Level; 2] = [ + Level { indication: 0x85, max_w: 720, max_h: 608, max_fps: 30, luma_rate: 11_059_200, bit_rate: 50_000_000, vbv_bits: 9_437_184 }, + Level { indication: 0x82, max_w: 1920, max_h: 1088, max_fps: 60, luma_rate: 62_668_800, bit_rate: 300_000_000, vbv_bits: 47_185_920 }, +]; + +/// frame_rate_code for an exact frame rate (Table 6-4). +fn frame_rate_code(num: u32, den: u32) -> Option { + if num == 0 || den == 0 { + return None; + } + let rates: [(u64, u64); 8] = [(24000, 1001), (24, 1), (25, 1), (30000, 1001), (30, 1), (50, 1), (60000, 1001), (60, 1)]; + rates.iter().position(|&(n, d)| u64::from(num) * d == n * u64::from(den)).map(|i| i as u32 + 1) +} + +/// The motion search's f_code: the smallest whose range [-8f, 8f-1] samples covers `range`. +fn f_code_for(range: u32) -> u8 { + (1..=4u8).find(|&fc| 8 * (1u32 << (fc - 1)) > range).unwrap_or(4) +} + +/// Pick the lowest level of the profile that holds the stream. +fn choose_level(cfg: &EncoderConfig) -> Result { + let levels: &[Level] = match cfg.chroma { + ChromaFormat::Yuv420 => &MAIN_LEVELS, + ChromaFormat::Yuv422 => &PROFILE_422_LEVELS, + ChromaFormat::Yuv444 => return Err(EncodeError::Config("4:4:4 has no MPEG-2 profile in use".into())), + }; + let fps = f64::from(cfg.fps_num) / f64::from(cfg.fps_den); + let luma_rate = f64::from(cfg.width) * f64::from(cfg.height) * fps; + let bps = match cfg.rate { + RateControl::Abr { kbps } => u64::from(kbps) * 1000, + RateControl::Quantiser(_) => 0, + }; + levels + .iter() + .find(|l| { + cfg.width <= l.max_w + && cfg.height <= l.max_h + && fps <= f64::from(l.max_fps) + 0.01 + && luma_rate <= l.luma_rate as f64 + && bps <= l.bit_rate + }) + .copied() + .ok_or_else(|| { + EncodeError::Config(format!( + "{}x{} at {:.3} fps{} is beyond every level of the profile", + cfg.width, + cfg.height, + fps, + if bps > 0 { format!(", {bps} bit/s") } else { String::new() } + )) + }) +} + +fn validate(cfg: &EncoderConfig) -> Result<(), EncodeError> { + let bad = |s: String| Err(EncodeError::Config(s)); + if cfg.width == 0 || cfg.height == 0 || !cfg.width.is_multiple_of(2) || !cfg.height.is_multiple_of(2) { + return bad(format!("picture size {}x{} must be even and non-zero", cfg.width, cfg.height)); + } + if frame_rate_code(cfg.fps_num, cfg.fps_den).is_none() { + return bad(format!("frame rate {}/{} is not one of H.262 Table 6-4", cfg.fps_num, cfg.fps_den)); + } + if !(1..=1023).contains(&cfg.gop_size) { + return bad(format!("GOP size {} (1..=1023)", cfg.gop_size)); + } + match cfg.rate { + RateControl::Quantiser(q) if !(1..=31).contains(&q) => return bad(format!("quantiser_scale_code {q} (1..=31)")), + RateControl::Abr { kbps: 0 } => return bad("bitrate 0".into()), + _ => {} + } + if !(8..=10).contains(&cfg.intra_dc_bits) { + return bad(format!("intra DC precision {} bits (8..=10)", cfg.intra_dc_bits)); + } + if [cfg.intra_matrix, cfg.non_intra_matrix].iter().flatten().any(|m| m.contains(&0)) { + return bad("quantiser matrix with a zero entry".into()); + } + if !(1..=63).contains(&cfg.search_range) { + return bad(format!("search range {} (1..=63)", cfg.search_range)); + } + Ok(()) +} + +/// Write a quantiser matrix (raster order) in zigzag order. +fn put_matrix(w: &mut BitWriter, m: &[u8; 64]) { + for &pos in &SCAN[0] { + w.put(u32::from(m[usize::from(pos)]), 8); + } +} + +/// A stateful encoder. Feed pictures in display order; packets come out in decoding order. +pub struct Encoder { + cfg: EncoderConfig, + level: Level, + mb_w: usize, + mb_h: usize, + shx: u32, + shy: u32, + f_code: u8, + intra_matrix: [u8; 64], + inter_matrix: [u8; 64], + /// Sequence header and sequence extension, sent before every GOP. + seq_header: Vec, + /// The reconstruction of the previous picture. + reference: Option, + prev_mvs: Vec<[i32; 2]>, + /// Pictures coded so far. + count: u64, + /// Position of the next picture in its GOP. + gop_pos: u32, + rc: Option, + recon_capture: bool, + recon: Vec, +} + +impl Encoder { + pub fn new(cfg: EncoderConfig) -> Result { + validate(&cfg)?; + let level = choose_level(&cfg)?; + let (shx, shy) = if cfg.chroma == ChromaFormat::Yuv422 { (1, 0) } else { (1, 1) }; + let nonlinear = cfg.nonlinear_quant; + let q_range = (f64::from(qscale(nonlinear, 1)), f64::from(qscale(nonlinear, 31))); + let rc = match cfg.rate { + RateControl::Abr { kbps } => Some(RateCtl::new(f64::from(kbps) * 1000.0, f64::from(cfg.fps_num) / f64::from(cfg.fps_den), q_range)), + RateControl::Quantiser(_) => None, + }; + let mut enc = Encoder { + level, + mb_w: (cfg.width as usize).div_ceil(16), + mb_h: (cfg.height as usize).div_ceil(16), + shx, + shy, + f_code: f_code_for(cfg.search_range), + intra_matrix: cfg.intra_matrix.unwrap_or(DEFAULT_INTRA), + inter_matrix: cfg.non_intra_matrix.unwrap_or([16; 64]), + seq_header: Vec::new(), + reference: None, + prev_mvs: Vec::new(), + count: 0, + gop_pos: 0, + rc, + recon_capture: false, + recon: Vec::new(), + cfg, + }; + enc.seq_header = enc.sequence_header_bytes(); + Ok(enc) + } + + pub fn config(&self) -> &EncoderConfig { + &self.cfg + } + + /// profile_and_level_indication of the stream (e.g. 0x48 Main Profile @ Main Level). + pub fn profile_and_level(&self) -> u8 { + self.level.indication + } + + /// The sequence header and sequence extension (for containers that store them apart). + pub fn sequence_header(&self) -> &[u8] { + &self.seq_header + } + + /// The sequence_end_code to append after the last packet of an elementary stream. + pub fn end_of_sequence(&self) -> &'static [u8] { + &SEQUENCE_END + } + + /// Keep each picture's reconstruction for [`Encoder::take_recon`]. + pub fn set_recon_capture(&mut self, on: bool) { + self.recon_capture = on; + } + + pub fn take_recon(&mut self) -> Vec { + std::mem::take(&mut self.recon) + } + + /// Pictures held for reordering (none yet: there are no B pictures). + pub fn flush(&mut self) -> Vec { + Vec::new() + } + + fn sequence_header_bytes(&self) -> Vec { + let c = &self.cfg; + let mut w = BitWriter::new(); + let bit_rate = self.level.bit_rate.div_ceil(400); + let vbv = self.level.vbv_bits / 16384; + w.start_code(0xB3); + w.put(c.width & 0xFFF, 12); + w.put(c.height & 0xFFF, 12); + w.put(c.aspect.code(), 4); + w.put(frame_rate_code(c.fps_num, c.fps_den).unwrap_or(3), 4); + w.put((bit_rate & 0x3FFFF) as u32, 18); + w.bit(true); // marker_bit + w.put((vbv & 0x3FF) as u32, 10); + w.bit(false); // constrained_parameters_flag + for m in [&c.intra_matrix, &c.non_intra_matrix] { + w.bit(m.is_some()); + if let Some(m) = m { + put_matrix(&mut w, m); + } + } + w.start_code(0xB5); + w.put(1, 4); // sequence extension + w.put(u32::from(self.level.indication), 8); + w.bit(true); // progressive_sequence + w.put(if c.chroma == ChromaFormat::Yuv422 { 2 } else { 1 }, 2); + w.put(c.width >> 12, 2); + w.put(c.height >> 12, 2); + w.put((bit_rate >> 18) as u32 & 0xFFF, 12); + w.bit(true); // marker_bit + w.put((vbv >> 10) as u32 & 0xFF, 8); + w.bit(true); // low_delay: no B pictures + w.put(0, 2); // frame_rate_extension_n + w.put(0, 5); // frame_rate_extension_d + w.finish() + } + + /// group_of_pictures_header with the time code of picture `self.count`. + fn gop_header(&self, w: &mut BitWriter) { + let fps = u64::from(self.cfg.fps_num.div_ceil(self.cfg.fps_den)).max(1); + let n = self.count; + let secs = n / fps; + w.start_code(0xB8); + w.bit(false); // drop_frame_flag + w.put(((secs / 3600) % 24) as u32, 5); + w.put(((secs / 60) % 60) as u32, 6); + w.bit(true); // marker_bit + w.put((secs % 60) as u32, 6); + w.put((n % fps) as u32, 6); + w.bit(true); // closed_gop + w.bit(false); // broken_link + } + + fn picture_header(&self, w: &mut BitWriter, intra: bool) { + w.start_code(0x00); + w.put(self.gop_pos & 0x3FF, 10); // temporal_reference: display order within the GOP + w.put(if intra { 1 } else { 2 }, 3); + w.put(0xFFFF, 16); // vbv_delay: variable bitrate + if !intra { + w.bit(false); // full_pel_forward_vector + w.put(7, 3); // forward_f_code (MPEG-2: in the extension) + } + w.bit(false); // extra_bit_picture + w.start_code(0xB5); + w.put(8, 4); // picture coding extension + let fc = if intra { 15 } else { u32::from(self.f_code) }; + for v in [fc, fc, 15, 15] { + w.put(v, 4); + } + w.put(u32::from(self.cfg.intra_dc_bits - 8), 2); + w.put(3, 2); // frame picture + w.bit(false); // top_field_first + w.bit(true); // frame_pred_frame_dct + w.bit(false); // concealment_motion_vectors + w.bit(self.cfg.nonlinear_quant); // q_scale_type + w.bit(true); // intra_vlc_format + w.bit(false); // alternate_scan + w.bit(false); // repeat_first_field + w.bit(self.cfg.chroma == ChromaFormat::Yuv420); // chroma_420_type = progressive_frame + w.bit(true); // progressive_frame + w.bit(false); // composite_display_flag + } + + /// Copy the input into a macroblock-aligned frame, repeating the last column and row. + fn load(&self, f: &YuvFrame) -> Result { + let (w, h) = (self.cfg.width as usize, self.cfg.height as usize); + let (cw, ch) = (w >> self.shx, h >> self.shy); + let (pw, ph) = (self.mb_w * 16, self.mb_h * 16); + let check = |name: &str, plane: &[u8], stride: usize, pw: usize, ph: usize| { + let need = stride.checked_mul(ph - 1).and_then(|n| n.checked_add(pw)); + if stride < pw || need.is_none_or(|n| plane.len() < n) { + return Err(EncodeError::BadFrame(format!("{name}: {} bytes with stride {stride} for {pw}x{ph}", plane.len()))); + } + Ok(()) + }; + check("luma", f.y, f.y_stride, w, h)?; + check("Cb", f.cb, f.c_stride, cw, ch)?; + check("Cr", f.cr, f.c_stride, cw, ch)?; + let mut out = Frame::new(pw, ph, pw >> self.shx, ph >> self.shy); + for (k, (src, stride, sw, sh)) in [(f.y, f.y_stride, w, h), (f.cb, f.c_stride, cw, ch), (f.cr, f.c_stride, cw, ch)].into_iter().enumerate() { + let dw = if k == 0 { pw } else { out.cw }; + let dh = if k == 0 { ph } else { out.ch }; + let dst = &mut out.planes[k]; + for r in 0..dh { + let s = &src[r.min(sh - 1) * stride..][..sw]; + let d = &mut dst[r * dw..(r + 1) * dw]; + d[..sw].copy_from_slice(s); + let last = s[sw - 1]; + d[sw..].fill(last); + } + } + Ok(out) + } + + /// quantiser_scale_code per macroblock: `base` scaled by each macroblock's activity + /// (H.262 Test Model 5 §5.2.3) with adaptive quantisation. + fn mb_quant(&self, src: &Frame, base: f64) -> Vec { + let n = self.mb_w * self.mb_h; + let nl = self.cfg.nonlinear_quant; + if !self.cfg.adaptive_quant { + return vec![qcode_for(nl, base); n]; + } + let acts: Vec = (0..n).map(|a| activity(src, a % self.mb_w, a / self.mb_w)).collect(); + let avg = acts.iter().sum::() / n.max(1) as f64; + acts.iter().map(|&a| qcode_for(nl, base * (2.0 * a + avg) / (a + 2.0 * avg))).collect() + } + + /// Encode one picture. Returns the packets that became ready (one per picture for now). + pub fn encode(&mut self, frame: &YuvFrame, pts: i64) -> Result, EncodeError> { + let src = self.load(frame)?; + let reference = if self.gop_pos == 0 { None } else { self.reference.take() }; + let intra = reference.is_none(); + if intra { + self.gop_pos = 0; + if let Some(rc) = self.rc.as_mut() { + rc.start_gop(self.cfg.gop_size); + } + } + let base = match (&self.rc, self.cfg.rate) { + (Some(rc), _) => rc.qscale(intra), + (None, RateControl::Quantiser(q)) => f64::from(qscale(self.cfg.nonlinear_quant, q)), + (None, _) => 8.0, + }; + let mb_qcode = self.mb_quant(&src, base); + let mut recon = Frame::new(src.w, src.h, src.cw, src.ch); + let ctx = PicCtx { + src: &src, + reference: reference.as_ref(), + prev_mvs: &self.prev_mvs, + mb_w: self.mb_w, + shx: self.shx, + shy: self.shy, + intra_matrix: &self.intra_matrix, + inter_matrix: &self.inter_matrix, + nonlinear: self.cfg.nonlinear_quant, + dc_prec: self.cfg.intra_dc_bits - 8, + f_code: self.f_code, + search: self.cfg.search_range as i32, + mb_qcode: &mb_qcode, + }; + let rows = encode_rows(&ctx, &mut recon, self.cfg.threads); + let mut w = BitWriter::new(); + if intra { + self.gop_header(&mut w); + } + self.picture_header(&mut w, intra); + let mut data = if intra { self.seq_header.clone() } else { Vec::new() }; + data.extend_from_slice(&w.finish()); + let mut qsum = 0u64; + let mut mvs = Vec::with_capacity(self.mb_w * self.mb_h); + for r in rows { + data.extend_from_slice(&r.bytes); + qsum += r.stats.qsum; + mvs.extend_from_slice(&r.mvs); + } + let avg_q = qsum as f64 / (self.mb_w * self.mb_h).max(1) as f64; + if let Some(rc) = self.rc.as_mut() { + rc.update(intra, data.len() as u64 * 8, avg_q); + } + if self.recon_capture { + self.recon.push(self.crop(&recon)); + } + self.reference = Some(recon); + self.prev_mvs = mvs; + self.count += 1; + self.gop_pos = (self.gop_pos + 1) % self.cfg.gop_size; + let picture_type = if intra { PictureType::I } else { PictureType::P }; + Ok(vec![Packet { data, pts, dts: pts, keyframe: intra, picture_type, quantiser_scale: avg_q as f32 }]) + } + + fn crop(&self, f: &Frame) -> ReconFrame { + let (w, h) = (self.cfg.width as usize, self.cfg.height as usize); + let (cw, ch) = (w >> self.shx, h >> self.shy); + let crop = |p: &[u8], stride: usize, w: usize, h: usize| (0..h).flat_map(|r| p[r * stride..r * stride + w].iter().copied()).collect(); + ReconFrame { + width: w as u32, + height: h as u32, + y: crop(&f.planes[0], f.w, w, h), + cb: crop(&f.planes[1], f.cw, cw, ch), + cr: crop(&f.planes[2], f.cw, cw, ch), + } + } +} + +/// Macroblock activity: one plus the smallest variance of its four luma blocks (TM5). +fn activity(f: &Frame, mbx: usize, mby: usize) -> f64 { + let mut min = f64::MAX; + for b in 0..4 { + let (x0, y0) = (mbx * 16 + (b & 1) * 8, mby * 16 + (b >> 1) * 8); + let (mut s, mut s2) = (0u64, 0u64); + for r in 0..8 { + for &v in &f.planes[0][(y0 + r) * f.w + x0..][..8] { + s += u64::from(v); + s2 += u64::from(v) * u64::from(v); + } + } + let mean = s as f64 / 64.0; + min = min.min(s2 as f64 / 64.0 - mean * mean); + } + 1.0 + min.max(0.0) +} + +/// Code every macroblock row of the picture into `recon`, in parallel when allowed. +fn encode_rows(ctx: &PicCtx, recon: &mut Frame, threads: bool) -> Vec { + let (w, cw) = (recon.w, recon.cw); + let clines = 16 >> ctx.shy; + let [y, cb, cr] = &mut recon.planes; + let run = |r: usize, y: &mut [u8], cb: &mut [u8], cr: &mut [u8]| encode_row(ctx, r, &mut RowPlanes { y, cb, cr }); + #[cfg(feature = "threads")] + if threads { + use rayon::prelude::*; + return y + .par_chunks_mut(16 * w) + .zip(cb.par_chunks_mut(clines * cw)) + .zip(cr.par_chunks_mut(clines * cw)) + .enumerate() + .map(|(r, ((y, cb), cr))| run(r, y, cb, cr)) + .collect(); + } + let _ = threads; + y.chunks_mut(16 * w) + .zip(cb.chunks_mut(clines * cw)) + .zip(cr.chunks_mut(clines * cw)) + .enumerate() + .map(|(r, ((y, cb), cr))| run(r, y, cb, cr)) + .collect() +} diff --git a/crates/video/src/mpeg2/encode/motion.rs b/crates/video/src/mpeg2/encode/motion.rs new file mode 100644 index 000000000..a7bedc80b --- /dev/null +++ b/crates/video/src/mpeg2/encode/motion.rs @@ -0,0 +1,191 @@ +//! Motion search for frame prediction: integer pattern search from predictor candidates, then +//! half-sample refinement. Costs are SAD + λ·(motion vector bits). + +use crate::mpeg2::mc::{Block, Frame, predict}; + +use super::tables::{enc_tables, wrap_delta}; + +pub(crate) struct Search<'a> { + pub reference: &'a Frame, + /// The source macroblock's luma (16×16). + pub src: &'a [u8; 256], + pub mbx: usize, + pub mby: usize, + pub f_code: u8, + /// Largest integer displacement searched, in samples. + pub range: i32, + /// Motion vector predictor (half-sample units). + pub pmv: [i32; 2], + pub lambda: u32, +} + +/// The chosen vector (half-sample units) and its SAD. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Found { + pub mv: [i32; 2], + pub sad: u32, +} + +/// Steps of the integer pattern search, coarse to fine. +const STEPS: [i32; 4] = [8, 4, 2, 1]; +/// Moves per step before moving on (bounds the search on pathological content). +const MAX_MOVES: usize = 16; + +impl Search<'_> { + fn origin(&self) -> (i32, i32) { + ((self.mbx * 16) as i32, (self.mby * 16) as i32) + } + + /// Integer displacements allowed per axis: inside the reference and the f_code range. + fn bounds(&self) -> ([i32; 2], [i32; 2]) { + let f = 1i32 << self.f_code.saturating_sub(1); + let (ox, oy) = self.origin(); + let (w, h) = (self.reference.w as i32, self.reference.h as i32); + let lo_lim = -(8 * f).min(self.range); + let hi_lim = (8 * f - 1).min(self.range); + ([lo_lim.max(-ox), lo_lim.max(-oy)], [hi_lim.min(w - 16 - ox), hi_lim.min(h - 16 - oy)]) + } + + /// A half-sample vector the bitstream can carry whose prediction stays inside the reference. + fn valid(&self, mv: [i32; 2]) -> bool { + let f = 1i32 << self.f_code.saturating_sub(1); + let (ox, oy) = self.origin(); + let size = [self.reference.w as i32, self.reference.h as i32]; + let o = [ox, oy]; + (0..2).all(|c| { + let start = o[c] + (mv[c] >> 1); + mv[c] >= -16 * f && mv[c] < 16 * f && start >= 0 && start + 16 + (mv[c] & 1) <= size[c] + }) + } + + fn mv_cost(&self, mv: [i32; 2]) -> u32 { + let t = enc_tables(); + let bits: u32 = (0..2).map(|c| t.motion_bits(wrap_delta(mv[c], self.pmv[c], self.f_code), self.f_code)).sum(); + self.lambda * bits + } + + fn sad_int(&self, d: [i32; 2]) -> u32 { + let (ox, oy) = self.origin(); + let stride = self.reference.w; + let x = (ox + d[0]) as usize; + let y = (oy + d[1]) as usize; + let plane = &self.reference.planes[0]; + let mut sad = 0u32; + for r in 0..16 { + let at = (y + r) * stride + x; + let Some(row) = plane.get(at..at + 16) else { return u32::MAX }; + for (a, b) in row.iter().zip(&self.src[r * 16..r * 16 + 16]) { + sad += u32::from(a.abs_diff(*b)); + } + } + sad + } + + fn sad_half(&self, mv: [i32; 2]) -> u32 { + let (ox, oy) = self.origin(); + let mut tmp = [0u8; 256]; + let blk = Block { x: ox + (mv[0] >> 1), y: oy + (mv[1] >> 1), hx: mv[0] & 1 != 0, hy: mv[1] & 1 != 0, bw: 16, bh: 16 }; + predict(&mut tmp, 0, 16, self.reference.plane(0, None), blk, false); + tmp.iter().zip(self.src.iter()).map(|(a, b)| u32::from(a.abs_diff(*b))).sum() + } + + /// Best vector among the candidates (half-sample units), refined. + pub fn run(&self, candidates: &[[i32; 2]]) -> Found { + let (lo, hi) = self.bounds(); + let clamp = |d: [i32; 2]| [d[0].clamp(lo[0], hi[0].max(lo[0])), d[1].clamp(lo[1], hi[1].max(lo[1]))]; + let eval = |d: [i32; 2]| { + let sad = self.sad_int(d); + (sad.saturating_add(self.mv_cost([2 * d[0], 2 * d[1]])), sad) + }; + let mut best_d = [0, 0]; + let (mut best_cost, mut best_sad) = eval(best_d); + for c in candidates { + let d = clamp([c[0] >> 1, c[1] >> 1]); + if d != best_d { + let (cost, sad) = eval(d); + if cost < best_cost { + (best_d, best_cost, best_sad) = (d, cost, sad); + } + } + } + for step in STEPS.into_iter().filter(|&s| s <= self.range.max(1)) { + for _ in 0..MAX_MOVES { + let mut moved = false; + for off in [[step, 0], [-step, 0], [0, step], [0, -step]] { + let d = clamp([best_d[0] + off[0], best_d[1] + off[1]]); + if d == best_d { + continue; + } + let (cost, sad) = eval(d); + if cost < best_cost { + (best_d, best_cost, best_sad) = (d, cost, sad); + moved = true; + } + } + if !moved { + break; + } + } + } + // half-sample refinement around the integer result + let centre = [2 * best_d[0], 2 * best_d[1]]; + let mut best = Found { mv: centre, sad: best_sad }; + for dy in -1..=1 { + for dx in -1..=1 { + let mv = [centre[0] + dx, centre[1] + dy]; + if (dx == 0 && dy == 0) || !self.valid(mv) { + continue; + } + let sad = self.sad_half(mv); + let cost = sad.saturating_add(self.mv_cost(mv)); + if cost < best_cost { + best_cost = cost; + best = Found { mv, sad }; + } + } + } + best + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A textured reference and a source macroblock cut from it at a known offset. + fn scene(dx: i32, dy: i32) -> (Frame, [u8; 256]) { + let mut f = Frame::new(64, 64, 32, 32); + for y in 0..64 { + for x in 0..64 { + let (x, y) = (x as f32, y as f32); + f.planes[0][y as usize * 64 + x as usize] = + (128.0 + 60.0 * (x * 0.31).sin() * (y * 0.23).cos() + 30.0 * ((x + y) * 0.13).sin()) as u8; + } + } + let mut src = [0u8; 256]; + for r in 0..16 { + for c in 0..16 { + src[r * 16 + c] = f.planes[0][(16 + dy + r as i32) as usize * 64 + (16 + dx + c as i32) as usize]; + } + } + (f, src) + } + + #[test] + fn finds_an_integer_displacement() { + let (f, src) = scene(3, -2); + let s = Search { reference: &f, src: &src, mbx: 1, mby: 1, f_code: 3, range: 15, pmv: [0, 0], lambda: 1 }; + let found = s.run(&[]); + assert_eq!(found, Found { mv: [6, -4], sad: 0 }); + } + + #[test] + fn stays_inside_the_reference() { + let (f, src) = scene(0, 0); + for (mbx, mby) in [(0, 0), (3, 3), (0, 3)] { + let s = Search { reference: &f, src: &src, mbx, mby, f_code: 4, range: 63, pmv: [0, 0], lambda: 0 }; + let found = s.run(&[[-200, -200], [200, 200]]); + assert!(s.valid(found.mv), "{found:?} at {mbx},{mby}"); + } + } +} diff --git a/crates/video/src/mpeg2/encode/picture.rs b/crates/video/src/mpeg2/encode/picture.rs new file mode 100644 index 000000000..e7bb84b7e --- /dev/null +++ b/crates/video/src/mpeg2/encode/picture.rs @@ -0,0 +1,412 @@ +//! Slices and macroblocks of one frame picture (H.262 §6.2.4-§6.2.6), with a reconstruction +//! that matches the decoder exactly. One slice per macroblock row; slices only depend on the +//! reference picture, so rows can be coded in parallel. + +use crate::mpeg2::headers::SCAN; +use crate::mpeg2::mc::{Block, Frame, predict}; +use crate::mpeg2::slice::NON_LINEAR_Q; +use crate::mpeg2::vlc::{MB_FWD, MB_INTRA, MB_PATTERN, MB_QUANT}; + +use super::bitwriter::BitWriter; +use super::motion::Search; +use super::tables::{EncTables, enc_tables, wrap_delta}; +use super::transform::{fdct, quant_inter, quant_intra, reconstruct}; + +/// Intra coding is chosen when its cost (the luma's deviation from its block means) undercuts +/// the best prediction's SAD by this much. +const INTRA_BIAS: u32 = 256; + +/// quantiser_scale for quantiser_scale_code `code` (Table 7-6 when `nonlinear`). +pub(crate) fn qscale(nonlinear: bool, code: u8) -> i32 { + if nonlinear { i32::from(NON_LINEAR_Q[usize::from(code & 31)]) } else { 2 * i32::from(code & 31) } +} + +/// The quantiser_scale_code (1..=31) whose quantiser_scale is closest to `qs`. +pub(crate) fn qcode_for(nonlinear: bool, qs: f64) -> u8 { + let mut best = (1u8, f64::MAX); + for code in 1..=31u8 { + let d = (f64::from(qscale(nonlinear, code)) - qs).abs(); + if d < best.1 { + best = (code, d); + } + } + best.0 +} + +/// What every slice of one picture shares (read-only). +pub(crate) struct PicCtx<'a> { + /// The padded source picture. + pub src: &'a Frame, + /// The reconstructed previous picture (P pictures); `None` codes an I picture. + pub reference: Option<&'a Frame>, + /// Vectors of the previous picture (motion search candidates), one per macroblock. + pub prev_mvs: &'a [[i32; 2]], + pub mb_w: usize, + pub shx: u32, + pub shy: u32, + pub intra_matrix: &'a [u8; 64], + pub inter_matrix: &'a [u8; 64], + pub nonlinear: bool, + /// intra_dc_precision (0..=2: 8..=10 bits). + pub dc_prec: u8, + pub f_code: u8, + pub search: i32, + /// quantiser_scale_code per macroblock. + pub mb_qcode: &'a [u8], +} + +impl PicCtx<'_> { + fn cmb(&self) -> (usize, usize) { + (16 >> self.shx, 16 >> self.shy) + } + fn blocks(&self) -> usize { + if self.shy == 1 { 6 } else { 8 } + } + fn dc_reset(&self) -> i32 { + 1 << (7 + self.dc_prec) + } +} + +/// The part of the reconstructed picture one macroblock row covers. +pub(crate) struct RowPlanes<'b> { + pub y: &'b mut [u8], + pub cb: &'b mut [u8], + pub cr: &'b mut [u8], +} + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct RowStats { + pub intra: u32, + pub skipped: u32, + /// Sum of the quantiser_scale of every macroblock. + pub qsum: u64, +} + +pub(crate) struct RowOut { + /// The slice, from its start code, byte aligned. + pub bytes: Vec, + /// The vector each macroblock used (zero for intra and skipped ones). + pub mvs: Vec<[i32; 2]>, + pub stats: RowStats, +} + +/// One macroblock's samples: luma 16×16, chroma rows of `16 >> shx`. +#[derive(Clone)] +struct MbPix { + y: [u8; 256], + c: [[u8; 256]; 2], +} + +impl MbPix { + fn load(f: &Frame, mbx: usize, mby: usize, (cbw, cbh): (usize, usize)) -> MbPix { + let mut m = MbPix { y: [0; 256], c: [[0; 256]; 2] }; + for r in 0..16 { + let at = (mby * 16 + r) * f.w + mbx * 16; + m.y[r * 16..r * 16 + 16].copy_from_slice(&f.planes[0][at..at + 16]); + } + for c in 0..2 { + for r in 0..cbh { + let at = (mby * cbh + r) * f.cw + mbx * cbw; + m.c[c][r * cbw..r * cbw + cbw].copy_from_slice(&f.planes[1 + c][at..at + cbw]); + } + } + m + } + + /// Plane, offset and stride of block `i` (H.262 Figure 6-10 order: four luma blocks, then + /// Cb, Cr, and for 4:2:2 the lower Cb, Cr). + fn block_at(i: usize, cbw: usize) -> (usize, usize, usize) { + if i < 4 { (0, (i >> 1) * 8 * 16 + (i & 1) * 8, 16) } else { (1 + (i & 1), ((i - 4) >> 1) * 8 * cbw, cbw) } + } + + fn plane(&self, p: usize) -> &[u8; 256] { + if p == 0 { &self.y } else { &self.c[p - 1] } + } + + fn plane_mut(&mut self, p: usize) -> &mut [u8; 256] { + if p == 0 { &mut self.y } else { &mut self.c[p - 1] } + } + + fn block(&self, i: usize, cbw: usize) -> [i32; 64] { + let (p, off, stride) = Self::block_at(i, cbw); + let d = self.plane(p); + std::array::from_fn(|k| i32::from(d[off + (k >> 3) * stride + (k & 7)])) + } + + /// Write `v` (intra samples) or add it to what is there (inter residual), saturated. + fn store(&mut self, i: usize, cbw: usize, v: &[i32; 64], add: bool) { + let (p, off, stride) = Self::block_at(i, cbw); + let d = self.plane_mut(p); + for (k, &x) in v.iter().enumerate() { + let at = off + (k >> 3) * stride + (k & 7); + let base = if add { i32::from(d[at]) } else { 0 }; + d[at] = (base + x).clamp(0, 255) as u8; + } + } + + fn put(&self, out: &mut RowPlanes, mbx: usize, (cbw, cbh): (usize, usize), (w, cw): (usize, usize)) { + for r in 0..16 { + let at = r * w + mbx * 16; + if let Some(d) = out.y.get_mut(at..at + 16) { + d.copy_from_slice(&self.y[r * 16..r * 16 + 16]); + } + } + for (c, plane) in [&mut *out.cb, &mut *out.cr].into_iter().enumerate() { + for r in 0..cbh { + let at = r * cw + mbx * cbw; + if let Some(d) = plane.get_mut(at..at + cbw) { + d.copy_from_slice(&self.c[c][r * cbw..r * cbw + cbw]); + } + } + } + } +} + +/// Slice coding state (what the decoder tracks too). +struct SliceSt { + qcode: u8, + dc: [i32; 3], + pmv: [i32; 2], + /// Skipped macroblocks not yet covered by an address increment. + skipped: usize, + /// The vector of the macroblock to the left (motion search candidate). + left_mv: [i32; 2], +} + +/// One macroblock being coded. +struct MbIn<'x> { + src: &'x MbPix, + mbx: usize, + mby: usize, + qcode: u8, +} + +/// Code macroblock row `row` as one slice, writing its reconstruction into `out`. +pub(crate) fn encode_row(ctx: &PicCtx, row: usize, out: &mut RowPlanes) -> RowOut { + let mut w = BitWriter::new(); + w.start_code(1 + row as u8); + let q0 = ctx.mb_qcode.get(row * ctx.mb_w).copied().unwrap_or(1); + w.put(u32::from(q0), 5); + w.bit(false); // extra_bit_slice + let mut st = SliceSt { qcode: q0, dc: [ctx.dc_reset(); 3], pmv: [0; 2], skipped: 0, left_mv: [0; 2] }; + let mut ro = RowOut { bytes: Vec::new(), mvs: vec![[0; 2]; ctx.mb_w], stats: RowStats::default() }; + for mbx in 0..ctx.mb_w { + macroblock(ctx, &mut st, &mut w, (mbx, row), out, &mut ro); + } + ro.bytes = w.finish(); + ro +} + +fn macroblock(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, (mbx, mby): (usize, usize), out: &mut RowPlanes, ro: &mut RowOut) { + let addr = mby * ctx.mb_w + mbx; + let qcode = ctx.mb_qcode.get(addr).copied().unwrap_or(st.qcode); + let src = MbPix::load(ctx.src, mbx, mby, ctx.cmb()); + let mb = MbIn { src: &src, mbx, mby, qcode }; + let qs = qscale(ctx.nonlinear, qcode); + ro.stats.qsum += qs as u64; + let planes = (ctx.src.w, ctx.src.cw); + let Some(reference) = ctx.reference else { + intra_mb(ctx, st, w, &mb).put(out, mbx, ctx.cmb(), planes); + ro.stats.intra += 1; + return; + }; + let mut candidates = vec![st.left_mv, st.pmv]; + for a in [Some(addr), addr.checked_sub(ctx.mb_w), Some(addr + 1), Some(addr + ctx.mb_w)].into_iter().flatten() { + if let Some(&v) = ctx.prev_mvs.get(a) { + candidates.push(v); + } + } + let lambda = ((qs * 3 + 4) / 8).max(1) as u32; + let search = Search { reference, src: &src.y, mbx, mby, f_code: ctx.f_code, range: ctx.search, pmv: st.pmv, lambda }; + let found = search.run(&candidates); + if intra_cost(&src.y).saturating_add(INTRA_BIAS) < found.sad { + intra_mb(ctx, st, w, &mb).put(out, mbx, ctx.cmb(), planes); + ro.stats.intra += 1; + st.left_mv = [0; 2]; + return; + } + let (rec, skipped) = inter_mb(ctx, st, w, &mb, reference, found.mv); + rec.put(out, mbx, ctx.cmb(), planes); + ro.stats.skipped += skipped as u32; + ro.mvs[mbx] = found.mv; + st.left_mv = found.mv; +} + +/// Σ |sample − block mean| over the four luma blocks: the cost of coding them intra. +fn intra_cost(y: &[u8; 256]) -> u32 { + let mut cost = 0u32; + for b in 0..4 { + let off = (b >> 1) * 128 + (b & 1) * 8; + let at = |k: usize| u32::from(y[off + (k >> 3) * 16 + (k & 7)]); + let mean = ((0..64).map(at).sum::() + 32) / 64; + cost += (0..64).map(|k| at(k).abs_diff(mean)).sum::(); + } + cost +} + +fn put_increment(w: &mut BitWriter, t: &EncTables, mut n: usize) { + while n > 33 { + t.mba_escape.put(w); + n -= 33; + } + t.mba[n.max(1)].put(w); +} + +/// Write the coefficients of a block (raster-order levels) in zigzag order from `start`, then +/// end of block. +fn put_coefficients(w: &mut BitWriter, t: &EncTables, levels: &[i32; 64], table: usize, start: usize) { + let mut run = 0; + let mut first = start == 0; + for &pos in &SCAN[0][start..] { + let l = levels[usize::from(pos)]; + if l == 0 { + run += 1; + continue; + } + t.put_coefficient(w, table, run, l, first); + first = false; + run = 0; + } + t.dct_eob[table].put(w); +} + +fn put_mb_type(w: &mut BitWriter, t: &EncTables, predicted: bool, flags: i16) { + let table = if predicted { &t.mbtype_p } else { &t.mbtype_i }; + if let Some(c) = table.get(flags as usize).copied().flatten() { + c.put(w); + } +} + +/// Code an intra macroblock; returns its reconstruction. +fn intra_mb(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, mb: &MbIn) -> MbPix { + let t = enc_tables(); + let quant = mb.qcode != st.qcode; + put_increment(w, t, st.skipped + 1); + st.skipped = 0; + put_mb_type(w, t, ctx.reference.is_some(), if quant { MB_INTRA | MB_QUANT } else { MB_INTRA }); + if quant { + w.put(u32::from(mb.qcode), 5); + st.qcode = mb.qcode; + } + st.pmv = [0; 2]; + let qs = qscale(ctx.nonlinear, st.qcode); + let dc_mult = 8 >> ctx.dc_prec; + let dc_max = (1 << (8 + ctx.dc_prec)) - 1; + let (cbw, _) = ctx.cmb(); + let mut rec = mb.src.clone(); + for i in 0..ctx.blocks() { + let cc = if i < 4 { 0 } else { 1 + (i & 1) }; + let f = fdct(&mb.src.block(i, cbw)); + let mut levels = [0i32; 64]; + levels[0] = ((f[0] / dc_mult as f32).round() as i32).clamp(0, dc_max); + for pos in 1..64 { + levels[pos] = quant_intra(f[pos], ctx.intra_matrix[pos], qs); + } + let diff = levels[0] - st.dc[cc]; + st.dc[cc] = levels[0]; + let size = 32 - diff.unsigned_abs().leading_zeros(); + let size_codes = if cc == 0 { &t.dc_luma } else { &t.dc_chroma }; + size_codes[size as usize].put(w); + if size > 0 { + let v = if diff > 0 { diff } else { diff + (1 << size) - 1 }; + w.put(v as u32, size); + } + put_coefficients(w, t, &levels, 1, 1); + rec.store(i, cbw, &reconstruct(&levels, ctx.intra_matrix, qs, true, dc_mult), false); + } + rec +} + +/// Code a predicted macroblock with vector `mv` (or skip it); returns its reconstruction and +/// whether it was skipped. +fn inter_mb(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, mb: &MbIn, reference: &Frame, mv: [i32; 2]) -> (MbPix, bool) { + let t = enc_tables(); + let mut rec = predict_mb(ctx, reference, mv, mb.mbx, mb.mby); + let qs = qscale(ctx.nonlinear, mb.qcode); + let (cbw, _) = ctx.cmb(); + let n = ctx.blocks(); + let mut levels = [[0i32; 64]; 8]; + let mut cbp = 0u32; + for i in 0..n { + let (s, p) = (mb.src.block(i, cbw), rec.block(i, cbw)); + let f = fdct(&std::array::from_fn(|k| s[k] - p[k])); + for pos in 0..64 { + levels[i][pos] = quant_inter(f[pos], ctx.inter_matrix[pos], qs); + } + if levels[i].iter().any(|&l| l != 0) { + cbp |= 1 << (n - 1 - i); + } + } + st.dc = [ctx.dc_reset(); 3]; + // the first and last macroblock of a slice are always coded + let edge = mb.mbx == 0 || mb.mbx + 1 == ctx.mb_w; + if mv == [0, 0] && cbp == 0 && !edge { + st.skipped += 1; + st.pmv = [0; 2]; + return (rec, true); + } + put_increment(w, t, st.skipped + 1); + st.skipped = 0; + let quant = cbp != 0 && mb.qcode != st.qcode; + let mut flags = if cbp == 0 { + MB_FWD + } else if mv == [0, 0] { + MB_PATTERN + } else { + MB_FWD | MB_PATTERN + }; + if quant { + flags |= MB_QUANT; + } + put_mb_type(w, t, true, flags); + if quant { + w.put(u32::from(mb.qcode), 5); + st.qcode = mb.qcode; + } + if flags & MB_FWD != 0 { + for c in 0..2 { + t.put_motion(w, wrap_delta(mv[c], st.pmv[c], ctx.f_code), ctx.f_code); + } + st.pmv = mv; + } else { + st.pmv = [0; 2]; // "No MC" resets the predictors + } + if cbp != 0 { + if n == 8 { + t.cbp[(cbp >> 2) as usize].put(w); + w.put(cbp & 3, 2); + } else { + t.cbp[cbp as usize].put(w); + } + for i in 0..n { + if cbp & (1 << (n - 1 - i)) != 0 { + put_coefficients(w, t, &levels[i], 0, 0); + rec.store(i, cbw, &reconstruct(&levels[i], ctx.inter_matrix, qs, false, 0), true); + } + } + } + (rec, false) +} + +/// The frame prediction of a macroblock from `f` with vector `mv`, formed as the decoder forms +/// it (§7.6.4: chroma vectors are the luma ones halved towards zero). +fn predict_mb(ctx: &PicCtx, f: &Frame, mv: [i32; 2], mbx: usize, mby: usize) -> MbPix { + let (cbw, cbh) = ctx.cmb(); + let mut p = MbPix { y: [0; 256], c: [[0; 256]; 2] }; + let blk = + Block { x: (mbx * 16) as i32 + (mv[0] >> 1), y: (mby * 16) as i32 + (mv[1] >> 1), hx: mv[0] & 1 != 0, hy: mv[1] & 1 != 0, bw: 16, bh: 16 }; + predict(&mut p.y, 0, 16, f.plane(0, None), blk, false); + let cmv = [if ctx.shx == 1 { mv[0] / 2 } else { mv[0] }, if ctx.shy == 1 { mv[1] / 2 } else { mv[1] }]; + let cb = Block { + x: (mbx * cbw) as i32 + (cmv[0] >> 1), + y: (mby * cbh) as i32 + (cmv[1] >> 1), + hx: cmv[0] & 1 != 0, + hy: cmv[1] & 1 != 0, + bw: cbw, + bh: cbh, + }; + for c in 0..2 { + predict(&mut p.c[c], 0, cbw, f.plane(1 + c, None), cb, false); + } + p +} diff --git a/crates/video/src/mpeg2/encode/rate.rs b/crates/video/src/mpeg2/encode/rate.rs new file mode 100644 index 000000000..65c23efc3 --- /dev/null +++ b/crates/video/src/mpeg2/encode/rate.rs @@ -0,0 +1,102 @@ +//! Single-pass average bitrate control at picture level, after the target bit allocation of the +//! MPEG-2 Test Model 5: each picture type keeps a complexity `X = bits · average quantiser`, the +//! bits left for the GOP are shared out by complexity, and a picture's quantiser is `X / target`. +//! There is no VBV model: the average converges on the target, single pictures may not. + +pub(crate) struct RateCtl { + bps: f64, + fps: f64, + /// Complexity of I and P pictures. + xi: f64, + xp: f64, + /// Bits left for the current GOP (carries over between GOPs). + remaining: f64, + /// P pictures left in the current GOP. + np: u32, + /// Range of quantiser_scale values. + q_min: f64, + q_max: f64, +} + +impl RateCtl { + pub fn new(bps: f64, fps: f64, q_range: (f64, f64)) -> Self { + // TM5's starting complexities + Self { bps, fps, xi: 160.0 * bps / 115.0, xp: 60.0 * bps / 115.0, remaining: 0.0, np: 0, q_min: q_range.0, q_max: q_range.1 } + } + + /// A GOP of `pictures` pictures (one I, the rest P) starts. + pub fn start_gop(&mut self, pictures: u32) { + self.remaining += self.bps * f64::from(pictures) / self.fps; + self.np = pictures.saturating_sub(1); + } + + /// quantiser_scale for the next picture. + pub fn qscale(&self, intra: bool) -> f64 { + let floor = self.bps / (8.0 * self.fps); + let (target, x) = if intra { + (self.remaining / (1.0 + f64::from(self.np) * self.xp / self.xi), self.xi) + } else { + (self.remaining / f64::from(self.np.max(1)), self.xp) + }; + (x / target.max(floor)).clamp(self.q_min, self.q_max) + } + + /// The picture used `bits` at average quantiser_scale `avg_q`. + pub fn update(&mut self, intra: bool, bits: u64, avg_q: f64) { + let x = bits as f64 * avg_q; + if intra { + self.xi = x.max(1.0); + } else { + self.xp = x.max(1.0); + self.np = self.np.saturating_sub(1); + } + self.remaining -= bits as f64; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A picture whose size is `complexity / q` (the model the controller assumes). + fn run(complexity_i: f64, complexity_p: f64, bps: f64, gops: u32) -> f64 { + let fps = 25.0; + let mut rc = RateCtl::new(bps, fps, (1.0, 112.0)); + let mut total = 0.0; + for _ in 0..gops { + rc.start_gop(12); + for k in 0..12 { + let intra = k == 0; + let q = rc.qscale(intra); + let bits = (if intra { complexity_i } else { complexity_p }) / q; + rc.update(intra, bits as u64, q); + total += bits; + } + } + total * fps / (12.0 * f64::from(gops)) + } + + #[test] + fn converges_on_the_target() { + // complexities whose target quantiser is inside the allowed range + for (ci, cp) in [(4e7, 1e7), (8e7, 2e6), (1e7, 9e6)] { + let got = run(ci, cp, 4e6, 20); + assert!((got / 4e6 - 1.0).abs() < 0.03, "{ci} {cp}: {got}"); + } + } + + #[test] + fn the_quantiser_stays_in_range() { + let mut rc = RateCtl::new(1e6, 25.0, (2.0, 62.0)); + rc.start_gop(2); + // a picture far larger than its budget pushes the next quantiser to the top + rc.update(true, 50_000_000, 10.0); + rc.start_gop(2); + assert_eq!(rc.qscale(true), 62.0); + // and a tiny one to the bottom + let mut rc = RateCtl::new(1e6, 25.0, (2.0, 62.0)); + rc.start_gop(2); + rc.update(true, 10, 2.0); + assert_eq!(rc.qscale(true), 2.0); + } +} diff --git a/crates/video/src/mpeg2/encode/tables.rs b/crates/video/src/mpeg2/encode/tables.rs new file mode 100644 index 000000000..b79dd92ea --- /dev/null +++ b/crates/video/src/mpeg2/encode/tables.rs @@ -0,0 +1,264 @@ +//! Encoder-side variable-length codes, built from the decoder's code lists (H.262 Annex B) so the +//! two cannot disagree. + +use std::sync::OnceLock; + +use crate::mpeg2::vlc::{ + CBP, DC_CHROMA, DC_LUMA, DCT_EOB, DCT_ESCAPE, DCT_ONE, DCT_ZERO, DCT_ZERO_LONG, MBA, MBA_ESCAPE, MBTYPE_I, MBTYPE_P, MOTION, dct_table, + parse_code, +}; + +use super::bitwriter::BitWriter; + +/// A code: `len` bits of `bits`, most significant first. +#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)] +pub(crate) struct Code { + pub bits: u32, + pub len: u8, +} + +impl Code { + fn of(s: &str) -> Code { + let (bits, len) = parse_code(s); + Code { bits, len: len as u8 } + } + + #[inline(always)] + pub fn put(self, w: &mut BitWriter) { + w.put(self.bits, u32::from(self.len)); + } +} + +/// Longest run with a table code (table zero and one: run 31 at level 1). +const MAX_RUN: usize = 31; +/// Largest level with a table code (run 0: level 40). +const MAX_LEVEL: usize = 40; + +pub(crate) struct EncTables { + /// macroblock_address_increment 1..=33 (index 0 unused) and the escape. + pub mba: [Code; 34], + pub mba_escape: Code, + /// macroblock_type by flags: I pictures, P pictures (index: flags, 0..32). + pub mbtype_i: [Option; 32], + pub mbtype_p: [Option; 32], + /// coded_block_pattern_420 (the first six blocks). + pub cbp: [Code; 64], + /// motion_code -16..=16 at index code + 16. + pub motion: [Code; 33], + pub dc_luma: [Code; 12], + pub dc_chroma: [Code; 12], + /// DCT coefficients `[table][run][level]` without the sign bit (table 0: B.14, table 1: B.15). + pub dct: [Box<[[Option; MAX_LEVEL + 1]; MAX_RUN + 1]>; 2], + pub dct_eob: [Code; 2], + pub dct_escape: Code, +} + +fn by_value(list: &[(&str, i16)], offset: i16) -> [Code; N] { + let mut out = [Code::default(); N]; + for &(s, v) in list { + if let Some(slot) = usize::try_from(v + offset).ok().and_then(|i| out.get_mut(i)) { + *slot = Code::of(s); + } + } + out +} + +fn mb_types(list: &[(&str, i16)]) -> [Option; 32] { + let mut out = [None; 32]; + for &(s, v) in list { + if let Some(slot) = usize::try_from(v).ok().and_then(|i| out.get_mut(i)) { + *slot = Some(Code::of(s)); + } + } + out +} + +fn dct( + short: &'static [(&'static str, i16)], + extra: &'static [(&'static str, i16)], +) -> (Box<[[Option; MAX_LEVEL + 1]; MAX_RUN + 1]>, Code, Code) { + let mut t = Box::new([[None; MAX_LEVEL + 1]; MAX_RUN + 1]); + let (mut eob, mut esc) = (Code::default(), Code::default()); + for (s, v) in dct_table(short, extra) { + match v { + DCT_EOB => eob = Code::of(s), + DCT_ESCAPE => esc = Code::of(s), + v => { + let (run, level) = ((v & 63) as usize, (v >> 6) as usize); + if let Some(slot) = t.get_mut(run).and_then(|r| r.get_mut(level)) { + *slot = Some(Code::of(s)); + } + } + } + } + (t, eob, esc) +} + +pub(crate) fn enc_tables() -> &'static EncTables { + static T: OnceLock = OnceLock::new(); + T.get_or_init(|| { + let (zero, eob0, esc) = dct(DCT_ZERO, DCT_ZERO_LONG); + let (one, eob1, _) = dct(DCT_ONE, &[]); + let mba_escape = MBA.iter().find(|e| e.1 == MBA_ESCAPE).map_or(Code::default(), |e| Code::of(e.0)); + EncTables { + mba: by_value(MBA, 0), + mba_escape, + mbtype_i: mb_types(MBTYPE_I), + mbtype_p: mb_types(MBTYPE_P), + cbp: by_value(CBP, 0), + motion: by_value(MOTION, 16), + dc_luma: by_value(DC_LUMA, 0), + dc_chroma: by_value(DC_CHROMA, 0), + dct: [zero, one], + dct_eob: [eob0, eob1], + dct_escape: esc, + } + }) +} + +impl EncTables { + /// One DCT coefficient. `first_inter`: the first coefficient of a non-intra block, where + /// run 0 level ±1 is "1s". + #[inline] + pub fn put_coefficient(&self, w: &mut BitWriter, table: usize, run: usize, level: i32, first_inter: bool) { + let mag = level.unsigned_abs() as usize; + let neg = level < 0; + if first_inter && run == 0 && mag == 1 { + w.put(0b10 | neg as u32, 2); + return; + } + if let Some(c) = self.dct[table & 1].get(run).and_then(|r| r.get(mag)).copied().flatten() { + c.put(w); + w.bit(neg); + return; + } + // escape: 6-bit run, 12-bit two's complement level (-2047..=2047) + self.dct_escape.put(w); + w.put(run as u32, 6); + w.put((level.clamp(-2047, 2047) & 0xFFF) as u32, 12); + } + + /// Bits of one motion vector component delta (`delta` already in the wrapped range). + #[inline] + pub fn motion_bits(&self, delta: i32, f_code: u8) -> u32 { + let r_size = u32::from(f_code.saturating_sub(1)); + if delta == 0 { + return u32::from(self.motion[16].len); + } + let code = ((delta.abs() - 1) >> r_size) + 1; + let idx = (16 + code.min(16)) as usize; + u32::from(self.motion[idx].len) + r_size + } + + /// Write one motion vector component delta (`delta` in the wrapped range). + #[inline] + pub fn put_motion(&self, w: &mut BitWriter, delta: i32, f_code: u8) { + let r_size = u32::from(f_code.saturating_sub(1)); + if delta == 0 { + self.motion[16].put(w); + return; + } + let a = delta.abs() - 1; + let code = ((a >> r_size) + 1).min(16); + let idx = (16 + code * delta.signum()) as usize; + self.motion[idx].put(w); + w.put((a & ((1 << r_size) - 1)) as u32, r_size); + } +} + +/// `mv - pred` brought into the range a motion code can carry ([-16f, 16f-1], §7.6.3.1). +#[inline] +pub(crate) fn wrap_delta(mv: i32, pred: i32, f_code: u8) -> i32 { + let f = 1i32 << f_code.saturating_sub(1); + let d = mv - pred; + if d > 16 * f - 1 { + d - 32 * f + } else if d < -16 * f { + d + 32 * f + } else { + d + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::mpeg2::bits::Bits; + use crate::mpeg2::slice::wrap_vector; + use crate::mpeg2::vlc::{MB_FWD, MB_INTRA, MB_PATTERN, MB_QUANT, rl, tables}; + + /// Macroblock types the encoder sends (frame pictures, frame prediction, no B pictures). + const ENC_MB_TYPES: [i16; 7] = + [MB_INTRA, MB_INTRA | MB_QUANT, MB_FWD | MB_PATTERN, MB_PATTERN, MB_FWD, MB_QUANT | MB_FWD | MB_PATTERN, MB_QUANT | MB_PATTERN]; + + #[test] + fn every_code_decodes_to_its_value() { + let t = enc_tables(); + let d = tables(); + let check = |c: Code, vlc: &crate::mpeg2::vlc::Vlc, v: i16| { + let mut w = BitWriter::new(); + c.put(&mut w); + let bytes = w.finish(); + let mut b = Bits::new(&bytes); + assert_eq!(vlc.decode(&mut b), Some(v)); + }; + for n in 1..=33 { + check(t.mba[n], &d.mba, n as i16); + } + check(t.mba_escape, &d.mba, MBA_ESCAPE); + for v in 0..64 { + check(t.cbp[v], &d.cbp, v as i16); + } + for v in -16..=16i16 { + check(t.motion[(v + 16) as usize], &d.motion, v); + } + for s in 0..12 { + check(t.dc_luma[s], &d.dc_luma, s as i16); + check(t.dc_chroma[s], &d.dc_chroma, s as i16); + } + for f in ENC_MB_TYPES { + // the I table holds only the intra types + if let Some(c) = t.mbtype_i[f as usize] { + check(c, &d.mbtype[0], f); + } + check(t.mbtype_p[f as usize].unwrap_or_default(), &d.mbtype[1], f); + } + for (k, vlc) in [&d.dct_zero, &d.dct_one].into_iter().enumerate() { + for run in 0..=MAX_RUN { + for level in 1..=MAX_LEVEL { + if let Some(c) = t.dct[k][run][level] { + check(c, vlc, rl(run as i16, level as i16)); + } + } + } + check(t.dct_eob[k], vlc, DCT_EOB); + } + } + + #[test] + fn motion_deltas_round_trip_through_the_decoder_wrap() { + let t = enc_tables(); + let d = tables(); + for f_code in 1..=4u8 { + let f = 1i32 << (f_code - 1); + for pred in [-16 * f, -3, 0, 5, 16 * f - 1] { + for mv in -16 * f..16 * f { + let delta = wrap_delta(mv, pred, f_code); + let mut w = BitWriter::new(); + t.put_motion(&mut w, delta, f_code); + let bits = w.bits() as u32; + assert_eq!(bits, t.motion_bits(delta, f_code)); + let bytes = w.finish(); + let mut b = Bits::new(&bytes); + // the decoder's reading of motion_code and motion_residual + let code = d.motion.decode(&mut b).unwrap_or(99) as i32; + let r_size = (f_code - 1) as u32; + let residual = if f_code != 1 && code != 0 { b.read(r_size) as i32 } else { 0 }; + let dd = if f == 1 || code == 0 { code } else { ((code.abs() - 1) * f + residual + 1) * code.signum() }; + assert_eq!(dd, delta); + assert_eq!(wrap_vector(pred, dd, f), mv, "f_code {f_code} pred {pred} mv {mv}"); + } + } + } + } +} diff --git a/crates/video/src/mpeg2/encode/tests.rs b/crates/video/src/mpeg2/encode/tests.rs new file mode 100644 index 000000000..5352161b3 --- /dev/null +++ b/crates/video/src/mpeg2/encode/tests.rs @@ -0,0 +1,250 @@ +//! Encoder tests against this crate's decoder: the decoded pictures must equal the encoder's +//! reconstruction exactly (any difference would drift along a GOP of P pictures). The ffmpeg +//! comparison is in `tests/mpeg2/encode.rs`. + +use super::*; +use crate::mpeg2::{Decoder, access_units, probe}; + +/// A moving, textured test sequence: a gradient with a sharp pattern drifting by a fraction of a +/// sample per picture, and a block moving across it. +pub(crate) fn scene(w: usize, h: usize, cw: usize, ch: usize, t: usize) -> (Vec, Vec, Vec) { + let tf = t as f32; + let mut y = vec![0u8; w * h]; + for r in 0..h { + for c in 0..w { + let (x, yy) = (c as f32 + tf * 1.5, r as f32 + tf * 0.75); + let v = 60.0 + + 0.3 * x + + 0.2 * yy + + 40.0 * ((x * 0.21).sin() * (yy * 0.17).cos()) + + if ((x as i32 / 8) + (yy as i32 / 8)) % 2 == 0 { 25.0 } else { 0.0 }; + y[r * w + c] = v.clamp(0.0, 255.0) as u8; + } + } + let (bx, by) = ((t * 3) % w.max(1), (t * 2) % h.max(1)); + for r in by..(by + 12).min(h) { + for c in bx..(bx + 12).min(w) { + y[r * w + c] = 230; + } + } + let mut cb = vec![0u8; cw * ch]; + let mut cr = vec![0u8; cw * ch]; + for r in 0..ch { + for c in 0..cw { + cb[r * cw + c] = (128.0 + 50.0 * ((c as f32 + tf) * 0.13).sin()) as u8; + cr[r * cw + c] = (128.0 + 40.0 * ((r as f32 - tf) * 0.11).cos()) as u8; + } + } + (y, cb, cr) +} + +/// Encode `frames` pictures of the scene; returns the stream and the reconstruction. +fn encode(cfg: EncoderConfig, frames: usize) -> (Vec, Vec, Vec) { + let (w, h) = (cfg.width as usize, cfg.height as usize); + let (cw, ch) = (w / 2, if cfg.chroma == ChromaFormat::Yuv422 { h } else { h / 2 }); + let mut enc = Encoder::new(cfg).unwrap(); + enc.set_recon_capture(true); + let mut es = Vec::new(); + let mut packets = Vec::new(); + for t in 0..frames { + let (y, cb, cr) = scene(w, h, cw, ch, t); + let f = YuvFrame { y: &y, cb: &cb, cr: &cr, y_stride: w, c_stride: cw }; + for p in enc.encode(&f, t as i64).unwrap() { + es.extend_from_slice(&p.data); + packets.push(p); + } + } + packets.extend(enc.flush()); + es.extend_from_slice(enc.end_of_sequence()); + (es, enc.take_recon(), packets) +} + +fn decode(es: &[u8]) -> Vec { + let mut d = Decoder::new(); + let mut out = Vec::new(); + for (i, r) in access_units(es).into_iter().enumerate() { + out.extend(d.decode(&es[r], i as i64).unwrap()); + } + out.extend(d.flush()); + assert_eq!(d.errors(), 0, "decoder errors: {:?}", d.last_error()); + out +} + +fn assert_decodes_to_recon(name: &str, cfg: EncoderConfig, frames: usize) -> Vec { + let (es, recon, packets) = encode(cfg, frames); + let pics = decode(&es); + assert_eq!(pics.len(), frames, "{name}: picture count"); + for (i, (p, r)) in pics.iter().zip(&recon).enumerate() { + assert_eq!(p.pts, i as i64, "{name}: display order"); + for (plane, (a, b)) in [("Y", (&p.y, &r.y)), ("Cb", (&p.cb, &r.cb)), ("Cr", (&p.cr, &r.cr))] { + let first = a.iter().zip(b.iter()).position(|(x, y)| x != y); + assert!(first.is_none() && a.len() == b.len(), "{name}: picture {i} ({:?}) {plane} differs at {first:?}", p.picture_type); + } + } + packets +} + +fn psnr(a: &[u8], b: &[u8]) -> f64 { + let sse: f64 = a.iter().zip(b).map(|(x, y)| (f64::from(*x) - f64::from(*y)).powi(2)).sum(); + 10.0 * (255.0f64.powi(2) * a.len() as f64 / sse.max(1e-9)).log10() +} + +#[test] +fn intra_only_decodes_to_the_reconstruction() { + let mut cfg = EncoderConfig::new(64, 48, 25, 1); + cfg.gop_size = 1; + let packets = assert_decodes_to_recon("intra", cfg, 3); + assert!(packets.iter().all(|p| p.keyframe && p.picture_type == PictureType::I)); +} + +#[test] +fn long_p_gop_does_not_drift() { + let mut cfg = EncoderConfig::new(96, 64, 25, 1); + cfg.gop_size = 30; + let packets = assert_decodes_to_recon("long gop", cfg, 30); + assert_eq!(packets.iter().filter(|p| p.keyframe).count(), 1); + assert!(packets[1..].iter().all(|p| p.picture_type == PictureType::P)); +} + +#[test] +fn every_coding_option_decodes_to_the_reconstruction() { + let base = EncoderConfig { gop_size: 6, ..EncoderConfig::new(80, 48, 30000, 1001) }; + let mut matrix = [0u8; 64]; + for (i, m) in matrix.iter_mut().enumerate() { + *m = (8 + i * 2) as u8; + } + let cases = [ + ("422", EncoderConfig { chroma: ChromaFormat::Yuv422, ..base.clone() }), + ("linear q", EncoderConfig { nonlinear_quant: false, rate: RateControl::Quantiser(3), ..base.clone() }), + ("coarse", EncoderConfig { rate: RateControl::Quantiser(31), ..base.clone() }), + ("fine", EncoderConfig { rate: RateControl::Quantiser(1), adaptive_quant: false, ..base.clone() }), + ("dc 8 and 10", EncoderConfig { intra_dc_bits: 10, ..base.clone() }), + ("dc 8", EncoderConfig { intra_dc_bits: 8, ..base.clone() }), + ("matrices", EncoderConfig { intra_matrix: Some(matrix), non_intra_matrix: Some(matrix), ..base.clone() }), + ("small range", EncoderConfig { search_range: 3, ..base.clone() }), + ("abr", EncoderConfig { rate: RateControl::Abr { kbps: 300 }, ..base.clone() }), + ("odd size", EncoderConfig { width: 50, height: 34, ..base.clone() }), + ("422 odd size", EncoderConfig { width: 70, height: 30, chroma: ChromaFormat::Yuv422, ..base.clone() }), + ]; + for (name, cfg) in cases { + assert_decodes_to_recon(name, cfg, 8); + } +} + +#[cfg(feature = "threads")] +#[test] +fn threaded_slices_give_the_same_stream() { + let cfg = EncoderConfig { gop_size: 5, threads: false, ..EncoderConfig::new(96, 80, 25, 1) }; + let (a, _, _) = encode(cfg.clone(), 6); + let (b, _, _) = encode(EncoderConfig { threads: true, ..cfg }, 6); + assert_eq!(a, b); +} + +#[test] +fn quality_follows_the_quantiser() { + let mut last = f64::MAX; + let mut last_size = usize::MAX; + for q in [1u8, 4, 12, 31] { + let cfg = EncoderConfig { rate: RateControl::Quantiser(q), gop_size: 4, ..EncoderConfig::new(96, 64, 25, 1) }; + let (es, recon, _) = encode(cfg, 4); + let p: f64 = recon.iter().enumerate().map(|(t, r)| psnr(&scene(96, 64, 48, 32, t).0, &r.y)).sum::() / recon.len() as f64; + assert!(p < last && es.len() < last_size, "q {q}: {p:.2} dB, {} bytes", es.len()); + if q == 1 { + assert!(p > 45.0, "q 1: {p:.2} dB"); + } + if q == 4 { + assert!(p > 36.0, "q 4: {p:.2} dB"); + } + (last, last_size) = (p, es.len()); + } +} + +#[test] +fn p_pictures_are_smaller_and_static_content_skips() { + let cfg = EncoderConfig { gop_size: 8, ..EncoderConfig::new(128, 64, 25, 1) }; + let mut enc = Encoder::new(cfg).unwrap(); + let (y, cb, cr) = scene(128, 64, 64, 32, 0); + let f = YuvFrame { y: &y, cb: &cb, cr: &cr, y_stride: 128, c_stride: 64 }; + let i = enc.encode(&f, 0).unwrap().remove(0); + let p1 = enc.encode(&f, 1).unwrap().remove(0); + let p2 = enc.encode(&f, 2).unwrap().remove(0); + assert_eq!(p1.picture_type, PictureType::P); + // the first P picture refines the I picture's reconstruction; the next repeat has almost + // nothing left to code and skips (every macroblock but the first and last of a slice) + assert!(p1.data.len() * 5 < i.data.len(), "I {} bytes, P {} bytes", i.data.len(), p1.data.len()); + assert!(p2.data.len() * 20 < i.data.len(), "I {} bytes, second P {} bytes", i.data.len(), p2.data.len()); +} + +#[test] +fn the_sequence_header_describes_the_stream() { + let cases = [ + (EncoderConfig::new(352, 288, 25, 1), 0x4A, "Main@Low"), + (EncoderConfig::new(720, 576, 25, 1), 0x48, "Main@Main"), + (EncoderConfig::new(1440, 1080, 25, 1), 0x46, "Main@High-1440"), + (EncoderConfig::new(1920, 1080, 30000, 1001), 0x44, "Main@High"), + (EncoderConfig { chroma: ChromaFormat::Yuv422, ..EncoderConfig::new(720, 608, 25, 1) }, 0x85, "4:2:2@Main"), + (EncoderConfig { chroma: ChromaFormat::Yuv422, ..EncoderConfig::new(1920, 1080, 25, 1) }, 0x82, "4:2:2@High"), + (EncoderConfig { rate: RateControl::Abr { kbps: 20_000 }, ..EncoderConfig::new(720, 576, 25, 1) }, 0x46, "Main@High-1440"), + ]; + for (cfg, pli, name) in cases { + let (w, h, aspect) = (cfg.width, cfg.height, cfg.aspect); + let enc = Encoder::new(cfg).unwrap(); + assert_eq!(enc.profile_and_level(), pli, "{name}"); + let info = probe(enc.sequence_header()).unwrap(); + assert_eq!((info.width, info.height, info.mpeg2, info.progressive_sequence, info.low_delay), (w, h, true, true, true)); + assert_eq!(info.codec_name(), format!("MPEG-2 Video ({name})")); + assert_eq!(aspect, AspectRatio::Square); + assert_eq!(info.sar, (1, 1)); + } + let enc = Encoder::new(EncoderConfig { aspect: AspectRatio::Dar16x9, ..EncoderConfig::new(720, 576, 30000, 1001) }).unwrap(); + let info = probe(enc.sequence_header()).unwrap(); + assert_eq!((info.dar, info.frame_rate), (Some((16, 9)), Some((30000, 1001)))); +} + +#[test] +fn bad_configurations_are_refused() { + let ok = EncoderConfig::new(64, 64, 25, 1); + let bad = [ + EncoderConfig { width: 63, ..ok.clone() }, + EncoderConfig { height: 0, ..ok.clone() }, + EncoderConfig { width: 3840, height: 2160, ..ok.clone() }, + EncoderConfig { chroma: ChromaFormat::Yuv444, ..ok.clone() }, + EncoderConfig { chroma: ChromaFormat::Yuv422, width: 1920, height: 1152, ..ok.clone() }, + EncoderConfig { fps_num: 26, ..ok.clone() }, + EncoderConfig { fps_den: 0, ..ok.clone() }, + EncoderConfig { gop_size: 0, ..ok.clone() }, + EncoderConfig { gop_size: 1024, ..ok.clone() }, + EncoderConfig { rate: RateControl::Quantiser(0), ..ok.clone() }, + EncoderConfig { rate: RateControl::Quantiser(32), ..ok.clone() }, + EncoderConfig { rate: RateControl::Abr { kbps: 0 }, ..ok.clone() }, + EncoderConfig { rate: RateControl::Abr { kbps: 90_000 }, ..ok.clone() }, + EncoderConfig { intra_dc_bits: 11, ..ok.clone() }, + EncoderConfig { intra_matrix: Some([0; 64]), ..ok.clone() }, + EncoderConfig { search_range: 0, ..ok.clone() }, + EncoderConfig { search_range: 64, ..ok.clone() }, + ]; + for cfg in bad { + let desc = format!("{cfg:?}"); + assert!(matches!(Encoder::new(cfg), Err(EncodeError::Config(_))), "{desc}"); + } +} + +#[test] +fn short_or_inconsistent_planes_are_refused() { + let mut enc = Encoder::new(EncoderConfig::new(32, 32, 25, 1)).unwrap(); + let (y, c) = (vec![0u8; 32 * 32], vec![0u8; 16 * 16]); + let ok = YuvFrame { y: &y, cb: &c, cr: &c, y_stride: 32, c_stride: 16 }; + assert!(enc.encode(&ok, 0).is_ok()); + let bad = [ + YuvFrame { y: &y[..32 * 31], ..ok }, + YuvFrame { cb: &c[..10], ..ok }, + YuvFrame { y_stride: 31, ..ok }, + YuvFrame { c_stride: usize::MAX, ..ok }, + YuvFrame { y_stride: usize::MAX / 2, ..ok }, + ]; + for f in bad { + assert!(matches!(enc.encode(&f, 1), Err(EncodeError::BadFrame(_)))); + } + // a refused frame leaves the encoder usable + assert_eq!(enc.encode(&ok, 1).unwrap()[0].picture_type, PictureType::P); +} diff --git a/crates/video/src/mpeg2/encode/transform.rs b/crates/video/src/mpeg2/encode/transform.rs new file mode 100644 index 000000000..d3a6ce4d3 --- /dev/null +++ b/crates/video/src/mpeg2/encode/transform.rs @@ -0,0 +1,122 @@ +//! Forward DCT, quantisation and the decoder-exact reconstruction of one block. + +use crate::mpeg2::idct::{basis, idct}; +use crate::mpeg2::slice::dequant_mpeg2; + +/// Forward 8×8 DCT of `p` (raster order), the transpose of the decoder's IDCT basis: +/// `F[v][u] = Σ_y Σ_x M[x][u] M[y][v] p[y][x]`, so a flat block of value `a` has `F[0][0] = 8a`. +pub(crate) fn fdct(p: &[i32; 64]) -> [f32; 64] { + let m = basis(); + let mut t = [0f32; 64]; + for y in 0..8 { + let row: [f32; 8] = std::array::from_fn(|x| p[y * 8 + x] as f32); + for u in 0..8 { + let mut s = 0f32; + for x in 0..8 { + s += m[x][u] * row[x]; + } + t[y * 8 + u] = s; + } + } + let mut out = [0f32; 64]; + for v in 0..8 { + for u in 0..8 { + let mut s = 0f32; + for y in 0..8 { + s += m[y][v] * t[y * 8 + u]; + } + out[v * 8 + u] = s; + } + } + out +} + +/// Rounding offset of intra AC quantisation (in units of the quantiser step): below one half, +/// a small dead zone. +const INTRA_ROUND: f32 = 0.375; + +/// Quantise intra AC coefficient `f` with weight `w` and quantiser_scale `qs` (inverse of +/// `F = 2·QF·w·qs / 32`). +#[inline] +pub(crate) fn quant_intra(f: f32, w: u8, qs: i32) -> i32 { + let y = 16.0 * f.abs() / (f32::from(w) * qs as f32); + let q = ((y + INTRA_ROUND).floor() as i32).min(2047); + if f < 0.0 { -q } else { q } +} + +/// Quantise non-intra coefficient `f` (inverse of `F = (2·QF + sign)·w·qs / 32`): truncation, +/// which leaves the usual wide dead zone around zero. +#[inline] +pub(crate) fn quant_inter(f: f32, w: u8, qs: i32) -> i32 { + let y = 16.0 * f.abs() / (f32::from(w) * qs as f32); + let q = (y.floor() as i32).min(2047); + if f < 0.0 { -q } else { q } +} + +/// Reconstruct a block as the decoder does: dequantise `levels` (raster order; for intra blocks +/// `levels[0]` is the quantised DC), mismatch control, IDCT. Returns the residual (inter) or +/// the samples before clamping (intra). +pub(crate) fn reconstruct(levels: &[i32; 64], matrix: &[u8; 64], qs: i32, intra: bool, dc_mult: i32) -> [i32; 64] { + let mut blk = [0i32; 64]; + let mut sum = 0i32; + for (i, &l) in levels.iter().enumerate() { + if l == 0 { + continue; + } + let v = if intra && i == 0 { l * dc_mult } else { dequant_mpeg2(l, qs, i32::from(matrix[i]), intra) }; + blk[i] = v; + sum += v; + } + if sum & 1 == 0 { + blk[63] ^= 1; + } + let dc_only = blk[1..].iter().all(|&v| v == 0); + idct(&mut blk, dc_only); + blk +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn flat_block_has_only_dc() { + let f = fdct(&[100; 64]); + assert!((f[0] - 800.0).abs() < 0.01); + assert!(f[1..].iter().all(|v| v.abs() < 0.01)); + } + + #[test] + fn forward_then_inverse_is_close() { + let p: [i32; 64] = std::array::from_fn(|i| ((i * 37 + i / 8 * 11) % 200) as i32 - 60); + let f = fdct(&p); + let mut b: [i32; 64] = std::array::from_fn(|i| f[i].round() as i32); + idct(&mut b, false); + for i in 0..64 { + assert!((b[i] - p[i]).abs() <= 1, "{i}: {} vs {}", b[i], p[i]); + } + } + + #[test] + fn quantisers_invert_the_dequantiser() { + for qs in [1, 2, 8, 31, 62, 112] { + for w in [8u8, 16, 40, 83] { + // only where the dequantiser's integer division is exact + if (i32::from(w) * qs) % 32 != 0 { + continue; + } + for level in [-40, -3, -1, 1, 2, 17] { + let f = dequant_mpeg2(level, qs, i32::from(w), true) as f32; + // without the clamp the intra quantiser returns the level it came from + if f.abs() < 2047.0 { + assert_eq!(quant_intra(f, w, qs), level, "intra qs {qs} w {w}"); + } + let f = dequant_mpeg2(level, qs, i32::from(w), false) as f32; + if f.abs() < 2047.0 { + assert_eq!(quant_inter(f, w, qs), level, "inter qs {qs} w {w}"); + } + } + } + } + } +} diff --git a/crates/video/src/mpeg2/idct.rs b/crates/video/src/mpeg2/idct.rs index f94b935f0..3828d05a6 100644 --- a/crates/video/src/mpeg2/idct.rs +++ b/crates/video/src/mpeg2/idct.rs @@ -6,7 +6,7 @@ use std::sync::OnceLock; /// `M[x][u] = C(u)/2 · cos((2x+1)uπ/16)`, `C(0) = 1/√2`, `C(u>0) = 1`. -fn basis() -> &'static [[f32; 8]; 8] { +pub(crate) fn basis() -> &'static [[f32; 8]; 8] { static B: OnceLock<[[f32; 8]; 8]> = OnceLock::new(); B.get_or_init(|| { let mut m = [[0f32; 8]; 8]; diff --git a/crates/video/src/mpeg2/mod.rs b/crates/video/src/mpeg2/mod.rs index 5d88ea4e5..ae2bb1682 100644 --- a/crates/video/src/mpeg2/mod.rs +++ b/crates/video/src/mpeg2/mod.rs @@ -1,5 +1,6 @@ //! Clean-room MPEG-2 video (ITU-T H.262 | ISO/IEC 13818-2) and MPEG-1 video (ISO/IEC 11172-2) -//! decoder. +//! decoder, and the start of an MPEG-2 encoder ([`encode`], feature `mpeg2-encode`: I and P +//! pictures). //! //! - Main Profile at Low/Main/High-1440/High Level, 4:2:2 Profile (XDCAM HD422, IMX / D-10), //! Simple Profile, and MPEG-1 (including D pictures and constrained-parameters streams). @@ -26,12 +27,17 @@ #![allow(clippy::needless_range_loop, clippy::type_complexity)] mod bits; +#[cfg(feature = "mpeg2-encode")] +pub mod encode; pub mod headers; mod idct; mod mc; mod slice; mod vlc; +#[cfg(feature = "mpeg2-encode")] +pub use encode::{AspectRatio, EncodeError, Encoder, EncoderConfig, Packet, RateControl, ReconFrame, YuvFrame}; + use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; diff --git a/crates/video/src/mpeg2/slice.rs b/crates/video/src/mpeg2/slice.rs index f7a6389f0..706439af4 100644 --- a/crates/video/src/mpeg2/slice.rs +++ b/crates/video/src/mpeg2/slice.rs @@ -519,8 +519,7 @@ pub(crate) fn wrap_vector(pred: i32, delta: i32, f: i32) -> i32 { #[inline(always)] fn dequant(p: &PicParams, level: i32, qs: i32, w: i32, intra: bool) -> i32 { if p.mpeg2 { - let k = if intra { 0 } else { level.signum() }; - ((2 * level + k) * w * qs / 32).clamp(-2048, 2047) + dequant_mpeg2(level, qs, w, intra) } else { let mut v = if intra { 2 * level * qs * w / 16 } else { (2 * level + level.signum()) * qs * w / 16 }; if v & 1 == 0 { @@ -530,6 +529,14 @@ fn dequant(p: &PicParams, level: i32, qs: i32, w: i32, intra: bool) -> i32 { } } +/// MPEG-2 inverse quantisation of one AC (or non-intra) coefficient (§7.4.2.3), saturated +/// (§7.4.3). Shared with the encoder, whose reconstruction must match the decoder exactly. +#[inline(always)] +pub(crate) fn dequant_mpeg2(level: i32, qs: i32, w: i32, intra: bool) -> i32 { + let k = if intra { 0 } else { level.signum() }; + ((2 * level + k) * w * qs / 32).clamp(-2048, 2047) +} + /// The reference field (or frame) for direction `s` and field `sel` (field prediction). fn reference<'a>(p: &PicParams<'a>, s: usize, sel: usize) -> Option<&'a Frame> { if s == 1 { diff --git a/crates/video/src/mpeg2/vlc.rs b/crates/video/src/mpeg2/vlc.rs index 192f86be0..3f8015bb5 100644 --- a/crates/video/src/mpeg2/vlc.rs +++ b/crates/video/src/mpeg2/vlc.rs @@ -22,7 +22,7 @@ pub(crate) struct Vlc { table: Vec, } -fn parse_code(s: &str) -> (u32, u32) { +pub(crate) fn parse_code(s: &str) -> (u32, u32) { let mut v = 0u32; let mut n = 0u32; for c in s.chars() { diff --git a/crates/video/tests/mpeg2/encode.rs b/crates/video/tests/mpeg2/encode.rs new file mode 100644 index 000000000..5cb2377dc --- /dev/null +++ b/crates/video/tests/mpeg2/encode.rs @@ -0,0 +1,206 @@ +//! The MPEG-2 encoder against ffmpeg as an external decoder: ffmpeg must decode our streams +//! without errors, to within the decoder suite's tolerance of the encoder's own reconstruction +//! (ffmpeg's integer IDCT differs from ours), and report the profile, level and picture types +//! we signal. Source pictures come from ffmpeg's `testsrc2`. + +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +use craft_video::mpeg2::{ChromaFormat, Encoder, EncoderConfig, RateControl, ReconFrame, YuvFrame}; + +use crate::common::{Stats, fixture_dir, pix_fmt_of}; +use crate::oracle::{MAX_DIFFERING, MIN_PSNR, TOL_MAX}; + +/// Raw source frames from ffmpeg's `testsrc2`. +fn source(ff: &Path, w: u32, h: u32, frames: usize, pix_fmt: &str) -> Vec { + let o = Command::new(ff) + .args([ + "-hide_banner", + "-loglevel", + "error", + "-f", + "lavfi", + "-i", + &format!("testsrc2=size={w}x{h}:rate=25"), + "-frames:v", + &frames.to_string(), + ]) + .args(["-pix_fmt", pix_fmt, "-f", "rawvideo", "-"]) + .stdin(Stdio::null()) + .output() + .expect("run ffmpeg"); + assert!(o.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&o.stderr)); + o.stdout +} + +fn sizes(cfg: &EncoderConfig) -> (usize, usize, usize) { + let (w, h) = (cfg.width as usize, cfg.height as usize); + let ch = if cfg.chroma == ChromaFormat::Yuv422 { h } else { h / 2 }; + (w * h, w / 2 * ch, w / 2) +} + +/// Encode `raw` frames; returns the elementary stream and the reconstruction. +fn encode(cfg: &EncoderConfig, raw: &[u8]) -> (Vec, Vec) { + let (ys, cs, cw) = sizes(cfg); + let fsize = ys + 2 * cs; + let mut enc = Encoder::new(cfg.clone()).expect("config"); + enc.set_recon_capture(true); + let mut es = Vec::new(); + for (i, f) in raw.chunks_exact(fsize).enumerate() { + let frame = YuvFrame { y: &f[..ys], cb: &f[ys..ys + cs], cr: &f[ys + cs..], y_stride: cfg.width as usize, c_stride: cw }; + for p in enc.encode(&frame, i as i64).expect("encode") { + es.extend_from_slice(&p.data); + } + } + for p in enc.flush() { + es.extend_from_slice(&p.data); + } + es.extend_from_slice(enc.end_of_sequence()); + (es, enc.take_recon()) +} + +/// ffmpeg's decode of `file`, failing on any decoding error. +fn ffmpeg_decode(ff: &Path, file: &Path, pix_fmt: &str) -> Vec { + let o = Command::new(ff) + .args(["-hide_banner", "-loglevel", "error", "-xerror", "-err_detect", "explode", "-i"]) + .arg(file) + .args(["-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]) + .stdin(Stdio::null()) + .output() + .expect("run ffmpeg"); + assert!(o.status.success() && o.stderr.is_empty(), "ffmpeg rejects {}: {}", file.display(), String::from_utf8_lossy(&o.stderr)); + o.stdout +} + +/// ffprobe's stream description and per-frame picture types. +fn ffprobe(file: &Path) -> Option<(String, Vec)> { + let fp = craft_media_testkit::ffprobe()?; + let stream = Command::new(&fp) + .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=codec_name,profile,level,pix_fmt", "-of", "default=nw=1"]) + .arg(file) + .output() + .ok()?; + let frames = Command::new(&fp) + .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "frame=pict_type", "-of", "csv=p=0"]) + .arg(file) + .output() + .ok()?; + let types = String::from_utf8_lossy(&frames.stdout).lines().map(|l| l.trim_end_matches(',').to_string()).filter(|l| !l.is_empty()).collect(); + // "codec_name=…\nprofile=…" in ffprobe's order → "codec,profile,level,pix_fmt" + let text = String::from_utf8_lossy(&stream.stdout).to_string(); + let field = |k: &str| text.lines().find_map(|l| l.strip_prefix(&format!("{k}="))).unwrap_or("").to_string(); + Some((["codec_name", "profile", "level", "pix_fmt"].map(field).join(","), types)) +} + +fn write(name: &str, es: &[u8]) -> PathBuf { + let path = fixture_dir().join(format!("enc_{name}.m2v")); + std::fs::write(&path, es).expect("write stream"); + path +} + +fn psnr_y(recon: &[ReconFrame], raw: &[u8], ys: usize, fsize: usize) -> f64 { + let mut s = Stats::default(); + for (r, f) in recon.iter().zip(raw.chunks_exact(fsize)) { + s.add(&r.y, &f[..ys]); + } + s.psnr() +} + +struct Case { + name: &'static str, + cfg: EncoderConfig, + frames: usize, + /// ffprobe's "codec,profile,level,pix_fmt". + probe: &'static str, + /// Lowest luma PSNR against the source. + min_psnr: f64, +} + +fn check(ff: &Path, c: &Case) -> (Vec, f64) { + let pix_fmt = pix_fmt_of(c.cfg.chroma); + let raw = source(ff, c.cfg.width, c.cfg.height, c.frames, pix_fmt); + let (ys, cs, _) = sizes(&c.cfg); + let fsize = ys + 2 * cs; + let (es, recon) = encode(&c.cfg, &raw); + let path = write(c.name, &es); + let decoded = ffmpeg_decode(ff, &path, pix_fmt); + assert_eq!(decoded.len(), c.frames * fsize, "{}: ffmpeg's frame count", c.name); + let mut all = Stats::default(); + for (i, (r, f)) in recon.iter().zip(decoded.chunks_exact(fsize)).enumerate() { + let mut s = Stats::default(); + s.add(&r.y, &f[..ys]); + s.add(&r.cb, &f[ys..ys + cs]); + s.add(&r.cr, &f[ys + cs..]); + assert!(s.max <= TOL_MAX && s.psnr() >= MIN_PSNR, "{} frame {i}: max {} psnr {:.2} against ffmpeg", c.name, s.max, s.psnr()); + all.merge(&s); + } + assert!((all.diff as f64) / (all.n as f64) <= MAX_DIFFERING, "{}: {} of {} samples differ from ffmpeg", c.name, all.diff, all.n); + let quality = psnr_y(&recon, &raw, ys, fsize); + assert!(quality >= c.min_psnr, "{}: luma PSNR {quality:.2} dB against the source", c.name); + if let Some((stream, types)) = ffprobe(&path) { + assert_eq!(stream, c.probe, "{}: ffprobe", c.name); + let gop = c.cfg.gop_size as usize; + let want: Vec<&str> = (0..c.frames).map(|i| if i % gop == 0 { "I" } else { "P" }).collect(); + assert_eq!(types, want, "{}: picture types", c.name); + } + let kbps = es.len() as f64 * 8.0 * 25.0 / c.frames as f64 / 1000.0; + println!( + "{}: {} frames, {kbps:.0} kbit/s, luma PSNR {quality:.2} dB, max diff to ffmpeg {}, {:.3}% differ", + c.name, + c.frames, + all.max, + 100.0 * all.diff as f64 / all.n as f64 + ); + (es, kbps) +} + +#[test] +fn ffmpeg_decodes_our_streams() { + let Some(ff) = craft_media_testkit::ffmpeg_or_skip("mpeg2 encoder oracle") else { return }; + let cif = EncoderConfig { gop_size: 12, ..EncoderConfig::new(352, 288, 25, 1) }; + let cases = [ + Case { name: "cif_q4", cfg: cif.clone(), frames: 25, probe: "mpeg2video,Main,10,yuv420p", min_psnr: 36.0 }, + Case { + name: "cif_intra", + cfg: EncoderConfig { gop_size: 1, rate: RateControl::Quantiser(8), ..cif.clone() }, + frames: 6, + probe: "mpeg2video,Main,10,yuv420p", + min_psnr: 32.0, + }, + Case { + name: "cif_linear_coarse", + cfg: EncoderConfig { nonlinear_quant: false, adaptive_quant: false, rate: RateControl::Quantiser(20), gop_size: 15, ..cif.clone() }, + frames: 20, + probe: "mpeg2video,Main,10,yuv420p", + min_psnr: 26.0, + }, + Case { + name: "sd_422", + cfg: EncoderConfig { chroma: ChromaFormat::Yuv422, intra_dc_bits: 10, ..EncoderConfig::new(720, 576, 25, 1) }, + frames: 8, + // 4:2:2@Main is indication 0x85; ffprobe prints its low four bits as the level + probe: "mpeg2video,4:2:2,5,yuv422p", + min_psnr: 36.0, + }, + ]; + for c in &cases { + check(&ff, c); + } +} + +#[test] +fn average_bitrate_reaches_its_target() { + let Some(ff) = craft_media_testkit::ffmpeg_or_skip("mpeg2 encoder bitrate") else { return }; + for kbps in [800u32, 2500] { + let c = Case { + name: if kbps == 800 { "cif_abr_800" } else { "cif_abr_2500" }, + cfg: EncoderConfig { rate: RateControl::Abr { kbps }, gop_size: 12, ..EncoderConfig::new(352, 288, 25, 1) }, + frames: 72, + probe: "mpeg2video,Main,10,yuv420p", + min_psnr: 26.0, + }; + let (_, got) = check(&ff, &c); + let err = got / f64::from(kbps) - 1.0; + assert!(err.abs() < 0.10, "{kbps} kbit/s target: got {got:.0} kbit/s ({:+.1}%)", 100.0 * err); + } +} diff --git a/crates/video/tests/mpeg2/main.rs b/crates/video/tests/mpeg2/main.rs index 1864b4a22..acbbf662e 100644 --- a/crates/video/tests/mpeg2/main.rs +++ b/crates/video/tests/mpeg2/main.rs @@ -1,4 +1,6 @@ mod common; +#[cfg(feature = "mpeg2-encode")] +mod encode; mod fuzz_hostile; mod hostile_dims; mod oracle; From f68e4369eb77475b6542a4814489ed11f7312edc Mon Sep 17 00:00:00 2001 From: alehel Date: Thu, 12 Nov 2026 18:41:44 +0100 Subject: [PATCH 070/520] craft-audio: decode the HE-AAC SBR high band MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit aac::Decoder::set_sbr(true) decodes HE-AAC at twice the core rate with the SBR tool of ISO/IEC 14496-3 §4.6.18: QMF analysis and synthesis, frequency band tables, patching with LPC inverse filtering, envelope adjustment with limiter, boost, noise and sinusoids. TrackDecoder uses it in place of the 2x upsampler. Tables are transcribed from ETSI ES 201 980 Annex I. Matches ffmpeg's decoder on AudioToolbox HE-AAC streams of 1-6 channels: the same 962-sample delay, ~54 dB SNR, high band level within 0.01 dB. Parametric stereo is still not decoded. Refs #1121 Co-Authored-By: Claude Opus 5.5 --- crates/audio/README.md | 13 +- crates/audio/src/aac/decoder.rs | 131 ++++- crates/audio/src/aac/huffman.rs | 6 +- crates/audio/src/aac/mod.rs | 22 +- crates/audio/src/aac/sbr/bitstream.rs | 489 +++++++++++++++++ crates/audio/src/aac/sbr/freq.rs | 353 ++++++++++++ crates/audio/src/aac/sbr/hf.rs | 332 ++++++++++++ crates/audio/src/aac/sbr/mod.rs | 513 ++++++++++++++++++ crates/audio/src/aac/sbr/qmf.rs | 155 ++++++ crates/audio/src/aac/sbr/tables.rs | 357 ++++++++++++ crates/audio/src/track.rs | 48 +- .../audio/tests/codecs/aac/he_aac_oracle.rs | 165 ++++++ crates/audio/tests/codecs/aac/mod.rs | 1 + 13 files changed, 2526 insertions(+), 59 deletions(-) create mode 100644 crates/audio/src/aac/sbr/bitstream.rs create mode 100644 crates/audio/src/aac/sbr/freq.rs create mode 100644 crates/audio/src/aac/sbr/hf.rs create mode 100644 crates/audio/src/aac/sbr/mod.rs create mode 100644 crates/audio/src/aac/sbr/qmf.rs create mode 100644 crates/audio/src/aac/sbr/tables.rs create mode 100644 crates/audio/tests/codecs/aac/he_aac_oracle.rs diff --git a/crates/audio/README.md b/crates/audio/README.md index 30f09ea57..abd3228f4 100644 --- a/crates/audio/README.md +++ b/crates/audio/README.md @@ -309,7 +309,8 @@ match craft_audio::decode(&bytes, None) { | AIFF, AIFF-C | native | native | | FLAC | symphonia | native | | AAC-LC | native | native | -| HE-AAC v1/v2 | native, core only (see [Limits](#limits)) | — | +| HE-AAC v1 | native (SBR) | — | +| HE-AAC v2 | native, without parametric stereo (see [Limits](#limits)) | — | | ALAC | native | native, packets (`alac::Encoder`; no file writer) | | MP1, MP2 (MPEG audio Layer I/II) | native | — | | MP3 (Layer III) | native | native, frames (`mp3::Encoder`; the frames in a row are an `.mp3` file) | @@ -329,10 +330,10 @@ match craft_audio::decode(&bytes, None) { ## Limits -- HE-AAC: the AAC-LC core is decoded. `decode`, `Decoder` and `TrackDecoder` upsample it 2×; - `aac::Decoder` alone outputs the core rate. SBR is not reconstructed, so there is no content - above the core's Nyquist frequency. -- HE-AAC v2: parametric stereo is not reconstructed either, so the mono core decodes as mono. +- HE-AAC: `decode`, `Decoder` and `TrackDecoder` decode SBR at twice the core rate. + `aac::Decoder` outputs the core rate unless `set_sbr(true)` is called. Downsampled SBR (an + output rate equal to the core rate) plays the core alone. +- HE-AAC v2: parametric stereo is not reconstructed, so the mono core decodes as mono, with SBR. - AAC: Main, SSR and LTP profiles, coupling channels and 960-sample frames are unsupported. - Raw ADTS: frames with several raw data blocks are unsupported: a stream of them is refused, and one among other frames decodes as silence of its length and counts as damaged, as does a frame @@ -413,7 +414,7 @@ Each codec module's rustdoc lists the coding tools it implements. |---|---|---|---| | WAV, AIFF | Microsoft RIFF, Apple AIFF/AIFF-C | read: integer 8–32-bit, float 32/64-bit, `WAVE_FORMAT_EXTENSIBLE` masks, RF64/BW64, BWF `bext`, truncated files; write: 16/24/32-bit integer and 32-bit float, RF64 above 4 GiB, any layout, TPDF dither | round trips; hostile-input tests | | FLAC encoder | RFC 9639 | 16, 24, 32 bits; 1–8 channels, layouts decoders would not assume for the count as a `WAVEFORMATEXTENSIBLE_CHANNEL_MASK` comment; up to 655,350 Hz; LPC, stereo decorrelation, wasted bits, STREAMINFO MD5 | bit-exact through ffmpeg, STREAMINFO MD5 checked against ffmpeg | -| AAC (`aac`) | ISO/IEC 14496-3 | AAC-LC decode and encode (CBR, VBR, ADTS), 1–8 channels; HE-AAC: see [Limits](#limits) | ffmpeg: ~135 dB SNR; PNS noise differs per sample at equal band energy | +| AAC (`aac`) | ISO/IEC 14496-3; SBR tables from ETSI ES 201 980 Annex I | AAC-LC decode and encode (CBR, VBR, ADTS), 1–8 channels; HE-AAC (SBR) decode, see [Limits](#limits) | ffmpeg: ~135 dB SNR; PNS noise differs per sample at equal band energy. HE-AAC from AudioToolbox, 1–6 channels: same 962-sample delay as ffmpeg, ~54 dB SNR, high band level within 0.01 dB | | Opus (`opus`) | RFC 6716, 8251, 7845 | decoder: SILK, hybrid, CELT, all frame sizes, PLC and FEC, multistream (`Decoder`) and single stream (`StreamDecoder`), 8–48 kHz output | all 36 RFC 8251 conformance cases | | AC-3/E-AC-3 (`ac3`) | decoder: ATSC A/52:2012; encoder/programme API: ETSI TS 102 366 V1.4.1 | single-syncframe and complete-programme decoding; packet encoding through discrete 7.1 (see below) | decoder comparisons and encoder output through ffmpeg, CRC/channel/timing/bit-budget regressions; synthetic quality limitations below | | MP1/MP2 (`mpa`) | ISO/IEC 11172-3, 13818-3 | Layer I and II, MPEG-1/2/2.5, 8–48 kHz; also the Layer II rate/mode pairs the spec disallows (ffmpeg's default mono 384 kbit/s); no free format | ffmpeg-encoded streams, 73–84 dB SNR | diff --git a/crates/audio/src/aac/decoder.rs b/crates/audio/src/aac/decoder.rs index 8c4d41300..1d8713f54 100644 --- a/crates/audio/src/aac/decoder.rs +++ b/crates/audio/src/aac/decoder.rs @@ -6,6 +6,7 @@ use crate::aac::config::{AudioSpecificConfig, ElementType, ProgramConfig}; use crate::aac::huffman::{self, INTENSITY_HCB, INTENSITY_HCB2, NOISE_HCB, ZERO_HCB}; use crate::aac::ics::{IcsInfo, WindowSequence}; use crate::aac::mdct::{MdctScratch, WindowShape, mdct_long, mdct_short, window}; +use crate::aac::sbr; use crate::aac::tables::{pow43, pow43_table, sf_gain}; use crate::aac::tns::{self, TnsData}; use crate::aac::{Error, Result}; @@ -63,7 +64,14 @@ impl Scratch { } } -/// AAC-LC decoder. +/// Where the SBR data of a channel element sits in its access unit: the bit range of the fill +/// element's payload after its extension type, and whether a CRC leads it. +type SbrRef = (usize, usize, bool); + +/// An access unit's core output, and the SBR data of each layout entry. +type Decoded = (Vec>, Vec>); + +/// AAC-LC decoder, with the SBR tool of HE-AAC when [`Decoder::set_sbr`] turns it on. #[derive(Clone)] pub struct Decoder { asc: AudioSpecificConfig, @@ -75,6 +83,8 @@ pub struct Decoder { scratch: Scratch, /// A fill element carried SBR data (implicitly signalled HE-AAC). sbr_seen: bool, + /// The SBR decoder of each layout entry, when output is at twice the core rate. + sbr_out: Option>, } impl Decoder { @@ -110,6 +120,7 @@ impl Decoder { noise_state: NOISE_SEED, scratch: Scratch::new(), sbr_seen: false, + sbr_out: None, }; d.set_layout(layout); Ok(d) @@ -131,16 +142,27 @@ impl Decoder { pub fn config(&self) -> &AudioSpecificConfig { &self.asc } - /// Output sampling rate in Hz: the AAC-LC core rate. + /// Output sampling rate in Hz: the AAC-LC core rate, or twice it with [`Self::set_sbr`] on. pub fn sample_rate(&self) -> u32 { - self.asc.sample_rate + if self.sbr_out.is_some() { self.asc.sample_rate.saturating_mul(2) } else { self.asc.sample_rate } } /// Whether the stream is HE-AAC: SBR signalled in the `AudioSpecificConfig` (explicit) or SBR - /// data seen in a fill element of a decoded access unit (implicit). Only the AAC-LC core is - /// decoded, at [`Self::sample_rate`]; the SBR high band is not reconstructed. + /// data seen in a fill element of a decoded access unit (implicit). Without [`Self::set_sbr`], + /// only the AAC-LC core is decoded. pub fn sbr(&self) -> bool { self.asc.sbr || self.sbr_seen } + /// Decode HE-AAC at twice the core rate (`true`) or the AAC-LC core alone (`false`, the + /// default). With SBR on, [`Self::decode`] returns 2048 samples per channel: the core through + /// the SBR filter banks, delayed by [`SBR_DELAY`](crate::aac::SBR_DELAY) samples, with the high + /// band rebuilt from each frame's SBR data. Until the stream's first SBR header, and for a + /// frame whose SBR data is missing or damaged, the high band is empty. Parametric stereo + /// (HE-AAC v2) is not decoded: its mono core stays mono. + pub fn set_sbr(&mut self, on: bool) { + if on != self.sbr_out.is_some() { + self.sbr_out = on.then(Vec::new); + } + } /// Output channels of the current element layout. pub fn channels(&self) -> usize { self.channels.len() @@ -158,30 +180,60 @@ impl Decoder { c.overlap.fill(0.0); c.prev_shape = WindowShape::Sine; } + for e in self.sbr_out.iter_mut().flatten() { + e.reset(); + } } fn sf_index(&self) -> u8 { self.asc.sf_index } - /// Decode one access unit (one `raw_data_block`) into 1024 samples per channel. An access unit - /// that fails leaves the decoder as it was, but for [`Self::sbr`]: its damage, including a damaged - /// program config element, does not reach the access units after it. + /// Decode one access unit (one `raw_data_block`) into 1024 samples per channel (2048 with + /// [`Self::set_sbr`] on). An access unit that fails leaves the decoder as it was, but for + /// [`Self::sbr`]: its damage, including a damaged program config element, does not reach the + /// access units after it. Damaged SBR data alone does not fail it. pub fn decode(&mut self, au: &[u8]) -> Result>> { let before = (self.asc.pce.clone(), self.layout.clone(), self.offsets.clone(), self.channels.clone(), self.noise_state); - let decoded = self.decode_block(au, true); - if decoded.is_err() { - (self.asc.pce, self.layout, self.offsets, self.channels, self.noise_state) = before; + match self.decode_block(au, true) { + Ok((pcm, sbr_refs)) => Ok(self.apply_sbr(au, pcm, &sbr_refs)), + Err(e) => { + (self.asc.pce, self.layout, self.offsets, self.channels, self.noise_state) = before; + Err(e) + } } - decoded } - fn decode_block(&mut self, au: &[u8], allow_reconfig: bool) -> Result>> { + /// Run each layout entry's channels through its SBR decoder, when SBR is on. + fn apply_sbr(&mut self, au: &[u8], pcm: Vec>, refs: &[Option]) -> Vec> { + let rate = self.sample_rate(); + let Some(elements) = self.sbr_out.as_mut() else { + return pcm; + }; + // a new layout (a program config element) starts the SBR decoders afresh + let fits = elements.len() == self.layout.len() && elements.iter().zip(&self.layout).all(|(e, l)| e.channels() == l.0.channels()); + if !fits { + *elements = self.layout.iter().map(|l| sbr::Element::new(l.0.channels(), rate)).collect(); + } + let mut out = Vec::with_capacity(pcm.len()); + for (i, e) in elements.iter_mut().enumerate() { + let first = self.offsets.get(i).copied().unwrap_or(0); + let core = pcm.get(first..first + e.channels()).unwrap_or_default(); + let payload = refs.get(i).copied().flatten().map(|(start, end, crc)| sbr::Payload { data: au, start, end, crc }); + out.extend(e.process(core, payload)); + } + out + } + + fn decode_block(&mut self, au: &[u8], allow_reconfig: bool) -> Result { let mut br = BitReader::new(au); let nch = self.channels.len(); let mut saw_element = false; let mut out: Vec>> = vec![None; nch]; let mut used = vec![false; self.layout.len()]; + // SBR data belongs to the single channel or pair element before its fill element + let mut sbr_refs: Vec> = vec![None; self.layout.len()]; + let mut last_slot = None; loop { let id = br.read_bits(3)?; saw_element |= matches!(id, ID_SCE | ID_CPE | ID_LFE); @@ -191,6 +243,7 @@ impl Decoder { let ty = if id == ID_SCE { ElementType::Sce } else { ElementType::Lfe }; let slot = self.find_slot(ty, tag, &used)?; used[slot] = true; + last_slot = (id == ID_SCE).then_some(slot); let mut ics = self.decode_ics(&mut br, None)?; let ch = self.offsets[slot]; self.fill_noise(&mut ics, None); @@ -200,6 +253,7 @@ impl Decoder { let tag = br.read_bits(4)? as u8; let slot = self.find_slot(ElementType::Cpe, tag, &used)?; used[slot] = true; + last_slot = Some(slot); let ch = self.offsets[slot]; let [a, b] = self.decode_cpe(&mut br, ch)?; out[ch] = Some(a); @@ -237,8 +291,13 @@ impl Decoder { } if count > 0 { let ext = br.read_bits(4)?; - self.sbr_seen |= matches!(ext, EXT_SBR_DATA | EXT_SBR_DATA_CRC); + let is_sbr = matches!(ext, EXT_SBR_DATA | EXT_SBR_DATA_CRC); + self.sbr_seen |= is_sbr; + let start = br.position(); br.skip(count * 8 - 4)?; + if let (true, Some(slot)) = (is_sbr, last_slot.take()) { + sbr_refs[slot] = Some((start, br.position(), ext == EXT_SBR_DATA_CRC)); + } } } _ => break, // ID_END @@ -251,7 +310,7 @@ impl Decoder { for o in out { res.push(o.ok_or(Error::Bitstream("access unit is missing a channel element"))?); } - Ok(res) + Ok((res, sbr_refs)) } fn find_slot(&self, ty: ElementType, tag: u8, used: &[bool]) -> Result { @@ -637,6 +696,48 @@ mod tests { assert_eq!(dec.sample_rate(), 22_050); } + /// With SBR on, an access unit decodes to 2048 samples, and over a silent core the high band + /// its SBR fill element describes is there; with SBR off, the core alone (silence). + #[test] + fn sbr_fill_elements_rebuild_the_high_band() { + let payload = sbr::test_payload(44_100, |_| 40, 0).unwrap(); + let mut bw = BitWriter::new(); + bw.write_bits(ID_SCE, 3); + bw.write_bits(0, 4); + bw.write_bits(0, 8 + 11 + 3); // global_gain, ics_info (max_sfb 0), no pulse/TNS/gain control + bw.write_bits(ID_FIL, 3); + let count = (4 + 8 * payload.len()).div_ceil(8); + if count < 15 { + bw.write_bits(count as u32, 4); + } else { + bw.write_bits(15, 4); + bw.write_bits((count - 14) as u32, 8); + } + bw.write_bits(EXT_SBR_DATA, 4); + for &b in &payload { + bw.write_bits(u32::from(b), 8); + } + bw.write_bits(0, 4); + bw.write_bits(ID_END, 3); + let au = bw.finish(); + // AAC-LC, 22.05 kHz, mono + let asc = AudioSpecificConfig::lc(7, 1); + let mut dec = Decoder::from_config(asc.clone()).unwrap(); + let core = dec.decode(&au).unwrap(); + assert_eq!((core[0].len(), dec.sample_rate()), (1024, 22_050)); + assert!(dec.sbr() && core[0].iter().all(|&s| s == 0.0)); + let mut dec = Decoder::from_config(asc).unwrap(); + dec.set_sbr(true); + assert_eq!(dec.sample_rate(), 44_100); + let mut out = Vec::new(); + for _ in 0..4 { + out.extend(dec.decode(&au).unwrap().remove(0)); + } + assert_eq!(out.len(), 4 * 2048); + let rms = (out[2048..].iter().map(|s| s * s).sum::() / (out.len() - 2048) as f32).sqrt(); + assert!(rms > 1e-4, "{rms}"); + } + /// An access unit whose elements are all empty: `elements` are (id, tag), then ID_END. fn silent_au(pce: Option<&ProgramConfig>, elements: &[(u32, u32)]) -> Vec { let mut bw = BitWriter::new(); diff --git a/crates/audio/src/aac/huffman.rs b/crates/audio/src/aac/huffman.rs index adac5c9c9..0c29673e7 100644 --- a/crates/audio/src/aac/huffman.rs +++ b/crates/audio/src/aac/huffman.rs @@ -62,12 +62,12 @@ const SUB: u32 = 1 << 31; /// entry with `SUB` set is `SUB | offset << 5 | width` and selects a sub-table of `1 << width` /// leaves indexed by the next `width` bits. An entry of 0 is a bit pattern that no codeword starts /// with (the codebooks are complete, so this does not occur). -struct Lut { +pub(crate) struct Lut { t: Vec, } impl Lut { - fn build(codes: &[(u32, u8)]) -> Lut { + pub(crate) fn build(codes: &[(u32, u8)]) -> Lut { let mut t = vec![0u32; 1 << L1]; // sub-table width per first-level prefix let mut width = vec![0u32; 1 << L1]; @@ -103,7 +103,7 @@ impl Lut { /// The symbol of the codeword at the reader position; `Eof` if the codeword is cut off. #[inline] - fn decode(&self, br: &mut BitReader) -> Result { + pub(crate) fn decode(&self, br: &mut BitReader) -> Result { let bits = br.peek(32); let mut e = self.t[(bits >> (32 - L1)) as usize]; if e & SUB != 0 { diff --git a/crates/audio/src/aac/mod.rs b/crates/audio/src/aac/mod.rs index 0a5f639a4..297aa7972 100644 --- a/crates/audio/src/aac/mod.rs +++ b/crates/audio/src/aac/mod.rs @@ -5,13 +5,13 @@ //! model, window switching, M/S, TNS, bit reservoir, CBR/VBR, 1–8 channels. //! - [`Decoder`] (feature `aac-decode`): `AudioSpecificConfig` + raw access units → planar f32. //! Supports M/S, intensity stereo, PNS, TNS, pulse data, both window shapes and every window -//! sequence. +//! sequence, and the SBR tool of HE-AAC ([`Decoder::set_sbr`]). //! - [`latm_config`] / [`latm_split`] (feature `aac-decode`): the configuration and payload of //! LOAS / LATM frames (AAC in MPEG transport streams), for the decoder. //! -//! HE-AAC (SBR/PS), Main/SSR/LTP profiles, coupling channels and 960-sample frames are not -//! supported; for HE-AAC only the AAC-LC core is decoded (at the core rate; [`Decoder::sbr`] says -//! when). +//! Parametric stereo (HE-AAC v2), Main/SSR/LTP profiles, coupling channels and 960-sample frames +//! are not supported. HE-AAC decodes at twice the core rate with [`Decoder::set_sbr`] on +//! ([`Decoder::sbr`] says when a stream uses SBR), and as its AAC-LC core otherwise. //! //! From FilmCraft's `filmcraft-aac` (storytold/filmcraft@8fda2f2). Public items keep their names, //! so FilmCraft can re-export this module. @@ -53,6 +53,8 @@ mod ics; #[cfg(feature = "aac-decode")] mod latm; mod mdct; +#[cfg(feature = "aac-decode")] +mod sbr; mod tables; mod tns; @@ -68,6 +70,12 @@ pub use latm::{LatmConfig, latm_config, latm_split}; pub use mdct::WindowShape; pub use tables::{SAMPLE_RATES, sample_rate_index}; +/// Output samples by which [`Decoder`] with [`Decoder::set_sbr`] on delays the core: the SBR filter +/// banks and the SBR frame's lag behind the core frame. HE-AAC encoders count it in the priming the +/// container signals. +#[cfg(feature = "aac-decode")] +pub const SBR_DELAY: usize = sbr::DELAY; + /// Samples per AAC-LC frame (per channel). pub const FRAME_LEN: usize = 1024; @@ -197,9 +205,9 @@ const UPSAMPLE_DELAY: usize = 481; #[cfg(feature = "aac-decode")] const UPSAMPLE_HISTORY: usize = UPSAMPLE_DELAY + UPSAMPLE_HALF; -/// 2× upsampler for HE-AAC. The decoder reconstructs the AAC-LC core at half the output rate and -/// not yet the SBR high band, so the core is interpolated to the output rate: timing and pitch are -/// right and the band above the core's Nyquist stays empty. Even outputs are the input samples, +/// 2× upsampler: interpolates an AAC-LC core to twice its rate, leaving the band above the core's +/// Nyquist empty. [`Decoder::set_sbr`] decodes HE-AAC with its high band instead; this stays for +/// callers that want the core alone at the output rate. Even outputs are the input samples, /// odd ones a Blackman-windowed sinc half-band interpolation over 2 × `UPSAMPLE_HALF` inputs. /// The output is delayed by `UPSAMPLE_DELAY` input samples, as an SBR decoder's is; the state /// carries across packets, so a seek primed with the preceding packet gives the same samples as diff --git a/crates/audio/src/aac/sbr/bitstream.rs b/crates/audio/src/aac/sbr/bitstream.rs new file mode 100644 index 000000000..6233504fa --- /dev/null +++ b/crates/audio/src/aac/sbr/bitstream.rs @@ -0,0 +1,489 @@ +//! The SBR bitstream (ISO/IEC 14496-3 §4.4.2.8 syntax, §4.5.2.8 semantics): the header and the +//! single and channel pair elements of an `EXT_SBR_DATA` fill element. + +use std::sync::OnceLock; + +use craft_media::bitstream::BitReader; + +use super::freq::FreqTables; +use super::tables::{ + F_ENV_1_5DB, F_ENV_3_0DB, F_ENV_BAL_1_5DB, F_ENV_BAL_3_0DB, T_ENV_1_5DB, T_ENV_3_0DB, T_ENV_BAL_1_5DB, T_ENV_BAL_3_0DB, T_NOISE_3_0DB, + T_NOISE_BAL_3_0DB, +}; +use super::{Header, MAX_ENV}; +use crate::aac::huffman::Lut; +use crate::aac::{Error, Result}; + +/// `bs_frame_class`: fixed leading and trailing frame borders. +pub(crate) const FIXFIX: u8 = 0; +/// Fixed leading border, variable trailing border. +pub(crate) const FIXVAR: u8 = 1; +/// Variable leading border, fixed trailing border. +pub(crate) const VARFIX: u8 = 2; +/// Variable leading and trailing borders. +pub(crate) const VARVAR: u8 = 3; + +/// SBR time slots per frame (`numTimeSlots`; two QMF slots each). +const TIME_SLOTS: usize = 16; + +/// Decoding tables for the SBR Huffman codes, in [`Book`] order. +fn luts() -> &'static [Lut; 10] { + static LUTS: OnceLock<[Lut; 10]> = OnceLock::new(); + LUTS.get_or_init(|| { + [ + Lut::build(&T_ENV_1_5DB), + Lut::build(&F_ENV_1_5DB), + Lut::build(&T_ENV_BAL_1_5DB), + Lut::build(&F_ENV_BAL_1_5DB), + Lut::build(&T_ENV_3_0DB), + Lut::build(&F_ENV_3_0DB), + Lut::build(&T_ENV_BAL_3_0DB), + Lut::build(&F_ENV_BAL_3_0DB), + Lut::build(&T_NOISE_3_0DB), + Lut::build(&T_NOISE_BAL_3_0DB), + ] + }) +} + +/// One of the SBR Huffman codes, and its largest absolute value. +#[derive(Clone, Copy)] +struct Book(usize, i32); + +const T_ENV_15: Book = Book(0, 60); +const F_ENV_15: Book = Book(1, 60); +const T_BAL_15: Book = Book(2, 24); +const F_BAL_15: Book = Book(3, 24); +const T_ENV_30: Book = Book(4, 31); +const F_ENV_30: Book = Book(5, 31); +const T_BAL_30: Book = Book(6, 12); +const F_BAL_30: Book = Book(7, 12); +const T_NOISE: Book = Book(8, 31); +const T_NOISE_BAL: Book = Book(9, 12); + +fn huff(br: &mut BitReader, book: Book) -> Result { + Ok(luts()[book.0].decode(br)? as i32 - book.1) +} + +fn bits(br: &mut BitReader, n: u32) -> Result { + Ok(br.read_bits(n)? as usize) +} + +impl Header { + /// `sbr_header()`, with the defaults of the fields its extra parts leave out. + pub(crate) fn read(br: &mut BitReader) -> Result
{ + let amp_res = br.read_flag()?; + let start_freq = br.read_bits(4)? as u8; + let stop_freq = br.read_bits(4)? as u8; + let xover_band = br.read_bits(3)? as u8; + br.skip(2)?; // bs_reserved + let (extra_1, extra_2) = (br.read_flag()?, br.read_flag()?); + let mut h = Header { + amp_res, + start_freq, + stop_freq, + xover_band, + freq_scale: 2, + alter_scale: true, + noise_bands: 2, + limiter_bands: 2, + limiter_gains: 2, + interpol_freq: true, + smoothing_mode: true, + }; + if extra_1 { + h.freq_scale = br.read_bits(2)? as u8; + h.alter_scale = br.read_flag()?; + h.noise_bands = br.read_bits(2)? as u8; + } + if extra_2 { + h.limiter_bands = br.read_bits(2)? as u8; + h.limiter_gains = br.read_bits(2)? as u8; + h.interpol_freq = br.read_flag()?; + h.smoothing_mode = br.read_flag()?; + } + Ok(h) + } +} + +/// The time/frequency grid of one channel in one frame (`sbr_grid()`, §4.6.18.3.3). +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Grid { + pub(crate) class: u8, + /// Envelopes (1 to 5). + pub(crate) num_env: usize, + /// Envelope borders in time slots; `t_env[num_env]` may reach past the frame (up to 19). + pub(crate) t_env: [usize; MAX_ENV + 1], + /// Noise floors (1 or 2). + pub(crate) num_noise: usize, + /// Noise floor borders. + pub(crate) t_noise: [usize; 3], + /// High frequency resolution per envelope. + pub(crate) freq_res: [bool; MAX_ENV], + /// The transient envelope `l_A`, if any (it may be `num_env`: the next frame's first). + pub(crate) la: Option, + /// 3 dB envelope steps (the header's choice, but 1.5 dB for a single FIXFIX envelope). + pub(crate) amp_res: bool, +} + +/// `ceil(log2(num_env + 1))`: the width of `bs_pointer`. +fn pointer_bits(num_env: usize) -> u32 { + usize::BITS - num_env.leading_zeros() +} + +fn read_rel(br: &mut BitReader, n: usize) -> Result<[usize; 3]> { + let mut rel = [0; 3]; + for r in rel.iter_mut().take(n) { + *r = 2 * bits(br, 2)? + 2; + } + Ok(rel) +} + +impl Grid { + fn read(br: &mut BitReader, amp_res: bool) -> Result { + let bad = || Error::Bitstream("invalid SBR time grid"); + let class = br.read_bits(2)? as u8; + let mut t = [0i64; MAX_ENV + 1]; + let mut freq_res = [false; MAX_ENV]; + let (n, pointer) = match class { + FIXFIX => { + let n = 1usize << br.read_bits(2)?; + if n > 4 { + return Err(bad()); + } + freq_res = [br.read_flag()?; MAX_ENV]; + for (l, t) in t.iter_mut().enumerate().take(n + 1) { + *t = (l * TIME_SLOTS / n) as i64; + } + (n, 0) + } + FIXVAR | VARFIX => { + let abs = bits(br, 2)?; + let rels = bits(br, 2)?; + let n = rels + 1; + let rel = read_rel(br, rels)?; + let pointer = bits(br, pointer_bits(n))?; + for l in 0..n { + freq_res[if class == FIXVAR { n - 1 - l } else { l }] = br.read_flag()?; + } + if class == FIXVAR { + t[n] = (abs + TIME_SLOTS) as i64; + for l in (1..n).rev() { + t[l] = t[l + 1] - rel[n - 1 - l] as i64; + } + } else { + (t[0], t[n]) = (abs as i64, TIME_SLOTS as i64); + for l in 1..n { + t[l] = t[l - 1] + rel[l - 1] as i64; + } + } + (n, pointer) + } + _ => { + let abs0 = bits(br, 2)?; + let abs1 = bits(br, 2)? + TIME_SLOTS; + let (rels0, rels1) = (bits(br, 2)?, bits(br, 2)?); + let n = rels0 + rels1 + 1; + if n > MAX_ENV { + return Err(bad()); + } + let (rel0, rel1) = (read_rel(br, rels0)?, read_rel(br, rels1)?); + let pointer = bits(br, pointer_bits(n))?; + for r in freq_res.iter_mut().take(n) { + *r = br.read_flag()?; + } + (t[0], t[n]) = (abs0 as i64, abs1 as i64); + for l in 1..=rels0 { + t[l] = t[l - 1] + rel0[l - 1] as i64; + } + for l in (rels0 + 1..n).rev() { + t[l] = t[l + 1] - rel1[n - 1 - l] as i64; + } + (n, pointer) + } + }; + if t[0] < 0 || t[..=n].windows(2).any(|w| w[0] >= w[1]) { + return Err(bad()); + } + let la = match (pointer, class) { + (0, _) => None, + (p, FIXVAR | VARVAR) => Some((n + 1).checked_sub(p).ok_or_else(bad)?), + (p, _) => Some(p - 1), + }; + if la.is_some_and(|la| la > n) { + return Err(bad()); + } + let t_env: [usize; MAX_ENV + 1] = std::array::from_fn(|i| t[i] as usize); + let (num_noise, t_noise) = if n == 1 { + (1, [t_env[0], t_env[1], 0]) + } else { + let middle = match (class, pointer) { + (FIXFIX, _) => n / 2, + (VARFIX, 0) => 1, + (VARFIX, 1) => n - 1, + (VARFIX, p) => p - 1, + (_, 0 | 1) => n - 1, + (_, p) => (n + 1).checked_sub(p).ok_or_else(bad)?, + }; + if middle == 0 || middle >= n { + return Err(bad()); + } + (2, [t_env[0], t_env[middle], t_env[n]]) + }; + Ok(Grid { class, num_env: n, t_env, num_noise, t_noise, freq_res, la, amp_res: amp_res && !(class == FIXFIX && n == 1) }) + } +} + +/// The data of one channel as transmitted: delta-coded envelope and noise floor values (the +/// first frequency-coded value of each is absolute), not yet scaled for a balance channel. +#[derive(Clone, Debug)] +pub(crate) struct ChannelData { + pub(crate) grid: Grid, + pub(crate) df_env: [bool; MAX_ENV], + pub(crate) df_noise: [bool; 2], + pub(crate) invf: [u8; 5], + pub(crate) env: [[i32; 64]; MAX_ENV], + pub(crate) noise: [[i32; 5]; 2], + /// `bs_add_harmonic` per high-resolution band, when `bs_add_harmonic_flag` is set. + pub(crate) harmonic: Option<[bool; 64]>, +} + +impl ChannelData { + fn new(grid: Grid) -> ChannelData { + ChannelData { + grid, + df_env: [false; MAX_ENV], + df_noise: [false; 2], + invf: [0; 5], + env: [[0; 64]; MAX_ENV], + noise: [[0; 5]; 2], + harmonic: None, + } + } + + fn read_dtdf(&mut self, br: &mut BitReader) -> Result<()> { + for d in self.df_env.iter_mut().take(self.grid.num_env) { + *d = br.read_flag()?; + } + for d in self.df_noise.iter_mut().take(self.grid.num_noise) { + *d = br.read_flag()?; + } + Ok(()) + } + + fn read_invf(&mut self, br: &mut BitReader, t: &FreqTables) -> Result<()> { + for m in self.invf.iter_mut().take(t.n_q()) { + *m = br.read_bits(2)? as u8; + } + Ok(()) + } + + /// `sbr_envelope()`; `balance` for the second channel of a coupled pair. + fn read_envelope(&mut self, br: &mut BitReader, t: &FreqTables, balance: bool) -> Result<()> { + let (t_book, f_book, start_bits) = match (self.grid.amp_res, balance) { + (false, false) => (T_ENV_15, F_ENV_15, 7), + (false, true) => (T_BAL_15, F_BAL_15, 6), + (true, false) => (T_ENV_30, F_ENV_30, 6), + (true, true) => (T_BAL_30, F_BAL_30, 5), + }; + for l in 0..self.grid.num_env { + let n = t.bands(self.grid.freq_res[l]).len() - 1; + let row = &mut self.env[l]; + if self.df_env[l] { + for v in row.iter_mut().take(n) { + *v = huff(br, t_book)?; + } + } else { + row[0] = br.read_bits(start_bits)? as i32; + for v in row.iter_mut().take(n).skip(1) { + *v = huff(br, f_book)?; + } + } + } + Ok(()) + } + + /// `sbr_noise()`. + fn read_noise(&mut self, br: &mut BitReader, t: &FreqTables, balance: bool) -> Result<()> { + let (t_book, f_book) = if balance { (T_NOISE_BAL, F_BAL_30) } else { (T_NOISE, F_ENV_30) }; + for l in 0..self.grid.num_noise { + let row = &mut self.noise[l]; + if self.df_noise[l] { + for v in row.iter_mut().take(t.n_q()) { + *v = huff(br, t_book)?; + } + } else { + row[0] = br.read_bits(5)? as i32; + for v in row.iter_mut().take(t.n_q()).skip(1) { + *v = huff(br, f_book)?; + } + } + } + Ok(()) + } + + /// `bs_add_harmonic_flag` and `sbr_sinusoidal_coding()`. + fn read_harmonics(&mut self, br: &mut BitReader, t: &FreqTables) -> Result<()> { + if br.read_flag()? { + let mut h = [false; 64]; + for v in h.iter_mut().take(t.n_high()) { + *v = br.read_flag()?; + } + self.harmonic = Some(h); + } + Ok(()) + } +} + +/// Skip `bs_extended_data` (parametric stereo, for one, is not decoded). +fn skip_extended(br: &mut BitReader) -> Result<()> { + if br.read_flag()? { + let mut count = bits(br, 4)?; + if count == 15 { + count += bits(br, 8)?; + } + br.skip(8 * count)?; + } + Ok(()) +} + +/// The SBR data of one channel element. +#[derive(Clone, Debug)] +pub(crate) struct Frame { + /// The pair codes its second channel as a balance against the first. + pub(crate) coupling: bool, + pub(crate) channels: Vec, +} + +/// `sbr_single_channel_element()` (`channels` 1) or `sbr_channel_pair_element()` (2). +pub(crate) fn read_data(br: &mut BitReader, t: &FreqTables, amp_res: bool, channels: usize) -> Result { + if br.read_flag()? { + br.skip(4 * channels)?; // bs_reserved + } + if channels == 1 { + let mut c = ChannelData::new(Grid::read(br, amp_res)?); + c.read_dtdf(br)?; + c.read_invf(br, t)?; + c.read_envelope(br, t, false)?; + c.read_noise(br, t, false)?; + c.read_harmonics(br, t)?; + skip_extended(br)?; + return Ok(Frame { coupling: false, channels: vec![c] }); + } + let coupling = br.read_flag()?; + let (mut a, mut b); + if coupling { + a = ChannelData::new(Grid::read(br, amp_res)?); + b = ChannelData::new(a.grid.clone()); + a.read_dtdf(br)?; + b.read_dtdf(br)?; + a.read_invf(br, t)?; + b.invf = a.invf; + a.read_envelope(br, t, false)?; + a.read_noise(br, t, false)?; + b.read_envelope(br, t, true)?; + b.read_noise(br, t, true)?; + } else { + a = ChannelData::new(Grid::read(br, amp_res)?); + b = ChannelData::new(Grid::read(br, amp_res)?); + a.read_dtdf(br)?; + b.read_dtdf(br)?; + a.read_invf(br, t)?; + b.read_invf(br, t)?; + a.read_envelope(br, t, false)?; + b.read_envelope(br, t, false)?; + a.read_noise(br, t, false)?; + b.read_noise(br, t, false)?; + } + a.read_harmonics(br, t)?; + b.read_harmonics(br, t)?; + skip_extended(br)?; + Ok(Frame { coupling, channels: vec![a, b] }) +} + +#[cfg(test)] +mod tests { + use craft_media::bitstream::BitWriter; + + use super::*; + + /// Every SBR Huffman table is a complete prefix code, with the shortest codeword for a zero + /// delta, and its decoder reads back every codeword. + #[test] + fn huffman_tables_are_complete_prefix_codes() { + let tables: [&[(u32, u8)]; 10] = [ + &T_ENV_1_5DB, + &F_ENV_1_5DB, + &T_ENV_BAL_1_5DB, + &F_ENV_BAL_1_5DB, + &T_ENV_3_0DB, + &F_ENV_3_0DB, + &T_ENV_BAL_3_0DB, + &F_ENV_BAL_3_0DB, + &T_NOISE_3_0DB, + &T_NOISE_BAL_3_0DB, + ]; + let lavs = [60, 60, 24, 24, 31, 31, 12, 12, 31, 12]; + for (i, (codes, lav)) in tables.iter().zip(lavs).enumerate() { + assert_eq!(codes.len(), 2 * lav + 1, "table {i}"); + let max = u32::from(codes.iter().map(|c| c.1).max().unwrap()); + assert_eq!(codes.iter().map(|&(_, l)| 1u64 << (max - u32::from(l))).sum::(), 1 << max, "table {i}: Kraft sum"); + let shortest = codes.iter().map(|c| c.1).min().unwrap(); + assert_eq!(codes[lav].1, shortest, "table {i}"); + for (sym, &(code, len)) in codes.iter().enumerate() { + let mut bw = BitWriter::new(); + bw.write_bits(code, u32::from(len)); + bw.write_bits(0x5A5A5A, 24); + let data = bw.finish(); + let mut br = BitReader::new(&data); + assert_eq!(huff(&mut br, Book(i, lav as i32)).unwrap(), sym as i32 - lav as i32, "table {i}"); + assert_eq!(br.position(), usize::from(len)); + } + } + } + + fn grid(bits: &[(u32, u32)]) -> Result { + let mut bw = BitWriter::new(); + for &(v, n) in bits { + bw.write_bits(v, n); + } + bw.write_bits(0, 16); + let data = bw.finish(); + Grid::read(&mut BitReader::new(&data), true) + } + + /// The four frame classes give the borders, noise floors and transient envelope of + /// §4.6.18.3.3; a FIXFIX frame of one envelope uses 1.5 dB steps. + #[test] + fn grids_of_each_frame_class() { + // FIXFIX, 4 envelopes, high resolution + let g = grid(&[(FIXFIX as u32, 2), (2, 2), (1, 1)]).unwrap(); + assert_eq!((g.num_env, &g.t_env[..5], g.num_noise, g.t_noise, g.la, g.amp_res), (4, &[0, 4, 8, 12, 16][..], 2, [0, 8, 16], None, true)); + let g = grid(&[(FIXFIX as u32, 2), (0, 2), (0, 1)]).unwrap(); + assert_eq!((g.num_env, g.t_noise, g.amp_res), (1, [0, 16, 0], false)); + // FIXVAR: trailing border 16 + 2, two relative borders (4, 2) back from it, pointer 1, + // resolutions sent last envelope first + let g = grid(&[(FIXVAR as u32, 2), (2, 2), (2, 2), (1, 2), (0, 2), (1, 2), (0, 1), (1, 1), (1, 1)]).unwrap(); + assert_eq!((g.num_env, &g.t_env[..4]), (3, &[0, 12, 14, 18][..])); + assert_eq!((g.la, g.t_noise, g.freq_res[..3].to_vec()), (Some(3), [0, 14, 18], vec![true, true, false])); + // VARFIX: leading border 1, relative 2, pointer 2: l_A = 1 + let g = grid(&[(VARFIX as u32, 2), (1, 2), (1, 2), (0, 2), (2, 2), (1, 1), (0, 1)]).unwrap(); + assert_eq!((g.num_env, &g.t_env[..3], g.la, g.t_noise), (2, &[1, 3, 16][..], Some(1), [1, 3, 16])); + // VARVAR: 1 .. 17 with one border from each end + let g = grid(&[(VARVAR as u32, 2), (1, 2), (1, 2), (1, 2), (1, 2), (0, 2), (0, 2), (0, 2), (0, 1), (0, 1), (0, 1)]).unwrap(); + assert_eq!((g.num_env, &g.t_env[..4], g.la), (3, &[1, 3, 15, 17][..], None)); + } + + /// Grids whose borders do not increase, too many envelopes and a pointer past the envelopes + /// are refused. + #[test] + fn invalid_grids_are_refused() { + // FIXFIX with 8 envelopes + assert!(grid(&[(FIXFIX as u32, 2), (3, 2), (0, 1)]).is_err()); + // VARVAR with 7 envelopes + assert!(grid(&[(VARVAR as u32, 2), (0, 2), (0, 2), (3, 2), (3, 2)]).is_err()); + // FIXVAR whose relative borders run back past the start: 16 - (8 + 8 + 8) + assert!(grid(&[(FIXVAR as u32, 2), (0, 2), (3, 2), (3, 2), (3, 2), (3, 2), (0, 3), (0, 4)]).is_err()); + // VARFIX, 2 envelopes, pointer 3: the middle border would be the trailing one + assert!(grid(&[(VARFIX as u32, 2), (0, 2), (1, 2), (0, 2), (3, 2), (0, 1), (0, 1)]).is_err()); + } +} diff --git a/crates/audio/src/aac/sbr/freq.rs b/crates/audio/src/aac/sbr/freq.rs new file mode 100644 index 000000000..dc2809c10 --- /dev/null +++ b/crates/audio/src/aac/sbr/freq.rs @@ -0,0 +1,353 @@ +//! Frequency band tables (ISO/IEC 14496-3 §4.6.18.3.2) and the patches and limiter bands built on +//! them (§4.6.18.6.3, §4.6.18.7.3). Every table holds QMF channel borders. + +use super::Header; + +/// The most patches the HF generator may build. +const MAX_PATCHES: usize = 5; + +/// The tables of one SBR header at one output rate. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct FreqTables { + /// First QMF channel of the master table (the start of the SBR range before the crossover). + pub(crate) k0: usize, + /// First QMF channel of the SBR range. + pub(crate) kx: usize, + /// Channels in the SBR range (`kx + m` is its end). + pub(crate) m: usize, + /// High-resolution envelope bands (`n_high + 1` borders). + pub(crate) high: Vec, + /// Low-resolution envelope bands. + pub(crate) low: Vec, + /// Noise floor bands (1 to 5). + pub(crate) noise: Vec, + /// Limiter bands. + pub(crate) lim: Vec, + /// `(first source channel, channels)` of each patch, in order from `kx` up. + pub(crate) patches: Vec<(usize, usize)>, +} + +/// Round half up, as the specification's `NINT`. +fn nint(x: f64) -> i64 { + (x + 0.5).floor() as i64 +} + +/// The start channel offsets of `bs_start_freq`, by output rate (§4.6.18.3.2.1). +fn start_offsets(fs: u32) -> &'static [i8; 16] { + match fs { + 0..=16_000 => &[-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7], + 16_001..=22_050 => &[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13], + 22_051..=24_000 => &[-5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16], + 24_001..=32_000 => &[-6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16], + 32_001..=64_000 => &[-4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20], + _ => &[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24], + } +} + +/// `NINT(f · 128 / fs)` for the 3/4/5 kHz (start) or 6/8/10 kHz (stop) minimum at `fs`. +fn min_channel(fs: u32, low: f64, mid: f64, high: f64) -> i64 { + let f = if fs < 32_000 { + low + } else if fs < 64_000 { + mid + } else { + high + }; + nint(f * 128.0 / f64::from(fs)) +} + +/// The start and stop channels `k0`, `k2` of the master table. +fn start_stop(h: &Header, fs: u32) -> Option<(usize, usize)> { + let k0 = min_channel(fs, 3000.0, 4000.0, 5000.0) + i64::from(start_offsets(fs)[usize::from(h.start_freq & 15)]); + let k2 = match h.stop_freq { + 14 => 2 * k0, + 15 => 3 * k0, + s => { + let stop_min = min_channel(fs, 6000.0, 8000.0, 10_000.0); + if stop_min <= 0 { + return None; + } + let ratio = 64.0 / stop_min as f64; + let at = |p: i64| nint(stop_min as f64 * ratio.powf(p as f64 / 13.0)); + let mut dk: Vec = (0..13).map(|p| at(p + 1) - at(p)).collect(); + dk.sort_unstable(); + stop_min + dk.iter().take(usize::from(s)).sum::() + } + } + .min(64); + // the low band reaches only the analysis bank's 32 channels + (1..32).contains(&k0).then_some(())?; + (k2 > k0).then_some((k0 as usize, k2 as usize)) +} + +/// The master table for `bs_freq_scale` 0: bands of one or two channels. +fn master_linear(k0: usize, k2: usize, alter_scale: bool) -> Option> { + let (dk, bands) = if alter_scale { (2, 2 * nint((k2 - k0) as f64 / 4.0) as usize) } else { (1, 2 * ((k2 - k0) / 2)) }; + if bands == 0 { + return None; + } + let mut v = vec![dk as i64; bands]; + let mut diff = k2 as i64 - (k0 + bands * dk) as i64; + // take the difference from the lowest bands (too wide) or give it to the highest (too narrow) + let mut k = if diff < 0 { 0 } else { bands - 1 }; + while diff != 0 { + let step = if diff < 0 { 1 } else { -1 }; + *v.get_mut(k)? -= step; + diff += step; + k = if step == 1 { k + 1 } else { k.checked_sub(1)? }; + } + cumulative(k0, &v) +} + +/// The borders `start, start + d[0], …`; `None` unless every band is at least a channel wide. +fn cumulative(start: usize, d: &[i64]) -> Option> { + let mut out = Vec::with_capacity(d.len() + 1); + out.push(start); + let mut at = start as i64; + for &d in d { + if d <= 0 { + return None; + } + at += d; + out.push(at as usize); + } + Some(out) +} + +/// The band widths of `2 · NINT(bands · log2(hi / lo) / (2 · warp))` logarithmic bands. +fn log_widths(lo: usize, hi: usize, bands: f64, warp: f64) -> Vec { + let ratio = hi as f64 / lo as f64; + let n = 2 * nint(bands * ratio.log2() / (2.0 * warp)); + let at = |k: i64| nint(lo as f64 * ratio.powf(k as f64 / n as f64)); + let mut d: Vec = (0..n.max(0)).map(|k| at(k + 1) - at(k)).collect(); + d.sort_unstable(); + d +} + +/// The master table for `bs_freq_scale` 1–3: 12, 10 or 8 bands per octave, warped above +/// `2 · k0` by `bs_alter_scale`. +fn master_log(k0: usize, k2: usize, freq_scale: u8, alter_scale: bool) -> Option> { + let bands = [12.0, 10.0, 8.0][usize::from(freq_scale.clamp(1, 3) - 1)]; + let two_regions = k2 as f64 / k0 as f64 > 2.2449; + let k1 = if two_regions { 2 * k0 } else { k2 }; + let d0 = log_widths(k0, k1, bands, 1.0); + let mut table = cumulative(k0, &d0)?; + if table.len() < 2 { + return None; + } + if two_regions { + let mut d1 = log_widths(k1, k2, bands, if alter_scale { 1.3 } else { 1.0 }); + let max0 = d0.iter().copied().max()?; + if d1.first().is_some_and(|&d| d < max0) { + // widen the first upper band to the widest lower one, at the cost of the last + let change = max0 - d1[0]; + d1[0] = max0; + *d1.last_mut()? -= change; + d1.sort_unstable(); + } + table.extend(cumulative(k1, &d1)?.into_iter().skip(1)); + } + Some(table) +} + +impl FreqTables { + /// The tables of `h` at output rate `fs`, or `None` if the header asks for an invalid layout. + pub(crate) fn new(h: &Header, fs: u32) -> Option { + let (k0, k2) = start_stop(h, fs)?; + let master = if h.freq_scale == 0 { master_linear(k0, k2, h.alter_scale)? } else { master_log(k0, k2, h.freq_scale, h.alter_scale)? }; + let n_master = master.len().checked_sub(1)?; + let xover = usize::from(h.xover_band); + if xover >= n_master { + return None; + } + let high: Vec = master.get(xover..)?.to_vec(); + let n_high = high.len() - 1; + let (kx, end) = (high[0], *high.last()?); + if kx > 32 || end > 64 || end <= kx { + return None; + } + let m = end - kx; + // every other border, keeping the first (and the second when the count is odd) + let n_low = n_high - n_high / 2; + let low: Vec = (0..=n_low).map(|k| high[if k == 0 { 0 } else { 2 * k - (n_high & 1) }]).collect(); + let n_q = if h.noise_bands == 0 { 1 } else { (nint(f64::from(h.noise_bands) * (end as f64 / kx as f64).log2()) as usize).max(1) }; + if n_q > 5 || n_q > n_low { + return None; + } + let mut noise = vec![low[0]]; + let mut i = 0; + for k in 1..=n_q { + i += (n_low - i) / (n_q + 1 - k); + noise.push(*low.get(i)?); + } + let patches = patches(&master, k0, kx, m, fs)?; + let lim = limiter(&low, &patches, kx, h.limiter_bands); + Some(FreqTables { k0, kx, m, high, low, noise, lim, patches }) + } + + /// High-resolution envelope bands. + pub(crate) fn n_high(&self) -> usize { + self.high.len() - 1 + } + + /// Noise floor bands. + pub(crate) fn n_q(&self) -> usize { + self.noise.len() - 1 + } + + /// The envelope band table of a resolution (`true`: high). + pub(crate) fn bands(&self, high_res: bool) -> &[usize] { + if high_res { &self.high } else { &self.low } + } +} + +/// Map the low band onto the SBR range (§4.6.18.6.3): runs of source channels, each ending below +/// `k0`, that together cover `kx .. kx + m`. +fn patches(master: &[usize], k0: usize, kx: usize, m: usize, fs: u32) -> Option> { + let n_master = master.len() - 1; + let goal = nint(2.048e6 / f64::from(fs)).max(0) as usize; + let mut k = if goal < kx + m { master.iter().position(|&f| f >= goal).unwrap_or(n_master) } else { n_master }; + let (mut msb, mut usb) = (k0, kx); + let mut out: Vec<(usize, usize)> = Vec::new(); + // each pass either adds a patch or lowers `msb` to `kx`; a stream that does neither twice + // running cannot finish + for _ in 0..2 * (MAX_PATCHES + 1) { + // the highest master border the patch can reach from below k0 + let mut j = k + 1; + let (sb, odd) = loop { + j = j.checked_sub(1)?; + let sb = *master.get(j)?; + let odd = (sb + k0) % 2; + if sb + odd < k0 + msb { + break (sb, odd); + } + }; + let count = sb.saturating_sub(usb); + if count > 0 { + let start = (k0 - odd).checked_sub(count)?; + out.push((start, count)); + if out.len() > MAX_PATCHES { + return None; + } + (usb, msb) = (sb, sb); + } else { + msb = kx; + } + if master.get(k)?.saturating_sub(sb) < 3 { + k = n_master; + } + if sb == kx + m { + if out.len() > 1 && out.last().is_some_and(|p| p.1 < 3) { + out.pop(); + } + return (!out.is_empty()).then_some(out); + } + } + None +} + +/// The limiter band table (§4.6.18.7.3): one band over the range, or about 1.2, 2 or 3 bands per +/// octave on the low-resolution and patch borders. +fn limiter(low: &[usize], patches: &[(usize, usize)], kx: usize, limiter_bands: u8) -> Vec { + let (Some(&first), Some(&last)) = (low.first(), low.last()) else { + return Vec::new(); + }; + if limiter_bands == 0 { + return vec![first, last]; + } + let per_octave = [1.2, 2.0, 3.0][usize::from(limiter_bands.clamp(1, 3) - 1)]; + let mut borders = vec![kx]; + for p in patches { + borders.push(borders.last().copied().unwrap_or(kx) + p.1); + } + let mut t: Vec = low.to_vec(); + t.extend(borders.iter().skip(1).take(patches.len().saturating_sub(1))); + t.sort_unstable(); + let is_border = |v: usize| borders.contains(&v); + let mut k = 1; + while k < t.len() { + let octaves = if t[k - 1] == 0 { f64::INFINITY } else { (t[k] as f64 / t[k - 1] as f64).log2() }; + if octaves * per_octave >= 0.49 { + k += 1; + } else if t[k] == t[k - 1] || !is_border(t[k]) { + t.remove(k); + } else if !is_border(t[k - 1]) { + t.remove(k - 1); + } else { + k += 1; + } + } + if t.len() < 2 { + return vec![first, last]; + } + t +} + +#[cfg(test)] +mod tests { + use super::*; + + fn header(start: u8, stop: u8, xover: u8, freq_scale: u8, alter: bool, noise: u8) -> Header { + Header { + amp_res: true, + start_freq: start, + stop_freq: stop, + xover_band: xover, + freq_scale, + alter_scale: alter, + noise_bands: noise, + limiter_bands: 2, + limiter_gains: 2, + interpol_freq: true, + smoothing_mode: true, + } + } + + /// Every header field combination at every output rate gives tables that are strictly + /// increasing, start at `kx`, end at `kx + m`, keep the low band within the analysis bank and + /// patch from below `k0` (or is refused). + #[test] + fn every_header_gives_consistent_tables_or_none() { + let mut valid = 0; + for fs in [16_000, 22_050, 24_000, 32_000, 44_100, 48_000, 64_000, 88_200, 96_000] { + for start in 0..16 { + for stop in 0..16 { + for xover in 0..8 { + for fsc in 0..4 { + for alter in [false, true] { + for noise in 0..4 { + let Some(t) = FreqTables::new(&header(start, stop, xover, fsc, alter, noise), fs) else { continue }; + valid += 1; + for v in [&t.high, &t.low, &t.noise, &t.lim] { + assert!(v.windows(2).all(|w| w[0] < w[1]), "{v:?}"); + assert_eq!(v[0], t.kx); + } + for v in [&t.high, &t.low, &t.noise] { + assert_eq!(*v.last().unwrap(), t.kx + t.m); + } + assert!(t.kx <= 32 && t.kx + t.m <= 64 && t.n_q() <= 5); + // a last patch of under 3 channels is dropped, and the limiter + // bands may then stop short of the range's end + assert!(t.patches.iter().map(|p| p.1).sum::() <= t.m); + assert!(t.lim.len() >= 2 && *t.lim.last().unwrap() <= t.kx + t.m); + assert!(t.patches.iter().all(|&(s, n)| s + n <= t.k0)); + } + } + } + } + } + } + } + assert!(valid > 10_000, "{valid}"); + } + + /// A typical 44.1 kHz header: start channel 12 + offset, stop near 64, three noise bands. + #[test] + fn a_44k_header_has_the_expected_layout() { + let t = FreqTables::new(&header(5, 9, 0, 2, true, 2), 44_100).unwrap(); + // startMin 12 + offset 2 = 14 + assert_eq!((t.k0, t.kx), (14, 14)); + assert_eq!(t.low.len() - 1, t.n_high() - t.n_high() / 2); + assert!(t.n_q() >= 1); + } +} diff --git a/crates/audio/src/aac/sbr/hf.rs b/crates/audio/src/aac/sbr/hf.rs new file mode 100644 index 000000000..43ae572cf --- /dev/null +++ b/crates/audio/src/aac/sbr/hf.rs @@ -0,0 +1,332 @@ +//! HF generation (ISO/IEC 14496-3 §4.6.18.6) and HF adjustment (§4.6.18.7) of one channel. + +use super::bitstream::Grid; +use super::freq::FreqTables; +use super::tables::NOISE; +use super::{Cx, Header, MAX_ENV, T_HF_ADJ, X_SLOTS}; + +/// Low band subband samples of a frame: slots -8 .. 32 relative to the frame (`X_Low`). +pub(crate) type LowBand = [[Cx; 32]; X_SLOTS]; +/// High band subband samples, at slot `l + T_HF_ADJ` for QMF slot `l` of the frame's envelopes. +pub(crate) type HighBand = [[Cx; 64]; X_SLOTS]; + +/// The per-channel state the HF tools carry from frame to frame. +#[derive(Clone, Debug)] +pub(crate) struct History { + /// `bs_invf_mode` and chirp factor per noise band. + invf: [u8; 5], + bw: [f32; 5], + /// The last frame's transient envelope was its last (`l_A = L_E`): no noise in this frame's + /// first. + la_at_end: bool, + /// Sinusoids present in the last envelope, by channel above `kx`. + sine: [bool; 64], + /// Gains and noise levels of the last four slots, newest first (smoothing). + gains: [[f32; 64]; 4], + noise_levels: [[f32; 64]; 4], + /// The smoothing history holds data of the current tables. + primed: bool, + noise_index: usize, + sine_index: usize, +} + +impl History { + pub(crate) fn new() -> History { + History { + invf: [0; 5], + bw: [0.0; 5], + la_at_end: false, + sine: [false; 64], + gains: [[0.0; 64]; 4], + noise_levels: [[0.0; 64]; 4], + primed: false, + noise_index: 0, + sine_index: 0, + } + } + + /// Forget what depends on the frequency tables (after a header that changes them). + pub(crate) fn reset_tables(&mut self) { + self.sine = [false; 64]; + self.primed = false; + self.noise_index = 0; + } +} + +/// The chirp factor of a noise band from its inverse filtering mode and the last frame's. +fn chirp(mode: u8, prev_mode: u8, prev_bw: f32) -> f32 { + let new_bw = match (mode, prev_mode) { + (0, 1) => 0.6, + (0, _) => 0.0, + (1, 0) => 0.6, + (1, _) => 0.75, + (2, _) => 0.9, + _ => 0.98, + }; + let bw = if new_bw < prev_bw { 0.75 * new_bw + 0.25 * prev_bw } else { 0.90625 * new_bw + 0.09375 * prev_bw }; + if bw < 0.015625 { 0.0 } else { bw } +} + +/// Second order linear prediction of one low band channel over the frame and the 6 slots +/// before it (the covariance method, §4.6.18.6.2): `(α0, α1)`. +fn predictor(x: &LowBand, k: usize) -> (Cx, Cx) { + // φ(i, j) = Σ x(n − i + 2) · conj(x(n − j + 2)), n = 0 .. 37 + let phi = |i: usize, j: usize| { + let (mut re, mut im) = (0f64, 0f64); + for n in 0..SLOTS_PLUS_6 { + let (a, b) = (x[n + 2 - i][k], x[n + 2 - j][k]); + re += f64::from(a.re) * f64::from(b.re) + f64::from(a.im) * f64::from(b.im); + im += f64::from(a.im) * f64::from(b.re) - f64::from(a.re) * f64::from(b.im); + } + (re, im) + }; + let (p01, p02, p11, p12, p22) = (phi(0, 1), phi(0, 2), phi(1, 1).0, phi(1, 2), phi(2, 2).0); + let d = p22 * p11 - (p12.0 * p12.0 + p12.1 * p12.1) / (1.0 + 1e-6); + let a1 = if d != 0.0 { + // (φ01 · φ12 − φ02 · φ11) / d + ((p01.0 * p12.0 - p01.1 * p12.1 - p02.0 * p11) / d, (p01.0 * p12.1 + p01.1 * p12.0 - p02.1 * p11) / d) + } else { + (0.0, 0.0) + }; + let a0 = if p11 != 0.0 { + // −(φ01 + α1 · conj(φ12)) / φ11 + (-(p01.0 + a1.0 * p12.0 + a1.1 * p12.1) / p11, -(p01.1 + a1.1 * p12.0 - a1.0 * p12.1) / p11) + } else { + (0.0, 0.0) + }; + let mag = |a: (f64, f64)| (a.0 * a.0 + a.1 * a.1).sqrt(); + if mag(a0) >= 4.0 || mag(a1) >= 4.0 { + return (Cx::default(), Cx::default()); + } + (Cx { re: a0.0 as f32, im: a0.1 as f32 }, Cx { re: a1.0 as f32, im: a1.1 as f32 }) +} + +/// Slots of the covariance sums: the frame's 32 and 6 before it. +const SLOTS_PLUS_6: usize = super::SLOTS + 6; + +/// The index of the band of `bands` holding channel `k` (bands are increasing borders). +fn band_of(bands: &[usize], k: usize) -> usize { + bands.partition_point(|&b| b <= k).saturating_sub(1).min(bands.len().saturating_sub(2)) +} + +/// Patch the low band into the SBR range, inverse filtered (§4.6.18.6.3), for QMF slots +/// `from .. to` of the frame. +pub(crate) fn generate(x: &LowBand, t: &FreqTables, invf: &[u8; 5], h: &mut History, (from, to): (usize, usize), out: &mut HighBand) { + let mut bw = [0f32; 5]; + for i in 0..t.n_q() { + bw[i] = chirp(invf[i], h.invf[i], h.bw[i]); + } + (h.invf, h.bw) = (*invf, bw); + let mut pred: [Option<(Cx, Cx)>; 32] = [None; 32]; + let mut k = t.kx; + for &(start, count) in &t.patches { + for p in start..start + count { + let (a0, a1) = *pred[p].get_or_insert_with(|| predictor(x, p)); + let g = bw[band_of(&t.noise, k)]; + let (c0, c1) = (a0.scale(g), a1.scale(g * g)); + for l in from..to { + let i = l + T_HF_ADJ; + out[i][k] = x[i][p] + c0 * x[i - 1][p] + c1 * x[i - 2][p]; + } + k += 1; + } + } +} + +/// Dequantised envelope and noise floor values of one channel in one frame. +#[derive(Clone, Debug)] +pub(crate) struct Levels { + /// Envelope energy per envelope and band (of that envelope's resolution). + pub(crate) energy: [[f32; 64]; MAX_ENV], + /// Noise to signal ratio per noise floor and noise band. + pub(crate) noise: [[f32; 5]; 2], +} + +/// Gains for one envelope, per channel of the SBR range. +struct EnvelopeGains { + gain: [f32; 64], + noise: [f32; 64], + sine: [f32; 64], +} + +const LIMITER_GAINS: [f32; 4] = [0.70795, 1.0, 1.41254, 1e10]; +/// `h_smooth`, as the specification gives it. +#[allow(clippy::excessive_precision)] +const SMOOTHING: [f32; 5] = [0.33333333333333, 0.30150283239582, 0.21816949906249, 0.11516383427084, 0.03183050093751]; +const EPS: f32 = 1.0; +const EPS0: f32 = 1e-12; + +/// What the adjustment of one channel's frame works from. +pub(crate) struct Frame<'a> { + /// The generated high band. + pub(crate) x: &'a HighBand, + pub(crate) t: &'a FreqTables, + pub(crate) hdr: &'a Header, + pub(crate) g: &'a Grid, + pub(crate) lv: &'a Levels, + /// `bs_add_harmonic`, when the frame codes sinusoids. + pub(crate) harmonic: Option<&'a [bool; 64]>, +} + +/// The gains of envelope `l` (§4.6.18.7.3–4): `delta` is 0 for a transient envelope, which gets +/// no noise. +fn envelope_gains(f: &Frame, l: usize, sine_index: &[bool; 64], delta: bool) -> EnvelopeGains { + let Frame { x, t, hdr, g, lv, .. } = *f; + let (kx, m) = (t.kx, t.m); + let bands = t.bands(g.freq_res[l]); + let (from, to) = (2 * g.t_env[l] + T_HF_ADJ, 2 * g.t_env[l + 1] + T_HF_ADJ); + let slots = (to - from) as f32; + let floor = usize::from(g.num_noise == 2 && g.t_env[l] >= g.t_noise[1]); + // per channel: original energy, current energy, noise ratio, sinusoid in its band + let mut e_orig = [0f32; 64]; + let mut e_curr = [0f32; 64]; + let mut q = [0f32; 64]; + let mut s_band = [false; 64]; + for j in 0..bands.len() - 1 { + let (lo, hi) = (bands[j] - kx, bands[j + 1] - kx); + let any_sine = sine_index[lo..hi].iter().any(|&s| s); + let mut sum = 0f32; + for c in lo..hi { + let ch = c + kx; + let e: f32 = (from..to).map(|i| x[i][ch].norm_sqr()).sum::() / slots; + e_curr[c] = e; + sum += e; + e_orig[c] = lv.energy[l][j]; + q[c] = lv.noise[floor][band_of(&t.noise, ch)]; + s_band[c] = any_sine; + } + if !hdr.interpol_freq { + e_curr[lo..hi].fill(sum / (hi - lo) as f32); + } + } + let mut out = EnvelopeGains { gain: [0.0; 64], noise: [0.0; 64], sine: [0.0; 64] }; + for c in 0..m { + let (eo, ec, q) = (e_orig[c], e_curr[c], q[c]); + out.noise[c] = (eo * q / (1.0 + q)).sqrt(); + if sine_index[c] { + out.sine[c] = (eo / (1.0 + q)).sqrt(); + } + out.gain[c] = + if s_band[c] { (eo / (EPS + ec) * q / (1.0 + q)).sqrt() } else { (eo / ((EPS + ec) * (1.0 + if delta { q } else { 0.0 }))).sqrt() }; + } + // limit per limiter band, then boost back the energy the limiter took + let lim_gain = LIMITER_GAINS[usize::from(hdr.limiter_gains & 3)]; + for w in t.lim.windows(2) { + let (lo, hi) = (w[0] - kx, w[1] - kx); + let (so, sc) = (e_orig[lo..hi].iter().sum::(), e_curr[lo..hi].iter().sum::()); + let max = (lim_gain * ((EPS0 + so) / (EPS0 + sc)).sqrt()).min(1e5); + let mut den = 0f32; + for c in lo..hi { + if out.gain[c] > max { + out.noise[c] *= max / out.gain[c]; + out.gain[c] = max; + } + den += e_curr[c] * out.gain[c] * out.gain[c]; + if out.sine[c] != 0.0 { + den += out.sine[c] * out.sine[c]; + } else if delta { + den += out.noise[c] * out.noise[c]; + } + } + let boost = ((EPS0 + so) / (EPS0 + den)).sqrt().min(1.584_893_2); + for c in lo..hi { + out.gain[c] *= boost; + out.noise[c] *= boost; + out.sine[c] *= boost; + } + } + out +} + +/// Adjust the generated high band to the transmitted envelopes and add noise and sinusoids +/// (§4.6.18.7), for the frame's envelopes, into `y`. +pub(crate) fn adjust(f: &Frame, h: &mut History, y: &mut HighBand) { + let Frame { x, t, hdr, g, harmonic, .. } = *f; + let (kx, m) = (t.kx, t.m); + let smooth = !hdr.smoothing_mode; + let prev_la_at_end = h.la_at_end; + for l in 0..g.num_env { + // sinusoids start at the transient envelope, unless one was already playing in the band + let mut sine_index = [false; 64]; + if let Some(harm) = harmonic { + for i in 0..t.n_high() { + let c = (t.high[i] + t.high[i + 1]) / 2 - kx; + sine_index[c] = harm[i] && (g.la.is_none_or(|la| l >= la) || h.sine[c]); + } + } + let transient = g.la == Some(l) || (l == 0 && prev_la_at_end); + let e = envelope_gains(f, l, &sine_index, !transient); + if !h.primed { + h.gains = [e.gain; 4]; + h.noise_levels = [e.noise; 4]; + h.primed = true; + } + for i in 2 * g.t_env[l]..2 * g.t_env[l + 1] { + let (mut gain, mut noise) = (e.gain, e.noise); + if smooth && !transient { + for c in 0..m { + gain[c] = SMOOTHING[0] * e.gain[c] + (0..4).map(|j| SMOOTHING[j + 1] * h.gains[j][c]).sum::(); + noise[c] = SMOOTHING[0] * e.noise[c] + (0..4).map(|j| SMOOTHING[j + 1] * h.noise_levels[j][c]).sum::(); + } + } + h.gains.rotate_right(1); + h.noise_levels.rotate_right(1); + (h.gains[0], h.noise_levels[0]) = (e.gain, e.noise); + let (sin_re, sin_im) = [(1.0, 0.0), (0.0, 1.0), (-1.0, 0.0), (0.0, -1.0)][h.sine_index]; + let row = &mut y[i + T_HF_ADJ]; + for c in 0..m { + let ch = c + kx; + let mut v = x[i + T_HF_ADJ][ch].scale(gain[c]); + h.noise_index = (h.noise_index + 1) & 511; + if e.sine[c] != 0.0 { + let sign = if ch % 2 == 0 { 1.0 } else { -1.0 }; + v.re += e.sine[c] * sin_re; + v.im += e.sine[c] * sin_im * sign; + } else if !transient { + let [nr, ni] = NOISE[h.noise_index]; + v.re += noise[c] * nr; + v.im += noise[c] * ni; + } + row[ch] = v; + } + h.sine_index = (h.sine_index + 1) & 3; + } + if l + 1 == g.num_env { + h.sine = sine_index; + } + } + h.la_at_end = g.la == Some(g.num_env); +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The chirp factor follows §4.6.18.6.2: rising quickly, falling slowly, and zero below 1/64. + #[test] + fn chirp_factors() { + assert!((chirp(3, 3, 0.0) - 0.90625 * 0.98).abs() < 1e-6); + assert!((chirp(0, 3, 0.98) - 0.25 * 0.98).abs() < 1e-6); + assert_eq!(chirp(0, 0, 0.05), 0.0); + assert!((chirp(1, 0, 0.0) - 0.90625 * 0.6).abs() < 1e-6); + } + + /// A channel that is a decaying complex exponential is predicted exactly by its first order + /// term. + #[test] + fn prediction_of_a_resonance() { + let mut x: LowBand = [[Cx::default(); 32]; X_SLOTS]; + let r = Cx { re: 0.9 * 0.3f32.cos(), im: 0.9 * 0.3f32.sin() }; + let mut v = Cx { re: 1.0, im: 0.0 }; + for slot in x.iter_mut() { + slot[3] = v; + v = v * r; + } + let (a0, a1) = predictor(&x, 3); + // x(n) + α0·x(n−1) + α1·x(n−2) = 0 with α0 = −r, α1 = 0 + assert!((a0.re + r.re).abs() < 1e-3 && (a0.im + r.im).abs() < 1e-3, "{a0:?}"); + assert!(a1.re.abs() < 1e-3 && a1.im.abs() < 1e-3, "{a1:?}"); + // a silent channel has no predictor + assert_eq!(predictor(&x, 4), (Cx::default(), Cx::default())); + } +} diff --git a/crates/audio/src/aac/sbr/mod.rs b/crates/audio/src/aac/sbr/mod.rs new file mode 100644 index 000000000..32a6c55b0 --- /dev/null +++ b/crates/audio/src/aac/sbr/mod.rs @@ -0,0 +1,513 @@ +//! Spectral band replication (HE-AAC, ISO/IEC 14496-3 §4.6.18): rebuilds the band above the +//! AAC-LC core from the core's own low band and the envelope data of the stream's SBR fill +//! elements, at twice the core rate. +//! +//! Each channel's core output goes through a 32-band QMF analysis bank. The HF generator copies +//! (patches) low band channels into the SBR range, inverse filtered; the HF adjuster scales them +//! to the transmitted envelopes and adds noise and sinusoids; a 64-band QMF synthesis bank merges +//! the delayed low band and the new high band. A frame without usable SBR data (none sent yet, or +//! damaged) still passes through the banks, so the output keeps its rate and delay. +//! +//! Written from the SBR description and tables of ETSI ES 201 980 (DRM, Annex I has the tables it +//! shares with ISO/IEC 14496-3) and the decoder process of ISO/IEC 14496-3. Parametric stereo +//! (HE-AAC v2) is not decoded: its extension is skipped and the mono core is extended alone. + +mod bitstream; +mod freq; +mod hf; +mod qmf; +mod tables; + +use std::ops::{Add, Mul}; + +use craft_media::bitstream::BitReader; + +use self::bitstream::{ChannelData, Frame}; +use self::freq::FreqTables; +use self::hf::{HighBand, History, Levels, LowBand}; +use self::qmf::{Analysis, Synthesis}; + +/// QMF slots per frame: 1024 core samples in slots of 32. +pub(crate) const SLOTS: usize = 32; +/// The most envelopes in a frame. +pub(crate) const MAX_ENV: usize = 5; +/// Slots of the last frame kept ahead of this one's low band (`t_HFGen`). +const T_HF_GEN: usize = 8; +/// The SBR frame lags the core frame by `T_HF_GEN − T_HF_ADJ` slots. +pub(crate) const T_HF_ADJ: usize = 2; +/// Slots in the low and high band buffers: the frame and `T_HF_GEN` before it. +pub(crate) const X_SLOTS: usize = SLOTS + T_HF_GEN; +/// The 16-bit full scale the specification's energies are in. +const PCM_SCALE: f32 = 32768.0; +/// `NOISE_FLOOR_OFFSET`. +const NOISE_OFFSET: i32 = 6; + +/// Output samples by which the SBR decoder delays the core: the filter banks, and the SBR frame +/// lagging the core frame by `T_HF_GEN − T_HF_ADJ` slots of 64. +pub const DELAY: usize = qmf::DELAY + 64 * (T_HF_GEN - T_HF_ADJ); + +/// A complex subband sample. +#[derive(Clone, Copy, Default, Debug, PartialEq)] +pub(crate) struct Cx { + pub(crate) re: f32, + pub(crate) im: f32, +} + +impl Cx { + fn scale(self, g: f32) -> Cx { + Cx { re: self.re * g, im: self.im * g } + } + fn norm_sqr(self) -> f32 { + self.re * self.re + self.im * self.im + } +} + +impl Add for Cx { + type Output = Cx; + fn add(self, o: Cx) -> Cx { + Cx { re: self.re + o.re, im: self.im + o.im } + } +} + +impl Mul for Cx { + type Output = Cx; + fn mul(self, o: Cx) -> Cx { + Cx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re } + } +} + +/// `sbr_header()` (§4.5.2.8.1). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Header { + pub(crate) amp_res: bool, + pub(crate) start_freq: u8, + pub(crate) stop_freq: u8, + pub(crate) xover_band: u8, + pub(crate) freq_scale: u8, + pub(crate) alter_scale: bool, + pub(crate) noise_bands: u8, + pub(crate) limiter_bands: u8, + pub(crate) limiter_gains: u8, + pub(crate) interpol_freq: bool, + pub(crate) smoothing_mode: bool, +} + +impl Header { + /// Whether `o` keeps the frequency band tables (a change of any of these fields is an SBR + /// reset). + fn same_tables(&self, o: &Header) -> bool { + (self.start_freq, self.stop_freq, self.freq_scale, self.alter_scale, self.xover_band, self.noise_bands) + == (o.start_freq, o.stop_freq, o.freq_scale, o.alter_scale, o.xover_band, o.noise_bands) + } +} + +/// Where an element's SBR data is: the access unit, and the bit range of the fill element's +/// payload after its extension type. +#[derive(Clone, Copy, Debug)] +pub(crate) struct Payload<'a> { + pub(crate) data: &'a [u8], + pub(crate) start: usize, + pub(crate) end: usize, + /// `EXT_SBR_DATA_CRC`: a 10-bit CRC leads the data. + pub(crate) crc: bool, +} + +/// Quantised envelope values per envelope and band, and noise floor values per floor and band. +type Quantised = ([[i32; 64]; MAX_ENV], [[i32; 5]; 2]); + +/// One channel's state across frames. +#[derive(Clone)] +struct Channel { + analysis: Analysis, + synthesis: Synthesis, + /// Low band of the frame, with the last `T_HF_GEN` slots of the frame before ahead of it. + x_low: Box, + /// Adjusted high band of the last frame (its envelopes may reach into this one). + y_prev: Box, + /// Where `y_prev` holds data: QMF channels, and the end of its slots. + y_prev_range: (usize, usize), + y_prev_end: usize, + /// Quantised envelope values of the last envelope, and its resolution; noise floor values of + /// the last noise floor (time-direction delta coding). + env: [i32; 64], + env_high_res: bool, + noise: [i32; 5], + history: History, +} + +impl Channel { + fn new() -> Channel { + Channel { + analysis: Analysis::new(), + synthesis: Synthesis::new(), + x_low: Box::new([[Cx::default(); 32]; X_SLOTS]), + y_prev: Box::new([[Cx::default(); 64]; X_SLOTS]), + y_prev_range: (0, 0), + y_prev_end: 0, + env: [0; 64], + env_high_res: false, + noise: [0; 5], + history: History::new(), + } + } + + /// Decode the delta-coded envelope and noise floor values (§4.6.18.3.4); `scale` is 2 for a + /// balance channel. + fn absolute(&mut self, d: &ChannelData, t: &FreqTables, scale: i32) -> Quantised { + let mut env = [[0i32; 64]; MAX_ENV]; + for l in 0..d.grid.num_env { + let res = d.grid.freq_res[l]; + let n = t.bands(res).len() - 1; + if d.df_env[l] { + for k in 0..n { + // the value this band refers to in the last envelope, mapped across resolutions + let reference = if res == self.env_high_res { + k + } else if res { + hf_band_of(t.bands(false), t.high[k]) + } else { + t.high.iter().position(|&b| b == t.low[k]).unwrap_or(0) + }; + env[l][k] = self.env.get(reference).copied().unwrap_or(0) + scale * d.env[l][k]; + } + } else { + env[l][0] = scale * d.env[l][0]; + for k in 1..n { + env[l][k] = env[l][k - 1] + scale * d.env[l][k]; + } + } + (self.env, self.env_high_res) = (env[l], res); + } + let mut noise = [[0i32; 5]; 2]; + for l in 0..d.grid.num_noise { + for k in 0..t.n_q() { + let delta = scale * d.noise[l][k]; + noise[l][k] = if d.df_noise[l] { + self.noise[k] + delta + } else if k == 0 { + delta + } else { + noise[l][k - 1] + delta + }; + } + self.noise = noise[l]; + } + (env, noise) + } +} + +/// The index of the band of `bands` holding channel `k`. +fn hf_band_of(bands: &[usize], k: usize) -> usize { + bands.partition_point(|&b| b <= k).saturating_sub(1) +} + +/// `2^x`, kept to a range whose energies stay finite. +fn exp2(x: f32) -> f32 { + x.clamp(-64.0, 64.0).exp2() +} + +/// Dequantise one channel (§4.6.18.3.5), or a coupled pair from its level and balance channels. +fn dequantise(frame: &Frame, values: &[Quantised], t: &FreqTables) -> Vec { + let g = &frame.channels[0].grid; + let amp = if g.amp_res { 1.0 } else { 0.5 }; + let mut out = vec![Levels { energy: [[0.0; 64]; MAX_ENV], noise: [[0.0; 5]; 2] }; values.len()]; + if frame.coupling && values.len() == 2 { + let pan = if g.amp_res { 12.0 } else { 24.0 }; + let ((e0, q0), (e1, q1)) = (&values[0], &values[1]); + for l in 0..g.num_env { + for k in 0..t.bands(g.freq_res[l]).len() - 1 { + let level = 64.0 * exp2(amp * e0[l][k] as f32 + 1.0); + let balance = amp * (pan - e1[l][k] as f32); + out[0].energy[l][k] = level / (1.0 + exp2(balance)); + out[1].energy[l][k] = level / (1.0 + exp2(-balance)); + } + } + for l in 0..g.num_noise { + for k in 0..t.n_q() { + let level = exp2((NOISE_OFFSET - q0[l][k] + 1) as f32); + let balance = (12 - q1[l][k]) as f32; + out[0].noise[l][k] = level / (1.0 + exp2(balance)); + out[1].noise[l][k] = level / (1.0 + exp2(-balance)); + } + } + return out; + } + for ((lv, (e, q)), d) in out.iter_mut().zip(values).zip(&frame.channels) { + let g = &d.grid; + let amp = if g.amp_res { 1.0 } else { 0.5 }; + for l in 0..g.num_env { + for k in 0..t.bands(g.freq_res[l]).len() - 1 { + lv.energy[l][k] = 64.0 * exp2(amp * e[l][k] as f32); + } + } + for l in 0..g.num_noise { + for k in 0..t.n_q() { + lv.noise[l][k] = exp2((NOISE_OFFSET - q[l][k]) as f32); + } + } + } + out +} + +/// The SBR decoder of one channel element: a single channel (SCE, or an LFE, which has no SBR +/// data) or a channel pair (CPE). +#[derive(Clone)] +pub(crate) struct Element { + /// Output sampling rate. + rate: u32, + header: Option
, + tables: Option, + channels: Vec, +} + +impl Element { + pub(crate) fn new(channels: usize, rate: u32) -> Element { + Element { rate, header: None, tables: None, channels: (0..channels).map(|_| Channel::new()).collect() } + } + + pub(crate) fn channels(&self) -> usize { + self.channels.len() + } + + /// Forget the signal (a seek). The header is kept: it holds until the stream sends another. + pub(crate) fn reset(&mut self) { + for c in &mut self.channels { + *c = Channel::new(); + } + } + + /// Read an element's SBR data: `None` without a header to read it by. + fn read(&mut self, p: Payload) -> Option { + let mut br = BitReader::new(p.data); + br.seek_bits(p.start); + if p.crc { + br.skip(10).ok()?; + } + if br.read_flag().ok()? { + let h = Header::read(&mut br).ok()?; + if self.header != Some(h) { + if self.header.is_none_or(|o| !o.same_tables(&h)) { + for c in &mut self.channels { + c.history.reset_tables(); + } + } + self.tables = FreqTables::new(&h, self.rate); + self.header = Some(h); + } + } + let (h, t) = (self.header?, self.tables.as_ref()?); + let frame = bitstream::read_data(&mut br, t, h.amp_res, self.channels.len()).ok()?; + (br.position() <= p.end).then_some(frame) + } + + /// Decode one frame: 1024 core samples per channel in, 2048 out. + pub(crate) fn process(&mut self, core: &[Vec], payload: Option) -> Vec> { + for (c, pcm) in self.channels.iter_mut().zip(core) { + c.x_low.copy_within(SLOTS..X_SLOTS, 0); + let scaled: Vec = pcm.iter().map(|s| s * PCM_SCALE).collect(); + c.analysis.process(&scaled, &mut c.x_low[T_HF_GEN..]); + } + let frame = payload.and_then(|p| self.read(p)); + let mut y: Vec> = self.channels.iter().map(|_| Box::new([[Cx::default(); 64]; X_SLOTS])).collect(); + // per channel: kx, m, and the first and end slot of `y` this frame fills + let mut current: Vec> = vec![None; self.channels.len()]; + if let (Some(frame), Some(h), Some(t)) = (&frame, self.header, self.tables.as_ref()) { + let values: Vec<_> = self + .channels + .iter_mut() + .zip(&frame.channels) + .enumerate() + .map(|(i, (c, d))| c.absolute(d, t, if frame.coupling && i == 1 { 2 } else { 1 })) + .collect(); + let levels = dequantise(frame, &values, t); + for (((c, d), (lv, y)), cur) in self.channels.iter_mut().zip(&frame.channels).zip(levels.iter().zip(&mut y)).zip(&mut current) { + let g = &d.grid; + let slots = (2 * g.t_env[0], 2 * g.t_env[g.num_env]); + let mut x_high: Box = Box::new([[Cx::default(); 64]; X_SLOTS]); + hf::generate(&c.x_low, t, &d.invf, &mut c.history, slots, &mut x_high); + let f = hf::Frame { x: &x_high, t, hdr: &h, g, lv, harmonic: d.harmonic.as_ref() }; + hf::adjust(&f, &mut c.history, y); + *cur = Some((t.kx, t.m, slots.0 + T_HF_ADJ, slots.1 + T_HF_ADJ)); + } + } + let mut out = vec![vec![0f32; 2 * 1024]; self.channels.len()]; + for (((c, y), out), current) in self.channels.iter_mut().zip(y).zip(&mut out).zip(current) { + for (l, block) in out.as_chunks_mut::<64>().0.iter_mut().enumerate() { + let i = l + T_HF_ADJ; + let mut x = [Cx::default(); 64]; + // the high band of this slot: this frame's envelopes, the last frame's reaching + // into this one, or none + let high = match current { + Some((kx, m, from, to)) if (from..to).contains(&i) => Some((&y[i], kx, m)), + _ if i + SLOTS < c.y_prev_end => Some((&c.y_prev[i + SLOTS], c.y_prev_range.0, c.y_prev_range.1)), + _ => None, + }; + let low = high.map_or(current.map_or(32, |cur| cur.0), |h| h.1); + x[..low].copy_from_slice(&c.x_low[i][..low]); + if let Some((row, kx, m)) = high { + x[kx..kx + m].copy_from_slice(&row[kx..kx + m]); + } + c.synthesis.process(&x, 64, block); + } + for s in out.iter_mut() { + *s /= PCM_SCALE; + } + c.y_prev = y; + (c.y_prev_range, c.y_prev_end) = current.map_or(((0, 0), 0), |cur| ((cur.0, cur.1), cur.3)); + } + out + } +} + +/// The header of [`test_payload`]. +#[cfg(test)] +pub(crate) const TEST_HEADER: Header = Header { + amp_res: true, + start_freq: 5, + stop_freq: 9, + xover_band: 0, + freq_scale: 2, + alter_scale: true, + noise_bands: 2, + limiter_bands: 2, + limiter_gains: 2, + interpol_freq: true, + smoothing_mode: true, +}; + +/// An SBR single channel element for tests at output rate `rate`: [`TEST_HEADER`] (3 dB steps, +/// start 5, stop 9, the default extra fields), then one FIXFIX envelope at high resolution with +/// band `k` at `env(k)` (1.5 dB steps, as a single FIXFIX envelope has) and every noise floor +/// value `noise`. +#[cfg(test)] +pub(crate) fn test_payload(rate: u32, env: impl Fn(usize) -> i32, noise: u32) -> Option> { + use craft_media::bitstream::BitWriter; + let t = FreqTables::new(&TEST_HEADER, rate)?; + let (env, n_q): (Vec, usize) = ((0..t.n_high()).map(env).collect(), t.n_q()); + let mut bw = BitWriter::new(); + bw.write_bits(1, 1); // bs_header_flag + for (v, n) in [(1, 1), (5, 4), (9, 4), (0, 3), (0, 2), (0, 1), (0, 1)] { + bw.write_bits(v, n); // amp_res, start, stop, xover, reserved, no extra fields + } + bw.write_bits(0, 1); // bs_data_extra + bw.write_bits(u32::from(bitstream::FIXFIX), 2); + bw.write_bits(0, 2); // one envelope + bw.write_bits(1, 1); // high resolution + bw.write_bits(0, 2); // frequency-direction envelope and noise floor + for _ in 0..n_q { + bw.write_bits(0, 2); // no inverse filtering + } + bw.write_bits(env[0] as u32, 7); + for w in env.windows(2) { + let (code, len) = tables::F_ENV_1_5DB[(w[1] - w[0] + 60) as usize]; + bw.write_bits(code, u32::from(len)); + } + bw.write_bits(noise, 5); + for _ in 1..n_q { + let (code, len) = tables::F_ENV_3_0DB[31]; + bw.write_bits(code, u32::from(len)); + } + bw.write_bits(0, 2); // no sinusoids, no extended data + Some(bw.finish()) +} + +#[cfg(test)] +mod tests { + use std::f64::consts::PI; + + use super::*; + + const RATE: u32 = 44_100; + + fn tables() -> FreqTables { + FreqTables::new(&TEST_HEADER, RATE).unwrap() + } + + /// Mean energy per DFT bin of `x` between `lo` and `hi` QMF channels (64 per half rate). + fn energy(x: &[f32], lo: usize, hi: usize) -> f64 { + let n = x.len(); + let (b0, b1) = (lo * n / 128, hi * n / 128); + let mut e = 0f64; + for b in b0 + 2..b1 - 2 { + let (mut re, mut im) = (0f64, 0f64); + for (i, &s) in x.iter().enumerate() { + let w = 0.5 - 0.5 * (2.0 * PI * i as f64 / n as f64).cos(); + let ph = 2.0 * PI * (b * i) as f64 / n as f64; + re += f64::from(s) * w * ph.cos(); + im += f64::from(s) * w * ph.sin(); + } + e += re * re + im * im; + } + e / (b1 - b0 - 4) as f64 + } + + fn run(e: &mut Element, data: &[u8], frames: usize) -> Vec { + let core = vec![vec![0f32; 1024]]; + let mut out = Vec::new(); + for _ in 0..frames { + let p = Payload { data, start: 0, end: 8 * data.len(), crc: false }; + let block = e.process(&core, Some(p)); + assert_eq!((block.len(), block[0].len()), (1, 2048)); + assert!(block[0].iter().all(|s| s.is_finite())); + out.extend_from_slice(&block[0]); + } + out + } + + /// Over a silent core the high band is the noise the envelope asks for: the upper bands, + /// 12 steps of 1.5 dB above the lower ones, come out 18 dB louder, and nothing below `kx`. + #[test] + fn the_high_band_follows_the_envelope() { + let t = tables(); + let mid = t.n_high() / 2; + let data = test_payload(RATE, |k| if k < mid { 30 } else { 42 }, 0).unwrap(); + let mut e = Element::new(1, RATE); + let out = run(&mut e, &data, 6); + let tail = &out[out.len() - 4096..]; + let (lower, upper) = (energy(tail, t.high[0], t.high[mid]), energy(tail, t.high[mid], t.high[t.n_high()])); + let step = 10.0 * (upper / lower).log10(); + assert!((step - 18.06).abs() < 1.5, "{step:.2} dB"); + let below = energy(tail, 1, t.kx - 1); + assert!(10.0 * (lower / below.max(1e-30)).log10() > 60.0); + // the same envelope without its header is not read: an element that has seen none + // passes the core through alone + let mut fresh = Element::new(1, RATE); + let mut no_header = data.clone(); + no_header[0] &= 0x7F; + assert!(run(&mut fresh, &no_header, 3).iter().all(|&s| s == 0.0)); + } + + /// Damaged SBR data (flipped bits, cut short, or noise) never panics and keeps the output + /// finite and of full length. + #[test] + fn damaged_sbr_data_is_survived() { + let good = test_payload(RATE, |k| 20 + k as i32, 3).unwrap(); + let mut seed = 0x1234_5678u32; + let mut next = || { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + seed + }; + let mut e = Element::new(1, RATE); + let mut pair = Element::new(2, RATE); + for i in 0..600 { + let mut data = good.clone(); + for _ in 0..1 + next() % 6 { + let bit = next() as usize % (8 * data.len()); + data[bit / 8] ^= 0x80 >> (bit % 8); + } + if i % 3 == 0 { + data.truncate(1 + next() as usize % data.len()); + } + if i % 5 == 0 { + data = (0..1 + next() % 40).map(|_| next() as u8).collect(); + data[0] |= 0x80; + } + run(&mut e, &data, 1); + let p = Payload { data: &data, start: 0, end: 8 * data.len(), crc: i % 2 == 0 }; + let out = pair.process(&[vec![0.1; 1024], vec![-0.1; 1024]], Some(p)); + assert!(out.iter().all(|c| c.len() == 2048 && c.iter().all(|s| s.is_finite()))); + } + } +} diff --git a/crates/audio/src/aac/sbr/qmf.rs b/crates/audio/src/aac/sbr/qmf.rs new file mode 100644 index 000000000..de947f303 --- /dev/null +++ b/crates/audio/src/aac/sbr/qmf.rs @@ -0,0 +1,155 @@ +//! The SBR filter banks (ISO/IEC 14496-3 §4.6.18.4): a 32-band complex QMF analysis bank at the +//! core rate and a 64-band complex QMF synthesis bank at the output rate, sharing the window `c`. +//! +//! Each bank is a polyphase window followed by a complex modulation: analysis +//! `2·exp(iπ(k + ½)(2n − ½)/64)`, synthesis `exp(iπ(k + ½)(2n − 251)/128)/64`. With the window's +//! indexing, the two phase offsets put the banks' combined response on its centre tap, so the low +//! band comes back unchanged (to about -60 dB, the window's own limit), 576 output samples late. +//! The synthesis bank holds back its last two output samples, which makes it 578: with the SBR +//! frame's lag, the 962 samples by which HE-AAC decoders (ffmpeg's among them) delay the core, and +//! which the priming encoders signal counts on. + +use std::f64::consts::PI; +use std::sync::OnceLock; + +use super::Cx; +use super::tables::QMF_WINDOW; + +/// Output samples by which analysis followed by synthesis delays the low band. +pub(crate) const DELAY: usize = 578; + +/// Modulation matrices, computed once. +struct Kernels { + /// Analysis, at `[k][n]`: 32 × 64. + analysis: Vec<[Cx; 64]>, + /// Synthesis, at `[n][k]`: 128 × 64. + synthesis: Vec<[Cx; 64]>, +} + +fn kernels() -> &'static Kernels { + static K: OnceLock = OnceLock::new(); + K.get_or_init(|| { + let cx = |phase: f64, scale: f64| Cx { re: (phase.cos() * scale) as f32, im: (phase.sin() * scale) as f32 }; + let analysis = (0..32).map(|k| std::array::from_fn(|n| cx(PI * (k as f64 + 0.5) * (2.0 * n as f64 - 0.5) / 64.0, 2.0))).collect(); + let synthesis = (0..128).map(|n| std::array::from_fn(|k| cx(PI * (k as f64 + 0.5) * (2.0 * n as f64 - 251.0) / 128.0, 1.0 / 64.0))).collect(); + Kernels { analysis, synthesis } + }) +} + +/// The 32-band analysis bank of one channel. +#[derive(Clone)] +pub(crate) struct Analysis { + /// The last 320 input samples, newest first. + x: Box<[f32; 320]>, +} + +impl Analysis { + pub(crate) fn new() -> Analysis { + Analysis { x: Box::new([0.0; 320]) } + } + + /// Split `32 · out.len()` input samples into `out.len()` slots of 32 subbands. + pub(crate) fn process(&mut self, input: &[f32], out: &mut [[Cx; 32]]) { + let k = kernels(); + for (block, slot) in input.as_chunks::<32>().0.iter().zip(out.iter_mut()) { + self.x.copy_within(0..288, 32); + for (n, &s) in block.iter().enumerate() { + self.x[31 - n] = s; + } + let mut u = [0f32; 64]; + for (n, u) in u.iter_mut().enumerate() { + *u = (0..5).map(|j| self.x[n + 64 * j] * QMF_WINDOW[2 * (n + 64 * j)]).sum(); + } + for (sb, row) in slot.iter_mut().zip(&k.analysis) { + let (mut re, mut im) = (0f32, 0f32); + for (u, m) in u.iter().zip(row) { + re += u * m.re; + im += u * m.im; + } + *sb = Cx { re, im }; + } + } + } +} + +/// The 64-band synthesis bank of one channel. +#[derive(Clone)] +pub(crate) struct Synthesis { + v: Box<[f32; 1280]>, + /// The last two output samples, which the next slot puts out first. + held: [f32; 2], +} + +impl Synthesis { + pub(crate) fn new() -> Synthesis { + Synthesis { v: Box::new([0.0; 1280]), held: [0.0; 2] } + } + + /// Merge one slot of 64 subbands into 64 output samples (`out[..64]`, of which it needs 64). + /// Bands from `bands` up are taken as zero. + pub(crate) fn process(&mut self, x: &[Cx; 64], bands: usize, out: &mut [f32]) { + let k = kernels(); + let bands = bands.min(64); + self.v.copy_within(0..1152, 128); + for (n, row) in k.synthesis.iter().enumerate() { + self.v[n] = x[..bands].iter().zip(row).map(|(x, m)| x.re * m.re - x.im * m.im).sum(); + } + let mut slot = [0f32; 66]; + slot[..2].copy_from_slice(&self.held); + for (n, o) in slot[2..].iter_mut().enumerate() { + let mut s = 0f32; + for i in 0..5 { + s += self.v[256 * i + n] * QMF_WINDOW[128 * i + n]; + s += self.v[256 * i + 192 + n] * QMF_WINDOW[128 * i + 64 + n]; + } + *o = s; + } + self.held = [slot[64], slot[65]]; + for (o, s) in out.iter_mut().zip(&slot[..64]) { + *o = *s; + } + } +} + +#[cfg(test)] +mod tests { + use super::super::SLOTS; + use super::*; + + fn tones(t: f64) -> f64 { + [0.0371, 0.0913, 0.1527, 0.2211, 0.2903, 0.3519, 0.4127].iter().enumerate().map(|(j, f)| (2.0 * PI * f * t + j as f64).sin() * 0.1).sum() + } + + /// Tones across the core band through analysis and synthesis (low 32 bands only) come out at + /// twice the rate, [`DELAY`] samples late, with the error 60 dB down. + #[test] + fn analysis_then_synthesis_reconstructs_the_input() { + let input: Vec = (0..8 * 1024).map(|i| tones(i as f64) as f32).collect(); + let (mut a, mut s) = (Analysis::new(), Synthesis::new()); + let mut out = Vec::new(); + let mut slots = [[Cx::default(); 32]; SLOTS]; + for frame in input.as_chunks::<1024>().0 { + a.process(frame, &mut slots); + for slot in &slots { + let mut x = [Cx::default(); 64]; + x[..32].copy_from_slice(slot); + let mut o = [0f32; 64]; + s.process(&x, 32, &mut o); + out.extend_from_slice(&o); + } + } + assert_eq!(out.len(), 2 * input.len()); + let snr_at = |delay: usize| { + let (mut err, mut sig) = (0f64, 0f64); + for (i, &o) in out.iter().enumerate().skip(3000) { + let want = tones((i as f64 - delay as f64) / 2.0); + err += (o as f64 - want).powi(2); + sig += want * want; + } + 10.0 * (sig / err).log10() + }; + let snr = snr_at(DELAY); + assert!(snr > 60.0, "reconstruction SNR {snr:.1} dB"); + assert!(snr_at(DELAY - 1) < 40.0 && snr_at(DELAY + 1) < 40.0); + } +} diff --git a/crates/audio/src/aac/sbr/tables.rs b/crates/audio/src/aac/sbr/tables.rs new file mode 100644 index 000000000..7a648120e --- /dev/null +++ b/crates/audio/src/aac/sbr/tables.rs @@ -0,0 +1,357 @@ +//! SBR tables (ETSI ES 201 980 V1.2.2 Annex I, the SBR tables it shares with ISO/IEC 14496-3 §4.A.6). +//! +//! Transcribed from the specification's tables, which give the window to 14 digits (more than an +//! `f32` holds). Annex I misprints the labels of `c[297]` and `c[305]`; the values are in order. +//! Each Huffman table is a complete prefix code (see the tests in `sbr::bitstream`). + +#![allow(clippy::excessive_precision)] + +/// The QMF bank window `c` (640 coefficients). +#[rustfmt::skip] +pub(crate) static QMF_WINDOW: [f32; 640] = [ + -0.00055252865047, -0.00056176925738, -0.00049475180896, -0.00048752279712, -0.00048937912498, -0.00050407143497, -0.00052265642972, -0.00054665656337, + -0.00056778025613, -0.00058709304852, -0.00061327473938, -0.00063124935319, -0.00065403333621, -0.00067776907764, -0.00069416146273, -0.00071577364744, + -0.00072550431222, -0.00074409418541, -0.00074905980532, -0.00076813719270, -0.00077248485949, -0.00078343322877, -0.00077798694927, -0.00078036647100, + -0.00078014496257, -0.00077579773310, -0.00076307935757, -0.00075300014201, -0.00073193571525, -0.00072153919876, -0.00069179375372, -0.00066504150893, + -0.00063415949025, -0.00059461189330, -0.00055645763906, -0.00051455722108, -0.00046063254803, -0.00040951214522, -0.00035011758756, -0.00028969811748, + -0.00020983373440, -0.00014463809349, -0.00006173344072, 0.00001349497418, 0.00010943831274, 0.00020430170688, 0.00029495311041, 0.00040265402160, + 0.00051073884952, 0.00062393761391, 0.00074580258865, 0.00086084433262, 0.00098859883015, 0.00112501551307, 0.00125778846475, 0.00139024948272, + 0.00154432198471, 0.00168680832531, 0.00183482654224, 0.00198411407369, 0.00214615835557, 0.00230172547746, 0.00246256169126, 0.00262017586902, + 0.00278704643465, 0.00294694477165, 0.00311254206525, 0.00327396134847, 0.00344188741828, 0.00360082681231, 0.00376039229104, 0.00392074323703, + 0.00408197531935, 0.00422642692270, 0.00437307196781, 0.00452098527825, 0.00466064606118, 0.00479325608498, 0.00491376035745, 0.00503930226013, + 0.00514073539032, 0.00524611661324, 0.00534716811982, 0.00541967759307, 0.00548760401507, 0.00554757145088, 0.00559380230045, 0.00562206432097, + 0.00564551969164, 0.00563891995151, 0.00562661141932, 0.00559171286630, 0.00554043639400, 0.00547537830770, 0.00538389758970, 0.00527157587272, + 0.00513822754514, 0.00498396877629, 0.00481094690600, 0.00460395301471, 0.00438018617447, 0.00412516423270, 0.00384564081246, 0.00354012465507, + 0.00320918858098, 0.00284467578623, 0.00245085400321, 0.00202741761850, 0.00157846825768, 0.00109023290512, 0.00058322642480, 0.00002760451905, + -0.00054642808664, -0.00115681355227, -0.00180394725893, -0.00248267236449, -0.00319337783900, -0.00394011240522, -0.00472225962400, -0.00553372111088, + -0.00637922932685, -0.00726158168517, -0.00817982333726, -0.00913253296085, -0.01011502154986, -0.01113155480321, -0.01218499959508, -0.01327182200351, + 0.01439046660792, 0.01554055533423, 0.01673247129989, 0.01794333813443, 0.01918724313698, 0.02045317933555, 0.02174675502535, 0.02306801692862, + 0.02441609920285, 0.02578758475467, 0.02718594296329, 0.02860721736385, 0.03005026574279, 0.03150176087389, 0.03297540810337, 0.03446209487686, + 0.03596975605542, 0.03748128504252, 0.03900536794745, 0.04053491705584, 0.04206490946367, 0.04360975421304, 0.04514884056413, 0.04668430272642, + 0.04821657200672, 0.04973857556014, 0.05125561555216, 0.05276307465207, 0.05424527683589, 0.05571736482138, 0.05716164501299, 0.05859156836260, + 0.05998374801761, 0.06134551717207, 0.06268578081172, 0.06397158980681, 0.06522471064380, 0.06643675122104, 0.06760759851228, 0.06870438283512, + 0.06976302447127, 0.07076287107266, 0.07170026731102, 0.07256825833083, 0.07336202550803, 0.07410036424342, 0.07474525581194, 0.07531373362019, + 0.07580083586584, 0.07619924793396, 0.07649921704119, 0.07670934904245, 0.07681739756964, 0.07682300113923, 0.07672049241746, 0.07650507183194, + 0.07617483218536, 0.07573057565061, 0.07515762552870, 0.07446643947564, 0.07364060057620, 0.07267746427299, 0.07158263647903, 0.07035330735093, + 0.06896640131951, 0.06745250215166, 0.06576906686508, 0.06394448059633, 0.06196027790387, 0.05981665708090, 0.05751526919867, 0.05504600343009, + 0.05240938217366, 0.04959786763445, 0.04663033051701, 0.04347687821958, 0.04014582784127, 0.03664181168133, 0.03295839306691, 0.02908240060125, + 0.02503075618909, 0.02079970728622, 0.01637012582228, 0.01176238327857, 0.00696368621617, 0.00197656014503, -0.00320868968304, -0.00857117491366, + -0.01412888273558, -0.01988341292573, -0.02582272888064, -0.03195312745332, -0.03827765720822, -0.04478068215856, -0.05148041767934, -0.05837053268336, + -0.06544098531359, -0.07269433008129, -0.08013729344279, -0.08775475365593, -0.09555333528914, -0.10353295311463, -0.11168269317730, -0.12000779846800, + -0.12850028503878, -0.13715517611934, -0.14597664911870, -0.15496070710605, -0.16409588556669, -0.17338081721706, -0.18281725485142, -0.19239667457267, + -0.20212501768103, -0.21197358538056, -0.22196526964149, -0.23206908706791, -0.24230168845974, -0.25264803095722, -0.26310532994603, -0.27366340405625, + -0.28432141891085, -0.29507167170646, -0.30590985751916, -0.31682789136456, -0.32781137272105, -0.33887226938665, -0.34999141229310, -0.36115899031355, + 0.37237955463061, 0.38363500139043, 0.39492117615675, 0.40623176767625, 0.41756968968409, 0.42891199207373, 0.44025537543665, 0.45159965356824, + 0.46293080852757, 0.47424532146115, 0.48552530911099, 0.49677082545707, 0.50798175000434, 0.51912349702391, 0.53022408956855, 0.54125534487322, + 0.55220512585061, 0.56307891401370, 0.57385241316923, 0.58454032354679, 0.59511230862496, 0.60557835389180, 0.61591099320291, 0.62612426956055, + 0.63619801077286, 0.64612696959461, 0.65590163024671, 0.66551398801627, 0.67496631901712, 0.68423532934598, 0.69332823767032, 0.70223887193539, + 0.71094104263095, 0.71944626349561, 0.72774489002994, 0.73582117582769, 0.74368278636488, 0.75131374561237, 0.75870807608242, 0.76586748650939, + 0.77277808813327, 0.77942875190216, 0.78583531203920, 0.79197358416424, 0.79784664137700, 0.80344857518505, 0.80876950044491, 0.81381912706217, + 0.81857760046468, 0.82304198905409, 0.82722753473360, 0.83110384571520, 0.83469373618402, 0.83797173378865, 0.84095413924722, 0.84362382812005, + 0.84598184698206, 0.84803157770763, 0.84978051984268, 0.85119715249343, 0.85230470352147, 0.85310209497017, 0.85357205739107, 0.85373856005937, + 0.85357205739107, 0.85310209497017, 0.85230470352147, 0.85119715249343, 0.84978051984268, 0.84803157770763, 0.84598184698206, 0.84362382812005, + 0.84095413924722, 0.83797173378865, 0.83469373618402, 0.83110384571520, 0.82722753473360, 0.82304198905409, 0.81857760046468, 0.81381912706217, + 0.80876950044491, 0.80344857518505, 0.79784664137700, 0.79197358416424, 0.78583531203920, 0.77942875190216, 0.77277808813327, 0.76586748650939, + 0.75870807608242, 0.75131374561237, 0.74368278636488, 0.73582117582769, 0.72774489002994, 0.71944626349561, 0.71094104263095, 0.70223887193539, + 0.69332823767032, 0.68423532934598, 0.67496631901712, 0.66551398801627, 0.65590163024671, 0.64612696959461, 0.63619801077286, 0.62612426956055, + 0.61591099320291, 0.60557835389180, 0.59511230862496, 0.58454032354679, 0.57385241316923, 0.56307891401370, 0.55220512585061, 0.54125534487322, + 0.53022408956855, 0.51912349702391, 0.50798175000434, 0.49677082545707, 0.48552530911099, 0.47424532146115, 0.46293080852757, 0.45159965356824, + 0.44025537543665, 0.42891199207373, 0.41756968968409, 0.40623176767625, 0.39492117615675, 0.38363500139043, 0.37237955463061, 0.36115899031355, + -0.34999141229310, -0.33887226938665, -0.32781137272105, -0.31682789136456, -0.30590985751916, -0.29507167170646, -0.28432141891085, -0.27366340405625, + -0.26310532994603, -0.25264803095722, -0.24230168845974, -0.23206908706791, -0.22196526964149, -0.21197358538056, -0.20212501768103, -0.19239667457267, + -0.18281725485142, -0.17338081721706, -0.16409588556669, -0.15496070710605, -0.14597664911870, -0.13715517611934, -0.12850028503878, -0.12000779846800, + -0.11168269317730, -0.10353295311463, -0.09555333528914, -0.08775475365593, -0.08013729344279, -0.07269433008129, -0.06544098531359, -0.05837053268336, + -0.05148041767934, -0.04478068215856, -0.03827765720822, -0.03195312745332, -0.02582272888064, -0.01988341292573, -0.01412888273558, -0.00857117491366, + -0.00320868968304, 0.00197656014503, 0.00696368621617, 0.01176238327857, 0.01637012582228, 0.02079970728622, 0.02503075618909, 0.02908240060125, + 0.03295839306691, 0.03664181168133, 0.04014582784127, 0.04347687821958, 0.04663033051701, 0.04959786763445, 0.05240938217366, 0.05504600343009, + 0.05751526919867, 0.05981665708090, 0.06196027790387, 0.06394448059633, 0.06576906686508, 0.06745250215166, 0.06896640131951, 0.07035330735093, + 0.07158263647903, 0.07267746427299, 0.07364060057620, 0.07446643947564, 0.07515762552870, 0.07573057565061, 0.07617483218536, 0.07650507183194, + 0.07672049241746, 0.07682300113923, 0.07681739756964, 0.07670934904245, 0.07649921704119, 0.07619924793396, 0.07580083586584, 0.07531373362019, + 0.07474525581194, 0.07410036424342, 0.07336202550803, 0.07256825833083, 0.07170026731102, 0.07076287107266, 0.06976302447127, 0.06870438283512, + 0.06760759851228, 0.06643675122104, 0.06522471064380, 0.06397158980681, 0.06268578081172, 0.06134551717207, 0.05998374801761, 0.05859156836260, + 0.05716164501299, 0.05571736482138, 0.05424527683589, 0.05276307465207, 0.05125561555216, 0.04973857556014, 0.04821657200672, 0.04668430272642, + 0.04514884056413, 0.04360975421304, 0.04206490946367, 0.04053491705584, 0.03900536794745, 0.03748128504252, 0.03596975605542, 0.03446209487686, + 0.03297540810337, 0.03150176087389, 0.03005026574279, 0.02860721736385, 0.02718594296329, 0.02578758475467, 0.02441609920285, 0.02306801692862, + 0.02174675502535, 0.02045317933555, 0.01918724313698, 0.01794333813443, 0.01673247129989, 0.01554055533423, 0.01439046660792, 0.01327182200351, + -0.01218499959508, -0.01113155480321, -0.01011502154986, -0.00913253296085, -0.00817982333726, -0.00726158168517, -0.00637922932685, -0.00553372111088, + -0.00472225962400, -0.00394011240522, -0.00319337783900, -0.00248267236449, -0.00180394725893, -0.00115681355227, -0.00054642808664, 0.00002760451905, + 0.00058322642480, 0.00109023290512, 0.00157846825768, 0.00202741761850, 0.00245085400321, 0.00284467578623, 0.00320918858098, 0.00354012465507, + 0.00384564081246, 0.00412516423270, 0.00438018617447, 0.00460395301471, 0.00481094690600, 0.00498396877629, 0.00513822754514, 0.00527157587272, + 0.00538389758970, 0.00547537830770, 0.00554043639400, 0.00559171286630, 0.00562661141932, 0.00563891995151, 0.00564551969164, 0.00562206432097, + 0.00559380230045, 0.00554757145088, 0.00548760401507, 0.00541967759307, 0.00534716811982, 0.00524611661324, 0.00514073539032, 0.00503930226013, + 0.00491376035745, 0.00479325608498, 0.00466064606118, 0.00452098527825, 0.00437307196781, 0.00422642692270, 0.00408197531935, 0.00392074323703, + 0.00376039229104, 0.00360082681231, 0.00344188741828, 0.00327396134847, 0.00311254206525, 0.00294694477165, 0.00278704643465, 0.00262017586902, + 0.00246256169126, 0.00230172547746, 0.00214615835557, 0.00198411407369, 0.00183482654224, 0.00168680832531, 0.00154432198471, 0.00139024948272, + 0.00125778846475, 0.00112501551307, 0.00098859883015, 0.00086084433262, 0.00074580258865, 0.00062393761391, 0.00051073884952, 0.00040265402160, + 0.00029495311041, 0.00020430170688, 0.00010943831274, 0.00001349497418, -0.00006173344072, -0.00014463809349, -0.00020983373440, -0.00028969811748, + -0.00035011758756, -0.00040951214522, -0.00046063254803, -0.00051455722108, -0.00055645763906, -0.00059461189330, -0.00063415949025, -0.00066504150893, + -0.00069179375372, -0.00072153919876, -0.00073193571525, -0.00075300014201, -0.00076307935757, -0.00077579773310, -0.00078014496257, -0.00078036647100, + -0.00077798694927, -0.00078343322877, -0.00077248485949, -0.00076813719270, -0.00074905980532, -0.00074409418541, -0.00072550431222, -0.00071577364744, + -0.00069416146273, -0.00067776907764, -0.00065403333621, -0.00063124935319, -0.00061327473938, -0.00058709304852, -0.00056778025613, -0.00054665656337, + -0.00052265642972, -0.00050407143497, -0.00048937912498, -0.00048752279712, -0.00049475180896, -0.00056176925738, -0.00055252865047, 0.00000000000000, +]; + +/// The SBR noise table `V` (Table I.13): 512 unit-variance complex values (re, im). +#[rustfmt::skip] +pub(crate) static NOISE: [[f32; 2]; 512] = [ + [-0.99948153278296, -0.59483417516607], [0.97113454393991, -0.67528515225647], [0.14130051758487, -0.95090983575689], [-0.47005496701697, -0.37340549728647], + [0.80705063769351, 0.29653668284408], [-0.38981478896926, 0.89572605717087], [-0.01053049862020, -0.66959058036166], [-0.91266367957293, -0.11522938140034], + [0.54840422910309, 0.75221367176302], [0.40009252867955, -0.98929400334421], [-0.99867974711855, -0.88147068645358], [-0.95531076805040, 0.90908757154593], + [-0.45725933317144, -0.56716323646760], [-0.72929675029275, -0.98008272727324], [0.75622801399036, 0.20950329995549], [0.07069442601050, -0.78247898470706], + [0.74496252926055, -0.91169004445807], [-0.96440182703856, -0.94739918296622], [0.30424629369539, -0.49438267012479], [0.66565033746925, 0.64652935542491], + [0.91697008020594, 0.17514097332009], [-0.70774918760427, 0.52548653416543], [-0.70051415345560, -0.45340028808763], [-0.99496513054797, -0.90071908066973], + [0.98164490790123, -0.77463155528697], [-0.54671580548181, -0.02570928536004], [-0.01689629065389, 0.00287506445732], [-0.86110349531986, 0.42548583726477], + [-0.98892980586032, -0.87881132267556], [0.51756627678691, 0.66926784710139], [-0.99635026409640, -0.58107730574765], [-0.99969370862163, 0.98369989360250], + [0.55266258627194, 0.59449057465591], [0.34581177741673, 0.94879421061866], [0.62664209577999, -0.74402970906471], [-0.77149701404973, -0.33883658042801], + [-0.91592244254432, 0.03687901376713], [-0.76285492357887, -0.91371867919124], [0.79788337195331, -0.93180971199849], [0.54473080610200, -0.11919206037186], + [-0.85639281671058, 0.42429854760451], [-0.92882402971423, 0.27871809078609], [-0.11708371046774, -0.99800843444966], [0.21356749817493, -0.90716295627033], + [-0.76191692573909, 0.99768118356265], [0.98111043100884, -0.95854459734407], [-0.85913269895572, 0.95766566168880], [-0.93307242253692, 0.49431757696466], + [0.30485754879632, -0.70540034357529], [0.85289650925190, 0.46766131791044], [0.91328082618125, -0.99839597361769], [-0.05890199924154, 0.70741827819497], + [0.28398686150148, 0.34633555702188], [0.95258164539612, -0.54893416026939], [-0.78566324168507, -0.75568541079691], [-0.95789495447877, -0.20423194696966], + [0.82411158711197, 0.96654618432562], [-0.65185446735885, -0.88734990773289], [-0.93643603134666, 0.99870790442385], [0.91427159529618, -0.98290505544444], + [-0.70395684036886, 0.58796798221039], [0.00563771969365, 0.61768196727244], [0.89065051931895, 0.52783352697585], [-0.68683707712762, 0.80806944710339], + [0.72165342518718, -0.69259857349564], [-0.62928247730667, 0.13627037407335], [0.29938434065514, -0.46051329682246], [-0.91781958879280, -0.74012716684186], + [0.99298717043688, 0.40816610075661], [0.82368298622748, -0.74036047190173], [-0.98512833386833, -0.99972330709594], [-0.95915368242257, -0.99237800466040], + [-0.21411126572790, -0.93424819052545], [-0.68821476106884, -0.26892306315457], [0.91851997982317, 0.09358228901785], [-0.96062769559127, 0.36099095133739], + [0.51646184922287, -0.71373332873917], [0.61130721139669, 0.46950141175917], [0.47336129371299, -0.27333178296162], [0.90998308703519, 0.96715662938132], + [0.44844799194357, 0.99211574628306], [0.66614891079092, 0.96590176169121], [0.74922239129237, -0.89879858826087], [-0.99571588506485, 0.52785521494349], + [0.97401082477563, -0.16855870075190], [0.72683747733879, -0.48060774432251], [0.95432193457128, 0.68849603408441], [-0.72962208425191, -0.76608443420917], + [-0.85359479233537, 0.88738125901579], [-0.81412430338535, -0.97480768049637], [-0.87930772356786, 0.74748307690436], [-0.71573331064977, -0.98570608178923], + [0.83524300028228, 0.83702537075163], [-0.48086065601423, -0.98848504923531], [0.97139128574778, 0.80093621198236], [0.51992825347895, 0.80247631400510], + [-0.00848591195325, -0.76670128000486], [-0.70294374303036, 0.55359910445577], [-0.95894428168140, -0.43265504344783], [0.97079252950321, 0.09325857238682], + [-0.92404293670797, 0.85507704027855], [-0.69506469500450, 0.98633412625459], [0.26559203620024, 0.73314307966524], [0.28038443336943, 0.14537913654427], + [-0.74138124825523, 0.99310339807762], [-0.01752795995444, -0.82616635284178], [-0.55126773094930, -0.98898543862153], [0.97960898850996, -0.94021446752851], + [-0.99196309146936, 0.67019017358456], [-0.67684928085260, 0.12631491649378], [0.09140039465500, -0.20537731453108], [-0.71658965751996, -0.97788200391224], + [0.81014640078925, 0.53722648362443], [0.40616991671205, -0.26469008598449], [-0.67680188682972, 0.94502052337695], [0.86849774348749, -0.18333598647899], + [-0.99500381284851, -0.02634122068550], [0.84329189340667, 0.10406957462213], [-0.09215968531446, 0.69540012101253], [0.99956173327206, -0.12358542001404], + [-0.79732779473535, -0.91582524736159], [0.96349973642406, 0.96640458041000], [-0.79942778496547, 0.64323902822857], [-0.11566039853896, 0.28587846253726], + [-0.39922954514662, 0.94129601616966], [0.99089197565987, -0.92062625581587], [0.28631285179909, -0.91035047143603], [-0.83302725605608, -0.67330410892084], + [0.95404443402072, 0.49162765398743], [-0.06449863579434, 0.03250560813135], [-0.99575054486311, 0.42389784469507], [-0.65501142790847, 0.82546114655624], + [-0.81254441908887, -0.51627234660629], [-0.99646369485481, 0.84490533520752], [0.00287840603348, 0.64768261158166], [0.70176989408455, -0.20453028573322], + [0.96361882270190, 0.40706967140989], [-0.68883758192426, 0.91338958840772], [-0.34875585502238, 0.71472290693300], [0.91980081243087, 0.66507455644919], + [-0.99009048343881, 0.85868021604848], [0.68865791458395, 0.55660316809678], [-0.99484402129368, -0.20052559254934], [0.94214511408023, -0.99696425367461], + [-0.67414626793544, 0.49548221180078], [-0.47339353684664, -0.85904328834047], [0.14323651387360, -0.94145598222488], [-0.29268293575672, 0.05759224927952], + [0.43793861458754, -0.78904969892724], [-0.36345126374441, 0.64874435357162], [-0.08750604656825, 0.97686944362527], [-0.96495267812511, -0.53960305946511], + [0.55526940659947, 0.78891523734774], [0.73538215752630, 0.96452072373404], [-0.30889773919437, -0.80664389776860], [0.03574995626194, -0.97325616900959], + [0.98720684660488, 0.48409133691962], [-0.81689296271203, -0.90827703628298], [0.67866860118215, 0.81284503870856], [-0.15808569732583, 0.85279555024382], + [0.80723395114371, -0.24717418514605], [0.47788757329038, -0.46333147839295], [0.96367554763201, 0.38486749303242], [-0.99143875716818, -0.24945277239809], + [0.83081876925833, -0.94780851414763], [-0.58753191905341, 0.01290772389163], [0.95538108220960, -0.85557052096538], [-0.96490920476211, -0.64020970923102], + [-0.97327101028521, 0.12378128133110], [0.91400366022124, 0.57972471346930], [-0.99925837363824, 0.71084847864067], [-0.86875903507313, -0.20291699203564], + [-0.26240034795124, -0.68264554369108], [-0.24664412953388, -0.87642273115183], [0.02416275806869, 0.27192914288905], [0.82068619590515, -0.85087787994476], + [0.88547373760759, -0.89636802901469], [-0.18173078152226, -0.26152145156800], [0.09355476558534, 0.54845123045604], [-0.54668414224090, 0.95980774020221], + [0.37050990604091, -0.59910140383171], [-0.70373594262891, 0.91227665827081], [-0.34600785879594, -0.99441426144200], [-0.68774481731008, -0.30238837956299], + [-0.26843291251234, 0.83115668004362], [0.49072334613242, -0.45359708737775], [0.38975993093975, 0.95515358099121], [-0.97757125224150, 0.05305894580606], + [-0.17325552859616, -0.92770672250494], [0.99948035025744, 0.58285545563426], [-0.64946246527458, 0.68645507104960], [-0.12016920576437, -0.57147322153312], + [-0.58947456517751, -0.34847132454388], [-0.41815140454465, 0.16276422358861], [0.99885650204884, 0.11136095490444], [-0.56649614128386, -0.90494866361587], + [0.94138021032330, 0.35281916733018], [-0.75725076534641, 0.53650549640587], [0.20541973692630, -0.94435144369918], [0.99980371023351, 0.79835913565599], + [0.29078277605775, 0.35393777921520], [-0.62858772103030, 0.38765693387102], [0.43440904467688, -0.98546330463232], [-0.98298583762390, 0.21021524625209], + [0.19513029146934, -0.94239832251867], [-0.95476662400101, 0.98364554179143], [0.93379635304810, -0.70881994583682], [-0.85235410573336, -0.08342347966410], + [-0.86425093011245, -0.45795025029466], [0.38879779059045, 0.97274429344593], [0.92045124735495, -0.62433652524220], [0.89162532251878, 0.54950955570563], + [-0.36834336949252, 0.96458298020975], [0.93891760988045, -0.89968353740388], [0.99267657565094, -0.03757034316958], [-0.94063471614176, 0.41332338538963], + [0.99740224117019, -0.16830494996370], [-0.35899413170555, -0.46633226649613], [0.05237237274947, -0.25640361602661], [0.36703583957424, -0.38653265641875], + [0.91653180367913, -0.30587628726597], [0.69000803499316, 0.90952171386132], [-0.38658751133527, 0.99501571208985], [-0.29250814029851, 0.37444994344615], + [-0.60182204677608, 0.86779651036123], [-0.97418588163217, 0.96468523666475], [0.88461574003963, 0.57508405276414], [0.05198933055162, 0.21269661669964], + [-0.53499621979720, 0.97241553731237], [-0.49429560226497, 0.98183865291903], [-0.98935142339139, -0.40249159006933], [-0.98081380091130, -0.72856895534041], + [-0.27338148835532, 0.99950922447209], [0.06310802338302, -0.54539587529618], [-0.20461677199539, -0.14209977628489], [0.66223843141647, 0.72528579940326], + [-0.84764345483665, 0.02372316801261], [-0.89039863483811, 0.88866581484602], [0.95903308477986, 0.76744927173873], [0.73504123909879, -0.03747203173192], + [-0.31744434966056, -0.36834111883652], [-0.34110827591623, 0.40211222807691], [0.47803883714199, -0.39423219786288], [0.98299195879514, 0.01989791390047], + [-0.30963073129751, -0.18076720599336], [0.99992588229018, -0.26281872094289], [-0.93149731080767, -0.98313162570490], [0.99923472302773, -0.80142993767554], + [-0.26024169633417, -0.75999759855752], [-0.35712514743563, 0.19298963768574], [-0.99899084509530, 0.74645156992493], [0.86557171579452, 0.55593866696299], + [0.33408042438752, 0.86185953874709], [0.99010736374716, 0.04602397576623], [-0.66694269691195, -0.91643611810148], [0.64016792079480, 0.15649530836856], + [0.99570534804836, 0.45844586038111], [-0.63431466947340, 0.21079116459234], [-0.07706847005931, -0.89581437101329], [0.98590090577724, 0.88241721133981], + [0.80099335254678, -0.36851896710853], [0.78368131392666, 0.45506999802597], [0.08707806671691, 0.80938994918745], [-0.86811883080712, 0.39347308654705], + [-0.39466529740375, -0.66809432114456], [0.97875325649683, -0.72467840967746], [-0.95038560288864, 0.89563219587625], [0.17005239424212, 0.54683053962658], + [-0.76910792026848, -0.96226617549298], [0.99743281016846, 0.42697157037567], [0.95437383549973, 0.97002324109952], [0.99578905365569, -0.54106826257356], + [0.28058259829990, -0.85361420634036], [0.85256524470573, -0.64567607735589], [-0.50608540105128, -0.65846015480300], [-0.97210735183243, -0.23095213067791], + [0.95424048234441, -0.99240147091219], [-0.96926570524023, 0.73775654896574], [0.30872163214726, 0.41514960556126], [-0.24523839572639, 0.63206633394807], + [-0.33813265086024, -0.38661779441897], [-0.05826828420146, -0.06940774188029], [-0.22898461455054, 0.97054853316316], [-0.18509915019881, 0.47565762892084], + [-0.10488238045009, -0.87769947402394], [-0.71886586182037, 0.78030982480538], [0.99793873738654, 0.90041310491497], [0.57563307626120, -0.91034337352097], + [0.28909646383717, 0.96307783970534], [0.42188998312520, 0.48148651230437], [0.93335049681047, -0.43537023883588], [-0.97087374418267, 0.86636445711364], + [0.36722871286923, 0.65291654172961], [-0.81093025665696, 0.08778370229363], [-0.26240603062237, -0.92774095379098], [0.83996497984604, 0.55839849139647], + [-0.99909615720225, -0.96024605713970], [0.74649464155061, 0.12144893606462], [-0.74774595569805, -0.26898062008959], [0.95781667469567, -0.79047927052628], + [0.95472308713099, -0.08588776019550], [0.48708332746299, 0.99999041579432], [0.46332038247497, 0.10964126185063], [-0.76497004940162, 0.89210929242238], + [0.57397389364339, 0.35289703373760], [0.75374316974495, 0.96705214651335], [-0.59174397685714, -0.89405370422752], [0.75087906691890, -0.29612672982396], + [-0.98607857336230, 0.25034911730023], [-0.40761056640505, -0.90045573444695], [0.66929266740477, 0.98629493401748], [-0.97463695257310, -0.00190223301301], + [0.90145509409859, 0.99781390365446], [-0.87259289048043, 0.99233587353666], [-0.91529461447692, -0.15698707534206], [-0.03305738840705, -0.37205262859764], + [0.07223051368337, -0.88805001733626], [0.99498012188353, 0.97094358113387], [-0.74904939500519, 0.99985483641521], [0.04585228574211, 0.99812337444082], + [-0.89054954257993, -0.31791913188064], [-0.83782144651251, 0.97637632547466], [0.33454804933804, -0.86231516800408], [-0.99707579362824, 0.93237990079441], + [-0.22827527843994, 0.18874759397997], [0.67248046289143, -0.03646211390569], [-0.05146538187944, -0.92599700120679], [0.99947295749905, 0.93625229707912], + [0.66951124390363, 0.98905825623893], [-0.99602956559179, -0.44654715757688], [0.82104905483590, 0.99540741724928], [0.99186510988782, 0.72023001312947], + [-0.65284592392918, 0.52186723253637], [0.93885443798188, -0.74895312615259], [0.96735248738388, 0.90891816978629], [-0.22225968841114, 0.57124029781228], + [-0.44132783753414, -0.92688840659280], [-0.85694974219574, 0.88844532719844], [0.91783042091762, -0.46356892383970], [0.72556974415690, -0.99899555770747], + [-0.99711581834508, 0.58211560180426], [0.77638976371966, 0.94321834873819], [0.07717324253925, 0.58638399856595], [-0.56049829194163, 0.82522301569036], + [0.98398893639988, 0.39467440420569], [0.47546946844938, 0.68613044836811], [0.65675089314631, 0.18331637134880], [0.03273375457980, -0.74933109564108], + [-0.38684144784738, 0.51337349030406], [-0.97346267944545, -0.96549364384098], [-0.53282156061942, -0.91423265091354], [0.99817310731176, 0.61133572482148], + [-0.50254500772635, -0.88829338134294], [0.01995873238855, 0.85223515096765], [0.99930381973804, 0.94578896296649], [0.82907767600783, -0.06323442598128], + [-0.58660709669728, 0.96840773806582], [-0.17573736667267, -0.48166920859485], [0.83434292401346, -0.13023450646997], [0.05946491307025, 0.20511047074866], + [0.81505484574602, -0.94685947861369], [-0.44976380954860, 0.40894572671545], [-0.89746474625671, 0.99846578838537], [0.39677256130792, -0.74854668609359], + [-0.07588948563079, 0.74096214084170], [0.76343198951445, 0.41746629422634], [-0.74490104699626, 0.94725911744610], [0.64880119792759, 0.41336660830571], + [0.62319537462542, -0.93098313552599], [0.42215817594807, -0.07712787385208], [0.02704554141885, -0.05417518053666], [0.80001773566818, 0.91542195141039], + [-0.79351832348816, -0.36208897989136], [0.63872359151636, 0.08128252493444], [0.52890520960295, 0.60048872455592], [0.74238552914587, 0.04491915291044], + [0.99096131449250, -0.19451182854402], [-0.80412329643109, -0.88513818199457], [-0.64612616129736, 0.72198674804544], [0.11657770663191, -0.83662833815041], + [-0.95053182488101, -0.96939905138082], [-0.62228872928622, 0.82767262846661], [0.03004475787316, -0.99738896333384], [-0.97987214341034, 0.36526129686425], + [-0.99986980746200, -0.36021610299715], [0.89110648599879, -0.97894250343044], [0.10407960510582, 0.77357793811619], [0.95964737821728, -0.35435818285502], + [0.50843233159162, 0.96107691266205], [0.17006334670615, -0.76854025314829], [0.25872675063360, 0.99893303933816], [-0.01115998681937, 0.98496019742444], + [-0.79598702973261, 0.97138411318894], [-0.99264708948101, -0.99542822402536], [-0.99829663752818, 0.01877138824311], [-0.70801016548184, 0.33680685948117], + [-0.70467057786826, 0.93272777501857], [0.99846021905254, -0.98725746254433], [-0.63364968534650, -0.16473594423746], [-0.16258217500792, -0.95939125400802], + [-0.43645594360633, -0.94805030113284], [-0.99848471702976, 0.96245166923809], [-0.16796458968998, -0.98987511890470], [-0.87979225745213, -0.71725725041680], + [0.44183099021786, -0.93568974498761], [0.93310180125532, -0.99913308068246], [-0.93941931782002, -0.56409379640356], [-0.88590003188677, 0.47624600491382], + [0.99971463703691, -0.83889954253462], [-0.75376385639978, 0.00814643438625], [0.93887685615875, -0.11284528204636], [0.85126435782309, 0.52349251543547], + [0.39701421446381, 0.81779634174316], [-0.37024464187437, -0.87071656222959], [-0.36024828242896, 0.34655735648287], [-0.93388812549209, -0.84476541096429], + [-0.65298804552119, -0.18439575450921], [0.11960319006843, 0.99899346780168], [0.94292565553160, 0.83163906518293], [0.75081145286948, -0.35533223142265], + [0.56721979748394, -0.24076836414499], [0.46857766746029, -0.30140233457198], [0.97312313923635, -0.99548191630031], [-0.38299976567017, 0.98516909715427], + [0.41025800019463, 0.02116736935734], [0.09638062008048, 0.04411984381457], [-0.85283249275397, 0.91475563922421], [0.88866808958124, -0.99735267083226], + [-0.48202429536989, -0.96805608884164], [0.27572582416567, 0.58634753335832], [-0.65889129659168, 0.58835634138583], [0.98838086953732, 0.99994349600236], + [-0.20651349620689, 0.54593044066355], [-0.62126416356920, -0.59893681700392], [0.20320105410437, -0.86879180355289], [-0.97790548600584, 0.96290806999242], + [0.11112534735126, 0.21484763313301], [-0.41368337314182, 0.28216837680365], [0.24133038992960, 0.51294362630238], [-0.66393410674885, -0.08249679629081], + [-0.53697829178752, -0.97649903936228], [-0.97224737889348, 0.22081333579837], [0.87392477144549, -0.12796173740361], [0.19050361015753, 0.01602615387195], + [-0.46353441212724, -0.95249041539006], [-0.07064096339021, -0.94479803205886], [-0.92444085484466, -0.10457590187436], [-0.83822593578728, -0.01695043208885], + [0.75214681811150, -0.99955681042665], [-0.42102998829339, 0.99720941999394], [-0.72094786237696, -0.35008961934255], [0.78843311019251, 0.52851398958271], + [0.97394027897442, -0.26695944086561], [0.99206463477946, -0.57010120849429], [0.76789609461795, -0.76519356730966], [-0.82002421836409, -0.73530179553767], + [0.81924990025724, 0.99698425250579], [-0.26719850873357, 0.68903369776193], [-0.43311260380975, 0.85321815947490], [0.99194979673836, 0.91876249766422], + [-0.80692001248487, -0.32627540663214], [0.43080003649976, -0.21919095636638], [0.67709491937357, -0.95478075822906], [0.56151770568316, -0.70693811747778], + [0.10831862810749, -0.08628837174592], [0.91229417540436, -0.65987351408410], [-0.48972893932274, 0.56289246362686], [-0.89033658689697, -0.71656563987082], + [0.65269447475094, 0.65916004833932], [0.67439478141121, -0.81684380846796], [-0.47770832416973, -0.16789556203025], [-0.99715979260878, -0.93565784007648], + [-0.90889593602546, 0.62034397054380], [-0.06618622548177, -0.23812217221359], [0.99430266919728, 0.18812555317553], [0.97686402381843, -0.28664534366620], + [0.94813650221268, -0.97506640027128], [-0.95434497492853, -0.79607978501983], [-0.49104783137150, 0.32895214359663], [0.99881175120751, 0.88993983831354], + [0.50449166760303, -0.85995072408434], [0.47162891065108, -0.18680204049569], [-0.62081581361840, 0.75000676218956], [-0.43867015250812, 0.99998069244322], + [0.98630563232075, -0.53578899600662], [-0.61510362277374, -0.89515019899997], [-0.03841517601843, -0.69888815681179], [-0.30102157304644, -0.07667808922205], + [0.41881284182683, 0.02188098922282], [-0.86135454941237, 0.98947480909359], [0.67226861393788, -0.13494389011014], [-0.70737398842068, -0.76547349325992], + [0.94044946687963, 0.09026201157416], [-0.82386352534327, 0.08924768823676], [-0.32070666698656, 0.50143421908753], [0.57593163224487, -0.98966422921509], + [-0.36326018419965, 0.07440243123228], [0.99979044674350, -0.14130287347405], [-0.92366023326932, -0.97979298068180], [-0.44607178518598, -0.54233252016394], + [0.44226800932956, 0.71326756742752], [0.03671907158312, 0.63606389366675], [0.52175424682195, -0.85396826735705], [-0.94701139690956, -0.01826348194255], + [-0.98759606946049, 0.82288714303073], [0.87434794743625, 0.89399495655433], [-0.93412041758744, 0.41374052024363], [0.96063943315511, 0.93116709541280], + [0.97534253457837, 0.86150930812689], [0.99642466504163, 0.70190043427512], [-0.94705089665984, -0.29580042814306], [0.91599807087376, -0.98147830385781], +]; + +/// `t_huffman_env_1_5dB` (`(codeword, length)` by index): envelope, time direction, 1.5 dB steps (LAV 60). +#[rustfmt::skip] +pub(crate) static T_ENV_1_5DB: [(u32, u8); 121] = [ + (0x3ffd6, 18), (0x3ffd7, 18), (0x3ffd8, 18), (0x3ffd9, 18), (0x3ffda, 18), (0x3ffdb, 18), (0x7ffb8, 19), (0x7ffb9, 19), + (0x7ffba, 19), (0x7ffbb, 19), (0x7ffbc, 19), (0x7ffbd, 19), (0x7ffbe, 19), (0x7ffbf, 19), (0x7ffc0, 19), (0x7ffc1, 19), + (0x7ffc2, 19), (0x7ffc3, 19), (0x7ffc4, 19), (0x7ffc5, 19), (0x7ffc6, 19), (0x7ffc7, 19), (0x7ffc8, 19), (0x7ffc9, 19), + (0x7ffca, 19), (0x7ffcb, 19), (0x7ffcc, 19), (0x7ffcd, 19), (0x7ffce, 19), (0x7ffcf, 19), (0x7ffd0, 19), (0x7ffd1, 19), + (0x7ffd2, 19), (0x7ffd3, 19), (0x1ffe6, 17), (0x3ffd4, 18), (0xfff0, 16), (0x1ffe9, 17), (0x3ffd5, 18), (0x1ffe7, 17), + (0xfff1, 16), (0xffec, 16), (0xffed, 16), (0xffee, 16), (0x7ff4, 15), (0x3ff9, 14), (0x3ff7, 14), (0x1ffa, 13), + (0x1ff9, 13), (0xffb, 12), (0x7fc, 11), (0x3fc, 10), (0x1fd, 9), (0xfd, 8), (0x7d, 7), (0x3d, 6), + (0x1d, 5), (0xd, 4), (0x5, 3), (0x1, 2), (0x0, 2), (0x4, 3), (0xc, 4), (0x1c, 5), + (0x3c, 6), (0x7c, 7), (0xfc, 8), (0x1fc, 9), (0x3fd, 10), (0xffa, 12), (0x1ff8, 13), (0x3ff6, 14), + (0x3ff8, 14), (0x7ff5, 15), (0xffef, 16), (0x1ffe8, 17), (0xfff2, 16), (0x7ffd4, 19), (0x7ffd5, 19), (0x7ffd6, 19), + (0x7ffd7, 19), (0x7ffd8, 19), (0x7ffd9, 19), (0x7ffda, 19), (0x7ffdb, 19), (0x7ffdc, 19), (0x7ffdd, 19), (0x7ffde, 19), + (0x7ffdf, 19), (0x7ffe0, 19), (0x7ffe1, 19), (0x7ffe2, 19), (0x7ffe3, 19), (0x7ffe4, 19), (0x7ffe5, 19), (0x7ffe6, 19), + (0x7ffe7, 19), (0x7ffe8, 19), (0x7ffe9, 19), (0x7ffea, 19), (0x7ffeb, 19), (0x7ffec, 19), (0x7ffed, 19), (0x7ffee, 19), + (0x7ffef, 19), (0x7fff0, 19), (0x7fff1, 19), (0x7fff2, 19), (0x7fff3, 19), (0x7fff4, 19), (0x7fff5, 19), (0x7fff6, 19), + (0x7fff7, 19), (0x7fff8, 19), (0x7fff9, 19), (0x7fffa, 19), (0x7fffb, 19), (0x7fffc, 19), (0x7fffd, 19), (0x7fffe, 19), + (0x7ffff, 19), +]; + +/// `f_huffman_env_1_5dB` (`(codeword, length)` by index): envelope, frequency direction, 1.5 dB steps (LAV 60). +#[rustfmt::skip] +pub(crate) static F_ENV_1_5DB: [(u32, u8); 121] = [ + (0x7ffe7, 19), (0x7ffe8, 19), (0xfffd2, 20), (0xfffd3, 20), (0xfffd4, 20), (0xfffd5, 20), (0xfffd6, 20), (0xfffd7, 20), + (0xfffd8, 20), (0x7ffda, 19), (0xfffd9, 20), (0xfffda, 20), (0xfffdb, 20), (0xfffdc, 20), (0x7ffdb, 19), (0xfffdd, 20), + (0x7ffdc, 19), (0x7ffdd, 19), (0xfffde, 20), (0x3ffe4, 18), (0xfffdf, 20), (0xfffe0, 20), (0xfffe1, 20), (0x7ffde, 19), + (0xfffe2, 20), (0xfffe3, 20), (0xfffe4, 20), (0x7ffdf, 19), (0xfffe5, 20), (0x7ffe0, 19), (0x3ffe8, 18), (0x7ffe1, 19), + (0x3ffe0, 18), (0x3ffe9, 18), (0x1ffef, 17), (0x3ffe5, 18), (0x1ffec, 17), (0x1ffed, 17), (0x1ffee, 17), (0xfff4, 16), + (0xfff3, 16), (0xfff0, 16), (0x7ff7, 15), (0x7ff6, 15), (0x3ffa, 14), (0x1ffa, 13), (0x1ff9, 13), (0xffa, 12), + (0xff8, 12), (0x7f9, 11), (0x3fb, 10), (0x1fc, 9), (0x1fa, 9), (0xfb, 8), (0x7c, 7), (0x3c, 6), + (0x1c, 5), (0xc, 4), (0x5, 3), (0x1, 2), (0x0, 2), (0x4, 3), (0xd, 4), (0x1d, 5), + (0x3d, 6), (0xfa, 8), (0xfc, 8), (0x1fb, 9), (0x3fa, 10), (0x7f8, 11), (0x7fa, 11), (0x7fb, 11), + (0xff9, 12), (0xffb, 12), (0x1ff8, 13), (0x1ffb, 13), (0x3ff8, 14), (0x3ff9, 14), (0xfff1, 16), (0xfff2, 16), + (0x1ffea, 17), (0x1ffeb, 17), (0x3ffe1, 18), (0x3ffe2, 18), (0x3ffea, 18), (0x3ffe3, 18), (0x3ffe6, 18), (0x3ffe7, 18), + (0x3ffeb, 18), (0xfffe6, 20), (0x7ffe2, 19), (0xfffe7, 20), (0xfffe8, 20), (0xfffe9, 20), (0xfffea, 20), (0xfffeb, 20), + (0xfffec, 20), (0x7ffe3, 19), (0xfffed, 20), (0xfffee, 20), (0xfffef, 20), (0xffff0, 20), (0x7ffe4, 19), (0xffff1, 20), + (0x3ffec, 18), (0xffff2, 20), (0xffff3, 20), (0x7ffe5, 19), (0x7ffe6, 19), (0xffff4, 20), (0xffff5, 20), (0xffff6, 20), + (0xffff7, 20), (0xffff8, 20), (0xffff9, 20), (0xffffa, 20), (0xffffb, 20), (0xffffc, 20), (0xffffd, 20), (0xffffe, 20), + (0xfffff, 20), +]; + +/// `t_huffman_env_bal_1_5dB` (`(codeword, length)` by index): envelope balance, time direction, 1.5 dB steps (LAV 24). +#[rustfmt::skip] +pub(crate) static T_ENV_BAL_1_5DB: [(u32, u8); 49] = [ + (0xffe4, 16), (0xffe5, 16), (0xffe6, 16), (0xffe7, 16), (0xffe8, 16), (0xffe9, 16), (0xffea, 16), (0xffeb, 16), + (0xffec, 16), (0xffed, 16), (0xffee, 16), (0xffef, 16), (0xfff0, 16), (0xfff1, 16), (0xfff2, 16), (0xfff3, 16), + (0xfff4, 16), (0xffe2, 16), (0xffc, 12), (0x7fc, 11), (0x1fe, 9), (0x7e, 7), (0x1e, 5), (0x6, 3), + (0x0, 1), (0x2, 2), (0xe, 4), (0x3e, 6), (0xfe, 8), (0x7fd, 11), (0xffd, 12), (0x7ff0, 15), + (0xffe3, 16), (0xfff5, 16), (0xfff6, 16), (0xfff7, 16), (0xfff8, 16), (0xfff9, 16), (0xfffa, 16), (0x1fff6, 17), + (0x1fff7, 17), (0x1fff8, 17), (0x1fff9, 17), (0x1fffa, 17), (0x1fffb, 17), (0x1fffc, 17), (0x1fffd, 17), (0x1fffe, 17), + (0x1ffff, 17), +]; + +/// `f_huffman_env_bal_1_5dB` (`(codeword, length)` by index): envelope balance, frequency direction, 1.5 dB steps (LAV 24). +#[rustfmt::skip] +pub(crate) static F_ENV_BAL_1_5DB: [(u32, u8); 49] = [ + (0x3ffe2, 18), (0x3ffe3, 18), (0x3ffe4, 18), (0x3ffe5, 18), (0x3ffe6, 18), (0x3ffe7, 18), (0x3ffe8, 18), (0x3ffe9, 18), + (0x3ffea, 18), (0x3ffeb, 18), (0x3ffec, 18), (0x3ffed, 18), (0x3ffee, 18), (0x3ffef, 18), (0x3fff0, 18), (0xfff7, 16), + (0x1fff0, 17), (0x3ffc, 14), (0x7fe, 11), (0x7fc, 11), (0xfe, 8), (0x7e, 7), (0xe, 4), (0x2, 2), + (0x0, 1), (0x6, 3), (0x1e, 5), (0x3e, 6), (0x1fe, 9), (0x7fd, 11), (0xffe, 12), (0x7ffa, 15), + (0xfff6, 16), (0x3fff1, 18), (0x3fff2, 18), (0x3fff3, 18), (0x3fff4, 18), (0x3fff5, 18), (0x3fff6, 18), (0x3fff7, 18), + (0x3fff8, 18), (0x3fff9, 18), (0x3fffa, 18), (0x3fffb, 18), (0x3fffc, 18), (0x3fffd, 18), (0x3fffe, 18), (0x7fffe, 19), + (0x7ffff, 19), +]; + +/// `t_huffman_env_3_0dB` (`(codeword, length)` by index): envelope, time direction, 3 dB steps (LAV 31). +#[rustfmt::skip] +pub(crate) static T_ENV_3_0DB: [(u32, u8); 63] = [ + (0x3ffed, 18), (0x3ffee, 18), (0x7ffde, 19), (0x7ffdf, 19), (0x7ffe0, 19), (0x7ffe1, 19), (0x7ffe2, 19), (0x7ffe3, 19), + (0x7ffe4, 19), (0x7ffe5, 19), (0x7ffe6, 19), (0x7ffe7, 19), (0x7ffe8, 19), (0x7ffe9, 19), (0x7ffea, 19), (0x7ffeb, 19), + (0x7ffec, 19), (0x1fff4, 17), (0xfff7, 16), (0xfff9, 16), (0xfff8, 16), (0x3ffb, 14), (0x3ffa, 14), (0x3ff8, 14), + (0x1ffa, 13), (0xffc, 12), (0x7fc, 11), (0xfe, 8), (0x3e, 6), (0xe, 4), (0x2, 2), (0x0, 1), + (0x6, 3), (0x1e, 5), (0x7e, 7), (0x1fe, 9), (0x7fd, 11), (0x1ffb, 13), (0x3ff9, 14), (0x3ffc, 14), + (0x7ffa, 15), (0xfff6, 16), (0x1fff5, 17), (0x3ffec, 18), (0x7ffed, 19), (0x7ffee, 19), (0x7ffef, 19), (0x7fff0, 19), + (0x7fff1, 19), (0x7fff2, 19), (0x7fff3, 19), (0x7fff4, 19), (0x7fff5, 19), (0x7fff6, 19), (0x7fff7, 19), (0x7fff8, 19), + (0x7fff9, 19), (0x7fffa, 19), (0x7fffb, 19), (0x7fffc, 19), (0x7fffd, 19), (0x7fffe, 19), (0x7ffff, 19), +]; + +/// `f_huffman_env_3_0dB` (`(codeword, length)` by index): envelope and noise floor, frequency direction, 3 dB steps (LAV 31). +#[rustfmt::skip] +pub(crate) static F_ENV_3_0DB: [(u32, u8); 63] = [ + (0xffff0, 20), (0xffff1, 20), (0xffff2, 20), (0xffff3, 20), (0xffff4, 20), (0xffff5, 20), (0xffff6, 20), (0x3fff3, 18), + (0x7fff5, 19), (0x7ffee, 19), (0x7ffef, 19), (0x7fff6, 19), (0x3fff4, 18), (0x3fff2, 18), (0xffff7, 20), (0x7fff0, 19), + (0x1fff5, 17), (0x3fff0, 18), (0x1fff4, 17), (0xfff7, 16), (0xfff6, 16), (0x7ff8, 15), (0x3ffb, 14), (0xffd, 12), + (0x7fd, 11), (0x3fd, 10), (0x1fd, 9), (0xfd, 8), (0x3e, 6), (0xe, 4), (0x2, 2), (0x0, 1), + (0x6, 3), (0x1e, 5), (0xfc, 8), (0x1fc, 9), (0x3fc, 10), (0x7fc, 11), (0xffc, 12), (0x1ffc, 13), + (0x3ffa, 14), (0x7ff9, 15), (0x7ffa, 15), (0xfff8, 16), (0xfff9, 16), (0x1fff6, 17), (0x1fff7, 17), (0x3fff5, 18), + (0x3fff6, 18), (0x3fff1, 18), (0xffff8, 20), (0x7fff1, 19), (0x7fff2, 19), (0x7fff3, 19), (0xffff9, 20), (0x7fff7, 19), + (0x7fff4, 19), (0xffffa, 20), (0xffffb, 20), (0xffffc, 20), (0xffffd, 20), (0xffffe, 20), (0xfffff, 20), +]; + +/// `t_huffman_env_bal_3_0dB` (`(codeword, length)` by index): envelope balance, time direction, 3 dB steps (LAV 12). +#[rustfmt::skip] +pub(crate) static T_ENV_BAL_3_0DB: [(u32, u8); 25] = [ + (0x1ff2, 13), (0x1ff3, 13), (0x1ff4, 13), (0x1ff5, 13), (0x1ff6, 13), (0x1ff7, 13), (0x1ff8, 13), (0xff8, 12), + (0xfe, 8), (0x7e, 7), (0xe, 4), (0x6, 3), (0x0, 1), (0x2, 2), (0x1e, 5), (0x3e, 6), + (0x1fe, 9), (0x1ff9, 13), (0x1ffa, 13), (0x1ffb, 13), (0x1ffc, 13), (0x1ffd, 13), (0x1ffe, 13), (0x3ffe, 14), + (0x3fff, 14), +]; + +/// `f_huffman_env_bal_3_0dB` (`(codeword, length)` by index): envelope and noise floor balance, frequency direction, 3 dB steps (LAV 12). +#[rustfmt::skip] +pub(crate) static F_ENV_BAL_3_0DB: [(u32, u8); 25] = [ + (0x1ff7, 13), (0x1ff8, 13), (0x1ff9, 13), (0x1ffa, 13), (0x1ffb, 13), (0x3ff8, 14), (0x3ff9, 14), (0x7fc, 11), + (0xfe, 8), (0x7e, 7), (0xe, 4), (0x2, 2), (0x0, 1), (0x6, 3), (0x1e, 5), (0x3e, 6), + (0x1fe, 9), (0xffa, 12), (0x1ff6, 13), (0x3ffa, 14), (0x3ffb, 14), (0x3ffc, 14), (0x3ffd, 14), (0x3ffe, 14), + (0x3fff, 14), +]; + +/// `t_huffman_noise_3_0dB` (`(codeword, length)` by index): noise floor, time direction (LAV 31). +#[rustfmt::skip] +pub(crate) static T_NOISE_3_0DB: [(u32, u8); 63] = [ + (0x1fce, 13), (0x1fcf, 13), (0x1fd0, 13), (0x1fd1, 13), (0x1fd2, 13), (0x1fd3, 13), (0x1fd4, 13), (0x1fd5, 13), + (0x1fd6, 13), (0x1fd7, 13), (0x1fd8, 13), (0x1fd9, 13), (0x1fda, 13), (0x1fdb, 13), (0x1fdc, 13), (0x1fdd, 13), + (0x1fde, 13), (0x1fdf, 13), (0x1fe0, 13), (0x1fe1, 13), (0x1fe2, 13), (0x1fe3, 13), (0x1fe4, 13), (0x1fe5, 13), + (0x1fe6, 13), (0x1fe7, 13), (0x7f2, 11), (0xfd, 8), (0x3e, 6), (0xe, 4), (0x6, 3), (0x0, 1), + (0x2, 2), (0x1e, 5), (0xfc, 8), (0x3f8, 10), (0x1fcc, 13), (0x1fe8, 13), (0x1fe9, 13), (0x1fea, 13), + (0x1feb, 13), (0x1fec, 13), (0x1fcd, 13), (0x1fed, 13), (0x1fee, 13), (0x1fef, 13), (0x1ff0, 13), (0x1ff1, 13), + (0x1ff2, 13), (0x1ff3, 13), (0x1ff4, 13), (0x1ff5, 13), (0x1ff6, 13), (0x1ff7, 13), (0x1ff8, 13), (0x1ff9, 13), + (0x1ffa, 13), (0x1ffb, 13), (0x1ffc, 13), (0x1ffd, 13), (0x1ffe, 13), (0x3ffe, 14), (0x3fff, 14), +]; + +/// `t_huffman_noise_bal_3_0dB` (`(codeword, length)` by index): noise floor balance, time direction (LAV 12). +#[rustfmt::skip] +pub(crate) static T_NOISE_BAL_3_0DB: [(u32, u8); 25] = [ + (0xec, 8), (0xed, 8), (0xee, 8), (0xef, 8), (0xf0, 8), (0xf1, 8), (0xf2, 8), (0xf3, 8), + (0xf4, 8), (0xf5, 8), (0x1c, 5), (0x2, 2), (0x0, 1), (0x6, 3), (0x3a, 6), (0xf6, 8), + (0xf7, 8), (0xf8, 8), (0xf9, 8), (0xfa, 8), (0xfb, 8), (0xfc, 8), (0xfd, 8), (0xfe, 8), + (0xff, 8), +]; diff --git a/crates/audio/src/track.rs b/crates/audio/src/track.rs index 920f571ae..e522e6f68 100644 --- a/crates/audio/src/track.rs +++ b/crates/audio/src/track.rs @@ -24,8 +24,10 @@ pub const LOOKAHEAD_PACKETS: usize = 8; enum Inner { #[cfg(feature = "aac-decode")] Aac { + /// The decoder, with SBR on for HE-AAC. dec: Box, - up: Option, + /// HE-AAC: 2048 samples per access unit, at twice the core rate. + sbr: bool, order: Option<&'static [usize]>, /// The format ADTS packets must keep, when their headers gave the configuration. adts_format: Option, @@ -74,7 +76,7 @@ pub(crate) type PacketFrames = fn(&[u8], u64) -> Option; /// /// It takes the track's [`CodecParams`] (from `isobmff::Track::codec_params`, /// `matroska::Track::codec_params`, `ogg::Stream::codec_params`, or built by hand for another -/// container) and decodes AAC (LC; HE-AAC as its upsampled LC core), ALAC, Opus, AC-3/E-AC-3, +/// container) and decodes AAC (LC, and HE-AAC with its SBR high band), ALAC, Opus, AC-3/E-AC-3, /// MP1/MP2, MP3 and PCM. Container timing (AAC priming, Opus pre-skip, end trimming) is the caller's: /// every decoded sample is returned. /// @@ -168,17 +170,18 @@ impl TrackDecoder { params.extradata.clone() }; let asc = asc.as_slice(); - let dec = aac::Decoder::new(asc)?; + let mut dec = aac::Decoder::new(asc)?; let out = aac::output_rate(asc, payloads.iter().copied()).unwrap_or(dec.sample_rate()); - let up = (dec.sample_rate().checked_mul(2) == Some(out)).then(|| aac::Upsample2x::new(dec.channels())); - let codec = if up.is_some() { CodecId::HeAac } else { CodecId::Aac }; - // Only twice the core rate is upsampled to; any other signalled rate plays the core. - let out = if up.is_some() { out } else { dec.sample_rate() }; + let sbr = dec.sample_rate().checked_mul(2) == Some(out); + let codec = if sbr { CodecId::HeAac } else { CodecId::Aac }; + // Only SBR at twice the core rate is decoded; any other signalled rate plays the core. + dec.set_sbr(sbr); + let out = dec.sample_rate(); // a program config element that spells out a configuration has its speakers let cfg = aac::speaker_config(dec.config()); let layout = aac_layout(cfg, dec.channels()); let order = layout.is_ordered().then(|| aac::wav_order(cfg)).flatten(); - (Inner::Aac { dec: Box::new(dec), up, order, adts_format }, codec, out, layout, SampleFormat::Compressed) + (Inner::Aac { dec: Box::new(dec), sbr, order, adts_format }, codec, out, layout, SampleFormat::Compressed) } #[cfg(feature = "alac-decode")] CodecId::Alac => { @@ -320,7 +323,7 @@ impl TrackDecoder { let mut partial = false; let channels = match &mut self.inner { #[cfg(feature = "aac-decode")] - Inner::Aac { dec, up, order, adts_format } => { + Inner::Aac { dec, order, adts_format, .. } => { let mut out: Vec> = Vec::new(); let units = match adts_frames(packet) { Some(frames) => adts_units(frames, *adts_format)?, @@ -328,17 +331,11 @@ impl TrackDecoder { }; // A failed frame leaves the decoder as it was, so the frames before it in the // packet must too: their audio is replaced by the packet's concealment. - let before = (units.len() > 1).then(|| (dec.clone(), up.clone())); - let decoded = units.into_iter().try_for_each(|unit| { - let mut block = dec.decode(unit)?; - if let Some(up) = up.as_mut() { - block = up.process(block); - } - append(&mut out, block) - }); + let before = (units.len() > 1).then(|| dec.clone()); + let decoded = units.into_iter().try_for_each(|unit| append(&mut out, dec.decode(unit)?)); if let Err(e) = decoded { if let Some(before) = before { - (*dec, *up) = before; + *dec = before; } return Err(e); } @@ -440,8 +437,8 @@ impl TrackDecoder { None } #[cfg(feature = "aac-decode")] - Inner::Aac { up, .. } => { - let per_unit = aac::FRAME_LEN * if up.is_some() { 2 } else { 1 }; + Inner::Aac { sbr, .. } => { + let per_unit = aac::FRAME_LEN * if *sbr { 2 } else { 1 }; match adts_frames(data) { Some(f) => Some(f.iter().map(|(h, _)| usize::from(h.raw_data_blocks)).sum::().min(MAX_ADTS_FRAMES_PER_PACKET) * per_unit), // A packet lost whole: as long as the last one, in a stream of multi-frame @@ -488,7 +485,7 @@ impl TrackDecoder { pub fn frames_per_packet(&self) -> Option { match &self.inner { #[cfg(feature = "aac-decode")] - Inner::Aac { up, .. } => Some(aac::FRAME_LEN * if up.is_some() { 2 } else { 1 }), + Inner::Aac { sbr, .. } => Some(aac::FRAME_LEN * if *sbr { 2 } else { 1 }), #[cfg(feature = "alac-decode")] Inner::Alac { .. } => None, #[cfg(feature = "ac3-decode")] @@ -544,7 +541,7 @@ impl TrackDecoder { pub fn preroll(&self) -> usize { match &self.inner { #[cfg(feature = "aac-decode")] - Inner::Aac { up, .. } => 2 * aac::FRAME_LEN * if up.is_some() { 2 } else { 1 }, + Inner::Aac { sbr, .. } => 2 * aac::FRAME_LEN * if *sbr { 2 } else { 1 }, #[cfg(feature = "alac-decode")] Inner::Alac { .. } => 0, #[cfg(feature = "ac3-decode")] @@ -570,12 +567,7 @@ impl TrackDecoder { self.last_frames = 0; match &mut self.inner { #[cfg(feature = "aac-decode")] - Inner::Aac { dec, up, .. } => { - dec.reset(); - if let Some(up) = up { - up.reset(); - } - } + Inner::Aac { dec, .. } => dec.reset(), #[cfg(feature = "alac-decode")] Inner::Alac { .. } => {} #[cfg(feature = "ac3-decode")] diff --git a/crates/audio/tests/codecs/aac/he_aac_oracle.rs b/crates/audio/tests/codecs/aac/he_aac_oracle.rs new file mode 100644 index 000000000..e41493e16 --- /dev/null +++ b/crates/audio/tests/codecs/aac/he_aac_oracle.rs @@ -0,0 +1,165 @@ +//! HE-AAC (SBR) decoding against ffmpeg's decoder, on streams from ffmpeg's AudioToolbox encoder +//! (oracle only; skipped without ffmpeg or without `aac_at`, which only macOS builds have). + +use std::process::Command; + +use craft_audio::aac::{Decoder, split_adts}; + +use super::common::*; + +/// ffmpeg, if it has the AudioToolbox AAC encoder. +fn ffmpeg_with_aac_at() -> Option { + let ff = ffmpeg()?; + let out = Command::new(&ff).args(["-hide_banner", "-encoders"]).output().ok()?; + if !String::from_utf8_lossy(&out.stdout).contains("aac_at") { + eprintln!("SKIPPED: ffmpeg has no aac_at encoder"); + return None; + } + Some(ff) +} + +fn input(rate: u32, channels: usize, secs: f32) -> Vec> { + let n = (rate as f32 * secs) as usize; + (0..channels) + .map(|c| { + let a = chords(rate, n, 0.004 * c as f32); + let s = speech(rate, n, 11 + c as u64); + let z = noise(n, 0.05, 3 + c as u64); + let k = clicks(rate, n); + a.iter().zip(&s).zip(&z).zip(&k).map(|(((a, s), z), k)| 0.4 * a + 0.4 * s * if c == 1 { -0.7 } else { 1.0 } + z + 0.3 * k).collect() + }) + .collect() +} + +/// ffmpeg's decode of a stream of `channels` channels. ffmpeg decodes mono HE-AAC as stereo (it +/// allows for parametric stereo the stream may hold), with the same signal in both channels when +/// there is none: of those, the first is taken (a downmix would scale it by -3 dB). +fn ffmpeg_decode_as(ff: &std::path::Path, file: &std::path::Path, channels: usize) -> Vec> { + let mut planar = ffmpeg_decode_raw(ff, file, channels.max(2)); + planar.truncate(channels); + planar +} + +fn ffmpeg_decode_raw(ff: &std::path::Path, file: &std::path::Path, channels: usize) -> Vec> { + let out = Command::new(ff).args(["-v", "error", "-i"]).arg(file).args(["-ac", &channels.to_string(), "-f", "f32le", "-"]).output().unwrap(); + assert!(out.status.success() && out.stderr.is_empty(), "{}", String::from_utf8_lossy(&out.stderr)); + let samples: Vec = out.stdout.as_chunks::<4>().0.iter().map(|&b| f32::from_le_bytes(b)).collect(); + (0..channels).map(|c| samples.iter().skip(c).step_by(channels).copied().collect()).collect() +} + +/// Our decode of an ADTS stream, SBR on. +fn decode_sbr(data: &[u8]) -> (Vec>, u32) { + let frames = split_adts(data).unwrap(); + let mut dec = Decoder::from_adts(&frames[0].0).unwrap(); + dec.set_sbr(true); + let mut out: Vec> = Vec::new(); + for (_, au) in frames { + let pcm = dec.decode(au).unwrap(); + out.resize(pcm.len(), Vec::new()); + for (o, p) in out.iter_mut().zip(pcm) { + o.extend(p); + } + } + (out, dec.sample_rate()) +} + +/// The lag of `b` behind `a` (within ±`range`) that correlates best. +fn best_lag(a: &[f32], b: &[f32], range: i64) -> i64 { + let n = a.len().min(b.len()) as i64; + let (from, to) = (range, n - range); + (-range..=range) + .max_by(|&x, &y| { + let c = |lag: i64| (from..to).step_by(3).map(|i| a[i as usize] as f64 * b[(i + lag) as usize] as f64).sum::(); + c(x).total_cmp(&c(y)) + }) + .unwrap() +} + +/// Energy in dB of `x[from..from + 2048]` within `lo..hi` Hz (Hann-windowed DFT). +fn band_db(x: &[f32], from: usize, rate: f32, lo: f32, hi: f32) -> f64 { + let n = 2048; + let seg = &x[from..from + n]; + let (b0, b1) = ((lo / rate * n as f32) as usize, (hi / rate * n as f32) as usize); + let mut e = 0f64; + for b in b0..b1.min(n / 2) { + let (mut re, mut im) = (0f64, 0f64); + for (i, &s) in seg.iter().enumerate() { + let w = 0.5 - 0.5 * (2.0 * std::f64::consts::PI * i as f64 / n as f64).cos(); + let ph = 2.0 * std::f64::consts::PI * (b * i) as f64 / n as f64; + re += s as f64 * w * ph.cos(); + im += s as f64 * w * ph.sin(); + } + e += re * re + im * im; + } + 10.0 * (e + 1e-12).log10() +} + +struct Case { + name: &'static str, + rate: u32, + channels: usize, + bitrate: &'static str, +} + +/// HE-AAC streams of 1 to 6 channels at 22.05 to 48 kHz decode as ffmpeg decodes them: the same +/// timing, the low band to 55 dB and the rebuilt high band's level to 0.1 dB. +#[test] +fn matches_ffmpeg_he_aac_decoder() { + let Some(ff) = ffmpeg_with_aac_at() else { return }; + let cases = [ + Case { name: "he_stereo_44k_48k", rate: 44_100, channels: 2, bitrate: "48k" }, + Case { name: "he_mono_44k_32k", rate: 44_100, channels: 1, bitrate: "32k" }, + Case { name: "he_stereo_48k_64k", rate: 48_000, channels: 2, bitrate: "64k" }, + Case { name: "he_stereo_32k_32k", rate: 32_000, channels: 2, bitrate: "32k" }, + Case { name: "he_stereo_44k_24k", rate: 44_100, channels: 2, bitrate: "24k" }, + Case { name: "he_stereo_22k_24k", rate: 22_050, channels: 2, bitrate: "24k" }, + Case { name: "he_51_48k_160k", rate: 48_000, channels: 6, bitrate: "160k" }, + ]; + for case in &cases { + let path = fixtures().join(format!("{}.aac", case.name)); + ffmpeg_encode(&ff, &input(case.rate, case.channels, 3.0), case.rate, &["-c:a", "aac_at", "-profile:a", "4", "-b:a", case.bitrate], &path); + let data = std::fs::read(&path).unwrap(); + let refr = ffmpeg_decode_as(&ff, &path, case.channels); + let (ours, rate) = decode_sbr(&data); + // ffmpeg puts surround in WAV order, the decoder in AAC element order + let ours: Vec> = match (case.channels == 6).then(|| craft_audio::aac::wav_order(6)).flatten() { + Some(order) => order.iter().map(|&c| ours[c].clone()).collect(), + None => ours, + }; + assert_eq!(rate, case.rate, "{}", case.name); + assert_eq!(ours.len(), case.channels); + // the same delay as ffmpeg's decoder: 962 samples + assert_eq!(best_lag(&refr[0], &ours[0], 2100), 0, "{}", case.name); + let core_nyquist = case.rate as f32 / 4.0; + for c in 0..case.channels { + let n = refr[c].len().min(ours[c].len()) - 4096; + let a: Vec = refr[c][2048..n].to_vec(); + let b: Vec = ours[c][2048..n].to_vec(); + let full = snr(&a, &b); + let (la, lb) = (lowpass(&a, core_nyquist * 0.8, case.rate as f32), lowpass(&b, core_nyquist * 0.8, case.rate as f32)); + let low = snr(&la, &lb); + let mut diffs = Vec::new(); + for from in (4096..a.len() - 4096).step_by(4096) { + for (lo, hi) in + [(core_nyquist * 1.05, core_nyquist * 1.3), (core_nyquist * 1.3, core_nyquist * 1.7), (core_nyquist * 1.7, core_nyquist * 2.0)] + { + let (ea, eb) = (band_db(&a, from, case.rate as f32, lo, hi), band_db(&b, from, case.rate as f32, lo, hi)); + if ea > 0.0 { + diffs.push(eb - ea); + } + } + } + let mean = diffs.iter().sum::() / diffs.len().max(1) as f64; + let mad = diffs.iter().map(|d| d.abs()).sum::() / diffs.len().max(1) as f64; + println!( + "{} ch{c}: full SNR {full:.1} dB, low band SNR {low:.1} dB, high band level {mean:+.2} dB (mean |Δ| {mad:.2} dB over {})", + case.name, + diffs.len() + ); + // The low band differs by the core decoders' rounding; the high band's noise comes + // from the same table, so whole frames match too. + assert!(full > 45.0 && low > 55.0, "{} ch{c}: SNR {full:.1} / {low:.1} dB", case.name); + assert!(mad < 0.1, "{} ch{c}: high band off by {mad:.2} dB", case.name); + } + } +} diff --git a/crates/audio/tests/codecs/aac/mod.rs b/crates/audio/tests/codecs/aac/mod.rs index 45e627242..7d55a7364 100644 --- a/crates/audio/tests/codecs/aac/mod.rs +++ b/crates/audio/tests/codecs/aac/mod.rs @@ -3,4 +3,5 @@ mod common; mod decoder_oracle; mod encoder_oracle; +mod he_aac_oracle; mod robustness; From fa9ca366973c0883c9c5750e2204903534fc04ef Mon Sep 17 00:00:00 2001 From: alehel Date: Thu, 12 Nov 2026 20:28:37 +0100 Subject: [PATCH 071/520] craft-audio: SBR review fixes - A VARFIX pointer of 1 marks no transient envelope, like 0. - A start at QMF channel 32 is valid; kx is checked once the crossover is known. - The noise and harmonic gains take q / (1 + q) first, so two clamped levels no longer overflow to infinity and NaN. - A header that runs past its fill element is refused before it replaces the one in use. - A new program config layout drops the SBR decoders, whose history was the old order's. - The ffmpeg oracle requires a high band to compare on every channel but the 5.1 LFE. Refs #1121 Co-Authored-By: Claude Opus 5.5 --- crates/audio/src/aac/decoder.rs | 11 +++- crates/audio/src/aac/sbr/bitstream.rs | 6 ++- crates/audio/src/aac/sbr/freq.rs | 13 ++++- crates/audio/src/aac/sbr/hf.rs | 7 +-- crates/audio/src/aac/sbr/mod.rs | 51 +++++++++++++++---- .../audio/tests/codecs/aac/he_aac_oracle.rs | 10 ++-- 6 files changed, 78 insertions(+), 20 deletions(-) diff --git a/crates/audio/src/aac/decoder.rs b/crates/audio/src/aac/decoder.rs index 1d8713f54..ec5dae9fc 100644 --- a/crates/audio/src/aac/decoder.rs +++ b/crates/audio/src/aac/decoder.rs @@ -135,6 +135,10 @@ impl Decoder { } self.layout = layout; self.offsets = offsets; + // the SBR decoders' history belongs to the old element order: `apply_sbr` rebuilds them + if let Some(e) = self.sbr_out.as_mut() { + e.clear(); + } self.channels = vec![ChannelState { overlap: vec![0.0; 1024], prev_shape: WindowShape::Sine }; n]; } @@ -700,7 +704,7 @@ mod tests { /// its SBR fill element describes is there; with SBR off, the core alone (silence). #[test] fn sbr_fill_elements_rebuild_the_high_band() { - let payload = sbr::test_payload(44_100, |_| 40, 0).unwrap(); + let payload = sbr::test_payload(44_100, |_| 40, |_| 0).unwrap(); let mut bw = BitWriter::new(); bw.write_bits(ID_SCE, 3); bw.write_bits(0, 4); @@ -736,6 +740,11 @@ mod tests { assert_eq!(out.len(), 4 * 2048); let rms = (out[2048..].iter().map(|s| s * s).sum::() / (out.len() - 2048) as f32).sqrt(); assert!(rms > 1e-4, "{rms}"); + // a new layout drops the SBR decoders (their history is the old element order's) and + // keeps SBR on + dec.set_layout(vec![(ElementType::Sce, 0)]); + assert_eq!(dec.sbr_out.as_ref().map(Vec::len), Some(0)); + assert_eq!(dec.decode(&au).unwrap()[0].len(), 2048); } /// An access unit whose elements are all empty: `elements` are (id, tag), then ID_END. diff --git a/crates/audio/src/aac/sbr/bitstream.rs b/crates/audio/src/aac/sbr/bitstream.rs index 6233504fa..1d343bf7b 100644 --- a/crates/audio/src/aac/sbr/bitstream.rs +++ b/crates/audio/src/aac/sbr/bitstream.rs @@ -204,8 +204,9 @@ impl Grid { if t[0] < 0 || t[..=n].windows(2).any(|w| w[0] >= w[1]) { return Err(bad()); } + // l_A (ISO/IEC 14496-3 §4.6.18.3.3): VARFIX pointer 1, like 0, marks no envelope let la = match (pointer, class) { - (0, _) => None, + (0, _) | (1, VARFIX) => None, (p, FIXVAR | VARVAR) => Some((n + 1).checked_sub(p).ok_or_else(bad)?), (p, _) => Some(p - 1), }; @@ -468,6 +469,9 @@ mod tests { // VARFIX: leading border 1, relative 2, pointer 2: l_A = 1 let g = grid(&[(VARFIX as u32, 2), (1, 2), (1, 2), (0, 2), (2, 2), (1, 1), (0, 1)]).unwrap(); assert_eq!((g.num_env, &g.t_env[..3], g.la, g.t_noise), (2, &[1, 3, 16][..], Some(1), [1, 3, 16])); + // the same with pointer 1: no transient envelope, the noise floors still split at 3 + let g = grid(&[(VARFIX as u32, 2), (1, 2), (1, 2), (0, 2), (1, 2), (1, 1), (0, 1)]).unwrap(); + assert_eq!((g.la, g.t_noise), (None, [1, 3, 16])); // VARVAR: 1 .. 17 with one border from each end let g = grid(&[(VARVAR as u32, 2), (1, 2), (1, 2), (1, 2), (1, 2), (0, 2), (0, 2), (0, 2), (0, 1), (0, 1), (0, 1)]).unwrap(); assert_eq!((g.num_env, &g.t_env[..4], g.la), (3, &[1, 3, 15, 17][..], None)); diff --git a/crates/audio/src/aac/sbr/freq.rs b/crates/audio/src/aac/sbr/freq.rs index dc2809c10..d12fbdc02 100644 --- a/crates/audio/src/aac/sbr/freq.rs +++ b/crates/audio/src/aac/sbr/freq.rs @@ -75,8 +75,9 @@ fn start_stop(h: &Header, fs: u32) -> Option<(usize, usize)> { } } .min(64); - // the low band reaches only the analysis bank's 32 channels - (1..32).contains(&k0).then_some(())?; + // the low band reaches only the analysis bank's 32 channels (`kx` is checked against them + // once the crossover is known) + (1..=32).contains(&k0).then_some(())?; (k2 > k0).then_some((k0 as usize, k2 as usize)) } @@ -341,6 +342,14 @@ mod tests { assert!(valid > 10_000, "{valid}"); } + /// The highest start a header can give, 32 at 24 kHz (start 15), is valid: with stop 14 + /// (twice the start) and linear bands it covers channels 32 to 64. + #[test] + fn a_start_at_channel_32_is_accepted() { + let t = FreqTables::new(&header(15, 14, 0, 0, false, 2), 24_000).unwrap(); + assert_eq!((t.k0, t.kx, t.m), (32, 32, 32)); + } + /// A typical 44.1 kHz header: start channel 12 + offset, stop near 64, three noise bands. #[test] fn a_44k_header_has_the_expected_layout() { diff --git a/crates/audio/src/aac/sbr/hf.rs b/crates/audio/src/aac/sbr/hf.rs index 43ae572cf..1c2fd81ac 100644 --- a/crates/audio/src/aac/sbr/hf.rs +++ b/crates/audio/src/aac/sbr/hf.rs @@ -201,13 +201,14 @@ fn envelope_gains(f: &Frame, l: usize, sine_index: &[bool; 64], delta: bool) -> } let mut out = EnvelopeGains { gain: [0.0; 64], noise: [0.0; 64], sine: [0.0; 64] }; for c in 0..m { + // `q / (1 + q)` first: energy and noise ratio are each clamped, their product is not let (eo, ec, q) = (e_orig[c], e_curr[c], q[c]); - out.noise[c] = (eo * q / (1.0 + q)).sqrt(); + let share = q / (1.0 + q); + out.noise[c] = (eo * share).sqrt(); if sine_index[c] { out.sine[c] = (eo / (1.0 + q)).sqrt(); } - out.gain[c] = - if s_band[c] { (eo / (EPS + ec) * q / (1.0 + q)).sqrt() } else { (eo / ((EPS + ec) * (1.0 + if delta { q } else { 0.0 }))).sqrt() }; + out.gain[c] = if s_band[c] { (eo / (EPS + ec) * share).sqrt() } else { (eo / ((EPS + ec) * (1.0 + if delta { q } else { 0.0 }))).sqrt() }; } // limit per limiter band, then boost back the energy the limiter took let lim_gain = LIMITER_GAINS[usize::from(hdr.limiter_gains & 3)]; diff --git a/crates/audio/src/aac/sbr/mod.rs b/crates/audio/src/aac/sbr/mod.rs index 32a6c55b0..752cd03a0 100644 --- a/crates/audio/src/aac/sbr/mod.rs +++ b/crates/audio/src/aac/sbr/mod.rs @@ -285,6 +285,8 @@ impl Element { } if br.read_flag().ok()? { let h = Header::read(&mut br).ok()?; + // a header that runs past its fill element is damaged: keep the one in use + (br.position() <= p.end).then_some(())?; if self.header != Some(h) { if self.header.is_none_or(|o| !o.same_tables(&h)) { for c in &mut self.channels { @@ -377,13 +379,13 @@ pub(crate) const TEST_HEADER: Header = Header { /// An SBR single channel element for tests at output rate `rate`: [`TEST_HEADER`] (3 dB steps, /// start 5, stop 9, the default extra fields), then one FIXFIX envelope at high resolution with -/// band `k` at `env(k)` (1.5 dB steps, as a single FIXFIX envelope has) and every noise floor -/// value `noise`. +/// band `k` at `env(k)` (1.5 dB steps, as a single FIXFIX envelope has) and noise band `k` at +/// `noise(k)`. #[cfg(test)] -pub(crate) fn test_payload(rate: u32, env: impl Fn(usize) -> i32, noise: u32) -> Option> { +pub(crate) fn test_payload(rate: u32, env: impl Fn(usize) -> i32, noise: impl Fn(usize) -> i32) -> Option> { use craft_media::bitstream::BitWriter; let t = FreqTables::new(&TEST_HEADER, rate)?; - let (env, n_q): (Vec, usize) = ((0..t.n_high()).map(env).collect(), t.n_q()); + let (env, noise): (Vec, Vec) = ((0..t.n_high()).map(env).collect(), (0..t.n_q()).map(noise).collect()); let mut bw = BitWriter::new(); bw.write_bits(1, 1); // bs_header_flag for (v, n) in [(1, 1), (5, 4), (9, 4), (0, 3), (0, 2), (0, 1), (0, 1)] { @@ -394,7 +396,7 @@ pub(crate) fn test_payload(rate: u32, env: impl Fn(usize) -> i32, noise: u32) -> bw.write_bits(0, 2); // one envelope bw.write_bits(1, 1); // high resolution bw.write_bits(0, 2); // frequency-direction envelope and noise floor - for _ in 0..n_q { + for _ in &noise { bw.write_bits(0, 2); // no inverse filtering } bw.write_bits(env[0] as u32, 7); @@ -402,9 +404,9 @@ pub(crate) fn test_payload(rate: u32, env: impl Fn(usize) -> i32, noise: u32) -> let (code, len) = tables::F_ENV_1_5DB[(w[1] - w[0] + 60) as usize]; bw.write_bits(code, u32::from(len)); } - bw.write_bits(noise, 5); - for _ in 1..n_q { - let (code, len) = tables::F_ENV_3_0DB[31]; + bw.write_bits(noise[0] as u32, 5); + for w in noise.windows(2) { + let (code, len) = tables::F_ENV_3_0DB[(w[1] - w[0] + 31) as usize]; bw.write_bits(code, u32::from(len)); } bw.write_bits(0, 2); // no sinusoids, no extended data @@ -460,7 +462,7 @@ mod tests { fn the_high_band_follows_the_envelope() { let t = tables(); let mid = t.n_high() / 2; - let data = test_payload(RATE, |k| if k < mid { 30 } else { 42 }, 0).unwrap(); + let data = test_payload(RATE, |k| if k < mid { 30 } else { 42 }, |_| 0).unwrap(); let mut e = Element::new(1, RATE); let out = run(&mut e, &data, 6); let tail = &out[out.len() - 4096..]; @@ -481,7 +483,7 @@ mod tests { /// finite and of full length. #[test] fn damaged_sbr_data_is_survived() { - let good = test_payload(RATE, |k| 20 + k as i32, 3).unwrap(); + let good = test_payload(RATE, |k| 20 + k as i32, |_| 3).unwrap(); let mut seed = 0x1234_5678u32; let mut next = || { seed ^= seed << 13; @@ -510,4 +512,33 @@ mod tests { assert!(out.iter().all(|c| c.len() == 2048 && c.iter().all(|s| s.is_finite()))); } } + + /// The largest envelope (127 steps of 1.5 dB) with the largest noise floor (each band 31 + /// steps below the last) keeps the output finite: the gains are formed so that no + /// intermediate product of two clamped levels overflows. + #[test] + fn extreme_levels_stay_finite() { + let rate = + [22_050, 24_000, 32_000, 44_100, 48_000].into_iter().find(|&r| FreqTables::new(&TEST_HEADER, r).is_some_and(|t| t.n_q() >= 3)).unwrap(); + let data = test_payload(rate, |_| 127, |k| -31 * k as i32).unwrap(); + let mut e = Element::new(1, rate); + run(&mut e, &data, 3); + } + + /// A header that runs past the end of its fill element is not taken: the one in use stays, + /// and the frame is dropped. + #[test] + fn a_header_cut_short_is_not_taken() { + let data = test_payload(RATE, |_| 30, |_| 0).unwrap(); + let mut e = Element::new(1, RATE); + run(&mut e, &data, 1); + assert_eq!(e.header, Some(TEST_HEADER)); + // another start frequency (bits 2..6), in an element that ends 6 bits in + let mut cut = data.clone(); + cut[0] ^= 0x04; + assert!(e.read(Payload { data: &cut, start: 0, end: 6, crc: false }).is_none()); + assert_eq!(e.header, Some(TEST_HEADER)); + assert!(e.read(Payload { data: &cut, start: 0, end: 8 * cut.len(), crc: false }).is_some()); + assert_ne!(e.header, Some(TEST_HEADER)); + } } diff --git a/crates/audio/tests/codecs/aac/he_aac_oracle.rs b/crates/audio/tests/codecs/aac/he_aac_oracle.rs index e41493e16..0eeba69c8 100644 --- a/crates/audio/tests/codecs/aac/he_aac_oracle.rs +++ b/crates/audio/tests/codecs/aac/he_aac_oracle.rs @@ -149,8 +149,12 @@ fn matches_ffmpeg_he_aac_decoder() { } } } - let mean = diffs.iter().sum::() / diffs.len().max(1) as f64; - let mad = diffs.iter().map(|d| d.abs()).sum::() / diffs.len().max(1) as f64; + // the LFE (WAV channel 3 of 5.1) has no SBR, so no high band; every other channel must + // have one to compare + let lfe = case.channels == 6 && c == 3; + assert!(lfe || !diffs.is_empty(), "{} ch{c}: no high band to compare", case.name); + let mean = diffs.iter().sum::() / diffs.len() as f64; + let mad = diffs.iter().map(|d| d.abs()).sum::() / diffs.len() as f64; println!( "{} ch{c}: full SNR {full:.1} dB, low band SNR {low:.1} dB, high band level {mean:+.2} dB (mean |Δ| {mad:.2} dB over {})", case.name, @@ -159,7 +163,7 @@ fn matches_ffmpeg_he_aac_decoder() { // The low band differs by the core decoders' rounding; the high band's noise comes // from the same table, so whole frames match too. assert!(full > 45.0 && low > 55.0, "{} ch{c}: SNR {full:.1} / {low:.1} dB", case.name); - assert!(mad < 0.1, "{} ch{c}: high band off by {mad:.2} dB", case.name); + assert!(lfe || mad < 0.1, "{} ch{c}: high band off by {mad:.2} dB", case.name); } } } From 48f5283ec899ec7eab328bc55acc9e7e50b0ca71 Mon Sep 17 00:00:00 2001 From: alehel Date: Thu, 12 Nov 2026 20:28:47 +0100 Subject: [PATCH 072/520] craft-audio: cite no section number for l_A Co-Authored-By: Claude Opus 5.5 --- crates/audio/src/aac/sbr/bitstream.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/audio/src/aac/sbr/bitstream.rs b/crates/audio/src/aac/sbr/bitstream.rs index 1d343bf7b..0e8e70c3a 100644 --- a/crates/audio/src/aac/sbr/bitstream.rs +++ b/crates/audio/src/aac/sbr/bitstream.rs @@ -204,7 +204,7 @@ impl Grid { if t[0] < 0 || t[..=n].windows(2).any(|w| w[0] >= w[1]) { return Err(bad()); } - // l_A (ISO/IEC 14496-3 §4.6.18.3.3): VARFIX pointer 1, like 0, marks no envelope + // l_A: in ISO/IEC 14496-3 a VARFIX pointer of 1, like 0, marks no transient envelope let la = match (pointer, class) { (0, _) | (1, VARFIX) => None, (p, FIXVAR | VARVAR) => Some((n + 1).checked_sub(p).ok_or_else(bad)?), From fe8c1f697a0fc04c8eabbffb526b5c9d51bfb9c6 Mon Sep 17 00:00:00 2001 From: alehel Date: Thu, 12 Nov 2026 20:45:15 +0100 Subject: [PATCH 073/520] craft-audio: keep the SBR history when a new program config fails The SBR decoders now record the layout they were built for and are rebuilt when the decoder's layout differs, instead of being cleared in set_layout. An access unit whose new program config fails puts the layout back, and with it the SBR decoders still match. Refs #1121 Co-Authored-By: Claude Opus 5.5 --- crates/audio/src/aac/decoder.rs | 97 ++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/crates/audio/src/aac/decoder.rs b/crates/audio/src/aac/decoder.rs index ec5dae9fc..825ebb23a 100644 --- a/crates/audio/src/aac/decoder.rs +++ b/crates/audio/src/aac/decoder.rs @@ -85,6 +85,9 @@ pub struct Decoder { sbr_seen: bool, /// The SBR decoder of each layout entry, when output is at twice the core rate. sbr_out: Option>, + /// The layout `sbr_out` was built for. Kept apart from `layout`, so that an access unit whose + /// new program config fails (and whose layout is put back) leaves the SBR history alone. + sbr_layout: Vec<(ElementType, u8)>, } impl Decoder { @@ -121,6 +124,7 @@ impl Decoder { scratch: Scratch::new(), sbr_seen: false, sbr_out: None, + sbr_layout: Vec::new(), }; d.set_layout(layout); Ok(d) @@ -135,10 +139,6 @@ impl Decoder { } self.layout = layout; self.offsets = offsets; - // the SBR decoders' history belongs to the old element order: `apply_sbr` rebuilds them - if let Some(e) = self.sbr_out.as_mut() { - e.clear(); - } self.channels = vec![ChannelState { overlap: vec![0.0; 1024], prev_shape: WindowShape::Sine }; n]; } @@ -214,10 +214,11 @@ impl Decoder { let Some(elements) = self.sbr_out.as_mut() else { return pcm; }; - // a new layout (a program config element) starts the SBR decoders afresh - let fits = elements.len() == self.layout.len() && elements.iter().zip(&self.layout).all(|(e, l)| e.channels() == l.0.channels()); - if !fits { + // a new layout (a program config element) starts the SBR decoders afresh: their history + // belongs to the old element order, even where the channel counts match + if elements.len() != self.layout.len() || self.sbr_layout != self.layout { *elements = self.layout.iter().map(|l| sbr::Element::new(l.0.channels(), rate)).collect(); + self.sbr_layout.clone_from(&self.layout); } let mut out = Vec::with_capacity(pcm.len()); for (i, e) in elements.iter_mut().enumerate() { @@ -704,26 +705,7 @@ mod tests { /// its SBR fill element describes is there; with SBR off, the core alone (silence). #[test] fn sbr_fill_elements_rebuild_the_high_band() { - let payload = sbr::test_payload(44_100, |_| 40, |_| 0).unwrap(); - let mut bw = BitWriter::new(); - bw.write_bits(ID_SCE, 3); - bw.write_bits(0, 4); - bw.write_bits(0, 8 + 11 + 3); // global_gain, ics_info (max_sfb 0), no pulse/TNS/gain control - bw.write_bits(ID_FIL, 3); - let count = (4 + 8 * payload.len()).div_ceil(8); - if count < 15 { - bw.write_bits(count as u32, 4); - } else { - bw.write_bits(15, 4); - bw.write_bits((count - 14) as u32, 8); - } - bw.write_bits(EXT_SBR_DATA, 4); - for &b in &payload { - bw.write_bits(u32::from(b), 8); - } - bw.write_bits(0, 4); - bw.write_bits(ID_END, 3); - let au = bw.finish(); + let au = sbr_au(0); // AAC-LC, 22.05 kHz, mono let asc = AudioSpecificConfig::lc(7, 1); let mut dec = Decoder::from_config(asc.clone()).unwrap(); @@ -740,11 +722,62 @@ mod tests { assert_eq!(out.len(), 4 * 2048); let rms = (out[2048..].iter().map(|s| s * s).sum::() / (out.len() - 2048) as f32).sqrt(); assert!(rms > 1e-4, "{rms}"); - // a new layout drops the SBR decoders (their history is the old element order's) and - // keeps SBR on - dec.set_layout(vec![(ElementType::Sce, 0)]); - assert_eq!(dec.sbr_out.as_ref().map(Vec::len), Some(0)); - assert_eq!(dec.decode(&au).unwrap()[0].len(), 2048); + } + + /// A program config that changes the layout restarts the SBR decoders, even where the + /// channel counts match, and one in an access unit that fails leaves their history alone: the + /// access units after it decode as if it had not been there. + #[test] + fn a_new_layout_restarts_sbr_and_a_failed_one_keeps_it() { + let au = sbr_au(0); + let one = ProgramConfig { front: vec![(false, 0)], ..Default::default() }; + let six = ProgramConfig { front: vec![(false, 0), (true, 0)], back: vec![(true, 1)], lfe: vec![0], ..Default::default() }; + let decoder = || { + let mut d = Decoder::from_config(AudioSpecificConfig { pce: Some(one.clone()), ..AudioSpecificConfig::lc(7, 0) }).unwrap(); + d.set_sbr(true); + d + }; + let (mut kept, mut failed) = (decoder(), decoder()); + for _ in 0..3 { + assert_eq!(kept.decode(&au).unwrap(), failed.decode(&au).unwrap()); + } + // the new layout's elements are missing from this access unit + assert!(failed.decode(&silent_au(Some(&six), &[(ID_SCE, 0)])).is_err()); + for _ in 0..2 { + assert_eq!(kept.decode(&au).unwrap(), failed.decode(&au).unwrap()); + } + // the same channel count under another tag is a new layout: the SBR decoder starts over, + // as in a decoder that had that layout from the start + let tag1 = ProgramConfig { front: vec![(false, 1)], ..Default::default() }; + let mut fresh = Decoder::from_config(AudioSpecificConfig { pce: Some(tag1.clone()), ..AudioSpecificConfig::lc(7, 0) }).unwrap(); + fresh.set_sbr(true); + kept.set_layout(tag1.layout()); + assert_eq!(kept.decode(&sbr_au(1)).unwrap(), fresh.decode(&sbr_au(1)).unwrap()); + } + + /// One access unit of SCE `tag` with an SBR fill element of [`sbr::test_payload`] (44.1 kHz + /// output). + fn sbr_au(tag: u32) -> Vec { + let payload = sbr::test_payload(44_100, |_| 40, |_| 0).unwrap(); + let mut bw = BitWriter::new(); + bw.write_bits(ID_SCE, 3); + bw.write_bits(tag, 4); + bw.write_bits(0, 8 + 11 + 3); // global_gain, ics_info (max_sfb 0), no pulse/TNS/gain control + bw.write_bits(ID_FIL, 3); + let count = (4 + 8 * payload.len()).div_ceil(8); + if count < 15 { + bw.write_bits(count as u32, 4); + } else { + bw.write_bits(15, 4); + bw.write_bits((count - 14) as u32, 8); + } + bw.write_bits(EXT_SBR_DATA, 4); + for &b in &payload { + bw.write_bits(u32::from(b), 8); + } + bw.write_bits(0, 4); + bw.write_bits(ID_END, 3); + bw.finish() } /// An access unit whose elements are all empty: `elements` are (id, tag), then ID_END. From 7af2f8e868e7e51c70d26fbe4459bb67076a8789 Mon Sep 17 00:00:00 2001 From: alehel Date: Fri, 13 Nov 2026 07:34:27 +0100 Subject: [PATCH 074/520] craft-media: AVI muxer (AviWriter) Write AVI files: hdrl with avih and one strl (strh, strf, strn, indx) per stream, odml/dmlh, an interleaved movi and idx1. Past the RIFF limit (default 1 GiB) the file continues in OpenDML RIFF AVIX extensions with ix## standard indexes and an indx super index per stream. - VideoSpec::for_codec builds the stream from CodecParams; MPEG-4 Part 2 is tagged FMP4. - Audio is timed by bytes (PCM-style) or one packet per chunk. - Bad options and chunks are errors; finish() closes a readable file after a failed write. Tests read the files back through this crate's demuxer by every index, and through ffmpeg (decode identical to the sources, seeks through the index, ffprobe fields), in one RIFF and across AVIX extensions. The MPEG-4 encoder tests play its packets from an AVI file. --- crates/media/Cargo.toml | 6 +- crates/media/README.md | 53 +- crates/media/src/avi/mod.rs | 9 +- crates/media/src/avi/mux.rs | 617 ++++++++++++++++++++++++ crates/media/src/avi/mux_tests.rs | 205 ++++++++ crates/media/tests/oracle_avi_mux.rs | 156 ++++++ crates/video/Cargo.toml | 2 + crates/video/tests/mpeg4_encode/main.rs | 34 ++ 8 files changed, 1071 insertions(+), 11 deletions(-) create mode 100644 crates/media/src/avi/mux.rs create mode 100644 crates/media/src/avi/mux_tests.rs create mode 100644 crates/media/tests/oracle_avi_mux.rs diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml index cdbf28349..4943b1771 100644 --- a/crates/media/Cargo.toml +++ b/crates/media/Cargo.toml @@ -18,7 +18,7 @@ default = ["isobmff"] bitstream = [] isobmff = ["bitstream", "dep:thiserror"] matroska = [] -# AVI (RIFF) and OpenDML demuxer (`craft_media::avi`). +# AVI (RIFF) and OpenDML demuxer and muxer (`craft_media::avi`). avi = [] # MXF demuxer and writer (`craft_media::mxf`); the writer formats dates with chrono. mxf = ["dep:chrono"] @@ -51,6 +51,10 @@ required-features = ["matroska"] name = "oracle_avi" required-features = ["avi"] +[[test]] +name = "oracle_avi_mux" +required-features = ["avi"] + [[test]] name = "oracle_demux" required-features = ["isobmff"] diff --git a/crates/media/README.md b/crates/media/README.md index 387ab8cdd..8eca6ecb5 100644 --- a/crates/media/README.md +++ b/crates/media/README.md @@ -1,8 +1,8 @@ # craft-media Media containers and bit-level I/O for the Craft apps: an MSB-first bit reader and writer, -clean-room ISO BMFF (MP4), QuickTime (MOV) and Matroska (MKV, WebM) and MXF demuxers and muxers, and -Ogg, AVI and MPEG-2 Systems (transport and program stream) demuxers, written from the public specifications. No `unsafe`, no panics on malformed +clean-room ISO BMFF (MP4), QuickTime (MOV), Matroska (MKV, WebM), MXF and AVI demuxers and muxers, and +Ogg and MPEG-2 Systems (transport and program stream) demuxers, written from the public specifications. No `unsafe`, no panics on malformed input, builds for `wasm32-unknown-unknown`. Dependencies: `thiserror` (for `isobmff`), `chrono` (for `mxf`), `serde` (optional). @@ -44,8 +44,7 @@ craft-media = { git = "https://github.com/storytold/craft-libs", rev = " - In scope: bit-level reading and writing, and container demuxing and muxing (boxes, sample tables, sample entries and codec configuration records) for audio and video tracks. - Out of scope: codecs. Audio codecs are in `craft-audio`; video codecs (H.264, HEVC, ProRes, - DNx, VP8, VP9, AV1) are in `craft-video`. Containers only FilmCraft reads (MXF, - MPEG-TS/PS, AVI) are not here yet. + DNx, VP8, VP9, AV1, MPEG-4 Part 2) are in `craft-video`. - Anything both the audio and the video codec crates need belongs here, so the two never depend on each other. @@ -448,10 +447,10 @@ No muxer. ## `avi` -A clean-room AVI demuxer from FilmCraft's `filmcraft-avi` (storytold/filmcraft@4bec22b), written -from Microsoft's published AVI RIFF file reference and the OpenDML AVI File Format Extensions 1.02. -No FFmpeg, libavformat or other AVI implementation was read; ffmpeg and ffprobe are test oracles -only. No muxer. +A clean-room AVI demuxer from FilmCraft's `filmcraft-avi` (storytold/filmcraft@4bec22b), and a +muxer, both written from Microsoft's published AVI RIFF file reference and the OpenDML AVI File +Format Extensions 1.02. No FFmpeg, libavformat or other AVI implementation was read; ffmpeg and +ffprobe are test oracles only. ```rust use craft_media::avi::{open, StreamKind}; @@ -496,6 +495,44 @@ Tests: hand-built files for every index path, truncation, every single-byte muta multi-byte damage (never a panic). `tests/oracle_avi.rs` checks ffmpeg-made files (Motion JPEG, H.264 with B-frames, BGR24, YUY2; PCM and MP3) against ffprobe; the ignored `opendml_file_past_1_gb` checks a 1.16 GB OpenDML file with `AVIX` extensions. + +### Muxer + +```rust +use craft_media::avi::{AudioSpec, AviWriter, MuxOptions, StreamSpec, VideoSpec}; + +let video = VideoSpec::new(*b"XVID", 640, 360, 25, 1); // or VideoSpec::for_codec(&encoder.codec_params(), 25, 1) +let audio = AudioSpec::pcm(48_000, 2, 16); +let mut w = AviWriter::new(file, vec![StreamSpec::Video(video), StreamSpec::Audio(audio)], MuxOptions::default())?; +w.write_chunk(0, &frame, keyframe)?; // chunks go in the order written: the caller interleaves +w.write_chunk(1, &pcm, true)?; +let file = w.finish()?; // Write + Seek: sizes and the header list are rewritten at the end +``` + +- Layout: `RIFF AVI ` with `hdrl` (`avih`; per stream `strh`, `strf`, `strn` and an OpenDML + `indx`; `odml`/`dmlh`), `LIST movi`, `idx1`. Chunks are `NNdc` (video) and `NNwb` (audio) with + decimal stream numbers, padded to even sizes. Empty video chunks (repeat the previous picture) + are allowed. +- Large files: when a RIFF would pass `MuxOptions::riff_limit` (1 GiB by default, 16 KiB to + 2 GiB) the file continues in a `RIFF AVIX`. Every RIFF ends its `movi` with an `ix##` standard + index per stream, listed in that stream's `indx`; `idx1` covers the first RIFF only, and + `avih` counts the first RIFF's frames while `dmlh` and `strh` count all of them, as OpenDML + asks. The `indx` has room for `MuxOptions::max_riffs` (256) RIFFs, reserved in the header. +- Streams: video with any `BITMAPINFOHEADER` (`VideoSpec::for_codec` picks the FourCC of an + encoder's `CodecParams`: `FMP4`, `H264`, `HEVC`, `MJPG`, `MPG2`, `VP80`, `VP90`, `AV01`; `FMP4` and + not `XVID` for MPEG-4 Part 2, which decoders take to name the encoder), and audio + with any `WAVEFORMATEX`, timed by its bytes (`AudioTiming::Bytes`: PCM and other fixed-size + sample frames) or a packet a chunk (`AudioTiming::Packets`: MP3, AAC, AC-3). Up to 100 streams. +- Errors, not panics: no streams, a zero rate, a chunk that does not fit in a RIFF, more RIFFs + than `max_riffs` (what was written still closes into a readable file). + +Tests: `mux_tests.rs` reads files back through the demuxer on every index path (OpenDML, `idx1` +with the `AVIX` extensions scanned, a scan), with odd and empty chunks, several `AVIX` +extensions, a file starting inside its output, packet-timed audio and the error cases. +`tests/oracle_avi_mux.rs` muxes an ffmpeg-made MPEG-4 Part 2 stream and PCM, in one RIFF and +across `AVIX` extensions: ffmpeg decodes the AVI to exactly the pictures and samples of the +sources, seeking through the index lands on the right picture, and ffprobe reads the codec, +FourCC, size, frame rate, packet count, audio format and duration. ## `mpegts` A clean-room MPEG-2 Systems demuxer from FilmCraft's `filmcraft-mpegts` diff --git a/crates/media/src/avi/mod.rs b/crates/media/src/avi/mod.rs index 592139cef..eaaa1bbe2 100644 --- a/crates/media/src/avi/mod.rs +++ b/crates/media/src/avi/mod.rs @@ -1,5 +1,5 @@ -//! Clean-room AVI demuxer, written from Microsoft's published AVI RIFF file reference and the -//! OpenDML AVI File Format Extensions 1.02. +//! Clean-room AVI demuxer and muxer ([`AviWriter`]), written from Microsoft's published AVI RIFF +//! file reference and the OpenDML AVI File Format Extensions 1.02. //! //! [`open`] reads the `hdrl` header list (`avih`, and per stream `strh` / `strf` / `strn` / //! `indx`) of the first `RIFF AVI ` and builds a per-stream table of [`Chunk`]s from the best index @@ -18,9 +18,14 @@ #![warn(missing_docs)] +mod mux; +#[cfg(test)] +mod mux_tests; #[cfg(test)] mod tests; +pub use mux::{AudioSpec, AudioTiming, AviWriter, MuxOptions, StreamSpec, VideoSpec}; + pub use crate::source::{ByteSource, ReadSeekSource}; use std::collections::HashSet; diff --git a/crates/media/src/avi/mux.rs b/crates/media/src/avi/mux.rs new file mode 100644 index 000000000..a6e03e174 --- /dev/null +++ b/crates/media/src/avi/mux.rs @@ -0,0 +1,617 @@ +//! Clean-room AVI muxer, written from Microsoft's published AVI RIFF file reference and the +//! OpenDML AVI File Format Extensions 1.02. +//! +//! [`AviWriter`] writes one `RIFF AVI ` with the `hdrl` header list, a `movi` list of chunks in +//! the order they are given, and the AVI 1.0 `idx1` index. A file that grows past +//! [`MuxOptions::riff_limit`] (1 GiB) continues in `RIFF AVIX` extensions. Every RIFF ends its +//! `movi` list with an OpenDML standard index (`ix##`) per stream, and each stream's `strl` +//! holds a super index (`indx`) of them, so readers find the chunks past the first RIFF; `idx1` +//! covers the first RIFF only, as OpenDML asks. +//! +//! The header list is written at the start with room reserved for the super indexes and +//! rewritten with the final counts by [`AviWriter::finish`]: the output must be `Write + Seek`. + +use std::io::{Seek, SeekFrom, Write}; + +use super::{BitmapInfo, Error, Result, WaveFormat}; +use crate::{CodecId, CodecParams}; + +/// `AVIF_HASINDEX`: the file has an `idx1`. +const AVIF_HASINDEX: u32 = 0x10; +/// `AVIF_ISINTERLEAVED`: the chunks of the streams are interleaved. +const AVIF_ISINTERLEAVED: u32 = 0x100; +/// `AVIIF_KEYFRAME` in an `idx1` entry. +const AVIIF_KEYFRAME: u32 = 0x10; +/// Bit 31 of a standard index entry's size: not a key frame. +const NOT_KEY: u32 = 0x8000_0000; +/// `dmlh` is 248 bytes: `dwTotalFrames` and reserved space. +const DMLH_SIZE: usize = 248; +/// Chunk ids carry a two-digit decimal stream number. +const MAX_STREAMS: usize = 100; +/// The smallest RIFF size the writer accepts. +const MIN_RIFF_LIMIT: u64 = 16 << 10; +/// The largest: RIFF and chunk sizes are 32-bit, and OpenDML entries use bit 31 as a flag. +const MAX_RIFF_LIMIT: u64 = 1 << 31; + +/// How the chunks of an audio stream measure time (`strh` `dwScale`, `dwRate`, `dwSampleSize`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AudioTiming { + /// Chunks are runs of fixed-size sample frames of `block_align` bytes (PCM, A-law, constant + /// bitrate MP3 written as a byte stream): the stream is timed by its bytes. + Bytes, + /// Each chunk is one packet of `samples_per_packet` sample frames (1152 for MPEG audio Layer + /// II/III at 32 kHz and above, 1024 for AAC, 1536 for AC-3). + Packets { + /// Sample frames in a packet. + samples_per_packet: u32, + }, +} + +/// A video stream for [`AviWriter`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct VideoSpec { + /// The `BITMAPINFOHEADER` written as `strf`. `compression` is the FourCC (`XVID`, `MJPG`, + /// `H264`…), and `extra` follows the 40-byte header. + pub format: BitmapInfo, + /// `fccHandler`; the compression FourCC when `None`. + pub handler: Option<[u8; 4]>, + /// Frames per second is `rate / scale`. + pub rate: u32, + /// See `rate`. + pub scale: u32, + /// `strn`. + pub name: Option, +} + +impl VideoSpec { + /// A compressed stream of `fourcc` pictures, 24 bits per pixel in the header, at `rate / scale` + /// frames per second. + pub fn new(fourcc: [u8; 4], width: u32, height: u32, rate: u32, scale: u32) -> VideoSpec { + VideoSpec { + format: BitmapInfo { + width: i32::try_from(width).unwrap_or(i32::MAX), + height: i32::try_from(height).unwrap_or(i32::MAX), + bit_count: 24, + compression: fourcc, + size_image: width.saturating_mul(height).saturating_mul(3), + extra: Vec::new(), + }, + handler: None, + rate, + scale, + name: None, + } + } + + /// A stream for an encoder's track description: the usual AVI FourCC of its codec (`FMP4` for + /// MPEG-4 Part 2, `H264`, `HEVC`, `MJPG`, `MPG2`, `VP80`, `VP90`, `AV01`), its picture size + /// and its extradata after the header. MPEG-4 Part 2 gets `FMP4` rather than `XVID` or `DIVX` + /// because decoders read those as naming the encoder: ffmpeg, for one, decodes an `XVID` + /// stream with Xvid's own IDCT. Set `format.compression` for a player that wants another. `None` for a codec without a FourCC here or a + /// description without a size. H.264 and HEVC chunks must then be Annex B with an Annex B or + /// empty extradata, as AVI readers expect. + pub fn for_codec(params: &CodecParams, rate: u32, scale: u32) -> Option { + let fourcc = match params.codec { + CodecId::Mpeg4Video => *b"FMP4", + CodecId::H264 => *b"H264", + CodecId::Hevc => *b"HEVC", + CodecId::Mjpeg => *b"MJPG", + CodecId::Mpeg2Video => *b"MPG2", + CodecId::Vp8 => *b"VP80", + CodecId::Vp9 => *b"VP90", + CodecId::Av1 => *b"AV01", + _ => return None, + }; + let mut v = VideoSpec::new(fourcc, params.width?, params.height?, rate, scale); + v.format.extra = params.extradata.clone(); + Some(v) + } +} + +/// An audio stream for [`AviWriter`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AudioSpec { + /// The `WAVEFORMATEX` written as `strf`. + pub format: WaveFormat, + /// How its chunks measure time. + pub timing: AudioTiming, + /// `strn`. + pub name: Option, +} + +impl AudioSpec { + /// Interleaved little-endian integer PCM (`bits` 8, 16, 24 or 32), timed by its bytes. + pub fn pcm(sample_rate: u32, channels: u16, bits: u16) -> AudioSpec { + let block_align = channels.saturating_mul(bits.div_ceil(8)); + AudioSpec { + format: WaveFormat { + format_tag: 1, + channels, + sample_rate, + avg_bytes_per_sec: sample_rate.saturating_mul(u32::from(block_align)), + block_align, + bits_per_sample: bits, + extra: Vec::new(), + }, + timing: AudioTiming::Bytes, + name: None, + } + } +} + +/// One stream of an AVI file. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum StreamSpec { + /// `vids`. + Video(VideoSpec), + /// `auds`. + Audio(AudioSpec), +} + +/// Options for [`AviWriter`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct MuxOptions { + /// Largest size of one RIFF (the first, then each `AVIX` extension), 16 KiB to 2 GiB. + /// 1 GiB by default, which OpenDML recommends for the first RIFF so AVI 1.0 readers can use it. + pub riff_limit: u64, + /// RIFFs the super indexes have room for (each takes 16 bytes a stream in the header). 256 by + /// default: 256 GiB at the default `riff_limit`. + pub max_riffs: u32, +} + +impl Default for MuxOptions { + fn default() -> Self { + MuxOptions { riff_limit: 1 << 30, max_riffs: 256 } + } +} + +/// A chunk's entry in the standard index of its RIFF. +#[derive(Clone, Copy, Debug)] +struct Entry { + /// Offset of the data from the `movi` list's type id (the index's base). + offset: u32, + size: u32, + key: bool, +} + +/// One `ix##` written: its position, its size and the stream time it covers. +#[derive(Clone, Copy, Debug)] +struct SuperEntry { + at: u64, + size: u32, + duration: u32, +} + +/// The RIFF being written. +#[derive(Clone, Copy, Debug)] +struct Riff { + /// Position of `RIFF`. + start: u64, + /// Position of the `movi` list's `LIST`. + movi: u64, +} + +#[derive(Debug)] +struct StreamState { + spec: StreamSpec, + /// `00dc`, `01wb`… + id: [u8; 4], + chunks: u64, + bytes: u64, + largest: u32, + /// The chunks of the current RIFF. + entries: Vec, + /// Stream time of the current RIFF's chunks. + duration: u64, + supers: Vec, +} + +impl StreamState { + /// Stream time (`dwScale / dwRate` units) of a chunk of `len` bytes. + fn units(&self, len: u64) -> u64 { + match &self.spec { + StreamSpec::Audio(a) if a.timing == AudioTiming::Bytes => len / u64::from(a.format.block_align.max(1)), + _ => 1, + } + } + + /// `(dwScale, dwRate, dwSampleSize)`. + fn time_base(&self) -> (u32, u32, u32) { + match &self.spec { + StreamSpec::Video(v) => (v.scale, v.rate, 0), + StreamSpec::Audio(a) => match a.timing { + AudioTiming::Bytes => (u32::from(a.format.block_align), a.format.avg_bytes_per_sec, u32::from(a.format.block_align)), + AudioTiming::Packets { samples_per_packet } => (samples_per_packet, a.format.sample_rate, 0), + }, + } + } + + /// The stream's length in its own units. + fn length(&self) -> u64 { + match &self.spec { + StreamSpec::Audio(a) if a.timing == AudioTiming::Bytes => self.bytes / u64::from(a.format.block_align.max(1)), + _ => self.chunks, + } + } + + /// The stream's duration in seconds. + fn seconds(&self) -> f64 { + let (scale, rate, _) = self.time_base(); + self.length() as f64 * f64::from(scale) / f64::from(rate.max(1)) + } +} + +fn le16(v: &mut Vec, x: u16) { + v.extend_from_slice(&x.to_le_bytes()); +} + +fn le32(v: &mut Vec, x: u32) { + v.extend_from_slice(&x.to_le_bytes()); +} + +fn sat32(x: u64) -> u32 { + u32::try_from(x).unwrap_or(u32::MAX) +} + +/// A chunk: id, size, data, a pad byte to an even size. +fn chunk(id: &[u8; 4], data: &[u8]) -> Vec { + let mut v = Vec::with_capacity(data.len() + 9); + v.extend_from_slice(id); + le32(&mut v, sat32(data.len() as u64)); + v.extend_from_slice(data); + if data.len() % 2 == 1 { + v.push(0); + } + v +} + +fn list(ltype: &[u8; 4], body: &[u8]) -> Vec { + chunk(b"LIST", &[ltype.as_slice(), body].concat()) +} + +fn invalid(msg: impl Into) -> Error { + Error::Invalid(msg.into()) +} + +/// The `hdrl` list. Its length depends only on the stream descriptions and `max_riffs`, so the +/// final one ([`AviWriter::finish`]) replaces the first in place. +fn hdrl(streams: &[StreamState], main: Option, max_riffs: u32, first_frames: u32) -> Vec { + let video = main.and_then(|i| streams.get(i)).and_then(|s| match &s.spec { + StreamSpec::Video(v) => Some((v, s)), + StreamSpec::Audio(_) => None, + }); + let seconds = streams.iter().map(StreamState::seconds).fold(0.0, f64::max); + let total: u64 = streams.iter().map(|s| s.bytes).sum(); + let largest = streams.iter().map(|s| s.largest).max().unwrap_or(0); + + let mut avih = Vec::with_capacity(56); + le32(&mut avih, video.map_or(0, |(v, _)| (1e6 * f64::from(v.scale) / f64::from(v.rate)).round() as u32)); + le32(&mut avih, if seconds > 0.0 { (total as f64 / seconds).ceil() as u32 } else { 0 }); + le32(&mut avih, 0); // dwPaddingGranularity + le32(&mut avih, AVIF_HASINDEX | AVIF_ISINTERLEAVED); + le32(&mut avih, first_frames); + le32(&mut avih, 0); // dwInitialFrames + le32(&mut avih, streams.len() as u32); + le32(&mut avih, largest.saturating_add(8)); + le32(&mut avih, video.map_or(0, |(v, _)| v.format.width.unsigned_abs())); + le32(&mut avih, video.map_or(0, |(v, _)| v.format.height.unsigned_abs())); + avih.extend_from_slice(&[0; 16]); + + let mut body = chunk(b"avih", &avih); + for s in streams { + let (scale, rate, sample_size) = s.time_base(); + let mut strh = Vec::with_capacity(56); + let (fcc, handler, frame) = match &s.spec { + StreamSpec::Video(v) => (*b"vids", v.handler.unwrap_or(v.format.compression), (v.format.width, v.format.height.abs())), + StreamSpec::Audio(_) => (*b"auds", [0; 4], (0, 0)), + }; + strh.extend_from_slice(&fcc); + strh.extend_from_slice(&handler); + le32(&mut strh, 0); // dwFlags + le16(&mut strh, 0); // wPriority + le16(&mut strh, 0); // wLanguage + le32(&mut strh, 0); // dwInitialFrames + le32(&mut strh, scale); + le32(&mut strh, rate); + le32(&mut strh, 0); // dwStart + le32(&mut strh, sat32(s.length())); + le32(&mut strh, s.largest); + le32(&mut strh, u32::MAX); // dwQuality: default + le32(&mut strh, sample_size); + for v in [0, 0, frame.0, frame.1] { + le16(&mut strh, i16::try_from(v).unwrap_or(i16::MAX) as u16); + } + + let mut strf = Vec::new(); + match &s.spec { + StreamSpec::Video(v) => { + let f = &v.format; + le32(&mut strf, sat32(40 + f.extra.len() as u64)); + le32(&mut strf, f.width as u32); + le32(&mut strf, f.height as u32); + le16(&mut strf, 1); // biPlanes + le16(&mut strf, f.bit_count); + strf.extend_from_slice(&f.compression); + le32(&mut strf, f.size_image); + strf.extend_from_slice(&[0; 16]); // pels per metre, colours used and important + strf.extend_from_slice(&f.extra); + } + StreamSpec::Audio(a) => { + let f = &a.format; + le16(&mut strf, f.format_tag); + le16(&mut strf, f.channels); + le32(&mut strf, f.sample_rate); + le32(&mut strf, f.avg_bytes_per_sec); + le16(&mut strf, f.block_align); + le16(&mut strf, f.bits_per_sample); + le16(&mut strf, u16::try_from(f.extra.len()).unwrap_or(u16::MAX)); + strf.extend_from_slice(&f.extra[..f.extra.len().min(usize::from(u16::MAX))]); + } + } + + // indx: AVI_INDEX_OF_INDEXES, room for `max_riffs` entries + let mut indx = Vec::with_capacity(24 + 16 * max_riffs as usize); + le16(&mut indx, 4); // wLongsPerEntry + indx.push(0); // bIndexSubType + indx.push(0); // bIndexType: AVI_INDEX_OF_INDEXES + le32(&mut indx, s.supers.len() as u32); + indx.extend_from_slice(&s.id); + indx.extend_from_slice(&[0; 12]); + for e in &s.supers { + indx.extend_from_slice(&e.at.to_le_bytes()); + le32(&mut indx, e.size); + le32(&mut indx, e.duration); + } + indx.resize(24 + 16 * max_riffs as usize, 0); + + let mut strl = chunk(b"strh", &strh); + strl.extend(chunk(b"strf", &strf)); + let name = match &s.spec { + StreamSpec::Video(v) => v.name.as_deref(), + StreamSpec::Audio(a) => a.name.as_deref(), + }; + if let Some(n) = name { + strl.extend(chunk(b"strn", &[n.as_bytes(), &[0]].concat())); + } + strl.extend(chunk(b"indx", &indx)); + body.extend(list(b"strl", &strl)); + } + let mut dmlh = vec![0u8; DMLH_SIZE]; + let frames = video.map_or(0, |(_, s)| sat32(s.chunks)); + dmlh[..4].copy_from_slice(&frames.to_le_bytes()); + body.extend(list(b"odml", &chunk(b"dmlh", &dmlh))); + list(b"hdrl", &body) +} + +/// Writes an AVI file chunk by chunk; see the [module documentation](self). +#[derive(Debug)] +pub struct AviWriter { + w: W, + /// Where the file starts in `w`. + base: u64, + opts: MuxOptions, + streams: Vec, + /// The stream whose frame period and size the main header gives (the first video stream). + main: Option, + /// The next byte to write, from the start of the file. + pos: u64, + /// Bytes of the `hdrl` list, fixed so it can be rewritten in place. + hdrl_len: usize, + riff: Riff, + /// RIFFs started so far. + riffs: u32, + /// `idx1` of the first RIFF (16 bytes an entry), while it is open. + idx1: Vec<[u8; 16]>, + /// Video frames in the first RIFF (`avih` `dwTotalFrames`). + first_frames: u32, +} + +impl AviWriter { + /// Start a file with `streams` (1 to 100) at the current position of `w`. + pub fn new(mut w: W, streams: Vec, opts: MuxOptions) -> Result { + if streams.is_empty() || streams.len() > MAX_STREAMS { + return Err(invalid(format!("{} streams (1 to {MAX_STREAMS})", streams.len()))); + } + if !(MIN_RIFF_LIMIT..=MAX_RIFF_LIMIT).contains(&opts.riff_limit) { + return Err(invalid(format!("riff_limit {} is outside {MIN_RIFF_LIMIT}..={MAX_RIFF_LIMIT}", opts.riff_limit))); + } + if opts.max_riffs == 0 || opts.max_riffs > 1 << 16 { + return Err(invalid("max_riffs must be 1 to 65536")); + } + for (i, s) in streams.iter().enumerate() { + match s { + StreamSpec::Video(v) if v.rate == 0 || v.scale == 0 => return Err(invalid(format!("stream {i}: zero frame rate"))), + StreamSpec::Audio(a) if a.format.sample_rate == 0 => return Err(invalid(format!("stream {i}: zero sample rate"))), + StreamSpec::Audio(a) if a.timing == AudioTiming::Bytes && (a.format.block_align == 0 || a.format.avg_bytes_per_sec == 0) => { + return Err(invalid(format!("stream {i}: byte-timed audio needs a block align and a byte rate"))); + } + StreamSpec::Audio(AudioSpec { timing: AudioTiming::Packets { samples_per_packet: 0 }, .. }) => { + return Err(invalid(format!("stream {i}: zero samples a packet"))); + } + _ => {} + } + } + let main = streams.iter().position(|s| matches!(s, StreamSpec::Video(_))); + let streams: Vec = streams + .into_iter() + .enumerate() + .map(|(i, spec)| { + let kind = if matches!(spec, StreamSpec::Video(_)) { b"dc" } else { b"wb" }; + StreamState { + spec, + id: [b'0' + (i / 10) as u8, b'0' + (i % 10) as u8, kind[0], kind[1]], + chunks: 0, + bytes: 0, + largest: 0, + entries: Vec::new(), + duration: 0, + supers: Vec::new(), + } + }) + .collect(); + let header = hdrl(&streams, main, opts.max_riffs, 0); + if (header.len() + 24) as u64 > opts.riff_limit / 2 { + return Err(invalid("the header list takes more than half of riff_limit")); + } + let base = w.stream_position()?; + let mut head = Vec::with_capacity(header.len() + 24); + head.extend_from_slice(b"RIFF"); + le32(&mut head, 0); + head.extend_from_slice(b"AVI "); + head.extend_from_slice(&header); + let movi = head.len() as u64; + head.extend_from_slice(b"LIST"); + le32(&mut head, 0); + head.extend_from_slice(b"movi"); + w.write_all(&head)?; + Ok(AviWriter { + w, + base, + opts, + streams, + main, + pos: head.len() as u64, + hdrl_len: header.len(), + riff: Riff { start: 0, movi }, + riffs: 1, + idx1: Vec::new(), + first_frames: 0, + }) + } + + /// Bytes the current RIFF would take if it were closed after one more chunk of `chunk_len` + /// bytes for `stream`: its chunks, the standard indexes and (first RIFF) `idx1`. + fn riff_size_with(&self, stream: usize, chunk_len: u64) -> u64 { + let ix: u64 = self.streams.iter().enumerate().map(|(i, s)| 32 + 8 * (s.entries.len() as u64 + u64::from(i == stream))).sum(); + let idx1 = if self.riffs == 1 { 8 + 16 * (self.idx1.len() as u64 + 1) } else { 0 }; + self.pos - self.riff.start + chunk_len + ix + idx1 + } + + /// Write one chunk of stream `stream`: a video frame (`key` for a key frame; an empty chunk + /// repeats the previous picture) or a run of audio. Chunks go into the file in call order, + /// so the caller interleaves the streams. + pub fn write_chunk(&mut self, stream: usize, data: &[u8], key: bool) -> Result<()> { + if stream >= self.streams.len() { + return Err(invalid(format!("no stream {stream}"))); + } + let len = data.len() as u64; + let chunk_len = 8 + len + (len & 1); + if self.riff_size_with(stream, chunk_len) > self.opts.riff_limit && self.streams.iter().any(|s| !s.entries.is_empty()) { + self.next_riff()?; + } + if self.riff_size_with(stream, chunk_len) > self.opts.riff_limit { + return Err(invalid(format!("a {len}-byte chunk does not fit in a RIFF of riff_limit {} bytes", self.opts.riff_limit))); + } + let index_base = self.riff.movi + 8; + let s = &mut self.streams[stream]; + let at = self.pos; + let mut head = Vec::with_capacity(8); + head.extend_from_slice(&s.id); + le32(&mut head, len as u32); + self.w.write_all(&head)?; + self.w.write_all(data)?; + if len & 1 == 1 { + self.w.write_all(&[0])?; + } + self.pos += chunk_len; + // riff_limit ≤ 2 GiB keeps every offset in the RIFF below 2^31 + s.entries.push(Entry { offset: (at + 8 - index_base) as u32, size: len as u32, key }); + s.duration += s.units(len); + s.chunks += 1; + s.bytes += len; + s.largest = s.largest.max(len as u32); + if self.riffs == 1 { + let mut e = [0u8; 16]; + e[..4].copy_from_slice(&s.id); + e[4..8].copy_from_slice(&(if key { AVIIF_KEYFRAME } else { 0 }).to_le_bytes()); + e[8..12].copy_from_slice(&((at - index_base) as u32).to_le_bytes()); + e[12..].copy_from_slice(&(len as u32).to_le_bytes()); + self.idx1.push(e); + if Some(stream) == self.main { + self.first_frames += 1; + } + } + Ok(()) + } + + fn patch(&mut self, at: u64, bytes: &[u8]) -> Result<()> { + self.w.seek(SeekFrom::Start(self.base + at))?; + self.w.write_all(bytes)?; + self.w.seek(SeekFrom::Start(self.base + self.pos))?; + Ok(()) + } + + fn put(&mut self, bytes: &[u8]) -> Result<()> { + self.w.write_all(bytes)?; + self.pos += bytes.len() as u64; + Ok(()) + } + + /// Close the current RIFF: the standard indexes at the end of its `movi`, the sizes, and + /// (first RIFF) `idx1`. + fn end_riff(&mut self) -> Result<()> { + let index_base = self.riff.movi + 8; + for i in 0..self.streams.len() { + let s = &mut self.streams[i]; + let mut ix = Vec::with_capacity(24 + 8 * s.entries.len()); + le16(&mut ix, 2); // wLongsPerEntry + ix.push(0); // bIndexSubType + ix.push(1); // bIndexType: AVI_INDEX_OF_CHUNKS + le32(&mut ix, s.entries.len() as u32); + ix.extend_from_slice(&s.id); + ix.extend_from_slice(&index_base.to_le_bytes()); + le32(&mut ix, 0); + for e in s.entries.drain(..) { + le32(&mut ix, e.offset); + le32(&mut ix, e.size | if e.key { 0 } else { NOT_KEY }); + } + let at = self.pos; + let id = [b'i', b'x', s.id[0], s.id[1]]; + let c = chunk(&id, &ix); + s.supers.push(SuperEntry { at, size: c.len() as u32, duration: sat32(s.duration) }); + s.duration = 0; + self.put(&c)?; + } + let movi_size = sat32(self.pos - self.riff.movi - 8); + self.patch(self.riff.movi + 4, &movi_size.to_le_bytes())?; + if self.riffs == 1 { + let idx1 = std::mem::take(&mut self.idx1).concat(); + self.put(&chunk(b"idx1", &idx1))?; + } + let riff_size = sat32(self.pos - self.riff.start - 8); + self.patch(self.riff.start + 4, &riff_size.to_le_bytes()) + } + + /// Close the current RIFF and start a `RIFF AVIX` extension. + fn next_riff(&mut self) -> Result<()> { + if self.riffs >= self.opts.max_riffs { + return Err(invalid(format!("the file needs more than max_riffs ({}) RIFFs", self.opts.max_riffs))); + } + self.end_riff()?; + let start = self.pos; + let mut head = Vec::with_capacity(24); + head.extend_from_slice(b"RIFF"); + le32(&mut head, 0); + head.extend_from_slice(b"AVIX"); + head.extend_from_slice(b"LIST"); + le32(&mut head, 0); + head.extend_from_slice(b"movi"); + self.put(&head)?; + self.riff = Riff { start, movi: start + 12 }; + self.riffs += 1; + Ok(()) + } + + /// Close the file: the last RIFF's indexes and sizes, then the header list with the final + /// counts. Returns the writer, positioned at the end of the file. + pub fn finish(mut self) -> Result { + self.end_riff()?; + let header = hdrl(&self.streams, self.main, self.opts.max_riffs, self.first_frames); + if header.len() != self.hdrl_len { + return Err(invalid("the header list changed size")); + } + self.patch(12, &header)?; + self.w.flush()?; + Ok(self.w) + } +} diff --git a/crates/media/src/avi/mux_tests.rs b/crates/media/src/avi/mux_tests.rs new file mode 100644 index 000000000..96f77a04c --- /dev/null +++ b/crates/media/src/avi/mux_tests.rs @@ -0,0 +1,205 @@ +//! The AVI muxer read back by this module's demuxer, through every index it writes. + +use std::io::Cursor; + +use super::*; + +/// Chunk `i` of a test stream: sizes vary (odd, even, empty) and the bytes name the chunk. +fn payload(stream: usize, i: usize) -> Vec { + let len = match i % 7 { + 0 => 1500 + i * 3, + 3 => 0, + 5 => 1, + _ => 200 + (i * 37) % 900, + }; + (0..len).map(|k| (k as u32 ^ (i as u32) << 3 ^ (stream as u32) << 7) as u8).collect() +} + +fn video() -> VideoSpec { + let mut v = VideoSpec::new(*b"XVID", 320, 240, 30000, 1001); + v.format.extra = vec![0, 0, 1, 0xB0, 3]; + v.name = Some("picture".into()); + v +} + +fn pcm() -> AudioSpec { + let mut a = AudioSpec::pcm(48_000, 2, 16); + a.name = Some("sound".into()); + a +} + +/// Each stream's chunks: bytes and key frame. +type Written = [Vec<(Vec, bool)>; 2]; + +/// Write `frames` video frames (every 12th a key frame) each followed by an audio chunk of 4·k +/// bytes, returning the file and what was written per stream. +fn write(frames: usize, opts: MuxOptions) -> (Vec, Written) { + let mut w = AviWriter::new(Cursor::new(Vec::new()), vec![StreamSpec::Video(video()), StreamSpec::Audio(pcm())], opts).unwrap(); + let mut written: Written = [Vec::new(), Vec::new()]; + for i in 0..frames { + let v = payload(0, i); + let key = i % 12 == 0; + w.write_chunk(0, &v, key).unwrap(); + written[0].push((v, key)); + let a: Vec = payload(1, i).into_iter().take(4 * (1 + i % 400)).collect(); + let a = a[..a.len() / 4 * 4].to_vec(); + w.write_chunk(1, &a, true).unwrap(); + written[1].push((a, true)); + } + (w.finish().unwrap().into_inner(), written) +} + +fn tables(file: &[u8], f: &AviFile) -> Written { + std::array::from_fn(|s| (0..f.streams[s].chunks.len()).map(|i| (f.read_chunk(file, s, i).unwrap(), f.streams[s].chunks[i].key)).collect()) +} + +/// The top-level chunks of a file: (id, form, size). +fn riffs(file: &[u8]) -> Vec<([u8; 4], [u8; 4], u64)> { + children(&file, 0, file.len() as u64).into_iter().map(|(id, size, data)| (id, fourcc(file, data as usize), size)).collect() +} + +#[test] +fn headers_round_trip() { + let (file, written) = write(30, MuxOptions::default()); + let f = open(&file).unwrap(); + assert_eq!(f.index, IndexSource::OpenDml); + assert_eq!(f.header, MainHeader { micro_sec_per_frame: 33367, flags: 0x110, total_frames: 30, streams: 2, width: 320, height: 240 }); + let (v, a) = (&f.streams[0], &f.streams[1]); + assert_eq!((v.kind, v.handler, v.scale, v.rate, v.length, v.sample_size), (StreamKind::Video, *b"XVID", 1001, 30000, 30, 0)); + assert_eq!(v.video.as_ref(), Some(&video().format)); + assert_eq!(v.name.as_deref(), Some("picture")); + let audio_bytes: usize = written[1].iter().map(|c| c.0.len()).sum(); + assert_eq!((a.kind, a.scale, a.rate, a.sample_size, a.length as usize), (StreamKind::Audio, 4, 192_000, 4, audio_bytes / 4)); + assert_eq!(a.audio.as_ref(), Some(&pcm().format)); + assert_eq!(a.name.as_deref(), Some("sound")); + assert_eq!(v.codec_params().codec, crate::CodecId::Mpeg4Video); + assert_eq!(a.codec_params().codec, crate::CodecId::Pcm); + // one RIFF: hdrl, movi, idx1 + assert_eq!(riffs(&file).iter().map(|r| (r.0, r.1)).collect::>(), [(*b"RIFF", *b"AVI ")]); + assert_eq!(riffs(&file)[0].2 + 8, file.len() as u64); +} + +#[test] +fn every_index_describes_the_same_chunks() { + let (file, written) = write(40, MuxOptions::default()); + for (opts, source) in [ + (OpenOptions::default(), IndexSource::OpenDml), + (OpenOptions { ignore_odml: true, ignore_idx1: false }, IndexSource::Idx1), + (OpenOptions { ignore_odml: true, ignore_idx1: true }, IndexSource::Scan), + ] { + let f = open_with(&file, opts).unwrap(); + assert_eq!(f.index, source); + let got = tables(&file, &f); + assert_eq!(got[1], written[1], "{source:?}: audio"); + if source == IndexSource::Scan { + // a scan cannot tell key frames + assert_eq!(got[0].iter().map(|c| &c.0).collect::>(), written[0].iter().map(|c| &c.0).collect::>()); + } else { + assert_eq!(got[0], written[0], "{source:?}: video"); + } + } +} + +#[test] +fn large_files_continue_in_avix_extensions() { + let opts = MuxOptions { riff_limit: 24 << 10, max_riffs: 64 }; + let (file, written) = write(200, opts); + let r = riffs(&file); + assert!(r.len() > 3, "{} RIFFs", r.len()); + assert_eq!((r[0].0, r[0].1), (*b"RIFF", *b"AVI ")); + assert!(r[1..].iter().all(|x| (x.0, x.1) == (*b"RIFF", *b"AVIX"))); + assert!(r.iter().all(|x| x.2 + 8 <= opts.riff_limit), "a RIFF is over the limit: {r:?}"); + assert_eq!(r.iter().map(|x| x.2 + 8).sum::(), file.len() as u64); + + let f = open(&file).unwrap(); + assert_eq!(f.index, IndexSource::OpenDml); + assert_eq!(tables(&file, &f), written); + assert_eq!(f.streams[0].length, 200); + // avih counts the first RIFF's frames; strh the whole stream + let first = f.streams[0].chunks.iter().filter(|c| c.offset < r[0].2).count() as u32; + assert_eq!(f.header.total_frames, first); + assert!(first < 200); + // idx1 covers the first RIFF and the AVIX extensions are scanned + let f = open_with(&file, OpenOptions { ignore_odml: true, ignore_idx1: false }).unwrap(); + assert_eq!(f.index, IndexSource::Idx1); + assert_eq!(tables(&file, &f)[1], written[1]); + assert_eq!(f.streams[0].chunks.len(), 200); +} + +#[test] +fn a_file_can_start_inside_the_output() { + let mut out = Cursor::new(b"prefix".to_vec()); + out.set_position(6); + let mut w = AviWriter::new(out, vec![StreamSpec::Video(video())], MuxOptions::default()).unwrap(); + w.write_chunk(0, &[1, 2, 3], true).unwrap(); + let bytes = w.finish().unwrap().into_inner(); + assert_eq!(&bytes[..6], b"prefix"); + let file = &bytes[6..]; + let f = open(&file).unwrap(); + assert_eq!(f.read_chunk(&file, 0, 0).unwrap(), [1, 2, 3]); + assert_eq!(f.header.total_frames, 1); +} + +#[test] +fn packet_timed_audio_counts_chunks() { + let mut mp3 = AudioSpec::pcm(44_100, 2, 0); + mp3.format = WaveFormat { + format_tag: 0x55, + channels: 2, + sample_rate: 44_100, + avg_bytes_per_sec: 16_000, + block_align: 1, + bits_per_sample: 0, + extra: vec![0; 12], + }; + mp3.timing = AudioTiming::Packets { samples_per_packet: 1152 }; + let mut w = AviWriter::new(Cursor::new(Vec::new()), vec![StreamSpec::Audio(mp3)], MuxOptions::default()).unwrap(); + for i in 0..10 { + w.write_chunk(0, &payload(0, i), true).unwrap(); + } + let file = w.finish().unwrap().into_inner(); + let f = open(&file).unwrap(); + let a = &f.streams[0]; + assert_eq!((a.scale, a.rate, a.sample_size, a.length), (1152, 44_100, 0, 10)); + assert_eq!(a.codec_params().codec, crate::CodecId::Mp3); + assert_eq!(f.header.width, 0); +} + +#[test] +fn codec_descriptions_pick_their_fourcc() { + let p = crate::CodecParams::new(crate::CodecId::Mpeg4Video).with_size(640, 360).with_extradata(vec![0, 0, 1, 0xB0]); + let v = VideoSpec::for_codec(&p, 25, 1).unwrap(); + assert_eq!((v.format.compression, v.format.width, v.format.height, &v.format.extra[..]), (*b"FMP4", 640, 360, &[0, 0, 1, 0xB0][..])); + assert!(VideoSpec::for_codec(&crate::CodecParams::new(crate::CodecId::H264), 25, 1).is_none(), "no size"); + assert!(VideoSpec::for_codec(&crate::CodecParams::new(crate::CodecId::Aac).with_size(1, 1), 25, 1).is_none()); +} + +#[test] +fn bad_input_is_an_error() { + let new = |streams: Vec, opts| AviWriter::new(Cursor::new(Vec::new()), streams, opts).err(); + let d = MuxOptions::default(); + assert!(new(Vec::new(), d).is_some()); + assert!(new(vec![StreamSpec::Video(video()); 101], d).is_some()); + assert!(new(vec![StreamSpec::Video(video())], MuxOptions { riff_limit: 1000, ..d }).is_some()); + assert!(new(vec![StreamSpec::Video(video())], MuxOptions { riff_limit: 3 << 30, ..d }).is_some()); + assert!(new(vec![StreamSpec::Video(video())], MuxOptions { max_riffs: 0, ..d }).is_some()); + assert!(new(vec![StreamSpec::Video(VideoSpec::new(*b"XVID", 16, 16, 0, 1))], d).is_some()); + let mut a = pcm(); + a.format.block_align = 0; + assert!(new(vec![StreamSpec::Audio(a)], d).is_some()); + let mut a = pcm(); + a.timing = AudioTiming::Packets { samples_per_packet: 0 }; + assert!(new(vec![StreamSpec::Audio(a)], d).is_some()); + // max_riffs large enough that the header list cannot fit in half a small RIFF + assert!(new(vec![StreamSpec::Video(video())], MuxOptions { riff_limit: 16 << 10, max_riffs: 1000 }).is_some()); + + let mut w = AviWriter::new(Cursor::new(Vec::new()), vec![StreamSpec::Video(video())], MuxOptions { riff_limit: 16 << 10, max_riffs: 2 }).unwrap(); + assert!(w.write_chunk(1, &[0], true).is_err(), "no such stream"); + assert!(w.write_chunk(0, &vec![0; 20 << 10], true).is_err(), "larger than a RIFF"); + w.write_chunk(0, &vec![0; 10 << 10], true).unwrap(); + w.write_chunk(0, &vec![0; 10 << 10], true).unwrap(); // the second RIFF + assert!(w.write_chunk(0, &vec![0; 10 << 10], true).is_err(), "a third RIFF is past max_riffs"); + // what was written still closes into a readable file + let file = w.finish().unwrap().into_inner(); + assert_eq!(open(&file).unwrap().streams[0].chunks.len(), 2); +} diff --git a/crates/media/tests/oracle_avi_mux.rs b/crates/media/tests/oracle_avi_mux.rs new file mode 100644 index 000000000..f78acc92d --- /dev/null +++ b/crates/media/tests/oracle_avi_mux.rs @@ -0,0 +1,156 @@ +//! AVI files from [`AviWriter`] read by ffmpeg and ffprobe (skipped without them): ffmpeg makes +//! an MPEG-4 Part 2 elementary stream and PCM, the muxer interleaves them, and ffmpeg must decode +//! the AVI to exactly what it decodes from the sources, in one RIFF and across AVIX extensions. + +use std::io::Cursor; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use craft_media::avi::{AudioSpec, AviWriter, MuxOptions, StreamSpec, VideoSpec}; + +const FRAMES: usize = 50; +/// 25 frames a second of 48 kHz stereo 16-bit PCM: bytes of audio a frame. +const AUDIO_PER_FRAME: usize = 48_000 / 25 * 4; + +fn tools() -> Option<(PathBuf, PathBuf)> { + let ff = craft_media_testkit::ffmpeg_or_skip("avi muxer oracle")?; + let probe = craft_media_testkit::ffprobe()?; + Some((ff, probe)) +} + +fn run(cmd: &mut Command) -> Vec { + let out = cmd.output().expect("run tool"); + assert!(out.status.success(), "{:?}: {}", cmd, String::from_utf8_lossy(&out.stderr)); + // A plain WAVEFORMATEX has no channel mask, so ffmpeg notes the layout it assumes for any + // stereo PCM in AVI, its own files included. Every other line is a failure. + let diag: Vec = + String::from_utf8_lossy(&out.stderr).lines().filter(|l| !l.contains("Guessed Channel Layout: stereo")).map(str::to_owned).collect(); + assert!(diag.is_empty(), "{cmd:?} reported: {}", diag.join("\n")); + out.stdout +} + +/// The sources: an MPEG-4 Part 2 elementary stream (I-VOP every 12 frames, no B-VOPs) and raw +/// PCM, both from ffmpeg. +fn sources(ff: &Path, dir: &Path) -> (Vec, Vec) { + let m4v = dir.join("video.m4v"); + run(Command::new(ff) + .args(["-v", "warning", "-y", "-f", "lavfi", "-i", "testsrc2=s=160x120:r=25"]) + .args(["-frames:v", &FRAMES.to_string(), "-c:v", "mpeg4", "-threads", "1", "-q:v", "5", "-g", "12", "-bf", "0", "-f", "m4v"]) + .arg(&m4v)); + let pcm = run(Command::new(ff) + .args(["-v", "warning", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000"]) + .args(["-t", "2", "-ac", "2", "-f", "s16le", "-"])); + (std::fs::read(&m4v).expect("read m4v"), pcm) +} + +/// The VOPs of an elementary stream: each runs from its start code (the first from the start of +/// the stream, so it carries the configuration headers) to the next. An I-VOP is a key frame. +fn vops(es: &[u8]) -> Vec<(&[u8], bool)> { + let starts: Vec = es.windows(4).enumerate().filter(|(_, w)| *w == [0, 0, 1, 0xB6]).map(|(i, _)| i).collect(); + let mut out = Vec::new(); + for (k, &s) in starts.iter().enumerate() { + let from = if k == 0 { 0 } else { s }; + let to = starts.get(k + 1).copied().unwrap_or(es.len()); + out.push((&es[from..to], es.get(s + 4).is_some_and(|b| b >> 6 == 0))); + } + out +} + +fn mux(video: &[(&[u8], bool)], pcm: &[u8], opts: MuxOptions) -> Vec { + let streams = vec![StreamSpec::Video(VideoSpec::new(*b"XVID", 160, 120, 25, 1)), StreamSpec::Audio(AudioSpec::pcm(48_000, 2, 16))]; + let mut w = AviWriter::new(Cursor::new(Vec::new()), streams, opts).expect("writer"); + for (i, (vop, key)) in video.iter().enumerate() { + w.write_chunk(0, vop, *key).expect("video"); + let a = pcm.get(i * AUDIO_PER_FRAME..((i + 1) * AUDIO_PER_FRAME).min(pcm.len())).unwrap_or_default(); + w.write_chunk(1, a, true).expect("audio"); + } + w.finish().expect("finish").into_inner() +} + +fn decode(ff: &Path, input: &Path, format: Option<&str>, map: &str, out: &[&str]) -> Vec { + let mut c = Command::new(ff); + c.args(["-v", "warning", "-nostdin"]); + if let Some(f) = format { + c.args(["-f", f]); + } + c.arg("-i").arg(input).args(["-map", map]).args(out).arg("-"); + run(&mut c) +} + +#[test] +fn ffmpeg_decodes_muxed_files_like_their_sources() { + let Some((ff, probe)) = tools() else { return }; + let dir = craft_media_testkit::private_dir("avi-mux"); + let (es, pcm) = sources(&ff, &dir); + let video = vops(&es); + assert_eq!(video.len(), FRAMES); + assert!(video.iter().filter(|v| v.1).count() >= 4); + let want_video = decode(&ff, &dir.join("video.m4v"), Some("m4v"), "0:v", &["-f", "rawvideo", "-pix_fmt", "yuv420p"]); + + for (name, opts) in [("one_riff.avi", MuxOptions::default()), ("avix.avi", MuxOptions { riff_limit: 64 << 10, max_riffs: 64 })] { + let file = mux(&video, &pcm, opts); + let path = dir.join(name); + std::fs::write(&path, &file).expect("write avi"); + if name == "avix.avi" { + assert!(file.windows(4).filter(|w| *w == b"AVIX").count() >= 3, "{name}: AVIX extensions"); + } + let got_video = decode(&ff, &path, None, "0:v", &["-f", "rawvideo", "-pix_fmt", "yuv420p"]); + assert!(got_video == want_video, "{name}: decoded pictures differ from the elementary stream's"); + // ffmpeg reads an interleaved file front to back; a seek goes through the index (OpenDML + // when the file has it). 1.0 s lands after key frame 24, so frame 25 comes out first. + for at in ["1.0", "1.6"] { + let frame = (at.parse::().unwrap_or(0.0) * 25.0).round() as usize; + let size = 160 * 120 * 3 / 2; + let mut c = Command::new(&ff); + c.args(["-v", "warning", "-nostdin", "-ss", at, "-i"]).arg(&path).args([ + "-map", + "0:v", + "-frames:v", + "1", + "-f", + "rawvideo", + "-pix_fmt", + "yuv420p", + "-", + ]); + let got = run(&mut c); + assert!(got == want_video[frame * size..(frame + 1) * size], "{name}: seeking to {at} s gave another picture than frame {frame}"); + } + let got_audio = decode(&ff, &path, None, "0:a", &["-f", "s16le"]); + assert!(got_audio == pcm, "{name}: decoded audio differs from the PCM ({} vs {} bytes)", got_audio.len(), pcm.len()); + + let info = String::from_utf8_lossy(&run(Command::new(&probe) + .args([ + "-v", + "error", + "-count_packets", + "-show_entries", + "stream=codec_name,codec_tag_string,width,height,avg_frame_rate,nb_read_packets,sample_rate,channels", + "-of", + "default=nw=1", + ]) + .arg(&path))) + .into_owned(); + for want in [ + "codec_name=mpeg4", + "codec_tag_string=XVID", + "width=160", + "height=120", + "avg_frame_rate=25/1", + "nb_read_packets=50", + "codec_name=pcm_s16le", + "sample_rate=48000", + "channels=2", + ] { + assert!(info.lines().any(|l| l == want), "{name}: ffprobe lacks {want}:\n{info}"); + } + let duration = String::from_utf8_lossy(&run(Command::new(&probe) + .args(["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0"]) + .arg(&path))) + .trim() + .parse::() + .unwrap_or(0.0); + assert!((duration - 2.0).abs() < 0.05, "{name}: duration {duration}"); + } + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/video/Cargo.toml b/crates/video/Cargo.toml index 0e31f8e26..ea8d4773d 100644 --- a/crates/video/Cargo.toml +++ b/crates/video/Cargo.toml @@ -58,6 +58,8 @@ rayon = { workspace = true, optional = true } [dev-dependencies] craft-media-testkit = { workspace = true } +# The AVI muxer, for the MPEG-4 Part 2 encoder's end-to-end test. +craft-media = { workspace = true, features = ["avi"] } [[test]] name = "dnx" diff --git a/crates/video/tests/mpeg4_encode/main.rs b/crates/video/tests/mpeg4_encode/main.rs index 0b53c5caf..8185acbc0 100644 --- a/crates/video/tests/mpeg4_encode/main.rs +++ b/crates/video/tests/mpeg4_encode/main.rs @@ -322,3 +322,37 @@ fn extreme_content_at_the_finest_quantiser() { assert!(d >= 45.0, "frame {i}: {d:.1} dB from the reconstruction"); } } + +/// The encoder's packets in an AVI (the track description through `VideoSpec::for_codec`) decode +/// in ffmpeg to exactly what the raw stream decodes to. Tagged `XVID` they would not: ffmpeg then +/// decodes with Xvid's IDCT. +#[test] +fn packets_play_from_an_avi_file() { + use craft_media::avi::{AviWriter, MuxOptions, StreamSpec, VideoSpec}; + let Some(ff) = ffmpeg() else { return }; + let mut cfg = EncoderConfig::new(160, 96, 25, 1); + cfg.keyint = 10; + let Encoded { enc, packets, .. } = encode(cfg, 30); + let spec = VideoSpec::for_codec(&enc.codec_params(), 25, 1).expect("an AVI FourCC for MPEG-4 Part 2"); + assert_eq!(spec.format.compression, *b"FMP4"); + let mut w = AviWriter::new(std::io::Cursor::new(Vec::new()), vec![StreamSpec::Video(spec)], MuxOptions::default()).expect("writer"); + for p in &packets { + w.write_chunk(0, &p.data, p.keyframe).expect("chunk"); + } + let avi = w.finish().expect("finish").into_inner(); + let dir = out_dir("avi"); + let (avi_path, es_path) = (dir.join("stream.avi"), dir.join("stream.m4v")); + std::fs::write(&avi_path, avi).expect("write avi"); + write_stream(&es_path, &packets); + let (_, want) = ffmpeg_decode(&ff, &es_path); + let out = Command::new(&ff) + .args(["-hide_banner", "-nostdin", "-v", "warning", "-i"]) + .arg(&avi_path) + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) + .output() + .expect("run ffmpeg"); + let diag = String::from_utf8_lossy(&out.stderr); + assert!(out.status.success() && diag.trim().is_empty(), "{diag}"); + assert_eq!(out.stdout.len(), 160 * 96 * 3 / 2 * 30); + assert!(out.stdout == want, "the AVI decodes differently from the raw stream"); +} From b4ada28f19067d4693e902e0e5f32eb292241e3a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:37:48 -0500 Subject: [PATCH 075/520] Fix format registry clippy and add ASE support --- crates/file-formats/src/lib.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 1e45ad447..c6ac77540 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -23,6 +23,7 @@ pub enum Format { Tiff, WebP, Heif, + Ase, Psd, Affinity, Ora, @@ -64,6 +65,9 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option { if bytes.starts_with(b"8BPS") { return Some(Format::Psd); } + if bytes.starts_with(b"ASEF") { + return Some(Format::Ase); + } if bytes.starts_with(b"PDN3") { return Some(Format::Pdn); } @@ -86,20 +90,19 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option { if bytes.trim_ascii_start().starts_with(b"{\\rtf") { return Some(Format::Rtf); } - if craft_svg::sniff::is_svgz(bytes) { - if let Ok(text) = craft_svg::svgz::text(bytes, 64 * 1024 * 1024) - && craft_svg::sniff::is_svg(text.as_bytes()) - { - return Some(Format::Svgz); - } + if craft_svg::sniff::is_svgz(bytes) + && let Ok(text) = craft_svg::svgz::text(bytes, 64 * 1024 * 1024) + && craft_svg::sniff::is_svg(text.as_bytes()) + { + return Some(Format::Svgz); } if craft_svg::sniff::is_svg(bytes) { return Some(Format::Svg); } - if let Some(ext) = extension(name) { - if let Some(format) = from_extension(ext) { - return Some(format); - } + if let Some(ext) = extension(name) + && let Some(format) = from_extension(ext) + { + return Some(format); } let head = bytes.get(..4096).unwrap_or(bytes); if dxf_ascii_prefix(head) || head.starts_with(b"AutoCAD Binary DXF\r\n\x1a\0") { @@ -125,6 +128,7 @@ pub fn from_extension(ext: &str) -> Option { "tif" | "tiff" => Some(Format::Tiff), "webp" => Some(Format::WebP), "heic" | "heif" => Some(Format::Heif), + "ase" => Some(Format::Ase), "psd" | "psb" => Some(Format::Psd), "afdesign" | "afphoto" | "afpub" => Some(Format::Affinity), "ora" => Some(Format::Ora), From e356ff84a4b5009518b1995e559675866e611d8f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:38:06 -0500 Subject: [PATCH 076/520] Initialize raster decode limits directly --- crates/raster-codecs/src/common.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index ec61aeedd..2d25f9f41 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -31,11 +31,13 @@ pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result limits.max_file_bytes { return Err(CodecError::LimitExceeded(format!("{} encoded bytes exceed max {}", bytes.len(), limits.max_file_bytes))); } - let mut decode_limits = craft_image_codecs::Limits::default(); - decode_limits.max_width = limits.max_width; - decode_limits.max_height = limits.max_height; - decode_limits.max_pixels = limits.max_pixels; - decode_limits.max_alloc = limits.max_alloc_bytes; + let decode_limits = craft_image_codecs::Limits { + max_width: limits.max_width, + max_height: limits.max_height, + max_pixels: limits.max_pixels, + max_alloc: limits.max_alloc_bytes, + ..Default::default() + }; let decoded = craft_image_codecs::decode_with(bytes, &craft_image_codecs::DecodeOptions { limits: decode_limits, ..Default::default() }) .map_err(map_codec_error)?; let output_bytes = u64::from(decoded.width()).saturating_mul(u64::from(decoded.height())).saturating_mul(4); From 8cdd25ce0a307bbe073e2af217d68068ec558f95 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 06:38:38 +0000 Subject: [PATCH 077/520] feat(figcraft-format): add native document header contract --- Cargo.lock | 9 ++++ Cargo.toml | 1 + crates/figcraft-format/Cargo.toml | 16 +++++++ crates/figcraft-format/src/lib.rs | 77 +++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 crates/figcraft-format/Cargo.toml create mode 100644 crates/figcraft-format/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 266217a1a..1bbfe7965 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -899,6 +899,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-figcraft-format" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-font-catalog" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index a32e1df27..b07dac4be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,6 +62,7 @@ craft-tiff = { path = "crates/tiff" } craft-ui = { path = "crates/ui" } craft-update = { path = "crates/update" } craft-vectorcraft-format = { path = "crates/vectorcraft-format" } +craft-figcraft-format = { path = "crates/figcraft-format" } craft-xml = { path = "crates/xml" } craft-video = { path = "crates/video" } craft-web-api = { path = "crates/web-api" } diff --git a/crates/figcraft-format/Cargo.toml b/crates/figcraft-format/Cargo.toml new file mode 100644 index 000000000..3275fbcd6 --- /dev/null +++ b/crates/figcraft-format/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "craft-figcraft-format" +description = "The native FigCraft document JSON and archive contract." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs new file mode 100644 index 000000000..5df23cf24 --- /dev/null +++ b/crates/figcraft-format/src/lib.rs @@ -0,0 +1,77 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! The neutral JSON header and version contract for FigCraft's native project format. +//! App-specific document fields remain a generic flattened payload. +use serde::de::{IgnoredAny, MapAccess, Visitor}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +pub const EXTENSION: &str = "figcraft"; +pub const FORMAT: &str = "figcraft"; +pub const VERSION: u32 = 1; + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Header { pub version: Option, pub images: Option> } + +pub fn read_header(text: &str) -> Result { serde_json::from_str(text) } + +impl<'de> Deserialize<'de> for Header { + fn deserialize>(d: D) -> Result { + struct V; + impl<'de> Visitor<'de> for V { + type Value = Header; + fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("a FigCraft document object") } + fn visit_map>(self, mut m: A) -> Result { + let mut h = Header::default(); + while let Some(k) = m.next_key::()? { + match k.as_str() { + "version" => h.version = m.next_value::()?.as_u64(), + "imageIndex" => h.images = m.next_value::()?.as_array().map(|a| a.iter().filter_map(|v| v.get("hash").and_then(Value::as_str).map(str::to_owned)).collect()), + _ => { m.next_value::()?; } + } + } + Ok(h) + } + } + d.deserialize_map(V) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +#[error("this file was saved by a newer FigCraft (format version {0})")] +pub struct NewerVersion(pub u32); + +pub fn check_version(h: &Header) -> Result<(), NewerVersion> { + if let Some(v) = h.version && v > u64::from(VERSION) { return Err(NewerVersion(u32::try_from(v).unwrap_or(u32::MAX))); } + Ok(()) +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +pub struct ImageIndexEntry { pub hash: String, pub width: u32, pub height: u32 } + +pub fn write_document_json(doc: &D, images: &[ImageIndexEntry], w: W) -> Result<(), serde_json::Error> { + #[derive(Serialize)] + struct Flat<'a, D: ?Sized> { format: &'static str, version: u32, #[serde(rename="imageIndex")] images: &'a [ImageIndexEntry], #[serde(flatten)] document: &'a D } + Flat { format: FORMAT, version: VERSION, images, document: doc }.serialize(&mut serde_json::Serializer::new(w)) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn header_skips_app_fields() { + let h = read_header(r#"{"version":1,"imageIndex":[{"hash":"abc"}],"nested":{"unused":true}}"#).unwrap(); + assert_eq!(h.version, Some(1)); assert_eq!(h.images, Some(vec!["abc".into()])); + } + #[test] + fn newer_version_is_reported_before_document_decode() { + assert_eq!(check_version(&Header { version: Some(2), images: None }), Err(NewerVersion(2))); + assert_eq!(check_version(&Header { version: Some(u64::MAX), images: None }), Err(NewerVersion(u32::MAX))); + } + #[test] + fn writer_flattens_document_fields() { + let doc = serde_json::json!({"name":"example"}); let mut bytes=Vec::new(); + write_document_json(&doc, &[], &mut bytes).unwrap(); + let v: Value=serde_json::from_slice(&bytes).unwrap(); + assert_eq!(v["format"], FORMAT); assert_eq!(v["version"], VERSION); assert_eq!(v["name"], "example"); + } +} From 111b522d05bcc75bb4f15eaa91496eb5f9c6a54f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 06:41:10 +0000 Subject: [PATCH 078/520] feat(figcraft-format): add bounded native archive access --- Cargo.lock | 10 +++ crates/figcraft-format/Cargo.toml | 1 + crates/figcraft-format/src/lib.rs | 137 +++++++++++++++++++++++++++--- 3 files changed, 136 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf0e0bd47..fda532570 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1445,6 +1445,16 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-figcraft-format" +version = "0.1.0" +dependencies = [ + "craft-zip", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-figma-rest" version = "0.1.0" diff --git a/crates/figcraft-format/Cargo.toml b/crates/figcraft-format/Cargo.toml index 3275fbcd6..22682034b 100644 --- a/crates/figcraft-format/Cargo.toml +++ b/crates/figcraft-format/Cargo.toml @@ -13,4 +13,5 @@ workspace = true [dependencies] serde = { workspace = true } serde_json = { workspace = true } +craft-zip = { workspace = true } thiserror = { workspace = true } diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index 5df23cf24..fd2c6ecf2 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -10,23 +10,37 @@ pub const FORMAT: &str = "figcraft"; pub const VERSION: u32 = 1; #[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct Header { pub version: Option, pub images: Option> } +pub struct Header { + pub version: Option, + pub images: Option>, +} -pub fn read_header(text: &str) -> Result { serde_json::from_str(text) } +pub fn read_header(text: &str) -> Result { + serde_json::from_str(text) +} impl<'de> Deserialize<'de> for Header { fn deserialize>(d: D) -> Result { struct V; impl<'de> Visitor<'de> for V { type Value = Header; - fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("a FigCraft document object") } + fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("a FigCraft document object") + } fn visit_map>(self, mut m: A) -> Result { let mut h = Header::default(); while let Some(k) = m.next_key::()? { match k.as_str() { "version" => h.version = m.next_value::()?.as_u64(), - "imageIndex" => h.images = m.next_value::()?.as_array().map(|a| a.iter().filter_map(|v| v.get("hash").and_then(Value::as_str).map(str::to_owned)).collect()), - _ => { m.next_value::()?; } + "imageIndex" => { + h.images = m + .next_value::()? + .as_array() + .map(|a| a.iter().filter_map(|v| v.get("hash").and_then(Value::as_str).map(str::to_owned)).collect()) + } + _ => { + m.next_value::()?; + } } } Ok(h) @@ -41,16 +55,31 @@ impl<'de> Deserialize<'de> for Header { pub struct NewerVersion(pub u32); pub fn check_version(h: &Header) -> Result<(), NewerVersion> { - if let Some(v) = h.version && v > u64::from(VERSION) { return Err(NewerVersion(u32::try_from(v).unwrap_or(u32::MAX))); } + if let Some(v) = h.version + && v > u64::from(VERSION) + { + return Err(NewerVersion(u32::try_from(v).unwrap_or(u32::MAX))); + } Ok(()) } #[derive(Clone, Debug, PartialEq, Eq, Serialize)] -pub struct ImageIndexEntry { pub hash: String, pub width: u32, pub height: u32 } +pub struct ImageIndexEntry { + pub hash: String, + pub width: u32, + pub height: u32, +} pub fn write_document_json(doc: &D, images: &[ImageIndexEntry], w: W) -> Result<(), serde_json::Error> { #[derive(Serialize)] - struct Flat<'a, D: ?Sized> { format: &'static str, version: u32, #[serde(rename="imageIndex")] images: &'a [ImageIndexEntry], #[serde(flatten)] document: &'a D } + struct Flat<'a, D: ?Sized> { + format: &'static str, + version: u32, + #[serde(rename = "imageIndex")] + images: &'a [ImageIndexEntry], + #[serde(flatten)] + document: &'a D, + } Flat { format: FORMAT, version: VERSION, images, document: doc }.serialize(&mut serde_json::Serializer::new(w)) } @@ -60,7 +89,8 @@ mod tests { #[test] fn header_skips_app_fields() { let h = read_header(r#"{"version":1,"imageIndex":[{"hash":"abc"}],"nested":{"unused":true}}"#).unwrap(); - assert_eq!(h.version, Some(1)); assert_eq!(h.images, Some(vec!["abc".into()])); + assert_eq!(h.version, Some(1)); + assert_eq!(h.images, Some(vec!["abc".into()])); } #[test] fn newer_version_is_reported_before_document_decode() { @@ -69,9 +99,92 @@ mod tests { } #[test] fn writer_flattens_document_fields() { - let doc = serde_json::json!({"name":"example"}); let mut bytes=Vec::new(); + let doc = serde_json::json!({"name":"example"}); + let mut bytes = Vec::new(); write_document_json(&doc, &[], &mut bytes).unwrap(); - let v: Value=serde_json::from_slice(&bytes).unwrap(); - assert_eq!(v["format"], FORMAT); assert_eq!(v["version"], VERSION); assert_eq!(v["name"], "example"); + let v: Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(v["format"], FORMAT); + assert_eq!(v["version"], VERSION); + assert_eq!(v["name"], "example"); + } +} + +/// Limits for the native ZIP package reader. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ArchiveLimits { + pub max_entries: u64, + pub max_total_bytes: u64, +} + +/// A bounded reader for a native `.figcraft` ZIP package. +pub struct Archive<'a> { + inner: craft_zip::Archive<'a>, +} + +impl<'a> Archive<'a> { + pub fn open(bytes: &'a [u8], limits: ArchiveLimits) -> Result { + let inner = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries, max_total_bytes: limits.max_total_bytes })?; + Ok(Self { inner }) + } + + pub fn names(&self) -> impl Iterator> { + self.inner.names() + } + + pub fn has_duplicate_names(&self) -> bool { + self.inner.has_duplicate_names() + } + + pub fn contains(&self, name: &str) -> bool { + self.inner.contains(name) + } + + pub fn read(&mut self, name: &str, max_bytes: u64) -> Result>, craft_zip::Error> { + self.inner.read(name, max_bytes) + } + + pub fn budget_used(&self) -> u64 { + self.inner.budget().used() + } +} + +/// One member to write into a native ZIP package. +#[derive(Clone, Copy, Debug)] +pub struct Member<'a> { + pub name: &'a str, + pub bytes: &'a [u8], + pub compression: craft_zip::Compression, +} + +/// Write a deterministic native ZIP package using the shared ZIP writer. +pub fn write_archive<'a>(members: impl IntoIterator>) -> Result, craft_zip::Error> { + let mut writer = craft_zip::Writer::new(); + for member in members { + writer.add(member.name, member.bytes, member.compression)?; + } + writer.finish() +} + +#[cfg(test)] +mod archive_tests { + use super::*; + + #[test] + fn bounded_archive_reader_and_writer_round_trip_members() { + let bytes = + write_archive([Member { name: "document.json", bytes: br#"{"version":1}"#, compression: craft_zip::Compression::Deflated }]).unwrap(); + let mut archive = Archive::open(&bytes, ArchiveLimits { max_entries: 4, max_total_bytes: 128 }).unwrap(); + assert!(archive.contains("document.json")); + assert_eq!(archive.read("document.json", 32).unwrap(), Some(br#"{"version":1}"#.to_vec())); + assert_eq!(archive.budget_used(), 13); + } + + #[test] + fn archive_reader_enforces_entry_and_total_limits() { + let bytes = write_archive([Member { name: "document.json", bytes: b"123456789", compression: craft_zip::Compression::Deflated }]).unwrap(); + let mut small_entry = Archive::open(&bytes, ArchiveLimits { max_entries: 4, max_total_bytes: 100 }).unwrap(); + assert!(small_entry.read("document.json", 8).is_err()); + let mut small_total = Archive::open(&bytes, ArchiveLimits { max_entries: 4, max_total_bytes: 8 }).unwrap(); + assert!(small_total.read("document.json", 100).is_err()); } } From 80762908548a215dd450b53b781fcd562105ff95 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:44:36 -0500 Subject: [PATCH 079/520] Avoid needless raster limit update --- crates/raster-codecs/src/common.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index 2d25f9f41..925935871 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -36,7 +36,6 @@ pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result Date: Fri, 13 Nov 2026 01:44:49 -0500 Subject: [PATCH 080/520] Detect DesignCraft and IDML document content --- Cargo.lock | 4 +++ crates/file-formats/Cargo.toml | 4 +++ crates/file-formats/src/lib.rs | 66 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 87ef135e7..8f13c4412 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1459,7 +1459,11 @@ name = "craft-file-formats" version = "0.1.0" dependencies = [ "craft-eps", + "craft-idml", "craft-svg", + "craft-zip", + "serde", + "serde_json", ] [[package]] diff --git a/crates/file-formats/Cargo.toml b/crates/file-formats/Cargo.toml index 00d3b388a..a1a605918 100644 --- a/crates/file-formats/Cargo.toml +++ b/crates/file-formats/Cargo.toml @@ -9,7 +9,11 @@ rust-version.workspace = true [dependencies] craft-eps = { workspace = true } +craft-idml = { workspace = true } craft-svg = { workspace = true } +craft-zip = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } [lints] workspace = true diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index c6ac77540..1bdfc3b5c 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -34,6 +34,8 @@ pub enum Format { Pcraft, Idml, DesignCraft, + /// Legacy DesignCraft documents serialized as one JSON object. + DesignCraftJson, Indd, Docx, /// Legacy OLE Compound File Word document; identified by its extension only. @@ -51,6 +53,15 @@ pub enum Format { /// Resolve a name and file bytes to a format. Strong signatures take precedence over /// extension hints. Only format-specific bounded probes and extension tables live here. pub fn detect(name: &str, bytes: &[u8]) -> Option { + if craft_idml::is_idml(bytes) { + return Some(Format::Idml); + } + if craft_zip::stored_mimetype(bytes) == Some(b"application/vnd.designcraft+zip") || designcraft_zip_has_document(bytes) { + return Some(Format::DesignCraft); + } + if is_designcraft_json(bytes) { + return Some(Format::DesignCraftJson); + } if bytes.starts_with(&INDD_SIGNATURE) { return Some(Format::Indd); } @@ -111,6 +122,61 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option { None } +/// DesignCraft's older ZIPs predate their leading `mimetype`; they are identified by the +/// `document.json` member. The directory is bounded before it is indexed and no entry is inflated. +fn designcraft_zip_has_document(bytes: &[u8]) -> bool { + if !bytes.starts_with(b"PK") { + return false; + } + craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 100_000, max_total_bytes: 0 }) + .is_ok_and(|archive| archive.contains("document.json")) +} + +/// Read only a bounded prefix, stopping as soon as the model's required top-level keys have +/// appeared. This distinguishes legacy DesignCraft JSON from arbitrary JSON without decoding the +/// document or walking its arrays and assets. +fn is_designcraft_json(bytes: &[u8]) -> bool { + const LIMIT: usize = 1 << 20; + const REQUIRED: u16 = (1 << 10) - 1; + const KEYS: [&str; 10] = ["title", "settings", "spreads", "parents", "layers", "stories", "styles", "swatches", "sections", "next_id"]; + + struct Probe; + impl<'de> serde::de::Visitor<'de> for Probe { + type Value = bool; + + fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str("a DesignCraft document object") + } + + fn visit_map>(self, mut map: A) -> Result { + let mut found = 0u16; + while let Some(key) = map.next_key::()? { + if let Some(index) = KEYS.iter().position(|candidate| *candidate == key) { + found |= 1 << index; + } + map.next_value::()?; + if found == REQUIRED { + return Ok(true); + } + } + Ok(false) + } + } + + use serde::de::DeserializeSeed; + struct Seed; + impl<'de> DeserializeSeed<'de> for Seed { + type Value = bool; + fn deserialize>(self, deserializer: D) -> Result { + deserializer.deserialize_map(Probe) + } + } + + let prefix = bytes.get(..bytes.len().min(LIMIT)).unwrap_or(bytes); + let mut deserializer = serde_json::Deserializer::from_slice(prefix); + Seed.deserialize(&mut deserializer).unwrap_or(false) +} + /// Resolve a file extension to a family, case-insensitively. pub fn from_extension(ext: &str) -> Option { match ext.trim_start_matches('.').to_ascii_lowercase().as_str() { From 30b1e426e0ef07e0606e0ab09ec540eca5665bb6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 06:49:46 +0000 Subject: [PATCH 081/520] feat(figcraft-format): stream native document JSON --- crates/figcraft-format/src/lib.rs | 57 +++++++++++++++++++++++++++++++ crates/zip/src/write.rs | 18 +++++----- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index fd2c6ecf2..08b9bcb9a 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -188,3 +188,60 @@ mod archive_tests { assert!(small_total.read("document.json", 100).is_err()); } } + +/// A native archive writer that keeps the streaming document serializer out of the consumer. +pub struct ArchiveWriter { + inner: craft_zip::Writer, +} + +impl ArchiveWriter { + pub fn new() -> Self { + Self { inner: craft_zip::Writer::new() } + } + + pub fn add(&mut self, name: &str, bytes: &[u8], compression: craft_zip::Compression) -> Result<(), craft_zip::Error> { + self.inner.add(name, bytes, compression) + } + + pub fn add_document_json(&mut self, name: &str, document: &D, images: &[ImageIndexEntry]) -> Result<(), ArchiveWriteError> { + self.inner + .add_with(name, craft_zip::Compression::Level(3), |w| write_document_json(document, images, w).map_err(std::io::Error::other)) + .map_err(ArchiveWriteError::Archive) + } + + pub fn finish(self) -> Result, craft_zip::Error> { + self.inner.finish() + } +} + +impl Default for ArchiveWriter { + fn default() -> Self { + Self::new() + } +} + +#[derive(Debug, thiserror::Error)] +pub enum ArchiveWriteError { + #[error("could not serialize native document JSON: {0}")] + Json(#[from] serde_json::Error), + #[error("could not write native archive: {0}")] + Archive(#[from] craft_zip::Error), +} + +#[cfg(test)] +mod streaming_writer_tests { + use super::*; + + #[test] + fn native_writer_streams_flat_document_into_archive() { + let document = serde_json::json!({"name":"streamed"}); + let mut writer = ArchiveWriter::new(); + writer.add_document_json("document.json", &document, &[]).unwrap(); + let bytes = writer.finish().unwrap(); + let mut archive = Archive::open(&bytes, ArchiveLimits { max_entries: 4, max_total_bytes: 4096 }).unwrap(); + let json = archive.read("document.json", 1024).unwrap().unwrap(); + let value: Value = serde_json::from_slice(&json).unwrap(); + assert_eq!(value["name"], "streamed"); + assert_eq!(value["format"], FORMAT); + } +} diff --git a/crates/zip/src/write.rs b/crates/zip/src/write.rs index 2f5d7ca47..12838a3b8 100644 --- a/crates/zip/src/write.rs +++ b/crates/zip/src/write.rs @@ -33,22 +33,24 @@ impl Writer { Writer::default() } - /// Adds an entry. A name the archive already has is an error. + /// Adds an entry from a byte slice. A name the archive already has is an error. pub fn add(&mut self, name: &str, data: &[u8], compression: Compression) -> Result<(), Error> { + self.add_with(name, compression, |writer| writer.write_all(data)) + } + + /// Adds an entry by streaming it into the compressor, so large generated entries need not + /// first be buffered in memory. A name the archive already has is an error. + pub fn add_with(&mut self, name: &str, compression: Compression, write: impl FnOnce(&mut dyn Write) -> std::io::Result<()>) -> Result<(), Error> { let (method, level) = match compression { Compression::Stored => (zip::CompressionMethod::Stored, None), Compression::Deflated => (zip::CompressionMethod::Deflated, Some(6)), Compression::Level(l) => (zip::CompressionMethod::Deflated, Some(i64::from(l.min(9)))), }; - let options = zip::write::SimpleFileOptions::default() - .compression_method(method) - .compression_level(level) - .last_modified_time(zip::DateTime::default()) - // Deflate can grow incompressible data a little, so ZIP64 well before 4 GiB. - .large_file(data.len() as u64 >= u64::from(u32::MAX) / 2); + let options = + zip::write::SimpleFileOptions::default().compression_method(method).compression_level(level).last_modified_time(zip::DateTime::default()); let fail = |e: &dyn std::fmt::Display| Error::Write(format!("{name}: {e}")); self.zip.start_file(name, options).map_err(|e| fail(&e))?; - self.zip.write_all(data).map_err(|e| fail(&e)) + write(&mut self.zip).map_err(|e| fail(&e)) } /// The finished archive. From 71337f6772b57a61e0f171c941f695822eea19a5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:51:08 -0500 Subject: [PATCH 082/520] Add shared CSV and TSV file formats --- crates/file-formats/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 1bdfc3b5c..a570f0af4 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -45,6 +45,8 @@ pub enum Format { TaggedText, Pptx, PlainText, + Csv, + Tsv, Gcode, Studio, StencilCraft, @@ -214,6 +216,8 @@ pub fn from_extension(ext: &str) -> Option { "xlsx" => Some(Format::Xlsx), "pptx" => Some(Format::Pptx), "txt" | "text" | "md" => Some(Format::PlainText), + "csv" => Some(Format::Csv), + "tsv" | "tab" => Some(Format::Tsv), "studio" | "studio3" => Some(Format::Studio), "stencilcraft" => Some(Format::StencilCraft), "gcode" | "nc" => Some(Format::Gcode), From afe51ed11e1f6d30ae76c4c8a8063346c597079e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:51:42 -0500 Subject: [PATCH 083/520] Probe maximum raster frame allocation bounds --- crates/image-codecs/src/allocation.rs | 194 ++++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 2 + 2 files changed, 196 insertions(+) create mode 100644 crates/image-codecs/src/allocation.rs diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs new file mode 100644 index 000000000..5425a0c24 --- /dev/null +++ b/crates/image-codecs/src/allocation.rs @@ -0,0 +1,194 @@ +//! Predecode bounds for raster containers whose frames may exceed their declared canvas. + +use crate::codecs::webp; +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, SampleType}; +use crate::options::Limits; + +/// The largest width and height any decoded frame can declare, without allocating pixel data. +/// Animated GIF and WebP frames are inspected individually, including frames larger than the +/// animation canvas. Other recognized raster formats use their container's image dimensions. +pub fn probe_allocation_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let format = crate::detect(bytes).ok_or(CodecError::UnknownFormat)?; + let (width, height) = match format { + Format::Gif => gif_bounds(bytes)?, + Format::WebP => { + let (width, height, _) = webp::probe(bytes, &Limits::default())?; + let (frame_width, frame_height) = webp_bounds(bytes)?; + (width.max(frame_width), height.max(frame_height)) + } + _ => { + if let Some(info) = crate::inspect_raster(bytes)? { + (info.width, info.height) + } else { + crate::probe_size(bytes)? + } + } + }; + Limits::default().check(width, height, ChannelLayout::Rgba, SampleType::U8)?; + Ok((width, height)) +} + +fn malformed(format: Format, message: &'static str) -> CodecError { + CodecError::malformed(format, message) +} + +fn u16_le(bytes: &[u8], at: usize) -> Option { + Some(u16::from_le_bytes(bytes.get(at..at.checked_add(2)?)?.try_into().ok()?)) +} + +fn gif_bounds(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let bad = || malformed(Format::Gif, "truncated or malformed GIF frame table"); + if !bytes.starts_with(b"GIF87a") && !bytes.starts_with(b"GIF89a") { + return Err(bad()); + } + let packed = *bytes.get(10).ok_or_else(bad)?; + let (mut width, mut height) = (u32::from(u16_le(bytes, 6).ok_or_else(bad)?), u32::from(u16_le(bytes, 8).ok_or_else(bad)?)); + let table_len = + if packed & 0x80 != 0 { 3usize.checked_mul(1usize.checked_shl(u32::from((packed & 7) + 1)).ok_or_else(bad)?).ok_or_else(bad)? } else { 0 }; + let mut at = 13usize.checked_add(table_len).ok_or_else(bad)?; + if bytes.get(..at).is_none() || width == 0 || height == 0 { + return Err(bad()); + } + let mut frames = 0usize; + loop { + match bytes.get(at).copied().ok_or_else(bad)? { + 0x3b => break, + 0x21 => { + at = at.checked_add(2).ok_or_else(bad)?; + loop { + let length = usize::from(*bytes.get(at).ok_or_else(bad)?); + at = at.checked_add(1).ok_or_else(bad)?; + if length == 0 { + break; + } + at = at.checked_add(length).ok_or_else(bad)?; + if bytes.get(..at).is_none() { + return Err(bad()); + } + } + } + 0x2c => { + let left = u32::from(u16_le(bytes, at + 1).ok_or_else(bad)?); + let top = u32::from(u16_le(bytes, at + 3).ok_or_else(bad)?); + let frame_width = u32::from(u16_le(bytes, at + 5).ok_or_else(bad)?); + let frame_height = u32::from(u16_le(bytes, at + 7).ok_or_else(bad)?); + let descriptor = bytes.get(at..at.checked_add(10).ok_or_else(bad)?).ok_or_else(bad)?; + let flags = descriptor[9]; + let mut body = at.checked_add(10).ok_or_else(bad)?; + if flags & 0x80 != 0 { + let table = 3usize.checked_mul(1usize.checked_shl(u32::from((flags & 7) + 1)).ok_or_else(bad)?).ok_or_else(bad)?; + body = body.checked_add(table).ok_or_else(bad)?; + } + body = body.checked_add(1).ok_or_else(bad)?; // LZW minimum code size. + if bytes.get(..body).is_none() || frame_width == 0 || frame_height == 0 { + return Err(bad()); + } + width = width.max(left.saturating_add(frame_width)); + height = height.max(top.saturating_add(frame_height)); + at = body; + loop { + let length = usize::from(*bytes.get(at).ok_or_else(bad)?); + at = at.checked_add(1).ok_or_else(bad)?; + if length == 0 { + break; + } + at = at.checked_add(length).ok_or_else(bad)?; + if bytes.get(..at).is_none() { + return Err(bad()); + } + } + frames = frames.saturating_add(1); + } + _ => return Err(bad()), + } + } + if frames == 0 { + return Err(bad()); + } + Ok((width, height)) +} + +fn webp_bounds(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let bad = || malformed(Format::WebP, "truncated or malformed WebP frame table"); + if bytes.get(0..4) != Some(b"RIFF") || bytes.get(8..12) != Some(b"WEBP") { + return Err(bad()); + } + let riff_len = usize::try_from(u32::from_le_bytes(bytes.get(4..8).ok_or_else(bad)?.try_into().map_err(|_| bad())?)).map_err(|_| bad())?; + let end = 8usize.checked_add(riff_len).ok_or_else(bad)?; + if end > bytes.len() || end < 12 { + return Err(bad()); + } + let mut max_width = 0u32; + let mut max_height = 0u32; + webp_chunks(bytes, 12, end, false, &mut max_width, &mut max_height)?; + if max_width == 0 || max_height == 0 { + return Err(bad()); + } + Ok((max_width, max_height)) +} + +fn webp_chunks(bytes: &[u8], mut at: usize, end: usize, nested: bool, max_width: &mut u32, max_height: &mut u32) -> Result<(), CodecError> { + let bad = || malformed(Format::WebP, "truncated or malformed WebP frame table"); + while at < end { + let header_end = at.checked_add(8).ok_or_else(bad)?; + let header = bytes.get(at..header_end).filter(|_| header_end <= end).ok_or_else(bad)?; + let size = usize::try_from(u32::from_le_bytes(header.get(4..8).ok_or_else(bad)?.try_into().map_err(|_| bad())?)).map_err(|_| bad())?; + let from = header_end; + let to = from.checked_add(size).ok_or_else(bad)?; + let payload = bytes.get(from..to).filter(|_| to <= end).ok_or_else(bad)?; + match header.get(..4).ok_or_else(bad)? { + b"VP8 " => { + if payload.get(3..6) != Some(&[0x9d, 0x01, 0x2a]) || payload.len() < 10 { + return Err(bad()); + } + let w = u32::from(u16::from_le_bytes([payload[6], payload[7]]) & 0x3fff); + let h = u32::from(u16::from_le_bytes([payload[8], payload[9]]) & 0x3fff); + *max_width = (*max_width).max(w); + *max_height = (*max_height).max(h); + } + b"VP8L" => { + if payload.len() < 5 || payload[0] != 0x2f { + return Err(bad()); + } + let bits = u32::from_le_bytes([payload[1], payload[2], payload[3], payload[4]]); + *max_width = (*max_width).max((bits & 0x3fff) + 1); + *max_height = (*max_height).max(((bits >> 14) & 0x3fff) + 1); + } + b"VP8X" => { + if nested || payload.len() < 10 { + return Err(bad()); + } + *max_width = (*max_width).max(read_le24(payload, 4).ok_or_else(bad)?.saturating_add(1)); + *max_height = (*max_height).max(read_le24(payload, 7).ok_or_else(bad)?.saturating_add(1)); + } + b"ANMF" => { + if nested || payload.len() < 16 { + return Err(bad()); + } + let x = read_le24(payload, 0).ok_or_else(bad)?.saturating_mul(2); + let y = read_le24(payload, 3).ok_or_else(bad)?.saturating_mul(2); + let w = read_le24(payload, 6).ok_or_else(bad)?.saturating_add(1); + let h = read_le24(payload, 9).ok_or_else(bad)?.saturating_add(1); + *max_width = (*max_width).max(x.saturating_add(w)); + *max_height = (*max_height).max(y.saturating_add(h)); + webp_chunks(payload, 16, payload.len(), true, max_width, max_height)?; + } + _ => {} + } + at = to.checked_add(size & 1).ok_or_else(bad)?; + if at > end { + return Err(bad()); + } + } + if at != end { + return Err(bad()); + } + Ok(()) +} + +fn read_le24(bytes: &[u8], at: usize) -> Option { + let b = bytes.get(at..at.checked_add(3)?)?; + Some(u32::from_le_bytes([b[0], b[1], b[2], 0])) +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 865e59865..e02c438ec 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -14,6 +14,7 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +mod allocation; pub mod cicp; mod codecs; mod error; @@ -29,6 +30,7 @@ pub mod orientation; pub mod resolution; pub mod source_settings; pub mod web; +pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; pub use crate::codecs::exr_cryptomatte::{cryptomatte_id, cryptomatte_key, cryptomatte_preview_color}; pub use crate::codecs::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; From 644e613b06ef267eed75b6fbdccfddc7217d599f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 06:52:59 +0000 Subject: [PATCH 084/520] feat(figcraft-format): expose native member sizes --- crates/figcraft-format/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index 08b9bcb9a..df61cd065 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -139,6 +139,14 @@ impl<'a> Archive<'a> { self.inner.contains(name) } + pub fn declared_size(&mut self, name: &str) -> Option { + self.inner.declared_size(name) + } + + pub fn compressed_size(&mut self, name: &str) -> Option { + self.inner.compressed_size(name) + } + pub fn read(&mut self, name: &str, max_bytes: u64) -> Result>, craft_zip::Error> { self.inner.read(name, max_bytes) } From 8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:53:37 -0500 Subject: [PATCH 085/520] Classify legacy Excel extensions in shared registry --- crates/file-formats/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index a570f0af4..7859bf539 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -47,6 +47,8 @@ pub enum Format { PlainText, Csv, Tsv, + /// Legacy binary Excel workbooks, identified by extension only. + LegacyExcel, Gcode, Studio, StencilCraft, @@ -218,6 +220,7 @@ pub fn from_extension(ext: &str) -> Option { "txt" | "text" | "md" => Some(Format::PlainText), "csv" => Some(Format::Csv), "tsv" | "tab" => Some(Format::Tsv), + "xls" | "xlsm" => Some(Format::LegacyExcel), "studio" | "studio3" => Some(Format::Studio), "stencilcraft" => Some(Format::StencilCraft), "gcode" | "nc" => Some(Format::Gcode), From f8acbd5832f661f7ab53628b007d8a431d5b7a7e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 06:55:26 +0000 Subject: [PATCH 086/520] feat(figcraft-format): expose archive compression modes --- crates/figcraft-format/src/lib.rs | 3 +++ crates/zip/src/write.rs | 21 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index df61cd065..19e5f1175 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -253,3 +253,6 @@ mod streaming_writer_tests { assert_eq!(value["format"], FORMAT); } } + +/// ZIP compression modes for native archive members. +pub use craft_zip::Compression; diff --git a/crates/zip/src/write.rs b/crates/zip/src/write.rs index 12838a3b8..2b32e1e74 100644 --- a/crates/zip/src/write.rs +++ b/crates/zip/src/write.rs @@ -35,7 +35,19 @@ impl Writer { /// Adds an entry from a byte slice. A name the archive already has is an error. pub fn add(&mut self, name: &str, data: &[u8], compression: Compression) -> Result<(), Error> { - self.add_with(name, compression, |writer| writer.write_all(data)) + let (method, level) = match compression { + Compression::Stored => (zip::CompressionMethod::Stored, None), + Compression::Deflated => (zip::CompressionMethod::Deflated, Some(6)), + Compression::Level(l) => (zip::CompressionMethod::Deflated, Some(i64::from(l.min(9)))), + }; + let options = zip::write::SimpleFileOptions::default() + .compression_method(method) + .compression_level(level) + .last_modified_time(zip::DateTime::default()) + .large_file(data.len() as u64 >= u64::from(u32::MAX) / 2); + let fail = |e: &dyn std::fmt::Display| Error::Write(format!("{name}: {e}")); + self.zip.start_file(name, options).map_err(|e| fail(&e))?; + self.zip.write_all(data).map_err(|e| fail(&e)) } /// Adds an entry by streaming it into the compressor, so large generated entries need not @@ -46,8 +58,11 @@ impl Writer { Compression::Deflated => (zip::CompressionMethod::Deflated, Some(6)), Compression::Level(l) => (zip::CompressionMethod::Deflated, Some(i64::from(l.min(9)))), }; - let options = - zip::write::SimpleFileOptions::default().compression_method(method).compression_level(level).last_modified_time(zip::DateTime::default()); + let options = zip::write::SimpleFileOptions::default() + .compression_method(method) + .compression_level(level) + .last_modified_time(zip::DateTime::default()) + .large_file(true); let fail = |e: &dyn std::fmt::Display| Error::Write(format!("{name}: {e}")); self.zip.start_file(name, options).map_err(|e| fail(&e))?; write(&mut self.zip).map_err(|e| fail(&e)) From d6f65e29e024ab8ce788d89247ae6907b7f7838b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 06:58:38 +0000 Subject: [PATCH 087/520] feat(figcraft-format): support seekable native readers --- Cargo.lock | 1 + crates/figcraft-format/Cargo.toml | 1 + crates/figcraft-format/src/lib.rs | 106 ++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fda532570..d0e9f1e6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1453,6 +1453,7 @@ dependencies = [ "serde", "serde_json", "thiserror 2.0.21", + "zip", ] [[package]] diff --git a/crates/figcraft-format/Cargo.toml b/crates/figcraft-format/Cargo.toml index 22682034b..009e40c01 100644 --- a/crates/figcraft-format/Cargo.toml +++ b/crates/figcraft-format/Cargo.toml @@ -14,4 +14,5 @@ workspace = true serde = { workspace = true } serde_json = { workspace = true } craft-zip = { workspace = true } +zip = { version = "9", default-features = false, features = ["deflate-flate2"] } thiserror = { workspace = true } diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index 19e5f1175..9e973475e 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -256,3 +256,109 @@ mod streaming_writer_tests { /// ZIP compression modes for native archive members. pub use craft_zip::Compression; + +/// A native ZIP reader for seekable sources that should not be copied into memory first. +pub struct SeekArchive { + inner: zip::ZipArchive, +} + +impl SeekArchive { + pub fn open(reader: R, max_entries: u64) -> Result { + let inner = zip::ZipArchive::new(reader).map_err(|e| craft_zip::Error::NotZip(e.to_string()))?; + let count = inner.len() as u64; + if count > max_entries { + return Err(craft_zip::Error::TooManyEntries { declared: count, limit: max_entries }); + } + Ok(Self { inner }) + } + + pub fn contains(&self, name: &str) -> bool { + self.inner.index_for_name(name).is_some() + } + + pub fn names(&self) -> Vec { + self.inner.file_names().filter_map(Result::ok).map(|n| n.into_owned()).collect() + } + + pub fn declared_size(&mut self, name: &str) -> Option { + self.inner.index_for_name(name).and_then(|i| self.inner.by_index_raw(i).ok().map(|f| f.size())) + } + + pub fn read(&mut self, name: &str, max_bytes: u64) -> Result>, craft_zip::Error> { + let Some(index) = self.inner.index_for_name(name) else { return Ok(None) }; + let mut file = self.inner.by_index(index).map_err(|e| craft_zip::Error::Entry { name: name.to_owned(), msg: e.to_string() })?; + if file.size() > max_bytes { + return Err(craft_zip::Error::EntryTooLarge { name: name.to_owned(), limit: max_bytes }); + } + let mut bytes = Vec::new(); + let mut limited = std::io::Read::take(&mut file, max_bytes.saturating_add(1)); + let read = std::io::Read::read_to_end(&mut limited, &mut bytes); + if let Err(error) = read { + return Err(craft_zip::Error::Entry { name: name.to_owned(), msg: error.to_string() }); + } + if bytes.len() as u64 > max_bytes { + return Err(craft_zip::Error::EntryTooLarge { name: name.to_owned(), limit: max_bytes }); + } + Ok(Some(bytes)) + } +} + +/// Shared access contract implemented by in-memory and seekable native archive readers. +pub trait NativeArchive { + fn contains(&self, name: &str) -> bool; + fn names(&self) -> Vec; + fn declared_size(&mut self, name: &str) -> Option; + fn compressed_size(&mut self, name: &str) -> Option; + fn read_member(&mut self, name: &str, max_bytes: u64) -> Result>, craft_zip::Error>; +} + +impl NativeArchive for Archive<'_> { + fn contains(&self, name: &str) -> bool { + Archive::contains(self, name) + } + fn names(&self) -> Vec { + Archive::names(self).map(|n| n.into_owned()).collect() + } + fn declared_size(&mut self, name: &str) -> Option { + Archive::declared_size(self, name) + } + fn compressed_size(&mut self, name: &str) -> Option { + Archive::compressed_size(self, name) + } + fn read_member(&mut self, name: &str, max_bytes: u64) -> Result>, craft_zip::Error> { + Archive::read(self, name, max_bytes) + } +} + +impl NativeArchive for SeekArchive { + fn contains(&self, name: &str) -> bool { + SeekArchive::contains(self, name) + } + fn names(&self) -> Vec { + SeekArchive::names(self) + } + fn declared_size(&mut self, name: &str) -> Option { + SeekArchive::declared_size(self, name) + } + fn compressed_size(&mut self, name: &str) -> Option { + self.inner.index_for_name(name).and_then(|i| self.inner.by_index_raw(i).ok().map(|f| f.compressed_size())) + } + fn read_member(&mut self, name: &str, max_bytes: u64) -> Result>, craft_zip::Error> { + SeekArchive::read(self, name, max_bytes) + } +} + +#[cfg(test)] +mod seek_archive_tests { + use super::*; + + #[test] + fn seekable_reader_matches_bounded_in_memory_reader() { + let bytes = write_archive([Member { name: "document.json", bytes: b"payload", compression: Compression::Stored }]).unwrap(); + let mut reader = SeekArchive::open(std::io::Cursor::new(bytes), 4).unwrap(); + assert!(reader.contains("document.json")); + assert_eq!(reader.read_member("document.json", 16).unwrap(), Some(b"payload".to_vec())); + assert_eq!(reader.declared_size("document.json"), Some(7)); + assert!(reader.read_member("document.json", 3).is_err()); + } +} From e8628c526ea74ae09144508f666bf6dd92f18029 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:02:49 +0000 Subject: [PATCH 088/520] feat(figcraft-format): own native archive policy --- crates/figcraft-format/src/lib.rs | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index 9e973475e..a06b39204 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -362,3 +362,60 @@ mod seek_archive_tests { assert!(reader.read_member("document.json", 3).is_err()); } } + +/// Largest individual ZIP member read by the native format. +pub const MAX_MEMBER: u64 = 512 * 1024 * 1024; +/// Most version snapshots kept on save and read on open. +pub const MAX_VERSIONS: usize = 100; +/// Maximum version and branch name lengths, in Unicode scalar values. +pub const MAX_VERSION_NAME: usize = 200; +pub const MAX_VERSION_DESCRIPTION: usize = 2000; +/// Maximum bytes of an index and all version snapshots read. +pub const MAX_VERSION_INDEX: u64 = 16 * 1024 * 1024; +pub const MAX_VERSION_BYTES: u64 = 1024 * 1024 * 1024; +/// Maximum images read and maximum combined image bytes. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ImageBudget { + pub images: usize, + pub bytes: u64, +} +pub const IMAGE_BUDGET: ImageBudget = ImageBudget { images: 20_000, bytes: 4 * 1024 * 1024 * 1024 }; +/// Maximum branch histories kept. +pub const MAX_BRANCHES: usize = 100; +/// The branch index member. +pub const BRANCH_INDEX: &str = "branches/index.json"; +/// Maximum uncompressed bytes read from one native archive in total. +#[cfg(not(target_pointer_width = "32"))] +pub const MAX_UNPACKED: u64 = 6 * 1024 * 1024 * 1024; +#[cfg(target_pointer_width = "32")] +pub const MAX_UNPACKED: u64 = 2 * 1024 * 1024 * 1024; + +/// Native-format errors shared with the consumer's document mapping. +#[derive(Debug, PartialEq, Eq)] +pub enum FormatError { + Io(String), + Corrupt(String), + Newer(u32), +} + +impl std::fmt::Display for FormatError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(e) => write!(f, "{e}"), + Self::Corrupt(e) => write!(f, "not a valid FigCraft file: {e}"), + Self::Newer(v) => write!(f, "this file was saved by a newer FigCraft (format version {v})"), + } + } +} + +impl std::error::Error for FormatError {} + +/// Whether `id` can name a version or branch: 1–64 ASCII letters, digits, `-` and `_`. +pub fn is_valid_version_id(id: &str) -> bool { + (1..=64).contains(&id.len()) && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_') +} + +/// Clip text to at most `max` Unicode scalar values. +pub fn clip(s: &str, max: usize) -> String { + s.chars().take(max).collect() +} From c5d1e937aeb88d24f1f221a7c3350ac265e2823c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:03:31 -0500 Subject: [PATCH 089/520] feat(materialx): add bounded reader and OpenPBR writer (#1167) * feat(materialx): add bounded document reader and OpenPBR writer * proof, video, latex, cms: satisfy the clippy lints that ship with Rust 1.100 The newer clippy flags needless bool guards and range loops whose index only indexes one slice; express both with the equivalent iterators so the workspace lint stays green on every platform. * cms: index the ink loops instead of a range loop, which newer clippy rejects * feat(materialx): resolve shader graphs and write OpenPBR materials * feat(materialx): resolve shader graphs and write OpenPBR materials --- Cargo.lock | 8 + Cargo.toml | 1 + crates/materialx/Cargo.toml | 15 + crates/materialx/src/lib.rs | 593 ++++++++++++++++++++++++++++++++++++ 4 files changed, 617 insertions(+) create mode 100644 crates/materialx/Cargo.toml create mode 100644 crates/materialx/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index cf0e0bd47..c8c391f08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1704,6 +1704,14 @@ dependencies = [ "craft-wordmodel", ] +[[package]] +name = "craft-materialx" +version = "0.1.0" +dependencies = [ + "craft-xml", + "thiserror 2.0.21", +] + [[package]] name = "craft-math" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 629bab64c..4867eb171 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,6 +77,7 @@ craft-latex = { path = "crates/latex" } craft-markdown = { path = "crates/markdown" } craft-math = { path = "crates/math" } craft-media = { path = "crates/media", default-features = false } +craft-materialx = { path = "crates/materialx" } craft-media-testkit = { path = "crates/media-testkit" } craft-midi = { path = "crates/midi" } craft-sf2 = { path = "crates/sf2" } diff --git a/crates/materialx/Cargo.toml b/crates/materialx/Cargo.toml new file mode 100644 index 000000000..4a1dc37d8 --- /dev/null +++ b/crates/materialx/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "craft-materialx" +description = "MaterialX document parsing and writing" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-xml = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/materialx/src/lib.rs b/crates/materialx/src/lib.rs new file mode 100644 index 000000000..d60db3eef --- /dev/null +++ b/crates/materialx/src/lib.rs @@ -0,0 +1,593 @@ +//! MaterialX XML documents and node graphs. +//! +//! This crate owns the format boundary: bounded parsing of `.mtlx` documents, ordered access to +//! their nodes and nodegraph outputs, and XML serialization. Applications map shader inputs to +//! their own material models. + +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +use std::collections::HashMap; + +use craft_xml::{Doctype, El, Error as XmlError, Limits, Options, UnknownEntity}; + +/// Maximum source document size accepted by the parser. +pub const MAX_BYTES: usize = 16 * 1024 * 1024; +/// Maximum XML nesting depth accepted by the parser. +pub const MAX_DEPTH: usize = 64; +/// Maximum XML element count accepted by the parser. +pub const MAX_NODES: usize = 200_000; + +/// An invalid or unsupported MaterialX document. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The document is not valid MaterialX XML. + #[error("invalid MaterialX document: {0}")] + Xml(#[from] XmlError), + /// The XML root element is not ``. + #[error("the XML root element is not ")] + Root, + /// The document does not contain the requested surface material. + #[error("no MaterialX material named {0:?}")] + MissingMaterial(String), + /// The document has an unsupported or incomplete shader connection. + #[error("unsupported MaterialX material: {0}")] + Unsupported(String), + /// The document's material references are malformed. + #[error("malformed MaterialX material: {0}")] + Malformed(String), +} + +/// A MaterialX document, with XML elements kept in source order. +#[derive(Clone, Debug, PartialEq)] +pub struct Document { + root: El, +} + +/// A recognized MaterialX surface shader. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Shader { + /// OpenPBR Surface. + OpenPbr, + /// Autodesk Standard Surface. + StandardSurface, + /// glTF PBR Surface. + GltfPbr, +} + +/// The value connected to a shader input. +#[derive(Clone, Debug, PartialEq)] +pub enum Value { + /// A finite numeric literal, kept in the source color space. `colorspace` is the input's + /// explicit tag; document-level defaults remain available on [`Document::root`]. + Constant { values: Vec, colorspace: Option }, + /// A file path from an image node, with the effective file prefix applied. + Image(String), +} + +/// A surface material resolved from a MaterialX document. +#[derive(Clone, Debug, PartialEq)] +pub struct Material { + /// Material name, or `Material` when omitted. + pub name: String, + /// Shader dialect represented by the material. + pub shader: Shader, + /// Resolved inputs in the supported shader table's deterministic order. + pub inputs: Vec<(String, Value)>, + /// Resolved displacement input, if present. + pub displacement: Option, + /// First valid image tiling value, or `[1, 1]`. + pub uv_tiling: [f32; 2], + /// Unsupported or ignored constructs encountered while resolving values. + pub warnings: Vec, +} + +#[derive(Clone, Debug)] +struct Found { + value: Value, + tiling: Option<[f32; 2]>, +} + +const OPEN_PBR_INPUTS: &[&str] = &[ + "base_color", + "base_metalness", + "specular_roughness", + "geometry_normal", + "geometry_opacity", + "emission_color", + "coat_weight", + "coat_roughness", + "geometry_coat_normal", + "fuzz_weight", + "fuzz_color", + "fuzz_roughness", + "specular_roughness_anisotropy", + "transmission_weight", + "subsurface_weight", +]; +const STANDARD_INPUTS: &[&str] = &[ + "base_color", + "metalness", + "specular_roughness", + "normal", + "opacity", + "emission_color", + "coat", + "coat_roughness", + "coat_normal", + "sheen", + "sheen_color", + "sheen_roughness", + "specular_anisotropy", + "transmission", + "subsurface", +]; +const GLTF_INPUTS: &[&str] = &[ + "base_color", + "metallic", + "roughness", + "normal", + "alpha", + "emission", + "occlusion", + "clearcoat", + "clearcoat_roughness", + "clearcoat_normal", + "sheen_color", + "sheen_roughness", + "transmission", +]; + +/// OpenPBR input kind accepted by [`write_open_pbr`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum InputKind { + /// Color value or color texture. + Color, + /// Scalar value or data texture. + Float, + /// Vector normal texture. + Normal, + /// Displacement map, serialized as the MaterialX displacement shader input. + Height, +} + +/// A texture connected to an OpenPBR input. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Texture { + /// MaterialX/OpenPBR input name, such as `base_color` or `geometry_normal`. + pub input: String, + /// Texture filename relative to the MaterialX document. + pub file: String, + /// Color space used for color inputs; data maps ignore this field. + pub colorspace: String, +} + +const OPENPBR_WRITABLE: &[(&str, InputKind)] = &[ + ("base_color", InputKind::Color), + ("base_metalness", InputKind::Float), + ("specular_roughness", InputKind::Float), + ("specular_roughness_anisotropy", InputKind::Float), + ("coat_weight", InputKind::Float), + ("coat_roughness", InputKind::Float), + ("fuzz_weight", InputKind::Float), + ("fuzz_color", InputKind::Color), + ("fuzz_roughness", InputKind::Float), + ("transmission_weight", InputKind::Float), + ("subsurface_weight", InputKind::Float), + ("emission_color", InputKind::Color), + ("geometry_opacity", InputKind::Float), + ("geometry_normal", InputKind::Normal), + ("height", InputKind::Height), +]; + +impl Document { + /// Parses a MaterialX document while enforcing size, nesting and node-count limits. + pub fn parse(bytes: &[u8]) -> Result { + let limits = Limits { max_input: MAX_BYTES, max_depth: MAX_DEPTH, max_nodes: MAX_NODES, max_attrs: 256, max_attrs_total: 400_000 }; + let options = Options { + doctype: Doctype::Reject, + unknown_entity: UnknownEntity::Error, + drop_blank_text: true, + names: craft_xml::Names::Local, + ns_decls: false, + ..Options::default() + } + .strict() + .with_limits(limits); + let root = options.parse(bytes)?; + if root.name.rsplit(':').next() != Some("materialx") { + return Err(Error::Root); + } + Ok(Document { root }) + } + + /// Creates a document from a `` element. + pub fn from_root(root: El) -> Result { + if root.name.rsplit(':').next() != Some("materialx") { + return Err(Error::Root); + } + Ok(Document { root }) + } + + /// The root `` element. + pub fn root(&self) -> &El { + &self.root + } + + /// The root `` element, for application-specific editing. + pub fn root_mut(&mut self) -> &mut El { + &mut self.root + } + + /// Serializes the document as XML, preserving element and attribute order. + pub fn to_xml(&self) -> String { + format!("{}{}", craft_xml::DECL, self.root.to_xml_pretty(" ")) + } + + /// Nodegraphs in document order. + pub fn nodegraphs(&self) -> impl Iterator { + self.root.children("nodegraph") + } + + /// Outputs in a named nodegraph, in document order. + pub fn outputs(&self, graph: &str) -> impl Iterator { + self.nodegraphs().filter(move |g| g.attr("name") == Some(graph)).flat_map(|g| g.children("output")) + } + + /// The output of a named graph. An omitted output name selects the first output in source + /// order. For duplicate output names, the last definition wins, matching the consumer's + /// existing named lookup behavior. + pub fn output(&self, graph: &str, output: Option<&str>) -> Option<&El> { + match output { + Some(name) => self.outputs(graph).filter(|node| node.attr("name") == Some(name)).last(), + None => self.outputs(graph).next(), + } + } + + /// Resolves a surface material into shader inputs, image paths and warnings. + pub fn read_material(&self, material: Option<&str>) -> Result { + let materials: Vec<&El> = self.root.children("surfacematerial").collect(); + let mat = match material { + Some(name) => materials.iter().copied().find(|m| m.attr("name") == Some(name)).ok_or_else(|| Error::MissingMaterial(name.to_owned()))?, + None => materials.first().copied().ok_or_else(|| Error::Unsupported("document has no surfacematerial".into()))?, + }; + let mut nodes = HashMap::new(); + let mut outputs = Vec::new(); + for child in self.root.elements() { + if child.name == "nodegraph" { + let Some(graph) = child.attr("name") else { continue }; + for node in child.elements() { + let Some(name) = node.attr("name") else { continue }; + if node.name == "output" { + if let Some(from) = node.attr("nodename") { + outputs.push((format!("{graph}/{name}"), format!("{graph}/{from}"))); + } + } else { + nodes.insert(format!("{graph}/{name}"), node); + } + } + } else if let Some(name) = child.attr("name") { + nodes.insert(name.to_owned(), child); + } + } + let shader_name = mat.children("input").find(|i| i.attr("name") == Some("surfaceshader")).and_then(|i| i.attr("nodename")); + let shader = shader_name.and_then(|n| nodes.get(n).copied()).ok_or_else(|| Error::Malformed("material has no surface shader".into()))?; + let (kind, inputs) = match shader.name.as_str() { + "open_pbr_surface" => (Shader::OpenPbr, OPEN_PBR_INPUTS), + "standard_surface" => (Shader::StandardSurface, STANDARD_INPUTS), + "gltf_pbr" => (Shader::GltfPbr, GLTF_INPUTS), + other => return Err(Error::Unsupported(format!("surface shader is {other}"))), + }; + let emission_gate = match kind { + Shader::OpenPbr => Some(("emission_luminance", true)), + Shader::StandardSurface => Some(("emission", true)), + Shader::GltfPbr => None, + }; + let emission_enabled = emission_gate.is_none_or(|(name, _)| { + shader + .children("input") + .find(|i| i.attr("name") == Some(name)) + .is_some_and(|i| i.attr("value").is_none_or(|v| parse_numbers(v).and_then(|n| n.first().copied()).is_some_and(|v| v > 0.0))) + }); + let mut warnings = Vec::new(); + let base_weight_name = match kind { + Shader::OpenPbr => Some("base_weight"), + Shader::StandardSurface => Some("base"), + Shader::GltfPbr => None, + }; + if base_weight_name.and_then(|n| numeric_input(shader, n)).is_some_and(|w| (w - 1.0).abs() > 1e-6) { + warnings.push("the base weight isn't 1; it was ignored".into()); + } + let mut resolved = Vec::new(); + let mut tiling = None; + for name in inputs { + let Some(input) = shader.children("input").find(|i| i.attr("name") == Some(*name)) else { continue }; + let is_emission = matches!((kind, *name), (Shader::OpenPbr, "emission_color") | (Shader::StandardSurface, "emission_color")); + if is_emission && !emission_enabled { + warnings.push(format!("{name} was skipped: the shader's emission is 0")); + continue; + } + if let Some(found) = resolve_value(&self.root, input, None, &nodes, &outputs, 0, &mut warnings) { + if tiling.is_none() { + tiling = found.tiling; + } + resolved.push(((*name).to_owned(), found.value)); + } + } + let displacement = mat + .children("input") + .find(|i| i.attr("name") == Some("displacementshader")) + .and_then(|i| i.attr("nodename")) + .and_then(|n| nodes.get(n).copied()) + .and_then(|d| d.children("input").find(|i| i.attr("name") == Some("displacement"))) + .and_then(|i| resolve_value(&self.root, i, None, &nodes, &outputs, 0, &mut warnings).map(|f| f.value)); + Ok(Material { + name: mat.attr("name").unwrap_or("Material").to_owned(), + shader: kind, + inputs: resolved, + displacement, + uv_tiling: tiling.unwrap_or([1.0, 1.0]), + warnings, + }) + } +} + +fn numeric_input(shader: &El, name: &str) -> Option { + shader + .children("input") + .find(|i| i.attr("name") == Some(name)) + .and_then(|i| i.attr("value")) + .and_then(parse_numbers) + .and_then(|n| n.first().copied()) +} + +fn parse_numbers(text: &str) -> Option> { + let nums: Option> = text.split(',').map(|part| part.trim().parse::().ok().filter(|v| v.is_finite())).collect(); + nums.filter(|v| !v.is_empty() && v.len() <= 4) +} + +fn resolve_value( + root: &El, + input: &El, + scope: Option<&str>, + nodes: &HashMap, + outputs: &[(String, String)], + hops: usize, + warnings: &mut Vec, +) -> Option { + if hops > 16 { + warnings.push("a node chain is longer than 16 nodes (or loops); skipped".into()); + return None; + } + if let Some(value) = input.attr("value") { + return parse_numbers(value) + .map(|values| Found { value: Value::Constant { values, colorspace: input.attr("colorspace").map(str::to_owned) }, tiling: None }); + } + let target = if let Some(graph) = input.attr("nodegraph") { + let selected = match input.attr("output") { + Some(output) => outputs.iter().rev().find(|(k, _)| k == &format!("{graph}/{output}")), + None => outputs.iter().find(|(k, _)| k.starts_with(&format!("{graph}/"))), + }; + selected.map(|(_, node)| node.clone()) + } else if let Some(name) = input.attr("nodename") { + let qualified = scope.map(|g| format!("{g}/{name}")); + qualified.filter(|q| nodes.contains_key(q)).or_else(|| Some(name.to_owned())) + } else { + None + }?; + let node = nodes.get(&target).copied()?; + let node_scope = target.split_once('/').map(|(g, _)| g); + match node.name.as_str() { + "image" | "tiledimage" => { + let file = node.children("input").find(|i| i.attr("name") == Some("file")).and_then(|i| i.attr("value"))?; + let prefix = node + .children("input") + .find(|i| i.attr("name") == Some("file")) + .and_then(|i| i.attr("fileprefix")) + .or_else(|| node.attr("fileprefix")) + .or_else(|| root.attr("fileprefix")) + .unwrap_or(""); + let uv = node + .children("input") + .find(|i| i.attr("name") == Some("uvtiling")) + .and_then(|i| i.attr("value")) + .and_then(parse_numbers) + .filter(|n| n.len() == 2 && n.iter().all(|v| *v > 0.0)) + .map(|n| [n[0].clamp(0.001, 1000.0), n[1].clamp(0.001, 1000.0)]); + Some(Found { value: Value::Image(format!("{prefix}{file}")), tiling: uv }) + } + "normalmap" | "convert" => node + .children("input") + .find(|i| i.attr("name") == Some("in")) + .and_then(|i| resolve_value(root, i, node_scope, nodes, outputs, hops + 1, warnings)), + "constant" => node + .children("input") + .find(|i| i.attr("name") == Some("value")) + .and_then(|i| resolve_value(root, i, node_scope, nodes, outputs, hops + 1, warnings)), + other => { + warnings.push(format!("a {other} node ({:?}) is not supported; its input was skipped", node.attr("name").unwrap_or(""))); + None + } + } +} + +/// Writes an OpenPBR MaterialX document for the supplied textures. +/// +/// Unknown inputs and duplicate inputs are skipped; the first texture for an input wins. Color +/// spaces are written for color textures only, while data textures are left linear. +pub fn write_open_pbr(name: &str, textures: &[Texture], physical_size_cm: Option<[f32; 2]>) -> String { + let base = node_name(name); + let mut seen = Vec::new(); + let selected: Vec<(&Texture, InputKind)> = textures + .iter() + .filter_map(|t| OPENPBR_WRITABLE.iter().find(|(input, _)| *input == t.input).map(|(_, kind)| (t, *kind))) + .filter(|(texture, _)| { + if seen.iter().any(|name: &&str| *name == texture.input) { + false + } else { + seen.push(texture.input.as_str()); + true + } + }) + .collect(); + let has = |input: &str| selected.iter().any(|(texture, _)| texture.input == input); + let mut root = El::new("materialx").with_attr("version", "1.39").with_attr("colorspace", "lin_rec709"); + let shader_name = format!("OPBR_{base}"); + let mut shader = El::new("open_pbr_surface").with_attr("name", &shader_name).with_attr("type", "surfaceshader"); + for (texture, kind) in &selected { + let (ty, node) = match kind { + InputKind::Color => ("color3", image_node_name(&texture.input)), + InputKind::Float => ("float", image_node_name(&texture.input)), + InputKind::Normal => ("vector3", "normalmap".to_owned()), + InputKind::Height => continue, + }; + shader.push(El::new("input").with_attr("name", &texture.input).with_attr("type", ty).with_attr("nodename", node)); + } + if has("emission_color") { + shader.push(El::new("input").with_attr("name", "emission_luminance").with_attr("type", "float").with_attr("value", "1")); + } + root.push(shader); + for (texture, kind) in &selected { + let (ty, color) = match kind { + InputKind::Color => ("color3", Some(texture.colorspace.as_str())), + InputKind::Normal => ("vector3", None), + InputKind::Float | InputKind::Height => ("float", None), + }; + let mut image = El::new("tiledimage").with_attr("name", image_node_name(&texture.input)).with_attr("type", ty); + let mut file = El::new("input").with_attr("name", "file").with_attr("type", "filename").with_attr("value", &texture.file); + if let Some(space) = color { + file.set_attr("colorspace", space); + } + image.push(file); + root.push(image); + } + if has("geometry_normal") { + root.push(El::new("normalmap").with_attr("name", "normalmap").with_attr("type", "vector3").with_child( + El::new("input").with_attr("name", "in").with_attr("type", "vector3").with_attr("nodename", image_node_name("geometry_normal")), + )); + } + if has("height") { + root.push( + El::new("displacement") + .with_attr("name", format!("DISP_{base}")) + .with_attr("type", "displacementshader") + .with_child( + El::new("input").with_attr("name", "displacement").with_attr("type", "float").with_attr("nodename", image_node_name("height")), + ) + .with_child(El::new("input").with_attr("name", "scale").with_attr("type", "float").with_attr("value", "1")), + ); + } + let mut material = El::new("surfacematerial").with_attr("name", &base).with_attr("type", "material"); + if let Some([width, height]) = physical_size_cm.filter(|[w, h]| w.is_finite() && h.is_finite()) { + material.set_attr("doc", format!("Tiles every {width} x {height} cm")); + } + material.push(El::new("input").with_attr("name", "surfaceshader").with_attr("type", "surfaceshader").with_attr("nodename", shader_name)); + if has("height") { + material.push( + El::new("input") + .with_attr("name", "displacementshader") + .with_attr("type", "displacementshader") + .with_attr("nodename", format!("DISP_{base}")), + ); + } + root.push(material); + format!("{}{}", craft_xml::DECL, root.to_xml_pretty(" ")) +} + +fn image_node_name(input: &str) -> String { + format!("image_{}", node_name(input)) +} + +fn node_name(name: &str) -> String { + let mut out: String = name.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' }).collect(); + if out.is_empty() || out.starts_with(|c: char| c.is_ascii_digit()) { + out.insert(0, '_'); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_and_serializes_a_materialx_document() { + let doc = Document::parse(br#""#).unwrap(); + assert_eq!(doc.root().attr("version"), Some("1.39")); + let roundtrip = Document::parse(doc.to_xml().as_bytes()).unwrap(); + assert_eq!(roundtrip, doc); + } + + #[test] + fn graph_outputs_keep_source_order_and_named_selection() { + let doc = Document::parse( + br#""#, + ) + .unwrap(); + assert_eq!(doc.output("g", None).and_then(|o| o.attr("name")), Some("second")); + assert_eq!(doc.output("g", Some("first")).and_then(|o| o.attr("nodename")), Some("a")); + } + + #[test] + fn duplicate_named_outputs_keep_last_definition() { + let doc = Document::parse( + br#""#, + ) + .unwrap(); + assert_eq!(doc.output("g", None).and_then(|o| o.attr("nodename")), Some("first")); + assert_eq!(doc.output("g", Some("o")).and_then(|o| o.attr("nodename")), Some("last")); + } + + #[test] + fn rejects_wrong_root_doctypes_and_oversized_documents() { + assert_eq!(Document::parse(b"").err(), Some(Error::Root)); + assert!(Document::parse(b"]>").is_err()); + assert!(matches!(Document::parse(&vec![b' '; MAX_BYTES + 1]), Err(Error::Xml(XmlError::TooLarge(_))))); + } + + #[test] + fn resolves_openpbr_values_images_graphs_and_displacement() { + let doc = Document::parse(br#" + + + + + + + + + + + + "#).unwrap(); + let material = doc.read_material(None).unwrap(); + assert_eq!(material.name, "M"); + assert_eq!(material.shader, Shader::OpenPbr); + assert_eq!(material.inputs[0], ("base_color".into(), Value::Image("tex/albedo.png".into()))); + assert_eq!(material.inputs[1], ("geometry_normal".into(), Value::Image("tex/normal.png".into()))); + assert_eq!(material.inputs[2].1, Value::Constant { values: vec![1.0, 0.5, 0.25], colorspace: None }); + assert_eq!(material.displacement, Some(Value::Constant { values: vec![0.25], colorspace: None })); + assert_eq!(material.uv_tiling, [2.0, 4.0]); + } + + #[test] + fn openpbr_writer_emits_texture_connections_and_roundtrips() { + let textures = vec![ + Texture { input: "base_color".into(), file: "albedo<&.png".into(), colorspace: "srgb_texture".into() }, + Texture { input: "specular_roughness".into(), file: "rough.png".into(), colorspace: "ignored".into() }, + Texture { input: "geometry_normal".into(), file: "normal.png".into(), colorspace: "ignored".into() }, + Texture { input: "height".into(), file: "height.png".into(), colorspace: "ignored".into() }, + ]; + let xml = write_open_pbr("Stone", &textures, Some([100.0, 50.0])); + let doc = Document::parse(xml.as_bytes()).unwrap(); + let material = doc.read_material(None).unwrap(); + assert_eq!(material.name, "Stone"); + assert_eq!(material.inputs[0], ("base_color".into(), Value::Image("albedo<&.png".into()))); + assert_eq!(material.inputs[1], ("specular_roughness".into(), Value::Image("rough.png".into()))); + assert_eq!(material.inputs[2], ("geometry_normal".into(), Value::Image("normal.png".into()))); + assert_eq!(material.displacement, Some(Value::Image("height.png".into()))); + assert!(xml.contains("colorspace=\"srgb_texture\"")); + assert!(xml.contains("doc=\"Tiles every 100 x 50 cm\"")); + assert!(!xml.contains("colorspace=\"ignored\"")); + } +} From 68189d1e6e1a926db4eaa2d7843f35174cb75315 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:03:35 +0000 Subject: [PATCH 090/520] feat(figcraft-format): define document expansion policy --- crates/figcraft-format/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index a06b39204..8fddd808d 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -365,6 +365,12 @@ mod seek_archive_tests { /// Largest individual ZIP member read by the native format. pub const MAX_MEMBER: u64 = 512 * 1024 * 1024; +/// The document expansion ratio that its compressed size may claim. +pub const DOCUMENT_RATIO: u64 = 16; +#[cfg(not(target_pointer_width = "32"))] +pub const MAX_DOCUMENT: u64 = 4 * 1024 * 1024 * 1024; +#[cfg(target_pointer_width = "32")] +pub const MAX_DOCUMENT: u64 = 1024 * 1024 * 1024; /// Most version snapshots kept on save and read on open. pub const MAX_VERSIONS: usize = 100; /// Maximum version and branch name lengths, in Unicode scalar values. From eeee3d08b622fe6dd8de898d2e984978ed67cb34 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:03:55 -0500 Subject: [PATCH 091/520] Add shared Base64 PNG helpers --- Cargo.lock | 1 + crates/raster-codecs/Cargo.toml | 1 + crates/raster-codecs/src/common.rs | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 4b671fb2e..94076f131 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2106,6 +2106,7 @@ dependencies = [ name = "craft-raster-codecs" version = "0.1.0" dependencies = [ + "base64 0.23.1", "image", "rayon", "thiserror 2.0.21", diff --git a/crates/raster-codecs/Cargo.toml b/crates/raster-codecs/Cargo.toml index cadf9cbd0..786e9d55c 100644 --- a/crates/raster-codecs/Cargo.toml +++ b/crates/raster-codecs/Cargo.toml @@ -11,6 +11,7 @@ repository.workspace = true workspace = true [dependencies] +base64 = "0.23" thiserror = { workspace = true } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "bmp", "gif", "tiff", "webp"] } diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index fd8c1be60..357123e7c 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -6,6 +6,7 @@ use std::io::Cursor; +use base64::{Engine as _, engine::general_purpose::STANDARD}; use image::{ImageReader, Limits as ImageLimits, RgbaImage}; use crate::CodecError; @@ -62,6 +63,29 @@ pub fn encode_png(pixels: &RgbaImage) -> Result, CodecError> { Ok(out.into_inner()) } +/// Encode an RGBA image as standard Base64 containing PNG bytes, for a document field that stores +/// an inline PNG image. +pub fn encode_png_base64(pixels: &RgbaImage) -> Result { + encode_png(pixels).map(|bytes| STANDARD.encode(bytes)) +} + +/// Decode standard Base64 containing one PNG into straight-alpha RGBA pixels. +/// +/// The encoded length is checked before Base64 allocates the decoded buffer, and the decoded PNG +/// then passes through the same file, dimension, pixel-count, and allocation limits as any other +/// raster input. +pub fn decode_png_base64(text: &str, limits: DecodeLimits) -> Result { + let max_encoded_bytes = limits.max_file_bytes.div_ceil(3).saturating_mul(4); + if text.len() > max_encoded_bytes { + return Err(CodecError::LimitExceeded(format!("{} Base64 characters exceed max {max_encoded_bytes}", text.len()))); + } + let bytes = STANDARD.decode(text).map_err(|e| CodecError::malformed("PNG Base64", e))?; + if !bytes.starts_with(b"\x89PNG\r\n\x1a\n") { + return Err(CodecError::malformed("PNG Base64", "decoded data has no PNG signature")); + } + decode_rgba(&bytes, limits) +} + /// Encode RGBA pixels as JPEG at the requested quality (1–100). pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecError> { if !(1..=100).contains(&quality) { From 56a1f2b7c44d2e9ae4c121bb4bdb76f366e9ed77 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:11:12 +0000 Subject: [PATCH 092/520] Avoid premature saturation in H264 type 1 POC --- crates/video/src/h264/decode/slice.rs | 28 ++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/crates/video/src/h264/decode/slice.rs b/crates/video/src/h264/decode/slice.rs index 61670f059..32366ccf9 100644 --- a/crates/video/src/h264/decode/slice.rs +++ b/crates/video/src/h264/decode/slice.rs @@ -475,26 +475,27 @@ impl PocState { } 1 => { let frame_num_offset = self.frame_num_offset(sh.idr, sh.frame_num, sps); - let n = sps.offset_for_ref_frame.len() as i32; - let mut abs_frame_num = if n != 0 { frame_num_offset.saturating_add(sh.frame_num as i32) } else { 0 }; + let n = sps.offset_for_ref_frame.len() as i64; + let mut abs_frame_num = if n != 0 { i64::from(frame_num_offset) + i64::from(sh.frame_num) } else { 0 }; if sh.nal_ref_idc == 0 && abs_frame_num > 0 { abs_frame_num -= 1; } - let mut expected = 0i32; + let mut expected = 0i64; if abs_frame_num > 0 { let cycle_cnt = (abs_frame_num - 1) / n; let in_cycle = (abs_frame_num - 1) % n; - let delta_cycle = sps.offset_for_ref_frame.iter().fold(0i32, |a, &o| a.saturating_add(o)); - expected = cycle_cnt.saturating_mul(delta_cycle); + let delta_cycle = sps.offset_for_ref_frame.iter().map(|&o| i64::from(o)).sum::(); + expected = cycle_cnt * delta_cycle; for &o in &sps.offset_for_ref_frame[..=in_cycle as usize] { - expected = expected.saturating_add(o); + expected += i64::from(o); } } if sh.nal_ref_idc == 0 { - expected = expected.saturating_add(sps.offset_for_non_ref_pic); + expected += i64::from(sps.offset_for_non_ref_pic); } - let top = expected.saturating_add(sh.delta_pic_order_cnt[0]); - let bottom = top.saturating_add(sps.offset_for_top_to_bottom_field).saturating_add(sh.delta_pic_order_cnt[1]); + let top = (expected + i64::from(sh.delta_pic_order_cnt[0])).clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32; + let bottom = (i64::from(top) + i64::from(sps.offset_for_top_to_bottom_field) + i64::from(sh.delta_pic_order_cnt[1])) + .clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32; Poc { top, bottom, msb: 0, frame_num_offset } } _ => { @@ -662,4 +663,13 @@ mod poc_tests { let _ = poc.frame(); state.update(&sh, &poc); } + + #[test] + fn poc_type1_cancellation_happens_before_clamping() { + let sps = sps(1); + let sh = header(0, 0, 0, [-i32::MAX, 0], 1); + let state = PocState { prev_frame_num_offset: 2, ..PocState::default() }; + let poc = state.compute(&sh, &sps); + assert_eq!(poc.top, i32::MAX); + } } From 47107f660191de2f0b425569506f4daa4faadac9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:17:30 -0500 Subject: [PATCH 093/520] craft-mesh: retain glTF texture and sampler inventory (#1151) * craft-mesh: expose unreferenced glTF texture variants * mesh: validate texture inventory image indices * Fix PR 1151 validation and workspace checks * Fix remaining Rust 1.95 workspace lint findings * Fix proof workspace Clippy warning * Fix remaining workspace Clippy findings * Fix video all-features Clippy warning * KRA: parse nested layers without recursion * Preserve KRA layer limit traversal order * Avoid expect in iterative KRA parser * OpenRaster: parse nested stacks without recursion * Plate tests honor optional font fixtures * Keep bundled font index bytes unchanged * Preserve font build license fixture bytes * ci: allow longer workspace test matrix * test: respect Windows Hunspell path behavior * ci: provision keymap fonts for all-feature tests * ci: provision PSD corpus for full workspace tests --- .gitattributes | 6 ++++++ crates/audio/src/read/native.rs | 2 +- crates/drawingml/src/chart/read.rs | 2 +- crates/keymap/src/profiles.rs | 2 +- crates/mesh/src/gltf.rs | 13 +++++++++++++ crates/mesh/src/lib.rs | 6 ++++++ crates/mesh/src/tests.rs | 5 +++++ crates/mesh/src/tests/gltf.rs | 24 ++++++++++++++++++++++++ crates/plate-test/tests/scenes.rs | 6 +++--- crates/psd/src/codepage.rs | 2 +- crates/ui/src/docking/render/preview.rs | 2 +- crates/xls/src/read.rs | 9 +++------ 12 files changed, 65 insertions(+), 14 deletions(-) diff --git a/.gitattributes b/.gitattributes index fa2751233..a854bbdb9 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,3 +8,9 @@ crates/font-store/index/indexes/*.gz binary # Copies of the vendored krilla source diff, one per patch (see vendor/krilla/README.craft.md). vendor/krilla/patches/*.patch linguist-generated=true + +# Has a pinned digest and is embedded as bytes by craft-font-store. +crates/font-store/index/sources.json -text + +# The build fixture verifies these license files are copied byte-for-byte. +crates/fonts-build/tests/fixture/craft-fonts/fonts/**/OFL.txt -text diff --git a/crates/audio/src/read/native.rs b/crates/audio/src/read/native.rs index cc0fac4c4..5502ba7e1 100644 --- a/crates/audio/src/read/native.rs +++ b/crates/audio/src/read/native.rs @@ -413,7 +413,7 @@ impl<'a> Native<'a> { let Some(next) = self.packets.next() else { // A track that declares no frames (an empty WAV or AIFF) is empty, not broken. let empty = self.frames_known && self.info.frames == 0 && self.errors == 0; - if !self.decoded && !empty && !(self.seeked && self.errors == 0) { + if !(self.decoded || empty || self.seeked && self.errors == 0) { return Err(self.no_audio("stream contains no audio")); } return Ok(None); diff --git a/crates/drawingml/src/chart/read.rs b/crates/drawingml/src/chart/read.rs index 9919433e9..38ce9f605 100644 --- a/crates/drawingml/src/chart/read.rs +++ b/crates/drawingml/src/chart/read.rs @@ -443,7 +443,7 @@ fn read_axes<'a>(c: &mut Chart, pa: &'a El, primary_val: &Option) -> (Op // A gridline whose `c:spPr/a:ln` has `a:noFill` is not drawn, though the element is there. let grid = |a: &El, k: &str| { a.child(Local(k)) - .is_some_and(|g| !g.child(Local("spPr")).and_then(|p| p.child(Local("ln"))).is_some_and(|l| l.child(Local("noFill")).is_some())) + .is_some_and(|g| g.child(Local("spPr")).and_then(|p| p.child(Local("ln"))).is_none_or(|l| l.child(Local("noFill")).is_none())) }; if c.kind == ChartType::Combo && c.series.iter().any(|s| s.secondary_axis) { let primary_id = val_ax.and_then(|a| a.child(Local("axId")).and_then(|c| c.attr_local("val"))); diff --git a/crates/keymap/src/profiles.rs b/crates/keymap/src/profiles.rs index df9634db6..3db892978 100644 --- a/crates/keymap/src/profiles.rs +++ b/crates/keymap/src/profiles.rs @@ -471,7 +471,7 @@ impl Loaded { /// notes why not otherwise. fn add(&mut self, name: &str, overrides: Option) { let shown = || name.chars().take(MAX_SET_NAME).collect::(); - if !set_name(name).is_some_and(|n| n == name) { + if set_name(name).is_none_or(|n| n != name) { self.note(SetProblem::BadName { name: shown() }); } else if self.sets.contains_key(name) { self.note(SetProblem::Duplicate { name: shown() }); diff --git a/crates/mesh/src/gltf.rs b/crates/mesh/src/gltf.rs index 44477ddfb..9d45a4453 100644 --- a/crates/mesh/src/gltf.rs +++ b/crates/mesh/src/gltf.rs @@ -563,6 +563,19 @@ fn materials(j: &Value, limits: &Limits, scene: &mut Scene) -> Result<()> { _ => Wrap::Repeat, }; let images = scene.images.len(); + let texture_list = arr(j, "textures"); + if texture_list.len() > limits.max_materials { + return Err(too_large(KIND, format!("more than {} textures", limits.max_materials))); + } + scene.texture_variants = texture_list + .iter() + .filter_map(|texture| { + let sampler = idx(texture, "sampler").and_then(|s| arr(j, "samplers").get(s)); + let webp = || texture.get("extensions").and_then(|e| e.get("EXT_texture_webp")).and_then(|e| idx(e, "source")); + let image = idx(texture, "source").or_else(webp).filter(|i| *i < images)?; + Some(TextureRef { image, uv_set: 0, wrap: [wrap(sampler, "wrapS"), wrap(sampler, "wrapT")] }) + }) + .collect(); let tref = |v: Option<&Value>| -> Option { let v = v?; let texture = arr(j, "textures").get(idx(v, "index")?)?; diff --git a/crates/mesh/src/lib.rs b/crates/mesh/src/lib.rs index c4bf90661..59870ff31 100644 --- a/crates/mesh/src/lib.rs +++ b/crates/mesh/src/lib.rs @@ -621,6 +621,9 @@ pub struct Scene { pub meshes: Vec, pub materials: Vec, pub images: Vec, + /// Texture and sampler entries in the file, including ones no material uses. Their image + /// index and wrap modes are format properties; UV sets belong to each material slot. + pub texture_variants: Vec, /// Every format fills this: a format without a hierarchy gets one root node per mesh. pub nodes: Vec, /// The top-level nodes. @@ -684,6 +687,9 @@ impl Scene { return bad(format!("material `{}` names an image that does not exist", m.name)); } } + if self.texture_variants.iter().any(|t| t.image >= self.images.len()) { + return bad("a texture names an image that does not exist".into()); + } let n = self.nodes.len(); for node in &self.nodes { let refs = diff --git a/crates/mesh/src/tests.rs b/crates/mesh/src/tests.rs index 587ef4db1..160b5b3dc 100644 --- a/crates/mesh/src/tests.rs +++ b/crates/mesh/src/tests.rs @@ -79,6 +79,11 @@ fn validate_catches_what_writers_cannot_take() { s.validate().unwrap(); s.meshes[0].parts[0].material = Some(0); assert!(s.validate().is_err()); + let mut s = Scene::default(); + s.texture_variants.push(TextureRef::default()); + assert!(s.validate().is_err(), "a texture references a missing image"); + s.images.push(Image::default()); + s.validate().unwrap(); let mut s = Scene::from_meshes(vec![tri_mesh()], vec![]); s.nodes[0].children.push(7); assert!(s.validate().is_err()); diff --git a/crates/mesh/src/tests/gltf.rs b/crates/mesh/src/tests/gltf.rs index 011f4d75e..d35b63d57 100644 --- a/crates/mesh/src/tests/gltf.rs +++ b/crates/mesh/src/tests/gltf.rs @@ -194,6 +194,30 @@ fn embedded_and_external_resources() { assert!(parse(&gltf_text("data:;base64,!!!", "x.png").0).is_err()); } +#[test] +fn texture_and_sampler_inventory_includes_unreferenced_entries() { + let doc = serde_json::to_vec(&json!({ + "asset": {"version": "2.0"}, + "images": [{"uri": "image.png"}], + "samplers": [{"wrapS": 33071, "wrapT": 33648}], + "textures": [ + {"source": 0, "sampler": 0}, + {"source": 0} + ] + })) + .unwrap(); + let scene = read(&doc, &Limits::default(), &mut |_| Some(vec![])).unwrap(); + assert_eq!( + scene.texture_variants, + [TextureRef { image: 0, uv_set: 0, wrap: [Wrap::Clamp, Wrap::Mirror] }, TextureRef { image: 0, uv_set: 0, wrap: [Wrap::Repeat; 2] },] + ); + assert!(scene.validate().is_ok()); + + let mut invalid = scene; + invalid.texture_variants[0].image = invalid.images.len(); + assert!(matches!(invalid.validate(), Err(Error::Malformed(_)))); +} + #[test] fn base64_and_uri_decoding() { assert_eq!(base64_decode("aGVsbG8=").unwrap(), b"hello"); diff --git a/crates/plate-test/tests/scenes.rs b/crates/plate-test/tests/scenes.rs index 0e114471e..8b23e72d8 100644 --- a/crates/plate-test/tests/scenes.rs +++ b/crates/plate-test/tests/scenes.rs @@ -8,10 +8,10 @@ fn scenes() -> Vec { scene::load_dir(&scene::scenes_dir()).unwrap() } -/// On CI (`CI` set) the workflow provisions the font, so a scene that left text out fails. -/// Local runs without the font skip text and only note it. +/// When the licensed fixture directory is configured, every scene must write its text. +/// Workflows that do not provision those fixtures can still validate scenes without text. fn text_must_be_written() -> bool { - std::env::var_os("CI").is_some_and(|v| !v.is_empty() && v != "false") + std::env::var_os(reference::FONT_DIR_ENV).is_some_and(|v| !v.is_empty()) } fn missing_font(scene: &Scene) -> String { diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index 2ef7389ee..8cfe169d1 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -242,7 +242,7 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B // never writes this one, says the writer meant that ("état", not "Žtat"). let written = |c: char| c.to_lowercase().all(|l| hint_letters.contains(&l)); let mac = name.get(i..=i).map(|b| decode(b, MACINTOSH)).and_then(|s| s.chars().next()); - word_initial && !(mac.is_some_and(|m| m.is_alphabetic() && written(m)) && !written(ch)) + word_initial && (written(ch) || !mac.is_some_and(|m| m.is_alphabetic() && written(m))) } else if enc == WINDOWS_1251 && is_cyrillic(ch) { (cyrillic_name && !lone_after_digit(&decoded, i)) || cyrillic_word(&decoded, i) } else { diff --git a/crates/ui/src/docking/render/preview.rs b/crates/ui/src/docking/render/preview.rs index b3cb62488..8cd1848b2 100644 --- a/crates/ui/src/docking/render/preview.rs +++ b/crates/ui/src/docking/render/preview.rs @@ -117,7 +117,7 @@ impl Renderer<'_, P> { // release outside the window floats. let gone = !has_pointer && !touch_ended; let floats_outside = !self.style.float_on_empty_drop && drag.gesture != GroupGesture::Window; - if !(released && !gone) && !(gone && floats_outside) { + if !(released && !gone || gone && floats_outside) { return; } let Some(pointer) = pointer.filter(|p| !self.blockers.iter().any(|r| r.contains(*p))) else { return }; diff --git a/crates/xls/src/read.rs b/crates/xls/src/read.rs index 529a36e11..8fdf5e473 100644 --- a/crates/xls/src/read.rs +++ b/crates/xls/src/read.rs @@ -598,7 +598,7 @@ fn resolve_pending( st.unsupported(TOO_MUCH); continue; } - if !st.formula_at(p.pos).is_some_and(|f| f.text.chars().count() <= MAX_SHARED_TEMPLATE) { + if st.formula_at(p.pos).is_none_or(|f| f.text.chars().count() > MAX_SHARED_TEMPLATE) { failed.insert(p.anchor); } } else { @@ -902,11 +902,8 @@ fn sheet_record(rec: &Record<'_>, st: &mut SheetState<'_>, budget: &mut Budget, report.warn(Warning::ConditionalFormattingSkipped); } }, - 0x01B1 => { - if conditional_format(&mut c, st).is_none() { - report.warn(Warning::ConditionalFormattingSkipped); - } - } + 0x01B1 if conditional_format(&mut c, st).is_none() => report.warn(Warning::ConditionalFormattingSkipped), + 0x01B1 => {} 0x0879 => report.warn(Warning::ConditionalFormatting12), // A short DVal starts no rules. 0x01B2 => st.dv_left = crate::dv::dval(&mut c).unwrap_or(0), From fd5133174b1f24dd6b5de7f7b9f005ccbd3eee19 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:07:34 -0500 Subject: [PATCH 094/520] Add shared pcraft bundle storage --- Cargo.lock | 12 + Cargo.toml | 1 + crates/pcraft-bundle/Cargo.toml | 19 ++ crates/pcraft-bundle/src/lib.rs | 421 ++++++++++++++++++++++++++++++++ 4 files changed, 453 insertions(+) create mode 100644 crates/pcraft-bundle/Cargo.toml create mode 100644 crates/pcraft-bundle/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 8fb3d4dd7..336fa8b0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1893,6 +1893,18 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-pcraft-bundle" +version = "0.1.0" +dependencies = [ + "blake3", + "craft-atomic", + "craft-pcraft-format", + "craft-zip", + "ruzstd", + "thiserror 2.0.21", +] + [[package]] name = "craft-pcraft-format" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 170ad8641..2b642f4fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,6 +95,7 @@ craft-ora = { path = "crates/ora" } craft-pptx = { path = "crates/pptx" } craft-penpot = { path = "crates/penpot" } craft-pcraft-format = { path = "crates/pcraft-format" } +craft-pcraft-bundle = { path = "crates/pcraft-bundle" } craft-pkcs7 = { path = "crates/pkcs7" } craft-plotter = { path = "crates/plotter" } craft-prepress = { path = "crates/prepress" } diff --git a/crates/pcraft-bundle/Cargo.toml b/crates/pcraft-bundle/Cargo.toml new file mode 100644 index 000000000..7521ad494 --- /dev/null +++ b/crates/pcraft-bundle/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "craft-pcraft-bundle" +description = "Bounded ZIP and directory storage for .pcraft manifests, previews and content-addressed objects" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-pcraft-format = { workspace = true } +craft-atomic = { workspace = true } +craft-zip = { workspace = true } +thiserror = { workspace = true } +ruzstd = { version = "0.9.1", default-features = false, features = ["std"] } +blake3 = { version = "1.8", default-features = false, features = ["pure"] } diff --git a/crates/pcraft-bundle/src/lib.rs b/crates/pcraft-bundle/src/lib.rs new file mode 100644 index 000000000..e44c23044 --- /dev/null +++ b/crates/pcraft-bundle/src/lib.rs @@ -0,0 +1,421 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! Container and content-addressed object storage for `.pcraft` bundles. +//! +//! Document schemas stay with the application. This crate owns the wire-level paths, ZIP +//! envelope, zstd object frames and content hashes. A caller supplies serialized manifest bytes +//! and opaque tile/blob bytes; it receives their stable paths to put in its manifest. + +use std::collections::BTreeMap; +use std::fs::{self, OpenOptions}; +use std::io::Read; +use std::path::{Path, PathBuf}; + +use craft_zip::{Archive, Compression, Writer}; + +/// Default bounds for opening an untrusted `.pcraft` ZIP. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// Maximum number of ZIP entries. + pub max_entries: u64, + /// Maximum total uncompressed ZIP bytes read. + pub max_total_bytes: u64, + /// Maximum manifest size. + pub max_manifest_bytes: u64, + /// Maximum compressed object bytes. + pub max_object_bytes: u64, + /// Maximum decoded object bytes. + pub max_decoded_object_bytes: u64, +} + +impl Default for Limits { + fn default() -> Self { + let limits = craft_pcraft_format::Limits::default(); + Self { + max_entries: limits.max_items as u64 + 4, + max_total_bytes: limits.max_total_bytes, + max_manifest_bytes: limits.max_manifest_bytes as u64, + max_object_bytes: limits.max_blob_bytes as u64, + max_decoded_object_bytes: limits.max_blob_bytes as u64, + } + } +} + +/// Why a `.pcraft` container or object could not be read or written. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// A path is not one of the bundle's canonical entry names. + #[error("invalid .pcraft entry path: {0}")] + InvalidPath(String), + /// An entry is missing, duplicated or violates a bundle invariant. + #[error("invalid .pcraft bundle: {0}")] + InvalidBundle(String), + /// An entry exceeds its caller-supplied bound. + #[error("{entry} exceeds the {limit}-byte limit")] + LimitExceeded { entry: String, limit: u64 }, + /// The object's content hash does not match its entry path. + #[error("content hash mismatch for {0}")] + HashMismatch(String), + /// ZIP container error. + #[error(transparent)] + Zip(#[from] craft_zip::Error), + /// I/O or zstd decoding error. + #[error("{0}")] + Io(String), +} + +pub type Result = std::result::Result; + +/// Whether a ZIP archive declares a `manifest.json` entry. +pub fn is_pcraft_zip(bytes: &[u8]) -> bool { + if !bytes.starts_with(b"PK\x03\x04") { + return false; + } + let limits = Limits { max_entries: 1_000_000, ..Limits::default() }; + Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries, max_total_bytes: 0 }) + .is_ok_and(|archive| !archive.has_duplicate_names() && archive.contains(craft_pcraft_format::MANIFEST)) +} + +/// Whether an entry path is a canonical manifest, preview, tile or blob path. +pub fn valid_entry_path(path: &str) -> bool { + if matches!( + path, + craft_pcraft_format::MANIFEST | craft_pcraft_format::THUMB | craft_pcraft_format::COMPOSITE | craft_pcraft_format::FULL_COMPOSITE + ) { + return true; + } + let Some((kind, name)) = path.split_once('/') else { return false }; + let Some(hash) = name.strip_suffix(craft_pcraft_format::OBJECT_SUFFIX) else { return false }; + craft_pcraft_format::is_valid_hash(hash) && (kind == craft_pcraft_format::TILES_DIR || kind == craft_pcraft_format::BLOBS_DIR) +} + +/// Content-addressed, compressed object bytes and their canonical bundle path. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct StoredObject { + /// `tiles/.zst` or `blobs/.zst`. + pub path: String, + /// BLAKE3 of the uncompressed bytes. + pub hash: String, + /// Zstandard frame containing the uncompressed bytes. + pub compressed: Vec, +} + +/// The kind of content-addressed object being stored. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ObjectKind { + /// Pixel tile samples. + Tile, + /// ICC, metadata, masks, or other opaque document data. + Blob, +} + +/// Hashes and compresses an object, returning the path to record in the manifest. +pub fn encode_object(kind: ObjectKind, bytes: &[u8]) -> StoredObject { + let hash = blake3::hash(bytes).to_hex().to_string(); + let path = match kind { + ObjectKind::Tile => craft_pcraft_format::tile_path(&hash), + ObjectKind::Blob => craft_pcraft_format::blob_path(&hash), + } + .unwrap_or_default(); + let compressed = ruzstd::encoding::compress_to_vec(bytes, ruzstd::encoding::CompressionLevel::Fastest); + StoredObject { path, hash, compressed } +} + +/// Decompresses a stored object within `max_decoded_bytes` and checks its content hash. +pub fn decode_object(path: &str, compressed: &[u8], max_decoded_bytes: u64) -> Result> { + if !valid_object_path(path) { + return Err(Error::InvalidPath(path.to_owned())); + } + let max = usize::try_from(max_decoded_bytes).unwrap_or(usize::MAX); + let decoder = ruzstd::decoding::StreamingDecoder::new(compressed).map_err(|e| Error::Io(e.to_string()))?; + let mut bytes = Vec::new(); + decoder.take(max_decoded_bytes.saturating_add(1)).read_to_end(&mut bytes).map_err(|e| Error::Io(e.to_string()))?; + if bytes.len() > max { + return Err(Error::LimitExceeded { entry: path.to_owned(), limit: max_decoded_bytes }); + } + let expected = path.rsplit('/').next().unwrap_or_default().trim_end_matches(craft_pcraft_format::OBJECT_SUFFIX); + if blake3::hash(&bytes).to_hex().as_str() != expected { + return Err(Error::HashMismatch(path.to_owned())); + } + Ok(bytes) +} + +fn valid_object_path(path: &str) -> bool { + let Some((kind, _)) = path.split_once('/') else { return false }; + (kind == craft_pcraft_format::TILES_DIR || kind == craft_pcraft_format::BLOBS_DIR) && valid_entry_path(path) +} + +/// An in-memory `.pcraft` ZIP reader with bounded entry and total reads. +pub struct ZipBundle<'a> { + archive: Archive<'a>, + limits: Limits, +} + +impl<'a> ZipBundle<'a> { + /// Opens a bundle, rejecting duplicate entry names and archives without a manifest. + pub fn open(bytes: &'a [u8], limits: Limits) -> Result { + let archive = Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries, max_total_bytes: limits.max_total_bytes })?; + if archive.has_duplicate_names() || !archive.contains(craft_pcraft_format::MANIFEST) { + return Err(Error::InvalidBundle("missing manifest or duplicate entry names".into())); + } + Ok(Self { archive, limits }) + } + + /// Reads the serialized document manifest. + pub fn manifest(&mut self) -> Result> { + self.read(craft_pcraft_format::MANIFEST, self.limits.max_manifest_bytes) + } + + /// Reads an optional preview entry. + pub fn preview(&mut self, path: &str) -> Result>> { + if !matches!(path, craft_pcraft_format::THUMB | craft_pcraft_format::COMPOSITE | craft_pcraft_format::FULL_COMPOSITE) { + return Err(Error::InvalidPath(path.to_owned())); + } + self.archive.read(path, self.limits.max_object_bytes).map_err(Into::into) + } + + /// Reads and verifies a compressed content-addressed object. + pub fn object(&mut self, path: &str) -> Result> { + let compressed = self.read(path, self.limits.max_object_bytes)?; + decode_object(path, &compressed, self.limits.max_decoded_object_bytes) + } + + fn read(&mut self, path: &str, limit: u64) -> Result> { + if !valid_entry_path(path) { + return Err(Error::InvalidPath(path.to_owned())); + } + self.archive.read(path, limit)?.ok_or_else(|| Error::InvalidBundle(format!("missing {path}"))) + } +} + +/// Builds a deterministic STORE ZIP bundle from format-neutral entries. +#[derive(Default)] +pub struct ZipBundleWriter { + entries: BTreeMap>, +} + +impl ZipBundleWriter { + /// Adds or replaces the manifest bytes. + pub fn set_manifest(&mut self, manifest: Vec) -> Result<()> { + if manifest.is_empty() { + return Err(Error::InvalidBundle("manifest is empty".into())); + } + self.entries.insert(craft_pcraft_format::MANIFEST.into(), manifest); + Ok(()) + } + + /// Adds an optional preview. + pub fn set_preview(&mut self, path: &str, bytes: Vec) -> Result<()> { + if !matches!(path, craft_pcraft_format::THUMB | craft_pcraft_format::COMPOSITE | craft_pcraft_format::FULL_COMPOSITE) { + return Err(Error::InvalidPath(path.to_owned())); + } + self.entries.insert(path.to_owned(), bytes); + Ok(()) + } + + /// Adds a compressed content-addressed object, returning its manifest path. + pub fn add_object(&mut self, kind: ObjectKind, bytes: &[u8]) -> Result { + let object = encode_object(kind, bytes); + if object.path.is_empty() { + return Err(Error::InvalidBundle("could not construct object path".into())); + } + self.entries.entry(object.path.clone()).or_insert(object.compressed); + Ok(object.path) + } + + /// Adds an already-compressed object after checking its path/hash. + pub fn add_stored_object(&mut self, path: &str, compressed: Vec, max_decoded_bytes: u64) -> Result<()> { + let _ = decode_object(path, &compressed, max_decoded_bytes)?; + self.entries.insert(path.to_owned(), compressed); + Ok(()) + } + + /// Writes entries in canonical path order, with every ZIP member stored without another layer + /// of compression (object payloads already use zstd). + pub fn finish(self) -> Result> { + if !self.entries.contains_key(craft_pcraft_format::MANIFEST) { + return Err(Error::InvalidBundle("manifest not set".into())); + } + let mut writer = Writer::new(); + let mut entries = self.entries; + let manifest = entries.remove(craft_pcraft_format::MANIFEST).ok_or_else(|| Error::InvalidBundle("manifest not set".into()))?; + writer.add(craft_pcraft_format::MANIFEST, &manifest, Compression::Stored)?; + for (path, bytes) in entries { + if !valid_entry_path(&path) { + return Err(Error::InvalidPath(path)); + } + writer.add(&path, &bytes, Compression::Stored)?; + } + writer.finish().map_err(Into::into) + } + + /// Saves the same entries as a directory bundle. Object files are published before the + /// manifest, the bundle lock serializes writers, and unreferenced canonical objects are + /// collected after the new manifest is durable. + pub fn save_directory(self, root: &Path) -> Result { + if !self.entries.contains_key(craft_pcraft_format::MANIFEST) { + return Err(Error::InvalidBundle("manifest not set".into())); + } + for path in self.entries.keys() { + if !valid_entry_path(path) { + return Err(Error::InvalidPath(path.clone())); + } + } + fs::create_dir_all(root).map_err(io_error)?; + let root = fs::canonicalize(root).map_err(io_error)?; + let lock_path = root.join(craft_pcraft_format::SAVE_LOCK); + refuse_symlink(&lock_path)?; + let lock = OpenOptions::new().read(true).write(true).create(true).truncate(false).open(&lock_path).map_err(io_error)?; + lock.lock().map_err(io_error)?; + + let mut written = 0usize; + for (path, bytes) in self.entries.iter().filter(|(path, _)| path.as_str() != craft_pcraft_format::MANIFEST) { + let target = safe_target(&root, path)?; + if let Some(parent) = target.parent() { + ensure_directory(parent)?; + } + refuse_symlink(&target)?; + craft_atomic::atomic_write(&target, bytes).map_err(io_error)?; + written = written.saturating_add(1); + } + let manifest = self.entries.get(craft_pcraft_format::MANIFEST).ok_or_else(|| Error::InvalidBundle("manifest not set".into()))?; + let manifest_path = root.join(craft_pcraft_format::MANIFEST); + refuse_symlink(&manifest_path)?; + craft_atomic::atomic_write(&manifest_path, manifest).map_err(io_error)?; + written = written.saturating_add(1); + + let mut removed = 0usize; + for kind in [craft_pcraft_format::TILES_DIR, craft_pcraft_format::BLOBS_DIR] { + let dir = root.join(kind); + if !dir.exists() { + continue; + } + refuse_symlink(&dir)?; + for entry in fs::read_dir(&dir).map_err(io_error)? { + let entry = entry.map_err(io_error)?; + let name = entry.file_name(); + let name = name.to_string_lossy(); + let path = format!("{kind}/{name}"); + if valid_entry_path(&path) && !self.entries.contains_key(&path) { + let meta = fs::symlink_metadata(entry.path()).map_err(io_error)?; + if meta.file_type().is_symlink() || !meta.is_file() { + return Err(Error::InvalidBundle(format!("refusing to collect non-file object {path}"))); + } + fs::remove_file(entry.path()).map_err(io_error)?; + removed = removed.saturating_add(1); + } + } + } + Ok(DirectoryStats { written, removed }) + } +} + +/// Work performed by a directory bundle save. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct DirectoryStats { + /// Entries atomically written, including `manifest.json`. + pub written: usize, + /// Stale content-addressed objects collected after manifest publication. + pub removed: usize, +} + +fn io_error(error: std::io::Error) -> Error { + Error::Io(error.to_string()) +} + +fn refuse_symlink(path: &Path) -> Result<()> { + match fs::symlink_metadata(path) { + Ok(metadata) if metadata.file_type().is_symlink() => Err(Error::InvalidBundle(format!("refusing symlink {}", path.display()))), + Ok(_) => Ok(()), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()), + Err(error) => Err(io_error(error)), + } +} + +fn ensure_directory(path: &Path) -> Result<()> { + refuse_symlink(path)?; + match fs::symlink_metadata(path) { + Ok(metadata) if metadata.is_dir() => Ok(()), + Ok(_) => Err(Error::InvalidBundle(format!("{} is not a directory", path.display()))), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + fs::create_dir(path).map_err(io_error)?; + Ok(()) + } + Err(error) => Err(io_error(error)), + } +} + +fn safe_target(root: &Path, relative: &str) -> Result { + if !valid_entry_path(relative) { + return Err(Error::InvalidPath(relative.to_owned())); + } + let mut target = root.to_path_buf(); + let components = relative.split('/').collect::>(); + for (index, component) in components.iter().enumerate() { + target.push(component); + if index + 1 < components.len() { + ensure_directory(&target)?; + } + } + Ok(target) +} + +/// Opens a directory bundle and reads canonical entries with caller-provided limits. +pub struct DirectoryBundle { + root: PathBuf, + limits: Limits, +} + +impl DirectoryBundle { + /// Opens a bundle directory containing a regular `manifest.json`. + pub fn open(root: &Path, limits: Limits) -> Result { + let root = fs::canonicalize(root).map_err(io_error)?; + let manifest = root.join(craft_pcraft_format::MANIFEST); + refuse_symlink(&manifest)?; + let metadata = fs::metadata(&manifest).map_err(io_error)?; + if !metadata.is_file() || metadata.len() > limits.max_manifest_bytes { + return Err(Error::LimitExceeded { entry: craft_pcraft_format::MANIFEST.into(), limit: limits.max_manifest_bytes }); + } + Ok(Self { root, limits }) + } + + /// Reads the serialized document manifest. + pub fn manifest(&self) -> Result> { + self.read(craft_pcraft_format::MANIFEST, self.limits.max_manifest_bytes) + } + + /// Reads an optional preview entry. + pub fn preview(&self, path: &str) -> Result>> { + if !matches!(path, craft_pcraft_format::THUMB | craft_pcraft_format::COMPOSITE | craft_pcraft_format::FULL_COMPOSITE) { + return Err(Error::InvalidPath(path.to_owned())); + } + self.read_optional(path, self.limits.max_object_bytes) + } + + /// Reads and verifies a compressed content-addressed object. + pub fn object(&self, path: &str) -> Result> { + let compressed = self.read(path, self.limits.max_object_bytes)?; + decode_object(path, &compressed, self.limits.max_decoded_object_bytes) + } + + fn read(&self, path: &str, limit: u64) -> Result> { + self.read_optional(path, limit)?.ok_or_else(|| Error::InvalidBundle(format!("missing {path}"))) + } + + fn read_optional(&self, path: &str, limit: u64) -> Result>> { + let target = safe_target(&self.root, path)?; + refuse_symlink(&target)?; + let metadata = match fs::metadata(&target) { + Ok(metadata) => metadata, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(error) => return Err(io_error(error)), + }; + if !metadata.is_file() || metadata.len() > limit { + return Err(Error::LimitExceeded { entry: path.to_owned(), limit }); + } + let bytes = fs::read(target).map_err(io_error)?; + if bytes.len() as u64 > limit { + return Err(Error::LimitExceeded { entry: path.to_owned(), limit }); + } + Ok(Some(bytes)) + } +} From b0284aad097235afca67a82de2596e0ba1d428f6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:08:04 -0500 Subject: [PATCH 095/520] Expose bounded pcraft entry reads --- crates/pcraft-bundle/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/pcraft-bundle/src/lib.rs b/crates/pcraft-bundle/src/lib.rs index e44c23044..9b2932523 100644 --- a/crates/pcraft-bundle/src/lib.rs +++ b/crates/pcraft-bundle/src/lib.rs @@ -165,6 +165,14 @@ impl<'a> ZipBundle<'a> { self.read(craft_pcraft_format::MANIFEST, self.limits.max_manifest_bytes) } + /// Reads any canonical bundle entry, including the stored zstd bytes of an object. + pub fn entry(&mut self, path: &str, max_bytes: u64) -> Result>> { + if !valid_entry_path(path) { + return Err(Error::InvalidPath(path.to_owned())); + } + self.archive.read(path, max_bytes).map_err(Into::into) + } + /// Reads an optional preview entry. pub fn preview(&mut self, path: &str) -> Result>> { if !matches!(path, craft_pcraft_format::THUMB | craft_pcraft_format::COMPOSITE | craft_pcraft_format::FULL_COMPOSITE) { From de087a45244e6581bcc5b8f42edb5c7bc5b8f758 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:12:14 -0500 Subject: [PATCH 096/520] Expose bounded directory bundle entries --- crates/pcraft-bundle/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/pcraft-bundle/src/lib.rs b/crates/pcraft-bundle/src/lib.rs index 9b2932523..fabf73963 100644 --- a/crates/pcraft-bundle/src/lib.rs +++ b/crates/pcraft-bundle/src/lib.rs @@ -391,6 +391,11 @@ impl DirectoryBundle { self.read(craft_pcraft_format::MANIFEST, self.limits.max_manifest_bytes) } + /// Reads any canonical bundle entry. + pub fn entry(&self, path: &str, max_bytes: u64) -> Result>> { + self.read_optional(path, max_bytes) + } + /// Reads an optional preview entry. pub fn preview(&self, path: &str) -> Result>> { if !matches!(path, craft_pcraft_format::THUMB | craft_pcraft_format::COMPOSITE | craft_pcraft_format::FULL_COMPOSITE) { From 6b8e79294d2eb4ed6ceab6ed18c8d31b2c41ec25 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:16:39 -0500 Subject: [PATCH 097/520] Expose bounded pcraft object codecs --- crates/pcraft-bundle/src/lib.rs | 40 ++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/pcraft-bundle/src/lib.rs b/crates/pcraft-bundle/src/lib.rs index fabf73963..1abd4fa9b 100644 --- a/crates/pcraft-bundle/src/lib.rs +++ b/crates/pcraft-bundle/src/lib.rs @@ -110,16 +110,50 @@ pub enum ObjectKind { /// Hashes and compresses an object, returning the path to record in the manifest. pub fn encode_object(kind: ObjectKind, bytes: &[u8]) -> StoredObject { - let hash = blake3::hash(bytes).to_hex().to_string(); + let hash = hash_content(bytes); let path = match kind { ObjectKind::Tile => craft_pcraft_format::tile_path(&hash), ObjectKind::Blob => craft_pcraft_format::blob_path(&hash), } .unwrap_or_default(); - let compressed = ruzstd::encoding::compress_to_vec(bytes, ruzstd::encoding::CompressionLevel::Fastest); + let compressed = encode_frame(bytes); StoredObject { path, hash, compressed } } +/// BLAKE3 hash of object bytes, in the canonical lowercase hexadecimal form used by entry paths. +pub fn hash_content(bytes: &[u8]) -> String { + blake3::hash(bytes).to_hex().to_string() +} + +/// Encodes one bounded zstd frame at the format's fixed fastest compression level. +pub fn encode_frame(bytes: &[u8]) -> Vec { + ruzstd::encoding::compress_to_vec(bytes, ruzstd::encoding::CompressionLevel::Fastest) +} + +/// Decodes one zstd frame under a byte limit and optional exact output length. +pub fn decode_frame(data: &[u8], max_bytes: usize, exact_bytes: Option, name: &str) -> Result> { + let window = u64::try_from(exact_bytes.unwrap_or(max_bytes)).unwrap_or(u64::MAX).max(8 << 20); + let mut decoder = match ruzstd::decoding::StreamingDecoder::new_with_max_window_size(data, window) { + Ok(decoder) => decoder, + Err(ruzstd::decoding::errors::FrameDecoderError::WindowSizeTooBig { requested, .. }) => { + return Err(Error::LimitExceeded { entry: format!("{name} zstd window ({requested})"), limit: window }); + } + Err(error) => return Err(Error::Io(format!("{name}: zstd: {error}"))), + }; + let mut out = Vec::new(); + let read_limit = u64::try_from(max_bytes).unwrap_or(u64::MAX).saturating_add(1); + (&mut decoder).take(read_limit).read_to_end(&mut out).map_err(|error| Error::Io(format!("{name}: zstd: {error}")))?; + if out.len() > max_bytes { + return Err(Error::LimitExceeded { entry: name.to_owned(), limit: max_bytes as u64 }); + } + if let Some(expected) = exact_bytes + && out.len() != expected + { + return Err(Error::InvalidBundle(format!("{name}: expected {expected} bytes, got {}", out.len()))); + } + Ok(out) +} + /// Decompresses a stored object within `max_decoded_bytes` and checks its content hash. pub fn decode_object(path: &str, compressed: &[u8], max_decoded_bytes: u64) -> Result> { if !valid_object_path(path) { @@ -133,7 +167,7 @@ pub fn decode_object(path: &str, compressed: &[u8], max_decoded_bytes: u64) -> R return Err(Error::LimitExceeded { entry: path.to_owned(), limit: max_decoded_bytes }); } let expected = path.rsplit('/').next().unwrap_or_default().trim_end_matches(craft_pcraft_format::OBJECT_SUFFIX); - if blake3::hash(&bytes).to_hex().as_str() != expected { + if hash_content(&bytes) != expected { return Err(Error::HashMismatch(path.to_owned())); } Ok(bytes) From 98144696139aafad86f63a776e147e989731f43e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:24:13 +0000 Subject: [PATCH 098/520] Bound extreme H264 POC cycle arithmetic --- crates/video/src/h264/decode/slice.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/crates/video/src/h264/decode/slice.rs b/crates/video/src/h264/decode/slice.rs index 32366ccf9..874c575ca 100644 --- a/crates/video/src/h264/decode/slice.rs +++ b/crates/video/src/h264/decode/slice.rs @@ -485,16 +485,18 @@ impl PocState { let cycle_cnt = (abs_frame_num - 1) / n; let in_cycle = (abs_frame_num - 1) % n; let delta_cycle = sps.offset_for_ref_frame.iter().map(|&o| i64::from(o)).sum::(); - expected = cycle_cnt * delta_cycle; + expected = cycle_cnt.saturating_mul(delta_cycle); for &o in &sps.offset_for_ref_frame[..=in_cycle as usize] { - expected += i64::from(o); + expected = expected.saturating_add(i64::from(o)); } } if sh.nal_ref_idc == 0 { - expected += i64::from(sps.offset_for_non_ref_pic); + expected = expected.saturating_add(i64::from(sps.offset_for_non_ref_pic)); } - let top = (expected + i64::from(sh.delta_pic_order_cnt[0])).clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32; - let bottom = (i64::from(top) + i64::from(sps.offset_for_top_to_bottom_field) + i64::from(sh.delta_pic_order_cnt[1])) + let top = expected.saturating_add(i64::from(sh.delta_pic_order_cnt[0])).clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32; + let bottom = i64::from(top) + .saturating_add(i64::from(sps.offset_for_top_to_bottom_field)) + .saturating_add(i64::from(sh.delta_pic_order_cnt[1])) .clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32; Poc { top, bottom, msb: 0, frame_num_offset } } @@ -672,4 +674,14 @@ mod poc_tests { let poc = state.compute(&sh, &sps); assert_eq!(poc.top, i32::MAX); } + + #[test] + fn poc_type1_saturates_only_after_wide_cycle_arithmetic_exceeds_i64() { + let mut sps = sps(1); + sps.offset_for_ref_frame = vec![i32::MAX; 255]; + let sh = header(u32::MAX, 0, 0, [0, 0], 1); + let state = PocState { prev_frame_num_offset: i32::MAX, ..PocState::default() }; + let poc = state.compute(&sh, &sps); + assert_eq!(poc.top, i32::MAX); + } } From c0df5be07dcc61047ddb005dca43df983ea99682 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:26:51 -0500 Subject: [PATCH 099/520] Reuse verified objects in directory bundles --- crates/pcraft-bundle/src/lib.rs | 211 ++++++++++++++++++++++++++++---- 1 file changed, 188 insertions(+), 23 deletions(-) diff --git a/crates/pcraft-bundle/src/lib.rs b/crates/pcraft-bundle/src/lib.rs index 1abd4fa9b..422776fd9 100644 --- a/crates/pcraft-bundle/src/lib.rs +++ b/crates/pcraft-bundle/src/lib.rs @@ -5,10 +5,11 @@ //! envelope, zstd object frames and content hashes. A caller supplies serialized manifest bytes //! and opaque tile/blob bytes; it receives their stable paths to put in its manifest. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap, HashSet}; use std::fs::{self, OpenOptions}; use std::io::Read; use std::path::{Path, PathBuf}; +use std::time::SystemTime; use craft_zip::{Archive, Compression, Writer}; @@ -267,6 +268,15 @@ impl ZipBundleWriter { /// Adds an already-compressed object after checking its path/hash. pub fn add_stored_object(&mut self, path: &str, compressed: Vec, max_decoded_bytes: u64) -> Result<()> { let _ = decode_object(path, &compressed, max_decoded_bytes)?; + self.stage_compressed_object(path, compressed) + } + + /// Stages an already-compressed object for a bundle write. Its path is checked now and its + /// frame/hash are verified by [`Self::finish`] or [`DirectoryBundleWriter::save_directory`]. + pub fn stage_compressed_object(&mut self, path: &str, compressed: Vec) -> Result<()> { + if !valid_object_path(path) { + return Err(Error::InvalidPath(path.to_owned())); + } self.entries.insert(path.to_owned(), compressed); Ok(()) } @@ -285,6 +295,9 @@ impl ZipBundleWriter { if !valid_entry_path(&path) { return Err(Error::InvalidPath(path)); } + if is_object_path(&path) { + let _ = decode_object(&path, &bytes, 1 << 30)?; + } writer.add(&path, &bytes, Compression::Stored)?; } writer.finish().map_err(Into::into) @@ -294,38 +307,133 @@ impl ZipBundleWriter { /// manifest, the bundle lock serializes writers, and unreferenced canonical objects are /// collected after the new manifest is durable. pub fn save_directory(self, root: &Path) -> Result { - if !self.entries.contains_key(craft_pcraft_format::MANIFEST) { + DirectoryBundleWriter::default().save_directory(root, self) + } +} + +/// Work performed by a directory bundle save. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct DirectoryStats { + /// Entries atomically written, including `manifest.json`. + pub written: usize, + /// Stale content-addressed objects collected after manifest publication. + pub removed: usize, + /// Existing, verified content-addressed objects reused without writing. + pub reused: usize, + /// Canonical object paths written during this save. + pub written_objects: Vec, + /// Canonical object paths reused after verification or a matching cached signature. + pub reused_objects: Vec, +} + +/// Incremental directory-bundle persistence with bounded rolling object verification. +#[derive(Default)] +pub struct DirectoryBundleWriter { + root: Option, + generation: u64, + reverify_budget: u64, + verified: HashMap, +} + +#[derive(Clone, Copy)] +struct CachedFile { + signature: FileSignature, + checked_at: u64, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +struct FileSignature { + len: u64, + modified: SystemTime, + #[cfg(unix)] + unix: (u64, u64, i64, i64), +} + +impl DirectoryBundleWriter { + /// Compressed bytes of already-verified objects to re-read on each save. + /// `0` trusts unchanged signatures; `u64::MAX` rechecks all referenced objects. + pub fn set_reverify_budget(&mut self, bytes: u64) { + self.reverify_budget = bytes; + } + + /// Saves an entry set incrementally. Existing objects are reused only after a hash check; + /// unchanged file signatures may skip checks until they enter the rolling verification + /// window. The manifest is atomically published last, then stale previews and objects are + /// collected while the bundle lock is held. + pub fn save_directory(&mut self, root: &Path, bundle: ZipBundleWriter) -> Result { + if !bundle.entries.contains_key(craft_pcraft_format::MANIFEST) { return Err(Error::InvalidBundle("manifest not set".into())); } - for path in self.entries.keys() { + for path in bundle.entries.keys() { if !valid_entry_path(path) { return Err(Error::InvalidPath(path.clone())); } } fs::create_dir_all(root).map_err(io_error)?; let root = fs::canonicalize(root).map_err(io_error)?; + if self.root.as_ref() != Some(&root) { + self.root = Some(root.clone()); + self.verified.clear(); + self.generation = 0; + } let lock_path = root.join(craft_pcraft_format::SAVE_LOCK); refuse_symlink(&lock_path)?; let lock = OpenOptions::new().read(true).write(true).create(true).truncate(false).open(&lock_path).map_err(io_error)?; lock.lock().map_err(io_error)?; + self.generation = self.generation.saturating_add(1); + let generation = self.generation; + let recheck = self.rolling_recheck(&bundle.entries); + let mut stats = DirectoryStats::default(); - let mut written = 0usize; - for (path, bytes) in self.entries.iter().filter(|(path, _)| path.as_str() != craft_pcraft_format::MANIFEST) { + for (path, bytes) in bundle.entries.iter().filter(|(path, _)| is_object_path(path)) { + let target = safe_target(&root, path)?; + if let Some(parent) = target.parent() { + ensure_directory(parent)?; + } + refuse_symlink(&target)?; + if let Some(signature) = file_signature(&target)? { + let cached = self.verified.get(path).is_some_and(|entry| entry.signature == signature); + if cached && !recheck.contains(path) { + stats.reused = stats.reused.saturating_add(1); + stats.reused_objects.push(path.clone()); + continue; + } + let existing = read_bounded(&target, 1 << 30)?; + if decode_object(path, &existing, 1 << 30).is_ok() { + if let Some(signature) = file_signature(&target)? { + self.verified.insert(path.clone(), CachedFile { signature, checked_at: generation }); + } + stats.reused = stats.reused.saturating_add(1); + stats.reused_objects.push(path.clone()); + continue; + } + } + let _ = decode_object(path, bytes, 1 << 30)?; + craft_atomic::atomic_write(&target, bytes).map_err(io_error)?; + if let Some(signature) = file_signature(&target)? { + self.verified.insert(path.clone(), CachedFile { signature, checked_at: generation }); + } + stats.written = stats.written.saturating_add(1); + stats.written_objects.push(path.clone()); + } + + for (path, bytes) in bundle.entries.iter().filter(|(path, _)| !is_object_path(path) && path.as_str() != craft_pcraft_format::MANIFEST) { let target = safe_target(&root, path)?; if let Some(parent) = target.parent() { ensure_directory(parent)?; } refuse_symlink(&target)?; craft_atomic::atomic_write(&target, bytes).map_err(io_error)?; - written = written.saturating_add(1); + stats.written = stats.written.saturating_add(1); } - let manifest = self.entries.get(craft_pcraft_format::MANIFEST).ok_or_else(|| Error::InvalidBundle("manifest not set".into()))?; + + let manifest = bundle.entries.get(craft_pcraft_format::MANIFEST).ok_or_else(|| Error::InvalidBundle("manifest not set".into()))?; let manifest_path = root.join(craft_pcraft_format::MANIFEST); refuse_symlink(&manifest_path)?; craft_atomic::atomic_write(&manifest_path, manifest).map_err(io_error)?; - written = written.saturating_add(1); + stats.written = stats.written.saturating_add(1); - let mut removed = 0usize; + let mut removed_paths = Vec::new(); for kind in [craft_pcraft_format::TILES_DIR, craft_pcraft_format::BLOBS_DIR] { let dir = root.join(kind); if !dir.exists() { @@ -335,29 +443,86 @@ impl ZipBundleWriter { for entry in fs::read_dir(&dir).map_err(io_error)? { let entry = entry.map_err(io_error)?; let name = entry.file_name(); - let name = name.to_string_lossy(); - let path = format!("{kind}/{name}"); - if valid_entry_path(&path) && !self.entries.contains_key(&path) { - let meta = fs::symlink_metadata(entry.path()).map_err(io_error)?; - if meta.file_type().is_symlink() || !meta.is_file() { + let path = format!("{kind}/{}", name.to_string_lossy()); + if is_object_path(&path) && !bundle.entries.contains_key(&path) { + let metadata = fs::symlink_metadata(entry.path()).map_err(io_error)?; + if metadata.file_type().is_symlink() || !metadata.is_file() { return Err(Error::InvalidBundle(format!("refusing to collect non-file object {path}"))); } fs::remove_file(entry.path()).map_err(io_error)?; - removed = removed.saturating_add(1); + removed_paths.push(path); + stats.removed = stats.removed.saturating_add(1); } } } - Ok(DirectoryStats { written, removed }) + for path in [craft_pcraft_format::THUMB, craft_pcraft_format::COMPOSITE, craft_pcraft_format::FULL_COMPOSITE] { + if !bundle.entries.contains_key(path) { + let target = root.join(path); + refuse_symlink(&target)?; + if fs::symlink_metadata(&target).is_ok() { + fs::remove_file(&target).map_err(io_error)?; + } + } + } + self.verified.retain(|path, _| bundle.entries.contains_key(path) && !removed_paths.contains(path)); + Ok(stats) + } + + fn rolling_recheck(&self, entries: &BTreeMap>) -> HashSet { + if self.reverify_budget == 0 { + return HashSet::new(); + } + let mut cached: Vec<(&String, &CachedFile)> = self.verified.iter().filter(|(path, _)| entries.contains_key(*path)).collect(); + cached.sort_by(|a, b| a.1.checked_at.cmp(&b.1.checked_at).then_with(|| a.0.cmp(b.0))); + let mut out = HashSet::new(); + let mut spent = 0u64; + for (path, value) in cached { + let next = spent.saturating_add(value.signature.len); + if !out.is_empty() && next > self.reverify_budget { + break; + } + spent = next; + out.insert(path.clone()); + } + out } } -/// Work performed by a directory bundle save. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct DirectoryStats { - /// Entries atomically written, including `manifest.json`. - pub written: usize, - /// Stale content-addressed objects collected after manifest publication. - pub removed: usize, +fn is_object_path(path: &str) -> bool { + path.starts_with("tiles/") || path.starts_with("blobs/") +} + +fn file_signature(path: &Path) -> Result> { + let metadata = match fs::symlink_metadata(path) { + Ok(metadata) if metadata.is_file() && !metadata.file_type().is_symlink() => metadata, + Ok(_) => return Err(Error::InvalidBundle(format!("{} is not a regular file", path.display()))), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(error) => return Err(io_error(error)), + }; + let modified = metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH); + #[cfg(unix)] + let unix = { + use std::os::unix::fs::MetadataExt; + (metadata.dev(), metadata.ino(), metadata.ctime(), metadata.ctime_nsec()) + }; + Ok(Some(FileSignature { + len: metadata.len(), + modified, + #[cfg(unix)] + unix, + })) +} + +fn read_bounded(path: &Path, max: u64) -> Result> { + let metadata = fs::metadata(path).map_err(io_error)?; + if metadata.len() > max { + return Err(Error::LimitExceeded { entry: path.display().to_string(), limit: max }); + } + let bytes = fs::read(path).map_err(io_error)?; + if bytes.len() as u64 > max { + return Err(Error::LimitExceeded { entry: path.display().to_string(), limit: max }); + } + Ok(bytes) } fn io_error(error: std::io::Error) -> Error { From de724aa188bb4131570cc052ef2be43da313da22 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:27:44 -0500 Subject: [PATCH 100/520] Configure bundle directory save locks --- crates/pcraft-bundle/src/lib.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/pcraft-bundle/src/lib.rs b/crates/pcraft-bundle/src/lib.rs index 422776fd9..acbe739b6 100644 --- a/crates/pcraft-bundle/src/lib.rs +++ b/crates/pcraft-bundle/src/lib.rs @@ -9,7 +9,7 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use std::fs::{self, OpenOptions}; use std::io::Read; use std::path::{Path, PathBuf}; -use std::time::SystemTime; +use std::time::{Duration, Instant, SystemTime}; use craft_zip::{Archive, Compression, Writer}; @@ -327,14 +327,20 @@ pub struct DirectoryStats { } /// Incremental directory-bundle persistence with bounded rolling object verification. -#[derive(Default)] pub struct DirectoryBundleWriter { root: Option, generation: u64, reverify_budget: u64, + lock_wait: Duration, verified: HashMap, } +impl Default for DirectoryBundleWriter { + fn default() -> Self { + Self { root: None, generation: 0, reverify_budget: 16 << 20, lock_wait: Duration::from_secs(10), verified: HashMap::new() } + } +} + #[derive(Clone, Copy)] struct CachedFile { signature: FileSignature, @@ -356,6 +362,11 @@ impl DirectoryBundleWriter { self.reverify_budget = bytes; } + /// How long to wait for another writer to release the bundle lock. + pub fn set_lock_wait(&mut self, wait: Duration) { + self.lock_wait = wait; + } + /// Saves an entry set incrementally. Existing objects are reused only after a hash check; /// unchanged file signatures may skip checks until they enter the rolling verification /// window. The manifest is atomically published last, then stale previews and objects are @@ -379,7 +390,17 @@ impl DirectoryBundleWriter { let lock_path = root.join(craft_pcraft_format::SAVE_LOCK); refuse_symlink(&lock_path)?; let lock = OpenOptions::new().read(true).write(true).create(true).truncate(false).open(&lock_path).map_err(io_error)?; - lock.lock().map_err(io_error)?; + let start = Instant::now(); + let _lock = loop { + match lock.try_lock() { + Ok(()) => break Some(lock), + Err(std::fs::TryLockError::WouldBlock) if start.elapsed() < self.lock_wait => std::thread::sleep(Duration::from_millis(20)), + Err(std::fs::TryLockError::WouldBlock) => { + return Err(Error::Io("another save of this bundle is in progress".into())); + } + Err(std::fs::TryLockError::Error(_)) => break None, + } + }; self.generation = self.generation.saturating_add(1); let generation = self.generation; let recheck = self.rolling_recheck(&bundle.entries); From 5e24ea380e54d4366d9f7aa8c674b3a3833cb472 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:47:44 +0000 Subject: [PATCH 101/520] Avoid duplicated timeline time primitives --- Cargo.lock | 1 + crates/time/src/lib.rs | 161 +++++++- crates/timeline/Cargo.toml | 1 + crates/timeline/src/time.rs | 632 +----------------------------- crates/timeline/src/time/tests.rs | 4 + 5 files changed, 164 insertions(+), 635 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2dbe34bdd..3925c1028 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2264,6 +2264,7 @@ dependencies = [ name = "craft-timeline" version = "0.1.0" dependencies = [ + "craft-time", "craft-xml", "proptest", "serde", diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs index 0d3703060..6cc780cf9 100644 --- a/crates/time/src/lib.rs +++ b/crates/time/src/lib.rs @@ -35,8 +35,7 @@ impl Tick { /// Seconds to ticks, clamped to [`Tick::MIN`]..=[`Tick::MAX`]. pub fn from_seconds_f64(s: f64) -> Tick { - let t = (s * TICKS_PER_SECOND as f64).round(); - Tick((t as i64).clamp(Tick::MIN.0, Tick::MAX.0)) + Tick((s * TICKS_PER_SECOND as f64).round() as i64) } pub fn seconds(self) -> f64 { @@ -246,7 +245,7 @@ impl TimeRange { } pub fn overlaps(&self, o: &TimeRange) -> bool { - self.start < o.end() && o.start < self.end() + !self.is_empty() && !o.is_empty() && self.start < o.end() && o.start < self.end() } pub fn intersect(&self, o: &TimeRange) -> Option { @@ -316,6 +315,19 @@ impl FrameRate { FrameRate::new((fps * 1000.0).round() as i64, 1000) } + /// Closest standard rate when `fps` is within `tolerance` of it, otherwise the nearest + /// 1/1000 rate. This retains the timeline API's coarse-timebase compatibility behavior. + pub fn from_f64_within(fps: f64, tolerance: f64) -> Self { + let mut best: Option<(f64, FrameRate)> = None; + for rate in Self::COMMON { + let off = (rate.as_f64() - fps).abs(); + if off <= tolerance * rate.as_f64() && best.is_none_or(|(closest, _)| off < closest) { + best = Some((off, rate)); + } + } + best.map_or_else(|| Self::from_f64(fps), |(_, rate)| rate) + } + /// Returns this frame rate if positive, or the default rate ([`FrameRate::FPS_23_976`]). /// /// ### Application Compatibility Note @@ -341,6 +353,11 @@ impl FrameRate { clamp_i64(frame_index(self.sane(), t)) } + /// Frames from `start` to `end`, counted between their frame indices. + pub fn frames_between(self, start: Tick, end: Tick) -> i64 { + self.frame_at(end).saturating_sub(self.frame_at(start)) + } + /// Start tick of frame `f` (ceiling division). pub fn tick_of(self, f: i64) -> Tick { Tick(clamp_i64(frame_boundary(self.sane(), i128::from(f)))) @@ -432,6 +449,9 @@ impl TimeDisplay { pub const ALL: [TimeDisplay; 6] = [TimeDisplay::Timecode, TimeDisplay::Feet35, TimeDisplay::Feet16, TimeDisplay::Frames, TimeDisplay::AudioSamples, TimeDisplay::Seconds]; + /// The video display formats (Sequence Settings ▸ Display Format, the burn-in's Time Format). + pub const VIDEO: [TimeDisplay; 4] = [TimeDisplay::Timecode, TimeDisplay::Feet35, TimeDisplay::Feet16, TimeDisplay::Frames]; + pub fn label(self) -> &'static str { match self { TimeDisplay::Timecode => "Timecode", @@ -442,6 +462,32 @@ impl TimeDisplay { TimeDisplay::Seconds => "Seconds", } } + + /// Stable id used by commands and the UI. + pub fn id(self) -> &'static str { + match self { + TimeDisplay::Timecode => "timecode", + TimeDisplay::Frames => "frames", + TimeDisplay::Feet16 => "feet16", + TimeDisplay::Feet35 => "feet35", + TimeDisplay::AudioSamples => "samples", + TimeDisplay::Seconds => "seconds", + } + } + + /// The display with this [`id`](Self::id), ignoring case. + pub fn from_id(id: &str) -> Option { + TimeDisplay::ALL.into_iter().find(|display| display.id().eq_ignore_ascii_case(id)) + } + + /// Frames per foot for the Feet + Frames displays. + pub fn frames_per_foot(self) -> Option { + match self { + TimeDisplay::Feet35 => Some(16), + TimeDisplay::Feet16 => Some(40), + _ => None, + } + } } fn df_params(rate: FrameRate) -> (i64, i64) { @@ -509,35 +555,33 @@ pub fn format_time(t: Tick, rate: FrameRate, drop_frame: bool, display: TimeDisp match display { TimeDisplay::Timecode => format_timecode_frames(frame, rate, drop_frame), TimeDisplay::Frames => format!("{frame}"), - TimeDisplay::Feet35 | TimeDisplay::Feet16 => { - let per_ft = if display == TimeDisplay::Feet35 { 16 } else { 40 }; - let neg = frame < 0; - let a = frame.unsigned_abs(); - format!("{}{}+{:02}", if neg { "-" } else { "" }, a / per_ft, a % per_ft) - } + TimeDisplay::Feet35 | TimeDisplay::Feet16 => format_feet_frames(frame, display.frames_per_foot().unwrap_or(16)), TimeDisplay::AudioSamples => { let neg = t.0 < 0; let abs_ticks = t.0.unsigned_abs(); let total_secs = abs_ticks / (TICKS_PER_SECOND as u64); let rem_ticks = abs_ticks % (TICKS_PER_SECOND as u64); - let rem_samples = (rem_ticks as u128 * sample_rate as u128) / (TICKS_PER_SECOND as u128); + let rem_samples = Tick(rem_ticks as i64).to_units_floor(sample_rate); let h = total_secs / 3600; let m = (total_secs / 60) % 60; let s = total_secs % 60; format!("{}{:02}:{:02}:{:02}:{:05}", if neg { "-" } else { "" }, h, m, s, rem_samples) } - TimeDisplay::Seconds => format!("{:.3}", t.seconds()), + TimeDisplay::Seconds => { + let seconds = format!("{:.3}", t.seconds()); + if seconds == "-0.000" { "0.000".into() } else { seconds } + } } } /// Format a frame count as Feet + Frames (film footage counting). pub fn format_feet_frames(frame: i64, per_foot: i64) -> String { - let pf = per_foot.max(1); + let per_foot = per_foot.max(1); let sign = if frame < 0 { "-" } else { "" }; - let a = frame.unsigned_abs(); - let pf = pf as u64; - let w = if pf > 100 { 3 } else { 2 }; - format!("{sign}{:04}+{:0w$}", a / pf, a % pf) + let frames = frame.unsigned_abs(); + let per_foot = per_foot as u64; + let width = if per_foot > 100 { 3 } else { 2 }; + format!("{sign}{:04}+{:0width$}", frames / per_foot, frames % per_foot) } /// Error from timecode parsing. @@ -561,6 +605,22 @@ impl std::error::Error for ParseError {} /// saturates to `i64::MAX` (or `i64::MIN`) to keep calculations total without errors. /// In contrast, EffectCraft returns `Err(ParseError("timecode too large"))`. pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i64) -> Result { + parse_timecode_with_negative_policy(input, rate, drop_frame, current, true) +} + +/// Parse timecode with the relative-negative policy used by the historical timeline API. +/// A leading minus offsets from `current`, including a complete four-field timecode. +pub fn parse_timecode_relative(input: &str, rate: FrameRate, drop_frame: bool, current: i64) -> Result { + parse_timecode_with_negative_policy(input, rate, drop_frame, current, false) +} + +fn parse_timecode_with_negative_policy( + input: &str, + rate: FrameRate, + drop_frame: bool, + current: i64, + full_negative_is_absolute: bool, +) -> Result { let s = input.trim(); if s.is_empty() { return Err(ParseError("empty timecode".into())); @@ -615,7 +675,10 @@ pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i let base = rate.timecode_base(); let frames = if nums.len() == 1 && sign_char.is_some() { fr - } else if drop_frame && rate.supports_drop_frame() && m < 60 && sec < 60 && fr < base { + } else if drop_frame && rate.supports_drop_frame() { + let (sec, fr) = (sec.saturating_add(fr / base), fr % base); + let (m, sec) = (m.saturating_add(sec / 60), sec % 60); + let (h, m) = (h.saturating_add(m / 60), m % 60); fields_to_frames(h, m, sec, fr, rate, true) } else { h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec).saturating_mul(base).saturating_add(fr) @@ -623,10 +686,11 @@ pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i match sign_char { Some('+') => Ok(current.saturating_add(frames)), + Some('-') if full_negative_is_absolute && nums.len() == 4 => Ok(-frames), Some('-') => { // A full 4-field timecode with leading '-' represents an absolute negative timecode (e.g. preroll). - // Single-number or partial relative offsets (e.g. -15 or -1.00) offset from current. - if nums.len() == 4 { Ok(-frames) } else { Ok(current.saturating_sub(frames)) } + // The timeline compatibility path treats every leading minus as an offset instead. + Ok(current.saturating_sub(frames)) } None | Some(_) => Ok(frames), } @@ -671,6 +735,65 @@ pub fn parse_feet_frames(input: &str, per_foot: i64, current: i64) -> Result Result { + parse_time_display_with_negative_policy(input, rate, drop_frame, display, current, true) +} + +/// Parse text using a [`TimeDisplay`] representation and the historical timeline relative-minus rule. +pub fn parse_time_display_relative(input: &str, rate: FrameRate, drop_frame: bool, display: TimeDisplay, current: i64) -> Result { + parse_time_display_with_negative_policy(input, rate, drop_frame, display, current, false) +} + +fn parse_time_display_with_negative_policy( + input: &str, + rate: FrameRate, + drop_frame: bool, + display: TimeDisplay, + current: i64, + full_negative_is_absolute: bool, +) -> Result { + let per_foot = match display { + TimeDisplay::Frames => 1, + TimeDisplay::Feet35 | TimeDisplay::Feet16 => display.frames_per_foot().unwrap_or(16), + _ => { + return if full_negative_is_absolute { + parse_timecode(input, rate, drop_frame, current) + } else { + parse_timecode_relative(input, rate, drop_frame, current) + }; + } + }; + let text = input.trim(); + if text.is_empty() { + return Err(ParseError("empty time".into())); + } + let (relative, body) = match text.as_bytes()[0] { + b'+' => (Some(1), &text[1..]), + b'-' => (Some(-1), &text[1..]), + _ => (None, text), + }; + let field = |part: &str| -> Result { + if part.is_empty() { + Ok(0) + } else if !part.bytes().all(|byte| byte.is_ascii_digit()) { + Err(ParseError(format!("not a time: `{input}`"))) + } else { + part.parse::().map_err(|_| ParseError("number too large".into())) + } + }; + let parts: Vec<&str> = body.split(['+', ':', ';', '.', ',']).collect(); + let frames = match parts.as_slice() { + [number] => field(number)?, + [feet, frames] if per_foot > 1 => field(feet)?.saturating_mul(per_foot).saturating_add(field(frames)?), + _ => return Err(ParseError(format!("not a time: `{input}`"))), + }; + Ok(match relative { + Some(sign) => current.saturating_add(frames.saturating_mul(sign)), + None => frames, + }) +} + /// Common audio sample rates offered in sequence settings. pub const SAMPLE_RATES: [i64; 6] = [32_000, 44_100, 48_000, 88_200, 96_000, 192_000]; diff --git a/crates/timeline/Cargo.toml b/crates/timeline/Cargo.toml index 3eca9bc5c..9e73908e4 100644 --- a/crates/timeline/Cargo.toml +++ b/crates/timeline/Cargo.toml @@ -20,6 +20,7 @@ otio = ["dep:serde_json"] [dependencies] serde = { workspace = true, optional = true } +craft-time.workspace = true craft-xml = { workspace = true, optional = true } serde_json = { workspace = true, optional = true } diff --git a/crates/timeline/src/time.rs b/crates/timeline/src/time.rs index f3f12f58d..2db3def5b 100644 --- a/crates/timeline/src/time.rs +++ b/crates/timeline/src/time.rs @@ -1,627 +1,27 @@ -//! Exact media time. +//! Compatibility re-exports for timeline time primitives. //! -//! All time is an integer number of [`Tick`]s at [`TICKS_PER_SECOND`] = 254 016 000 000/s. That -//! rate divides evenly into every broadcast frame duration (23.976, 24, 25, 29.97, 30, 48, 50, -//! 59.94, 60, 120 …) and every common audio sample duration (8 k … 192 kHz, including the 44.1 k -//! family), so edits, frame math and audio alignment never drift. -//! -//! Timecode (SMPTE drop/non-drop, frames, feet+frames, samples) is only a *display* of ticks. -//! -//! Every conversion is total: a zero or negative rate (from a damaged file or project) gives -//! zero instead of a division-by-zero panic, and an invalid [`FrameRate`] counts as the default. +//! The implementation lives in `craft-time`; this module keeps the established +//! `craft_timeline::time::*` paths available to downstream users. -use std::fmt; -use std::ops::{Add, AddAssign, Neg, Sub, SubAssign}; +pub use craft_time::{ + FrameRate, ParseError, SAMPLE_RATES, TICKS_PER_SECOND, Tick, TimeDisplay, TimeRange, fields_to_frames, format_timecode_frames, frames_to_fields, + parse_time_display_relative as parse_time_display, parse_timecode_relative as parse_timecode, +}; -/// Ticks per second. -pub const TICKS_PER_SECOND: i64 = 254_016_000_000; - -/// A point or duration in time, in ticks. -/// Arithmetic saturates at the i64 storage limits; representable results remain exact. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(transparent))] -pub struct Tick(pub i64); - -impl Tick { - pub const ZERO: Tick = Tick(0); - pub const MAX: Tick = Tick(i64::MAX / 4); - pub const MIN: Tick = Tick(i64::MIN / 4); - - pub fn from_seconds_f64(s: f64) -> Tick { - Tick((s * TICKS_PER_SECOND as f64).round() as i64) - } - pub fn seconds(self) -> f64 { - self.0 as f64 / TICKS_PER_SECOND as f64 - } - /// Exact conversion from a count of `units` at `per_second` (e.g. samples at 48 000). - pub fn from_units(units: i64, per_second: i64) -> Tick { - Tick(clamp_i64((units as i128 * TICKS_PER_SECOND as i128).checked_div(per_second as i128).unwrap_or(0))) - } - /// Floor conversion to a count of units at `per_second`. - pub fn to_units_floor(self, per_second: i64) -> i64 { - clamp_i64((self.0 as i128 * per_second as i128).div_euclid(TICKS_PER_SECOND as i128)) - } - /// Conversion from a rational timestamp `pts * num / den` seconds (container timebases). - pub fn from_rational(pts: i64, num: i64, den: i64) -> Tick { - if den == 0 { - return Tick::ZERO; - } - let Some(t) = (pts as i128 * num as i128).checked_mul(TICKS_PER_SECOND as i128) else { - // A product beyond i128 divided by an i64 cannot fit back into i64. - return Tick(if (pts < 0) ^ (num < 0) ^ (den < 0) { i64::MIN } else { i64::MAX }); - }; - Tick(clamp_i64(t.div_euclid(den as i128))) - } - /// Inverse of [`Tick::from_rational`], floored to the timebase. - pub fn to_rational_floor(self, num: i64, den: i64) -> i64 { - clamp_i64((self.0 as i128 * den as i128).checked_div_euclid(num as i128 * TICKS_PER_SECOND as i128).unwrap_or(0)) - } - /// Inverse of [`Tick::from_rational`], rounded to the nearest timebase unit (halves up). - /// - /// For finding a sample by its timestamp: containers store timestamps rounded to their - /// timebase (Matroska usually to 1 ms), so a stamp can sit up to half a unit after the true - /// time. Flooring the requested time misses every frame whose stamp was rounded up. - pub fn to_rational_round(self, num: i64, den: i64) -> i64 { - let n = self.0 as i128 * den as i128; - let unit = num as i128 * TICKS_PER_SECOND as i128; - let (n, unit) = if unit < 0 { (-n, -unit) } else { (n, unit) }; - let (Some(q), Some(r)) = (n.checked_div_euclid(unit), n.checked_rem_euclid(unit)) else { return 0 }; - // 0 <= r < unit: round up from the halfway point - clamp_i64(if r * 2 >= unit { q + 1 } else { q }) - } - pub fn abs(self) -> Tick { - Tick(self.0.saturating_abs()) - } - pub fn min(self, o: Tick) -> Tick { - Tick(self.0.min(o.0)) - } - pub fn max(self, o: Tick) -> Tick { - Tick(self.0.max(o.0)) - } - pub fn clamp(self, lo: Tick, hi: Tick) -> Tick { - // max/min rather than `clamp`, which panics when the bounds cross. - Tick(self.0.max(lo.0).min(hi.0)) - } - /// Scale by a rational factor (`num/den`), flooring. - pub fn mul_ratio(self, num: i64, den: i64) -> Tick { - Tick(clamp_i64((self.0 as i128 * num as i128).checked_div_euclid(den as i128).unwrap_or(0))) - } -} - -impl Add for Tick { - type Output = Tick; - fn add(self, o: Tick) -> Tick { - Tick(self.0.saturating_add(o.0)) - } -} -impl Sub for Tick { - type Output = Tick; - fn sub(self, o: Tick) -> Tick { - Tick(self.0.saturating_sub(o.0)) - } -} -impl Neg for Tick { - type Output = Tick; - fn neg(self) -> Tick { - Tick(self.0.saturating_neg()) - } -} -impl AddAssign for Tick { - fn add_assign(&mut self, o: Tick) { - self.0 = self.0.saturating_add(o.0); - } -} -impl SubAssign for Tick { - fn sub_assign(&mut self, o: Tick) { - self.0 = self.0.saturating_sub(o.0); - } -} - -/// A half-open time range `[start, start + duration)`. -/// Zero or negative durations are empty and never overlap another range. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct TimeRange { - pub start: Tick, - pub duration: Tick, -} - -impl TimeRange { - pub fn new(start: Tick, duration: Tick) -> Self { - Self { start, duration } - } - pub fn from_bounds(start: Tick, end: Tick) -> Self { - Self { start, duration: end - start } - } - pub fn end(&self) -> Tick { - self.start + self.duration - } - pub fn contains(&self, t: Tick) -> bool { - t >= self.start && t < self.end() - } - pub fn overlaps(&self, o: &TimeRange) -> bool { - !self.is_empty() && !o.is_empty() && self.start < o.end() && o.start < self.end() - } - pub fn intersect(&self, o: &TimeRange) -> Option { - let s = self.start.max(o.start); - let e = self.end().min(o.end()); - (e > s).then(|| TimeRange::from_bounds(s, e)) - } - pub fn is_empty(&self) -> bool { - self.duration.0 <= 0 - } -} - -/// A frame rate as an exact rational (`num / den` frames per second). -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct FrameRate { - pub num: i64, - pub den: i64, -} - -impl Default for FrameRate { - fn default() -> Self { - FrameRate::FPS_23_976 - } -} - -impl FrameRate { - pub const FPS_23_976: FrameRate = FrameRate { num: 24000, den: 1001 }; - pub const FPS_24: FrameRate = FrameRate { num: 24, den: 1 }; - pub const FPS_25: FrameRate = FrameRate { num: 25, den: 1 }; - pub const FPS_29_97: FrameRate = FrameRate { num: 30000, den: 1001 }; - pub const FPS_30: FrameRate = FrameRate { num: 30, den: 1 }; - pub const FPS_48: FrameRate = FrameRate { num: 48, den: 1 }; - pub const FPS_50: FrameRate = FrameRate { num: 50, den: 1 }; - pub const FPS_59_94: FrameRate = FrameRate { num: 60000, den: 1001 }; - pub const FPS_60: FrameRate = FrameRate { num: 60, den: 1 }; - pub const FPS_119_88: FrameRate = FrameRate { num: 120000, den: 1001 }; - pub const FPS_120: FrameRate = FrameRate { num: 120, den: 1 }; - - /// Rates offered in sequence settings (Premiere's list). - pub const COMMON: [FrameRate; 11] = [ - Self::FPS_23_976, - Self::FPS_24, - Self::FPS_25, - Self::FPS_29_97, - Self::FPS_30, - Self::FPS_48, - Self::FPS_50, - Self::FPS_59_94, - Self::FPS_60, - Self::FPS_119_88, - Self::FPS_120, - ]; - - pub fn new(num: i64, den: i64) -> Self { - let g = i128::from(gcd(num.unsigned_abs(), den.unsigned_abs()).max(1)); - FrameRate { num: (i128::from(num) / g) as i64, den: (i128::from(den) / g) as i64 } - } - - /// Closest standard rate for a float (e.g. from a container's average rate). - pub fn from_f64(fps: f64) -> Self { - for r in Self::COMMON { - if (r.as_f64() - fps).abs() < 0.005 { - return r; - } - } - FrameRate::new((fps * 1000.0).round() as i64, 1000) - } - - /// Closest standard rate when `fps` is within `tolerance` of it, `tolerance` a fraction of the - /// rate (`0.005` is half a percent), else the nearest 1/1000 rate. A rate a container reports - /// from timestamps rounded to a coarse unit lands slightly off a standard rate: at 90 kHz, - /// whole milliseconds leave a 30 fps clip's average at 30.02 or 29.95 (#1700). - pub fn from_f64_within(fps: f64, tolerance: f64) -> Self { - let mut best: Option<(f64, FrameRate)> = None; - for r in Self::COMMON { - let off = (r.as_f64() - fps).abs(); - if off <= tolerance * r.as_f64() && best.is_none_or(|(closest, _)| off < closest) { - best = Some((off, r)); - } - } - best.map_or_else(|| Self::from_f64(fps), |(_, r)| r) - } - - /// This rate if it is positive, else the default (a damaged file or project can carry a zero - /// or negative rate; every frame computation uses this so it can't divide by zero). - pub fn sane(self) -> FrameRate { - if self.num > 0 && self.den > 0 { self } else { FrameRate::default() } - } - - pub fn as_f64(self) -> f64 { - self.num as f64 / self.den as f64 - } - - /// Exact duration of one frame (rounded down only for exotic rates). - /// Values outside the integer tick range saturate at its limit. - pub fn frame_duration(self) -> Tick { - let r = self.sane(); - Tick(clamp_i64((TICKS_PER_SECOND as i128 * r.den as i128) / r.num as i128)) - } - - /// Index of the frame containing `t` (floor), saturated to the integer frame range. - pub fn frame_at(self, t: Tick) -> i64 { - clamp_i64(frame_index(self.sane(), t)) - } - - /// Frames from `start` to `end`, counted between their frame indices. `frame_at(end - start)` - /// can read one short when a frame is not a whole number of ticks: the ticks between two - /// boundaries can be one fewer than the exact length of those frames. - pub fn frames_between(self, start: Tick, end: Tick) -> i64 { - self.frame_at(end).saturating_sub(self.frame_at(start)) - } - - /// Start tick of frame `f` (ceiling division). - /// - /// Uses ceiling (not floor) so that `frame_at(tick_of(f)) == f` holds for every - /// representable frame boundary with at least one tick per frame, even when the frame duration is not an integer number of ticks - /// (exotic rates like 37.516 fps where `TICKS_PER_SECOND * den / num` has a - /// fractional remainder). Floor division truncates the remainder, causing each - /// frame boundary to drift backward by one tick until `frame_at` returns `f - 1` - /// instead of `f` — frame stepping then stalls or moves backward. - /// Values outside the integer tick range saturate at its limit. - pub fn tick_of(self, f: i64) -> Tick { - Tick(clamp_i64(frame_boundary(self.sane(), i128::from(f)))) - } - - /// Snap `t` down to a frame boundary. - pub fn snap(self, t: Tick) -> Tick { - let r = self.sane(); - Tick(clamp_i64(frame_boundary(r, frame_index(r, t)))) - } - - /// Snap `t` to the nearest frame boundary. - pub fn snap_nearest(self, t: Tick) -> Tick { - let r = self.sane(); - // Keep the frame index and both boundaries wide until choosing the nearest. Saturating - // an unrepresentable upper boundary first would make it seem closer than it really is. - let f = frame_index(r, t); - let a = frame_boundary(r, f); - let b = frame_boundary(r, f.saturating_add(1)); - let t = i128::from(t.0); - Tick(clamp_i64(if t - a <= b - t { a } else { b })) - } - - /// Timecode base (frames counted per timecode second): 30 for 29.97, 24 for 23.976. - pub fn timecode_base(self) -> i64 { - let r = self.sane(); - (r.num / r.den).saturating_add(i64::from(r.num % r.den != 0)).max(1) - } - - /// NTSC (x/1001) rates can use drop-frame timecode. - pub fn is_ntsc(self) -> bool { - self.den == 1001 - } - - /// Whether drop-frame counting applies (29.97 / 59.94 / 119.88). - pub fn supports_drop_frame(self) -> bool { - self.is_ntsc() && self.timecode_base() % 30 == 0 - } - - pub fn label(self) -> String { - if self.den == 1 { - format!("{}", self.num) - } else { - let v = self.as_f64(); - let s = format!("{v:.3}"); - let s = s.trim_end_matches('0').trim_end_matches('.'); - s.to_string() - } - } -} - -impl fmt::Display for FrameRate { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{} fps", self.label()) - } -} - -/// Narrow only after applying the storage bounds, never by a wrapping integer cast. -fn clamp_i64(value: i128) -> i64 { - value.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64 -} - -/// Callers pass a positive rate. Products of two i64 values fit in i128. -fn frame_index(rate: FrameRate, t: Tick) -> i128 { - (i128::from(t.0) * i128::from(rate.num)).div_euclid(i128::from(TICKS_PER_SECOND) * i128::from(rate.den)) -} - -/// Ceiling of the rational boundary. An overflowing numerator divided by an i64 rate cannot -/// fit in an i64 tick, so saturating that intermediate preserves the public result. Boundaries -/// derived from frame_index remain within i128 (at most one frame away from an i64 tick). -fn frame_boundary(rate: FrameRate, frame: i128) -> i128 { - let Some(n) = frame.checked_mul(i128::from(TICKS_PER_SECOND)).and_then(|n| n.checked_mul(i128::from(rate.den))) else { - return if frame < 0 { i128::MIN } else { i128::MAX }; - }; - let d = i128::from(rate.num); - // Flooring a fractional boundary puts the tick in the preceding frame. - n.div_euclid(d) + i128::from(n.rem_euclid(d) != 0) -} - -fn gcd(mut a: u64, mut b: u64) -> u64 { - while b != 0 { - (a, b) = (b, a % b); - } - a -} - -/// How time is displayed (Premiere: Timecode, Feet+Frames 16mm/35mm, Frames, Audio Samples). -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub enum TimeDisplay { - #[default] - Timecode, - Frames, - Feet16, - Feet35, - AudioSamples, - Seconds, -} - -impl TimeDisplay { - pub const ALL: [TimeDisplay; 6] = - [TimeDisplay::Timecode, TimeDisplay::Feet35, TimeDisplay::Feet16, TimeDisplay::Frames, TimeDisplay::AudioSamples, TimeDisplay::Seconds]; - pub fn label(self) -> &'static str { - match self { - TimeDisplay::Timecode => "Timecode", - TimeDisplay::Frames => "Frames", - TimeDisplay::Feet16 => "Feet + Frames 16mm", - TimeDisplay::Feet35 => "Feet + Frames 35mm", - TimeDisplay::AudioSamples => "Audio Samples", - TimeDisplay::Seconds => "Seconds", - } - } -} - -impl TimeDisplay { - /// The video display formats (Sequence Settings ▸ Display Format, the burn-in's Time Format). - pub const VIDEO: [TimeDisplay; 4] = [TimeDisplay::Timecode, TimeDisplay::Feet35, TimeDisplay::Feet16, TimeDisplay::Frames]; - - /// Stable id used by commands and the UI: `timecode`, `feet35`, `feet16`, `frames`, `samples`, `seconds`. - pub fn id(self) -> &'static str { - match self { - TimeDisplay::Timecode => "timecode", - TimeDisplay::Frames => "frames", - TimeDisplay::Feet16 => "feet16", - TimeDisplay::Feet35 => "feet35", - TimeDisplay::AudioSamples => "samples", - TimeDisplay::Seconds => "seconds", - } - } - - /// The display with this [`id`](Self::id), ignoring case. - pub fn from_id(id: &str) -> Option { - TimeDisplay::ALL.into_iter().find(|d| d.id().eq_ignore_ascii_case(id)) - } - - /// Frames per foot of film for the Feet + Frames displays: 16 for 35mm 4-perf, 40 for 16mm. - pub fn frames_per_foot(self) -> Option { - match self { - TimeDisplay::Feet35 => Some(16), - TimeDisplay::Feet16 => Some(40), - _ => None, - } - } -} - -/// Format a frame count as film feet + frames (`F+FF`, e.g. `2+08`; negative: `-2+08`). -/// `frames_per_foot` below 1 is treated as 1 so a bad value never divides by zero. +/// Format a timeline frame count as film feet + frames, retaining its established compact display. pub fn format_feet_frames(frame: i64, frames_per_foot: i64) -> String { - let per_ft = frames_per_foot.max(1).unsigned_abs(); - let a = frame.unsigned_abs(); - format!("{}{}+{:02}", if frame < 0 { "-" } else { "" }, a / per_ft, a % per_ft) + let per_foot = frames_per_foot.max(1).unsigned_abs(); + let frames = frame.unsigned_abs(); + format!("{}{}+{:02}", if frame < 0 { "-" } else { "" }, frames / per_foot, frames % per_foot) } -/// Drop-frame parameters: frames dropped per minute and nominal base. -fn df_params(rate: FrameRate) -> (i64, i64) { - let base = rate.timecode_base(); - (base / 15, base) // 30 → 2, 60 → 4, 120 → 8 -} - -/// Convert a frame count to SMPTE fields `(negative, h, m, s, f)`. -pub fn frames_to_fields(frame: i64, rate: FrameRate, drop_frame: bool) -> (bool, i64, i64, i64, i64) { - let neg = frame < 0; - let mut n = i128::from(frame).abs(); - let (drop, base) = df_params(rate); - let (drop, base) = (i128::from(drop), i128::from(base)); - if drop_frame && rate.supports_drop_frame() { - let per_min = base * 60 - drop; - let per_10 = per_min * 10 + drop; - let d = n / per_10; - let m = n % per_10; - n = n.saturating_add(drop * 9 * d); - if m > drop { - n = n.saturating_add(drop * ((m - drop) / per_min)); - } +/// Format time using the timeline's historical compact Feet + Frames display. +pub fn format_time(tick: Tick, rate: FrameRate, drop_frame: bool, display: TimeDisplay, sample_rate: i64) -> String { + if let TimeDisplay::Feet35 | TimeDisplay::Feet16 = display { + return format_feet_frames(rate.frame_at(tick), display.frames_per_foot().unwrap_or(16)); } - let f = n % base; - let s = (n / base) % 60; - let mi = (n / (base * 60)) % 60; - let h = n / (base * 3600); - (neg, clamp_i64(h), clamp_i64(mi), clamp_i64(s), clamp_i64(f)) + craft_time::format_time(tick, rate, drop_frame, display, sample_rate) } -/// Convert SMPTE fields back to a frame count. -pub fn fields_to_frames(h: i64, m: i64, s: i64, f: i64, rate: FrameRate, drop_frame: bool) -> i64 { - let (drop, base) = df_params(rate); - let seconds = i128::from(h) * 3600 + i128::from(m) * 60 + i128::from(s); - // Nominal drop-frame labels can exceed i64 even when the final frame count fits. - let mut n = seconds.saturating_mul(i128::from(base)).saturating_add(i128::from(f)); - if drop_frame && rate.supports_drop_frame() { - let total_min = i128::from(h) * 60 + i128::from(m); - n = n.saturating_sub(i128::from(drop) * (total_min - total_min / 10)); - } - clamp_i64(n) -} - -/// Format a frame count as SMPTE timecode (`HH:MM:SS:FF`, or `HH;MM;SS;FF` for drop-frame). -pub fn format_timecode_frames(frame: i64, rate: FrameRate, drop_frame: bool) -> String { - let df = drop_frame && rate.supports_drop_frame(); - let (neg, h, m, s, f) = frames_to_fields(frame, rate, df); - let sep = if df { ';' } else { ':' }; - let fw = if rate.timecode_base() >= 100 { 3 } else { 2 }; - format!("{}{h:02}{sep}{m:02}{sep}{s:02}{sep}{f:0fw$}", if neg { "-" } else { "" }) -} - -/// Format a tick according to a display mode. -pub fn format_time(t: Tick, rate: FrameRate, drop_frame: bool, display: TimeDisplay, sample_rate: i64) -> String { - let frame = rate.frame_at(t); - match display { - TimeDisplay::Timecode => format_timecode_frames(frame, rate, drop_frame), - TimeDisplay::Frames => format!("{frame}"), - TimeDisplay::Feet35 | TimeDisplay::Feet16 => format_feet_frames(frame, display.frames_per_foot().unwrap_or(16)), - TimeDisplay::AudioSamples => { - let neg = t.0 < 0; - let a = t.0.unsigned_abs(); - let tps = TICKS_PER_SECOND.unsigned_abs(); - let secs = a / tps; - let rem = Tick((a % tps) as i64).to_units_floor(sample_rate); - format!("{}{:02}:{:02}:{:02}:{rem:05}", if neg { "-" } else { "" }, secs / 3600, (secs / 60) % 60, secs % 60) - } - TimeDisplay::Seconds => { - let s = format!("{:.3}", t.seconds()); - // A tiny negative time rounds to zero but keeps its sign: drop it. - if s == "-0.000" { "0.000".to_string() } else { s } - } - } -} - -/// Error from [`parse_timecode`]. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ParseError(pub String); - -impl fmt::Display for ParseError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&self.0) - } -} -impl std::error::Error for ParseError {} - -/// Parse user-typed timecode into a frame count, the way Premiere's timecode fields do: -/// - `01:02:03:04`, `01;02;03;04`, `1.2.3.4` (any of `:;.,` separators), -/// - bare digits are read right-aligned as `HHMMSSFF` (`1000` = 00:00:10:00), -/// - a leading `+`/`-` makes the value relative to `current` (`+15` = 15 frames later, `-1.00` = 1 s earlier). -pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i64) -> Result { - let s = input.trim(); - if s.is_empty() { - return Err(ParseError("empty timecode".into())); - } - let (rel, body) = match s.as_bytes()[0] { - b'+' => (Some(1), &s[1..]), - b'-' => (Some(-1), &s[1..]), - _ => (None, s), - }; - let parts: Vec<&str> = body.split([':', ';', '.', ',']).collect(); - let nums: Vec = if parts.len() == 1 { - let digits = parts[0]; - if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { - return Err(ParseError(format!("not a timecode: `{input}`"))); - } - // Right-aligned pairs: "12345" -> [1, 23, 45]; relative bare numbers are frames. - if rel.is_some() { - vec![digits.parse().map_err(|_| ParseError("number too large".into()))?] - } else { - let mut v = Vec::new(); - let b = digits.as_bytes(); - let mut end = b.len(); - while end > 0 { - let start = end.saturating_sub(2); - v.push(digits[start..end].parse::().unwrap_or(0)); - end = start; - } - v.reverse(); - v - } - } else { - parts - .iter() - .map(|p| { - if p.is_empty() { - Ok(0) - } else if !p.bytes().all(|b| b.is_ascii_digit()) { - Err(ParseError(format!("bad field `{p}`"))) - } else { - p.parse::().map_err(|_| ParseError(format!("bad field `{p}`"))) - } - }) - .collect::>()? - }; - if nums.len() > 4 { - return Err(ParseError("too many timecode fields".into())); - } - let mut f4 = [0i64; 4]; - let off = 4 - nums.len(); - f4[off..].copy_from_slice(&nums); - let [h, m, sec, fr] = f4; - // Overflowing fields (e.g. 90 frames) are allowed, as in Premiere. - let base = rate.timecode_base(); - let frames = if nums.len() == 1 && rel.is_some() { - fr - } else if drop_frame && rate.supports_drop_frame() { - // overflowing fields carry into the next one first, then drop-frame counting applies - let (sec, fr) = (sec.saturating_add(fr / base), fr % base); - let (m, sec) = (m.saturating_add(sec / 60), sec % 60); - let (h, m) = (h.saturating_add(m / 60), m % 60); - fields_to_frames(h, m, sec, fr, rate, true) - } else { - // Saturating: typed or agent-sent timecodes can have absurdly large fields. - h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec).saturating_mul(base).saturating_add(fr) - }; - Ok(match rel { - Some(sign) => current.saturating_add(frames.saturating_mul(sign)), - None => frames, - }) -} - -/// Parse typed text in the given display format into a frame count. -/// -/// - [`TimeDisplay::Frames`]: a plain frame count (`240`). -/// - [`TimeDisplay::Feet35`] / [`TimeDisplay::Feet16`]: `F+FF` (`2+08`; `.`, `:`, `;` and `,` also -/// separate); a bare number is a frame count. A frames field past the foot rolls over -/// (`1+20` at 35mm is 36 frames), as the timecode parser does. -/// - A leading `+` / `-` makes the value relative to `current` (a bare relative number is frames). -/// - Every other display parses as timecode ([`parse_timecode`]). -pub fn parse_time_display(input: &str, rate: FrameRate, drop_frame: bool, display: TimeDisplay, current: i64) -> Result { - let per_ft = match display { - TimeDisplay::Frames => 1, - TimeDisplay::Feet35 | TimeDisplay::Feet16 => display.frames_per_foot().unwrap_or(16), - _ => return parse_timecode(input, rate, drop_frame, current), - }; - let s = input.trim(); - if s.is_empty() { - return Err(ParseError("empty time".into())); - } - let (rel, body) = match s.as_bytes()[0] { - b'+' => (Some(1), &s[1..]), - b'-' => (Some(-1), &s[1..]), - _ => (None, s), - }; - let field = |p: &str| -> Result { - if p.is_empty() { - Ok(0) - } else if !p.bytes().all(|b| b.is_ascii_digit()) { - Err(ParseError(format!("not a time: `{input}`"))) - } else { - p.parse::().map_err(|_| ParseError("number too large".into())) - } - }; - let parts: Vec<&str> = body.split(['+', ':', ';', '.', ',']).collect(); - let frames = match parts.as_slice() { - [n] => field(n)?, - [feet, fr] if per_ft > 1 => field(feet)?.saturating_mul(per_ft).saturating_add(field(fr)?), - _ => return Err(ParseError(format!("not a time: `{input}`"))), - }; - Ok(match rel { - Some(sign) => current.saturating_add(frames.saturating_mul(sign)), - None => frames, - }) -} - -/// Common audio sample rates offered in sequence settings. -pub const SAMPLE_RATES: [i64; 6] = [32_000, 44_100, 48_000, 88_200, 96_000, 192_000]; - #[cfg(test)] mod tests; diff --git a/crates/timeline/src/time/tests.rs b/crates/timeline/src/time/tests.rs index e6aada244..40bea7624 100644 --- a/crates/timeline/src/time/tests.rs +++ b/crates/timeline/src/time/tests.rs @@ -1,6 +1,10 @@ use super::*; use proptest::prelude::*; +fn clamp_i64(value: i128) -> i64 { + value.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64 +} + #[test] fn empty_time_ranges_never_overlap() { let clip = TimeRange::new(Tick(0), Tick(10)); From 55f6235c5b50b6e987ef2886ddca84070e1ca541 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:54:31 -0500 Subject: [PATCH 102/520] Simplify gridline no-fill check --- crates/drawingml/src/chart/read.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/drawingml/src/chart/read.rs b/crates/drawingml/src/chart/read.rs index 9919433e9..38ce9f605 100644 --- a/crates/drawingml/src/chart/read.rs +++ b/crates/drawingml/src/chart/read.rs @@ -443,7 +443,7 @@ fn read_axes<'a>(c: &mut Chart, pa: &'a El, primary_val: &Option) -> (Op // A gridline whose `c:spPr/a:ln` has `a:noFill` is not drawn, though the element is there. let grid = |a: &El, k: &str| { a.child(Local(k)) - .is_some_and(|g| !g.child(Local("spPr")).and_then(|p| p.child(Local("ln"))).is_some_and(|l| l.child(Local("noFill")).is_some())) + .is_some_and(|g| g.child(Local("spPr")).and_then(|p| p.child(Local("ln"))).is_none_or(|l| l.child(Local("noFill")).is_none())) }; if c.kind == ChartType::Combo && c.series.iter().any(|s| s.secondary_axis) { let primary_id = val_ax.and_then(|a| a.child(Local("axId")).and_then(|c| c.attr_local("val"))); From f6cab1980bbe422cc00c4b19d9f0f9ba337e1316 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:54:31 -0500 Subject: [PATCH 103/520] Preserve SVG picture fallbacks in the Presentation model --- crates/drawingml/src/read.rs | 6 ++++++ crates/drawingml/src/style.rs | 3 +++ crates/drawingml/src/write.rs | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/crates/drawingml/src/read.rs b/crates/drawingml/src/read.rs index 496870774..9e9f8ff3e 100644 --- a/crates/drawingml/src/read.rs +++ b/crates/drawingml/src/read.rs @@ -166,6 +166,12 @@ pub fn blip_fill(src: &mut dyn Source, bf: &El) -> Option { { pf.artistic = Some(crate::style::Artistic { effect, original }); } + pf.svg = blip.and_then(|b| b.child(Local("extLst"))).and_then(|extensions| { + extensions + .elements() + .filter(|extension| extension.local() == "ext" && extension.attr_local("uri") == Some("{96DAC541-7B7A-43D3-8B79-37D633B846F1}")) + .find_map(|extension| extension.child(Local("svgBlip")).and_then(|svg| media_ref(src, svg))) + }); if let Some(b) = blip { for e in b.elements() { match e.local() { diff --git a/crates/drawingml/src/style.rs b/crates/drawingml/src/style.rs index 291ee4e76..36b5a6922 100644 --- a/crates/drawingml/src/style.rs +++ b/crates/drawingml/src/style.rs @@ -184,6 +184,9 @@ impl Default for PictureMode { #[serde(default, rename_all = "camelCase")] pub struct PictureFill { pub media: MediaId, + /// Optional vector SVG version of `media`; the regular media remains the bitmap fallback. + #[serde(skip_serializing_if = "Option::is_none")] + pub svg: Option, /// Source crop as fractions of the image (left, top, right, bottom); negative values pad. pub crop: [f64; 4], pub mode: PictureMode, diff --git a/crates/drawingml/src/write.rs b/crates/drawingml/src/write.rs index 0d9b83b1a..25bfb89db 100644 --- a/crates/drawingml/src/write.rs +++ b/crates/drawingml/src/write.rs @@ -107,6 +107,7 @@ pub fn blip_fill(w: &mut Writer, out: &mut dyn Sink, tag: &str, pf: &PictureFill w.tag(tag).attr("rotWithShape", if pf.upright { "0" } else { "1" }).open(); let adj = &pf.adjust; let has_kids = pf.alpha.is_some() + || pf.svg.is_some() || adj.brightness != 0.0 || adj.contrast != 0.0 || adj.grayscale @@ -168,6 +169,13 @@ pub fn blip_fill(w: &mut Writer, out: &mut dyn Sink, tag: &str, pf: &PictureFill w.close(); w.close(); } + if let Some(svg) = pf.svg.and_then(|id| out.media_rel(id, "image")) { + w.open("a:extLst", &[]); + w.tag("a:ext").attr("uri", "{96DAC541-7B7A-43D3-8B79-37D633B846F1}").open(); + w.tag("asvg:svgBlip").attr("xmlns:asvg", "http://schemas.microsoft.com/office/drawing/2016/SVG/main").attr("r:embed", &svg).empty(); + w.close(); + w.close(); + } w.close(); } else { w.tag("a:blip").list(a).empty(); From b9992dbebd531c7362c087c93910b5113cc88ac8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:55:56 -0500 Subject: [PATCH 104/520] Canonicalize DNG CFA at sensor origin --- crates/raw/src/dng.rs | 17 +++++++---------- crates/raw/src/lib.rs | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index 87f23f81f..7c5b361cd 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -406,17 +406,14 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f if cpp != 1 { return Err(RawError::Unsupported("CFA data with several samples per pixel".into())); } - // the tag is anchored at the top-left of the ActiveArea (DNG spec); `RawImage::cfa` is anchored at the - // sensor origin (the crop to the active area shifts it back), so move the anchor out by the area's offset - // (DNGs that LightCraft wrote before its writer followed the spec carry the sensor-origin pattern; they are marked - // by a bare "LightCraft" Software tag and keep their original reading) - let mut at_origin = Cfa { width: cols, height: rows, pattern, origin_x: 0, origin_y: 0 }; + // DNG's CFAPattern is anchored at ActiveArea. Store one canonical CFA anchored at the + // sensor origin instead: invert the ActiveArea phase before building RawImage::cfa. + // Older LightCraft files wrote the pattern at the sensor origin and are identified by + // their bare Software tag, so those keep the bytes as written. + let at_area = Cfa { width: cols, height: rows, pattern, origin_x: 0, origin_y: 0 }; let legacy_writer = [raw, ifd0].iter().any(|i| i.string(t::SOFTWARE).as_deref().map(str::trim) == Some("LightCraft")); - if !legacy_writer { - at_origin.origin_x = active_area.x; - at_origin.origin_y = active_area.y; - } - Some(at_origin) + let cfa = if legacy_writer { at_area } else { at_area.shifted((cols - active_area.x % cols) % cols, (rows - active_area.y % rows) % rows) }; + Some(cfa) } else { None }; diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 8c8dd227d..c9c072122 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -862,7 +862,7 @@ impl Cfa { } /// Pattern name like `"RGGB"` for 2×2 patterns. pub fn name(&self) -> String { - self.pattern.iter().map(|&c| ['R', 'G', 'B', '?'][c.min(3) as usize]).collect() + self.shifted(0, 0).pattern.iter().map(|&c| ['R', 'G', 'B', '?'][c.min(3) as usize]).collect() } fn valid(&self) -> bool { self.width > 0 && self.height > 0 && self.width <= 16 && self.height <= 16 && self.pattern.len() == self.width * self.height From f16d99f71c417d1f9247ae17ec53f4a66f6bfb82 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:57:51 +0000 Subject: [PATCH 105/520] fix time parsing and frame boundary edge cases --- crates/time/README.md | 2 +- crates/time/src/lib.rs | 23 ++++++++++++++- crates/time/src/soundcraft/format.rs | 2 +- crates/time/src/soundcraft/grid.rs | 40 ++++++++++++++++++++++---- crates/time/src/soundcraft/mod.rs | 28 +++++++++++++++++- crates/time/src/soundcraft/timecode.rs | 23 +++++++++++---- 6 files changed, 103 insertions(+), 15 deletions(-) diff --git a/crates/time/README.md b/crates/time/README.md index 0f62fb3b1..9c85b0219 100644 --- a/crates/time/README.md +++ b/crates/time/README.md @@ -35,7 +35,7 @@ assert_eq!(audio_sample, one_sec); Frame rates are represented as exact fractions (`num / den`): ```rust -use craft_time::FrameRate; +use craft_time::{FrameRate, Tick}; // Standard NTSC and PAL rates let ntsc_24 = FrameRate::FPS_23_976; // 24000 / 1001 diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs index 6cc780cf9..4a3af58df 100644 --- a/crates/time/src/lib.rs +++ b/crates/time/src/lib.rs @@ -561,7 +561,7 @@ pub fn format_time(t: Tick, rate: FrameRate, drop_frame: bool, display: TimeDisp let abs_ticks = t.0.unsigned_abs(); let total_secs = abs_ticks / (TICKS_PER_SECOND as u64); let rem_ticks = abs_ticks % (TICKS_PER_SECOND as u64); - let rem_samples = Tick(rem_ticks as i64).to_units_floor(sample_rate); + let rem_samples = Tick(rem_ticks as i64).to_units_floor(sample_rate.max(0)); let h = total_secs / 3600; let m = (total_secs / 60) % 60; let s = total_secs % 60; @@ -625,11 +625,22 @@ fn parse_timecode_with_negative_policy( if s.is_empty() { return Err(ParseError("empty timecode".into())); } + if s.len() > 80 { + return Err(ParseError("timecode is too long".into())); + } let (sign_char, body) = match s.as_bytes()[0] { b'+' => (Some('+'), &s[1..]), b'-' => (Some('-'), &s[1..]), _ => (None, s), }; + // Four fields is the complete timecode grammar. Check limits before allocating split vectors. + let separators = body.bytes().filter(|b| matches!(b, b':' | b';' | b'.' | b',')).count(); + if separators > 3 { + return Err(ParseError("too many timecode fields".into())); + } + if separators == 0 && body.len() > if sign_char.is_some() { 19 } else { 8 } { + return Err(ParseError("timecode is too long".into())); + } let parts: Vec<&str> = body.split([':', ';', '.', ',']).collect(); let nums: Vec = if parts.len() == 1 { let digits = parts[0]; @@ -824,6 +835,15 @@ mod tests { } } + #[test] + fn timecode_parser_rejects_oversized_inputs_before_field_collection() { + let too_many_fields = ":".repeat(100_000); + assert!(parse_timecode(&too_many_fields, FrameRate::FPS_30, false, 0).is_err()); + let compact = "1".repeat(9); + assert!(parse_timecode(&compact, FrameRate::FPS_30, false, 0).is_err()); + assert!(parse_timecode_relative("+12345678901234567890", FrameRate::FPS_30, false, 0).is_err()); + } + #[test] fn test_broadcast_frame_rates() { for r in FrameRate::COMMON { @@ -858,6 +878,7 @@ mod tests { assert_eq!(format_time(neg_complex, r, false, TimeDisplay::AudioSamples, 48_000), "-01:01:01:00000"); let zero = Tick::ZERO; + assert_eq!(format_time(Tick(2), r, false, TimeDisplay::AudioSamples, -1), "00:00:00:00000"); assert_eq!(format_time(zero, r, false, TimeDisplay::AudioSamples, 48_000), "00:00:00:00000"); } diff --git a/crates/time/src/soundcraft/format.rs b/crates/time/src/soundcraft/format.rs index 11b588c3f..b16511006 100644 --- a/crates/time/src/soundcraft/format.rs +++ b/crates/time/src/soundcraft/format.rs @@ -76,7 +76,7 @@ pub fn format_length(start: Samples, len: Samples, fmt: TimeFormat, sr: SampleRa let t0 = map.samples_to_ticks(start, sr); let t1 = map.samples_to_ticks(start.saturating_add(len), sr); let m = map.meter_at_tick(t0); - let d = (t1 - t0).max(0); + let d = t1.saturating_sub(t0).max(0); let bars = d / m.ticks_per_bar().max(1); let rem = d % m.ticks_per_bar().max(1); format!("{bars}| {}| {:03}", rem / m.ticks_per_beat().max(1), rem % m.ticks_per_beat().max(1)) diff --git a/crates/time/src/soundcraft/grid.rs b/crates/time/src/soundcraft/grid.rs index 5f32ca6d8..2d40a454b 100644 --- a/crates/time/src/soundcraft/grid.rs +++ b/crates/time/src/soundcraft/grid.rs @@ -430,7 +430,7 @@ impl GridValue { // always four quarter notes and therefore keeps a continuous tick origin. let bb = map.bar_beat_at_tick(t); let bar_tick = map.bar_start_tick(bb.bar); - let origin = if value == NoteValue::Whole { 0 } else { bar_tick }; + let origin = if value == NoteValue::Whole || (value == NoteValue::Bar && dotted) { 0 } else { bar_tick }; let step = Self::note_ticks(value, dotted, triplet, map, origin); let k = (t - origin).div_euclid(step); map.tick_to_samples(origin + k * step, sr) @@ -547,7 +547,14 @@ impl GridValue { } let step = self.step_samples(line, sr, map, rate); let candidate = self.line_at_or_before(line.saturating_add(step), sr, map, rate); - if candidate > line { candidate } else { line.saturating_add(step) } + let next = if candidate > line { candidate } else { line.saturating_add(step) }; + if matches!(self, GridValue::Note { value: NoteValue::Bar, .. }) { + return next; + } + let tick = map.samples_to_ticks(line, sr); + let bar = map.bar_beat_at_tick(tick).bar; + let next_bar = map.tick_to_samples(map.bar_start_tick(bar.saturating_add(1)), sr); + if next_bar > line { next.min(next_bar) } else { next } } /// All grid lines within `[start, end)`, capped at `max` lines. @@ -681,6 +688,27 @@ mod tests { assert_eq!(note(NoteValue::Half).lines(0, 150_000, sr, &map, rate, 10), [0, 48_000, 72_000, 120_000, 144_000]); } + #[test] + fn dotted_bar_grid_keeps_its_spacing_across_bars() { + let (sr, map, rate) = (SampleRate::HZ_48000, TempoMap::default(), FrameRate::Fps25); + let grid = GridValue::Note { value: NoteValue::Bar, dotted: true, triplet: false }; + assert_eq!(grid.line_at_or_before(143_999, sr, &map, rate), 0); + assert_eq!(grid.line_at_or_before(144_000, sr, &map, rate), 144_000); + assert_eq!(grid.next_line(0, sr, &map, rate), 144_000); + } + + #[test] + fn dotted_note_does_not_skip_a_meter_change_boundary() { + let sr = SampleRate::HZ_48000; + let mut map = TempoMap::default(); + map.set_meter(TICKS_PER_QUARTER * 4, 2, 4).unwrap(); + let rate = FrameRate::Fps25; + let grid = GridValue::Note { value: NoteValue::Half, dotted: true, triplet: false }; + assert_eq!(grid.next_line(72_000, sr, &map, rate), 96_000); + assert!(grid.lines(72_000, 120_000, sr, &map, rate, 10).contains(&96_000)); + assert_eq!(grid.snap(90_000, sr, &map, rate), 96_000); + } + #[test] fn seconds_grid_lines_land_on_whole_seconds() { let (sr, map, rate) = (SampleRate::HZ_44100, TempoMap::default(), FrameRate::Fps30); @@ -745,7 +773,8 @@ mod tests { let frame = GridValue::drop_second_frame(seconds, rate); let line = rate.samples_at_frame(frame, sr); assert_eq!(grid.line_at_or_before(line, sr, &map, rate), line, "{rate:?} second {seconds}"); - assert_eq!(grid.line_at_or_before(-line, sr, &map, rate), -line, "{rate:?} second -{seconds}"); + let negative_line = rate.samples_at_frame(-frame, sr); + assert_eq!(grid.line_at_or_before(negative_line, sr, &map, rate), negative_line, "{rate:?} second -{seconds}"); for offset in -2..=rate.nominal() + 2 { let position = rate.samples_at_frame(frame.saturating_add(offset), sr); let before = grid.line_at_or_before(position, sr, &map, rate); @@ -761,8 +790,9 @@ mod tests { let boundary = rate.samples_at_frame(minute_frame, sr); assert_eq!(grid.line_at_or_before(boundary, sr, &map, rate), boundary, "{rate:?} positive minute boundary"); assert_eq!(grid.snap(boundary, sr, &map, rate), boundary, "{rate:?} positive minute snap"); - assert_eq!(grid.line_at_or_before(-boundary, sr, &map, rate), -boundary, "{rate:?} negative minute boundary"); - assert_eq!(grid.snap(-boundary, sr, &map, rate), -boundary, "{rate:?} negative minute snap"); + let negative_boundary = rate.samples_at_frame(-minute_frame, sr); + assert_eq!(grid.line_at_or_before(negative_boundary, sr, &map, rate), negative_boundary, "{rate:?} negative minute boundary"); + assert_eq!(grid.snap(negative_boundary, sr, &map, rate), negative_boundary, "{rate:?} negative minute snap"); } } diff --git a/crates/time/src/soundcraft/mod.rs b/crates/time/src/soundcraft/mod.rs index bd4df3aa9..300909003 100644 --- a/crates/time/src/soundcraft/mod.rs +++ b/crates/time/src/soundcraft/mod.rs @@ -38,8 +38,16 @@ pub enum TimeError { /// Sample rates supported by sessions (Pro Tools offers 44.1 k – 192 k; we accept 8 k – 768 k). #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +#[serde(try_from = "u32")] pub struct SampleRate(u32); +impl TryFrom for SampleRate { + type Error = TimeError; + fn try_from(hz: u32) -> Result { + Self::new(hz) + } +} + impl SampleRate { pub const HZ_44100: SampleRate = SampleRate(44_100); pub const HZ_48000: SampleRate = SampleRate(48_000); @@ -107,7 +115,7 @@ impl Range { s >= self.start && s < self.end } pub fn overlaps(&self, o: &Range) -> bool { - self.start < o.end && o.start < self.end + !self.is_empty() && !o.is_empty() && self.start < o.end && o.start < self.end } pub fn intersect(&self, o: &Range) -> Option { let s = self.start.max(o.start); @@ -123,6 +131,24 @@ impl Range { mod tests { use super::*; + #[test] + fn sample_rate_serde_validates_nonzero_bounds() { + assert!(serde_json::from_str::("0").is_err()); + assert!(serde_json::from_str::("7999").is_err()); + let rate = SampleRate::new(48_000).unwrap(); + assert_eq!(serde_json::to_string(&rate).unwrap(), "48000"); + assert_eq!(serde_json::from_str::("48000").unwrap(), rate); + } + + #[test] + fn empty_ranges_never_overlap() { + let point = Range::point(5); + let span = Range::new(0, 10); + assert!(!point.overlaps(&span)); + assert!(!span.overlaps(&point)); + assert!(Range::new(0, 5).overlaps(&span)); + } + #[test] fn sample_rate_bounds() { assert!(SampleRate::new(48_000).is_ok()); diff --git a/crates/time/src/soundcraft/timecode.rs b/crates/time/src/soundcraft/timecode.rs index 03eb1cc94..d09458e5c 100644 --- a/crates/time/src/soundcraft/timecode.rs +++ b/crates/time/src/soundcraft/timecode.rs @@ -108,9 +108,12 @@ impl FrameRate { if n == 0 { return 0; } - // Round half away from zero so negative frames mirror positive ones. - let mag = (i128::from(f.unsigned_abs()) * i128::from(sr.hz()) * i128::from(d) + i128::from(n) / 2) / i128::from(n); - let v = if f < 0 { -mag } else { mag }; + // Use the first integer sample whose floored frame lookup is `f`; nearest rounding + // can put fractional-rate frame starts just before their own lookup interval. + let numerator = i128::from(f) * i128::from(sr.hz()) * i128::from(d); + let denominator = i128::from(n); + let floor = numerator.div_euclid(denominator); + let v = floor + i128::from(numerator.rem_euclid(denominator) != 0); i64::try_from(v).unwrap_or(if v < 0 { i64::MIN } else { i64::MAX }) } @@ -194,6 +197,12 @@ impl Timecode { Some(r) => (true, r), None => (false, t), }; + if t.len() > 80 { + return Err(TimeError::Parse("timecode is too long".into(), "timecode")); + } + if t.bytes().filter(|b| matches!(b, b':' | b';' | b'.')).count() > 3 { + return Err(TimeError::Parse(text.to_string(), "timecode")); + } let parts: Vec<&str> = t.split([':', ';', '.']).collect(); if parts.is_empty() || parts.len() > 4 { return Err(TimeError::Parse(text.to_string(), "timecode")); @@ -232,15 +241,16 @@ mod tests { } #[test] - fn negative_timecode_rounds_symmetrically() { + fn fractional_frame_starts_match_floored_lookup_on_both_sides_of_zero() { let sr = SampleRate::HZ_48000; let tc = Timecode::parse("-00:00:01:00").unwrap(); assert_eq!(tc.to_samples(FrameRate::Fps25, sr), -48_000); for r in [FrameRate::Fps25, FrameRate::Fps29_97, FrameRate::Fps29_97Drop, FrameRate::Fps23_976] { - for f in [1i64, 7, 25, 1799, 107_892] { - assert_eq!(r.samples_at_frame(-f, sr), -r.samples_at_frame(f, sr), "{r:?} frame {f}"); + for f in [-107_892i64, -25, -7, -1, 0, 1, 2, 7, 25, 1799, 107_892] { + assert_eq!(r.frames_at(r.samples_at_frame(f, sr), sr), f, "{r:?} frame {f}"); } } + assert_eq!(FrameRate::Fps29_97.frames_at(FrameRate::Fps29_97.samples_at_frame(2, sr), sr), 2); } #[test] @@ -299,6 +309,7 @@ mod tests { assert_eq!((tc.seconds, tc.frames), (3, 4)); assert!(Timecode::parse("a:b").is_err()); assert!(Timecode::parse("1:2:3:4:5").is_err()); + assert!(Timecode::parse(&format!("{}:", "1".repeat(100_000))).is_err()); } #[test] From 07bac57ec2bfe1633f1b2fbb8047cdb3d2096729 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:58:41 +0000 Subject: [PATCH 106/520] add shared FigCraft history and branch schemas --- crates/figcraft-format/src/lib.rs | 146 +++++++++++++++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index 8fddd808d..5a53ea614 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -2,13 +2,129 @@ //! The neutral JSON header and version contract for FigCraft's native project format. //! App-specific document fields remain a generic flattened payload. use serde::de::{IgnoredAny, MapAccess, Visitor}; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; pub const EXTENSION: &str = "figcraft"; pub const FORMAT: &str = "figcraft"; pub const VERSION: u32 = 1; +fn is_false(value: &bool) -> bool { + !value +} + +/// One native version-history index entry. Invalid optional values decode to the same defaults as +/// the historical lenient reader; the app applies document-specific validity and issue reporting. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +pub struct VersionEntry { + pub id: String, + pub name: String, + pub description: String, + pub time: u64, + #[serde(skip_serializing_if = "is_false")] + pub auto: bool, + pub images: Vec, +} + +impl<'de> Deserialize<'de> for VersionEntry { + fn deserialize>(deserializer: D) -> Result { + let value = Value::deserialize(deserializer)?; + let string = |key: &str| value.get(key).and_then(Value::as_str).unwrap_or_default().to_owned(); + let images = value.get("images").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).map(str::to_owned).collect(); + Ok(Self { + id: string("id"), + name: string("name"), + description: string("description"), + time: value.get("time").and_then(Value::as_u64).unwrap_or(0), + auto: value.get("auto").and_then(Value::as_bool).unwrap_or(false), + images, + }) + } +} + +/// Native version-history index, oldest first. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +pub struct VersionIndex { + pub versions: Vec, +} + +impl<'de> Deserialize<'de> for VersionIndex { + fn deserialize>(deserializer: D) -> Result { + let value = Value::deserialize(deserializer)?; + let versions = value + .get("versions") + .and_then(Value::as_array) + .map(|entries| entries.iter().cloned().map(serde_json::from_value).collect::, _>>()) + .transpose() + .map_err(serde::de::Error::custom)? + .unwrap_or_default(); + Ok(Self { versions }) + } +} + +/// State stored on a native branch entry. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum BranchState { + #[default] + Active, + Merged, + Archived, +} + +/// One native branch index entry. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct BranchEntry { + pub id: String, + pub name: String, + pub time: u64, + pub client: u32, + pub state: BranchState, + pub base_images: Vec, + pub head_images: Vec, +} + +impl<'de> Deserialize<'de> for BranchEntry { + fn deserialize>(deserializer: D) -> Result { + let value = Value::deserialize(deserializer)?; + let string = |key: &str| value.get(key).and_then(Value::as_str).unwrap_or_default().to_owned(); + let images = + |key: &str| value.get(key).and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).map(str::to_owned).collect(); + let state = value.get("state").cloned().and_then(|v| serde_json::from_value(v).ok()).unwrap_or_default(); + let client = value.get("client").and_then(Value::as_u64).and_then(|v| u32::try_from(v).ok()).unwrap_or(0); + Ok(Self { + id: string("id"), + name: string("name"), + time: value.get("time").and_then(Value::as_u64).unwrap_or(0), + client, + state, + base_images: images("baseImages"), + head_images: images("headImages"), + }) + } +} + +/// Native branch index. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +pub struct BranchIndex { + pub branches: Vec, +} + +impl<'de> Deserialize<'de> for BranchIndex { + fn deserialize>(deserializer: D) -> Result { + let value = Value::deserialize(deserializer)?; + let branches = value + .get("branches") + .and_then(Value::as_array) + .map(|entries| entries.iter().cloned().map(serde_json::from_value).collect::, _>>()) + .transpose() + .map_err(serde::de::Error::custom)? + .unwrap_or_default(); + Ok(Self { branches }) + } +} + #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct Header { pub version: Option, @@ -92,6 +208,34 @@ mod tests { assert_eq!(h.version, Some(1)); assert_eq!(h.images, Some(vec!["abc".into()])); } + + #[test] + fn version_index_preserves_lenient_defaults_and_writes_compact_auto_flag() { + let index: VersionIndex = + serde_json::from_str(r#"{"extra":true,"versions":[{"id":"v1","time":7,"auto":false,"images":["a",3]},{}]}"#).unwrap(); + assert_eq!(index.versions.len(), 2); + assert_eq!(index.versions[0].images, vec!["a"]); + assert_eq!(index.versions[1], VersionEntry::default()); + let encoded = serde_json::to_value(&index).unwrap(); + assert!(encoded["versions"][0].get("auto").is_none()); + assert_eq!(encoded["versions"][0]["time"], 7); + } + + #[test] + fn branch_index_uses_native_camel_case_and_tolerates_invalid_optional_fields() { + let index: BranchIndex = serde_json::from_str( + r#"{"branches":[{"id":"b1","client":4294967296,"state":"unknown","baseImages":["a",false],"headImages":["b"]},{}]}"#, + ) + .unwrap(); + let first = &index.branches[0]; + assert_eq!(first.client, 0); + assert_eq!(first.state, BranchState::Active); + assert_eq!(first.base_images, vec!["a"]); + assert_eq!(first.head_images, vec!["b"]); + let encoded = serde_json::to_value(&index).unwrap(); + assert_eq!(encoded["branches"][0]["baseImages"][0], "a"); + assert!(encoded["branches"][0].get("base_images").is_none()); + } #[test] fn newer_version_is_reported_before_document_decode() { assert_eq!(check_version(&Header { version: Some(2), images: None }), Err(NewerVersion(2))); From a75b9df240d3e2eb98898fcbf99403ae5a1c73bb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 08:00:56 +0000 Subject: [PATCH 107/520] share FigCraft document expansion policy --- crates/figcraft-format/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index 5a53ea614..f086d4728 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -236,6 +236,16 @@ mod tests { assert_eq!(encoded["branches"][0]["baseImages"][0], "a"); assert!(encoded["branches"][0].get("base_images").is_none()); } + + #[test] + fn document_limit_keeps_compressible_files_and_caps_expansion() { + const MB: u64 = 1024 * 1024; + assert_eq!(document_limit(0, 0), MAX_MEMBER); + assert_eq!(document_limit(MB, 2 * MB), MAX_MEMBER); + assert_eq!(document_limit(u64::MAX, MB), MAX_MEMBER); + assert_eq!(document_limit(MAX_DOCUMENT, MAX_DOCUMENT), MAX_DOCUMENT); + assert_eq!(document_limit(244_362_923, 260 * MB), 244_362_923 * DOCUMENT_RATIO); + } #[test] fn newer_version_is_reported_before_document_decode() { assert_eq!(check_version(&Header { version: Some(2), images: None }), Err(NewerVersion(2))); @@ -515,6 +525,13 @@ pub const DOCUMENT_RATIO: u64 = 16; pub const MAX_DOCUMENT: u64 = 4 * 1024 * 1024 * 1024; #[cfg(target_pointer_width = "32")] pub const MAX_DOCUMENT: u64 = 1024 * 1024 * 1024; + +/// Upper bound for the expanded document member based on both the archive and member policy. +/// The floor preserves support for large but highly compressible documents; the ceiling prevents +/// a tiny archive from claiming an unbounded expansion. +pub fn document_limit(packed: u64, archive_bytes: u64) -> u64 { + packed.min(archive_bytes).saturating_mul(DOCUMENT_RATIO).clamp(MAX_MEMBER, MAX_DOCUMENT) +} /// Most version snapshots kept on save and read on open. pub const MAX_VERSIONS: usize = 100; /// Maximum version and branch name lengths, in Unicode scalar values. From d249336e76465ccbccf8b6570a9ed10a27b2cb1d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 08:10:02 +0000 Subject: [PATCH 108/520] move native archive read budgets into shared format --- crates/figcraft-format/src/lib.rs | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index f086d4728..c7e7261aa 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -246,6 +246,23 @@ mod tests { assert_eq!(document_limit(MAX_DOCUMENT, MAX_DOCUMENT), MAX_DOCUMENT); assert_eq!(document_limit(244_362_923, 260 * MB), 244_362_923 * DOCUMENT_RATIO); } + + #[test] + fn read_budget_enforces_member_and_archive_totals() { + let bytes = write_archive([ + Member { name: "one", bytes: b"12345678", compression: Compression::Stored }, + Member { name: "two", bytes: b"abcdefgh", compression: Compression::Stored }, + ]) + .unwrap(); + let mut archive = Archive::open(&bytes, ArchiveLimits { max_entries: 4, max_total_bytes: 128 }).unwrap(); + let mut budget = ReadBudget::new(10); + assert_eq!(budget.read(&mut archive, "one", 16).unwrap(), b"12345678"); + assert!(budget.read(&mut archive, "two", 16).unwrap_err().to_string().contains("contents together pass 10 bytes")); + + let mut member_budget = ReadBudget::new(100); + assert!(member_budget.read(&mut archive, "two", 4).unwrap_err().to_string().contains("two is too large")); + assert_eq!(member_budget.read(&mut archive, "two", 16).unwrap(), b"abcdefgh"); + } #[test] fn newer_version_is_reported_before_document_decode() { assert_eq!(check_version(&Header { version: Some(2), images: None }), Err(NewerVersion(2))); @@ -577,6 +594,48 @@ impl std::fmt::Display for FormatError { impl std::error::Error for FormatError {} +/// Tracks the total bytes unpacked from one native archive across its document, images, library, +/// history, and branch members. +#[derive(Clone, Debug)] +pub struct ReadBudget { + left: u64, + total: u64, +} + +impl ReadBudget { + pub fn new(total: u64) -> Self { + Self { left: total, total } + } + + fn passed(&self) -> String { + match self.total / (1024 * 1024) { + 0 => format!("too large: the file's contents together pass {} bytes", self.total), + mb => format!("too large: the file's contents together pass {mb} MB"), + } + } + + /// Read one native member within its own size limit and the remaining archive-wide budget. + /// A failed read that consumed a member is charged at its declared size. + pub fn read(&mut self, archive: &mut impl NativeArchive, name: &str, limit: u64) -> Result, FormatError> { + let declared = archive.declared_size(name); + if declared.is_some_and(|size| size > self.left && size <= limit) { + return Err(FormatError::Corrupt(format!("{name}: {}", self.passed()))); + } + let cap = limit.min(self.left); + let size = declared.unwrap_or(0); + let result = match archive.read_member(name, cap) { + Ok(Some(bytes)) => Ok(bytes), + Ok(None) => Err(FormatError::Corrupt(format!("{name}: not found"))), + Err(error) if error.is_limit() => Err(FormatError::Corrupt(format!("{name} is too large"))), + Err(error) => Err(FormatError::Corrupt(format!("{name}: {error}"))), + }; + if size <= cap { + self.left = self.left.saturating_sub(result.as_ref().map_or(size, |bytes| bytes.len() as u64)); + } + result + } +} + /// Whether `id` can name a version or branch: 1–64 ASCII letters, digits, `-` and `_`. pub fn is_valid_version_id(id: &str) -> bool { (1..=64).contains(&id.len()) && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_') From 1592923e6db6ed1458f7a4f2982b6e9db4057224 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 08:15:45 +0000 Subject: [PATCH 109/520] expose archive budget remaining diagnostics --- crates/figcraft-format/src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index c7e7261aa..3dc8e3a32 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -257,6 +257,8 @@ mod tests { let mut archive = Archive::open(&bytes, ArchiveLimits { max_entries: 4, max_total_bytes: 128 }).unwrap(); let mut budget = ReadBudget::new(10); assert_eq!(budget.read(&mut archive, "one", 16).unwrap(), b"12345678"); + assert!(budget.exceeds_remaining(&mut archive, "two")); + assert!(budget.exhausted_message().contains("contents together pass 10 bytes")); assert!(budget.read(&mut archive, "two", 16).unwrap_err().to_string().contains("contents together pass 10 bytes")); let mut member_budget = ReadBudget::new(100); @@ -614,6 +616,16 @@ impl ReadBudget { } } + /// Whether the member's declared uncompressed size exceeds what remains in this budget. + pub fn exceeds_remaining(&self, archive: &mut impl NativeArchive, name: &str) -> bool { + archive.declared_size(name).is_some_and(|size| size > self.left) + } + + /// Human-readable reason for leaving out a member after the archive-wide limit is spent. + pub fn exhausted_message(&self) -> String { + self.passed() + } + /// Read one native member within its own size limit and the remaining archive-wide budget. /// A failed read that consumed a member is charged at its declared size. pub fn read(&mut self, archive: &mut impl NativeArchive, name: &str, limit: u64) -> Result, FormatError> { From 370f6714edb1153dc9497afd79bd421bc0d6841f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:34:41 -0500 Subject: [PATCH 110/520] craft-stencilcraft-format: support typed document fields --- crates/stencilcraft-format/src/lib.rs | 140 ++++++++++++++++++-------- 1 file changed, 99 insertions(+), 41 deletions(-) diff --git a/crates/stencilcraft-format/src/lib.rs b/crates/stencilcraft-format/src/lib.rs index 709bf7ac7..9a0df0093 100644 --- a/crates/stencilcraft-format/src/lib.rs +++ b/crates/stencilcraft-format/src/lib.rs @@ -16,10 +16,9 @@ //! ``` //! //! [`DocumentFields`] owns the JSON field names, required and defaulted fields, and the -//! flattening rule. Its values are JSON because the consuming app owns the document model, -//! domain validation, and conversion between that model and the wire fields. The crate does not -//! open files or set a file-size policy; callers handle bounded file I/O. [`File::read_from`] -//! lets callers parse from a reader without first buffering source bytes in a string. +//! flattening rule. Its generic form lets an application use its typed domain values directly, +//! while keeping the wire schema and JSON encoding here. The crate does not open files or set a +//! file-size policy; callers handle bounded file I/O. [`File::read_from`] parses from a reader. use serde::de::{self, IgnoredAny, MapAccess, SeqAccess, Visitor}; use serde::{Deserialize, Deserializer, Serialize}; @@ -37,62 +36,96 @@ pub const FORMAT_VERSION: u64 = 1; pub const HEADER_KEY: &str = "stencilcraft"; /// Wire fields in the flattened native document object. +/// +/// The type parameters are the consuming app's typed model values. Field names, required fields, +/// version-1 defaults, and omission of default canvas values remain owned by this crate. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct DocumentFields { +#[serde(bound( + serialize = "Board: Serialize, Kerf: Serialize, Layers: Serialize, Image: Serialize, BoardSetup: Serialize + Default + PartialEq, Pages: Serialize + Default + PartialEq, Guides: Serialize + Default + PartialEq, Grid: Serialize + Default + PartialEq, Snap: Serialize + Default + PartialEq, Groups: Serialize + Default + PartialEq, Meta: Serialize + Default + PartialEq", + deserialize = "Board: Deserialize<'de>, Kerf: Deserialize<'de>, Layers: Deserialize<'de>, Image: Deserialize<'de>, BoardSetup: Deserialize<'de> + Default, Pages: Deserialize<'de> + Default, Guides: Deserialize<'de> + Default, Grid: Deserialize<'de> + Default, Snap: Deserialize<'de> + Default, Groups: Deserialize<'de> + Default, Meta: Deserialize<'de> + Default" +))] +pub struct DocumentFields< + Board = Value, + Kerf = Value, + Layers = Value, + Image = Value, + BoardSetup = Option, + Pages = Option, + Guides = Option, + Grid = Option, + Snap = Option, + Groups = Option, + Meta = Option, +> { /// Required millimetre artboard dimensions. - pub board: Value, + pub board: Board, /// Required kerf settings. - pub kerf: Value, + pub kerf: Kerf, /// Required drawing layers. - pub layers: Value, + pub layers: Layers, /// Next document object id; absent in early version 1 files, where it defaults to 1. #[serde(rename = "next-shape-id", default = "default_next_shape_id")] pub next_shape_id: u64, /// Embedded raster images; absent in early version 1 files, where it defaults to empty. #[serde(default)] - pub images: Vec, + pub images: Vec, /// Optional canvas page setup, omitted when absent. - #[serde(rename = "board-setup", default, skip_serializing_if = "Option::is_none")] - pub board_setup: Option, + #[serde(rename = "board-setup", default, skip_serializing_if = "is_default")] + pub board_setup: BoardSetup, /// Optional pages, omitted when absent. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub pages: Option, + #[serde(default, skip_serializing_if = "is_default")] + pub pages: Pages, /// Optional guides, omitted when absent. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub guides: Option, + #[serde(default, skip_serializing_if = "is_default")] + pub guides: Guides, /// Optional grid settings, omitted when absent. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub grid: Option, + #[serde(default, skip_serializing_if = "is_default")] + pub grid: Grid, /// Optional snap settings, omitted when absent. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub snap: Option, + #[serde(default, skip_serializing_if = "is_default")] + pub snap: Snap, /// Optional groups, omitted when absent. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub groups: Option, + #[serde(default, skip_serializing_if = "is_default")] + pub groups: Groups, /// Optional user metadata, omitted when absent. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub meta: Option, + #[serde(default, skip_serializing_if = "is_default")] + pub meta: Meta, +} + +fn is_default(value: &T) -> bool { + value == &T::default() } fn default_next_shape_id() -> u64 { 1 } -/// A native document and its flattened wire fields. +/// A native document and its flattened, typed wire fields. #[derive(Debug, Clone, PartialEq)] -pub struct File { - /// The fields after the header have been parsed, with app-domain objects still encoded as - /// JSON values. - pub document: DocumentFields, +pub struct File { + /// Typed document fields after the shared codec has checked the header and schema. + pub document: T, } -impl File { +impl File { /// Builds a native document from its wire fields. - pub fn new(document: DocumentFields) -> Self { + pub fn new(document: T) -> Self { Self { document } } - /// Parses JSON, validates the format header and version, then reads the wire fields. + /// Writes the current format header and flattened wire fields as indented JSON. + pub fn to_json_pretty(&self) -> Result + where + T: Serialize, + { + let raw = RawFileRef { header: HeaderRef { name: FORMAT_NAME, version: FORMAT_VERSION }, document: &self.document }; + let value = serde_json::to_value(raw).map_err(Error::Json)?; + serde_json::to_string_pretty(&value).map_err(Error::Json) + } +} + +impl File { + /// Parses JSON, validates the format header and version, then reads the untyped wire fields. pub fn read(text: &str) -> Result { let wire: RawWire = serde_json::from_str(text).map_err(Error::Json)?; Self::from_wire(wire) @@ -118,15 +151,40 @@ impl File { if version != FORMAT_VERSION { return Err(Error::Version { found: version, supported: FORMAT_VERSION }); } - let raw: RawFile = serde_json::from_value(Value::Object(wire.fields)).map_err(|source| Error::Document { source })?; + let raw: RawFile = serde_json::from_value(Value::Object(wire.fields)).map_err(|source| Error::Document { source })?; Ok(Self { document: raw.document }) } +} - /// Writes the current format header and flattened wire fields as indented JSON. - pub fn to_json_pretty(&self) -> Result { - let raw = RawFileRef { header: HeaderRef { name: FORMAT_NAME, version: FORMAT_VERSION }, document: &self.document }; - let value = serde_json::to_value(raw).map_err(Error::Json)?; - serde_json::to_string_pretty(&value).map_err(Error::Json) +impl File +where + T: for<'de> Deserialize<'de>, +{ + /// Parses JSON into a typed document model after validating the shared format header. + pub fn read_typed(text: &str) -> Result { + let wire: RawWire = serde_json::from_str(text).map_err(Error::Json)?; + Self::from_typed_wire(wire) + } + + /// Parses JSON from a reader into a typed document model. + pub fn read_typed_from(reader: impl Read) -> Result { + let wire: RawWire = serde_json::from_reader(reader).map_err(Error::Json)?; + Self::from_typed_wire(wire) + } + + fn from_typed_wire(wire: RawWire) -> Result { + let header = wire.fields.get(HEADER_KEY).and_then(Value::as_object).ok_or(Error::NotStencilCraft)?; + if header.get("name").and_then(Value::as_str) != Some(FORMAT_NAME) { + return Err(Error::NotStencilCraft); + } + let Some(version) = header.get("version").and_then(Value::as_u64) else { + return Err(Error::NotStencilCraft); + }; + if version != FORMAT_VERSION { + return Err(Error::Version { found: version, supported: FORMAT_VERSION }); + } + let raw: RawFile = serde_json::from_value(Value::Object(wire.fields)).map_err(|source| Error::Document { source })?; + Ok(Self { document: raw.document }) } } @@ -137,11 +195,11 @@ struct Header { } #[derive(Debug, Deserialize)] -struct RawFile { +struct RawFile { #[serde(rename = "stencilcraft")] _header: Header, #[serde(flatten)] - document: DocumentFields, + document: T, } /// A top-level streaming visitor that stores only fields owned by this wire schema. @@ -277,11 +335,11 @@ struct HeaderRef<'a> { } #[derive(Serialize)] -struct RawFileRef<'a> { +struct RawFileRef<'a, T> { #[serde(rename = "stencilcraft")] header: HeaderRef<'a>, #[serde(flatten)] - document: &'a DocumentFields, + document: &'a T, } /// A file was malformed, belongs to another format, or uses an unsupported version. From 166d0d21274aba0b761cd312438b7f075b5b8e5d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:46:47 -0500 Subject: [PATCH 111/520] craft-stencilcraft-document: share the native document model --- Cargo.lock | 13 + Cargo.toml | 1 + README.md | 3 +- crates/stencilcraft-document/Cargo.toml | 20 + crates/stencilcraft-document/README.md | 9 + crates/stencilcraft-document/src/canvas.rs | 580 ++++++++++++ crates/stencilcraft-document/src/geom.rs | 305 +++++++ crates/stencilcraft-document/src/lib.rs | 841 ++++++++++++++++++ .../stencilcraft-document/src/production.rs | 206 +++++ crates/stencilcraft-format/README.md | 15 +- 10 files changed, 1986 insertions(+), 7 deletions(-) create mode 100644 crates/stencilcraft-document/Cargo.toml create mode 100644 crates/stencilcraft-document/README.md create mode 100644 crates/stencilcraft-document/src/canvas.rs create mode 100644 crates/stencilcraft-document/src/geom.rs create mode 100644 crates/stencilcraft-document/src/lib.rs create mode 100644 crates/stencilcraft-document/src/production.rs diff --git a/Cargo.lock b/Cargo.lock index fd986012c..e9a21eda5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2198,6 +2198,19 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-stencilcraft-document" +version = "0.1.0" +dependencies = [ + "craft-atomic", + "craft-stencilcraft-format", + "craft-svg", + "kurbo 0.13.1", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-stencilcraft-format" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 170ad8641..12d961be1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -112,6 +112,7 @@ craft-settings = { path = "crates/settings" } craft-silhouette-studio = { path = "crates/silhouette-studio" } craft-svg = { path = "crates/svg" } craft-stencilcraft-format = { path = "crates/stencilcraft-format" } +craft-stencilcraft-document = { path = "crates/stencilcraft-document" } craft-svg-paths = { path = "crates/svg-paths" } craft-stream-codecs = { path = "crates/stream-codecs" } craft-text = { path = "crates/text" } diff --git a/README.md b/README.md index c79fa2aff..d94b57690 100644 --- a/README.md +++ b/README.md @@ -325,7 +325,8 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-sheet`](crates/sheet/README.md): spreadsheet vocabulary shared by `craft-xls`, `craft-xlsx` and `craft-ods`: cell positions and A1 text, cell values and error values, sheet-name quoting, Excel's built-in number formats and indexed colour palette. Not a file format; no dependencies. - [`craft-sf2`](crates/sf2/README.md): SoundFont 2 (`.sf2`) reader: the sample pool and, per preset, the key and velocity regions a note sounds, with tuning, loop points, pan, level and volume envelope resolved from the preset and instrument zones. Bounded on hostile input. From LoopCraft. - [`craft-silhouette-studio`](crates/silhouette-studio/src/lib.rs): bounded, fail-closed reader for Silhouette Studio `.studio` and `.studio3` documents: outlines, line colours and page size in millimetres. Protected designs are refused. -- [`craft-stencilcraft-format`](crates/stencilcraft-format/README.md): the versioned `.stencilcraft` JSON wire schema and envelope: required members, version 1 defaults, optional fields, and header checks. The app retains its document model and domain validation. +- [`craft-stencilcraft-format`](crates/stencilcraft-format/README.md): the versioned `.stencilcraft` JSON wire schema and envelope: required members, version 1 defaults, optional fields, and header checks. +- `craft-stencilcraft-document`: StencilCraft's typed document model, geometry helpers, validation, and native `.stencilcraft` codec. - [`craft-sketch`](crates/sketch/README.md): clean-room reader for the `.sketch` container (Sketch 48 and newer): the ZIP archive, the version cut, `document.json`, the pages in document order and images, as the JSON the file holds. Bounded on hostile input. Carved from FigCraft. - [`craft-svg`](crates/svg/src/lib.rs): SVG. `sniff` recognises SVG and SVGZ data; `svgz` unpacks SVGZ within a byte limit, decodes UTF-8, UTF-16 and ISO 8859-1, and compresses; `guard` bounds diff --git a/crates/stencilcraft-document/Cargo.toml b/crates/stencilcraft-document/Cargo.toml new file mode 100644 index 000000000..6d5ff6b25 --- /dev/null +++ b/crates/stencilcraft-document/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "craft-stencilcraft-document" +description = "StencilCraft document model, validation, geometry, and native file codec" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-atomic = { workspace = true } +craft-stencilcraft-format = { workspace = true } +craft-svg = { workspace = true } +kurbo = { workspace = true, features = ["serde"] } +serde = { workspace = true, features = ["rc"] } +serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/stencilcraft-document/README.md b/crates/stencilcraft-document/README.md new file mode 100644 index 000000000..d286c5789 --- /dev/null +++ b/crates/stencilcraft-document/README.md @@ -0,0 +1,9 @@ +# craft-stencilcraft-document + +Shared StencilCraft document model, validation, geometry helpers, and native file codec. +Applications re-export these model types rather than defining a second set of serializable +document types. The `.stencilcraft` header and typed wire schema are provided by +`craft-stencilcraft-format`. + +The crate's `Document::load_json` and `Document::save_json` APIs read and write the versioned +native format. File path I/O helpers preserve application paths in errors and use atomic writes. diff --git a/crates/stencilcraft-document/src/canvas.rs b/crates/stencilcraft-document/src/canvas.rs new file mode 100644 index 000000000..69878cb5e --- /dev/null +++ b/crates/stencilcraft-document/src/canvas.rs @@ -0,0 +1,580 @@ +//! Canvas model beyond shapes: pages (artboards), page presets, ruler guides, grid and +//! snap settings, document metadata and groups. Everything here is plain data stored in +//! the `.stencilcraft` file; behaviour lives in the engine. + +use std::collections::{BTreeMap, BTreeSet}; + +use serde::{Deserialize, Serialize}; + +use crate::{Board, DocError, Document, Shape}; + +/// Most pages a document may hold (the main board included). +pub const MAX_PAGES: usize = 100; +/// Most ruler guides a document may hold. +pub const MAX_GUIDES: usize = 1000; +/// Most groups a document may hold. +pub const MAX_GROUPS: usize = 100_000; +/// Deepest group nesting accepted. +pub const MAX_GROUP_DEPTH: usize = 64; +/// Longest title, page/layer/group name, colour or mat label, in bytes. +pub const MAX_NAME_BYTES: usize = 1024; +/// Longest notes text, in bytes. +pub const MAX_NOTES_BYTES: usize = 64 * 1024; +/// Largest stored thumbnail, in bytes. +pub const MAX_THUMBNAIL_BYTES: usize = 256 * 1024; + +const INCH_MM: f64 = 25.4; + +/// Labels that describe a page beyond its size: the mat it sits on, the media loaded and +/// the page colour shown on the canvas. All optional; empty means "not set". +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct PageSetup { + #[serde(default, skip_serializing_if = "String::is_empty")] + pub name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mat: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub media: Option, + /// Page colour, `#rrggbb`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub color: Option, + /// Cut mirrored (heat-transfer material is cut face down). + #[serde(default, skip_serializing_if = "crate::is_false")] + pub mirror: bool, + /// The page is printed with registration marks for Print & Cut. + #[serde(default, skip_serializing_if = "crate::is_false")] + pub registration_marks: bool, +} + +impl PageSetup { + pub fn is_empty(&self) -> bool { + self == &PageSetup::default() + } +} + +/// An extra page (artboard) placed on the document plane. The main board is page 0 at the +/// origin; extra pages sit beside it, and a shape belongs to the page under its centre. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Page { + pub x_mm: f64, + pub y_mm: f64, + pub width_mm: f64, + pub height_mm: f64, + #[serde(flatten)] + pub setup: PageSetup, +} + +impl Page { + pub fn rect(&self) -> kurbo::Rect { + kurbo::Rect::new(self.x_mm, self.y_mm, self.x_mm + self.width_mm, self.y_mm + self.height_mm) + } +} + +/// Common page and mat sizes. Sizes are portrait (width ≤ height); see [`Orientation`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub enum PagePreset { + #[serde(rename = "letter")] + Letter, + #[serde(rename = "a4")] + A4, + #[serde(rename = "mat-12x12")] + Mat12x12, + #[serde(rename = "mat-12x24")] + Mat12x24, + #[serde(rename = "joy-mat-4.5x6.5")] + JoyMat4_5x6_5, + #[serde(rename = "joy-mat-4.5x12")] + JoyMat4_5x12, + #[serde(rename = "joy-xtra-mat-8.5x12")] + JoyXtraMat8_5x12, + /// Width and height are given explicitly. + #[serde(rename = "custom")] + Custom, +} + +impl PagePreset { + /// Portrait size in millimetres; `None` for `Custom`. + pub fn size_mm(self) -> Option<(f64, f64)> { + let inches = |w: f64, h: f64| Some((w * INCH_MM, h * INCH_MM)); + match self { + PagePreset::Letter => inches(8.5, 11.0), + PagePreset::A4 => Some((210.0, 297.0)), + PagePreset::Mat12x12 => inches(12.0, 12.0), + PagePreset::Mat12x24 => inches(12.0, 24.0), + PagePreset::JoyMat4_5x6_5 => inches(4.5, 6.5), + PagePreset::JoyMat4_5x12 => inches(4.5, 12.0), + PagePreset::JoyXtraMat8_5x12 => inches(8.5, 12.0), + PagePreset::Custom => None, + } + } + + /// The mat label a mat preset implies (`None` for paper sizes and custom). + pub fn mat_label(self) -> Option<&'static str> { + match self { + PagePreset::Mat12x12 => Some("12x12"), + PagePreset::Mat12x24 => Some("12x24"), + PagePreset::JoyMat4_5x6_5 => Some("joy-4.5x6.5"), + PagePreset::JoyMat4_5x12 => Some("joy-4.5x12"), + PagePreset::JoyXtraMat8_5x12 => Some("joy-xtra-8.5x12"), + _ => None, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Orientation { + Portrait, + Landscape, +} + +impl Orientation { + /// `(w, h)` ordered for this orientation. + pub fn apply(self, (w, h): (f64, f64)) -> (f64, f64) { + let (short, long) = if w <= h { (w, h) } else { (h, w) }; + match self { + Orientation::Portrait => (short, long), + Orientation::Landscape => (long, short), + } + } +} + +/// Which way a ruler guide runs. A horizontal guide is the line `y = position`. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum GuideAxis { + Horizontal, + Vertical, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Guide { + pub axis: GuideAxis, + pub position_mm: f64, + #[serde(default, skip_serializing_if = "crate::is_false")] + pub locked: bool, +} + +/// The background grid: major lines every `spacing_mm`, split into `subdivisions`. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Grid { + pub spacing_mm: f64, + pub subdivisions: u32, + #[serde(default)] + pub visible: bool, +} + +impl Default for Grid { + fn default() -> Self { + Self { spacing_mm: 10.0, subdivisions: 1, visible: false } + } +} + +impl Grid { + /// Distance between snap lines: spacing over subdivisions. + pub fn step_mm(&self) -> f64 { + self.spacing_mm / f64::from(self.subdivisions.max(1)) + } +} + +/// Which targets the snapping service considers, and how close counts. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct SnapSettings { + #[serde(default)] + pub grid: bool, + #[serde(default = "crate::yes")] + pub guides: bool, + /// Edges and centres of other shapes and of pages. + #[serde(default = "crate::yes")] + pub objects: bool, + /// Smart guides: equal spacing between neighbours. + #[serde(default = "crate::yes")] + pub spacing: bool, + /// Snap to point: shapes' anchor points (Illustrator's View › Snap to Point). + #[serde(default)] + pub points: bool, + #[serde(default = "default_tolerance")] + pub tolerance_mm: f64, +} + +fn default_tolerance() -> f64 { + 2.0 +} + +impl Default for SnapSettings { + fn default() -> Self { + Self { grid: false, guides: true, objects: true, spacing: true, points: false, tolerance_mm: default_tolerance() } + } +} + +/// Title, notes and a small preview, stored in the file for file browsers and recovery. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct Metadata { + #[serde(default, skip_serializing_if = "String::is_empty")] + pub title: String, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub notes: String, + /// A small standalone SVG of the design (see `Document::render_thumbnail`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub thumbnail_svg: Option, +} + +impl Metadata { + pub fn is_empty(&self) -> bool { + self == &Metadata::default() + } +} + +/// A member of a group: a shape or another group. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum GroupChild { + Shape(u64), + Group(u64), +} + +/// Shapes and groups that move and select together. Grouping never changes how shapes +/// cut, nor their stacking order, which stays with their layers. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Group { + pub id: u64, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub name: String, + pub children: Vec, +} + +/// `#rrggbb` (or `#rgb`) to RGB; `None` for anything else. +pub fn parse_hex_color(text: &str) -> Option<[u8; 3]> { + let hex = text.strip_prefix('#')?; + let digit = |i: usize| u8::from_str_radix(hex.get(i..i + 1)?, 16).ok(); + match hex.len() { + 6 => { + let byte = |i: usize| u8::from_str_radix(hex.get(i..i + 2)?, 16).ok(); + Some([byte(0)?, byte(2)?, byte(4)?]) + } + 3 => Some([digit(0)? * 17, digit(1)? * 17, digit(2)? * 17]), + _ => None, + } +} + +/// Canonical lowercase `#rrggbb`. +pub fn format_hex_color([r, g, b]: [u8; 3]) -> String { + format!("#{r:02x}{g:02x}{b:02x}") +} + +/// Euclidean RGB distance (0 to about 441.7). +pub fn color_distance(a: [u8; 3], b: [u8; 3]) -> f64 { + a.iter().zip(b).map(|(x, y)| (f64::from(*x) - f64::from(y)).powi(2)).sum::().sqrt() +} + +fn check_name(text: &str, what: &str) -> Result<(), DocError> { + if text.len() > MAX_NAME_BYTES { + return Err(DocError::Model(format!("{what}: longer than {MAX_NAME_BYTES} bytes"))); + } + Ok(()) +} + +fn check_color(color: Option<&str>, what: &str) -> Result<(), DocError> { + match color { + Some(c) if parse_hex_color(c).is_none() => { + Err(DocError::Model(format!("{what}: `{}` is not a #rrggbb colour", c.chars().take(16).collect::()))) + } + _ => Ok(()), + } +} + +fn check_setup(setup: &PageSetup, what: &str) -> Result<(), DocError> { + check_name(&setup.name, what)?; + for label in [&setup.mat, &setup.media].into_iter().flatten() { + check_name(label, what)?; + } + check_color(setup.color.as_deref(), what) +} + +impl Document { + /// Pages, guides, grid, snapping, colours, metadata and groups: finite, within caps, + /// and groups form a forest (unknown shape members are allowed: they are pruned). + pub(crate) fn validate_canvas(&self) -> Result<(), DocError> { + let bad = |message: String| Err(DocError::Model(message)); + if self.pages.len() >= MAX_PAGES { + return bad(format!("pages: more than {MAX_PAGES}")); + } + check_setup(&self.board_setup, "board")?; + for (i, page) in self.pages.iter().enumerate() { + let finite = [page.x_mm, page.y_mm, page.width_mm, page.height_mm].iter().all(|v| v.is_finite() && v.abs() < 1e7); + if !finite || page.width_mm <= 0.0 || page.height_mm <= 0.0 { + return bad(format!("page {}: position and size must be finite, size positive", i + 1)); + } + check_setup(&page.setup, "page")?; + } + if self.guides.len() > MAX_GUIDES { + return bad(format!("guides: more than {MAX_GUIDES}")); + } + if self.guides.iter().any(|g| !g.position_mm.is_finite()) { + return bad("guides: positions must be finite".to_string()); + } + if !(self.grid.spacing_mm.is_finite() && (0.01..=10_000.0).contains(&self.grid.spacing_mm) && (1..=100).contains(&self.grid.subdivisions)) { + return bad("grid: spacing must be 0.01-10000 mm, subdivisions 1-100".to_string()); + } + if !(0.0..=1000.0).contains(&self.snap.tolerance_mm) { + return bad("snap: tolerance must be 0-1000 mm".to_string()); + } + for layer in &self.layers { + check_name(&layer.name, "layer name")?; + check_color(layer.color.as_deref(), "layer colour")?; + } + check_name(&self.meta.title, "title")?; + if self.meta.notes.len() > MAX_NOTES_BYTES { + return bad(format!("notes: longer than {MAX_NOTES_BYTES} bytes")); + } + if self.meta.thumbnail_svg.as_ref().is_some_and(|t| t.len() > MAX_THUMBNAIL_BYTES) { + return bad(format!("thumbnail: larger than {MAX_THUMBNAIL_BYTES} bytes")); + } + self.validate_groups() + } + + fn validate_groups(&self) -> Result<(), DocError> { + let bad = |message: String| Err(DocError::Model(message)); + if self.groups.len() > MAX_GROUPS { + return bad(format!("groups: more than {MAX_GROUPS}")); + } + let ids: BTreeSet = self.groups.iter().map(|g| g.id).collect(); + if ids.len() != self.groups.len() { + return bad("groups: ids must be unique".to_string()); + } + let mut parents: BTreeMap = BTreeMap::new(); + for group in &self.groups { + check_name(&group.name, "group name")?; + for child in &group.children { + if let GroupChild::Group(id) = child + && !ids.contains(id) + { + return bad(format!("group {}: member group {id} does not exist", group.id)); + } + if parents.insert(*child, group.id).is_some() { + return bad(format!("groups: {child:?} belongs to two groups")); + } + } + } + // With one parent each, a cycle is a walk up that never reaches a root. + for id in &ids { + let mut current = GroupChild::Group(*id); + let mut depth = 0; + while let Some(parent) = parents.get(¤t) { + depth += 1; + if depth > MAX_GROUP_DEPTH { + return bad(format!("groups: nesting deeper than {MAX_GROUP_DEPTH}, or a cycle, at group {id}")); + } + current = GroupChild::Group(*parent); + } + } + Ok(()) + } + + /// Whether shape `id` may not be edited: the shape or its layer is locked. + pub fn shape_locked(&self, id: u64) -> bool { + self.layers.iter().any(|layer| layer.shapes.iter().any(|s| s.id == id && (s.locked || layer.locked))) + } + + /// Index of the layer holding shape `id`. + pub fn layer_of_shape(&self, id: u64) -> Option { + self.layers.iter().position(|layer| layer.shapes.iter().any(|s| s.id == id)) + } + + pub fn group(&self, id: u64) -> Option<&Group> { + self.groups.iter().find(|g| g.id == id) + } + + /// The group directly holding `child`. + pub fn group_parent(&self, child: GroupChild) -> Option { + self.groups.iter().find(|g| g.children.contains(&child)).map(|g| g.id) + } + + /// Each group member's direct parent group; build once for many [`group_chain_in`] calls. + pub fn group_parent_map(&self) -> BTreeMap { + self.groups.iter().flat_map(|g| g.children.iter().map(move |c| (*c, g.id))).collect() + } + + /// Groups holding `child`, outermost first. + pub fn group_chain(&self, child: GroupChild) -> Vec { + group_chain_in(&self.group_parent_map(), child) + } + + /// Every existing shape inside group `id`, nested groups included, in document order. + pub fn group_shapes(&self, id: u64) -> Vec { + let mut members = BTreeSet::new(); + let mut seen = BTreeSet::new(); + let mut stack = vec![id]; + while let Some(gid) = stack.pop() { + if !seen.insert(gid) { + continue; + } + for child in self.group(gid).map_or(&[][..], |g| &g.children) { + match child { + GroupChild::Shape(s) => { + members.insert(*s); + } + GroupChild::Group(g) => stack.push(*g), + } + } + } + self.shapes().map(|(_, s)| s.id).filter(|s| members.contains(s)).collect() + } + + /// The groups without members that no longer exist; groups left empty go too. + pub fn pruned_groups(&self) -> Vec { + let shapes: BTreeSet = self.shapes().map(|(_, s)| s.id).collect(); + let mut groups = self.groups.clone(); + loop { + let before: usize = groups.iter().map(|g| g.children.len() + 1).sum(); + let live: BTreeSet = groups.iter().map(|g| g.id).collect(); + for group in &mut groups { + group.children.retain(|child| match child { + GroupChild::Shape(id) => shapes.contains(id), + GroupChild::Group(id) => live.contains(id), + }); + } + groups.retain(|g| !g.children.is_empty()); + let after: usize = groups.iter().map(|g| g.children.len() + 1).sum(); + if after == before { + return groups; + } + } + } + + /// A free group id. + pub fn next_group_id(&self) -> u64 { + self.groups.iter().map(|g| g.id).max().map_or(1, |m| m.saturating_add(1)) + } + + /// All pages: the main board (index 0, at the origin) then the extra pages. + pub fn all_pages(&self) -> Vec { + let mut pages = + vec![Page { x_mm: 0.0, y_mm: 0.0, width_mm: self.board.width_mm, height_mm: self.board.height_mm, setup: self.board_setup.clone() }]; + pages.extend(self.pages.iter().cloned()); + pages + } + + /// The page a shape belongs to: the first extra page under its bounds centre, else 0. + pub fn page_of_shape(&self, shape: &Shape) -> usize { + let Some(center) = shape.bounds().map(|b| b.center()) else { + return 0; + }; + self.pages.iter().position(|p| p.rect().contains(center)).map_or(0, |i| i + 1) + } + + /// One page as its own document, ready to export or cut: the board is the page, and + /// the shapes on that page are moved to its origin. `None` for an unknown page. + pub fn page_document(&self, index: usize) -> Option { + let page = self.all_pages().get(index)?.clone(); + let mut out = self.clone(); + out.board = Board { width_mm: page.width_mm, height_mm: page.height_mm }; + out.board_setup = page.setup.clone(); + out.pages.clear(); + out.guides.clear(); + let shift = kurbo::Affine::translate((-page.x_mm, -page.y_mm)); + for layer in &mut out.layers { + layer.shapes.retain(|s| self.page_of_shape(s) == index); + for shape in &mut layer.shapes { + shape.path.apply_affine(shift); + } + } + out.groups = out.pruned_groups(); + Some(out) + } + + /// A small standalone SVG preview of the board's output shapes, at most `max_bytes` + /// long: shapes past the budget are left out. Coordinates keep two decimals. + pub fn render_thumbnail(&self, max_bytes: usize) -> String { + let (w, h) = (self.board.width_mm, self.board.height_mm); + // Documents are y-up; flip into SVG's y-down space. The shared SVG writer owns markup, + // escaping, numeric formatting, and path-data formatting. + let mut svg = thumbnail_writer(w, h); + let mut empty = svg.clone(); + empty.line(1); + empty.close(); + empty.line(0); + empty.close(); + let empty_len = empty.finish().len(); + if empty_len > max_bytes { + return String::new(); + } + let mut remaining = max_bytes - empty_len; + 'layers: for layer in self.layers.iter().filter(|l| l.is_output()) { + for shape in layer.shapes.iter().filter(|s| !s.hidden) { + let mut element = craft_svg::write::SvgWriter::fragment(); + element.line(2); + element.tag("path").attr("d", craft_svg::write::path_data_fixed(&shape.path, 2)).empty(); + let element = element.finish(); + if element.len() > remaining { + break 'layers; + } + remaining -= element.len(); + svg.raw(&element); + } + } + svg.line(1); + svg.close(); + svg.line(0); + svg.close(); + svg.finish() + } +} + +fn thumbnail_writer(width_mm: f64, height_mm: f64) -> craft_svg::write::SvgWriter { + use craft_svg::write::{SvgWriter, num}; + + let mut writer = SvgWriter::fragment(); + writer.root().attr("viewBox", format!("0 0 {} {}", num(width_mm, 2), num(height_mm, 2))).open(); + writer + .tag("g") + .attr("transform", format!("matrix(1 0 0 -1 0 {})", num(height_mm, 2))) + .attr("fill", "none") + .attr("stroke", "#000") + .attr("stroke-width", num((width_mm.max(height_mm) / 200.0).max(0.01), 2)) + .open(); + writer +} + +/// Groups holding `child` according to `parents` ([`Document::group_parent_map`]), +/// outermost first. Stops at a cycle or past the depth cap. +pub fn group_chain_in(parents: &BTreeMap, child: GroupChild) -> Vec { + let mut chain = Vec::new(); + let mut current = child; + while let Some(parent) = parents.get(¤t) { + if chain.contains(parent) || chain.len() > MAX_GROUP_DEPTH { + break; + } + chain.push(*parent); + current = GroupChild::Group(*parent); + } + chain.reverse(); + chain +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn presets_and_orientation() { + let (w, h) = PagePreset::Letter.size_mm().unwrap(); + assert!((w - 215.9).abs() < 1e-9 && (h - 279.4).abs() < 1e-9); + assert_eq!(Orientation::Landscape.apply((210.0, 297.0)), (297.0, 210.0)); + assert_eq!(Orientation::Portrait.apply((297.0, 210.0)), (210.0, 297.0)); + assert!(PagePreset::Custom.size_mm().is_none()); + let json = serde_json::to_string(&PagePreset::JoyMat4_5x6_5).unwrap(); + assert_eq!(json, "\"joy-mat-4.5x6.5\""); + } + + #[test] + fn hex_colors() { + assert_eq!(parse_hex_color("#FF0080"), Some([255, 0, 128])); + assert_eq!(parse_hex_color("#f08"), Some([255, 0, 136])); + assert_eq!(parse_hex_color("ff0080"), None); + assert_eq!(parse_hex_color("#ff00"), None); + assert_eq!(parse_hex_color("#é0000"), None); + assert_eq!(format_hex_color([1, 2, 255]), "#0102ff"); + assert!(color_distance([0, 0, 0], [3, 4, 0]) - 5.0 < 1e-9); + } +} diff --git a/crates/stencilcraft-document/src/geom.rs b/crates/stencilcraft-document/src/geom.rs new file mode 100644 index 000000000..e569afeec --- /dev/null +++ b/crates/stencilcraft-document/src/geom.rs @@ -0,0 +1,305 @@ +//! Geometry primitives for StencilCraft: kurbo re-exports, millimetre helpers, +//! path flattening. +//! +//! The bottom layer of the workspace (L1 in `docs/architecture.md`): nothing here +//! depends on the document model, kerf, exports or the engine. All coordinates are +//! millimetres; coordinate-axis orientation is defined at each format boundary. + +pub use kurbo::{Affine, BezPath, Circle, CubicBez, Ellipse, Line, ParamCurve, Point, QuadBez, Rect, Shape, Vec2}; + +#[derive(Debug, thiserror::Error)] +#[error("geometry: {0}")] +pub struct GeomError(String); + +pub type Result = std::result::Result; + +/// The smallest length that can meaningfully exist on an artboard; below this a +/// value is treated as zero. Values above this but non-finite are always an error. +pub const MM_EPSILON: f64 = 1e-9; + +/// Millimetres per inch: the one inch/mm conversion factor (inch-based mats and page sizes). +pub const MM_PER_INCH: f64 = 25.4; + +/// Converts inches at an input boundary to document millimetres. +pub const fn inches_to_mm(inches: f64) -> f64 { + inches * MM_PER_INCH +} + +/// Converts document millimetres to inches at a display boundary. +pub const fn mm_to_inches(mm: f64) -> f64 { + mm / MM_PER_INCH +} + +/// Map an artboard's downward-growing Y coordinate to a Cartesian Y coordinate. +/// +/// The board's top is `height_mm` and its bottom is zero in the resulting space. +/// Values outside the board remain outside; non-finite inputs or results are rejected. +pub fn artboard_y_to_cartesian(y_mm: f64, height_mm: f64) -> Result { + check_finite_mm(y_mm, "artboard y")?; + check_finite_mm(height_mm, "artboard height")?; + check_finite_mm(height_mm - y_mm, "cartesian y") +} + +/// Rejects a non-finite millimetre length before it can poison a document. +pub fn check_finite_mm(value: f64, what: &'static str) -> Result { + if !value.is_finite() { + return Err(GeomError(format!("{what}: not a finite length"))); + } + let _ = MM_EPSILON; + Ok(value) +} + +/// A flattened subpath: the raw points of a contour, plus whether the source path +/// closed it. Used by kerf (offsetting) and every polyline-based exporter. +#[derive(Debug, Clone, PartialEq)] +pub struct Polyline { + pub points: Vec, + pub closed: bool, +} + +impl Polyline { + pub fn from_path(path: &BezPath) -> Vec { + path_to_polylines(path, DEFAULT_FLATTEN_STEPS) + } +} + +/// Flattening resolution: samples per Bézier segment. 32 steps at artboard scale +/// keeps chord error far below a typical kerf width. +pub const DEFAULT_FLATTEN_STEPS: usize = 32; + +/// Convert a path into polylines by sampling Bézier segments (`steps` samples per +/// segment). Closed subpaths produce `closed: true` polylines that do not repeat +/// the start point. +pub fn path_to_polylines(path: &BezPath, steps: usize) -> Vec { + let steps = steps.max(2); + let mut out: Vec = Vec::new(); + let mut current: Option = None; + let mut start = Point::ZERO; + let mut cursor = Point::ZERO; + + for elem in path.elements() { + match *elem { + kurbo::PathEl::MoveTo(p) => { + if let Some(done) = current.take() { + out.push(done); + } + start = p; + cursor = p; + current = Some(Polyline { points: vec![p], closed: false }); + } + kurbo::PathEl::LineTo(p) => { + if let Some(pl) = current.as_mut() { + pl.points.push(p); + } + cursor = p; + } + kurbo::PathEl::ClosePath => { + if let Some(pl) = current.as_mut() { + pl.closed = true; + } + cursor = start; + } + kurbo::PathEl::QuadTo(c, p) => { + if let Some(pl) = current.as_mut() { + let quad = QuadBez::new(cursor, c, p); + for i in 1..steps { + let t = i as f64 / steps as f64; + pl.points.push(quad.eval(t)); + } + } + cursor = p; + } + kurbo::PathEl::CurveTo(c1, c2, p) => { + if let Some(pl) = current.as_mut() { + let cubic = CubicBez::new(cursor, c1, c2, p); + for i in 1..steps { + let t = i as f64 / steps as f64; + pl.points.push(cubic.eval(t)); + } + } + cursor = p; + } + } + } + if let Some(done) = current.take() { + out.push(done); + } + out +} + +/// Signed area of a closed polyline (shoelace): positive when wound +/// counter-clockwise with y up. An open polyline contributes 0. +pub fn signed_area(points: &[Point]) -> f64 { + if points.len() < 3 { + return 0.0; + } + let mut sum = 0.0; + for i in 0..points.len() { + let a = points[i]; + let b = points[(i + 1) % points.len()]; + sum += a.x * b.y - b.x * a.y; + } + sum / 2.0 +} + +/// Bounds of a set of points, or `None` when empty. +pub fn bounds_of_points(points: &[Point]) -> Option { + let mut it = points.iter(); + let first = *it.next()?; + let mut acc = Rect::from_points(first, first); + for p in it { + acc = acc.union(Rect::from_points(*p, *p)); + } + Some(acc) +} + +/// Axis-aligned bounds of every point of every polyline. +pub fn bounds_of_polylines(lines: &[Polyline]) -> Option { + let mut acc: Option = None; + for pl in lines { + if let Some(r) = bounds_of_points(&pl.points) { + acc = Some(match acc { + Some(prev) => prev.union(r), + None => r, + }); + } + } + acc +} + +/// Exact (non-flattened) control-point bounds of a path through kurbo. +pub fn bounds_of_shape(path: &BezPath) -> Option { + path.bounding_box().as_rect() +} + +/// Display units. The document is always millimetres; convert only where a value meets +/// the user (fields, rulers, callouts), through these helpers. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Unit { + #[default] + Mm, + Cm, + In, + Pt, +} + +impl Unit { + pub const ALL: [Unit; 4] = [Unit::Mm, Unit::Cm, Unit::In, Unit::Pt]; + + /// Millimetres in one of this unit. + pub fn mm_per_unit(self) -> f64 { + match self { + Unit::Mm => 1.0, + Unit::Cm => 10.0, + Unit::In => 25.4, + Unit::Pt => 25.4 / 72.0, + } + } + + pub fn to_mm(self, value: f64) -> f64 { + value * self.mm_per_unit() + } + + pub fn from_mm(self, mm: f64) -> f64 { + mm / self.mm_per_unit() + } + + pub fn suffix(self) -> &'static str { + match self { + Unit::Mm => "mm", + Unit::Cm => "cm", + Unit::In => "in", + Unit::Pt => "pt", + } + } + + /// `mm` shown in this unit with `decimals` places and the suffix, e.g. `1.000 in`. + pub fn format(self, mm: f64, decimals: usize) -> String { + format!("{:.*} {}", decimals.min(9), self.from_mm(mm), self.suffix()) + } + + /// Parses `12.5`, `12.5mm`, `1 in`, `3cm`, `72 pt` or `2"` into millimetres; a bare + /// number is in this unit. `None` for anything else or a non-finite result. + pub fn parse_to_mm(self, text: &str) -> Option { + let text = text.trim(); + let (number, unit) = Unit::ALL + .iter() + .find_map(|u| text.strip_suffix(u.suffix()).map(|n| (n, *u))) + .or_else(|| text.strip_suffix('"').map(|n| (n, Unit::In))) + .unwrap_or((text, self)); + let value: f64 = number.trim().parse().ok()?; + let mm = unit.to_mm(value); + mm.is_finite().then_some(mm) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn units_convert_format_and_parse() { + assert_eq!(Unit::In.to_mm(1.0), 25.4); + assert!((Unit::Pt.from_mm(25.4) - 72.0).abs() < 1e-12); + assert_eq!(Unit::Cm.from_mm(15.0), 1.5); + assert_eq!(Unit::In.format(25.4, 3), "1.000 in"); + assert_eq!(Unit::Mm.parse_to_mm("12.5"), Some(12.5)); + assert_eq!(Unit::In.parse_to_mm("2"), Some(50.8)); + assert_eq!(Unit::Mm.parse_to_mm(" 1 in "), Some(25.4)); + assert_eq!(Unit::Mm.parse_to_mm("2\""), Some(50.8)); + assert_eq!(Unit::Mm.parse_to_mm("3cm"), Some(30.0)); + assert_eq!(Unit::Mm.parse_to_mm("abc"), None); + assert_eq!(Unit::Mm.parse_to_mm("1e400"), None); + for unit in Unit::ALL { + assert!((unit.to_mm(unit.from_mm(123.4)) - 123.4).abs() < 1e-9); + } + } + + fn square(side: f64) -> BezPath { + let mut p = BezPath::new(); + p.move_to(Point::ZERO); + p.line_to(Point::new(side, 0.0)); + p.line_to(Point::new(side, side)); + p.line_to(Point::new(0.0, side)); + p.close_path(); + p + } + + #[test] + fn flatten_square_closes() { + let lines = path_to_polylines(&square(10.0), 4); + assert_eq!(lines.len(), 1); + let pl = &lines[0]; + assert!(pl.closed); + assert_eq!(pl.points.len(), 4); + assert_eq!(pl.points[0], Point::ZERO); + assert_eq!(pl.points[2], Point::new(10.0, 10.0)); + } + + #[test] + fn area_sign_of_square() { + let lines = path_to_polylines(&square(10.0), 4); + let area = signed_area(&lines[0].points); + assert!((area - 100.0).abs() < 1e-9, "got {area}"); + } + + #[test] + fn cubic_flattens_to_steps_per_segment() { + let mut p = BezPath::new(); + p.move_to(Point::ZERO); + p.curve_to(Point::new(10.0, 0.0), Point::new(20.0, 10.0), Point::new(0.0, 20.0)); + p.close_path(); + let lines = path_to_polylines(&p, 8); + assert_eq!(lines.len(), 1); + // The move plus 7 interior samples; the segment's endpoint is the move origin + // after close, so it is not repeated. + assert_eq!(lines[0].points.len(), 8); + } + + #[test] + fn bounds_union_works() { + let lines = path_to_polylines(&square(10.0), 4); + let b = bounds_of_polylines(&lines).unwrap(); + assert_eq!(b, Rect::new(0.0, 0.0, 10.0, 10.0)); + } +} diff --git a/crates/stencilcraft-document/src/lib.rs b/crates/stencilcraft-document/src/lib.rs new file mode 100644 index 000000000..a966536c5 --- /dev/null +++ b/crates/stencilcraft-document/src/lib.rs @@ -0,0 +1,841 @@ +//! StencilCraft document model and domain validation. +//! +//! A document holds an artboard in millimetres, kerf settings and layers of shapes. +//! Every read path returns `Result` with a calm, printable error: a malformed file +//! must never crash (`AGENTS.md` › "Never crash"), because it is untrusted input. + +pub mod canvas; +pub mod geom; + +use crate::geom::{GeomError, bounds_of_shape, check_finite_mm}; +pub use canvas::{Grid, Group, GroupChild, Guide, GuideAxis, Metadata, Orientation, Page, PagePreset, PageSetup, SnapSettings}; +use kurbo::BezPath; +use serde::{Deserialize, Serialize}; + +pub mod production; +pub use production::{CombineOp, GroupKind, Operation, SendAssign, ShapeProduction, SourceGroup}; + +/// Native format identity and supported version, defined by the shared wire-format crate. +pub use craft_stencilcraft_format::{FORMAT_NAME, FORMAT_VERSION}; + +#[derive(Debug, thiserror::Error)] +pub enum DocError { + #[error("{path}: {source}")] + Io { path: String, source: std::io::Error }, + + #[error("{path}: not a stencilcraft file")] + NotAStencilcraftFile { path: String }, + + #[error("{path}: format version {found}, this build reads {expected}")] + FormatVersion { path: String, found: u64, expected: u64 }, + + #[error("{path}: {source}")] + Parse { path: String, source: serde_json::Error }, + + #[error("{path}: {message}")] + Structure { path: String, message: String }, + + #[error("document: {0}")] + Model(String), + + #[error(transparent)] + Geom(#[from] GeomError), +} + +/// How a shape is physically produced on a machine. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum CutStyle { + /// Trace the closed outline with the cutting tool. + Cut, + /// Trace without cutting: scoring, engraving outlines, marking. + Engrave, +} + +/// Kerf settings held in the document; applying them is `stencilcraft-kerf`'s job. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct KerfSettings { + /// Nominal kerf (material removed by the tool), in millimetres. Compensation + /// offsets contours by `width_mm / 2`. + pub width_mm: f64, + /// Whether exports should carry compensated contours already. + pub apply_to_exports: bool, +} + +impl Default for KerfSettings { + fn default() -> Self { + Self { width_mm: 0.0, apply_to_exports: false } + } +} + +/// The cutting sheet: artboard size in millimetres. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Board { + pub width_mm: f64, + pub height_mm: f64, +} + +impl Default for Board { + fn default() -> Self { + // A default sheet for vinyl work: 100 × 150 mm. + Self { width_mm: 100.0, height_mm: 150.0 } + } +} + +/// One layer of the document: a group of shapes with a production style and a +/// visibility flag. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Layer { + pub name: String, + pub kind: CutStyle, + pub visible: bool, + pub shapes: Vec, + /// A locked layer's shapes cannot be selected or edited. + #[serde(default, skip_serializing_if = "is_false")] + pub locked: bool, + /// A template (guide) layer is shown on the canvas but never exported or cut. + #[serde(default, skip_serializing_if = "is_false")] + pub template: bool, + /// Material colour label, `#rrggbb`; Color Sync merges similar ones. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub color: Option, +} + +impl Layer { + pub fn new(name: impl Into, kind: CutStyle) -> Self { + Self { name: name.into(), kind, visible: true, shapes: Vec::new(), locked: false, template: false, color: None } + } + + /// Whether exports and cuts include this layer: visible and not a template. + pub fn is_output(&self) -> bool { + self.visible && !self.template + } + + /// Whether newly created shapes may land here: not locked, not a template. + pub fn takes_new_shapes(&self) -> bool { + !self.locked && !self.template + } + + /// Sum of per-shape bounds; `None` when the layer has no shapes or only empty + /// paths. + pub fn bounds(&self) -> Option { + let mut acc: Option = None; + for shape in &self.shapes { + if let Some(r) = shape.bounds() { + acc = Some(match acc { + Some(prev) => prev.union(r), + None => r, + }); + } + } + acc + } +} + +/// A path plus its production style. kurbo's `BezPath` serialises with the +/// workspace's `serde` feature on — no wrapper needed. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Shape { + pub id: u64, + pub path: BezPath, + pub style: CutStyle, + /// Hidden shapes are not drawn, selected or exported. + #[serde(default, skip_serializing_if = "is_false")] + pub hidden: bool, + /// Locked shapes cannot be selected or edited until unlocked. + #[serde(default, skip_serializing_if = "is_false")] + pub locked: bool, + /// Live text: the parameters `path` was laid out from, so the text stays editable + /// (`text.edit` lays it out again). Absent for plain paths and in older files. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub text: Option>, + /// A tool-specific operation for this path (score, emboss, engrave, foil, punch); + /// `None` is a plain cut or engrave pass by `style`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub tool: Option, + /// Production metadata (operation, colours, attach, contours, combine sources). + #[serde(default, skip_serializing_if = "ShapeProduction::is_default")] + pub production: ShapeProduction, +} + +/// Score and emboss tool tip. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Tip { + #[default] + Fine, + Wide, +} + +fn one_pass() -> u32 { + 1 +} + +/// A tool-specific operation a machine backend runs instead of a plain cut/engrave pass. +/// Paths tagged with one live on an engrave layer (they must never be cut through), except +/// `punch`, whose path is the hole outline for display and fallback cutting. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(tag = "op", rename_all = "kebab-case")] +pub enum ToolOp { + /// Crease along the path (scoring stylus/wheel). + Score { + #[serde(default)] + tip: Tip, + #[serde(default = "one_pass")] + passes: u32, + }, + /// Press the path into the material (emboss, or deboss from the other side). + Emboss { + #[serde(default)] + tip: Tip, + #[serde(default = "one_pass")] + passes: u32, + #[serde(default)] + deboss: bool, + }, + /// Rotary/power engraving tool following the path. + PowerEngrave, + /// Heat pen pressing foil along the path. + HeatPen, + /// One punch per contour, at the contour's bounds centre: a backend with a punch tool + /// plunges there instead of cutting the circle. + Punch { diameter_mm: f64 }, +} + +fn is_false(value: &bool) -> bool { + !*value +} + +/// A live text object's parameters. The shape's `path` always holds the laid-out outlines, +/// so readers that ignore `text` (older builds, every export) still cut the right thing. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct TextSpec { + pub text: String, + pub font: FontSpec, + /// Em size in points (1 pt = 25.4/72 mm). + pub size: f64, + /// Anchor in mm: the first line's baseline at the alignment point. + pub x: f64, + pub y: f64, + #[serde(default)] + pub alignment: TextAlign, + /// Space added between letters, in percent of the em (0 = the font's own spacing). + #[serde(default)] + pub letter_spacing: f64, + /// Distance between lines in percent of the normal line distance (100 = normal). + #[serde(default = "hundred")] + pub line_spacing: f64, + /// Pair kerning from the font's tables. + #[serde(default = "yes")] + pub kerning: bool, + /// Top-to-bottom columns instead of left-to-right lines. + #[serde(default, skip_serializing_if = "is_false")] + pub vertical: bool, + /// Merge overlapping letter outlines into one cut path (script fonts). + #[serde(default, skip_serializing_if = "is_false")] + pub weld: bool, + #[serde(default, skip_serializing_if = "TextFlow::is_straight")] + pub flow: TextFlow, + /// Per-letter nudges, by character index into `text` (newlines count). + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub letters: Vec, + /// Affine `[a, b, c, d, e, f]` applied after layout: the moves, turns and scales made to + /// the shape since its text was laid out. + #[serde(default = "identity", skip_serializing_if = "is_identity")] + pub transform: [f64; 6], +} + +/// A font by family and style, optionally from a font file. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct FontSpec { + pub family: String, + #[serde(default = "regular")] + pub style: String, + /// The font file the family came from, loaded again when the text is laid out again. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub file: Option, +} + +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum TextAlign { + #[default] + Left, + #[serde(alias = "center", alias = "centre")] + Centre, + Right, + /// Every line but the last is stretched to the widest line. + Justify, +} + +/// How the text runs: straight lines, one line bent round a circle, or one line along a path. +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "kebab-case")] +pub enum TextFlow { + #[default] + Straight, + /// One line bent into an arc of this diameter in mm: positive arches up, negative dips. + Curve { diameter_mm: f64 }, + /// One line along `path` (mm). `start` is where the anchor sits, as a fraction of the + /// path's length; `offset_mm` lifts the letters off the path; `flip` runs the text the + /// other way round, on the other side. `shape` names the shape the path was copied from. + OnPath { + #[serde(default)] + path: BezPath, + #[serde(default, skip_serializing_if = "Option::is_none")] + shape: Option, + #[serde(default)] + start: f64, + #[serde(default)] + offset_mm: f64, + #[serde(default)] + flip: bool, + }, +} + +impl TextFlow { + pub fn is_straight(&self) -> bool { + matches!(self, TextFlow::Straight) + } +} + +/// A nudge to one letter: moved by `dx_mm`, `dy_mm` and turned `rotate_deg` about its origin. +#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)] +pub struct LetterOffset { + pub index: usize, + #[serde(default)] + pub dx_mm: f64, + #[serde(default)] + pub dy_mm: f64, + #[serde(default)] + pub rotate_deg: f64, +} + +fn hundred() -> f64 { + 100.0 +} + +fn yes() -> bool { + true +} + +fn regular() -> String { + "Regular".to_string() +} + +/// The identity affine in `TextSpec::transform` form. +pub const IDENTITY_TRANSFORM: [f64; 6] = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + +fn identity() -> [f64; 6] { + IDENTITY_TRANSFORM +} + +fn is_identity(xf: &[f64; 6]) -> bool { + *xf == IDENTITY_TRANSFORM +} + +impl Shape { + pub fn bounds(&self) -> Option { + bounds_of_shape(&self.path) + } +} + +/// Most placed raster images one document may hold. +pub const MAX_IMAGES: usize = 256; +/// Most base64 PNG bytes one placed image may carry (about 96 MiB of PNG). +pub const MAX_IMAGE_DATA: usize = 128 * 1024 * 1024; + +/// A placed raster image (a trace source, never cut). Pixels are PNG, base64-encoded so +/// the document stays JSON; `Arc` keeps undo snapshots from copying the bytes. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct RasterImage { + /// Shares the shape id counter, so an id names one object in the document. + pub id: u64, + pub name: String, + pub x_mm: f64, + pub y_mm: f64, + pub width_mm: f64, + pub height_mm: f64, + pub width_px: u32, + pub height_px: u32, + pub png_base64: std::sync::Arc, +} + +impl RasterImage { + fn validate(&self) -> Result<(), DocError> { + for (value, what) in [(self.x_mm, "image x"), (self.y_mm, "image y"), (self.width_mm, "image width"), (self.height_mm, "image height")] { + check_finite_mm(value, what)?; + } + if self.width_mm <= 0.0 || self.height_mm <= 0.0 || self.width_px == 0 || self.height_px == 0 { + return Err(DocError::Model(format!("image {}: width and height must be positive", self.id))); + } + if self.png_base64.len() > MAX_IMAGE_DATA { + return Err(DocError::Model(format!("image {}: pixel data exceeds the 128 MiB limit", self.id))); + } + Ok(()) + } +} + +/// A StencilCraft document: artboard, kerf configuration, layers. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Document { + pub board: Board, + pub kerf: KerfSettings, + pub layers: Vec, + /// Next free shape id; maintained by whoever mutates the document. + #[serde(default)] + pub next_shape_id: u64, + /// Placed raster images, bottom first, drawn under the cut layers. + #[serde(default)] + pub images: Vec, + /// Mat, media, colour and name of the main board (page 0). + #[serde(default)] + pub board_setup: PageSetup, + /// Extra pages beside the main board. + #[serde(default)] + pub pages: Vec, + #[serde(default)] + pub guides: Vec, + #[serde(default)] + pub grid: Grid, + #[serde(default)] + pub snap: SnapSettings, + /// Groups; members that no longer exist are ignored and dropped on save. + #[serde(default)] + pub groups: Vec, + #[serde(default)] + pub meta: Metadata, +} + +/// Typed fields in the native wire format. Field names and version-1 defaults are owned by +/// `craft-stencilcraft-format`; this alias supplies only StencilCraft's domain types. +type NativeFileFields = craft_stencilcraft_format::DocumentFields< + Board, + KerfSettings, + Vec, + RasterImage, + PageSetup, + Vec, + Vec, + Grid, + SnapSettings, + Vec, + Metadata, +>; + +impl Default for Document { + fn default() -> Self { + Self { + board: Board::default(), + kerf: KerfSettings::default(), + layers: vec![Layer::new("cut", CutStyle::Cut)], + next_shape_id: 1, + images: Vec::new(), + board_setup: PageSetup::default(), + pages: Vec::new(), + guides: Vec::new(), + grid: Grid::default(), + snap: SnapSettings::default(), + groups: Vec::new(), + meta: Metadata::default(), + } + } +} + +impl Document { + /// Shapes across all layers, in order, paired with their layer index. + pub fn shapes(&self) -> impl Iterator { + self.layers.iter().enumerate().flat_map(|(li, l)| l.shapes.iter().map(move |s| (li, s))) + } + + pub fn shape_count(&self) -> usize { + self.layers.iter().map(|l| l.shapes.len()).sum() + } + + pub fn get_shape(&self, id: u64) -> Option<&Shape> { + self.shapes().map(|(_, s)| s).find(|s| s.id == id) + } + + pub fn get_shape_mut(&mut self, id: u64) -> Option<&mut Shape> { + self.layers.iter_mut().flat_map(|l| l.shapes.iter_mut()).find(|s| s.id == id) + } + + /// Allocates the next shape id and records the shape in `layer_index`. + /// Errors come out printable: messages name the layer and the fault. + pub fn push_shape(&mut self, layer_index: usize, path: BezPath, style: CutStyle) -> Result { + if layer_index >= self.layers.len() { + return Err(DocError::Model(format!("layer {layer_index}: no such layer (document has {})", self.layers.len()))); + } + let Some(layer) = self.layers.get_mut(layer_index) else { + return Err(DocError::Model(format!("layer {layer_index}: disappeared"))); + }; + let id = self.next_shape_id; + self.next_shape_id = self.next_shape_id.checked_add(1).ok_or_else(|| DocError::Model("shape ids exhausted".to_string()))?; + let shape = Shape { id, path, style, hidden: false, locked: false, tool: None, text: None, production: ShapeProduction::default() }; + layer.shapes.push(shape.clone()); + Ok(shape) + } + + /// Places a raster image with the next object id and returns that id. + pub fn push_image(&mut self, mut image: RasterImage) -> Result { + if self.images.len() >= MAX_IMAGES { + return Err(DocError::Model(format!("a document holds at most {MAX_IMAGES} images"))); + } + image.id = self.next_shape_id; + image.validate()?; + self.next_shape_id = self.next_shape_id.checked_add(1).ok_or_else(|| DocError::Model("shape ids exhausted".to_string()))?; + let id = image.id; + self.images.push(image); + Ok(id) + } + + pub fn get_image(&self, id: u64) -> Option<&RasterImage> { + self.images.iter().find(|image| image.id == id) + } + + /// Checks the board is a sane sheet: positive, finite dimensions. + pub fn validate(&self) -> Result<(), DocError> { + check_finite_mm(self.board.width_mm, "board width")?; + check_finite_mm(self.board.height_mm, "board height")?; + check_finite_mm(self.kerf.width_mm, "kerf width")?; + if self.board.width_mm <= 0.0 || self.board.height_mm <= 0.0 { + return Err(DocError::Model("board: width and height must be positive".to_string())); + } + if self.kerf.width_mm < 0.0 { + return Err(DocError::Model("kerf: width cannot be negative".to_string())); + } + if self.images.len() > MAX_IMAGES { + return Err(DocError::Model(format!("a document holds at most {MAX_IMAGES} images"))); + } + self.images.iter().try_for_each(RasterImage::validate)?; + self.validate_canvas() + } + + /// Serialises with the format header. The header wraps the document so a reader + /// knows what it is before reading the rest. Canvas fields are written only when + /// set, so a plain document saves exactly as it did before they existed. + pub fn save_json(&self) -> Result { + self.validate()?; + let fields = NativeFileFields { + board: self.board.clone(), + kerf: self.kerf.clone(), + layers: self.layers.clone(), + next_shape_id: self.next_shape_id, + images: self.images.clone(), + board_setup: self.board_setup.clone(), + pages: self.pages.clone(), + guides: self.guides.clone(), + grid: self.grid.clone(), + snap: self.snap.clone(), + groups: self.pruned_groups(), + meta: self.meta.clone(), + }; + craft_stencilcraft_format::File::::new(fields) + .to_json_pretty() + .map_err(|e| DocError::Structure { path: "output".to_string(), message: e.to_string() }) + } + + /// Parses a document from JSON with calm, positioned error messages. + pub fn load_json(json: &str) -> Result { + load_json_named(json, "input") + } +} + +fn load_json_named(json: &str, path: &str) -> Result { + let file = craft_stencilcraft_format::File::::read_typed(json).map_err(|e| match e { + craft_stencilcraft_format::Error::Json(source) => DocError::Parse { path: path.to_string(), source }, + craft_stencilcraft_format::Error::NotStencilCraft => DocError::NotAStencilcraftFile { path: path.to_string() }, + craft_stencilcraft_format::Error::Version { found, supported } => { + DocError::FormatVersion { path: path.to_string(), found, expected: supported } + } + craft_stencilcraft_format::Error::Document { source } => structure_err(path, &source), + _ => DocError::NotAStencilcraftFile { path: path.to_string() }, + })?; + let fields = file.document; + let mut doc = Document { + board: fields.board, + kerf: fields.kerf, + layers: fields.layers, + next_shape_id: fields.next_shape_id, + images: fields.images, + board_setup: fields.board_setup, + pages: fields.pages, + guides: fields.guides, + grid: fields.grid, + snap: fields.snap, + groups: fields.groups, + meta: fields.meta, + }; + doc.validate()?; + doc.groups = doc.pruned_groups(); + Ok(doc) +} + +fn structure_err(path: &str, e: &serde_json::Error) -> DocError { + DocError::Structure { path: path.to_string(), message: e.to_string() } +} + +/// File helpers: errors keep the path so the message names the file. +pub fn save_to(doc: &Document, path: &str) -> Result<(), DocError> { + let json = doc.save_json()?; + craft_atomic::atomic_write(std::path::Path::new(path), json.as_bytes()).map_err(|source| DocError::Io { path: path.to_string(), source }) +} + +pub fn load_from(path: &str) -> Result { + let json = std::fs::read_to_string(path).map_err(|source| DocError::Io { path: path.to_string(), source })?; + load_json_named(&json, path) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::geom::Point; + + fn rect_path(x: f64, y: f64, w: f64, h: f64) -> BezPath { + let mut p = BezPath::new(); + p.move_to(Point::new(x, y)); + p.line_to(Point::new(x + w, y)); + p.line_to(Point::new(x + w, y + h)); + p.line_to(Point::new(x, y + h)); + p.close_path(); + p + } + + fn sample() -> Document { + let mut doc = Document::default(); + doc.kerf.width_mm = 0.2; + doc.push_shape(0, rect_path(10.0, 20.0, 30.0, 40.0), CutStyle::Cut).unwrap(); + doc + } + + #[test] + fn round_trip() { + let doc = sample(); + let json = doc.save_json().unwrap(); + let back = Document::load_json(&json).unwrap(); + assert_eq!(doc, back); + } + + #[test] + fn flags_round_trip_and_default_off() { + let mut doc = sample(); + let plain = doc.save_json().unwrap(); + assert!(!plain.contains("hidden") && !plain.contains("locked")); + doc.layers[0].shapes[0].hidden = true; + doc.layers[0].shapes[0].locked = true; + let back = Document::load_json(&doc.save_json().unwrap()).unwrap(); + assert_eq!(doc, back); + } + + #[test] + fn text_round_trips_and_minimal_text_loads() { + let mut doc = sample(); + let plain = doc.save_json().unwrap(); + assert!(!plain.contains("\"text\"")); + doc.layers[0].shapes[0].text = Some(Box::new(TextSpec { + text: "Hi".into(), + font: FontSpec { family: "Sans".into(), style: "Bold".into(), file: None }, + size: 72.0, + x: 1.0, + y: 2.0, + alignment: TextAlign::Centre, + letter_spacing: -5.0, + line_spacing: 120.0, + kerning: false, + vertical: false, + weld: true, + flow: TextFlow::Curve { diameter_mm: 50.0 }, + letters: vec![LetterOffset { index: 1, dx_mm: 1.0, ..LetterOffset::default() }], + transform: IDENTITY_TRANSFORM, + })); + let back = Document::load_json(&doc.save_json().unwrap()).unwrap(); + assert_eq!(doc, back); + // A minimal hand-written text object fills in the defaults. + let json = plain.replace( + "\"style\": \"cut\"", + "\"style\": \"cut\", \"text\": {\"text\": \"A\", \"font\": {\"family\": \"X\"}, \"size\": 12, \"x\": 0, \"y\": 0}", + ); + let spec = Document::load_json(&json).unwrap().layers[0].shapes[0].text.clone().unwrap(); + assert_eq!(spec.line_spacing, 100.0); + assert!(spec.kerning && spec.flow.is_straight()); + assert_eq!(spec.font.style, "Regular"); + } + + #[test] + fn header_written() { + let json = sample().save_json().unwrap(); + let v: serde_json::Value = serde_json::from_str(&json).unwrap(); + let h = &v["stencilcraft"]; + assert_eq!(h["name"].as_str(), Some(FORMAT_NAME)); + assert_eq!(h["version"].as_u64(), Some(FORMAT_VERSION)); + } + + #[test] + fn junk_is_an_error_not_a_crash() { + assert!(Document::load_json("not json at all").is_err()); + assert!(matches!(Document::load_json("{}"), Err(DocError::NotAStencilcraftFile { .. }))); + } + + #[test] + fn future_version_is_rejected_calmly() { + let json = r#"{"stencilcraft": {"name": "stencilcraft", "version": 99}}"#; + assert!(matches!(Document::load_json(json), Err(DocError::FormatVersion { found: 99, expected: 1, .. }))); + } + + #[test] + fn missing_field_has_structure_error() { + let json = r#"{"stencilcraft": {"name": "stencilcraft", "version": 1}, + "board": {"width_mm": 1, "height_mm": 1}}"#; + assert!(matches!( + Document::load_json(json), + Err(DocError::Structure { ref message, .. }) if message.contains("kerf") + )); + } + + #[test] + fn images_round_trip_and_share_the_id_counter() { + let mut doc = canvas_sample(); + let id = doc + .push_image(RasterImage { + id: 0, + name: "photo".to_string(), + x_mm: 0.0, + y_mm: 0.0, + width_mm: 10.0, + height_mm: 5.0, + width_px: 2, + height_px: 1, + png_base64: "AAAA".into(), + }) + .unwrap(); + assert_eq!(id, 3); + let back = Document::load_json(&doc.save_json().unwrap()).unwrap(); + assert_eq!(back, doc); + let mut bad = doc.clone(); + bad.images[0].width_mm = -1.0; + assert!(bad.validate().is_err()); + } + + #[test] + fn push_shape_assigns_ids() { + let mut doc = Document::default(); + let s = doc.push_shape(0, rect_path(0.0, 0.0, 5.0, 5.0), CutStyle::Cut).unwrap(); + assert_eq!(s.id, 1); + let s2 = doc.push_shape(0, rect_path(5.0, 5.0, 5.0, 5.0), CutStyle::Cut).unwrap(); + assert_eq!(s2.id, 2); + assert!(matches!(doc.push_shape(9, rect_path(0.0, 0.0, 1.0, 1.0), CutStyle::Cut), Err(DocError::Model(_)))); + } + + fn canvas_sample() -> Document { + let mut doc = sample(); + doc.push_shape(0, rect_path(50.0, 50.0, 5.0, 5.0), CutStyle::Cut).unwrap(); + doc.board_setup.mat = Some("12x12".to_string()); + doc.pages.push(Page { + x_mm: 120.0, + y_mm: 0.0, + width_mm: 210.0, + height_mm: 297.0, + setup: PageSetup { name: "A4".to_string(), color: Some("#ffeedd".to_string()), ..PageSetup::default() }, + }); + doc.guides.push(Guide { axis: GuideAxis::Vertical, position_mm: 12.5, locked: true }); + doc.grid.spacing_mm = 5.0; + doc.grid.subdivisions = 4; + doc.snap.grid = true; + doc.layers[0].color = Some("#112233".to_string()); + doc.layers.push(Layer { template: true, locked: true, ..Layer::new("guide", CutStyle::Cut) }); + doc.groups = vec![ + Group { id: 1, name: "inner".to_string(), children: vec![GroupChild::Shape(1)] }, + Group { id: 2, name: String::new(), children: vec![GroupChild::Group(1), GroupChild::Shape(2)] }, + ]; + doc.meta.title = "Card".to_string(); + doc.meta.notes = "for mum".to_string(); + doc.meta.thumbnail_svg = Some(doc.render_thumbnail(4096)); + doc + } + + #[test] + fn canvas_fields_round_trip() { + let doc = canvas_sample(); + let back = Document::load_json(&doc.save_json().unwrap()).unwrap(); + assert_eq!(doc, back); + assert_eq!(back.group_chain(GroupChild::Shape(1)), vec![2, 1]); + assert_eq!(back.group_shapes(2), vec![1, 2]); + } + + #[test] + fn plain_documents_save_without_canvas_keys() { + let json = sample().save_json().unwrap(); + for key in ["pages", "guides", "grid", "snap", "groups", "meta", "board-setup", "template"] { + assert!(!json.contains(key), "{key} written for a plain document"); + } + } + + #[test] + fn version_1_files_without_canvas_fields_still_load() { + for name in ["starter", "poster", "stencil-frame"] { + let path = format!("{}/../../examples/{name}.stencilcraft", env!("CARGO_MANIFEST_DIR")); + let doc = load_from(&path).unwrap(); + assert!(doc.pages.is_empty() && doc.groups.is_empty() && doc.guides.is_empty()); + assert_eq!(doc.grid, Grid::default()); + assert!(doc.layers.iter().all(|l| !l.template && !l.locked)); + } + } + + #[test] + fn dangling_group_members_are_pruned_not_fatal() { + let mut doc = canvas_sample(); + doc.layers[0].shapes.retain(|s| s.id != 1); + // Group 1 loses its only member and disappears; group 2 keeps shape 2. + let back = Document::load_json(&doc.save_json().unwrap()).unwrap(); + assert_eq!(back.groups.len(), 1); + assert_eq!(back.groups[0].children, vec![GroupChild::Shape(2)]); + } + + #[test] + fn bad_canvas_data_is_rejected_calmly() { + type DocumentMutation = Box; + let cases: Vec = vec![ + Box::new(|d| d.groups[0].children.push(GroupChild::Group(2))), + Box::new(|d| d.groups[1].children.push(GroupChild::Shape(1))), + Box::new(|d| d.groups[0].id = 2), + Box::new(|d| d.pages[0].width_mm = f64::NAN), + Box::new(|d| d.guides[0].position_mm = f64::INFINITY), + Box::new(|d| d.grid.subdivisions = 0), + Box::new(|d| d.layers[0].color = Some("red".to_string())), + Box::new(|d| d.meta.notes = "x".repeat(canvas::MAX_NOTES_BYTES + 1)), + Box::new(|d| d.snap.tolerance_mm = f64::NAN), + ]; + for (i, corrupt) in cases.iter().enumerate() { + let mut doc = canvas_sample(); + corrupt(&mut doc); + assert!(doc.validate().is_err(), "case {i} validated"); + assert!(doc.save_json().is_err(), "case {i} saved"); + } + // The same faults in a file are load errors. + let json = canvas_sample().save_json().unwrap().replace("#112233", "#zz"); + assert!(Document::load_json(&json).is_err()); + } + + #[test] + fn page_document_takes_the_shapes_on_that_page() { + let mut doc = canvas_sample(); + doc.push_shape(0, rect_path(130.0, 10.0, 10.0, 10.0), CutStyle::Cut).unwrap(); + let page = doc.page_document(1).unwrap(); + assert_eq!((page.board.width_mm, page.board.height_mm), (210.0, 297.0)); + assert_eq!(page.shape_count(), 1); + let b = page.layers[0].shapes[0].bounds().unwrap(); + assert!((b.x0 - 10.0).abs() < 1e-9 && (b.y0 - 10.0).abs() < 1e-9); + assert_eq!(doc.page_document(0).unwrap().shape_count(), 2); + assert!(doc.page_document(5).is_none()); + } + + #[test] + fn thumbnail_respects_its_budget() { + let doc = canvas_sample(); + let full = doc.render_thumbnail(64 * 1024); + assert!(full.starts_with("")); + assert_eq!(full.matches("")); + } +} diff --git a/crates/stencilcraft-document/src/production.rs b/crates/stencilcraft-document/src/production.rs new file mode 100644 index 000000000..c03717c0b --- /dev/null +++ b/crates/stencilcraft-document/src/production.rs @@ -0,0 +1,206 @@ +//! Per-shape production metadata: what the machine does with a shape (operation, colour and +//! material keys for mats and cut groups), Attach groups, hidden contours, a manual mat, and the +//! retained source shapes of a non-destructive Combine or a Flatten. Everything defaults to +//! "unset" and is skipped when saving, so older files and files without production data are +//! unchanged. + +use kurbo::{Affine, BezPath, PathEl}; +use serde::{Deserialize, Serialize}; + +use crate::Shape; + +/// What the machine does with a shape. `None` on a shape means "follow its cut/engrave style". +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Operation { + /// Cut the outline (the default for cut shapes). + Cut, + /// Sticker kiss cut: through the face material only, the backing stays whole. + KissCut, + /// Sticker die cut: through the face material and the backing. + DieCut, + /// Draw the outline with a pen. + Draw, + /// Crease a fold line. + Score, + /// Engrave the line (the default for engrave shapes). + Engrave, + /// A dashed tear line. + Perforate, + /// Print on a home printer, then cut around the print using registration marks. + PrintThenCut, + /// Shown on the canvas only, never sent to the machine or printer. + Guide, +} + +/// A per-shape tool assignment from the send preview, overriding the group settings. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum SendAssign { + /// Every line of the shape with tool 1. + Tool1, + /// Only the shape's outer outline with tool 1 (inner contours are skipped). + Tool1Outline, + Tool2, + Tool2Outline, + /// Leave the shape out of the job. + Ignore, +} + +/// A Combine's shape operation. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum CombineOp { + Unite, + /// The back-most member minus every other member. + Subtract, + Intersect, + /// Area covered by an odd number of members. + Exclude, +} + +/// Why a shape keeps its sources. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", tag = "kind", content = "operation")] +pub enum GroupKind { + /// Non-destructive Combine: the path is the operation's result; Uncombine restores the + /// members. + Combine(CombineOp), + /// Flatten: the members are the print artwork, the path is its cut outline. + Flatten, +} + +/// The shapes a Combine or Flatten replaced, kept so the action can be undone later. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct SourceGroup { + pub kind: GroupKind, + pub members: Vec, + /// The grouped shape's path when the group was made. Comparing it with the current path + /// tells how the group was moved, rotated or scaled since, so the members follow. + pub path: BezPath, +} + +impl SourceGroup { + /// The members moved the way the grouped shape moved since grouping (`current` is its + /// path now). An edit that is not an affine map of the old outline (node edits) falls back + /// to mapping the old bounds onto the new ones. + pub fn members_now(&self, current: &BezPath) -> Vec { + let map = fit_affine(&self.path, current).unwrap_or_else(|| bounds_map(&self.path, current)); + self.members + .iter() + .map(|m| { + let mut m = m.clone(); + m.path = map * m.path; + m + }) + .collect() + } +} + +fn endpoints(path: &BezPath) -> Vec { + path.elements().iter().filter_map(|el| el.end_point()).collect() +} + +/// The affine map taking `old` onto `new` point for point, when the two have the same +/// structure and one exists (within 0.01 mm). +fn fit_affine(old: &BezPath, new: &BezPath) -> Option { + let (a, b) = (endpoints(old), endpoints(new)); + if a.len() != b.len() || a.len() < 3 { + return None; + } + let p0 = *a.first()?; + // The point farthest from p0, then the one making the largest triangle. + let i = (0..a.len()).max_by(|&i, &j| (a[i] - p0).hypot2().total_cmp(&(a[j] - p0).hypot2()))?; + let j = (0..a.len()).max_by(|&j, &k| (a[i] - p0).cross(a[j] - p0).abs().total_cmp(&(a[i] - p0).cross(a[k] - p0).abs()))?; + let (u, v) = (a[i] - p0, a[j] - p0); + let det = u.cross(v); + if det.abs() < 1e-9 { + // Collinear outline (or no movement information): a pure translation still works. + let t = Affine::translate(*b.first()? - p0); + return a.iter().zip(&b).all(|(p, q)| (t * *p - *q).hypot() < 0.01).then_some(t); + } + let q0 = *b.first()?; + let (s, t) = (b[i] - q0, b[j] - q0); + // M maps u→s and v→t: M = [s t]·[u v]⁻¹. + let inv = [v.y / det, -v.x / det, -u.y / det, u.x / det]; + let m = [s.x * inv[0] + t.x * inv[2], s.y * inv[0] + t.y * inv[2], s.x * inv[1] + t.x * inv[3], s.y * inv[1] + t.y * inv[3]]; + let linear = Affine::new([m[0], m[1], m[2], m[3], 0.0, 0.0]); + let map = Affine::translate(q0.to_vec2()) * linear * Affine::translate(-p0.to_vec2()); + a.iter().zip(&b).all(|(p, q)| (map * *p - *q).hypot() < 0.01).then_some(map) +} + +fn bounds_map(old: &BezPath, new: &BezPath) -> Affine { + use kurbo::Shape as _; + let (a, b) = (old.bounding_box(), new.bounding_box()); + let sx = if a.width() > 1e-9 { b.width() / a.width() } else { 1.0 }; + let sy = if a.height() > 1e-9 { b.height() / a.height() } else { 1.0 }; + Affine::translate((b.x0, b.y0)) * Affine::scale_non_uniform(sx, sy) * Affine::translate((-a.x0, -a.y0)) +} + +/// Production metadata of one shape. Every field is optional. +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +pub struct ShapeProduction { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub operation: Option, + /// Canonical `#rrggbb` line colour: the cut-by-line-colour key. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub line_color: Option, + /// Canonical `#rrggbb` fill colour: the print colour and cut-by-fill-colour key. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub fill_color: Option, + /// Material name; shapes on different materials go on different mats. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub material: Option, + /// Attach group: shapes sharing it keep their relative positions on one mat. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub attach: Option, + /// Manual mat: the piece goes on this mat index instead of its automatic one. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mat: Option, + /// Tool assignment from the send preview. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub send: Option, + /// Contours (subpath indices) hidden by Contour: kept in the path, left out of exports. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub hidden_contours: Vec, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub group: Option>, +} + +impl ShapeProduction { + pub fn is_default(&self) -> bool { + *self == Self::default() + } +} + +impl Shape { + /// The path the machine sees: the shape's path without the contours Contour hid. + pub fn cut_path(&self) -> std::borrow::Cow<'_, BezPath> { + if self.production.hidden_contours.is_empty() { + return std::borrow::Cow::Borrowed(&self.path); + } + let mut out = BezPath::new(); + let mut contour = 0usize; + let mut started = false; + for el in self.path.elements() { + if matches!(el, PathEl::MoveTo(_)) { + if started { + contour += 1; + } + started = true; + } + if !self.production.hidden_contours.contains(&contour) { + out.push(*el); + } + } + std::borrow::Cow::Owned(out) + } + + /// The shape's operation, falling back to its cut/engrave style. + pub fn operation(&self) -> Operation { + self.production.operation.unwrap_or(match self.style { + crate::CutStyle::Cut => Operation::Cut, + crate::CutStyle::Engrave => Operation::Engrave, + }) + } +} diff --git a/crates/stencilcraft-format/README.md b/crates/stencilcraft-format/README.md index 2391d3c56..c5b32582d 100644 --- a/crates/stencilcraft-format/README.md +++ b/crates/stencilcraft-format/README.md @@ -3,8 +3,10 @@ Reader and writer for the versioned `.stencilcraft` JSON file. The JSON header is `{"stencilcraft":{"name":"stencilcraft","version":1}}`; the document fields share the top-level object with the header. `DocumentFields` owns field names, flattening, required fields, -version 1 defaults, and omitted optional members. Its values remain JSON so the consuming app -owns the domain model, geometry validation, and field-to-model mapping. +version 1 defaults, and omission of default canvas members. Its type parameters let consumers use +their typed model fields directly; `File::>::read_typed` and +`to_json_pretty` handle the shared schema and JSON conversion. StencilCraft's typed model and +domain validation live in `craft-stencilcraft-document`. The crate does not open files or set a file-size policy. Callers perform bounded file I/O before calling `File::read`. @@ -13,10 +15,11 @@ calling `File::read`. craft-stencilcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "" } ``` -`File::read` parses JSON, validates the name and exact supported version, reads `DocumentFields`, -and ignores unknown members for version 1 compatibility. `File::new` and `to_json_pretty` write -the current header and flattened fields. Absent `next-shape-id` defaults to 1 and absent `images` -defaults to an empty list, matching early version 1 files. +`File::read` parses JSON into the default JSON-valued schema. For typed models, use +`File::>::read_typed`; both readers validate the name and exact supported +version and ignore unknown members for version 1 compatibility. `File::new` and `to_json_pretty` +write the current header and flattened fields. Absent `next-shape-id` defaults to 1 and absent +`images` defaults to an empty list, matching early version 1 files. `cargo test -p craft-stencilcraft-format` covers round-trip wire shape, version 1 defaults, unknown members, wrong and missing headers, unsupported versions, malformed JSON, and missing From 1f3be111b8dc0b1d7d05195357df0889b5b507fe Mon Sep 17 00:00:00 2001 From: alehel Date: Fri, 13 Nov 2026 09:56:22 +0100 Subject: [PATCH 112/520] craft-audio: drop a short last SBR patch before the patch limit A header whose sixth patch is short (96 kHz, start 3, stop 9) was rejected, though that patch is dropped and five remain. Co-Authored-By: Claude Opus 5.5 --- crates/audio/src/aac/sbr/freq.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/audio/src/aac/sbr/freq.rs b/crates/audio/src/aac/sbr/freq.rs index d12fbdc02..7bb372dc4 100644 --- a/crates/audio/src/aac/sbr/freq.rs +++ b/crates/audio/src/aac/sbr/freq.rs @@ -211,8 +211,9 @@ fn patches(master: &[usize], k0: usize, kx: usize, m: usize, fs: u32) -> Option< let (mut msb, mut usb) = (k0, kx); let mut out: Vec<(usize, usize)> = Vec::new(); // each pass either adds a patch or lowers `msb` to `kx`; a stream that does neither twice - // running cannot finish - for _ in 0..2 * (MAX_PATCHES + 1) { + // running cannot finish. One patch over the limit may still be a short last one that is + // dropped below. + for _ in 0..2 * (MAX_PATCHES + 2) { // the highest master border the patch can reach from below k0 let mut j = k + 1; let (sb, odd) = loop { @@ -227,7 +228,7 @@ fn patches(master: &[usize], k0: usize, kx: usize, m: usize, fs: u32) -> Option< if count > 0 { let start = (k0 - odd).checked_sub(count)?; out.push((start, count)); - if out.len() > MAX_PATCHES { + if out.len() > MAX_PATCHES + 1 { return None; } (usb, msb) = (sb, sb); @@ -241,7 +242,7 @@ fn patches(master: &[usize], k0: usize, kx: usize, m: usize, fs: u32) -> Option< if out.len() > 1 && out.last().is_some_and(|p| p.1 < 3) { out.pop(); } - return (!out.is_empty()).then_some(out); + return (!out.is_empty() && out.len() <= MAX_PATCHES).then_some(out); } } None @@ -350,6 +351,15 @@ mod tests { assert_eq!((t.k0, t.kx, t.m), (32, 32, 32)); } + /// At 96 kHz this header builds patches of 6, 6, 6, 6, 5 and 2 channels: the short sixth is + /// dropped, as any short last patch is, and the five left are within the limit. + #[test] + fn a_short_sixth_patch_is_dropped_before_the_limit() { + let t = FreqTables::new(&header(3, 9, 0, 0, false, 2), 96_000).unwrap(); + assert_eq!((t.k0, t.kx + t.m), (8, 39)); + assert_eq!(t.patches.iter().map(|p| p.1).collect::>(), [6, 6, 6, 6, 5]); + } + /// A typical 44.1 kHz header: start channel 12 + offset, stop near 64, three noise bands. #[test] fn a_44k_header_has_the_expected_layout() { From b373553e988b9f8ff4ac2c09a98a80fc94d1620b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:54:43 -0500 Subject: [PATCH 113/520] Add shared vector path import APIs --- Cargo.lock | 372 +++++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 + crates/dxf/Cargo.toml | 1 + crates/dxf/src/lib.rs | 1 + crates/dxf/src/paths.rs | 218 +++++++++++++++++++++++ crates/eps/Cargo.toml | 8 + crates/eps/src/lib.rs | 2 + crates/eps/src/paths.rs | 75 ++++++++ crates/pdf/Cargo.toml | 2 + crates/pdf/src/lib.rs | 2 + crates/pdf/src/paths.rs | 127 ++++++++++++++ 11 files changed, 801 insertions(+), 9 deletions(-) create mode 100644 crates/dxf/src/paths.rs create mode 100644 crates/eps/src/paths.rs create mode 100644 crates/pdf/src/paths.rs diff --git a/Cargo.lock b/Cargo.lock index cd280f90c..a0ad1277b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1384,6 +1384,7 @@ version = "0.1.0" dependencies = [ "craft-codepage", "encoding_rs", + "kurbo 0.13.1", "log", "thiserror 2.0.21", ] @@ -1400,6 +1401,12 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" +dependencies = [ + "kurbo 0.13.1", + "thiserror 2.0.21", + "vectorcraft-doc", + "vectorcraft-eps", +] [[package]] name = "craft-epub" @@ -1954,6 +1961,17 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-pdf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +dependencies = [ + "hayro-interpret", + "hayro-syntax", + "krilla", + "thiserror 2.0.21", +] + [[package]] name = "craft-pdn" version = "0.1.0" @@ -2010,7 +2028,7 @@ name = "craft-plate-test" version = "0.1.0" dependencies = [ "craft-cms", - "craft-pdf", + "craft-pdf 0.1.0", "hayro-syntax", "serde", "serde_json", @@ -2071,7 +2089,7 @@ name = "craft-prepress" version = "0.1.0" dependencies = [ "craft-cms", - "craft-pdf", + "craft-pdf 0.1.0", "craft-plate-test", "kurbo 0.13.1", "linesweeper", @@ -3442,7 +3460,7 @@ dependencies = [ "emath", "epaint_default_fonts", "font-types 0.12.7", - "harfrust", + "harfrust 0.12.0", "log", "nohash-hasher", "parking_lot", @@ -3670,6 +3688,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "foldhash" version = "0.2.0" @@ -3968,7 +3992,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d99fc21d493812643aae86d53b7bbd02f376434a90317e8a790bc209fdd6605e" dependencies = [ "bytemuck", - "foldhash", + "foldhash 0.2.0", "hashbrown 0.17.1", "log", "peniko", @@ -3985,7 +4009,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282a26c1e23de04bdab3e34a21b6f877a479b96737792516b9b8b8f69b6661be" dependencies = [ "bytemuck", - "foldhash", + "foldhash 0.2.0", "hashbrown 0.17.1", "log", "peniko", @@ -4160,6 +4184,28 @@ dependencies = [ "smallvec", ] +[[package]] +name = "harfrust" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "948d0741125ba89cd3e1c23e5642415b6ade7e1d29d67ba25fb925b533e989d6" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "once_cell", + "read-fonts 0.43.3", + "smallvec", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash 0.1.5", +] + [[package]] name = "hashbrown" version = "0.16.1" @@ -4168,7 +4214,7 @@ checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" dependencies = [ "allocator-api2", "equivalent", - "foldhash", + "foldhash 0.2.0", ] [[package]] @@ -4177,7 +4223,7 @@ version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" dependencies = [ - "foldhash", + "foldhash 0.2.0", ] [[package]] @@ -6085,6 +6131,15 @@ dependencies = [ "libredox", ] +[[package]] +name = "ordered-channel" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95be4d57809897b5a7539fc15a7dfe0e84141bc3dfaa2e9b1b27caa90acf61ab" +dependencies = [ + "crossbeam-channel", +] + [[package]] name = "ordered-float" version = "5.5.0" @@ -7035,6 +7090,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "read-fonts" +version = "0.43.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "005c8acf251756c478b0bf402885bfd88a1476020c4c7e6060edc1aa68da38ea" +dependencies = [ + "bytemuck", + "font-types 0.12.7", + "once_cell", +] + [[package]] name = "read-fonts" version = "0.44.0" @@ -7053,7 +7119,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4edc8db0d972de88e53a085b1f01fb3fbdae7bb0cb7a0ce2b2f8285a412fe3ef" dependencies = [ "bytemuck", - "foldhash", + "foldhash 0.2.0", "font-types 0.12.7", "once_cell", "yoke", @@ -8203,6 +8269,16 @@ dependencies = [ "float-cmp", ] +[[package]] +name = "string-interner" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23de088478b31c349c9ba67816fa55d9355232d63c3afea8bf513e31f0f1d2c0" +dependencies = [ + "hashbrown 0.15.5", + "serde", +] + [[package]] name = "stringprep" version = "0.1.5" @@ -8646,6 +8722,15 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + [[package]] name = "tiff" version = "0.11.3" @@ -9171,7 +9256,7 @@ dependencies = [ "base64 0.23.1", "data-url", "fontdb", - "harfrust", + "harfrust 0.12.0", "imagesize 0.15.0", "kurbo 0.13.1", "log", @@ -9218,6 +9303,221 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "vectorcraft-brush" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo 0.13.1", + "serde", + "serde_json", + "vectorcraft-color", + "vectorcraft-doc", + "vectorcraft-geom", +] + +[[package]] +name = "vectorcraft-color" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo 0.13.1", + "moxcms", + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "vectorcraft-doc" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "chrono", + "hayro-ccitt", + "image", + "kurbo 0.13.1", + "serde", + "serde_json", + "thiserror 2.0.21", + "tiff", + "vectorcraft-color", + "vectorcraft-geom", + "zune-jpeg", +] + +[[package]] +name = "vectorcraft-effects" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "image", + "kurbo 0.13.1", + "serde_json", + "vectorcraft-doc", + "vectorcraft-geom", + "vectorcraft-pathops", + "vectorcraft-plugins", + "vectorcraft-text", + "vectorcraft-three-d", +] + +[[package]] +name = "vectorcraft-eps" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "flate2", + "hayro-ccitt", + "image", + "kurbo 0.13.1", + "memchr", + "ruzstd", + "serde_json", + "vectorcraft-brush", + "vectorcraft-color", + "vectorcraft-doc", + "vectorcraft-effects", + "vectorcraft-geom", + "vectorcraft-metafile", + "vectorcraft-pdf", + "vectorcraft-render", + "vectorcraft-text", +] + +[[package]] +name = "vectorcraft-geom" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo 0.13.1", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "vectorcraft-metafile" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "image", + "kurbo 0.13.1", + "vectorcraft-brush", + "vectorcraft-color", + "vectorcraft-doc", + "vectorcraft-effects", + "vectorcraft-geom", + "vectorcraft-text", +] + +[[package]] +name = "vectorcraft-pathops" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo 0.13.1", + "linesweeper", + "thiserror 2.0.21", + "vectorcraft-geom", +] + +[[package]] +name = "vectorcraft-pdf" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "aes 0.8.4", + "chrono", + "craft-pdf 0.1.0 (git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0)", + "flate2", + "getrandom 0.4.3", + "image", + "jpeg-encoder 0.6.1", + "kurbo 0.13.1", + "log", + "md-5 0.10.6", + "roxmltree 0.21.2", + "serde", + "sha2 0.10.9", + "skrifa 0.47.0", + "smallvec", + "thiserror 2.0.21", + "vectorcraft-brush", + "vectorcraft-color", + "vectorcraft-doc", + "vectorcraft-effects", + "vectorcraft-geom", + "vectorcraft-text", + "weezl 0.2.1", +] + +[[package]] +name = "vectorcraft-plugins" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", + "vectorcraft-color", + "vectorcraft-doc", + "vectorcraft-geom", + "wasmi", +] + +[[package]] +name = "vectorcraft-render" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "flate2", + "gif", + "image", + "jpeg-encoder 0.6.1", + "log", + "png 0.18.1", + "vectorcraft-brush", + "vectorcraft-color", + "vectorcraft-doc", + "vectorcraft-effects", + "vectorcraft-geom", + "vectorcraft-text", + "vello_common 0.2.0", + "vello_cpu 0.2.0", + "weezl 0.2.1", + "winsafe", +] + +[[package]] +name = "vectorcraft-text" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "harfrust 0.13.3", + "icu_segmenter", + "kurbo 0.13.1", + "log", + "skrifa 0.47.0", + "subsetter", + "unicode-bidi", + "unicode-general-category", + "unicode-script", + "unicode-segmentation", + "vectorcraft-doc", + "vectorcraft-geom", + "vello_cpu 0.2.0", + "windows-registry", +] + +[[package]] +name = "vectorcraft-three-d" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "vectorcraft-geom", + "vectorcraft-pathops", +] + [[package]] name = "vello_common" version = "0.0.8" @@ -9288,9 +9588,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace506ca414548966fcf3c283be94eb1cad8254de488791ee698bf438b0890a9" dependencies = [ "bytemuck", + "crossbeam-channel", "glifo 0.3.0", "hashbrown 0.17.1", + "ordered-channel", "png 0.18.1", + "rayon", + "thread_local", "vello_common 0.2.0", ] @@ -9448,6 +9752,56 @@ version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f692aa943ccd88363733b77063f32cfed5bc6cbea8e6e8b251b302f881606fe" +[[package]] +name = "wasmi" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78693fcdd618e0fc34af59c6b8efa9ac5d58c68df940beff4bedddb6acfe7c27" +dependencies = [ + "spin", + "wasmi_collections", + "wasmi_core", + "wasmi_ir", + "wasmparser", +] + +[[package]] +name = "wasmi_collections" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a8be2aa467cf2d29e96ff759472c36eeb44a3c81c67fc9cb76c9a24c519c557" +dependencies = [ + "string-interner", +] + +[[package]] +name = "wasmi_core" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69372d5fda3ea3d1e0aa6603c7888110e0187e88ea17cd8fc2e2df0a0e1f37fa" +dependencies = [ + "libm", +] + +[[package]] +name = "wasmi_ir" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f17b774caa13c618c7244f1ee51fe23c5e7b8538a471fa46d9949779758aed6" +dependencies = [ + "wasmi_core", +] + +[[package]] +name = "wasmparser" +version = "0.228.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4abf1132c1fdf747d56bbc1bb52152400c70f336870f968b85e89ea422198ae3" +dependencies = [ + "bitflags 2.13.2", + "indexmap", +] + [[package]] name = "wayland-backend" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index 46f2824e5..6bc7b282f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,8 @@ craft-dxf = { path = "crates/dxf" } craft-drawingml = { path = "crates/drawingml" } craft-eps = { path = "crates/eps" } craft-file-formats = { path = "crates/file-formats" } +vectorcraft-doc = { git = "https://github.com/storytold/vectorcraft.git", rev = "6b3a6b8788ebcb420d69449199cbdf323888d27e" } +vectorcraft-eps = { git = "https://github.com/storytold/vectorcraft.git", rev = "6b3a6b8788ebcb420d69449199cbdf323888d27e" } craft-doc = { path = "crates/doc" } craft-docpath = { path = "crates/docpath" } craft-dpx = { path = "crates/dpx" } diff --git a/crates/dxf/Cargo.toml b/crates/dxf/Cargo.toml index 70c4f1234..11de1c377 100644 --- a/crates/dxf/Cargo.toml +++ b/crates/dxf/Cargo.toml @@ -15,3 +15,4 @@ craft-codepage = { workspace = true } thiserror = { workspace = true } log = "0.4" encoding_rs = "0.8.35" +kurbo = { workspace = true } diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index 0109e9040..4cf3e46e9 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -11,6 +11,7 @@ use std::io::Write as _; pub mod model; +pub mod paths; mod version; pub use version::Version; diff --git a/crates/dxf/src/paths.rs b/crates/dxf/src/paths.rs new file mode 100644 index 000000000..045986b99 --- /dev/null +++ b/crates/dxf/src/paths.rs @@ -0,0 +1,218 @@ +//! Bounded conversion of common planar DXF entities into neutral Bézier paths. +//! +//! This is geometry extraction, not a full CAD scene renderer: unsupported entities such as +//! text, dimensions, meshes, and block inserts are reported as warnings and omitted. + +use kurbo::{BezPath, Point, Rect, Shape as _}; + +/// Limits on the number of paths returned by [`import`]. Parsing also uses the crate's tag cap. +pub const MAX_PATHS: usize = 200_000; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("DXF parse failed: {0}")] + Parse(#[from] crate::DxfError), + #[error("DXF contains more than {MAX_PATHS} paths")] + TooManyPaths, + #[error("DXF contains a non-finite path coordinate")] + NonFinite, +} + +/// Geometry in source DXF coordinates. `unit_to_mm` maps a source coordinate to millimetres; +/// unitless drawings use `1.0`, as the coordinates are then interpreted as millimetres. +#[derive(Debug, Clone)] +pub struct Imported { + pub paths: Vec, + pub bounds: Option, + pub unit_to_mm: f64, + pub warnings: Vec, +} + +/// Parse bounded DXF input and extract visible planar linework. +pub fn import(bytes: &[u8]) -> Result { + let tags = crate::parse_with_limit(bytes, crate::max_tags())?; + let drawing = crate::model::Document::read(&tags); + let unit_to_mm = units_to_mm(drawing.header.insunits(), drawing.header.measurement()); + let mut paths = Vec::new(); + let mut warnings = Vec::new(); + for record in &drawing.entities { + let entity = crate::model::Entity::read(record); + if !entity.common.visible || record.paper_space() { + continue; + } + let converted = entity_paths(&entity.kind, entity.common.extrusion); + if converted.iter().flat_map(|path| path.elements()).any(|element| { + let finite = |p: Point| p.x.is_finite() && p.y.is_finite(); + match *element { + kurbo::PathEl::MoveTo(p) | kurbo::PathEl::LineTo(p) => !finite(p), + kurbo::PathEl::QuadTo(a, b) => !finite(a) || !finite(b), + kurbo::PathEl::CurveTo(a, b, c) => !finite(a) || !finite(b) || !finite(c), + kurbo::PathEl::ClosePath => false, + } + }) { + return Err(Error::NonFinite); + } + if converted.is_empty() { + continue; + } + if paths.len().saturating_add(converted.len()) > MAX_PATHS { + return Err(Error::TooManyPaths); + } + paths.extend(converted); + } + if drawing.skipped.iter().any(|(_, n)| *n > 0) { + warnings.push("some unsupported or stray DXF records were skipped".into()); + } + let bounds = paths.iter().map(|path| path.bounding_box()).reduce(|a, b| a.union(b)); + Ok(Imported { paths, bounds, unit_to_mm, warnings }) +} + +fn units_to_mm(code: i64, measurement: Option) -> f64 { + match code { + 0 if measurement == Some(0) => 25.4, + 1 => 25.4, + 2 => 304.8, + 3 => 1_609_344.0, + 5 => 10.0, + 6 => 1_000.0, + 7 => 1_000_000.0, + 8 => 0.000_025_4, + 9 => 0.0254, + 10 => 914.4, + 11 => 1e-7, + 12 => 1e-6, + 13 => 0.001, + 14 => 100.0, + 15 => 10_000.0, + 16 => 100_000.0, + 17 => 1e9, + 4 | _ => 1.0, + } +} + +fn entity_paths(kind: &crate::model::EntityKind, normal: crate::model::Point) -> Vec { + use crate::model::EntityKind as E; + match kind { + E::Line { start, end } => vec![line(*start, *end)], + E::Circle { center, radius } if radius.is_finite() && *radius > 0.0 => { + vec![circle(ocs(normal, *center), *radius)] + } + E::Arc { center, radius, start_angle, end_angle } + if radius.is_finite() && *radius > 0.0 && start_angle.is_finite() && end_angle.is_finite() => + { + vec![arc(ocs(normal, *center), *radius, start_angle.to_radians(), end_angle.to_radians())] + } + E::LwPolyline(poly) => vec![polyline(poly.vertices.iter().map(|v| (ocs(normal, [v.x, v.y, poly.elevation]), v.bulge)), poly.closed)], + E::Polyline(poly) if !poly.is_mesh() && !poly.is_polyface() => { + vec![polyline(poly.vertices.iter().filter(|v| !v.is_face()).map(|v| (ocs(normal, v.location), v.bulge)), poly.closed())] + } + E::Ellipse { center, major, ratio, start, end } if ratio.is_finite() && *ratio > 0.0 => { + vec![ellipse(*center, *major, *ratio, *start, *end)] + } + _ => Vec::new(), + } +} + +fn ocs(normal: crate::model::Point, point: crate::model::Point) -> Point { + let p = crate::model::ocs_to_wcs(normal, point); + Point::new(p[0], p[1]) +} + +fn line(start: crate::model::Point, end: crate::model::Point) -> BezPath { + let mut path = BezPath::new(); + path.move_to((start[0], start[1])); + path.line_to((end[0], end[1])); + path +} + +fn polyline(vertices: impl IntoIterator, closed: bool) -> BezPath { + let vertices = vertices.into_iter().collect::>(); + let mut path = BezPath::new(); + let Some((first, _)) = vertices.first().copied() else { return path }; + path.move_to(first); + for i in 1..vertices.len() { + let (a, bulge) = vertices[i - 1]; + let (b, _) = vertices[i]; + if bulge.is_finite() && bulge.abs() > 1e-12 { + append_bulge(&mut path, a, b, bulge); + } else { + path.line_to(b); + } + } + if closed && vertices.len() > 1 { + let (a, bulge) = *vertices.last().unwrap_or(&(first, 0.0)); + if bulge.is_finite() && bulge.abs() > 1e-12 { + append_bulge(&mut path, a, first, bulge); + } else { + path.line_to(first); + } + path.close_path(); + } + path +} + +fn append_bulge(path: &mut BezPath, a: Point, b: Point, bulge: f64) { + let chord = b - a; + let length = chord.hypot(); + if !(length.is_finite() && length > 0.0) { + path.line_to(b); + return; + } + let center = a + chord * 0.5 + kurbo::Vec2::new(-chord.y, chord.x) * ((1.0 - bulge * bulge) / (4.0 * bulge)); + let start = (a.y - center.y).atan2(a.x - center.x); + let sweep = 4.0 * bulge.atan(); + let radius = length * (1.0 + bulge * bulge) / (4.0 * bulge.abs()); + arc_segments(path, center, start, sweep, radius); +} + +fn circle(center: Point, radius: f64) -> BezPath { + let mut path = BezPath::new(); + path.move_to((center.x + radius, center.y)); + arc_segments(&mut path, center, 0.0, std::f64::consts::TAU, radius); + path.close_path(); + path +} + +fn arc(center: Point, radius: f64, start: f64, end: f64) -> BezPath { + let mut path = BezPath::new(); + path.move_to((center.x + radius * start.cos(), center.y + radius * start.sin())); + let sweep = if end >= start { end - start } else { end - start + std::f64::consts::TAU }; + arc_segments(&mut path, center, start, sweep, radius); + path +} + +fn arc_segments(path: &mut BezPath, center: Point, start: f64, sweep: f64, radius: f64) { + let count = (sweep.abs() / (std::f64::consts::FRAC_PI_2)).ceil().max(1.0) as usize; + let step = sweep / count as f64; + for i in 0..count { + let a = start + step * i as f64; + let b = a + step; + let k = 4.0 / 3.0 * (step / 4.0).tan(); + let p1 = Point::new(center.x + radius * (a.cos() - k * a.sin()), center.y + radius * (a.sin() + k * a.cos())); + let p2 = Point::new(center.x + radius * (b.cos() + k * b.sin()), center.y + radius * (b.sin() - k * b.cos())); + let p3 = Point::new(center.x + radius * b.cos(), center.y + radius * b.sin()); + path.curve_to(p1, p2, p3); + } +} + +fn ellipse(center: crate::model::Point, major: crate::model::Point, ratio: f64, start: f64, end: f64) -> BezPath { + let (cx, cy) = (center[0], center[1]); + let (ax, ay) = (major[0], major[1]); + let (bx, by) = (-ay * ratio, ax * ratio); + let point = |t: f64| Point::new(cx + ax * t.cos() + bx * t.sin(), cy + ay * t.cos() + by * t.sin()); + let mut path = BezPath::new(); + path.move_to(point(start)); + let sweep = if end >= start { end - start } else { end - start + std::f64::consts::TAU }; + let count = (sweep.abs() / std::f64::consts::FRAC_PI_2).ceil().max(1.0) as usize; + let step = sweep / count as f64; + for i in 0..count { + let a = start + step * i as f64; + let b = a + step; + let k = 4.0 / 3.0 * (step / 4.0).tan(); + let derivative = |t: f64| Point::new(-ax * t.sin() + bx * t.cos(), -ay * t.sin() + by * t.cos()); + let p1 = point(a) + derivative(a).to_vec2() * k; + let p2 = point(b) - derivative(b).to_vec2() * k; + path.curve_to(p1, p2, point(b)); + } + path +} diff --git a/crates/eps/Cargo.toml b/crates/eps/Cargo.toml index fbe69c26f..6984fb576 100644 --- a/crates/eps/Cargo.toml +++ b/crates/eps/Cargo.toml @@ -11,3 +11,11 @@ repository.workspace = true workspace = true [dependencies] +kurbo = { workspace = true, optional = true } +vectorcraft-doc = { workspace = true, optional = true } +vectorcraft-eps = { workspace = true, optional = true } +thiserror = { workspace = true, optional = true } + +[features] +default = [] +paths = ["dep:kurbo", "dep:thiserror", "dep:vectorcraft-doc", "dep:vectorcraft-eps"] diff --git a/crates/eps/src/lib.rs b/crates/eps/src/lib.rs index acffdfcf2..6e5e91d93 100644 --- a/crates/eps/src/lib.rs +++ b/crates/eps/src/lib.rs @@ -18,6 +18,8 @@ pub mod ascii85; mod container; mod dsc; +#[cfg(feature = "paths")] +pub mod paths; mod preview; pub use container::{DOS_HEADER, DOS_MAGIC, Sections, sections, wrap}; diff --git a/crates/eps/src/paths.rs b/crates/eps/src/paths.rs new file mode 100644 index 000000000..241c812f7 --- /dev/null +++ b/crates/eps/src/paths.rs @@ -0,0 +1,75 @@ +//! Neutral path extraction for EPS and PostScript files. +//! +//! This facade keeps the PostScript interpreter and its format decisions in shared dependencies; +//! applications receive Bézier paths, page bounds and warnings for their own document adapters. + +use kurbo::BezPath; + +/// Limits on the extracted geometry. The interpreter has its own execution and allocation caps. +#[derive(Debug, Clone, Copy)] +pub struct Limits { + pub max_input_bytes: usize, + pub max_paths: usize, + pub max_path_elements: usize, +} + +impl Default for Limits { + fn default() -> Self { + Self { max_input_bytes: 64 * 1024 * 1024, max_paths: 200_000, max_path_elements: 2_000_000 } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("PostScript input exceeds {limit} bytes")] + InputLimit { limit: usize }, + #[error("PostScript import failed: {0}")] + Parse(String), + #[error("PostScript path output exceeds configured limit")] + OutputLimit, +} + +/// First-page path geometry in PostScript points, plus the page size and import notes. +#[derive(Debug, Clone)] +pub struct Imported { + pub paths: Vec, + pub page_points: (f64, f64), + pub warnings: Vec, + pub preview_fallback: bool, +} + +/// Interpret EPS/PostScript bytes and return their editable vector path geometry. +pub fn import(bytes: &[u8], limits: Limits) -> Result { + if bytes.len() > limits.max_input_bytes { + return Err(Error::InputLimit { limit: limits.max_input_bytes }); + } + let source = vectorcraft_eps::import(bytes).map_err(Error::Parse)?; + let page_points = source.document.artboards.first().map_or((612.0, 792.0), |board| (board.rect.width(), board.rect.height())); + let mut paths = Vec::new(); + let mut total_elements = 0usize; + for layer in &source.document.layers { + collect(layer, &mut paths, &mut total_elements, limits)?; + } + Ok(Imported { paths, page_points, warnings: source.warnings, preview_fallback: source.preview }) +} + +fn collect(node: &vectorcraft_doc::Node, paths: &mut Vec, elements: &mut usize, limits: Limits) -> Result<(), Error> { + if !node.visible { + return Ok(()); + } + if let Some(data) = node.path_data() { + let path = data.to_bezpath(); + if !path.elements().is_empty() { + *elements = elements.saturating_add(path.elements().len()); + if paths.len() >= limits.max_paths || *elements > limits.max_path_elements { + return Err(Error::OutputLimit); + } + paths.push(path); + } + } else if let Some(children) = node.children() { + for child in children { + collect(child, paths, elements, limits)?; + } + } + Ok(()) +} diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 193aeb9df..b370416ab 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -14,6 +14,7 @@ workspace = true default = [] read = ["dep:hayro-syntax", "dep:thiserror"] interpret = ["read", "dep:hayro-interpret"] +paths = ["interpret", "dep:kurbo"] raster = ["interpret", "dep:hayro", "dep:bytemuck"] write = ["dep:krilla", "dep:thiserror"] write-pdf = ["write", "krilla/pdf"] @@ -40,6 +41,7 @@ stringprep = { version = "0.1", optional = true } zeroize = { version = "1", optional = true } # PDF date strings in the object layer (`cos`). chrono = { workspace = true, optional = true } +kurbo = { workspace = true, optional = true } # On the web, a rewrite's fresh file identifier (`cos`) takes its entropy from the browser # (`crypto.getRandomValues`); native builds compile neither. diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index e052c7304..ed9f21a79 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -21,6 +21,8 @@ pub use link::safe_url; mod read; #[cfg(feature = "read")] pub use read::*; +#[cfg(feature = "paths")] +pub mod paths; #[cfg(feature = "raster")] pub mod raster; #[cfg(feature = "write")] diff --git a/crates/pdf/src/paths.rs b/crates/pdf/src/paths.rs new file mode 100644 index 000000000..bb51e2a0f --- /dev/null +++ b/crates/pdf/src/paths.rs @@ -0,0 +1,127 @@ +//! Bounded extraction of page vector paths from PDF content streams. +//! +//! Paths remain in PDF user-space points. Text glyphs and raster images are counted as omitted +//! content; clipping and paint are not applied. Applications own page selection and model mapping. + +use hayro_interpret::font::Glyph; +use hayro_interpret::{ + BlendMode, ClipPath, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, Paint, PathDrawMode, RectExt, SoftMask, +}; +use kurbo::{Affine, BezPath, Rect}; + +/// Limits for vector path extraction. They bound the output model, not interpreter peak memory. +#[derive(Debug, Clone, Copy)] +pub struct Limits { + pub max_input_bytes: usize, + pub max_pages: usize, + pub max_paths: usize, + pub max_path_elements: usize, +} + +impl Default for Limits { + fn default() -> Self { + Self { max_input_bytes: 64 * 1024 * 1024, max_pages: 1000, max_paths: 200_000, max_path_elements: 2_000_000 } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error(transparent)] + Open(#[from] crate::read::OpenError), + #[error(transparent)] + Interpret(#[from] crate::read::ExecutionError), + #[error("PDF contains more than {limit} pages")] + Pages { limit: usize }, + #[error("PDF path output exceeds configured limit")] + OutputLimit, +} + +/// Extracted paths for one PDF page, with its intersected crop box in user-space points. +#[derive(Debug, Clone)] +pub struct Page { + pub crop_box: Rect, + pub paths: Vec, + pub omitted_text: usize, + pub omitted_images: usize, + pub clips_seen: usize, +} + +/// Pages from one PDF. Paths from separate pages are never combined. +#[derive(Debug, Clone)] +pub struct Imported { + pub pages: Vec, + pub warnings: Vec, +} + +/// Open and interpret vector content using shared PDF parsing and interpreter APIs. +pub fn import(bytes: &[u8], limits: Limits) -> Result { + let options = crate::read::OpenOptions { max_input_bytes: limits.max_input_bytes, ..Default::default() }; + let pdf = crate::read::open_slice_with_options(bytes, None, &options)?; + if pdf.pages().len() > limits.max_pages { + return Err(Error::Pages { limit: limits.max_pages }); + } + let cache = InterpreterCache::new(); + let mut pages = Vec::with_capacity(pdf.pages().len()); + let mut total_paths = 0usize; + let mut total_elements = 0usize; + for page in pdf.pages().iter() { + let crop_box = page.intersected_crop_box().to_kurbo(); + let mut context = hayro_interpret::Context::new(Affine::IDENTITY, crop_box, &cache, pdf.xref(), InterpreterSettings::default()); + let mut device = Collector::default(); + crate::read::interpret_page(page, &mut context, &mut device)?; + total_paths = total_paths.saturating_add(device.paths.len()); + total_elements = device.paths.iter().fold(total_elements, |n, path| n.saturating_add(path.elements().len())); + if total_paths > limits.max_paths || total_elements > limits.max_path_elements { + return Err(Error::OutputLimit); + } + pages.push(Page { + crop_box, + paths: device.paths, + omitted_text: device.omitted_text, + omitted_images: device.omitted_images, + clips_seen: device.clips_seen, + }); + } + let mut warnings = Vec::new(); + if pages.iter().any(|page| page.omitted_text > 0) { + warnings.push("text glyphs are not returned as paths".into()); + } + if pages.iter().any(|page| page.omitted_images > 0) { + warnings.push("raster images are not returned as paths".into()); + } + if pages.iter().any(|page| page.clips_seen > 0) { + warnings.push("clip paths are not applied to extracted geometry".into()); + } + Ok(Imported { pages, warnings }) +} + +#[derive(Default)] +struct Collector { + paths: Vec, + omitted_text: usize, + omitted_images: usize, + clips_seen: usize, +} + +impl Device<'_> for Collector { + fn set_soft_mask(&mut self, _: Option>) {} + fn set_blend_mode(&mut self, _: BlendMode) {} + fn draw_path(&mut self, path: &BezPath, transform: Affine, _: &Paint<'_>, _: &PathDrawMode) { + let path = transform * path.clone(); + if !path.is_empty() { + self.paths.push(path); + } + } + fn push_clip_path(&mut self, _: &ClipPath) { + self.clips_seen = self.clips_seen.saturating_add(1); + } + fn push_transparency_group(&mut self, _: f32, _: Option>, _: BlendMode) {} + fn draw_glyph(&mut self, _: &Glyph<'_>, _: Affine, _: Affine, _: &Paint<'_>, _: &GlyphDrawMode) { + self.omitted_text = self.omitted_text.saturating_add(1); + } + fn draw_image(&mut self, _: Image<'_, '_>, _: Affine) { + self.omitted_images = self.omitted_images.saturating_add(1); + } + fn pop_clip_path(&mut self) {} + fn pop_transparency_group(&mut self) {} +} From f80d86172b89a9bd9cfcb68ab272f55d0c1c512e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:01:33 -0500 Subject: [PATCH 114/520] Expose PostScript page bounds in path imports --- crates/eps/src/paths.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/eps/src/paths.rs b/crates/eps/src/paths.rs index 241c812f7..adbe294f3 100644 --- a/crates/eps/src/paths.rs +++ b/crates/eps/src/paths.rs @@ -3,7 +3,7 @@ //! This facade keeps the PostScript interpreter and its format decisions in shared dependencies; //! applications receive Bézier paths, page bounds and warnings for their own document adapters. -use kurbo::BezPath; +use kurbo::{BezPath, Rect}; /// Limits on the extracted geometry. The interpreter has its own execution and allocation caps. #[derive(Debug, Clone, Copy)] @@ -33,7 +33,8 @@ pub enum Error { #[derive(Debug, Clone)] pub struct Imported { pub paths: Vec, - pub page_points: (f64, f64), + /// Page bounds in source PostScript points, including a nonzero or negative origin. + pub page_bounds: Rect, pub warnings: Vec, pub preview_fallback: bool, } @@ -44,13 +45,17 @@ pub fn import(bytes: &[u8], limits: Limits) -> Result { return Err(Error::InputLimit { limit: limits.max_input_bytes }); } let source = vectorcraft_eps::import(bytes).map_err(Error::Parse)?; - let page_points = source.document.artboards.first().map_or((612.0, 792.0), |board| (board.rect.width(), board.rect.height())); + let page_bounds = source + .document + .artboards + .first() + .map_or(Rect::new(0.0, 0.0, 612.0, 792.0), |board| Rect::new(board.rect.x0, board.rect.y0, board.rect.x1, board.rect.y1)); let mut paths = Vec::new(); let mut total_elements = 0usize; for layer in &source.document.layers { collect(layer, &mut paths, &mut total_elements, limits)?; } - Ok(Imported { paths, page_points, warnings: source.warnings, preview_fallback: source.preview }) + Ok(Imported { paths, page_bounds, warnings: source.warnings, preview_fallback: source.preview }) } fn collect(node: &vectorcraft_doc::Node, paths: &mut Vec, elements: &mut usize, limits: Limits) -> Result<(), Error> { From 5082d8bd95807de5a851b6ab6ec3546fdf0ec5c4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:08:44 -0500 Subject: [PATCH 115/520] Add structured SVG scene writer --- crates/svg/src/write.rs | 146 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/crates/svg/src/write.rs b/crates/svg/src/write.rs index 614642de5..ac884d8f2 100644 --- a/crates/svg/src/write.rs +++ b/crates/svg/src/write.rs @@ -30,6 +30,152 @@ use kurbo::{BezPath, PathEl, Point}; /// The SVG namespace. pub const SVG_NS: &str = "http://www.w3.org/2000/svg"; +/// A structured SVG scene. Applications provide geometry, styles and attributes; this module +/// owns the SVG element tree and serializes it with XML escaping. +#[derive(Clone, Debug, PartialEq)] +pub struct Scene { + pub width_mm: f64, + pub height_mm: f64, + pub view_box: [f64; 4], + pub elements: Vec, +} + +/// Structured SVG elements supported by the path-document writer. +#[derive(Clone, Debug, PartialEq)] +pub enum Element { + Group { attributes: Vec, children: Vec }, + Path { attributes: Vec, path: BezPath }, + Rect { attributes: Vec, x: f64, y: f64, width: f64, height: f64 }, + Text { attributes: Vec, x: f64, y: f64, text: String }, + Title(String), + Style(String), + NestedSvg { attributes: Vec, children: Vec }, +} + +/// An escaped SVG attribute. Element names and attribute names are API constants and must be +/// valid XML names; values are always escaped by the shared writer. +#[derive(Clone, Debug, PartialEq)] +pub struct Attribute { + pub name: String, + pub value: Value, +} + +/// Attribute values written by the shared serializer. +#[derive(Clone, Debug, PartialEq)] +pub enum Value { + Text(String), + Number(f64), + Millimetres(f64), + ViewBox([f64; 4]), + TranslateScale { x: f64, y: f64, scale_x: f64, scale_y: f64 }, +} + +impl Attribute { + pub fn text(name: impl Into, value: impl Into) -> Self { + Self { name: name.into(), value: Value::Text(value.into()) } + } + pub fn number(name: impl Into, value: f64) -> Self { + Self { name: name.into(), value: Value::Number(value) } + } + pub fn millimetres(name: impl Into, value: f64) -> Self { + Self { name: name.into(), value: Value::Millimetres(value) } + } + pub fn view_box(bounds: [f64; 4]) -> Self { + Self { name: "viewBox".into(), value: Value::ViewBox(bounds) } + } + pub fn translate_scale(x: f64, y: f64, scale_x: f64, scale_y: f64) -> Self { + Self { name: "transform".into(), value: Value::TranslateScale { x, y, scale_x, scale_y } } + } +} + +impl Scene { + /// Serialize the scene as an SVG 1.1 document. + pub fn write(&self) -> String { + let mut writer = SvgWriter::new(); + writer + .root() + .attr("version", "1.1") + .attr("width", format!("{}mm", num(self.width_mm, 3))) + .attr("height", format!("{}mm", num(self.height_mm, 3))) + .attr("viewBox", view_box_data(self.view_box)) + .open(); + write_elements(&mut writer, &self.elements, 1); + writer.line(0); + writer.close(); + writer.finish() + } +} + +fn write_attributes<'a>(mut tag: craft_xml::Tag<'a>, attributes: &'a [Attribute]) -> craft_xml::Tag<'a> { + for attribute in attributes { + tag = match &attribute.value { + Value::Text(value) => tag.attr(&attribute.name, value), + Value::Number(value) => tag.attr(&attribute.name, num(*value, 3)), + Value::Millimetres(value) => tag.attr(&attribute.name, format!("{}mm", num(*value, 3))), + Value::ViewBox(bounds) => tag.attr(&attribute.name, view_box_data(*bounds)), + Value::TranslateScale { x, y, scale_x, scale_y } => { + tag.attr(&attribute.name, format!("translate({} {}) scale({} {})", num(*x, 3), num(*y, 3), num(*scale_x, 4), num(*scale_y, 4))) + } + }; + } + tag +} + +fn write_elements(writer: &mut SvgWriter, elements: &[Element], depth: usize) { + for element in elements { + writer.line(depth); + match element { + Element::Group { attributes, children } => { + write_attributes(writer.tag("g"), attributes).open(); + write_elements(writer, children, depth + 1); + writer.line(depth); + writer.close(); + } + Element::Path { attributes, path } => { + let attributes = attributes.clone(); + write_attributes(writer.tag("path").attr("d", path_data(path)), &attributes).empty(); + } + Element::Rect { attributes, x, y, width, height } => { + let mut attrs = vec![ + Attribute::number("x", *x), + Attribute::number("y", *y), + Attribute::number("width", *width), + Attribute::number("height", *height), + ]; + attrs.extend(attributes.iter().cloned()); + write_attributes(writer.tag("rect"), &attrs).empty(); + } + Element::Text { attributes, x, y, text } => { + let mut attrs = vec![Attribute::number("x", *x), Attribute::number("y", *y)]; + attrs.extend(attributes.iter().cloned()); + write_attributes(writer.tag("text"), &attrs).open(); + writer.text(text); + writer.close(); + } + Element::Title(text) => { + writer.tag("title").open(); + writer.text(text); + writer.close(); + } + Element::Style(css) => { + writer.tag("style").open(); + writer.text(css); + writer.close(); + } + Element::NestedSvg { attributes, children } => { + write_attributes(writer.tag("svg"), attributes).open(); + write_elements(writer, children, depth + 1); + writer.line(depth); + writer.close(); + } + } + } +} + +fn view_box_data(bounds: [f64; 4]) -> String { + format!("{} {} {} {}", num(bounds[0], 3), num(bounds[1], 3), num(bounds[2], 3), num(bounds[3], 3)) +} + /// A number written with a fixed count of decimals. A value that is not finite is written as /// zero, because SVG has no spelling for it. #[derive(Debug, Clone, Copy, PartialEq)] From 54f3a37edb4472349418cc4a503890ac49d9bf84 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:11:26 -0500 Subject: [PATCH 116/520] Extend shared vector format boundaries --- crates/dxf/src/paths.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/dxf/src/paths.rs b/crates/dxf/src/paths.rs index 045986b99..f34f0e62f 100644 --- a/crates/dxf/src/paths.rs +++ b/crates/dxf/src/paths.rs @@ -86,7 +86,7 @@ fn units_to_mm(code: i64, measurement: Option) -> f64 { 15 => 10_000.0, 16 => 100_000.0, 17 => 1e9, - 4 | _ => 1.0, + _ => 1.0, } } From 85ed941ac5a5c963949694a6a422390607ee5c1f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:30:17 -0500 Subject: [PATCH 117/520] Address vector import review findings --- crates/dxf/src/paths.rs | 32 ++++++++++++++++++++++++++++++-- crates/svg/src/write.rs | 6 +++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/dxf/src/paths.rs b/crates/dxf/src/paths.rs index f34f0e62f..1b57cec40 100644 --- a/crates/dxf/src/paths.rs +++ b/crates/dxf/src/paths.rs @@ -35,6 +35,7 @@ pub fn import(bytes: &[u8]) -> Result { let unit_to_mm = units_to_mm(drawing.header.insunits(), drawing.header.measurement()); let mut paths = Vec::new(); let mut warnings = Vec::new(); + let mut unsupported = 0usize; for record in &drawing.entities { let entity = crate::model::Entity::read(record); if !entity.common.visible || record.paper_space() { @@ -53,6 +54,17 @@ pub fn import(bytes: &[u8]) -> Result { return Err(Error::NonFinite); } if converted.is_empty() { + if matches!( + entity.kind, + crate::model::EntityKind::Other + | crate::model::EntityKind::Text(_) + | crate::model::EntityKind::MText(_) + | crate::model::EntityKind::Insert(_) + | crate::model::EntityKind::Dimension(_) + | crate::model::EntityKind::Hatch(_) + ) { + unsupported = unsupported.saturating_add(1); + } continue; } if paths.len().saturating_add(converted.len()) > MAX_PATHS { @@ -60,6 +72,9 @@ pub fn import(bytes: &[u8]) -> Result { } paths.extend(converted); } + if unsupported > 0 { + warnings.push(format!("{unsupported} visible DXF entities are unsupported and were omitted")); + } if drawing.skipped.iter().any(|(_, n)| *n > 0) { warnings.push("some unsupported or stray DXF records were skipped".into()); } @@ -67,9 +82,9 @@ pub fn import(bytes: &[u8]) -> Result { Ok(Imported { paths, bounds, unit_to_mm, warnings }) } -fn units_to_mm(code: i64, measurement: Option) -> f64 { +fn units_to_mm(code: i64, _measurement: Option) -> f64 { match code { - 0 if measurement == Some(0) => 25.4, + 0 => 1.0, 1 => 25.4, 2 => 304.8, 3 => 1_609_344.0, @@ -86,6 +101,13 @@ fn units_to_mm(code: i64, measurement: Option) -> f64 { 15 => 10_000.0, 16 => 100_000.0, 17 => 1e9, + 18 => 149_597_870_700.0, + 19 => 9.460_730_472_580_8e18, + 20 => 3.085_677_581_491_367e19, + 21 => 1200.0 / 3937.0 * 1000.0, + 22 => 1200.0 / 3937.0 * 1000.0 / 12.0, + 23 => 1200.0 / 3937.0 * 1000.0 * 3.0, + 24 => 1200.0 / 3937.0 * 1000.0 * 5280.0, _ => 1.0, } } @@ -183,6 +205,9 @@ fn arc(center: Point, radius: f64, start: f64, end: f64) -> BezPath { fn arc_segments(path: &mut BezPath, center: Point, start: f64, sweep: f64, radius: f64) { let count = (sweep.abs() / (std::f64::consts::FRAC_PI_2)).ceil().max(1.0) as usize; + if count > 4096 { + return; + } let step = sweep / count as f64; for i in 0..count { let a = start + step * i as f64; @@ -204,6 +229,9 @@ fn ellipse(center: crate::model::Point, major: crate::model::Point, ratio: f64, path.move_to(point(start)); let sweep = if end >= start { end - start } else { end - start + std::f64::consts::TAU }; let count = (sweep.abs() / std::f64::consts::FRAC_PI_2).ceil().max(1.0) as usize; + if count > 4096 { + return path; + } let step = sweep / count as f64; for i in 0..count { let a = start + step * i as f64; diff --git a/crates/svg/src/write.rs b/crates/svg/src/write.rs index ac884d8f2..664ce8f01 100644 --- a/crates/svg/src/write.rs +++ b/crates/svg/src/write.rs @@ -114,13 +114,17 @@ fn write_attributes<'a>(mut tag: craft_xml::Tag<'a>, attributes: &'a [Attribute] Value::Millimetres(value) => tag.attr(&attribute.name, format!("{}mm", num(*value, 3))), Value::ViewBox(bounds) => tag.attr(&attribute.name, view_box_data(*bounds)), Value::TranslateScale { x, y, scale_x, scale_y } => { - tag.attr(&attribute.name, format!("translate({} {}) scale({} {})", num(*x, 3), num(*y, 3), num(*scale_x, 4), num(*scale_y, 4))) + tag.attr(&attribute.name, format!("translate({} {}) scale({} {})", num(*x, 3), num(*y, 3), shortest(*scale_x), shortest(*scale_y))) } }; } tag } +fn shortest(value: f64) -> String { + if value.is_finite() { value.to_string() } else { "0".into() } +} + fn write_elements(writer: &mut SvgWriter, elements: &[Element], depth: usize) { for element in elements { writer.line(depth); From e3d76471f6c3bcd8e19fba308c62ec0050ae1b64 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:36:05 -0500 Subject: [PATCH 118/520] Fix vector import edge cases from review --- crates/dxf/src/paths.rs | 69 ++++++++++++++++++++++++++--------------- crates/eps/src/paths.rs | 49 +++++++++++++++++++++++------ crates/pdf/src/paths.rs | 44 +++++++++++++++++++++++--- crates/svg/src/write.rs | 11 +++++-- 4 files changed, 130 insertions(+), 43 deletions(-) diff --git a/crates/dxf/src/paths.rs b/crates/dxf/src/paths.rs index 1b57cec40..01e555797 100644 --- a/crates/dxf/src/paths.rs +++ b/crates/dxf/src/paths.rs @@ -14,6 +14,8 @@ pub enum Error { Parse(#[from] crate::DxfError), #[error("DXF contains more than {MAX_PATHS} paths")] TooManyPaths, + #[error("DXF curve exceeds the subdivision limit")] + TooManySegments, #[error("DXF contains a non-finite path coordinate")] NonFinite, } @@ -41,6 +43,9 @@ pub fn import(bytes: &[u8]) -> Result { if !entity.common.visible || record.paper_space() { continue; } + if excessive_sweep(&entity.kind) { + return Err(Error::TooManySegments); + } let converted = entity_paths(&entity.kind, entity.common.extrusion); if converted.iter().flat_map(|path| path.elements()).any(|element| { let finite = |p: Point| p.x.is_finite() && p.y.is_finite(); @@ -54,17 +59,7 @@ pub fn import(bytes: &[u8]) -> Result { return Err(Error::NonFinite); } if converted.is_empty() { - if matches!( - entity.kind, - crate::model::EntityKind::Other - | crate::model::EntityKind::Text(_) - | crate::model::EntityKind::MText(_) - | crate::model::EntityKind::Insert(_) - | crate::model::EntityKind::Dimension(_) - | crate::model::EntityKind::Hatch(_) - ) { - unsupported = unsupported.saturating_add(1); - } + unsupported = unsupported.saturating_add(1); continue; } if paths.len().saturating_add(converted.len()) > MAX_PATHS { @@ -117,27 +112,44 @@ fn entity_paths(kind: &crate::model::EntityKind, normal: crate::model::Point) -> match kind { E::Line { start, end } => vec![line(*start, *end)], E::Circle { center, radius } if radius.is_finite() && *radius > 0.0 => { - vec![circle(ocs(normal, *center), *radius)] + vec![map_ocs_path(circle(Point::new(center[0], center[1]), *radius), normal, center[2])] } E::Arc { center, radius, start_angle, end_angle } if radius.is_finite() && *radius > 0.0 && start_angle.is_finite() && end_angle.is_finite() => { - vec![arc(ocs(normal, *center), *radius, start_angle.to_radians(), end_angle.to_radians())] + vec![map_ocs_path(arc(Point::new(center[0], center[1]), *radius, start_angle.to_radians(), end_angle.to_radians()), normal, center[2])] + } + E::LwPolyline(poly) => { + vec![map_ocs_path(polyline(poly.vertices.iter().map(|v| (Point::new(v.x, v.y), v.bulge)), poly.closed), normal, poly.elevation)] } - E::LwPolyline(poly) => vec![polyline(poly.vertices.iter().map(|v| (ocs(normal, [v.x, v.y, poly.elevation]), v.bulge)), poly.closed)], E::Polyline(poly) if !poly.is_mesh() && !poly.is_polyface() => { - vec![polyline(poly.vertices.iter().filter(|v| !v.is_face()).map(|v| (ocs(normal, v.location), v.bulge)), poly.closed())] + let is_3d = poly.flags & 8 != 0; + let z = if is_3d { 0.0 } else { poly.elevation }; + let path = + polyline(poly.vertices.iter().filter(|v| !v.is_face()).map(|v| (Point::new(v.location[0], v.location[1]), v.bulge)), poly.closed()); + vec![if is_3d { path } else { map_ocs_path(path, normal, z) }] } E::Ellipse { center, major, ratio, start, end } if ratio.is_finite() && *ratio > 0.0 => { - vec![ellipse(*center, *major, *ratio, *start, *end)] + vec![ellipse(*center, *major, normal, *ratio, *start, *end)] } _ => Vec::new(), } } -fn ocs(normal: crate::model::Point, point: crate::model::Point) -> Point { - let p = crate::model::ocs_to_wcs(normal, point); - Point::new(p[0], p[1]) +fn map_ocs_path(path: BezPath, normal: crate::model::Point, z: f64) -> BezPath { + let [x, y, n] = crate::model::ocs_axes(normal); + let transform = kurbo::Affine::new([x[0], y[0], x[1], y[1], n[0] * z, n[1] * z]); + transform * path +} + +fn excessive_sweep(kind: &crate::model::EntityKind) -> bool { + let cap = 4096.0 * std::f64::consts::FRAC_PI_2; + let over = |sweep: f64| !sweep.is_finite() || sweep.abs() > cap; + match kind { + crate::model::EntityKind::Arc { start_angle, end_angle, .. } => over((end_angle - start_angle).to_radians()), + crate::model::EntityKind::Ellipse { start, end, .. } => over(end - start), + _ => false, + } } fn line(start: crate::model::Point, end: crate::model::Point) -> BezPath { @@ -220,11 +232,14 @@ fn arc_segments(path: &mut BezPath, center: Point, start: f64, sweep: f64, radiu } } -fn ellipse(center: crate::model::Point, major: crate::model::Point, ratio: f64, start: f64, end: f64) -> BezPath { - let (cx, cy) = (center[0], center[1]); - let (ax, ay) = (major[0], major[1]); - let (bx, by) = (-ay * ratio, ax * ratio); - let point = |t: f64| Point::new(cx + ax * t.cos() + bx * t.sin(), cy + ay * t.cos() + by * t.sin()); +fn ellipse(center: crate::model::Point, major: crate::model::Point, normal: crate::model::Point, ratio: f64, start: f64, end: f64) -> BezPath { + let minor = cross(normal, major); + let minor_len = minor.iter().map(|v| v * v).sum::().sqrt(); + if !(minor_len.is_finite() && minor_len > 0.0) { + return BezPath::new(); + } + let bx = [minor[0] / minor_len * ratio, minor[1] / minor_len * ratio, minor[2] / minor_len * ratio]; + let point = |t: f64| Point::new(center[0] + major[0] * t.cos() + bx[0] * t.sin(), center[1] + major[1] * t.cos() + bx[1] * t.sin()); let mut path = BezPath::new(); path.move_to(point(start)); let sweep = if end >= start { end - start } else { end - start + std::f64::consts::TAU }; @@ -237,10 +252,14 @@ fn ellipse(center: crate::model::Point, major: crate::model::Point, ratio: f64, let a = start + step * i as f64; let b = a + step; let k = 4.0 / 3.0 * (step / 4.0).tan(); - let derivative = |t: f64| Point::new(-ax * t.sin() + bx * t.cos(), -ay * t.sin() + by * t.cos()); + let derivative = |t: f64| Point::new(-major[0] * t.sin() + bx[0] * t.cos(), -major[1] * t.sin() + bx[1] * t.cos()); let p1 = point(a) + derivative(a).to_vec2() * k; let p2 = point(b) - derivative(b).to_vec2() * k; path.curve_to(p1, p2, point(b)); } path } + +fn cross(a: crate::model::Point, b: crate::model::Point) -> crate::model::Point { + [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]] +} diff --git a/crates/eps/src/paths.rs b/crates/eps/src/paths.rs index adbe294f3..ad1f23793 100644 --- a/crates/eps/src/paths.rs +++ b/crates/eps/src/paths.rs @@ -45,24 +45,46 @@ pub fn import(bytes: &[u8], limits: Limits) -> Result { return Err(Error::InputLimit { limit: limits.max_input_bytes }); } let source = vectorcraft_eps::import(bytes).map_err(Error::Parse)?; - let page_bounds = source - .document - .artboards - .first() - .map_or(Rect::new(0.0, 0.0, 612.0, 792.0), |board| Rect::new(board.rect.x0, board.rect.y0, board.rect.x1, board.rect.y1)); + let section = crate::sections(bytes).map_err(|e| Error::Parse(e.to_string()))?; + let source_box = crate::Comments::read(section.postscript).page_box().unwrap_or([0.0, 0.0, 612.0, 792.0]); + let [llx, _lly, urx, ury] = source_box; + let page_bounds = Rect::new(llx, lly, urx, ury); let mut paths = Vec::new(); let mut total_elements = 0usize; + let mut clipped = false; + let mut omitted = false; for layer in &source.document.layers { - collect(layer, &mut paths, &mut total_elements, limits)?; + collect(layer, &mut paths, &mut total_elements, limits, &mut clipped, &mut omitted)?; } - Ok(Imported { paths, page_bounds, warnings: source.warnings, preview_fallback: source.preview }) + let inverse_page = kurbo::Affine::new([1.0, 0.0, 0.0, -1.0, llx, ury]); + for path in &mut paths { + *path = inverse_page * path.clone(); + } + let mut warnings = source.warnings; + if clipped { + warnings.push("EPS clipping groups are returned as unclipped artwork".into()); + } + if omitted { + warnings.push("visible EPS text or image content is not returned as paths".into()); + } + Ok(Imported { paths, page_bounds, warnings, preview_fallback: source.preview }) } -fn collect(node: &vectorcraft_doc::Node, paths: &mut Vec, elements: &mut usize, limits: Limits) -> Result<(), Error> { +fn collect( + node: &vectorcraft_doc::Node, + paths: &mut Vec, + elements: &mut usize, + limits: Limits, + clipped: &mut bool, + omitted: &mut bool, +) -> Result<(), Error> { if !node.visible { return Ok(()); } if let Some(data) = node.path_data() { + if matches!(node.kind, vectorcraft_doc::NodeKind::Path { clipping: true, .. }) { + return Ok(()); + } let path = data.to_bezpath(); if !path.elements().is_empty() { *elements = elements.saturating_add(path.elements().len()); @@ -72,9 +94,16 @@ fn collect(node: &vectorcraft_doc::Node, paths: &mut Vec, elements: &mu paths.push(path); } } else if let Some(children) = node.children() { - for child in children { - collect(child, paths, elements, limits)?; + let is_clip = matches!(node.kind, vectorcraft_doc::NodeKind::Group { clip: true, .. } | vectorcraft_doc::NodeKind::Layer { clip: true, .. }); + *clipped |= is_clip; + for (index, child) in children.iter().enumerate() { + if is_clip && index == 0 { + continue; + } + collect(child, paths, elements, limits, clipped, omitted)?; } + } else { + *omitted = true; } Ok(()) } diff --git a/crates/pdf/src/paths.rs b/crates/pdf/src/paths.rs index bb51e2a0f..9f7126013 100644 --- a/crates/pdf/src/paths.rs +++ b/crates/pdf/src/paths.rs @@ -8,6 +8,10 @@ use hayro_interpret::{ BlendMode, ClipPath, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, Paint, PathDrawMode, RectExt, SoftMask, }; use kurbo::{Affine, BezPath, Rect}; +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; /// Limits for vector path extraction. They bound the output model, not interpreter peak memory. #[derive(Debug, Clone, Copy)] @@ -61,14 +65,31 @@ pub fn import(bytes: &[u8], limits: Limits) -> Result { return Err(Error::Pages { limit: limits.max_pages }); } let cache = InterpreterCache::new(); + let truncated = Arc::new(AtomicBool::new(false)); + let truncation_flag = truncated.clone(); + let settings = InterpreterSettings { + warning_sink: Arc::new(move |warning| { + if matches!(warning, hayro_interpret::InterpreterWarning::ContentTruncated) { + truncation_flag.store(true, Ordering::Relaxed); + } + }), + ..InterpreterSettings::default() + }; let mut pages = Vec::with_capacity(pdf.pages().len()); let mut total_paths = 0usize; let mut total_elements = 0usize; for page in pdf.pages().iter() { let crop_box = page.intersected_crop_box().to_kurbo(); - let mut context = hayro_interpret::Context::new(Affine::IDENTITY, crop_box, &cache, pdf.xref(), InterpreterSettings::default()); - let mut device = Collector::default(); + let mut context = hayro_interpret::Context::new(Affine::IDENTITY, crop_box, &cache, pdf.xref(), settings.clone()); + let mut device = Collector { + max_paths: limits.max_paths.saturating_sub(total_paths), + max_elements: limits.max_path_elements.saturating_sub(total_elements), + ..Collector::default() + }; crate::read::interpret_page(page, &mut context, &mut device)?; + if device.exceeded { + return Err(Error::OutputLimit); + } total_paths = total_paths.saturating_add(device.paths.len()); total_elements = device.paths.iter().fold(total_elements, |n, path| n.saturating_add(path.elements().len())); if total_paths > limits.max_paths || total_elements > limits.max_path_elements { @@ -92,6 +113,9 @@ pub fn import(bytes: &[u8], limits: Limits) -> Result { if pages.iter().any(|page| page.clips_seen > 0) { warnings.push("clip paths are not applied to extracted geometry".into()); } + if truncated.load(Ordering::Relaxed) { + warnings.push("PDF content was truncated by the interpreter".into()); + } Ok(Imported { pages, warnings }) } @@ -101,16 +125,26 @@ struct Collector { omitted_text: usize, omitted_images: usize, clips_seen: usize, + max_paths: usize, + max_elements: usize, + elements: usize, + exceeded: bool, } impl Device<'_> for Collector { fn set_soft_mask(&mut self, _: Option>) {} fn set_blend_mode(&mut self, _: BlendMode) {} fn draw_path(&mut self, path: &BezPath, transform: Affine, _: &Paint<'_>, _: &PathDrawMode) { - let path = transform * path.clone(); - if !path.is_empty() { - self.paths.push(path); + if path.is_empty() { + return; } + let count = path.elements().len(); + if self.paths.len() >= self.max_paths || self.elements.saturating_add(count) > self.max_elements { + self.exceeded = true; + return; + } + self.elements = self.elements.saturating_add(count); + self.paths.push(transform * path.clone()); } fn push_clip_path(&mut self, _: &ClipPath) { self.clips_seen = self.clips_seen.saturating_add(1); diff --git a/crates/svg/src/write.rs b/crates/svg/src/write.rs index 664ce8f01..b73c8c1bf 100644 --- a/crates/svg/src/write.rs +++ b/crates/svg/src/write.rs @@ -107,7 +107,11 @@ impl Scene { } fn write_attributes<'a>(mut tag: craft_xml::Tag<'a>, attributes: &'a [Attribute]) -> craft_xml::Tag<'a> { - for attribute in attributes { + // SVG consumers disagree about duplicate attributes; make the last supplied value win. + for (index, attribute) in attributes.iter().enumerate() { + if attributes[index + 1..].iter().any(|later| later.name == attribute.name) { + continue; + } tag = match &attribute.value { Value::Text(value) => tag.attr(&attribute.name, value), Value::Number(value) => tag.attr(&attribute.name, num(*value, 3)), @@ -136,8 +140,9 @@ fn write_elements(writer: &mut SvgWriter, elements: &[Element], depth: usize) { writer.close(); } Element::Path { attributes, path } => { - let attributes = attributes.clone(); - write_attributes(writer.tag("path").attr("d", path_data(path)), &attributes).empty(); + let mut attrs = vec![Attribute::text("d", path_data(path))]; + attrs.extend(attributes.iter().cloned()); + write_attributes(writer.tag("path"), &attrs).empty(); } Element::Rect { attributes, x, y, width, height } => { let mut attrs = vec![ From 7fbe3837bcfda6d85fb193402922e8f3cde8b94d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:39:40 -0500 Subject: [PATCH 119/520] Fix EPS path bounds variable --- crates/eps/src/paths.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eps/src/paths.rs b/crates/eps/src/paths.rs index ad1f23793..c4adc0d1b 100644 --- a/crates/eps/src/paths.rs +++ b/crates/eps/src/paths.rs @@ -47,7 +47,7 @@ pub fn import(bytes: &[u8], limits: Limits) -> Result { let source = vectorcraft_eps::import(bytes).map_err(Error::Parse)?; let section = crate::sections(bytes).map_err(|e| Error::Parse(e.to_string()))?; let source_box = crate::Comments::read(section.postscript).page_box().unwrap_or([0.0, 0.0, 612.0, 792.0]); - let [llx, _lly, urx, ury] = source_box; + let [llx, lly, urx, ury] = source_box; let page_bounds = Rect::new(llx, lly, urx, ury); let mut paths = Vec::new(); let mut total_elements = 0usize; From 27d3b4acf5ac48d62db2e9390e08e5a4e2daea7b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:08:30 -0500 Subject: [PATCH 120/520] Keep StencilCraft v1 fixtures with shared document crate --- crates/stencilcraft-document/src/lib.rs | 9 +- .../tests/fixtures/poster.stencilcraft | 98 ++++++++++++++++ .../tests/fixtures/starter.stencilcraft | 111 ++++++++++++++++++ .../tests/fixtures/stencil-frame.stencilcraft | 86 ++++++++++++++ 4 files changed, 301 insertions(+), 3 deletions(-) create mode 100644 crates/stencilcraft-document/tests/fixtures/poster.stencilcraft create mode 100644 crates/stencilcraft-document/tests/fixtures/starter.stencilcraft create mode 100644 crates/stencilcraft-document/tests/fixtures/stencil-frame.stencilcraft diff --git a/crates/stencilcraft-document/src/lib.rs b/crates/stencilcraft-document/src/lib.rs index a966536c5..bca33fb8d 100644 --- a/crates/stencilcraft-document/src/lib.rs +++ b/crates/stencilcraft-document/src/lib.rs @@ -772,9 +772,12 @@ mod tests { #[test] fn version_1_files_without_canvas_fields_still_load() { - for name in ["starter", "poster", "stencil-frame"] { - let path = format!("{}/../../examples/{name}.stencilcraft", env!("CARGO_MANIFEST_DIR")); - let doc = load_from(&path).unwrap(); + for json in [ + include_str!("../tests/fixtures/starter.stencilcraft"), + include_str!("../tests/fixtures/poster.stencilcraft"), + include_str!("../tests/fixtures/stencil-frame.stencilcraft"), + ] { + let doc = Document::load_json(json).unwrap(); assert!(doc.pages.is_empty() && doc.groups.is_empty() && doc.guides.is_empty()); assert_eq!(doc.grid, Grid::default()); assert!(doc.layers.iter().all(|l| !l.template && !l.locked)); diff --git a/crates/stencilcraft-document/tests/fixtures/poster.stencilcraft b/crates/stencilcraft-document/tests/fixtures/poster.stencilcraft new file mode 100644 index 000000000..4de690925 --- /dev/null +++ b/crates/stencilcraft-document/tests/fixtures/poster.stencilcraft @@ -0,0 +1,98 @@ +{ + "board": { + "height_mm": 400.0, + "width_mm": 500.0 + }, + "kerf": { + "apply_to_exports": false, + "width_mm": 0.0 + }, + "layers": [ + { + "kind": "cut", + "name": "cut", + "shapes": [ + { + "id": 1, + "path": [ + { + "MoveTo": { + "x": 10.0, + "y": 20.0 + } + }, + { + "LineTo": { + "x": 70.0, + "y": 20.0 + } + }, + { + "LineTo": { + "x": 70.0, + "y": 80.0 + } + }, + { + "LineTo": { + "x": 10.0, + "y": 80.0 + } + }, + "ClosePath" + ], + "style": "cut" + }, + { + "id": 2, + "path": [ + { + "MoveTo": { + "x": 10.0, + "y": 390.0 + } + }, + { + "LineTo": { + "x": 490.0, + "y": 10.0 + } + } + ], + "style": "cut" + } + ], + "visible": true + }, + { + "kind": "engrave", + "name": "engrave", + "shapes": [ + { + "id": 3, + "path": [ + { + "MoveTo": { + "x": 100.0, + "y": 200.0 + } + }, + { + "LineTo": { + "x": 400.0, + "y": 200.0 + } + } + ], + "style": "engrave" + } + ], + "visible": true + } + ], + "next-shape-id": 4, + "stencilcraft": { + "name": "stencilcraft", + "version": 1 + } +} diff --git a/crates/stencilcraft-document/tests/fixtures/starter.stencilcraft b/crates/stencilcraft-document/tests/fixtures/starter.stencilcraft new file mode 100644 index 000000000..2a18fbb11 --- /dev/null +++ b/crates/stencilcraft-document/tests/fixtures/starter.stencilcraft @@ -0,0 +1,111 @@ +{ + "board": { + "height_mm": 150.0, + "width_mm": 100.0 + }, + "kerf": { + "apply_to_exports": false, + "width_mm": 0.0 + }, + "layers": [ + { + "kind": "cut", + "name": "cut", + "shapes": [ + { + "id": 1, + "path": [ + { + "MoveTo": { + "x": 10.0, + "y": 10.0 + } + }, + { + "LineTo": { + "x": 60.0, + "y": 10.0 + } + }, + { + "LineTo": { + "x": 60.0, + "y": 60.0 + } + }, + { + "LineTo": { + "x": 10.0, + "y": 60.0 + } + }, + "ClosePath" + ], + "style": "cut" + }, + { + "id": 2, + "path": [ + { + "MoveTo": { + "x": 70.0, + "y": 20.0 + } + }, + { + "LineTo": { + "x": 85.0, + "y": 20.0 + } + }, + { + "LineTo": { + "x": 85.0, + "y": 60.0 + } + }, + { + "LineTo": { + "x": 70.0, + "y": 60.0 + } + }, + "ClosePath" + ], + "style": "cut" + } + ], + "visible": true + }, + { + "kind": "engrave", + "name": "engrave", + "shapes": [ + { + "id": 3, + "path": [ + { + "MoveTo": { + "x": 10.0, + "y": 90.0 + } + }, + { + "LineTo": { + "x": 90.0, + "y": 90.0 + } + } + ], + "style": "engrave" + } + ], + "visible": true + } + ], + "next-shape-id": 4, + "stencilcraft": { + "name": "stencilcraft", + "version": 1 + } +} \ No newline at end of file diff --git a/crates/stencilcraft-document/tests/fixtures/stencil-frame.stencilcraft b/crates/stencilcraft-document/tests/fixtures/stencil-frame.stencilcraft new file mode 100644 index 000000000..3e0a77ed8 --- /dev/null +++ b/crates/stencilcraft-document/tests/fixtures/stencil-frame.stencilcraft @@ -0,0 +1,86 @@ +{ + "board": { + "height_mm": 150.0, + "width_mm": 120.0 + }, + "kerf": { + "apply_to_exports": true, + "width_mm": 0.2 + }, + "layers": [ + { + "kind": "cut", + "name": "cut", + "shapes": [ + { + "id": 1, + "path": [ + { + "MoveTo": { + "x": 0.0, + "y": 0.0 + } + }, + { + "LineTo": { + "x": 100.0, + "y": 0.0 + } + }, + { + "LineTo": { + "x": 100.0, + "y": 100.0 + } + }, + { + "LineTo": { + "x": 0.0, + "y": 100.0 + } + }, + "ClosePath" + ], + "style": "cut" + }, + { + "id": 2, + "path": [ + { + "MoveTo": { + "x": 30.0, + "y": 30.0 + } + }, + { + "LineTo": { + "x": 70.0, + "y": 30.0 + } + }, + { + "LineTo": { + "x": 70.0, + "y": 70.0 + } + }, + { + "LineTo": { + "x": 30.0, + "y": 70.0 + } + }, + "ClosePath" + ], + "style": "cut" + } + ], + "visible": true + } + ], + "next-shape-id": 3, + "stencilcraft": { + "name": "stencilcraft", + "version": 1 + } +} From 10e7f7763a5bae00a9f30929dbbc9aaf084b57a0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:13:55 -0500 Subject: [PATCH 121/520] Test typed StencilCraft document readers --- crates/stencilcraft-format/src/lib.rs | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/stencilcraft-format/src/lib.rs b/crates/stencilcraft-format/src/lib.rs index 9a0df0093..1ff64af59 100644 --- a/crates/stencilcraft-format/src/lib.rs +++ b/crates/stencilcraft-format/src/lib.rs @@ -364,6 +364,7 @@ pub enum Error { mod tests { use std::io::{self, Cursor, Read}; + use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use super::{DocumentFields, Error, FORMAT_VERSION, File}; @@ -469,4 +470,43 @@ mod tests { assert!(matches!(File::read("{"), Err(Error::Json(_)))); assert!(matches!(File::read(r#"{"stencilcraft":{"name":"stencilcraft","version":1}}"#), Err(Error::Document { .. }))); } + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct TypedBoard { + width_mm: f64, + height_mm: f64, + } + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct TypedFields { + board: TypedBoard, + #[serde(default, rename = "board-setup", skip_serializing_if = "Option::is_none")] + board_setup: Option, + } + + fn typed_fields() -> TypedFields { + TypedFields { board: TypedBoard { width_mm: 100.0, height_mm: 150.0 }, board_setup: None } + } + + #[test] + fn typed_readers_round_trip_typed_fields_and_preserve_header_errors() -> Result<(), Error> { + let file = File::new(typed_fields()); + let text = file.to_json_pretty()?; + assert!(!text.contains("board-setup")); + assert_eq!(File::read_typed(&text)?, file); + assert_eq!(File::read_typed_from(Cursor::new(text.as_bytes()))?, file); + + let malformed = r#"{"stencilcraft":{"name":"stencilcraft","version":1},"board":"wrong"}"#; + assert!(matches!(File::::read_typed(malformed), Err(Error::Document { .. }))); + assert!(matches!(File::::read_typed_from(Cursor::new(malformed)), Err(Error::Document { .. }))); + + let wrong_header = r#"{"stencilcraft":{"name":"other","version":1},"board":{}}"#; + assert!(matches!(File::::read_typed(wrong_header), Err(Error::NotStencilCraft))); + assert!(matches!(File::::read_typed_from(Cursor::new(wrong_header)), Err(Error::NotStencilCraft))); + + let future_version = r#"{"stencilcraft":{"name":"stencilcraft","version":2},"board":{}}"#; + assert!(matches!(File::::read_typed(future_version), Err(Error::Version { found: 2, supported: 1 }))); + assert!(matches!(File::::read_typed_from(Cursor::new(future_version)), Err(Error::Version { found: 2, supported: 1 }))); + Ok(()) + } } From b32e55a956e6f25517468cc2563c79bc59e16f1b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 09:21:57 +0000 Subject: [PATCH 122/520] Preserve session repairs and frame grid boundaries --- crates/scraft-format/src/session.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/scraft-format/src/session.rs b/crates/scraft-format/src/session.rs index 5f73f940e..021cab8bc 100644 --- a/crates/scraft-format/src/session.rs +++ b/crates/scraft-format/src/session.rs @@ -348,6 +348,7 @@ impl AutoName { pub struct Session { pub format_version: u32, pub name: String, + #[serde(deserialize_with = "sample_rate_or_default")] pub sample_rate: SampleRate, pub bit_depth: BitDepthSetting, pub frame_rate: FrameRate, @@ -384,6 +385,17 @@ pub struct Session { pub folder: Option, } +/// Older SoundCraft builds repaired an in-range but invalid sample rate after reading a session. +/// `craft-time` validates `SampleRate` during serde, so keep that file compatibility at this +/// domain boundary while retaining strict serde validation for direct `SampleRate` use. +fn sample_rate_or_default<'de, D>(deserializer: D) -> Result +where + D: serde::Deserializer<'de>, +{ + let hz = ::deserialize(deserializer)?; + Ok(SampleRate::new(hz).unwrap_or_default()) +} + impl Default for Session { fn default() -> Self { Session::new("Untitled", SampleRate::HZ_48000) From 2f281a419c90282348306d2ba019ce8f0cc9f3f0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 09:22:49 +0000 Subject: [PATCH 123/520] Align frame grid lines with typed position rounding --- crates/time/src/soundcraft/grid.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/time/src/soundcraft/grid.rs b/crates/time/src/soundcraft/grid.rs index 2d40a454b..9055340ba 100644 --- a/crates/time/src/soundcraft/grid.rs +++ b/crates/time/src/soundcraft/grid.rs @@ -219,15 +219,15 @@ impl FeetFramesValue { } } - /// Apply the counter origin in frames before rounding once to the nearest sample. + /// Apply the counter origin in frames before rounding once to the first sample at the frame + /// boundary. This matches `FrameRate::samples_at_frame` and typed Feet+Frames positions. fn sample_line(self, index: i128, sr: SampleRate, rate: FrameRate, origin: i64) -> Samples { let (num, den) = self.frame_ratio(); let (rate_num, rate_den) = rate.rational(); let frames = index * num - i128::from(origin) * den; let samples = frames * i128::from(sr.hz().max(1)) * i128::from(rate_den); let divisor = den * i128::from(rate_num); - let magnitude = (samples.abs() + divisor / 2) / divisor; - let rounded = if samples < 0 { -magnitude } else { magnitude }; + let rounded = samples.div_euclid(divisor) + i128::from(samples.rem_euclid(divisor) != 0); i64::try_from(rounded).unwrap_or(if rounded < 0 { i64::MIN } else { i64::MAX }) } @@ -588,6 +588,7 @@ fn step_enum(values: &[T], value: T, larger: bool) -> Optio #[cfg(test)] mod tests { use super::*; + use crate::soundcraft::TimeFormat; /// Frame origins are subtracted before rounding whole frames, quarter frames and subframes. #[test] @@ -601,6 +602,24 @@ mod tests { assert_eq!(FeetFramesValue::SubFrame.lines(49_665, 49_667, sr, rate, 1, 4), [49_666]); } + /// Grid lines use the same ceil-rounded sample boundary as typed positions at fractional rates. + #[test] + fn feet_frames_grid_lines_match_typed_positions_at_fractional_rates() { + let sr = SampleRate::HZ_48000; + let map = TempoMap::default(); + for rate in [FrameRate::Fps23_976, FrameRate::Fps29_97, FrameRate::Fps29_97Drop, FrameRate::Fps59_94Drop] { + for origin in [-17, -1, 1, 17] { + for feet in [-64, -2, 0, 1, 2, 4, 7, 12, 128] { + let text = format!("{feet}+00"); + let at = crate::soundcraft::format::parse_position(&text, TimeFormat::FeetFrames, sr, &map, rate, 0, origin).unwrap(); + for value in FeetFramesValue::ALL { + assert_eq!(value.line_at_or_before(at, sr, rate, origin), at, "{rate:?} {value:?} origin {origin}, {text}"); + } + } + } + } + } + /// Dense subframes retain distinct sample lines and remain bounded with extreme signed origins. #[test] fn feet_frames_subframes_handle_dense_steps_and_extreme_origins() { From 00d82f5bc7d3ca15e488fe21d08a951c029643c2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:23:57 -0500 Subject: [PATCH 124/520] Add bounded SVG tree rendering facade --- Cargo.lock | 39 ++++++++++++- crates/svg/Cargo.toml | 7 +++ crates/svg/src/tree.rs | 103 ++++++++++++++++++++++++++-------- crates/svg/src/tree/draw.rs | 109 ++++++++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 09d5fa6a9..9a8abed4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2391,9 +2391,11 @@ dependencies = [ name = "craft-svg" version = "0.1.0" dependencies = [ + "craft-image-codecs", "craft-xml", "flate2", "kurbo 0.13.1", + "resvg 0.48.1", "roxmltree 0.21.2", "thiserror 2.0.21", "usvg 0.48.1", @@ -3381,7 +3383,7 @@ dependencies = [ "itertools 0.15.0", "log", "profiling", - "resvg", + "resvg 0.45.1", ] [[package]] @@ -7567,10 +7569,28 @@ dependencies = [ "pico-args", "rgb", "svgtypes 0.15.3", - "tiny-skia", + "tiny-skia 0.11.4", "usvg 0.45.1", ] +[[package]] +name = "resvg" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67e3803f97b999e80cbf7c6ecdd07a8102204d92e1633cf48783720c521196bd" +dependencies = [ + "bytemuck", + "gif", + "image-webp", + "log", + "pico-args", + "rgb", + "svgtypes 0.16.2", + "tiny-skia 0.12.0", + "usvg 0.48.1", + "zune-jpeg", +] + [[package]] name = "rfc6979" version = "0.4.0" @@ -9108,6 +9128,21 @@ dependencies = [ "tiny-skia-path 0.11.4", ] +[[package]] +name = "tiny-skia" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47ffee5eaaf5527f630fb0e356b90ebdec84d5d18d937c5e440350f88c5a91ea" +dependencies = [ + "arrayref", + "arrayvec", + "bytemuck", + "cfg-if", + "log", + "png 0.18.1", + "tiny-skia-path 0.12.0", +] + [[package]] name = "tiny-skia-path" version = "0.11.4" diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml index 49b22e213..3d20a0e5b 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -12,6 +12,7 @@ workspace = true [dependencies] craft-xml = { workspace = true } +craft-image-codecs = { workspace = true } # SVGZ (gzip), pure-Rust backend. flate2 = { version = "1", default-features = false, features = ["rust_backend"] } kurbo = { workspace = true } @@ -20,6 +21,9 @@ thiserror = { workspace = true } # The full SVG parser (Apache-2.0 OR MIT). The one version every Craft app uses: apps depend on # craft-svg's `tree` feature and take usvg from its re-export, never directly. usvg = { version = "=0.48.1", default-features = false, optional = true } +# Rendering the parsed tree uses the matching renderer version. Keep it behind an opt-in feature +# so callers that only need bounded SVG text/tree parsing do not pull in raster backends. +resvg = { version = "=0.48.1", default-features = false, features = ["text", "raster-images"], optional = true } [features] # `craft_svg::tree`: usvg behind the bounded entry point. @@ -28,3 +32,6 @@ tree = ["dep:usvg"] tree-text = ["tree", "usvg/text"] # The system's fonts in `usvg::fontdb`. tree-system-fonts = ["tree-text", "usvg/system-fonts"] +# Bounded rendering of a parsed tree through the matching resvg/tiny-skia versions. +tree-render = ["tree", "dep:resvg"] +tree-render-system-fonts = ["tree-render", "resvg/system-fonts"] diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index dd14be619..742c5c3e1 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -22,10 +22,10 @@ //! 16 MB or so, and a 1 MB stack takes a depth of about 34. use std::borrow::Cow; -use std::collections::HashMap; use std::collections::hash_map::Entry; -use std::sync::Arc; -use std::sync::atomic::{AtomicUsize, Ordering}; +use std::collections::{HashMap, HashSet}; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; pub use usvg; @@ -58,6 +58,10 @@ pub struct Limits { pub embedded_bytes: usize, /// SVG documents embedded inside one another, for [`image_resolver`]. pub embedded_depth: usize, + /// Decoded RGBA pixels retained across the raster images embedded in one drawing. Dimensions + /// are probed before usvg hands the bytes to a decoder; repeated references to the same data + /// URL share one charge. + pub embedded_raster_pixels: u64, } impl Default for Limits { @@ -72,6 +76,7 @@ impl Default for Limits { xml_nodes: 4_000_000, embedded_bytes: 32 << 20, embedded_depth: 4, + embedded_raster_pixels: 24_000_000, } } } @@ -264,13 +269,56 @@ pub fn height(xml: &roxmltree::Document<'_>, most: usize) -> Result>, } -fn raster(data: &[u8]) -> bool { - data.starts_with(b"\x89PNG") - || data.starts_with(&[0xff, 0xd8, 0xff]) - || data.starts_with(b"GIF8") - || (data.len() > 12 && data.get(8..12) == Some(b"WEBP")) +fn raster_format(data: &[u8]) -> Option { + match craft_image_codecs::detect(data)? { + format @ (craft_image_codecs::Format::Png + | craft_image_codecs::Format::Jpeg + | craft_image_codecs::Format::Gif + | craft_image_codecs::Format::WebP) => Some(format), + _ => None, + } +} + +fn raster_image(used: &Used, limits: &Limits, data: Arc>, format: craft_image_codecs::Format) -> Option { + let (width, height) = craft_image_codecs::probe_allocation_size(&data).ok()?; + if !matches!( + format, + craft_image_codecs::Format::Png | craft_image_codecs::Format::Jpeg | craft_image_codecs::Format::Gif | craft_image_codecs::Format::WebP + ) { + return None; + } + let pixels = u64::from(width).checked_mul(u64::from(height))?; + if pixels == 0 || pixels > limits.embedded_raster_pixels { + return None; + } + let pointer = Arc::as_ptr(&data) as usize; + let mut seen = used.raster_data.lock().unwrap_or_else(PoisonError::into_inner); + if seen.insert(pointer) { + let mut current = used.raster_pixels.load(Ordering::Relaxed); + loop { + let next = current.checked_add(pixels)?; + if next > limits.embedded_raster_pixels { + seen.remove(&pointer); + return None; + } + match used.raster_pixels.compare_exchange_weak(current, next, Ordering::Relaxed, Ordering::Relaxed) { + Ok(_) => break, + Err(actual) => current = actual, + } + } + } + drop(seen); + Some(match format { + craft_image_codecs::Format::Png => usvg::ImageKind::PNG(data), + craft_image_codecs::Format::Jpeg => usvg::ImageKind::JPEG(data), + craft_image_codecs::Format::Gif => usvg::ImageKind::GIF(data), + craft_image_codecs::Format::WebP => usvg::ImageKind::WEBP(data), + _ => return None, + }) } /// An embedded SVG document, if the budgets allow it. @@ -302,22 +350,31 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' /// [`Limits::embedded_depth`] inside one another. An image past a limit is left out. Linked /// files are never read. One resolver is one import's budget: make a new one for each [`parse`]. pub fn image_resolver(limits: Limits) -> usvg::ImageHrefResolver<'static> { - let used = Arc::new(Used { bytes: AtomicUsize::new(0), depth: AtomicUsize::new(0) }); + let used = Arc::new(Used { + bytes: AtomicUsize::new(0), + depth: AtomicUsize::new(0), + raster_pixels: AtomicU64::new(0), + raster_data: Mutex::new(HashSet::new()), + }); usvg::ImageHrefResolver { - resolve_data: Box::new(move |mime, data, options| match mime { - "image/jpg" | "image/jpeg" => Some(usvg::ImageKind::JPEG(data)), - "image/png" => Some(usvg::ImageKind::PNG(data)), - "image/gif" => Some(usvg::ImageKind::GIF(data)), - "image/webp" => Some(usvg::ImageKind::WEBP(data)), - "image/svg+xml" => embedded(&used, &limits, &data, options), - "text/plain" if raster(&data) => match data.first() { - Some(0x89) => Some(usvg::ImageKind::PNG(data)), - Some(0xff) => Some(usvg::ImageKind::JPEG(data)), - Some(b'G') => Some(usvg::ImageKind::GIF(data)), - _ => Some(usvg::ImageKind::WEBP(data)), - }, - "text/plain" => embedded(&used, &limits, &data, options), - _ => None, + resolve_data: Box::new(move |mime, data, options| { + let detected = raster_format(&data); + let format = match mime { + "image/jpg" | "image/jpeg" => (detected == Some(craft_image_codecs::Format::Jpeg)).then_some(craft_image_codecs::Format::Jpeg), + "image/png" => (detected == Some(craft_image_codecs::Format::Png)).then_some(craft_image_codecs::Format::Png), + "image/gif" => (detected == Some(craft_image_codecs::Format::Gif)).then_some(craft_image_codecs::Format::Gif), + "image/webp" => (detected == Some(craft_image_codecs::Format::WebP)).then_some(craft_image_codecs::Format::WebP), + "text/plain" => detected, + _ => None, + }; + if let Some(format) = format { + return raster_image(&used, &limits, data, format); + } + match mime { + "image/svg+xml" => embedded(&used, &limits, &data, options), + "text/plain" => embedded(&used, &limits, &data, options), + _ => None, + } }), resolve_string: Box::new(|_, _| None), } diff --git a/crates/svg/src/tree/draw.rs b/crates/svg/src/tree/draw.rs index fed7ac569..d472fdc7d 100644 --- a/crates/svg/src/tree/draw.rs +++ b/crates/svg/src/tree/draw.rs @@ -24,6 +24,9 @@ use std::collections::HashMap; use usvg::Transform; +#[cfg(feature = "tree-render")] +pub use resvg::tiny_skia; + /// Filter primitives per filter. A result of every primitive is kept; real filters have 1 to 12 /// (Inkscape's drop shadow has 5), and 64 leaves headroom for generated ones. const MAX_PRIMITIVES: usize = 64; @@ -157,6 +160,112 @@ pub fn check(nodes: &[usvg::Node], ts: Transform, width: u32, height: u32, held: walk.nodes(nodes, ts, cx).map(|_| ()) } +/// Draws a complete tree onto a transparent pixmap after checking the peak RGBA allocation. +/// `held` is caller-owned RGBA storage that remains live during the draw; the target itself is +/// charged by this function. +#[cfg(feature = "tree-render")] +pub fn render(tree: &usvg::Tree, ts: Transform, width: u32, height: u32, held: u64, budget: u64) -> Result { + check_filter_cost(tree, ts, 2_000_000_000, 10_000_000, 200)?; + check(tree.root().children(), ts, width, height, held, budget)?; + let mut pixmap = tiny_skia::Pixmap::new(width, height).ok_or_else(|| Refusal { + complex: false, + memory: true, + message: format!("the {width}×{height} px target pixmap could not be allocated"), + })?; + resvg::render(tree, ts, &mut pixmap.as_mut()); + Ok(pixmap) +} + +/// Draws one node onto `target` after checking the peak RGBA allocation. The node remains in its +/// parent's coordinate space, matching `resvg::render_node`. +#[cfg(feature = "tree-render")] +pub fn render_node(node: &usvg::Node, ts: Transform, target: &mut tiny_skia::Pixmap, held: u64, budget: u64) -> Result { + check(std::slice::from_ref(node), ts, target.width(), target.height(), held, budget)?; + Ok(resvg::render_node(node, ts, &mut target.as_mut()).is_some()) +} + +/// Refuse a tree whose filters would exceed `max_work` pixel reads, or whose referenced roots +/// exceed `max_visits` and `max_depth`. +pub fn check_filter_cost(tree: &usvg::Tree, ts: Transform, max_work: u64, max_visits: usize, max_depth: usize) -> Result<(), Refusal> { + let mut visits = max_visits; + let cost = filter_cost(tree.root(), ts, 0, &mut visits, max_depth); + if cost > max_work { + Err(Refusal { + complex: visits == 0, + memory: false, + message: "the drawing's filters are too costly to render (a large morphology radius, convolution kernel or turbulence octave count, or patterns nested too deep)".into(), + }) + } else { + Ok(()) + } +} + +fn filter_cost(root: &usvg::Group, base: Transform, rec: usize, visits: &mut usize, max_depth: usize) -> u64 { + use usvg::filter::Kind; + let mut cost = 0u64; + let mut stack: Vec<&usvg::Group> = vec![root]; + while let Some(group) = stack.pop() { + let ts = base.pre_concat(group.abs_transform()); + let (sx, sy) = ts.get_scale(); + for filter in group.filters() { + let Some(rect) = filter.rect().transform(ts) else { continue }; + let (width, height) = (rect.width().ceil() as u64, rect.height().ceil() as u64); + let window = |radius: f32, scale: f32, side: u64| ((radius * scale).ceil() as u64).saturating_mul(2).min(side); + for primitive in filter.primitives() { + let reads = match primitive.kind() { + Kind::ConvolveMatrix(conv) => u64::from(conv.matrix().columns()) * u64::from(conv.matrix().rows()), + Kind::Morphology(morphology) => { + window(morphology.radius_x().get(), sx, width).saturating_mul(window(morphology.radius_y().get(), sy, height)) + } + Kind::Turbulence(turbulence) => u64::from(turbulence.num_octaves()).saturating_mul(16), + _ => 0, + }; + cost = cost.saturating_add(width.saturating_mul(height).saturating_mul(reads.saturating_sub(256))); + } + } + for node in group.children() { + *visits = visits.saturating_sub(1); + if *visits == 0 { + return u64::MAX; + } + if let usvg::Node::Group(child) = node { + stack.push(child); + } + if rec >= max_depth { + let mut skipped = false; + match node { + usvg::Node::Path(path) => { + skipped = [path.fill().map(|fill| fill.paint()), path.stroke().map(|stroke| stroke.paint())] + .into_iter() + .flatten() + .any(|paint| matches!(paint, usvg::Paint::Pattern(_))); + } + _ => node.subroots(|_| skipped = true), + } + if skipped { + return u64::MAX; + } + continue; + } + let world = base.pre_concat(node.abs_transform()); + let mut sub = + |root: &usvg::Group, transform: Transform| cost = cost.saturating_add(filter_cost(root, transform, rec + 1, visits, max_depth)); + match node { + usvg::Node::Path(path) => { + for paint in [path.fill().map(|fill| fill.paint()), path.stroke().map(|stroke| stroke.paint())].into_iter().flatten() { + if let usvg::Paint::Pattern(pattern) = paint { + let (sx, sy) = world.pre_concat(pattern.transform()).get_scale(); + sub(pattern.root(), Transform::from_scale(sx, sy)); + } + } + } + _ => node.subroots(|root| sub(root, world)), + } + } + } + cost +} + impl Walk { fn need(&self, live: f64, what: impl FnOnce() -> String) -> Result<(), Refusal> { if live > self.budget { Err(memory(&what(), live, self.budget)) } else { Ok(()) } From 23e633895d06ed0a4cbb8a2df857497837bc252a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 03:55:37 -0500 Subject: [PATCH 125/520] Track bounded SVG image resolver usage --- crates/svg/src/svgz.rs | 2 +- crates/svg/src/tree.rs | 120 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 111 insertions(+), 11 deletions(-) diff --git a/crates/svg/src/svgz.rs b/crates/svg/src/svgz.rs index 1f3ca90ec..de5232612 100644 --- a/crates/svg/src/svgz.rs +++ b/crates/svg/src/svgz.rs @@ -21,7 +21,7 @@ pub enum Error { Corrupt(String), /// The gzip stream holds gzip data again. A reader that unpacks what it is handed would /// unpack that second layer with no limit, so it is refused here. - #[error("the compressed SVG holds another compressed file, not a document ({0} decoded bytes)")] + #[error("the compressed SVG holds another compressed file, not UTF-8 SVG text ({0} decoded bytes)")] Nested(usize), /// The text is not valid in its encoding. #[error("the SVG is not UTF-8, UTF-16 or ISO 8859-1")] diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index 742c5c3e1..ed49f8ef0 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -269,8 +269,83 @@ pub fn height(xml: &roxmltree::Document<'_>, most: usize) -> Result>, + invalid_raster: AtomicUsize, + raster_limit: AtomicUsize, + invalid_svg: AtomicUsize, + svg_limit: AtomicUsize, +} + +/// What resolving one drawing's embedded images used or refused. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct ImageUsage { + /// Unique decoded raster pixels reserved before decode. + pub raster_pixels: u64, + /// Unpacked SVG bytes consumed by embedded SVG data URLs. + pub embedded_svg_bytes: usize, + /// Embedded SVG images parsed, including nested images. + pub embedded_svg_count: usize, + /// Deepest embedded SVG chain. + pub embedded_svg_depth: usize, + /// Raster image data that could not be probed or decoded by the usvg resolver. + pub invalid_raster_images: usize, + /// Raster images refused by their per-image or aggregate pixel limit. + pub raster_limit_images: usize, + /// Embedded SVG data that was invalid. + pub invalid_svg_images: usize, + /// Embedded SVG data refused by byte or nesting limits. + pub svg_limit_images: usize, +} + +/// A data-URL resolver and the accounting for the one drawing it serves. +/// +/// Keep this value while parsing, then read [`usage`](Self::usage). Linked files are never read. +pub struct ImageResolver { + limits: Limits, + used: Arc, +} + +impl ImageResolver { + /// Create one resolver budget for one drawing import. + pub fn new(limits: Limits) -> Self { + ImageResolver { + limits, + used: Arc::new(Used { + bytes: AtomicUsize::new(0), + depth: AtomicUsize::new(0), + svg_count: AtomicUsize::new(0), + svg_depth: AtomicUsize::new(0), + raster_pixels: AtomicU64::new(0), + raster_data: Mutex::new(HashSet::new()), + invalid_raster: AtomicUsize::new(0), + raster_limit: AtomicUsize::new(0), + invalid_svg: AtomicUsize::new(0), + svg_limit: AtomicUsize::new(0), + }), + } + } + + /// Resolver to put in `usvg::Options::image_href_resolver`. + pub fn resolver(&self) -> usvg::ImageHrefResolver<'static> { + image_resolver_for(self.limits, Arc::clone(&self.used)) + } + + /// Snapshot accounting after parsing has finished. + pub fn usage(&self) -> ImageUsage { + ImageUsage { + raster_pixels: self.used.raster_pixels.load(Ordering::Relaxed), + embedded_svg_bytes: self.used.bytes.load(Ordering::Relaxed), + embedded_svg_count: self.used.svg_count.load(Ordering::Relaxed), + embedded_svg_depth: self.used.svg_depth.load(Ordering::Relaxed), + invalid_raster_images: self.used.invalid_raster.load(Ordering::Relaxed), + raster_limit_images: self.used.raster_limit.load(Ordering::Relaxed), + invalid_svg_images: self.used.invalid_svg.load(Ordering::Relaxed), + svg_limit_images: self.used.svg_limit.load(Ordering::Relaxed), + } + } } fn raster_format(data: &[u8]) -> Option { @@ -284,15 +359,20 @@ fn raster_format(data: &[u8]) -> Option { } fn raster_image(used: &Used, limits: &Limits, data: Arc>, format: craft_image_codecs::Format) -> Option { - let (width, height) = craft_image_codecs::probe_allocation_size(&data).ok()?; + let Ok((width, height)) = craft_image_codecs::probe_allocation_size(&data) else { + used.invalid_raster.fetch_add(1, Ordering::Relaxed); + return None; + }; if !matches!( format, craft_image_codecs::Format::Png | craft_image_codecs::Format::Jpeg | craft_image_codecs::Format::Gif | craft_image_codecs::Format::WebP ) { + used.invalid_raster.fetch_add(1, Ordering::Relaxed); return None; } let pixels = u64::from(width).checked_mul(u64::from(height))?; if pixels == 0 || pixels > limits.embedded_raster_pixels { + used.raster_limit.fetch_add(1, Ordering::Relaxed); return None; } let pointer = Arc::as_ptr(&data) as usize; @@ -303,6 +383,7 @@ fn raster_image(used: &Used, limits: &Limits, data: Arc>, format: craft_ let next = current.checked_add(pixels)?; if next > limits.embedded_raster_pixels { seen.remove(&pointer); + used.raster_limit.fetch_add(1, Ordering::Relaxed); return None; } match used.raster_pixels.compare_exchange_weak(current, next, Ordering::Relaxed, Ordering::Relaxed) { @@ -324,6 +405,7 @@ fn raster_image(used: &Used, limits: &Limits, data: Arc>, format: craft_ /// An embedded SVG document, if the budgets allow it. fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'_>) -> Option { if used.depth.load(Ordering::Relaxed) >= limits.embedded_depth { + used.svg_limit.fetch_add(1, Ordering::Relaxed); return None; } let left = limits.embedded_bytes.saturating_sub(used.bytes.load(Ordering::Relaxed)); @@ -333,15 +415,32 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' // is left, so the next image can't repeat it. Err(_) => { used.bytes.fetch_add(left, Ordering::Relaxed); + used.svg_limit.fetch_add(1, Ordering::Relaxed); return None; } }; used.bytes.fetch_add(text.len(), Ordering::Relaxed); - let xml = checked(&text, limits).ok()?; - used.depth.fetch_add(1, Ordering::Relaxed); + let xml = match checked(&text, limits) { + Ok(xml) => xml, + Err(_) => { + used.invalid_svg.fetch_add(1, Ordering::Relaxed); + return None; + } + }; + let depth = used.depth.fetch_add(1, Ordering::Relaxed).saturating_add(1); + used.svg_depth.fetch_max(depth, Ordering::Relaxed); let tree = usvg::Tree::from_xmltree(&xml, options); used.depth.fetch_sub(1, Ordering::Relaxed); - tree.ok().map(usvg::ImageKind::SVG) + match tree { + Ok(tree) => { + used.svg_count.fetch_add(1, Ordering::Relaxed); + Some(usvg::ImageKind::SVG(tree)) + } + Err(_) => { + used.invalid_svg.fetch_add(1, Ordering::Relaxed); + None + } + } } /// An image resolver for `usvg::Options::image_href_resolver` that reads `` data URLs @@ -350,12 +449,10 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' /// [`Limits::embedded_depth`] inside one another. An image past a limit is left out. Linked /// files are never read. One resolver is one import's budget: make a new one for each [`parse`]. pub fn image_resolver(limits: Limits) -> usvg::ImageHrefResolver<'static> { - let used = Arc::new(Used { - bytes: AtomicUsize::new(0), - depth: AtomicUsize::new(0), - raster_pixels: AtomicU64::new(0), - raster_data: Mutex::new(HashSet::new()), - }); + ImageResolver::new(limits).resolver() +} + +fn image_resolver_for(limits: Limits, used: Arc) -> usvg::ImageHrefResolver<'static> { usvg::ImageHrefResolver { resolve_data: Box::new(move |mime, data, options| { let detected = raster_format(&data); @@ -370,6 +467,9 @@ pub fn image_resolver(limits: Limits) -> usvg::ImageHrefResolver<'static> { if let Some(format) = format { return raster_image(&used, &limits, data, format); } + if matches!(mime, "image/jpg" | "image/jpeg" | "image/png" | "image/gif" | "image/webp") { + used.invalid_raster.fetch_add(1, Ordering::Relaxed); + } match mime { "image/svg+xml" => embedded(&used, &limits, &data, options), "text/plain" => embedded(&used, &limits, &data, options), From 64f0cd410dc9e5ea4755c9a903dbc33af50cf09e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:10:29 -0500 Subject: [PATCH 126/520] Share XML depth across embedded SVGs --- crates/svg/src/tree.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index ed49f8ef0..ec0c594bd 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -269,6 +269,7 @@ pub fn height(xml: &roxmltree::Document<'_>, most: usize) -> Result ImageUsage { ImageUsage { @@ -427,9 +435,20 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' return None; } }; + let Some(xml_depth) = height(&xml, limits.depth).ok().flatten() else { + used.svg_limit.fetch_add(1, Ordering::Relaxed); + return None; + }; + let total_depth = used.xml_depth.load(Ordering::Relaxed).checked_add(xml_depth); + let Some(total_depth) = total_depth.filter(|depth| *depth <= limits.depth) else { + used.svg_limit.fetch_add(1, Ordering::Relaxed); + return None; + }; + let previous_depth = used.xml_depth.swap(total_depth, Ordering::Relaxed); let depth = used.depth.fetch_add(1, Ordering::Relaxed).saturating_add(1); used.svg_depth.fetch_max(depth, Ordering::Relaxed); let tree = usvg::Tree::from_xmltree(&xml, options); + used.xml_depth.store(previous_depth, Ordering::Relaxed); used.depth.fetch_sub(1, Ordering::Relaxed); match tree { Ok(tree) => { From 81b88332b59f16db5e64e59e4116cdde1c6ba0f7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:34:44 -0500 Subject: [PATCH 127/520] Share library and recent files JSON models --- crates/stencilcraft-document/src/lib.rs | 2 + crates/stencilcraft-document/src/metadata.rs | 85 ++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 crates/stencilcraft-document/src/metadata.rs diff --git a/crates/stencilcraft-document/src/lib.rs b/crates/stencilcraft-document/src/lib.rs index bca33fb8d..6e04be5b5 100644 --- a/crates/stencilcraft-document/src/lib.rs +++ b/crates/stencilcraft-document/src/lib.rs @@ -6,6 +6,7 @@ pub mod canvas; pub mod geom; +pub mod metadata; use crate::geom::{GeomError, bounds_of_shape, check_finite_mm}; pub use canvas::{Grid, Group, GroupChild, Guide, GuideAxis, Metadata, Orientation, Page, PagePreset, PageSetup, SnapSettings}; @@ -13,6 +14,7 @@ use kurbo::BezPath; use serde::{Deserialize, Serialize}; pub mod production; +pub use metadata::{LibraryIndex, LibraryTrashEntry, MAX_RECENT_FILES, RecentFiles}; pub use production::{CombineOp, GroupKind, Operation, SendAssign, ShapeProduction, SourceGroup}; /// Native format identity and supported version, defined by the shared wire-format crate. diff --git a/crates/stencilcraft-document/src/metadata.rs b/crates/stencilcraft-document/src/metadata.rs new file mode 100644 index 000000000..04fb24d83 --- /dev/null +++ b/crates/stencilcraft-document/src/metadata.rs @@ -0,0 +1,85 @@ +//! Small JSON records used by StencilCraft's local library and recent-files list. +//! +//! These records are separate from a document, but their on-disk field names, defaults and +//! JSON encoding are part of StencilCraft's supported persisted data. + +use std::path::Path; + +use serde::{Deserialize, Serialize}; + +use crate::DocError; + +/// One item in the local library recycle bin. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct LibraryTrashEntry { + /// File name inside the bin. + pub name: String, + /// Where it was, for restoring. + pub original: String, +} + +/// The `.library.json` record stored at the root of a StencilCraft library. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct LibraryIndex { + #[serde(default)] + pub favourites: Vec, + #[serde(default)] + pub trash: Vec, +} + +impl LibraryIndex { + /// Parse the library index, applying defaults for fields absent in older indexes. + pub fn from_json(json: &str) -> Result { + serde_json::from_str(json) + } + + /// Serialize the library index using the canonical pretty JSON representation. + pub fn to_json_pretty(&self) -> Result { + serde_json::to_string_pretty(self) + } +} + +/// Maximum number of paths retained in the recent-files record. +pub const MAX_RECENT_FILES: usize = 10; + +/// Most recently used document paths, newest first, without duplicates. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct RecentFiles { + #[serde(default)] + pub paths: Vec, +} + +impl RecentFiles { + /// Parse recent files from their persisted JSON representation. + pub fn from_json(json: &str) -> Result { + serde_json::from_str(json) + } + + /// Serialize recent files using the canonical pretty JSON representation. + pub fn to_json_pretty(&self) -> Result { + serde_json::to_string_pretty(self) + } + + /// Moves `path` to the front, keeping at most [`MAX_RECENT_FILES`]. + pub fn push(&mut self, path: &str) { + self.paths.retain(|p| p != path); + self.paths.insert(0, path.to_string()); + self.paths.truncate(MAX_RECENT_FILES); + } + + pub fn remove(&mut self, path: &str) { + self.paths.retain(|p| p != path); + } + + /// Reads the list; a missing or damaged file gives an empty list. + pub fn load(file: &Path) -> Self { + let mut list = std::fs::read_to_string(file).ok().and_then(|text| Self::from_json(&text).ok()).unwrap_or_default(); + list.paths.truncate(MAX_RECENT_FILES); + list + } + + pub fn save(&self, file: &Path) -> Result<(), DocError> { + let json = self.to_json_pretty().map_err(|e| DocError::Structure { path: file.display().to_string(), message: e.to_string() })?; + craft_atomic::atomic_write(file, json.as_bytes()).map_err(|source| DocError::Io { path: file.display().to_string(), source }) + } +} From 3fea43cf8337e9e094d2b3c13b028ed8c4e4dd23 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:36:50 -0500 Subject: [PATCH 128/520] Match Apple HEIF chroma reconstruction --- crates/image-codecs/src/codecs/heif.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/codecs/heif.rs b/crates/image-codecs/src/codecs/heif.rs index 775891ed8..c3fb33508 100644 --- a/crates/image-codecs/src/codecs/heif.rs +++ b/crates/image-codecs/src/codecs/heif.rs @@ -55,7 +55,14 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> R if !keep_orientation && (info.coded_width, info.coded_height) != (w, h) { limits.check(info.coded_width, info.coded_height, layout, sample)?; } - let options = craft_heif::Options { max_pixels: limits.max_pixels, apply_transforms: !keep_orientation, ..Default::default() }; + let options = craft_heif::Options { + max_pixels: limits.max_pixels, + apply_transforms: !keep_orientation, + // PhotoCraft's HEIF fixtures and export expectations match Apple's decoder. In + // particular, its left-sited bilinear chroma reconstruction differs at sharp edges + // from the libheif default, even though both produce the same RGB strip centres. + chroma: craft_heif::Chroma::Apple, + }; let decoded = craft_heif::decode(bytes, &options).map_err(err)?; // The decoder re-reads the container; trust its output's shape, not the probe's. let layout = if decoded.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; From 34f2d55dc0ba5278f7499170b368b2ce55265f68 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:36:50 -0500 Subject: [PATCH 129/520] Match Apple HEIF chroma reconstruction --- crates/image-codecs/src/codecs/heif.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/codecs/heif.rs b/crates/image-codecs/src/codecs/heif.rs index 775891ed8..c3fb33508 100644 --- a/crates/image-codecs/src/codecs/heif.rs +++ b/crates/image-codecs/src/codecs/heif.rs @@ -55,7 +55,14 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> R if !keep_orientation && (info.coded_width, info.coded_height) != (w, h) { limits.check(info.coded_width, info.coded_height, layout, sample)?; } - let options = craft_heif::Options { max_pixels: limits.max_pixels, apply_transforms: !keep_orientation, ..Default::default() }; + let options = craft_heif::Options { + max_pixels: limits.max_pixels, + apply_transforms: !keep_orientation, + // PhotoCraft's HEIF fixtures and export expectations match Apple's decoder. In + // particular, its left-sited bilinear chroma reconstruction differs at sharp edges + // from the libheif default, even though both produce the same RGB strip centres. + chroma: craft_heif::Chroma::Apple, + }; let decoded = craft_heif::decode(bytes, &options).map_err(err)?; // The decoder re-reads the container; trust its output's shape, not the probe's. let layout = if decoded.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; From 65a8069d32e3717d0c736077c7375e49e504d0db Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:42:29 -0500 Subject: [PATCH 130/520] Refuse SVG render targets over the memory budget --- crates/svg/src/tree/draw.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/svg/src/tree/draw.rs b/crates/svg/src/tree/draw.rs index d472fdc7d..2b5c40ca8 100644 --- a/crates/svg/src/tree/draw.rs +++ b/crates/svg/src/tree/draw.rs @@ -166,6 +166,12 @@ pub fn check(nodes: &[usvg::Node], ts: Transform, width: u32, height: u32, held: #[cfg(feature = "tree-render")] pub fn render(tree: &usvg::Tree, ts: Transform, width: u32, height: u32, held: u64, budget: u64) -> Result { check_filter_cost(tree, ts, 2_000_000_000, 10_000_000, 200)?; + // Even an empty tree allocates the target. `check` only walks buffers added by nodes, so + // enforce the target allocation here before `Pixmap::new` (whose OOM handler aborts). + let target = u64::from(width).saturating_mul(u64::from(height)).saturating_mul(4).saturating_add(held); + if target > budget { + return Err(memory("the target pixmap", target as f64, budget as f64)); + } check(tree.root().children(), ts, width, height, held, budget)?; let mut pixmap = tiny_skia::Pixmap::new(width, height).ok_or_else(|| Refusal { complex: false, From 6417d6b90adbefb89b044d2a41e94222e7decbaf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:49:49 -0500 Subject: [PATCH 131/520] Preserve shared format boundary behavior --- crates/affinity/src/model/read.rs | 6 ++- crates/image-codecs/src/allocation.rs | 66 +++++++++++++++++++++++---- crates/raw/src/cr3.rs | 7 +++ crates/raw/src/jxl.rs | 4 ++ crates/raw/src/lib.rs | 10 +++- crates/xml/src/lib.rs | 2 +- 6 files changed, 83 insertions(+), 12 deletions(-) diff --git a/crates/affinity/src/model/read.rs b/crates/affinity/src/model/read.rs index c0cf58e77..14b17ad99 100644 --- a/crates/affinity/src/model/read.rs +++ b/crates/affinity/src/model/read.rs @@ -679,7 +679,11 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { } let class = s.class(id).unwrap_or(Tag::of(b"ShpN")); let (path, even_odd) = crate::geometry::outline(self, id, class, world).unwrap_or_else(|| { - self.warn("an artboard whose outline could not be read (clipped to its rectangle)"); + if class == Tag::of(b"PCrv") { + self.warn("a converted artboard outline could not be read (clipped to its rectangle)"); + } else { + self.warn("an artboard whose outline could not be read (clipped to its rectangle)"); + } (Path { subpaths: vec![crate::shapes::rectangle(b.x0, b.y0, b.x1, b.y1)] }, false) }); let (background, strokes) = paint::node_paint(self, id, world); diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index 5425a0c24..a82f43f8e 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -1,10 +1,7 @@ //! Predecode bounds for raster containers whose frames may exceed their declared canvas. -use crate::codecs::webp; use crate::error::CodecError; use crate::format::Format; -use crate::image::{ChannelLayout, SampleType}; -use crate::options::Limits; /// The largest width and height any decoded frame can declare, without allocating pixel data. /// Animated GIF and WebP frames are inspected individually, including frames larger than the @@ -12,12 +9,10 @@ use crate::options::Limits; pub fn probe_allocation_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { let format = crate::detect(bytes).ok_or(CodecError::UnknownFormat)?; let (width, height) = match format { + Format::Png => png_bounds(bytes)?, + Format::Jpeg => jpeg_bounds(bytes)?, Format::Gif => gif_bounds(bytes)?, - Format::WebP => { - let (width, height, _) = webp::probe(bytes, &Limits::default())?; - let (frame_width, frame_height) = webp_bounds(bytes)?; - (width.max(frame_width), height.max(frame_height)) - } + Format::WebP => webp_bounds(bytes)?, _ => { if let Some(info) = crate::inspect_raster(bytes)? { (info.width, info.height) @@ -26,7 +21,9 @@ pub fn probe_allocation_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { } } }; - Limits::default().check(width, height, ChannelLayout::Rgba, SampleType::U8)?; + // This is an allocation-size probe, not a decode policy. Callers need the coded extents even + // when they exceed this crate's default decode limits so they can report/refuse against their + // own budgets before passing the bytes to a decoder. Ok((width, height)) } @@ -34,6 +31,57 @@ fn malformed(format: Format, message: &'static str) -> CodecError { CodecError::malformed(format, message) } +fn png_bounds(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let bad = || malformed(Format::Png, "truncated or malformed PNG header"); + if bytes.get(..8) != Some(b"\x89PNG\r\n\x1a\n") || bytes.get(8..16) != Some(b"\0\0\0\rIHDR") { + return Err(bad()); + } + let width = u32::from_be_bytes(bytes.get(16..20).ok_or_else(bad)?.try_into().map_err(|_| bad())?); + let height = u32::from_be_bytes(bytes.get(20..24).ok_or_else(bad)?.try_into().map_err(|_| bad())?); + if width == 0 || height == 0 { + return Err(bad()); + } + Ok((width, height)) +} + +fn jpeg_bounds(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let bad = || malformed(Format::Jpeg, "truncated or malformed JPEG frame header"); + if bytes.get(..2) != Some(&[0xff, 0xd8]) { + return Err(bad()); + } + let mut at = 2usize; + while at < bytes.len() { + while bytes.get(at) == Some(&0xff) { + at += 1; + } + let marker = *bytes.get(at).ok_or_else(bad)?; + at += 1; + if matches!(marker, 0xd8 | 0xd9 | 0x01 | 0xd0..=0xd7) { + continue; + } + let length = + usize::from(u16::from_be_bytes(bytes.get(at..at.checked_add(2).ok_or_else(bad)?).ok_or_else(bad)?.try_into().map_err(|_| bad())?)); + if length < 2 { + return Err(bad()); + } + let end = at.checked_add(length).ok_or_else(bad)?; + let segment = bytes.get(at..end).ok_or_else(bad)?; + if matches!(marker, 0xc0..=0xc3 | 0xc5..=0xc7 | 0xc9..=0xcb | 0xcd..=0xcf) { + if segment.len() < 7 { + return Err(bad()); + } + let height = u32::from(u16::from_be_bytes([segment[3], segment[4]])); + let width = u32::from(u16::from_be_bytes([segment[5], segment[6]])); + if width == 0 || height == 0 { + return Err(bad()); + } + return Ok((width, height)); + } + at = end; + } + Err(bad()) +} + fn u16_le(bytes: &[u8], at: usize) -> Option { Some(u16::from_le_bytes(bytes.get(at..at.checked_add(2)?)?.try_into().ok()?)) } diff --git a/crates/raw/src/cr3.rs b/crates/raw/src/cr3.rs index 60fe292c8..956446c6e 100644 --- a/crates/raw/src/cr3.rs +++ b/crates/raw/src/cr3.rs @@ -184,6 +184,13 @@ fn black_at_active(levels: [f32; 4], pattern: u8, active: Rect) -> BlackLevel { BlackLevel { repeat_rows: 2, repeat_cols: 2, values, delta_h: vec![], delta_v: vec![] } } +/// Orientation metadata usable for embedded-preview fallback even when the raw track is unsupported. +pub(crate) fn orientation(bytes: &[u8]) -> Option { + let container = parse_cr3(bytes)?; + let cmt1 = container.cmt.first().and_then(|b| *b).and_then(|b| Tiff::parse(b).ok())?; + Some(cmt1.ifds.first()?.u16(craft_tiff::tags::ORIENTATION).unwrap_or(1)) +} + pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result { let container = parse_cr3(bytes).ok_or(RawError::NotRaw)?; let track = main_track(&container.tracks).ok_or_else(|| RawError::Unsupported("CR3 without a Bayer raw track".into()))?; diff --git a/crates/raw/src/jxl.rs b/crates/raw/src/jxl.rs index 128f4eeea..e0458fb68 100644 --- a/crates/raw/src/jxl.rs +++ b/crates/raw/src/jxl.rs @@ -238,6 +238,10 @@ fn open(src: &[u8], cw: usize, ch: usize, vw: usize, vh: usize, cpp: usize, bits } let max = match header.metadata.bit_depth { BitDepth::IntegerSample { bits_per_sample } if !float && (1..=16).contains(&bits_per_sample) => ((1u32 << bits_per_sample) - 1) as f32, + // Some DNG writers label LinearRaw samples as floating point while storing a lossless + // integer JPEG XL codestream. JXL exposes decoded pixels as normalized f32 in either + // case, so retain them as floats instead of rejecting the otherwise valid IFD/tile pair. + BitDepth::IntegerSample { bits_per_sample: 1..=16 } if float => 0.0, BitDepth::FloatSample { .. } if float => 0.0, d => return Err(RawError::Corrupt(format!("JPEG XL tile sample type {d:?} does not match the IFD"))), }; diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index c9c072122..dbcf6f953 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -638,7 +638,15 @@ pub fn probe_info(bytes: &[u8]) -> Result { /// Orientation of a supported raw, read through the same format parser as [`probe_info`]. pub fn orientation(bytes: &[u8]) -> Option { - probe_info(bytes).ok().map(|info| info.orientation.to_exif()) + if let Ok(info) = probe_info(bytes) { + return Some(info.orientation.to_exif()); + } + // A damaged/unsupported pixel IFD must not hide usable orientation metadata from preview + // fallback. CR3 keeps IFD0 in its CMT1 box; TIFF-based raws keep it at the file start. + if probe(bytes) == Some(RawFormat::Cr3) { + return cr3::orientation(bytes); + } + craft_tiff::Tiff::parse(bytes).ok()?.ifds.first()?.u16(craft_tiff::tags::ORIENTATION).or(Some(1)) } /// [`probe_info`] within `limits`: a file that declares more than they allow fails as [`decode_with`] would. diff --git a/crates/xml/src/lib.rs b/crates/xml/src/lib.rs index b953690bf..87057b387 100644 --- a/crates/xml/src/lib.rs +++ b/crates/xml/src/lib.rs @@ -44,7 +44,7 @@ pub enum Error { #[error("the XML is larger than {0} bytes")] TooLarge(usize), /// Elements nest deeper than [`Limits::max_depth`]. - #[error("XML elements nest deeper than {0} levels")] + #[error("XML elements are nested deeper than {0} levels")] TooDeep(usize), /// The tree would hold more than [`Limits::max_nodes`] nodes. #[error("the XML holds more than {0} nodes")] From 6de1ffc9d784d47400fdb2983947735d362ad498 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:53:57 -0500 Subject: [PATCH 132/520] Add shared format capability catalog --- Cargo.lock | 11 +- crates/file-formats/Cargo.toml | 24 ++ crates/file-formats/src/lib.rs | 404 +++++++++++++++++++++++++++++---- 3 files changed, 391 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a8abed4c..a8d8480b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1572,6 +1572,7 @@ version = "0.1.0" dependencies = [ "craft-eps", "craft-idml", + "craft-image-codecs", "craft-svg", "craft-zip", "serde", @@ -4941,17 +4942,17 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" -[[package]] -name = "indd" -version = "0.1.0" -source = "git+https://github.com/storytold/indd-utils?rev=7f3060e5bce78d21ea70e0d5eacb96edce15dfbe#7f3060e5bce78d21ea70e0d5eacb96edce15dfbe" - [[package]] name = "imgref" version = "1.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e44b0a4eaa4c82f441d50a963f2d5f05a787240aeee097597033e72accfd22f" +[[package]] +name = "indd" +version = "0.1.0" +source = "git+https://github.com/storytold/indd-utils?rev=7f3060e5bce78d21ea70e0d5eacb96edce15dfbe#7f3060e5bce78d21ea70e0d5eacb96edce15dfbe" + [[package]] name = "indexmap" version = "2.14.2" diff --git a/crates/file-formats/Cargo.toml b/crates/file-formats/Cargo.toml index a1a605918..e7321f502 100644 --- a/crates/file-formats/Cargo.toml +++ b/crates/file-formats/Cargo.toml @@ -9,11 +9,35 @@ rust-version.workspace = true [dependencies] craft-eps = { workspace = true } +craft-image-codecs = { workspace = true, optional = true } craft-idml = { workspace = true } craft-svg = { workspace = true } craft-zip = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } +[features] +default = [] +# These flags report which shared parser/codec families a client builds. The application still +# selects the corresponding shared crates in its dependency graph. +raster-codecs = ["dep:craft-image-codecs"] +heif = ["raster-codecs", "craft-image-codecs/heif"] +avif = ["raster-codecs", "craft-image-codecs/avif"] +pdf = [] +svg = [] +ora = [] +kra = [] +xcf = [] +pdn = [] +raw = [] +psd = [] +affinity = [] +pcraft = [] +ase = [] +office = [] +designcraft = [] +stencilcraft = [] +plotter = [] + [lints] workspace = true diff --git a/crates/file-formats/src/lib.rs b/crates/file-formats/src/lib.rs index 7859bf539..d10c22b46 100644 --- a/crates/file-formats/src/lib.rs +++ b/crates/file-formats/src/lib.rs @@ -23,6 +23,22 @@ pub enum Format { Tiff, WebP, Heif, + Tga, + Ico, + Pnm, + Pcx, + Xpm, + Dds, + Qoi, + OpenExr, + Hdr, + Avif, + Fits, + Dpx, + Jxl, + Cineon, + Dicom, + Iff, Ase, Psd, Affinity, @@ -54,6 +70,349 @@ pub enum Format { StencilCraft, } +/// Availability of one operation in the current client's shared-format build. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Availability { + /// The shared reader or writer is included in this build. + Available, + /// This format is known, but the corresponding shared operation is not implemented. + Unsupported, + /// The operation exists behind a Cargo feature that this client did not enable. + BuildDisabled, +} + +/// Shared reader/writer capability and sequence behavior for a known format. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Capabilities { + pub read: Availability, + pub write: Availability, + /// Input may contain multiple frames/pages that the shared reader can expose individually. + pub multiple_frames: bool, + /// This format can be opened as an ordered sequence of files by an application adapter. + pub file_sequence: bool, +} + +const fn capability(feature: bool, read: bool, write: bool, multiple_frames: bool, file_sequence: bool) -> Capabilities { + Capabilities { read: availability(feature, read), write: availability(feature, write), multiple_frames, file_sequence } +} + +const fn availability(feature: bool, supported: bool) -> Availability { + if !supported { + Availability::Unsupported + } else if feature { + Availability::Available + } else { + Availability::BuildDisabled + } +} + +impl Format { + /// Every known format, including formats whose reader or writer is not enabled in this build. + pub const ALL: [Format; 56] = [ + Self::Pdf, + Self::Eps, + Self::PostScript, + Self::Illustrator, + Self::Dxf, + Self::Svg, + Self::Svgz, + Self::Png, + Self::Jpeg, + Self::Bmp, + Self::Gif, + Self::Tiff, + Self::WebP, + Self::Heif, + Self::Tga, + Self::Ico, + Self::Pnm, + Self::Pcx, + Self::Xpm, + Self::Dds, + Self::Qoi, + Self::OpenExr, + Self::Hdr, + Self::Avif, + Self::Fits, + Self::Dpx, + Self::Jxl, + Self::Cineon, + Self::Dicom, + Self::Iff, + Self::Ase, + Self::Psd, + Self::Affinity, + Self::Ora, + Self::Kra, + Self::Xcf, + Self::Pdn, + Self::Raw, + Self::Pcraft, + Self::Idml, + Self::DesignCraft, + Self::DesignCraftJson, + Self::Indd, + Self::Docx, + Self::LegacyDoc, + Self::Rtf, + Self::Xlsx, + Self::TaggedText, + Self::Pptx, + Self::PlainText, + Self::Csv, + Self::Tsv, + Self::LegacyExcel, + Self::Studio, + Self::StencilCraft, + Self::Gcode, + ]; + + /// Known extensions in lowercase form, in preferred-output order. + pub fn extensions(self) -> &'static [&'static str] { + match self { + Self::Pdf => &["pdf"], + Self::Eps => &["eps"], + Self::PostScript => &["ps"], + Self::Illustrator => &["ai"], + Self::Dxf => &["dxf"], + Self::Svg => &["svg"], + Self::Svgz => &["svgz"], + Self::Png => &["png", "apng"], + Self::Jpeg => &["jpg", "jpeg", "jpe", "jfif"], + Self::Bmp => &["bmp", "dib"], + Self::Gif => &["gif"], + Self::Tiff => &["tif", "tiff"], + Self::WebP => &["webp"], + Self::Heif => &["heic", "heif", "hif"], + Self::Tga => &["tga", "icb", "vda", "vst"], + Self::Ico => &["ico"], + Self::Pnm => &["pnm", "pbm", "pgm", "ppm", "pam", "pfm"], + Self::Pcx => &["pcx"], + Self::Xpm => &["xpm"], + Self::Dds => &["dds"], + Self::Qoi => &["qoi"], + Self::OpenExr => &["exr"], + Self::Hdr => &["hdr"], + Self::Avif => &["avif"], + Self::Fits => &["fits", "fit", "fts"], + Self::Dpx => &["dpx"], + Self::Jxl => &["jxl"], + Self::Cineon => &["cin"], + Self::Dicom => &["dcm", "dicom", "dc3"], + Self::Iff => &["iff", "lbm", "ilbm"], + Self::Ase => &["ase"], + Self::Psd => &["psd", "psb"], + Self::Affinity => &["afdesign", "afphoto", "afpub"], + Self::Ora => &["ora"], + Self::Kra => &["kra"], + Self::Xcf => &["xcf"], + Self::Pdn => &["pdn"], + Self::Raw => &[ + "dng", "3fr", "ari", "arw", "bay", "cap", "cr2", "cr3", "dcs", "dcr", "drf", "eip", "erf", "fff", "gpr", "iiq", "k25", "kdc", "mdc", + "mef", "mos", "mrw", "nef", "nrw", "obm", "orf", "pef", "ptx", "pxn", "r3d", "raf", "raw", "rwl", "rw2", "rwz", "sr2", "srf", "sti", + "x3f", + ], + Self::Pcraft => &["pcraft"], + Self::Idml => &["idml"], + Self::DesignCraft => &["designcraft", "dcdoc"], + Self::DesignCraftJson => &[], + Self::Indd => &["indd", "indt"], + Self::Docx => &["docx"], + Self::LegacyDoc => &["doc"], + Self::Rtf => &["rtf"], + Self::Xlsx => &["xlsx"], + Self::TaggedText => &[], + Self::Pptx => &["pptx"], + Self::PlainText => &["txt", "text", "md"], + Self::Csv => &["csv"], + Self::Tsv => &["tsv", "tab"], + Self::LegacyExcel => &["xls", "xlsm"], + Self::Studio => &["studio", "studio3"], + Self::StencilCraft => &["stencilcraft"], + Self::Gcode => &["gcode", "nc"], + } + } + + /// Preferred extension for creating a new file of this type, if one is defined. + pub fn preferred_extension(self) -> Option<&'static str> { + self.extensions().first().copied() + } + + /// Registered MIME type, where the format has a stable MIME registration. + pub fn mime_type(self) -> Option<&'static str> { + Some(match self { + Self::Pdf => "application/pdf", + Self::Eps => "application/postscript", + Self::PostScript => "application/postscript", + Self::Illustrator => "application/illustrator", + Self::Dxf => "image/vnd.dxf", + Self::Svg | Self::Svgz => "image/svg+xml", + Self::Png => "image/png", + Self::Jpeg => "image/jpeg", + Self::Bmp => "image/bmp", + Self::Gif => "image/gif", + Self::Tiff => "image/tiff", + Self::WebP => "image/webp", + Self::Heif => "image/heif", + Self::Tga => "image/x-tga", + Self::Ico => "image/vnd.microsoft.icon", + Self::Pnm => "image/x-portable-anymap", + Self::Pcx => "image/vnd.zbrush.pcx", + Self::Xpm => "image/x-xpixmap", + Self::Dds => "image/vnd-ms.dds", + Self::Qoi => "image/qoi", + Self::OpenExr => "image/x-exr", + Self::Hdr => "image/vnd.radiance", + Self::Avif => "image/avif", + Self::Fits => "image/fits", + Self::Dpx => "image/dpx", + Self::Jxl => "image/jxl", + Self::Cineon => "image/x-cineon", + Self::Dicom => "application/dicom", + Self::Iff => "image/ilbm", + Self::Ase => "application/x-adobe-swatch-exchange", + Self::Psd => "image/vnd.adobe.photoshop", + Self::Affinity => "application/x-affinity", + Self::Ora => "image/openraster", + Self::Kra => "application/x-krita", + Self::Xcf => "image/x-xcf", + Self::Pdn => "application/x-paint.net", + Self::Raw => "image/x-camera-raw", + Self::Pcraft => "application/vnd.photocraft+zip", + Self::Idml => "application/vnd.adobe.indesign-idml-package", + Self::DesignCraft => "application/vnd.designcraft+zip", + Self::DesignCraftJson => "application/json", + Self::Indd => "application/x-indesign", + Self::Docx => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + Self::LegacyDoc => "application/msword", + Self::Rtf => "application/rtf", + Self::Xlsx => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + Self::TaggedText => "text/plain", + Self::Pptx => "application/vnd.openxmlformats-officedocument.presentationml.presentation", + Self::PlainText => "text/plain", + Self::Csv => "text/csv", + Self::Tsv => "text/tab-separated-values", + Self::LegacyExcel => "application/vnd.ms-excel", + Self::Studio => "application/x-silhouette-studio", + Self::StencilCraft => "application/vnd.stencilcraft+json", + Self::Gcode => "text/x.gcode", + }) + } + + /// The corresponding flat-raster codec, when this format is handled by `craft-image-codecs`. + #[cfg(feature = "raster-codecs")] + pub fn raster_codec(self) -> Option { + use craft_image_codecs::Format as Codec; + Some(match self { + Self::Png => Codec::Png, + Self::Jpeg => Codec::Jpeg, + Self::Bmp => Codec::Bmp, + Self::Gif => Codec::Gif, + Self::Tiff => Codec::Tiff, + Self::WebP => Codec::WebP, + Self::Heif => Codec::Heif, + Self::Tga => Codec::Tga, + Self::Ico => Codec::Ico, + Self::Pnm => Codec::Pnm, + Self::Pcx => Codec::Pcx, + Self::Xpm => Codec::Xpm, + Self::Dds => Codec::Dds, + Self::Qoi => Codec::Qoi, + Self::OpenExr => Codec::OpenExr, + Self::Hdr => Codec::Hdr, + Self::Avif => Codec::Avif, + Self::Fits => Codec::Fits, + Self::Dpx => Codec::Dpx, + Self::Jxl => Codec::Jxl, + Self::Cineon => Codec::Cineon, + Self::Dicom => Codec::Dicom, + Self::Iff => Codec::Iff, + _ => return None, + }) + } + + /// Operations exposed by this build's shared parser/codec features. + pub fn capabilities(self) -> Capabilities { + #[cfg(feature = "raster-codecs")] + if let Some(format) = self.raster_codec() { + let codec = format.caps(); + let read = if codec.read { + Availability::Available + } else if matches!(self, Self::Heif) && !cfg!(feature = "heif") { + Availability::BuildDisabled + } else { + Availability::Unsupported + }; + let write = if codec.write { + Availability::Available + } else if matches!(self, Self::Avif) && !cfg!(feature = "avif") { + Availability::BuildDisabled + } else { + Availability::Unsupported + }; + return Capabilities { read, write, multiple_frames: codec.animation, file_sequence: false }; + } + let raster = cfg!(feature = "raster-codecs"); + match self { + Self::Png | Self::Jpeg | Self::Bmp | Self::Gif | Self::Tiff | Self::WebP => { + capability(raster, true, true, matches!(self, Self::Png | Self::Gif | Self::WebP), false) + } + Self::Heif => Capabilities { + read: availability(cfg!(feature = "heif"), true), + write: Availability::Unsupported, + multiple_frames: true, + file_sequence: false, + }, + Self::Avif => Capabilities { + read: Availability::Unsupported, + write: availability(cfg!(feature = "avif"), true), + multiple_frames: false, + file_sequence: false, + }, + Self::Jxl => capability(raster, false, true, false, false), + Self::Pcx | Self::Xpm | Self::Dicom => capability(raster, true, false, false, false), + Self::Tga + | Self::Ico + | Self::Pnm + | Self::Dds + | Self::Qoi + | Self::OpenExr + | Self::Hdr + | Self::Fits + | Self::Dpx + | Self::Cineon + | Self::Iff => capability(raster, true, true, false, false), + Self::Pdf => capability(cfg!(feature = "pdf"), true, true, false, false), + Self::Eps | Self::PostScript | Self::Illustrator => capability(cfg!(feature = "pdf"), true, false, false, false), + Self::Svg | Self::Svgz => capability(cfg!(feature = "svg"), true, false, false, false), + Self::Dxf | Self::Gcode => capability(cfg!(feature = "plotter"), true, false, false, false), + Self::Ora => capability(cfg!(feature = "ora"), true, true, false, false), + Self::Kra => capability(cfg!(feature = "kra"), true, false, false, false), + Self::Xcf => capability(cfg!(feature = "xcf"), true, false, false, false), + Self::Pdn => capability(cfg!(feature = "pdn"), true, false, false, false), + Self::Raw => capability(cfg!(feature = "raw"), true, false, true, true), + Self::Psd => capability(cfg!(feature = "psd"), true, true, false, false), + Self::Affinity => capability(cfg!(feature = "affinity"), true, false, false, false), + Self::Pcraft => capability(cfg!(feature = "pcraft"), true, true, false, false), + Self::Ase => capability(cfg!(feature = "ase"), true, true, false, false), + Self::Studio => capability(cfg!(feature = "stencilcraft"), true, false, false, false), + Self::Idml | Self::Indd => capability(cfg!(feature = "designcraft"), true, false, false, false), + Self::DesignCraft | Self::DesignCraftJson => capability(cfg!(feature = "designcraft"), true, true, false, false), + Self::StencilCraft => capability(cfg!(feature = "stencilcraft"), true, true, false, false), + Self::Docx + | Self::LegacyDoc + | Self::Rtf + | Self::Xlsx + | Self::Pptx + | Self::TaggedText + | Self::PlainText + | Self::Csv + | Self::Tsv + | Self::LegacyExcel => capability(cfg!(feature = "office"), true, true, false, false), + } + } +} + /// Resolve a name and file bytes to a format. Strong signatures take precedence over /// extension hints. Only format-specific bounded probes and extension tables live here. pub fn detect(name: &str, bytes: &[u8]) -> Option { @@ -183,49 +542,8 @@ fn is_designcraft_json(bytes: &[u8]) -> bool { /// Resolve a file extension to a family, case-insensitively. pub fn from_extension(ext: &str) -> Option { - match ext.trim_start_matches('.').to_ascii_lowercase().as_str() { - "pdf" => Some(Format::Pdf), - "eps" => Some(Format::Eps), - "ps" => Some(Format::PostScript), - "ai" => Some(Format::Illustrator), - "dxf" => Some(Format::Dxf), - "svg" => Some(Format::Svg), - "svgz" => Some(Format::Svgz), - "png" => Some(Format::Png), - "jpg" | "jpeg" => Some(Format::Jpeg), - "bmp" => Some(Format::Bmp), - "gif" => Some(Format::Gif), - "tif" | "tiff" => Some(Format::Tiff), - "webp" => Some(Format::WebP), - "heic" | "heif" => Some(Format::Heif), - "ase" => Some(Format::Ase), - "psd" | "psb" => Some(Format::Psd), - "afdesign" | "afphoto" | "afpub" => Some(Format::Affinity), - "ora" => Some(Format::Ora), - "kra" => Some(Format::Kra), - "xcf" => Some(Format::Xcf), - "pdn" => Some(Format::Pdn), - "dng" | "3fr" | "ari" | "arw" | "bay" | "cap" | "cr2" | "cr3" | "dcs" | "dcr" | "drf" | "eip" | "erf" | "fff" | "gpr" | "iiq" | "k25" - | "kdc" | "mdc" | "mef" | "mos" | "mrw" | "nef" | "nrw" | "obm" | "orf" | "pef" | "ptx" | "pxn" | "r3d" | "raf" | "raw" | "rwl" | "rw2" - | "rwz" | "sr2" | "srf" | "sti" | "x3f" => Some(Format::Raw), - "pcraft" => Some(Format::Pcraft), - "idml" => Some(Format::Idml), - "designcraft" | "dcdoc" => Some(Format::DesignCraft), - "indd" | "indt" => Some(Format::Indd), - "docx" => Some(Format::Docx), - "doc" => Some(Format::LegacyDoc), - "rtf" => Some(Format::Rtf), - "xlsx" => Some(Format::Xlsx), - "pptx" => Some(Format::Pptx), - "txt" | "text" | "md" => Some(Format::PlainText), - "csv" => Some(Format::Csv), - "tsv" | "tab" => Some(Format::Tsv), - "xls" | "xlsm" => Some(Format::LegacyExcel), - "studio" | "studio3" => Some(Format::Studio), - "stencilcraft" => Some(Format::StencilCraft), - "gcode" | "nc" => Some(Format::Gcode), - _ => None, - } + let ext = ext.trim_start_matches('.'); + Format::ALL.into_iter().find(|format| format.extensions().iter().any(|candidate| candidate.eq_ignore_ascii_case(ext))) } const INDD_SIGNATURE: [u8; 16] = [0x06, 0x06, 0xED, 0xF5, 0xD8, 0x1D, 0x46, 0xE5, 0xBD, 0x31, 0xEF, 0xE7, 0xFE, 0x74, 0xB7, 0x1D]; From 65b2a68df0b46d987b44dd2a51f1d706c1a7c372 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:12:58 -0500 Subject: [PATCH 133/520] Add shared DesignCraft native format codec --- Cargo.lock | 11 ++ Cargo.toml | 1 + crates/designcraft-format/Cargo.toml | 18 ++ crates/designcraft-format/src/lib.rs | 247 +++++++++++++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 crates/designcraft-format/Cargo.toml create mode 100644 crates/designcraft-format/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b24081da4..75a575b7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1286,6 +1286,17 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-designcraft-format" +version = "0.1.0" +dependencies = [ + "craft-document-bundle", + "craft-eps", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-dialogs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index b86ae2fa8..7c38b6f92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ craft-dialogs = { path = "crates/dialogs" } craft-doc-binary = { path = "crates/doc-binary" } craft-docx = { path = "crates/docx" } craft-document-bundle = { path = "crates/document-bundle" } +craft-designcraft-format = { path = "crates/designcraft-format" } craft-dxf = { path = "crates/dxf" } craft-drawingml = { path = "crates/drawingml" } craft-eps = { path = "crates/eps" } diff --git a/crates/designcraft-format/Cargo.toml b/crates/designcraft-format/Cargo.toml new file mode 100644 index 000000000..19557b7ae --- /dev/null +++ b/crates/designcraft-format/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "craft-designcraft-format" +description = "Versioned DesignCraft document JSON codec and legacy migrations over craft-document-bundle" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-document-bundle = { workspace = true } +craft-eps = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/designcraft-format/src/lib.rs b/crates/designcraft-format/src/lib.rs new file mode 100644 index 000000000..b497aa4cd --- /dev/null +++ b/crates/designcraft-format/src/lib.rs @@ -0,0 +1,247 @@ +//! Versioned JSON and bundle codec for DesignCraft's native document format. +//! +//! The application owns its document model. This crate owns the on-disk schema version, +//! serialization, legacy JSON upgrades, version inference and the shared ZIP envelope. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +use std::collections::{HashMap, HashSet}; +use std::io::{Read, Seek}; + +use craft_document_bundle::{Asset, Spec}; +use serde::{Serialize, de::DeserializeOwned}; +use serde_json::Value; + +pub const MIME: &str = "application/vnd.designcraft+zip"; +pub const VERSION: u32 = 4; +pub const EXTENSION: &str = "designcraft"; +pub const PREVIEW: &str = "preview.png"; +pub const MAX_PREVIEW_BYTES: u64 = 4 << 20; + +const BUNDLE: Spec<'static> = Spec { + mimetype_entry: "mimetype", + mimetype: MIME, + metadata_entry: "meta.json", + document_entry: "document.json", + preview_entry: PREVIEW, + assets_directory: "assets", + format_name: "designcraft", + legacy_assets_field: Some("assetData"), +}; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("not a DesignCraft document: {0}")] + NotOurs(String), + #[error("document is from a newer DesignCraft (format {0}); please update")] + TooNew(u64), + #[error("{0}")] + Io(String), + #[error("document cannot be saved: {0}")] + Unsaveable(String), +} + +impl From for Error { + fn from(error: craft_document_bundle::Error) -> Self { + match error { + craft_document_bundle::Error::TooNew(version) => Error::TooNew(version), + craft_document_bundle::Error::NotBundle(message) => Error::NotOurs(message), + craft_document_bundle::Error::Read(message) | craft_document_bundle::Error::Write(message) => Error::Io(message), + } + } +} + +/// A decoded native document and the separately stored embedded assets. +#[derive(Clone, Debug, PartialEq)] +pub struct Loaded { + pub document: T, + pub assets: Vec<(String, Vec)>, +} + +/// The reader capability required by `document`, inferred from its neutral JSON schema and +/// embedded assets. Traversal beyond 64 levels conservatively requires version 4. +pub fn required_version(document: &Value, assets: &[Asset<'_>]) -> u32 { + if has_version_four_feature(document) { + return 4; + } + if assets.iter().any(|asset| craft_eps::is_eps(asset.data)) { 3 } else { 2 } +} + +/// Serialize a document and its external assets as a `.designcraft` ZIP bundle. +pub fn save(document: &T, generator: &str, preview: Option<&[u8]>, assets: &[Asset<'_>]) -> Result, Error> { + let value = serde_json::to_value(document).map_err(|error| Error::Io(error.to_string()))?; + let version = required_version(&value, assets); + let json = serde_json::to_vec_pretty(&value).map_err(|error| Error::Io(error.to_string()))?; + craft_document_bundle::write(&json, version, generator, preview, assets, BUNDLE).map_err(Into::into) +} + +/// Read a `.designcraft` ZIP or legacy single-file JSON, applying all versioned schema migrations +/// before deserializing the caller's model. +pub fn load(bytes: &[u8]) -> Result, Error> { + let mut package = craft_document_bundle::read(bytes, VERSION, BUNDLE)?; + upgrade_legacy_json(&mut package.document, package.version); + let document = serde_json::from_value(package.document).map_err(|error| Error::NotOurs(error.to_string()))?; + Ok(Loaded { document, assets: package.assets }) +} + +/// Whether `bytes` are a native DesignCraft ZIP package. +pub fn is_designcraft(bytes: &[u8]) -> bool { + craft_document_bundle::has_document(bytes, BUNDLE) +} + +/// Whether `prefix` begins with this format's stored mimetype entry. +pub fn starts_designcraft(prefix: &[u8]) -> bool { + craft_document_bundle::starts_with_mimetype(prefix, BUNDLE) +} + +/// Read the first-page PNG preview from a ZIP package, with a fixed 4 MiB cap. +pub fn read_preview(reader: R) -> Option> { + craft_document_bundle::read_preview_from(reader, PREVIEW) +} + +fn has_version_four_feature(document: &Value) -> bool { + let mut stack = vec![(document, 0usize)]; + let mut visited = 0usize; + while let Some((value, depth)) = stack.pop() { + if depth > 64 || visited >= 1_000_000 { + return true; + } + visited += 1; + match value { + Value::Object(object) => { + if object.get("layerOptions").is_some_and(|options| !options.is_null()) + || (object.get("pdfLayerOverride").and_then(Value::as_bool) == Some(true) + && object.get("pdfHiddenLayers").and_then(Value::as_array).is_none_or(Vec::is_empty)) + { + return true; + } + stack.extend(object.values().map(|child| (child, depth + 1))); + } + Value::Array(children) => stack.extend(children.iter().map(|child| (child, depth + 1))), + _ => {} + } + } + false +} + +fn upgrade_legacy_json(document: &mut Value, from: u64) { + let mut vertical = HashSet::new(); + vertical_frames(document, &mut vertical, 0); + if let Some(stories) = document.get_mut("stories").and_then(Value::as_object_mut) { + for story in stories.values_mut() { + let first = story.get("frames").and_then(|frames| frames.get(0)).and_then(Value::as_u64); + if let (Some(frame), Some(story)) = (first, story.as_object_mut()) + && vertical.contains(&frame) + && !story.contains_key("vertical") + { + story.insert("vertical".into(), Value::Bool(true)); + } + } + } + if from < 2 { + pin_paragraph_defaults(document); + } +} + +fn vertical_frames(value: &Value, out: &mut HashSet, depth: usize) { + if depth > 256 { + return; + } + match value { + Value::Object(object) => { + if let (Some(id), Some(content)) = (object.get("id").and_then(Value::as_u64), object.get("content")) + && content.get("type").and_then(Value::as_str) == Some("text") + && content.get("options").and_then(|options| options.get("vertical")).and_then(Value::as_bool) == Some(true) + { + out.insert(id); + } + for child in object.values() { + vertical_frames(child, out, depth + 1); + } + } + Value::Array(children) => { + for child in children { + vertical_frames(child, out, depth + 1); + } + } + _ => {} + } +} + +fn pin_paragraph_defaults(document: &mut Value) { + const MAX_STYLE_DEPTH: usize = 64; + let styles = document.pointer("/styles/paragraph").and_then(Value::as_array).cloned().unwrap_or_default(); + let mut by_name: HashMap, bool, bool)> = HashMap::new(); + for style in &styles { + let Some(name) = style.get("name").and_then(Value::as_str) else { continue }; + let based_on = style.get("basedOn").and_then(Value::as_str).map(str::to_string); + let attrs = style.get("para"); + let missing_keep = attrs.and_then(|attrs| attrs.get("keepAllLines")).is_none(); + let missing_drop_cap = attrs.and_then(|attrs| attrs.get("dropCapAlignLeft")).is_none(); + by_name.entry(name.to_string()).or_insert((based_on, missing_keep, missing_drop_cap)); + } + + let mut roots = HashSet::new(); + for name in by_name.keys() { + let mut chain = Vec::new(); + let mut seen = HashSet::new(); + let mut current = Some(name.as_str()); + while let Some(style_name) = current { + let Some((based_on, missing_keep, missing_drop_cap)) = by_name.get(style_name) else { break }; + if chain.len() >= MAX_STYLE_DEPTH || !seen.insert(style_name.to_string()) { + break; + } + chain.push((style_name, *missing_keep, *missing_drop_cap)); + current = based_on.as_deref(); + } + if !chain.is_empty() + && chain.iter().all(|(_, keep, drop_cap)| *keep && *drop_cap) + && let Some((root, _, _)) = chain.last() + { + roots.insert((*root).to_string()); + } + } + if let Some(styles) = document.pointer_mut("/styles/paragraph").and_then(Value::as_array_mut) { + for style in styles { + if let Some(style) = style.as_object_mut() + && style.get("name").and_then(Value::as_str).is_some_and(|name| roots.contains(name)) + { + let attrs = style.entry("para").or_insert_with(|| Value::Object(Default::default())); + if let Some(attrs) = attrs.as_object_mut() { + attrs.entry("keepAllLines").or_insert(Value::Bool(true)); + attrs.entry("dropCapAlignLeft").or_insert(Value::Bool(false)); + } + } + } + } + let known_styles: HashSet = by_name.into_keys().collect(); + pin_unstyled_paragraphs(document, &known_styles, 0); +} + +fn pin_unstyled_paragraphs(value: &mut Value, known_styles: &HashSet, depth: usize) { + if depth > 256 { + return; + } + match value { + Value::Object(object) => { + if let Some(style) = object.get("style").and_then(Value::as_str) + && !known_styles.contains(style) + { + let attrs = object.entry("para").or_insert_with(|| Value::Object(Default::default())); + if let Some(attrs) = attrs.as_object_mut() { + attrs.entry("keepAllLines").or_insert(Value::Bool(true)); + attrs.entry("dropCapAlignLeft").or_insert(Value::Bool(false)); + } + } + for child in object.values_mut() { + pin_unstyled_paragraphs(child, known_styles, depth + 1); + } + } + Value::Array(children) => { + for child in children { + pin_unstyled_paragraphs(child, known_styles, depth + 1); + } + } + _ => {} + } +} From 19692b6190465671e41967c75d0cd4c439d0c84c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:15:16 -0500 Subject: [PATCH 134/520] Expose shared native bundle asset type --- crates/designcraft-format/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/designcraft-format/src/lib.rs b/crates/designcraft-format/src/lib.rs index b497aa4cd..21633a464 100644 --- a/crates/designcraft-format/src/lib.rs +++ b/crates/designcraft-format/src/lib.rs @@ -8,7 +8,8 @@ use std::collections::{HashMap, HashSet}; use std::io::{Read, Seek}; -use craft_document_bundle::{Asset, Spec}; +pub use craft_document_bundle::Asset; +use craft_document_bundle::Spec; use serde::{Serialize, de::DeserializeOwned}; use serde_json::Value; From c44b9c609545e2a0749870701f7733d00bc06342 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:18:43 -0500 Subject: [PATCH 135/520] Reject truncated unsupported DNG samples --- crates/raw/src/tiffraw.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/raw/src/tiffraw.rs b/crates/raw/src/tiffraw.rs index ccbc92025..57dc83a6c 100644 --- a/crates/raw/src/tiffraw.rs +++ b/crates/raw/src/tiffraw.rs @@ -37,14 +37,6 @@ pub(crate) fn check_image(data: &[u8], info: &ImageInfo, limits: &Limits) -> Res if total > limits.samples() || total == 0 { return Err(RawError::Limit("image too large")); } - let bits = info.bits() as u32; - let float = info.sample_format == 3; - if float && !matches!(bits, 16 | 24 | 32) { - return Err(RawError::Unsupported(format!("{bits}-bit floating point samples"))); - } - if !float && !(1..=16).contains(&bits) { - return Err(RawError::Unsupported(format!("{bits}-bit integer samples"))); - } let chunks = info.chunks(data.len() as u64); if chunks.is_empty() { return Err(RawError::Corrupt("no image data chunks".into())); @@ -95,6 +87,17 @@ pub(crate) fn check_image(data: &[u8], info: &ImageInfo, limits: &Limits) -> Res if info.compression == comp::JPEG && referenced.saturating_mul(32) < total as u64 { return Err(RawError::Corrupt(format!("{referenced} bytes of lossless JPEG data cannot hold {total} samples"))); } + // Validate that the source actually contains the declared image before reporting a format + // limitation. A truncated unsupported float DNG must not be mistaken for a valid raw and + // silently replaced by its embedded preview. + let bits = info.bits() as u32; + let float = info.sample_format == 3; + if float && !matches!(bits, 16 | 24 | 32) { + return Err(RawError::Unsupported(format!("{bits}-bit floating-point samples"))); + } + if !float && !(1..=16).contains(&bits) { + return Err(RawError::Unsupported(format!("{bits}-bit integer samples"))); + } Ok(total) } From eed3c28cf016723d004d6f2c79c61424c15d4558 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:18:43 -0500 Subject: [PATCH 136/520] Reject truncated unsupported DNG samples --- crates/raw/src/tiffraw.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/raw/src/tiffraw.rs b/crates/raw/src/tiffraw.rs index ccbc92025..57dc83a6c 100644 --- a/crates/raw/src/tiffraw.rs +++ b/crates/raw/src/tiffraw.rs @@ -37,14 +37,6 @@ pub(crate) fn check_image(data: &[u8], info: &ImageInfo, limits: &Limits) -> Res if total > limits.samples() || total == 0 { return Err(RawError::Limit("image too large")); } - let bits = info.bits() as u32; - let float = info.sample_format == 3; - if float && !matches!(bits, 16 | 24 | 32) { - return Err(RawError::Unsupported(format!("{bits}-bit floating point samples"))); - } - if !float && !(1..=16).contains(&bits) { - return Err(RawError::Unsupported(format!("{bits}-bit integer samples"))); - } let chunks = info.chunks(data.len() as u64); if chunks.is_empty() { return Err(RawError::Corrupt("no image data chunks".into())); @@ -95,6 +87,17 @@ pub(crate) fn check_image(data: &[u8], info: &ImageInfo, limits: &Limits) -> Res if info.compression == comp::JPEG && referenced.saturating_mul(32) < total as u64 { return Err(RawError::Corrupt(format!("{referenced} bytes of lossless JPEG data cannot hold {total} samples"))); } + // Validate that the source actually contains the declared image before reporting a format + // limitation. A truncated unsupported float DNG must not be mistaken for a valid raw and + // silently replaced by its embedded preview. + let bits = info.bits() as u32; + let float = info.sample_format == 3; + if float && !matches!(bits, 16 | 24 | 32) { + return Err(RawError::Unsupported(format!("{bits}-bit floating-point samples"))); + } + if !float && !(1..=16).contains(&bits) { + return Err(RawError::Unsupported(format!("{bits}-bit integer samples"))); + } Ok(total) } From 40304f4295d98695d8a4a087197534c26a1d43d1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 05:24:44 -0500 Subject: [PATCH 137/520] Report unsupported embedded raster images --- crates/svg/src/tree.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index ec0c594bd..71a6df206 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -486,7 +486,10 @@ fn image_resolver_for(limits: Limits, used: Arc) -> usvg::ImageHrefResolve if let Some(format) = format { return raster_image(&used, &limits, data, format); } - if matches!(mime, "image/jpg" | "image/jpeg" | "image/png" | "image/gif" | "image/webp") { + // usvg can decode only the raster variants above. Count every other declared + // image as an invalid raster so strict consumers can reject it instead of + // silently accepting a document after the image was omitted (for example BMP). + if mime.starts_with("image/") && mime != "image/svg+xml" { used.invalid_raster.fetch_add(1, Ordering::Relaxed); } match mime { From c96926f5bf450f4c176160999842a71de472dd12 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 10:24:48 +0000 Subject: [PATCH 138/520] share published FigCraft library manifest schema --- crates/figcraft-format/src/lib.rs | 1 + crates/figcraft-format/src/library.rs | 107 ++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 crates/figcraft-format/src/library.rs diff --git a/crates/figcraft-format/src/lib.rs b/crates/figcraft-format/src/lib.rs index 3dc8e3a32..ccbab57ed 100644 --- a/crates/figcraft-format/src/lib.rs +++ b/crates/figcraft-format/src/lib.rs @@ -1,6 +1,7 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] //! The neutral JSON header and version contract for FigCraft's native project format. //! App-specific document fields remain a generic flattened payload. +pub mod library; use serde::de::{IgnoredAny, MapAccess, Visitor}; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; diff --git a/crates/figcraft-format/src/library.rs b/crates/figcraft-format/src/library.rs new file mode 100644 index 000000000..fe42dc7a4 --- /dev/null +++ b/crates/figcraft-format/src/library.rs @@ -0,0 +1,107 @@ +//! Wire schema and member limits for published libraries embedded in a native project archive. +use serde::{Deserialize, Serialize}; + +pub const MANIFEST: &str = "library/manifest.json"; +pub const DOCUMENT: &str = "library/document.json"; +pub const IMAGES_PREFIX: &str = "library/images/"; +pub const MAX_MANIFEST_BYTES: u64 = 16 * 1024 * 1024; +pub const MAX_IMAGE_BYTES: u64 = 1024 * 1024 * 1024; +pub const MAX_ID_CHARS: usize = 64; +pub const MAX_NAME_CHARS: usize = 200; +pub const MAX_DESCRIPTION_CHARS: usize = 2000; + +/// Native library manifest, independent of the consumer's document model. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct Manifest { + pub id: String, + pub name: String, + pub version: u64, + pub description: String, + pub time: u64, + pub components: Vec, + pub styles: Vec