Merge branch 'codex/omf-bounds' of https://github.com/storytold/craft-libs into codex/aaf-compat-schema

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 17:51:49 +00:00
commit 5476e01e39
57 files changed
+6930 -50

No files matched your search

Generated
+27 -7
View File
@@ -1843,6 +1843,13 @@ dependencies = [
"image",
]
[[package]]
name = "craft-gzip"
version = "0.1.0"
dependencies = [
"flate2",
]
[[package]]
name = "craft-hdr"
version = "0.1.0"
@@ -1919,6 +1926,7 @@ dependencies = [
"avif-serialize",
"craft-heif",
"craft-jpeg",
"craft-media-testkit",
"craft-metadata",
"craft-video",
"crc32fast",
@@ -2383,13 +2391,6 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-plist"
version = "0.1.0"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-platform"
version = "0.1.0"
@@ -2407,6 +2408,13 @@ dependencies = [
"zbus",
]
[[package]]
name = "craft-plist"
version = "0.1.0"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-plotter"
version = "0.1.0"
@@ -2704,6 +2712,7 @@ dependencies = [
"data-url",
"flate2",
"kurbo 0.13.1",
"quick-xml 0.41.0",
"resvg 0.48.1",
"roxmltree 0.21.2",
"thiserror 2.0.21",
@@ -2720,6 +2729,16 @@ dependencies = [
"kurbo 0.13.1",
]
[[package]]
name = "craft-tcraft"
version = "0.1.0"
dependencies = [
"craft-zip",
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-text"
version = "0.1.0"
@@ -2849,6 +2868,7 @@ name = "craft-vectorcraft-format"
version = "0.1.0"
dependencies = [
"flate2",
"proptest",
"serde",
"serde_json",
"thiserror 2.0.21",
+2
View File
@@ -38,6 +38,7 @@ hayro-syntax = { version = "=0.7.2", default-features = false, features = ["std"
bytemuck = "1"
kurbo = "0.13"
thiserror = "2"
craft-gzip = { path = "crates/gzip" }
craft-aaf = { path = "crates/aaf" }
craft-atomic = { path = "crates/atomic" }
craft-captions = { path = "crates/captions" }
@@ -146,6 +147,7 @@ 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" }
craft-tcraft = { path = "crates/tcraft" }
craft-texture = { path = "crates/texture" }
craft-tga = { path = "crates/tga" }
craft-timeline = { path = "crates/timeline" }
+8
View File
@@ -38,6 +38,10 @@ aac-decode = ["craft-media/bitstream"]
# The AAC encoder (`aac::Encoder`), and AAC in M4A through `encode` and `FileEncoder`.
aac-encode = ["craft-media/bitstream", "craft-media/isobmff"]
opus-decode = []
# QOA (Quite OK Audio) files: decoder and encoder in the `qoa` module, each on its own.
qoa = ["qoa-decode", "qoa-encode"]
qoa-decode = []
qoa-encode = []
opus-encode = []
ac3-decode = ["craft-media/bitstream"]
mpa-decode = ["craft-media/bitstream"]
@@ -84,6 +88,10 @@ required-features = ["decode", "lossless-encode", "peaks"]
name = "codecs"
required-features = ["decode", "encode"]
[[test]]
name = "qoa_oracle"
required-features = ["qoa"]
[[example]]
name = "info"
required-features = ["decode"]
+6 -1
View File
@@ -27,7 +27,8 @@
//! - Codec modules for packet-level use, each with its own features: [`aac`] (`aac-decode`,
//! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`, and
//! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`]
//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-encode`: the packet encoder).
//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-encode`: the packet encoder), [`qoa`]
//! (`qoa-decode`, `qoa-encode`: whole QOA files).
//!
//! The shared types below build with no features.
@@ -49,6 +50,8 @@ pub mod opus;
mod pcm;
#[cfg(feature = "peaks")]
pub mod peaks;
#[cfg(any(feature = "qoa-decode", feature = "qoa-encode"))]
pub mod qoa;
#[cfg(feature = "decode-native")]
mod read;
#[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))]
@@ -70,6 +73,8 @@ mod write;
/// PCM WAVE and AIFF wrappers for interchange essence.
#[cfg(feature = "pcm-encode")]
pub mod essence_pcm;
/// Range-backed PCM WAVE access and a 16-bit PCM writer.
pub mod wav_range;
#[cfg(feature = "vorbis-encode")]
pub use write::{OGG_BITRATES, encode_ogg};
+411
View File
@@ -0,0 +1,411 @@
//! QOA ("Quite OK Audio"): a decoder (feature `qoa-decode`) and an encoder (feature `qoa-encode`)
//! for whole `.qoa` files, 16-bit, 1 to 8 channels.
//!
//! Written from the published QOA specification (qoaformat.org). A file is an 8-byte header (the
//! magic `qoaf` and the number of samples per channel) followed by frames of up to 5120 samples
//! per channel. Each frame starts with its channel count, sample rate, sample count and size, then
//! the state of one 4-tap sign-sign LMS predictor per channel, then 64-bit slices of 20 samples:
//! a 4-bit scale factor and twenty 3-bit quantised residuals, the channels interleaved slice by
//! slice.
//!
//! Streaming files (a sample count of 0 in the header) are not supported.
//! Samples are `i16`, one `Vec` per channel.
#[cfg(feature = "qoa-decode")]
const MAX_FRAME_SAMPLES: usize = 5120;
const SLICE_SAMPLES: usize = 20;
const LMS_LEN: usize = 4;
const MAGIC: &[u8; 4] = b"qoaf";
/// The most channels a file may have.
pub const MAX_CHANNELS: usize = 8;
/// `dequant[scale factor][quantised residual]`: `round(round((sf + 1)^2.75) * [0.75, -0.75, 2.5,
/// -2.5, 4.5, -4.5, 7, -7])`.
const DEQUANT: [[i32; 8]; 16] = [
[1, -1, 3, -3, 5, -5, 7, -7],
[5, -5, 18, -18, 32, -32, 49, -49],
[16, -16, 53, -53, 95, -95, 147, -147],
[34, -34, 113, -113, 203, -203, 315, -315],
[63, -63, 210, -210, 378, -378, 588, -588],
[104, -104, 345, -345, 621, -621, 966, -966],
[158, -158, 528, -528, 950, -950, 1477, -1477],
[228, -228, 760, -760, 1368, -1368, 2128, -2128],
[316, -316, 1053, -1053, 1895, -1895, 2947, -2947],
[422, -422, 1405, -1405, 2529, -2529, 3934, -3934],
[548, -548, 1828, -1828, 3290, -3290, 5117, -5117],
[696, -696, 2320, -2320, 4176, -4176, 6496, -6496],
[868, -868, 2893, -2893, 5207, -5207, 8099, -8099],
[1064, -1064, 3548, -3548, 6386, -6386, 9933, -9933],
[1286, -1286, 4288, -4288, 7718, -7718, 12005, -12005],
[1536, -1536, 5120, -5120, 9216, -9216, 14336, -14336],
];
/// What went wrong reading or writing a QOA file.
#[non_exhaustive]
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum Error {
/// The input is not a QOA file, or is damaged or truncated.
#[error("malformed QOA: {0}")]
Malformed(&'static str),
/// A valid file this module does not handle (a streaming file).
#[error("unsupported QOA: {0}")]
Unsupported(&'static str),
/// The samples or settings given to the encoder cannot be written as QOA.
#[error("QOA cannot encode: {0}")]
InvalidInput(&'static str),
}
/// A decoded file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Audio {
/// Sample rate in Hz (1 to 16 777 215).
pub sample_rate: u32,
/// One `Vec` of samples per channel, all the same length.
pub channels: Vec<Vec<i16>>,
}
/// The predictor: the last four samples and four weights.
#[derive(Clone, Copy)]
struct Lms {
history: [i32; LMS_LEN],
weights: [i32; LMS_LEN],
}
impl Lms {
fn new() -> Self {
Lms { history: [0; LMS_LEN], weights: [0, 0, -(1 << 13), 1 << 14] }
}
fn predict(&self) -> i32 {
let mut p = 0i32;
for i in 0..LMS_LEN {
p = p.wrapping_add(self.weights[i].wrapping_mul(self.history[i]));
}
p >> 13
}
fn update(&mut self, sample: i32, residual: i32) {
let delta = residual >> 4;
for i in 0..LMS_LEN {
self.weights[i] += if self.history[i] < 0 { -delta } else { delta };
}
self.history.copy_within(1.., 0);
self.history[LMS_LEN - 1] = sample;
}
}
fn clamp16(v: i32) -> i32 {
v.clamp(-32768, 32767)
}
#[cfg(feature = "qoa-decode")]
mod decode {
use super::{Audio, DEQUANT, Error, LMS_LEN, Lms, MAGIC, MAX_CHANNELS, MAX_FRAME_SAMPLES, SLICE_SAMPLES, clamp16};
fn be16(b: &[u8], i: usize) -> u16 {
u16::from_be_bytes([b[i], b[i + 1]])
}
/// Decode a whole `.qoa` file.
///
/// The declared length is checked against the input before anything is allocated: a slice of
/// 8 bytes holds at most 20 samples, so a file cannot declare more samples than that.
pub fn decode(bytes: &[u8]) -> Result<Audio, Error> {
if bytes.len() < 8 || &bytes[..4] != MAGIC {
return Err(Error::Malformed("missing the qoaf magic"));
}
let total = u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) as usize;
if total == 0 {
return Err(Error::Unsupported("streaming files (a sample count of 0)"));
}
if bytes.len() < 16 {
return Err(Error::Malformed("truncated frame header"));
}
let channels = usize::from(bytes[8]);
let sample_rate = u32::from_be_bytes([0, bytes[9], bytes[10], bytes[11]]);
if channels == 0 || channels > MAX_CHANNELS || sample_rate == 0 {
return Err(Error::Malformed("bad channel count or sample rate"));
}
// 8 bytes (20 samples) per channel at the very least, plus the frame headers.
let room = (bytes.len() - 8) / (8 * channels) * SLICE_SAMPLES;
if total > room {
return Err(Error::Malformed("the file is too short for its declared length"));
}
let mut out: Vec<Vec<i16>> = Vec::new();
for _ in 0..channels {
let mut v = Vec::new();
v.try_reserve_exact(total).map_err(|_| Error::Malformed("too many samples"))?;
out.push(v);
}
let mut pos = 8;
let mut done = 0usize;
while done < total {
let head = bytes.get(pos..pos + 8).ok_or(Error::Malformed("truncated frame header"))?;
let rate = u32::from_be_bytes([0, head[1], head[2], head[3]]);
if usize::from(head[0]) != channels || rate != sample_rate {
return Err(Error::Malformed("the channel count or sample rate changes between frames"));
}
let samples = usize::from(be16(head, 4));
let size = usize::from(be16(head, 6));
let slices = samples.div_ceil(SLICE_SAMPLES);
if samples == 0 || samples > MAX_FRAME_SAMPLES || samples > total - done {
return Err(Error::Malformed("bad frame sample count"));
}
if size != 8 + 16 * channels + 8 * slices * channels {
return Err(Error::Malformed("frame size does not match its sample count"));
}
let frame = bytes.get(pos..pos + size).ok_or(Error::Malformed("truncated frame"))?;
let mut lms = [Lms::new(); MAX_CHANNELS];
for (c, state) in lms.iter_mut().take(channels).enumerate() {
let at = 8 + c * 16;
for i in 0..LMS_LEN {
state.history[i] = i32::from(be16(frame, at + 2 * i) as i16);
state.weights[i] = i32::from(be16(frame, at + 8 + 2 * i) as i16);
}
}
let mut at = 8 + 16 * channels;
for s in 0..slices {
let n = (samples - s * SLICE_SAMPLES).min(SLICE_SAMPLES);
for (c, state) in lms.iter_mut().take(channels).enumerate() {
let mut slice = u64::from_be_bytes([
frame[at],
frame[at + 1],
frame[at + 2],
frame[at + 3],
frame[at + 4],
frame[at + 5],
frame[at + 6],
frame[at + 7],
]);
at += 8;
let table = &DEQUANT[(slice >> 60) as usize & 15];
for _ in 0..n {
let dequantised = table[(slice >> 57) as usize & 7];
slice <<= 3;
let sample = clamp16(state.predict() + dequantised);
out[c].push(sample as i16);
state.update(sample, dequantised);
}
}
}
pos += size;
done += samples;
}
Ok(Audio { sample_rate, channels: out })
}
}
#[cfg(feature = "qoa-decode")]
pub use decode::decode;
#[cfg(feature = "qoa-encode")]
mod encode {
use super::{DEQUANT, Error, Lms, MAGIC, MAX_CHANNELS, SLICE_SAMPLES, clamp16};
/// The quantised residual (0..8) whose dequantised value is nearest `residual`.
fn quantise(table: &[i32; 8], residual: i32) -> usize {
let mut best = 0;
let mut best_err = i64::MAX;
for (q, &d) in table.iter().enumerate() {
let e = i64::from(residual - d).pow(2);
if e < best_err {
best = q;
best_err = e;
}
}
best
}
/// Encode one slice of one channel with every scale factor and keep the one with the least
/// squared error. Returns the 64-bit slice; `lms` is advanced to the chosen result.
fn encode_slice(lms: &mut Lms, samples: &[i16]) -> u64 {
let mut best: Option<(i64, u64, Lms)> = None;
for (sf, table) in DEQUANT.iter().enumerate() {
let mut trial = *lms;
let mut slice = sf as u64;
let mut err = 0i64;
for k in 0..SLICE_SAMPLES {
let mut q = 0;
if let Some(&s) = samples.get(k) {
let predicted = trial.predict();
q = quantise(table, i32::from(s) - predicted);
let sample = clamp16(predicted + table[q]);
err += i64::from(i32::from(s) - sample).pow(2);
trial.update(sample, table[q]);
}
slice = slice << 3 | q as u64;
}
// 4 + 60 bits are in `slice`; the scale factor belongs on top.
let slice = slice & ((1 << 60) - 1) | (sf as u64) << 60;
if best.as_ref().is_none_or(|b| err < b.0) {
best = Some((err, slice, trial));
}
}
match best {
Some((_, slice, state)) => {
*lms = state;
slice
}
None => 0,
}
}
/// Encode `channels` (one `Vec` of samples per channel, all the same length) as a `.qoa` file.
pub fn encode(sample_rate: u32, channels: &[Vec<i16>]) -> Result<Vec<u8>, Error> {
let n = channels.len();
if n == 0 || n > MAX_CHANNELS {
return Err(Error::InvalidInput("QOA stores 1 to 8 channels"));
}
if sample_rate == 0 || sample_rate > 0x00ff_ffff {
return Err(Error::InvalidInput("the sample rate must be 1 to 16777215 Hz"));
}
let total = channels[0].len();
if channels.iter().any(|c| c.len() != total) {
return Err(Error::InvalidInput("the channels differ in length"));
}
let total32 = u32::try_from(total).map_err(|_| Error::InvalidInput("too many samples"))?;
if total == 0 {
return Err(Error::InvalidInput("QOA cannot store an empty file"));
}
let mut out = Vec::with_capacity(8 + total * n / 2);
out.extend_from_slice(MAGIC);
out.extend_from_slice(&total32.to_be_bytes());
let mut lms = [Lms::new(); MAX_CHANNELS];
let mut start = 0;
while start < total {
let len = (total - start).min(5120);
let slices = len.div_ceil(SLICE_SAMPLES);
let size = 8 + 16 * n + 8 * slices * n;
out.push(n as u8);
out.extend_from_slice(&sample_rate.to_be_bytes()[1..]);
out.extend_from_slice(&(len as u16).to_be_bytes());
out.extend_from_slice(&(size as u16).to_be_bytes());
// The state a frame starts from is written as 16-bit values, so the predictor
// continues from exactly what a decoder reads.
for state in lms.iter_mut().take(n) {
for v in state.history.iter_mut().chain(state.weights.iter_mut()) {
*v = i32::from(*v as i16);
out.extend_from_slice(&(*v as i16).to_be_bytes());
}
}
for s in 0..slices {
let from = start + s * SLICE_SAMPLES;
let to = (from + SLICE_SAMPLES).min(start + len);
for (c, state) in lms.iter_mut().take(n).enumerate() {
out.extend_from_slice(&encode_slice(state, &channels[c][from..to]).to_be_bytes());
}
}
start += len;
}
Ok(out)
}
}
#[cfg(feature = "qoa-encode")]
pub use encode::encode;
#[cfg(all(test, feature = "qoa-decode", feature = "qoa-encode"))]
mod tests {
use super::*;
fn tone(frames: usize, ch: usize) -> Vec<Vec<i16>> {
(0..ch)
.map(|c| (0..frames).map(|i| (((i as f64) * (0.03 + 0.01 * c as f64)).sin() * 12000.0 + ((i * 7919) % 200) as f64) as i16).collect())
.collect()
}
fn snr(a: &[i16], b: &[i16]) -> f64 {
let (mut s, mut e) = (0f64, 0f64);
for (&x, &y) in a.iter().zip(b) {
s += f64::from(x).powi(2);
e += (f64::from(x) - f64::from(y)).powi(2);
}
10.0 * (s / e.max(1.0)).log10()
}
#[test]
fn dequant_table_matches_its_formula() {
for (sf, row) in DEQUANT.iter().enumerate() {
let scale = ((sf as f64 + 1.0).powf(2.75)).round();
for (q, base) in [0.75, -0.75, 2.5, -2.5, 4.5, -4.5, 7.0, -7.0].into_iter().enumerate() {
assert_eq!(f64::from(row[q]), (scale * base).round(), "sf {sf} q {q}");
}
}
}
#[test]
fn round_trips_with_a_good_signal_to_noise_ratio() {
for (frames, ch) in [(1, 1), (19, 2), (20, 1), (21, 2), (5120, 1), (5121, 2), (12_345, 8)] {
let src = tone(frames, ch);
let bytes = encode(44_100, &src).unwrap();
let back = decode(&bytes).unwrap();
assert_eq!((back.sample_rate, back.channels.len(), back.channels[0].len()), (44_100, ch, frames));
if frames > 1000 {
for (c, (a, b)) in src.iter().zip(&back.channels).enumerate() {
assert!(snr(a, b) > 25.0, "{frames}x{ch} channel {c}");
}
}
}
}
#[test]
fn silence_and_full_scale_extremes_survive() {
let silent = vec![vec![0i16; 3000]];
// No residual value is zero, so silence decodes to a tiny residue, never exactly 0.
let quiet = decode(&encode(8000, &silent).unwrap()).unwrap();
assert!(quiet.channels[0].iter().all(|&s| s.abs() <= 2), "silence stays within 2 LSB");
let square: Vec<i16> = (0..4000).map(|i| if (i / 50) % 2 == 0 { i16::MAX } else { i16::MIN }).collect();
let back = decode(&encode(48_000, std::slice::from_ref(&square)).unwrap()).unwrap();
assert_eq!(back.channels[0].len(), square.len());
}
#[test]
fn the_file_has_the_documented_layout() {
let bytes = encode(48_000, &tone(100, 2)).unwrap();
assert_eq!(&bytes[..4], b"qoaf");
assert_eq!(u32::from_be_bytes(bytes[4..8].try_into().unwrap()), 100);
assert_eq!(bytes[8], 2);
assert_eq!(&bytes[9..12], &48_000u32.to_be_bytes()[1..]);
assert_eq!(u16::from_be_bytes([bytes[12], bytes[13]]), 100);
// 8 header + 2 channels * 16 state + 5 slices * 2 channels * 8
assert_eq!(usize::from(u16::from_be_bytes([bytes[14], bytes[15]])), 8 + 32 + 80);
assert_eq!(bytes.len(), 8 + 8 + 32 + 80);
// The first frame starts from the specified predictor state.
assert_eq!(&bytes[16 + 8..16 + 16], &[0, 0, 0, 0, 0xe0, 0, 0x40, 0]);
}
#[test]
fn invalid_input_to_the_encoder_is_refused() {
assert!(encode(44_100, &[]).is_err());
assert!(encode(0, &[vec![0; 10]]).is_err());
assert!(encode(1 << 24, &[vec![0; 10]]).is_err());
assert!(encode(44_100, &[vec![0; 10], vec![0; 9]]).is_err());
assert!(encode(44_100, &[vec![]]).is_err());
assert!(encode(44_100, &vec![vec![0; 10]; 9]).is_err());
}
#[test]
fn hostile_input_is_an_error_not_a_panic() {
let good = encode(44_100, &tone(6000, 2)).unwrap();
for len in 0..good.len().min(600) {
let _ = decode(&good[..len]);
}
assert!(decode(&good[..good.len() - 1]).is_err());
// A header that declares more samples than the bytes can hold.
let mut huge = good.clone();
huge[4..8].copy_from_slice(&u32::MAX.to_be_bytes());
assert!(decode(&huge).is_err());
// A streaming file, and a frame whose size field lies.
let mut streaming = good.clone();
streaming[4..8].fill(0);
assert_eq!(decode(&streaming), Err(Error::Unsupported("streaming files (a sample count of 0)")));
let mut lying = good.clone();
lying[14] ^= 1;
assert!(decode(&lying).is_err());
let mut zero_rate = good.clone();
zero_rate[9..12].fill(0);
assert!(decode(&zero_rate).is_err());
let mut changed = good;
let second = 8 + usize::from(u16::from_be_bytes([changed[14], changed[15]]));
changed[second] = 1;
assert!(decode(&changed).is_err());
}
}
+223
View File
@@ -0,0 +1,223 @@
//! Range-backed PCM WAVE reader and canonical 16-bit writer.
//!
//! Reads chunk headers and only the requested sample range; it never loads a whole file.
use crate::{AudioError, Result};
use craft_media::ByteSource;
/// Whether a prefix identifies a RIFF or RF64 WAVE file.
pub fn sniff(b: &[u8]) -> bool {
b.len() >= 12 && (&b[0..4] == b"RIFF" || &b[0..4] == b"RF64") && &b[8..12] == b"WAVE"
}
const MAX_CHUNKS: usize = 1 << 16;
const IXML_MAX: u64 = 1 << 20;
/// Maximum sample data returned by a single range read.
pub const MAX_RANGE_BYTES: u64 = 4 << 20;
fn le16(b: &[u8], o: usize) -> u16 {
b.get(o..o.saturating_add(2)).and_then(|s| s.try_into().ok()).map_or(0, u16::from_le_bytes)
}
fn le32(b: &[u8], o: usize) -> u32 {
b.get(o..o.saturating_add(4)).and_then(|s| s.try_into().ok()).map_or(0, u32::from_le_bytes)
}
fn read_range(source: &impl ByteSource, off: u64, len: usize) -> Result<Vec<u8>> {
let mut bytes = vec![0; len];
source.read_at(off, &mut bytes)?;
Ok(bytes)
}
/// A PCM WAVE file held by an arbitrary random-access source.
pub struct WavRange<S> {
source: S,
/// Number of complete sample frames.
pub frames: u64,
/// Channel count.
pub channels: u16,
/// Sample rate in Hz.
pub rate: u32,
/// Container bits per sample.
pub bits: u16,
/// Whether samples are floating point.
pub float: bool,
/// BWF time reference in samples, from bext or iXML.
pub time_reference: Option<u64>,
data_off: u64,
}
impl<S: ByteSource> WavRange<S> {
/// Open with caller-selected bounds on channel count and sample rate.
pub fn open(source: S, max_channels: u32, max_rate: u32) -> Result<Self> {
let len = source.len();
let mut pos: u64 = 12;
let mut fmt = None;
let mut data = None;
let mut bext_tr = None;
let mut ixml_tr = None;
let mut ds64_data = None;
for _ in 0..MAX_CHUNKS {
let Some(body) = pos.checked_add(8).filter(|&b| b <= len) else { break };
let mut hdr = [0u8; 8];
source.read_at(pos, &mut hdr)?;
let id = &hdr[0..4];
let mut clen = u64::from(le32(&hdr, 4));
let rest = len - body;
if id == b"data" {
if clen == 0xFFFF_FFFF {
clen = ds64_data.unwrap_or(rest);
}
clen = clen.min(rest);
}
if clen > rest {
break;
}
match id {
b"fmt " if clen >= 16 => {
let b = read_range(&source, body, clen.min(40) as usize)?;
let mut tag = le16(&b, 0);
if tag == 0xFFFE && clen >= 40 {
tag = le16(&b, 24);
}
fmt = Some((tag, le16(&b, 2), le32(&b, 4), le16(&b, 14)));
}
b"ds64" if clen >= 16 => {
let b = read_range(&source, body + 8, 8)?;
ds64_data = Some(u64::from(le32(&b, 0)) | u64::from(le32(&b, 4)) << 32);
}
b"data" => data = Some((body, clen)),
b"bext" if clen >= 346 => {
let b = read_range(&source, body + 338, 8)?;
bext_tr = Some(u64::from(le32(&b, 0)) | u64::from(le32(&b, 4)) << 32);
}
b"iXML" => {
let b = read_range(&source, body, clen.min(IXML_MAX) as usize)?;
ixml_tr = ixml_time_reference(&b);
}
_ => {}
}
pos = body.saturating_add(clen).saturating_add(clen & 1);
}
let (tag, channels, rate, bits) = fmt.ok_or_else(|| AudioError::Malformed("missing fmt chunk".into()))?;
let (data_off, dlen) = data.ok_or_else(|| AudioError::Malformed("missing data chunk".into()))?;
let float = tag == 3;
if !(tag == 1 || float)
|| channels == 0
|| rate == 0
|| u32::from(channels) > max_channels
|| rate > max_rate
|| !matches!(bits, 8 | 16 | 24 | 32 | 64)
{
return Err(AudioError::Unsupported(format!("WAV format tag {tag}, {bits} bits")));
}
let frames = dlen / (u64::from(channels) * u64::from(bits / 8));
Ok(Self { source, frames, channels, rate, bits, float, time_reference: bext_tr.or(ixml_tr), data_off })
}
/// Bytes in one whole frame.
pub fn frame_bytes(&self) -> usize {
usize::from(self.channels) * usize::from(self.bits / 8)
}
/// Read the inclusive range of frames, capped to prevent unbounded allocation.
pub fn read_frames(&self, first: u64, last: u64) -> Result<Vec<u8>> {
if first > last || last >= self.frames {
return Err(AudioError::Malformed("WAV frame range outside data".into()));
}
let n = (last - first + 1).saturating_mul(self.frame_bytes() as u64);
if n > MAX_RANGE_BYTES {
return Err(AudioError::TooLarge("WAV frame range".into()));
}
let off = self.data_off.saturating_add(first.saturating_mul(self.frame_bytes() as u64));
read_range(&self.source, off, n as usize)
}
/// Decode a channel from a buffer returned by `read_frames`.
pub fn sample(&self, block: &[u8], frame: usize, ch: usize) -> f32 {
if ch >= usize::from(self.channels) {
return 0.0;
}
let bps = usize::from(self.bits / 8);
let Some(o) = frame.checked_mul(usize::from(self.channels)).and_then(|v| v.checked_add(ch)).and_then(|v| v.checked_mul(bps)) else {
return 0.0;
};
let Some(b) = block.get(o..o.saturating_add(bps)) else { return 0.0 };
match (self.bits, self.float, b) {
(8, _, &[a]) => (a as f32 - 128.0) / 128.0,
(16, _, &[a, b]) => i16::from_le_bytes([a, b]) as f32 / 32768.0,
(24, _, &[a, b, c]) => (i32::from_le_bytes([0, a, b, c]) >> 8) as f32 / 8_388_608.0,
(32, false, &[a, b, c, d]) => i32::from_le_bytes([a, b, c, d]) as f32 / 2_147_483_648.0,
(32, true, &[a, b, c, d]) => f32::from_le_bytes([a, b, c, d]),
(64, true, b) => f64::from_le_bytes(b.try_into().unwrap_or([0; 8])) as f32,
_ => 0.0,
}
}
}
fn ixml_time_reference(x: &[u8]) -> Option<u64> {
let text = String::from_utf8_lossy(x);
let field = |tag: &str| -> Option<u64> {
let open = format!("<{tag}>");
let a = text.find(&open)? + open.len();
let b = a + text[a..].find('<')?;
text[a..b].trim().parse().ok()
};
let low = field("BWF_TIME_REFERENCE_LOW")?;
Some(low | field("BWF_TIME_REFERENCE_HIGH").unwrap_or(0) << 32)
}
/// Write interleaved samples as 16-bit PCM WAVE, using symmetric 32767 scaling.
#[cfg(feature = "pcm-encode")]
pub fn write_wav16(samples: &[f32], channels: u16, rate: u32) -> Vec<u8> {
let data_len = samples.len().saturating_mul(2);
let mut v = crate::pcm_wav_header(u32::try_from(data_len).unwrap_or(u32::MAX), channels, rate, 16);
v.reserve(data_len);
for s in samples {
v.extend_from_slice(&((s.clamp(-1.0, 1.0) * 32767.0).round() as i16).to_le_bytes());
}
v
}
#[cfg(all(test, feature = "pcm-encode"))]
mod tests {
use super::*;
use std::sync::{Arc, Mutex};
struct Source {
bytes: Vec<u8>,
reads: Arc<Mutex<Vec<(u64, usize)>>>,
}
impl ByteSource for Source {
fn len(&self) -> u64 {
self.bytes.len() as u64
}
fn read_at(&self, offset: u64, buf: &mut [u8]) -> std::io::Result<()> {
self.reads.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push((offset, buf.len()));
self.bytes.read_at(offset, buf)
}
}
#[test]
fn bounded_reads_and_pcm_values() {
let bytes = write_wav16(&[-1.0, -0.5, 0.0, 0.5, 1.0], 1, 48_000);
let reads = Arc::new(Mutex::new(Vec::new()));
let wav = WavRange::open(Source { bytes, reads: reads.clone() }, 64, 384_000).unwrap();
assert_eq!((wav.frames, wav.channels, wav.rate, wav.bits), (5, 1, 48_000, 16));
assert!(reads.lock().unwrap().iter().all(|(_, n)| *n <= 40));
let data = wav.read_frames(1, 3).unwrap();
assert_eq!(wav.sample(&data, 0, 0), -0.5);
assert_eq!(wav.sample(&data, 2, 0), 0.5);
assert!(wav.read_frames(0, 5).is_err());
assert!(wav.read_frames(3, 1).is_err());
assert_eq!(reads.lock().unwrap().last(), Some(&(46, 6)));
}
#[test]
fn ixml_fallback_accepts_flat_fields() {
let base = write_wav16(&[0.0], 1, 48_000);
let mut bytes = base[..36].to_vec();
let xml = b"<BWF_TIME_REFERENCE_LOW>5</BWF_TIME_REFERENCE_LOW><BWF_TIME_REFERENCE_HIGH>1</BWF_TIME_REFERENCE_HIGH>";
bytes.extend_from_slice(b"iXML");
bytes.extend_from_slice(&(xml.len() as u32).to_le_bytes());
bytes.extend_from_slice(xml);
if xml.len() & 1 != 0 {
bytes.push(0);
}
bytes.extend_from_slice(&base[36..]);
assert_eq!(WavRange::open(bytes.as_slice(), 64, 384_000).unwrap().time_reference, Some((1 << 32) + 5));
}
}
+62
View File
@@ -0,0 +1,62 @@
//! QOA against ffmpeg's decoder as the reference (oracle; skipped when ffmpeg is absent, a failure
//! with `CRAFT_MEDIA_REQUIRE_ORACLES=1`): ffmpeg decodes the files our encoder writes to exactly
//! the samples our decoder returns for them, over several sizes (partial slices, partial frames,
//! several frames), channel counts and rates.
use craft_audio::qoa::{decode, encode};
use std::process::Command;
fn signal(channels: usize, frames: usize) -> Vec<Vec<i16>> {
let mut seed = 0x2545_F491u32;
(0..channels)
.map(|c| {
(0..frames)
.map(|i| {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
let tone = ((i as f64) * (0.02 + 0.013 * c as f64)).sin() * 9000.0;
// Tones, noise bursts, silence and full-scale peaks.
match (i / 700) % 4 {
0 => tone as i16,
1 => (tone + f64::from(seed % 6000) - 3000.0) as i16,
2 => 0,
_ => {
if i % 2 == 0 {
i16::MAX
} else {
i16::MIN
}
}
}
})
.collect()
})
.collect()
}
#[test]
fn ffmpeg_decodes_our_files_to_the_same_samples() {
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("qoa");
for (rate, channels, frames) in
[(44_100u32, 1usize, 1usize), (8_000, 1, 19), (48_000, 2, 5_121), (22_050, 2, 12_345), (96_000, 1, 20_000), (44_100, 4, 6_000)]
{
let src = signal(channels, frames);
let bytes = encode(rate, &src).expect("encode");
let ours = decode(&bytes).expect("decode");
let file = dir.join(format!("ours_{rate}_{channels}_{frames}.qoa"));
std::fs::write(&file, &bytes).expect("write fixture");
let out = Command::new(&ffmpeg)
.args(["-v", "error", "-y", "-i"])
.arg(&file)
.args(["-f", "s16le", "-acodec", "pcm_s16le", "-"])
.output()
.expect("run ffmpeg");
assert!(out.status.success(), "ffmpeg failed on {}: {}", file.display(), String::from_utf8_lossy(&out.stderr));
let theirs: Vec<i16> = out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect();
let interleaved: Vec<i16> = (0..frames).flat_map(|i| ours.channels.iter().map(move |ch| ch[i])).collect();
assert_eq!(theirs.len(), interleaved.len(), "{rate} Hz, {channels} ch, {frames} frames: sample count");
assert!(theirs == interleaved, "{rate} Hz, {channels} ch, {frames} frames: samples differ");
}
}
+12
View File
@@ -424,6 +424,18 @@ fn shipped_cmyk(bytes: &[u8], b: Builtin) -> Profile {
})
}
/// Generates the compact ICC v2.1 sRGB matrix/TRC profile used by PDFCraft.
///
/// Unlike [`Builtin::Srgb`]'s v4 profile, this uses a 1024-sample tone curve and
/// v2 `desc`/`text` tags for consumers that require a v2 output intent. The legacy
/// description `sRGB (PdfCraft)` is retained to preserve existing document bytes.
/// The generated profile contains no external assets; callers choose where it is
/// appropriate (including their own PDF/A conformance policy).
/// All sizes are fixed; no input-controlled allocation or recursion is involved.
pub fn srgb_v2() -> Vec<u8> {
crate::builtin_v2::srgb()
}
#[cfg(test)]
mod tests {
use super::*;
+143
View File
@@ -0,0 +1,143 @@
//! A small sRGB ICC profile (version 2, display class, matrix/TRC), written from the published
//! sRGB primaries (IEC 61966-2-1) adapted to D50, with the sRGB tone curve sampled at 1024
//! points. Used as the PDF/A output intent's destination profile.
fn s15f16(v: f64) -> [u8; 4] {
((v * 65536.0).round() as i32).to_be_bytes()
}
fn xyz(x: f64, y: f64, z: f64) -> Vec<u8> {
let mut t = b"XYZ \0\0\0\0".to_vec();
t.extend(s15f16(x));
t.extend(s15f16(y));
t.extend(s15f16(z));
t
}
fn desc(text: &str) -> Vec<u8> {
let mut t = b"desc\0\0\0\0".to_vec();
let ascii: Vec<u8> = text.bytes().chain(std::iter::once(0)).collect();
t.extend((ascii.len() as u32).to_be_bytes());
t.extend(&ascii);
// No Unicode or ScriptCode descriptions.
t.extend([0u8; 4 + 4]);
t.extend([0u8; 2 + 1 + 67]);
t
}
fn text(text: &str) -> Vec<u8> {
let mut t = b"text\0\0\0\0".to_vec();
t.extend(text.bytes());
t.push(0);
t
}
fn curve() -> Vec<u8> {
const N: usize = 1024;
let mut t = b"curv\0\0\0\0".to_vec();
t.extend((N as u32).to_be_bytes());
for i in 0..N {
let v = i as f64 / (N - 1) as f64;
let lin = if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) };
t.extend(((lin * 65535.0).round() as u16).to_be_bytes());
}
t
}
/// Fixed-size profile bytes; all tag sizes and loop counts are constants.
pub fn srgb() -> Vec<u8> {
// sRGB primaries, Bradford-adapted to the D50 profile connection space.
let tags: Vec<(&[u8; 4], Vec<u8>)> = vec![
(b"desc", desc("sRGB (PdfCraft)")),
(b"cprt", text("No copyright, use freely")),
(b"wtpt", xyz(0.9642, 1.0, 0.8249)),
(b"rXYZ", xyz(0.4361, 0.2225, 0.0139)),
(b"gXYZ", xyz(0.3851, 0.7169, 0.0971)),
(b"bXYZ", xyz(0.1431, 0.0606, 0.7141)),
(b"rTRC", curve()),
];
// gTRC and bTRC share rTRC's data.
let count = tags.len() + 2;
let table_len = 4 + count * 12;
let mut offset = 128 + table_len;
let mut table = (count as u32).to_be_bytes().to_vec();
let mut data = Vec::new();
let mut trc = (0, 0);
for (sig, body) in &tags {
let len = body.len();
table.extend(*sig);
table.extend((offset as u32).to_be_bytes());
table.extend((len as u32).to_be_bytes());
if *sig == b"rTRC" {
trc = (offset, len);
}
data.extend(body);
// Tags start on 4-byte boundaries.
let pad = (4 - len % 4) % 4;
data.extend(std::iter::repeat_n(0u8, pad));
offset += len + pad;
}
for sig in [b"gTRC", b"bTRC"] {
table.extend(sig);
table.extend((trc.0 as u32).to_be_bytes());
table.extend((trc.1 as u32).to_be_bytes());
}
let size = 128 + table.len() + data.len();
let mut h = Vec::with_capacity(size);
h.extend((size as u32).to_be_bytes());
h.extend(b"\0\0\0\0"); // preferred CMM
h.extend([2, 0x10, 0, 0]); // version 2.1
h.extend(b"mntr");
h.extend(b"RGB ");
h.extend(b"XYZ ");
h.extend([0u8; 12]); // date
h.extend(b"acsp");
h.extend(b"\0\0\0\0"); // platform
h.extend([0u8; 4]); // flags
h.extend([0u8; 4]); // manufacturer
h.extend([0u8; 4]); // model
h.extend([0u8; 8]); // attributes
h.extend([0, 0, 0, 0]); // perceptual intent
h.extend(s15f16(0.9642));
h.extend(s15f16(1.0));
h.extend(s15f16(0.8249));
h.extend([0u8; 4]); // creator
h.resize(128, 0);
h.extend(table);
h.extend(data);
h
}
#[cfg(test)]
mod tests {
#[test]
fn parses_as_v2_with_shared_rgb_curves() {
let bytes = super::srgb();
let p = crate::Profile::parse(&bytes).unwrap();
assert_eq!(p.version, (2, 0x10));
assert_eq!(p.color_space, crate::ColorSpace::Rgb);
assert_eq!(p.pcs, crate::Pcs::Xyz);
assert_eq!(p.description, "sRGB (PdfCraft)");
let curves = p.trc.unwrap();
assert_eq!(curves[0], curves[1]);
assert_eq!(curves[1], curves[2]);
assert!(matches!(&curves[0], crate::Curve::Table(v) if v.len() == 1024 && v[0] == 0.0 && v[1023] == 1.0));
assert_eq!(bytes, super::srgb());
}
#[test]
fn profile_is_well_formed() {
let p = super::srgb();
assert_eq!(u32::from_be_bytes(p[0..4].try_into().unwrap()) as usize, p.len());
assert_eq!(&p[36..40], b"acsp");
assert_eq!(&p[12..20], b"mntrRGB ");
let n = u32::from_be_bytes(p[128..132].try_into().unwrap()) as usize;
assert_eq!(n, 9);
for i in 0..n {
let e = 132 + i * 12;
let off = u32::from_be_bytes(p[e + 4..e + 8].try_into().unwrap()) as usize;
let len = u32::from_be_bytes(p[e + 8..e + 12].try_into().unwrap()) as usize;
assert!(off.is_multiple_of(4) && off + len <= p.len(), "tag {i}");
}
}
}
+1
View File
@@ -20,6 +20,7 @@
#![forbid(unsafe_code)]
pub mod builtin;
mod builtin_v2;
pub mod clut;
pub mod curve;
pub mod gamut;
+1
View File
@@ -11,6 +11,7 @@
use std::io::Write as _;
pub mod model;
pub mod mtext;
pub mod paths;
mod version;
pub use version::Version;
+103
View File
@@ -0,0 +1,103 @@
//! MTEXT inline-escape handling independent of drawing and text layout.
//!
//! Unicode and MIF escapes are decoded to literal characters, with markup-significant
//! characters re-escaped so subsequent MTEXT parsers cannot treat them as commands.
/// Whether an inline command takes an argument terminated by `;`.
pub fn takes_arg(code: char) -> bool {
matches!(code, 'f' | 'F' | 'H' | 'W' | 'Q' | 'T' | 'A' | 'C' | 'c' | 'p')
}
/// Four hex digits at `chars[at..at + 4]`.
fn hex4(chars: &[char], at: usize) -> Option<u32> {
let digits = chars.get(at..at.checked_add(4)?)?;
digits.iter().try_fold(0u32, |acc, c| Some(acc * 16 + c.to_digit(16)?))
}
/// Push a decoded character so that the tokenizers read it as text, never as markup.
fn push_literal(out: &mut String, c: char) {
if matches!(c, '\\' | '{' | '}') {
out.push('\\');
}
out.push(c);
}
/// Decode `\U+hhhh` (one UTF-16 unit; a surrogate pair gives one character, a lone surrogate
/// U+FFFD) and `\M+nhhhh` (double-byte East Asian characters) to the characters they stand
/// for. Malformed escapes remain unchanged for the caller to interpret.
/// An escaped backslash (`\\U+0041`) is not an escape.
pub fn unescape_codes(s: &str) -> std::borrow::Cow<'_, str> {
if !s.contains("\\U+") && !s.contains("\\M+") {
return std::borrow::Cow::Borrowed(s);
}
let chars: Vec<char> = s.chars().collect();
let mut out = String::with_capacity(s.len());
let mut i = 0;
while let Some(&c) = chars.get(i) {
if c != '\\' {
out.push(c);
i += 1;
continue;
}
let (n, plus) = (chars.get(i + 1).copied(), chars.get(i + 2).copied());
if n == Some('U') && plus == Some('+') {
if let Some(unit) = hex4(&chars, i + 3) {
i += 7;
let low = |at: usize| {
(chars.get(at) == Some(&'\\') && chars.get(at + 1) == Some(&'U') && chars.get(at + 2) == Some(&'+'))
.then(|| hex4(&chars, at + 3))
.flatten()
.filter(|u| (0xDC00..0xE000).contains(u))
};
let ch = if (0xD800..0xDC00).contains(&unit) {
match low(i) {
Some(lo) => {
i += 7;
char::from_u32(0x10000 + ((unit - 0xD800) << 10) + (lo - 0xDC00))
}
None => None,
}
} else {
char::from_u32(unit)
};
push_literal(&mut out, ch.unwrap_or('\u{fffd}'));
continue;
}
} else if n == Some('M') && plus == Some('+') && chars.get(i + 3).is_some_and(char::is_ascii_digit) && hex4(&chars, i + 4).is_some() {
push_literal(&mut out, crate::mif_escape_at(&chars, i).unwrap_or('\u{fffd}'));
i += 8;
continue;
}
// Any other backslash pair stays as it is (and an escaped backslash stays one).
out.push('\\');
i += 1;
if let Some(&next) = chars.get(i) {
out.push(next);
i += 1;
}
}
std::borrow::Cow::Owned(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unicode_and_mif_escapes_remain_literal_markup() {
assert_eq!(unescape_codes(r"a\U+0041\U+D83D\U+DE00"), "aA😀");
assert_eq!(unescape_codes(r"\M+5BCFE"), "件");
assert_eq!(unescape_codes(r"\U+005C\U+007B\U+007D"), r"\\\{\}");
assert_eq!(unescape_codes(r"\\U+0041"), r"\\U+0041");
assert_eq!(unescape_codes(r"\U+12"), r"\U+12");
assert_eq!(unescape_codes(r"\U+D800"), "�");
}
#[test]
fn parameterized_commands_are_classified_without_relaxing_others() {
for c in "fFHWQTACcp".chars() {
assert!(takes_arg(c));
}
for c in ['S', 'P', 'Z', '\\'] {
assert!(!takes_arg(c));
}
}
}
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "craft-gzip"
description = "Streaming gzip writer with stored and compressed segments"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
+119
View File
@@ -0,0 +1,119 @@
//! Streaming gzip output with optional stored segments.
//!
//! Copyright (c) 2026 ArtCraft Team and the VectorCraft contributors.
//! Extracted from VectorCraft, licensed under MIT OR Apache-2.0.
//!
//! `flush` does not emit a deflate block; call [`Gz::finish`] to complete and flush the
//! stream. After an I/O error, discard the writer: the output may be incomplete.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
use flate2::{Compress, Compression, Crc, FlushCompress, Status};
use std::io::{self, Write};
/// Bytes of compressed output gathered before they go to the writer.
const OUT_CHUNK: usize = 64 << 10;
/// A gzip stream, written as [`flate2::write::GzEncoder`] at the default level writes it (the
/// same header and the same data; the deflate blocks are the same too where flate2 deflates with
/// miniz_oxide, not with zlib-rs, which writes them by how the data is fed), except that data which
/// is compressed already ([`Gz::packed`]) is stored as it is: the default level spends a second
/// or more per photo on it for next to nothing, and makes it slower to unpack. Where the level
/// changes the deflate blocks so far end with a sync flush and a fresh compressor writes on,
/// which makes one valid deflate stream that every reader unpacks.
pub struct Gz<W: Write> {
out: W,
deflate: Compress,
/// What `deflate` writes: stored blocks (packed data), else the default level.
stored: bool,
crc: Crc,
buf: Vec<u8>,
/// Bytes written (unpacked).
total: u64,
}
impl<W: Write> Gz<W> {
/// Write a gzip header and construct a writer.
pub fn new(mut out: W) -> io::Result<Self> {
// flate2's header for the default level: no name, no time, unknown OS.
out.write_all(&[0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 255])?;
Ok(Self {
out,
deflate: Compress::new(Compression::default(), false),
stored: false,
crc: Crc::new(),
buf: Vec::with_capacity(OUT_CHUNK),
total: 0,
})
}
/// Run the compressor over `input` with `flush` until it has taken it all (and, flushing,
/// written everything out).
fn deflate(&mut self, mut input: &[u8], flush: FlushCompress) -> io::Result<()> {
loop {
if self.buf.len() == self.buf.capacity() {
self.out.write_all(&self.buf)?;
self.buf.clear();
}
let (before_in, before_out) = (self.deflate.total_in(), self.deflate.total_out());
let status = self.deflate.compress_vec(input, &mut self.buf, flush).map_err(io::Error::other)?;
let taken = usize::try_from(self.deflate.total_in() - before_in).unwrap_or(input.len()).min(input.len());
input = input.get(taken..).unwrap_or_default();
let made = self.deflate.total_out() - before_out;
let full = self.buf.len() == self.buf.capacity();
match flush {
FlushCompress::None if input.is_empty() => return Ok(()),
FlushCompress::Finish if status == Status::StreamEnd => return Ok(()),
// A flush is done once it left room in the output.
FlushCompress::Sync if input.is_empty() && !full => return Ok(()),
_ => {}
}
if taken == 0 && made == 0 && !full {
return Err(io::Error::other("the compressor made no progress"));
}
}
}
/// Return the number of uncompressed bytes supplied.
pub fn written(&self) -> u64 {
self.total
}
/// What is written next is compressed already (stored as it is), or not (compressed at the
/// default level).
pub fn packed(&mut self, packed: bool) -> io::Result<()> {
if packed == self.stored {
return Ok(());
}
self.deflate(&[], FlushCompress::Sync)?;
self.deflate = Compress::new(if packed { Compression::none() } else { Compression::default() }, false);
self.stored = packed;
Ok(())
}
/// Complete the gzip trailer and flush the underlying writer.
pub fn finish(mut self) -> io::Result<()> {
self.deflate(&[], FlushCompress::Finish)?;
self.out.write_all(&self.buf)?;
self.out.write_all(&self.crc.sum().to_le_bytes())?;
self.out.write_all(&self.crc.amount().to_le_bytes())?;
self.out.flush()
}
}
impl<W: Write> Write for Gz<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.write_all(buf)?;
Ok(buf.len())
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.total = self.total.checked_add(buf.len() as u64).ok_or_else(|| io::Error::other("uncompressed byte count overflow"))?;
self.crc.update(buf);
self.deflate(buf, FlushCompress::None)
}
fn flush(&mut self) -> io::Result<()> {
// Flushing would end a deflate block: the stream is flushed when it is finished.
Ok(())
}
}
+77
View File
@@ -0,0 +1,77 @@
use std::io::{self, Read, Write};
use craft_gzip::Gz;
use flate2::{Compression, read::GzDecoder, write::GzEncoder};
fn decode(bytes: &[u8]) -> Vec<u8> {
let mut result = Vec::new();
GzDecoder::new(bytes).read_to_end(&mut result).unwrap();
result
}
#[test]
fn matches_flate2_without_mode_changes() {
let data = b"The quick brown fox jumps over the lazy dog. ".repeat(10000);
let mut expected = GzEncoder::new(Vec::new(), Compression::default());
let mut actual = Vec::new();
{
let mut gz = Gz::new(&mut actual).unwrap();
for chunk in data.chunks(131) {
expected.write_all(chunk).unwrap();
gz.write_all(chunk).unwrap();
}
assert_eq!(gz.written(), data.len() as u64);
gz.finish().unwrap();
}
assert_eq!(actual, expected.finish().unwrap());
assert_eq!(decode(&actual), data);
}
#[test]
fn mixed_segments_round_trip_and_stored_bytes_remain_uncompressed() {
let plain = b"repeat me ".repeat(20000);
let packed: Vec<u8> = (0..200_000).map(|i| (i * 31) as u8).collect();
let mut bytes = Vec::new();
{
let mut gz = Gz::new(&mut bytes).unwrap();
gz.write_all(&plain).unwrap();
gz.packed(true).unwrap();
gz.packed(true).unwrap();
gz.write_all(&packed).unwrap();
gz.packed(false).unwrap();
gz.write_all(&plain).unwrap();
assert_eq!(gz.written(), (plain.len() * 2 + packed.len()) as u64);
gz.finish().unwrap();
}
let expected = [plain.as_slice(), packed.as_slice(), plain.as_slice()].concat();
assert_eq!(decode(&bytes), expected);
assert!(bytes.windows(1024).any(|window| window == &packed[..1024]));
assert!(bytes.len() < expected.len());
}
struct FailAfter {
remaining: usize,
}
impl Write for FailAfter {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
if self.remaining == 0 {
return Err(io::Error::other("injected failure"));
}
let n = self.remaining.min(data.len());
self.remaining -= n;
Ok(n)
}
fn flush(&mut self) -> io::Result<()> {
Err(io::Error::other("injected flush failure"))
}
}
#[test]
fn propagates_header_body_and_flush_failures() {
assert!(Gz::new(FailAfter { remaining: 0 }).is_err());
let mut body = Gz::new(FailAfter { remaining: 10 }).unwrap();
body.packed(true).unwrap();
assert!(body.write_all(&vec![42; 200_000]).is_err());
let empty = Gz::new(FailAfter { remaining: usize::MAX }).unwrap();
assert_eq!(empty.finish().unwrap_err().to_string(), "injected flush failure");
}
+1
View File
@@ -47,6 +47,7 @@ exr = { version = "1.73", default-features = false, features = ["rayon"] }
proptest = { workspace = true }
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff"] }
sha2 = { version = "0.10", default-features = false, features = ["std"] }
craft-media-testkit = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
craft-video = { workspace = true, features = ["av1-decode"] }
+1
View File
@@ -56,6 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "<
| 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 |
| JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | 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` |
+145
View File
@@ -0,0 +1,145 @@
//! Header-only BMP dimensions and pixel-data coverage, without allocating a canvas.
/// Coverage of the declared pixel array. Row byte counts include row padding; RLE coverage
/// counts the pixels drawn by commands and may be smaller than the canvas.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Coverage {
pub pixels: u64,
pub row_bytes: Option<(u64, u64)>,
}
/// Inspect pixel data for the given dimensions without decoding a bitmap. The row tuple is
/// `(available, needed)`; callers choose their own sparse-canvas and allocation policy.
pub fn coverage(bytes: &[u8], w: u32, h: u32) -> Coverage {
match bmp_layout(bytes) {
BmpLayout::Rle { offset, rle4 } => Coverage { pixels: bmp_rle_pixels(bytes, offset, rle4, w, h), row_bytes: None },
BmpLayout::Rows { offset, bits } => {
let needed = u64::from(w).saturating_mul(u64::from(bits)).div_ceil(32).saturating_mul(4).saturating_mul(u64::from(h));
let held = bytes.len().saturating_sub(offset) as u64;
Coverage { pixels: u64::from(w) * u64::from(h), row_bytes: Some((held, needed)) }
}
}
}
enum BmpLayout {
/// RLE8 or RLE4 commands.
Rle { offset: usize, rle4: bool },
/// Rows of `bits` per pixel, each padded to 4 bytes.
Rows { offset: usize, bits: u16 },
}
/// The layout the decoder reads, from the file header and the bitmap header: a 12-byte core header
/// (bits per pixel at 24, never compressed) or a 40-byte or larger one (bits at 28, compression at
/// 30). Fields the decoder would have refused are read as 1 bit per pixel at offset 0, the most
/// lenient layout.
fn bmp_layout(bytes: &[u8]) -> BmpLayout {
let u16_at = |i: usize| bytes.get(i..i.checked_add(2)?).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes);
let u32_at = |i: usize| bytes.get(i..i.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes);
let offset = u32_at(10).and_then(|o| usize::try_from(o).ok()).unwrap_or(0);
let (bits, compression) = match u32_at(14) {
Some(12) => (u16_at(24), Some(0)),
_ => (u16_at(28), u32_at(30)),
};
match compression {
Some(1) => BmpLayout::Rle { offset, rle4: false },
Some(2) => BmpLayout::Rle { offset, rle4: true },
_ => BmpLayout::Rows { offset, bits: bits.filter(|&b| b > 0).unwrap_or(1) },
}
}
/// The pixels an RLE8 or RLE4 BMP's commands draw on a `w` × `h` canvas, read the way the decoder
/// paints them: a run is clipped at the end of its row, an end-of-row code moves to the next row, a
/// delta skips pixels (left blank), and an end-of-bitmap code, a truncated command or the last row
/// ends the picture. Every command takes at least 2 bytes, so the walk is linear in the file.
fn bmp_rle_pixels(bytes: &[u8], offset: usize, rle4: bool, w: u32, h: u32) -> u64 {
let (w, h) = (u64::from(w), u64::from(h));
let (mut pos, mut x, mut y, mut covered) = (offset, 0u64, 0u64, 0u64);
while y < h {
let (Some(&count), Some(&value)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else {
break;
};
pos = pos.saturating_add(2);
match (count, value) {
// end of row
(0, 0) => (x, y) = (0, y.saturating_add(1)),
// end of bitmap
(0, 1) => break,
// delta: right and down
(0, 2) => {
let (Some(&dx), Some(&dy)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else {
break;
};
pos = pos.saturating_add(2);
(x, y) = (x.saturating_add(u64::from(dx)), y.saturating_add(u64::from(dy)));
}
// absolute: `value` pixels stored one per byte (RLE8) or nibble (RLE4), padded to 2 bytes
(0, n) => {
let len = if rle4 { usize::from(n).div_ceil(2) } else { usize::from(n) };
pos = pos.saturating_add(len + (len & 1));
if pos > bytes.len() {
break;
}
covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x)));
x = x.saturating_add(u64::from(n));
}
// a run of `count` pixels
(n, _) => {
covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x)));
x = x.saturating_add(u64::from(n));
}
}
}
covered
}
/// Reads BMP `(width, height)` from the DIB header without allocating.
pub fn dimensions(bytes: &[u8]) -> Option<(u32, u32)> {
if bytes.get(0..2) != Some(b"BM") {
return None;
}
let dib = u32::from_le_bytes(bytes.get(14..18)?.try_into().ok()?);
let (w, h) = if dib == 12 {
(u16::from_le_bytes(bytes.get(18..20)?.try_into().ok()?) as u32, u16::from_le_bytes(bytes.get(20..22)?.try_into().ok()?) as u32)
} else {
let w = i32::from_le_bytes(bytes.get(18..22)?.try_into().ok()?);
let h = i32::from_le_bytes(bytes.get(22..26)?.try_into().ok()?);
(u32::try_from(w).ok()?, h.unsigned_abs())
};
Some((w, h))
}
#[cfg(test)]
mod tests {
use super::*;
fn bmp(compression: u32, pixels: &[u8]) -> Vec<u8> {
let mut b = vec![0; 54];
b[..2].copy_from_slice(b"BM");
b[10..14].copy_from_slice(&54u32.to_le_bytes());
b[14..18].copy_from_slice(&40u32.to_le_bytes());
b[18..22].copy_from_slice(&4i32.to_le_bytes());
b[22..26].copy_from_slice(&2i32.to_le_bytes());
b[28..30].copy_from_slice(&8u16.to_le_bytes());
b[30..34].copy_from_slice(&compression.to_le_bytes());
b.extend_from_slice(pixels);
b
}
#[test]
fn dimensions_and_padded_rows() {
let b = bmp(0, &[0; 7]);
assert_eq!(dimensions(&b), Some((4, 2)));
assert_eq!(coverage(&b, 4, 2), Coverage { pixels: 8, row_bytes: Some((7, 8)) });
assert_eq!(dimensions(&b[..20]), None);
}
#[test]
fn rle_counts_clipped_runs_deltas_and_complete_absolute_blocks() {
let b = bmp(1, &[3, 7, 0, 0, 0, 2, 1, 0, 0, 3, 1, 2, 3, 0, 0, 1]);
assert_eq!(coverage(&b, 4, 2).pixels, 6);
let truncated = bmp(1, &[0, 4, 1, 2]);
assert_eq!(coverage(&truncated, 4, 2).pixels, 0);
let nibble = bmp(2, &[0, 3, 0x12, 0x30, 0, 1]);
assert_eq!(coverage(&nibble, 4, 2).pixels, 3);
}
}
+1
View File
@@ -13,6 +13,7 @@ pub(crate) mod heif;
pub(crate) mod iff;
pub(crate) mod jp2;
pub(crate) mod jpeg;
pub(crate) mod jpegls;
pub(crate) mod jxl;
pub(crate) mod pcx;
pub(crate) mod png;
+870
View File
@@ -0,0 +1,870 @@
//! JPEG-LS (ITU-T T.87 | ISO/IEC 14495-1, "LOCO-I"): lossless and near-lossless gray and RGB
//! images, 2 to 16 bits per sample.
//!
//! Read: baseline JPEG-LS with the default or `LSE`-preset coding parameters, any `NEAR`, and
//! scans that code one component each (`ILV` 0), which is also how this module writes. Line- and
//! sample-interleaved scans, colour transforms and mapping tables are reported as unsupported.
//! Write: lossless (`NEAR` 0), 8 or 16 bits, gray or RGB, one scan per component.
//!
//! Samples of 9 to 15 bits are scaled to 16 bits on read (the top bits are repeated into the low
//! ones), so a decoded image always spans the full range of its sample type.
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::JpegLs;
const SOF55: u8 = 0xF7;
const LSE: u8 = 0xF8;
const RESET_DEFAULT: i32 = 64;
const MIN_C: i32 = -128;
const MAX_C: i32 = 127;
/// `J[RUNindex]`: the log2 of the run length a run-index step codes.
const J: [u32; 32] = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15];
/// Context slots: the regular contexts (365 of the 405 slots the index below can reach), then the two
/// run-interruption contexts.
const RI_BASE: usize = 405;
const CONTEXTS: usize = RI_BASE + 2;
fn bad(reason: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, reason)
}
/// Whether `b` is a JPEG-LS file: an SOI followed by marker segments up to an SOF55 frame header.
pub(crate) fn looks_like_jpegls(b: &[u8]) -> bool {
if !b.starts_with(&[0xFF, 0xD8]) {
return false;
}
let mut pos = 2;
for _ in 0..64 {
let (Some(&0xFF), Some(&marker)) = (b.get(pos), b.get(pos + 1)) else { return false };
match marker {
SOF55 => return true,
0xDA | 0xD9 => return false,
0xC0..=0xCF | 0xDB..=0xDF => return false,
0x01 | 0xD0..=0xD7 | 0xFF => pos += if marker == 0xFF { 1 } else { 2 },
_ => {
let Some(len) = b.get(pos + 2..pos + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { return false };
pos += 2 + len;
}
}
}
false
}
/// The coding parameters of one scan, derived as T.87 section C.2 specifies.
#[derive(Clone, Copy)]
struct Params {
maxval: i32,
near: i32,
range: i32,
qbpp: u32,
limit: u32,
t1: i32,
t2: i32,
t3: i32,
reset: i32,
}
fn ceil_log2(v: i32) -> u32 {
let mut n = 0;
while (1i64 << n) < i64::from(v) {
n += 1;
}
n
}
impl Params {
/// `preset` holds the `LSE` values (`maxval`, `t1`, `t2`, `t3`, `reset`), 0 where absent.
fn new(precision: u32, near: i32, preset: [i32; 5]) -> Result<Params, CodecError> {
let default_max = (1i32 << precision) - 1;
let maxval = if preset[0] == 0 { default_max } else { preset[0] };
if maxval < 1 || maxval > default_max || near < 0 || near > (maxval / 2).min(255) {
return Err(bad("invalid MAXVAL or NEAR"));
}
let clamp = |v: i32, lo: i32, hi: i32| if v > hi || v < lo { lo } else { v };
let (d1, d2, d3) = if maxval >= 128 {
let factor = (maxval.min(4095) + 128) / 256;
let t1 = clamp(factor + 2 + 3 * near, near + 1, maxval);
let t2 = clamp(factor * 4 + 3 + 5 * near, t1, maxval);
let t3 = clamp(factor * 17 + 4 + 7 * near, t2, maxval);
(t1, t2, t3)
} else {
let factor = 256 / (maxval + 1);
let t1 = clamp((3 / factor + 3 * near).max(2), near + 1, maxval);
let t2 = clamp((7 / factor + 5 * near).max(3), t1, maxval);
let t3 = clamp((21 / factor + 7 * near).max(4), t2, maxval);
(t1, t2, t3)
};
let t1 = if preset[1] == 0 { d1 } else { preset[1] };
let t2 = if preset[2] == 0 { d2 } else { preset[2] };
let t3 = if preset[3] == 0 { d3 } else { preset[3] };
let reset = if preset[4] == 0 { RESET_DEFAULT } else { preset[4] };
if !(near < t1 && t1 <= t2 && t2 <= t3 && t3 <= maxval) || !(3..=255).contains(&reset) {
return Err(bad("invalid threshold or RESET preset"));
}
let range = (maxval + 2 * near) / (2 * near + 1) + 1;
let bpp = ceil_log2(maxval + 1).max(2);
Ok(Params { maxval, near, range, qbpp: ceil_log2(range), limit: 2 * (bpp + bpp.max(8)), t1, t2, t3, reset })
}
}
/// Adaptive state of one scan: contexts, bias and the run index.
struct State {
a: [i32; CONTEXTS],
b: [i32; CONTEXTS],
c: [i32; CONTEXTS],
n: [i32; CONTEXTS],
nn: [i32; 2],
run_index: usize,
}
impl State {
fn new(p: &Params) -> State {
let a0 = ((p.range + 32) / 64).max(2);
State { a: [a0; CONTEXTS], b: [0; CONTEXTS], c: [0; CONTEXTS], n: [1; CONTEXTS], nn: [0; 2], run_index: 0 }
}
}
fn quantise_gradient(d: i32, p: &Params) -> i32 {
if d <= -p.t3 {
-4
} else if d <= -p.t2 {
-3
} else if d <= -p.t1 {
-2
} else if d < -p.near {
-1
} else if d <= p.near {
0
} else if d < p.t1 {
1
} else if d < p.t2 {
2
} else if d < p.t3 {
3
} else {
4
}
}
fn predict(ra: i32, rb: i32, rc: i32) -> i32 {
if rc >= ra.max(rb) {
ra.min(rb)
} else if rc <= ra.min(rb) {
ra.max(rb)
} else {
ra + rb - rc
}
}
/// The error quantised for `NEAR` and reduced into the modulo range, as the encoder codes it.
fn reduce(mut e: i32, p: &Params) -> i32 {
if p.near > 0 {
e = if e > 0 { (e + p.near) / (2 * p.near + 1) } else { -(p.near - e) / (2 * p.near + 1) };
}
if e < 0 {
e += p.range;
}
if e >= (p.range + 1) / 2 {
e -= p.range;
}
e
}
/// The reconstructed sample for a decoded (sign-adjusted) error.
fn reconstruct(px: i32, sign: i32, e: i32, p: &Params) -> i32 {
let mut r = px + sign * e * (2 * p.near + 1);
if r < -p.near {
r += p.range * (2 * p.near + 1);
} else if r > p.maxval + p.near {
r -= p.range * (2 * p.near + 1);
}
r.clamp(0, p.maxval)
}
fn golomb_k(n: i32, a: i32) -> u32 {
let mut k = 0;
while (i64::from(n) << k) < i64::from(a) {
k += 1;
}
k
}
/// Bits of the entropy-coded segment, with T.87's stuffing: after an `0xFF` byte the next byte
/// carries only 7 bits.
struct BitReader<'a> {
data: &'a [u8],
pos: usize,
cur: u32,
left: u32,
prev_ff: bool,
}
impl<'a> BitReader<'a> {
fn new(data: &'a [u8]) -> Self {
BitReader { data, pos: 0, cur: 0, left: 0, prev_ff: false }
}
fn bit(&mut self) -> Result<u32, CodecError> {
if self.left == 0 {
let byte = *self.data.get(self.pos).ok_or_else(|| bad("the entropy-coded data ends early"))?;
self.pos += 1;
if self.prev_ff {
if byte & 0x80 != 0 {
return Err(bad("a marker inside entropy-coded data"));
}
self.cur = u32::from(byte);
self.left = 7;
} else {
self.cur = u32::from(byte);
self.left = 8;
}
self.prev_ff = byte == 0xFF;
}
self.left -= 1;
Ok((self.cur >> self.left) & 1)
}
fn bits(&mut self, n: u32) -> Result<u32, CodecError> {
let mut v = 0;
for _ in 0..n {
v = v << 1 | self.bit()?;
}
Ok(v)
}
/// A limited-length Golomb code: the mapped error.
fn golomb(&mut self, k: u32, limit: u32, qbpp: u32) -> Result<i32, CodecError> {
let max_zeros = limit.checked_sub(qbpp + 1).ok_or_else(|| bad("invalid LIMIT"))?;
let mut zeros = 0;
while self.bit()? == 0 {
zeros += 1;
if zeros > max_zeros {
return Err(bad("a Golomb code is longer than LIMIT"));
}
}
let v = if zeros < max_zeros { (zeros << k) + self.bits(k)? } else { self.bits(qbpp)? + 1 };
i32::try_from(v).map_err(|_| bad("mapped error out of range"))
}
}
/// Decode one component scan of `w` x `h` samples into `out` (row-major).
fn decode_scan(data: &[u8], w: usize, h: usize, p: &Params, out: &mut [u16]) -> Result<(), CodecError> {
let mut r = BitReader::new(data);
let mut st = State::new(p);
let mut prev = vec![0i32; w + 2];
let mut cur = vec![0i32; w + 2];
for y in 0..h {
// Ra at the first sample is Rb; the slot at index 0 is Rc there, left over from the line above.
cur[0] = prev[1];
if y > 0 {
prev[w + 1] = prev[w];
}
let mut x = 0usize;
while x < w {
let (ra, rb, rc, rd) = (cur[x], prev[x + 1], prev[x], if y == 0 { 0 } else { prev[x + 2] });
let (q1, q2, q3) = (quantise_gradient(rd - rb, p), quantise_gradient(rb - rc, p), quantise_gradient(rc - ra, p));
if q1 == 0 && q2 == 0 && q3 == 0 {
x = decode_run(&mut r, &mut st, p, &prev, &mut cur, ra, x, w)?;
continue;
}
let (sign, q1, q2, q3) = if q1 < 0 || (q1 == 0 && (q2 < 0 || (q2 == 0 && q3 < 0))) { (-1, -q1, -q2, -q3) } else { (1, q1, q2, q3) };
let q = (q1 * 81 + (q2 + 4) * 9 + (q3 + 4)) as usize;
let k = golomb_k(st.n[q], st.a[q]);
let m = r.golomb(k, p.limit, p.qbpp)?;
let swap = p.near == 0 && k == 0 && 2 * st.b[q] <= -st.n[q];
let e = if swap {
if m & 1 == 1 { (m - 1) >> 1 } else { -(m >> 1) - 1 }
} else if m & 1 == 0 {
m >> 1
} else {
-((m + 1) >> 1)
};
let px = (predict(ra, rb, rc) + sign * st.c[q]).clamp(0, p.maxval);
let rx = reconstruct(px, sign, e, p);
update_regular(&mut st, q, e, p);
cur[x + 1] = rx;
x += 1;
}
let row = &mut out[y * w..(y + 1) * w];
for (o, &v) in row.iter_mut().zip(&cur[1..=w]) {
*o = v as u16;
}
// The line just decoded becomes the line above; its slot 0 is the Rc of the next line's first sample.
std::mem::swap(&mut prev, &mut cur);
}
Ok(())
}
fn update_regular(st: &mut State, q: usize, e: i32, p: &Params) {
st.b[q] += e * (2 * p.near + 1);
st.a[q] += e.abs();
if st.n[q] == p.reset {
st.a[q] >>= 1;
st.b[q] = if st.b[q] >= 0 { st.b[q] >> 1 } else { -((1 - st.b[q]) >> 1) };
st.n[q] >>= 1;
}
st.n[q] += 1;
if st.b[q] <= -st.n[q] {
if st.c[q] > MIN_C {
st.c[q] -= 1;
}
st.b[q] += st.n[q];
if st.b[q] <= -st.n[q] {
st.b[q] = -st.n[q] + 1;
}
} else if st.b[q] > 0 {
if st.c[q] < MAX_C {
st.c[q] += 1;
}
st.b[q] -= st.n[q];
if st.b[q] > 0 {
st.b[q] = 0;
}
}
}
/// Run-interruption variables update shared by both directions.
fn update_run(st: &mut State, q: usize, e: i32, em: i32, ri_type: i32, p: &Params) {
let i = q - RI_BASE;
if e < 0 {
st.nn[i] += 1;
}
st.a[q] += (em + 1 - ri_type) >> 1;
if st.n[q] == p.reset {
st.a[q] >>= 1;
st.n[q] >>= 1;
st.nn[i] >>= 1;
}
st.n[q] += 1;
}
/// The run-interruption Golomb parameter and the mapping flag of an error.
fn run_k(st: &State, q: usize, ri_type: i32) -> u32 {
let temp = if ri_type == 1 { st.a[q] + (st.n[q] >> 1) } else { st.a[q] };
golomb_k(st.n[q], temp)
}
#[allow(clippy::too_many_arguments)]
fn decode_run(
r: &mut BitReader,
st: &mut State,
p: &Params,
prev: &[i32],
cur: &mut [i32],
ra: i32,
x: usize,
w: usize,
) -> Result<usize, CodecError> {
let remaining = w - x;
let mut run = 0usize;
while r.bit()? == 1 {
let step = 1usize << J[st.run_index];
let count = step.min(remaining - run);
run += count;
if count == step && st.run_index < 31 {
st.run_index += 1;
}
if run == remaining {
break;
}
}
if run < remaining {
let rest = r.bits(J[st.run_index])? as usize;
run += rest;
if run > remaining {
return Err(bad("a run passes the end of the line"));
}
}
for v in &mut cur[x + 1..x + 1 + run] {
*v = ra;
}
if run == remaining {
return Ok(w);
}
// The sample that ended the run: its own Rb, the sample above it.
let rb = prev[x + run + 1];
let ri_type = i32::from((ra - rb).abs() <= p.near);
let q = RI_BASE + ri_type as usize;
let k = run_k(st, q, ri_type);
let limit = p.limit - J[st.run_index] - 1;
let em = r.golomb(k, limit, p.qbpp)?;
let tmp = em + ri_type;
let cond = k == 0 && 2 * st.nn[ri_type as usize] < st.n[q];
let map = tmp & 1;
let mag = (tmp + map) / 2;
let e = if (cond && map == 1) || (!cond && map == 0) { mag } else { -mag };
let (px, sign) = if ri_type == 1 { (ra, 1) } else { (rb, if ra > rb { -1 } else { 1 }) };
// The decoder holds the sign-adjusted error; the sign flips the reconstruction.
let rx = reconstruct(px, sign, e, p);
update_run(st, q, e, em, ri_type, p);
if st.run_index > 0 {
st.run_index -= 1;
}
cur[x + run + 1] = rx;
Ok(x + run + 1)
}
// ---- encoder ----
struct BitWriter {
out: Vec<u8>,
cur: u32,
used: u32,
cap: u32,
}
impl BitWriter {
fn new() -> Self {
BitWriter { out: Vec::new(), cur: 0, used: 0, cap: 8 }
}
fn put(&mut self, value: u32, n: u32) {
for i in (0..n).rev() {
// A run of more than 32 zero bits (a long escape code) has no bits of `value` to shift out.
let bit = if i < 32 { (value >> i) & 1 } else { 0 };
self.cur = self.cur << 1 | bit;
self.used += 1;
if self.used == self.cap {
let byte = self.cur as u8;
self.out.push(byte);
self.cap = if byte == 0xFF { 7 } else { 8 };
self.cur = 0;
self.used = 0;
}
}
}
/// Pad the last byte with zero bits; a closing 0xFF is followed by a zero byte.
fn finish(mut self) -> Vec<u8> {
if self.used > 0 {
let pad = self.cap - self.used;
self.put(0, pad);
}
if self.out.last() == Some(&0xFF) {
self.out.push(0);
}
self.out
}
fn golomb(&mut self, m: i32, k: u32, limit: u32, qbpp: u32) {
let m = m as u32;
let max_zeros = limit - qbpp - 1;
let q = m >> k;
if q < max_zeros {
self.put(0, q);
self.put(1, 1);
self.put(m & ((1 << k) - 1), k);
} else {
self.put(0, max_zeros);
self.put(1, 1);
self.put(m - 1, qbpp);
}
}
}
fn encode_scan(plane: &[u16], w: usize, h: usize, p: &Params) -> Vec<u8> {
let mut wr = BitWriter::new();
let mut st = State::new(p);
let mut prev = vec![0i32; w + 2];
let mut cur = vec![0i32; w + 2];
for y in 0..h {
cur[0] = prev[1];
if y > 0 {
prev[w + 1] = prev[w];
}
let line = &plane[y * w..(y + 1) * w];
let mut x = 0usize;
while x < w {
let (ra, rb, rc, rd) = (cur[x], prev[x + 1], prev[x], if y == 0 { 0 } else { prev[x + 2] });
let ix = i32::from(line[x]);
let (q1, q2, q3) = (quantise_gradient(rd - rb, p), quantise_gradient(rb - rc, p), quantise_gradient(rc - ra, p));
if q1 == 0 && q2 == 0 && q3 == 0 {
x = encode_run(&mut wr, &mut st, p, line, &prev, &mut cur, ra, x);
continue;
}
let (sign, q1, q2, q3) = if q1 < 0 || (q1 == 0 && (q2 < 0 || (q2 == 0 && q3 < 0))) { (-1, -q1, -q2, -q3) } else { (1, q1, q2, q3) };
let q = (q1 * 81 + (q2 + 4) * 9 + (q3 + 4)) as usize;
let px = (predict(ra, rb, rc) + sign * st.c[q]).clamp(0, p.maxval);
let e = reduce(sign * (ix - px), p);
let k = golomb_k(st.n[q], st.a[q]);
let swap = p.near == 0 && k == 0 && 2 * st.b[q] <= -st.n[q];
let m = if swap {
if e >= 0 { 2 * e + 1 } else { -2 * (e + 1) }
} else if e >= 0 {
2 * e
} else {
-2 * e - 1
};
wr.golomb(m, k, p.limit, p.qbpp);
cur[x + 1] = reconstruct(px, sign, e, p);
update_regular(&mut st, q, e, p);
x += 1;
}
std::mem::swap(&mut prev, &mut cur);
}
wr.finish()
}
#[allow(clippy::too_many_arguments)]
fn encode_run(wr: &mut BitWriter, st: &mut State, p: &Params, line: &[u16], prev: &[i32], cur: &mut [i32], ra: i32, x: usize) -> usize {
let w = line.len();
let mut run = 0usize;
while x + run < w && (i32::from(line[x + run]) - ra).abs() <= p.near {
cur[x + run + 1] = ra;
run += 1;
}
let mut count = run;
while count >= 1usize << J[st.run_index] {
wr.put(1, 1);
count -= 1usize << J[st.run_index];
if st.run_index < 31 {
st.run_index += 1;
}
}
if x + run == w {
if count > 0 {
wr.put(1, 1);
}
return w;
}
wr.put(0, 1);
wr.put(count as u32, J[st.run_index]);
let ix = i32::from(line[x + run]);
let rb = prev[x + run + 1];
let ri_type = i32::from((ra - rb).abs() <= p.near);
let q = RI_BASE + ri_type as usize;
let (px, sign) = if ri_type == 1 { (ra, 1) } else { (rb, if ra > rb { -1 } else { 1 }) };
let e = reduce(sign * (ix - px), p);
let k = run_k(st, q, ri_type);
let n_neg = st.nn[ri_type as usize];
let map = i32::from((k == 0 && e > 0 && 2 * n_neg < st.n[q]) || (e < 0 && 2 * n_neg >= st.n[q]) || (e < 0 && k != 0));
let em = 2 * e.abs() - ri_type - map;
wr.golomb(em, k, p.limit - J[st.run_index] - 1, p.qbpp);
cur[x + run + 1] = reconstruct(px, sign, e, p);
update_run(st, q, e, em, ri_type, p);
if st.run_index > 0 {
st.run_index -= 1;
}
x + run + 1
}
/// Writes a lossless JPEG-LS file: 8 or 16 bits, gray or RGB, one scan per component.
pub(crate) fn encode(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
let enc = |msg: String| CodecError::encode(F, msg);
let img = src.converted(plan.layout, plan.sample);
let nc = match img.layout() {
ChannelLayout::Gray => 1,
ChannelLayout::Rgb => 3,
l => return Err(enc(format!("no JPEG-LS layout for {l:?}"))),
};
let precision: u32 = match img.sample_type() {
SampleType::U8 => 8,
SampleType::U16 => 16,
s => return Err(enc(format!("JPEG-LS stores 8 or 16 bits per sample, not {s:?}"))),
};
let (w, h) = img.dimensions();
let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else {
return Err(enc(format!("dimensions exceed 65535x65535: {w}x{h}")));
};
let (wu, hu) = (w as usize, h as usize);
let data = img.data();
if data.len() != wu * hu * nc * (precision as usize / 8) {
return Err(enc("the image has the wrong number of samples".into()));
}
let params = Params::new(precision, 0, [0; 5]).map_err(|_| enc("invalid coding parameters".into()))?;
let mut out = vec![0xFF, 0xD8, 0xFF, SOF55];
out.extend_from_slice(&((8 + 3 * nc) as u16).to_be_bytes());
out.push(precision as u8);
out.extend_from_slice(&h16.to_be_bytes());
out.extend_from_slice(&w16.to_be_bytes());
out.push(nc as u8);
for c in 0..nc {
out.extend_from_slice(&[c as u8 + 1, 0x11, 0]);
}
let mut plane = vec![0u16; wu * hu];
for c in 0..nc {
for (i, v) in plane.iter_mut().enumerate() {
let at = i * nc + c;
*v = if precision == 8 { u16::from(data[at]) } else { u16::from_ne_bytes([data[2 * at], data[2 * at + 1]]) };
}
out.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, c as u8 + 1, 0, 0, 0, 0]);
out.extend_from_slice(&encode_scan(&plane, wu, hu, &params));
}
out.extend_from_slice(&[0xFF, 0xD9]);
Ok(out)
}
struct Frame {
precision: u32,
width: usize,
height: usize,
components: Vec<u8>,
}
fn be16(b: &[u8], i: usize) -> Result<usize, CodecError> {
b.get(i..i + 2).map(|s| usize::from(u16::from_be_bytes([s[0], s[1]]))).ok_or_else(|| bad("truncated header"))
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if !looks_like_jpegls(b) {
return Err(bad("not a JPEG-LS file"));
}
let mut pos = 2;
let mut frame: Option<Frame> = None;
let mut preset = [0i32; 5];
let mut planes: Vec<Vec<u16>> = Vec::new();
let mut scans_done = 0;
loop {
// Find the next marker.
while b.get(pos) != Some(&0xFF) {
if pos >= b.len() {
return finish(frame, planes, scans_done);
}
pos += 1;
}
while b.get(pos + 1) == Some(&0xFF) {
pos += 1;
}
let Some(&marker) = b.get(pos + 1) else { return finish(frame, planes, scans_done) };
if marker == 0xD9 {
return finish(frame, planes, scans_done);
}
if marker == 0x00 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 {
pos += 2;
continue;
}
let len = be16(b, pos + 2)?;
let seg = b.get(pos + 4..pos + 2 + len).ok_or_else(|| bad("truncated marker segment"))?;
match marker {
SOF55 => {
if frame.is_some() || seg.len() < 6 {
return Err(bad("bad or repeated frame header"));
}
let (precision, height, width, nf) = (u32::from(seg[0]), be16(seg, 1)?, be16(seg, 3)?, usize::from(seg[5]));
if !(2..=16).contains(&precision) || width == 0 || height == 0 || !matches!(nf, 1 | 3) || seg.len() < 6 + 3 * nf {
return Err(bad("unsupported frame header"));
}
if height == 0 {
return Err(CodecError::unsupported(F, "a frame height set later by a DNL marker"));
}
let layout = if nf == 1 { ChannelLayout::Gray } else { ChannelLayout::Rgb };
let sample = if precision <= 8 { SampleType::U8 } else { SampleType::U16 };
limits.check(width as u32, height as u32, layout, sample)?;
let components = (0..nf).map(|i| seg[6 + 3 * i]).collect();
frame = Some(Frame { precision, width, height, components });
}
LSE => {
if seg.first() == Some(&1) && seg.len() >= 11 {
for (i, p) in preset.iter_mut().enumerate() {
*p = be16(seg, 1 + 2 * i)? as i32;
}
} else if seg.first() != Some(&1) {
return Err(CodecError::unsupported(F, "mapping tables and colour transforms (LSE)"));
}
}
0xDA => {
let f = frame.as_ref().ok_or_else(|| bad("a scan before the frame header"))?;
let ns = usize::from(*seg.first().ok_or_else(|| bad("empty scan header"))?);
if seg.len() < 1 + 2 * ns + 3 || ns != 1 {
return Err(CodecError::unsupported(F, "interleaved scans (ILV 1 and 2)"));
}
let (cs, tm, near, ilv) = (seg[1], seg[2], i32::from(seg[3]), seg[4]);
if tm != 0 {
return Err(CodecError::unsupported(F, "mapping tables"));
}
if ilv != 0 && f.components.len() > 1 {
return Err(CodecError::unsupported(F, "interleaved scans (ILV 1 and 2)"));
}
if seg[5] & 0x0f != 0 {
return Err(CodecError::unsupported(F, "a point transform"));
}
let idx = f.components.iter().position(|&c| c == cs).ok_or_else(|| bad("a scan names an unknown component"))?;
if planes.len() <= idx {
planes.resize(f.components.len(), Vec::new());
}
if !planes[idx].is_empty() {
return Err(bad("a component is coded twice"));
}
let params = Params::new(f.precision, near, preset)?;
let start = pos + 2 + len;
// The scan ends at the next real marker: an 0xFF followed by a byte with its top bit set.
let mut end = start;
while end < b.len() {
if b[end] == 0xFF && b.get(end + 1).is_none_or(|&n| n & 0x80 != 0) {
break;
}
end += 1;
}
let mut samples = Vec::new();
samples.try_reserve_exact(f.width * f.height).map_err(|_| bad("pixel buffer is too large"))?;
samples.resize(f.width * f.height, 0u16);
decode_scan(&b[start..end], f.width, f.height, &params, &mut samples)?;
planes[idx] = samples;
scans_done += 1;
pos = end;
continue;
}
_ => {}
}
pos += 2 + len;
}
}
fn finish(frame: Option<Frame>, planes: Vec<Vec<u16>>, scans: usize) -> Result<Image, CodecError> {
let f = frame.ok_or_else(|| bad("no frame header"))?;
if scans != f.components.len() || planes.iter().any(Vec::is_empty) || planes.len() != f.components.len() {
return Err(bad("the file ends before every component is coded"));
}
let (w, h, nc) = (f.width, f.height, f.components.len());
let layout = if nc == 1 { ChannelLayout::Gray } else { ChannelLayout::Rgb };
let p = f.precision;
let scale = |v: u16| -> u16 {
if p == 16 {
v
} else {
let v = u32::from(v);
((v << (16 - p)) | (v >> (2 * p).saturating_sub(16).min(15))) as u16
}
};
if p <= 8 {
let to8 = |v: u16| -> u8 { if p == 8 { v as u8 } else { (scale(v) >> 8) as u8 } };
let mut data = Vec::with_capacity(w * h * nc);
for i in 0..w * h {
for plane in &planes {
data.push(to8(plane[i]));
}
}
Image::from_u8(w as u32, h as u32, layout, data)
} else {
let mut data = Vec::with_capacity(w * h * nc);
for i in 0..w * h {
for plane in &planes {
data.push(scale(plane[i]));
}
}
Image::from_u16(w as u32, h as u32, layout, &data)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn noise_u16(n: usize, seed: &mut u32, mask: u32) -> Vec<u16> {
(0..n)
.map(|_| {
*seed ^= *seed << 13;
*seed ^= *seed >> 17;
*seed ^= *seed << 5;
(*seed & mask) as u16
})
.collect()
}
fn image(w: u32, h: u32, layout: ChannelLayout, wide: bool, kind: u8) -> Image {
let n = w as usize * h as usize * layout.channels();
let mut seed = 0x1234_5679u32;
let samples: Vec<u16> = match kind {
0 => noise_u16(n, &mut seed, if wide { 0xFFFF } else { 0xFF }),
1 => vec![if wide { 40_000 } else { 100 }; n],
_ => (0..n).map(|i| if wide { ((i / 3) * 37 % 65_536) as u16 } else { ((i / 3) % 251) as u16 }).collect(),
};
if wide {
Image::from_u16(w, h, layout, &samples).unwrap()
} else {
Image::from_u8(w, h, layout, samples.iter().map(|&v| v as u8).collect()).unwrap()
}
}
fn plan(img: &Image) -> Plan {
Plan { layout: img.layout(), sample: img.sample_type() }
}
#[test]
fn round_trips_noise_flat_and_smooth_images() {
for layout in [ChannelLayout::Gray, ChannelLayout::Rgb] {
for wide in [false, true] {
for kind in 0..3 {
// Widths around the run-length steps, and single-pixel dimensions.
for (w, h) in [(1, 1), (1, 9), (2, 3), (7, 5), (33, 4), (300, 3), (1025, 2)] {
let img = image(w, h, layout, wide, kind);
let bytes = encode(&img, plan(&img)).unwrap();
assert!(looks_like_jpegls(&bytes));
let back = decode(&bytes, &Limits::default()).unwrap_or_else(|e| panic!("{layout:?} wide={wide} kind={kind} {w}x{h}: {e}"));
assert_eq!(back.data(), img.data(), "{layout:?} wide={wide} kind={kind} {w}x{h}");
}
}
}
}
}
#[test]
fn default_parameters_follow_the_standard() {
let p = Params::new(8, 0, [0; 5]).unwrap();
assert_eq!((p.maxval, p.range, p.qbpp, p.limit, p.t1, p.t2, p.t3, p.reset), (255, 256, 8, 32, 3, 7, 21, 64));
let p = Params::new(16, 0, [0; 5]).unwrap();
assert_eq!((p.maxval, p.range, p.qbpp, p.limit, p.t1, p.t2, p.t3), (65535, 65536, 16, 64, 18, 67, 276));
let p = Params::new(8, 3, [0; 5]).unwrap();
assert_eq!((p.range, p.t1), (38, 12)); // RANGE = floor((255 + 2 * 3) / 7) + 1
assert!(Params::new(8, 200, [0; 5]).is_err());
}
#[test]
fn detection_tells_jpeg_ls_from_jpeg() {
let img = image(4, 4, ChannelLayout::Gray, false, 0);
let ls = encode(&img, plan(&img)).unwrap();
assert!(looks_like_jpegls(&ls));
// A baseline JPEG starts SOI, APP0 / DQT, ... SOF0: not JPEG-LS.
assert!(!looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xC0, 0, 11]));
assert!(!looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11]));
// SOF55 after an application segment still is.
assert!(looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xE8, 0, 4, 0, 0, 0xFF, 0xF7, 0, 11]));
assert!(!looks_like_jpegls(&[0xFF, 0xD8]));
}
#[test]
fn interleaved_scans_and_tables_are_unsupported() {
let img = image(8, 4, ChannelLayout::Rgb, false, 2);
let good = encode(&img, plan(&img)).unwrap();
// The scan header body is Ns, Cs, Tm, NEAR, ILV, point transform, after the marker and length.
let sos = good.windows(2).position(|w| w == [0xFF, 0xDA]).unwrap() + 4;
let mut line = good.clone();
line[sos + 4] = 1;
assert!(matches!(decode(&line, &Limits::default()), Err(CodecError::Unsupported { .. })));
let mut mapped = good.clone();
mapped[sos + 2] = 1;
assert!(matches!(decode(&mapped, &Limits::default()), Err(CodecError::Unsupported { .. })));
let mut transform = good;
transform[sos + 5] = 1;
assert!(matches!(decode(&transform, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
#[test]
fn hostile_input_is_an_error_not_a_panic() {
for (w, h, layout, wide) in [(23, 9, ChannelLayout::Gray, false), (11, 7, ChannelLayout::Rgb, true)] {
let img = image(w, h, layout, wide, 0);
let good = encode(&img, plan(&img)).unwrap();
for len in 0..good.len() {
let _ = decode(&good[..len], &Limits::default());
}
// Flip every bit of the entropy-coded data and of the headers, one at a time.
for i in 0..good.len() {
for bit in 0..8 {
let mut c = good.clone();
c[i] ^= 1 << bit;
let _ = decode(&c, &Limits::default());
}
}
}
// A huge frame is stopped by the limits or the length check before any allocation.
let img = image(4, 4, ChannelLayout::Gray, false, 0);
let mut huge = encode(&img, plan(&img)).unwrap();
let sof = huge.windows(2).position(|w| w == [0xFF, 0xF7]).unwrap();
huge[sof + 5..sof + 9].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]);
assert!(decode(&huge, &Limits::default()).is_err());
}
}
+58 -7
View File
@@ -141,12 +141,12 @@ impl<B: AsRef<[u8]>> PngDecodeRows<B> {
/// 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, CodecError> {
Self::open(bytes, limits, true)
Self::open(bytes, limits, true, false)
}
/// [`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<Self, CodecError> {
fn open(bytes: B, limits: &Limits, reserve: bool, bounded_stream: bool) -> Result<Self, CodecError> {
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() };
@@ -187,7 +187,14 @@ impl<B: AsRef<[u8]>> PngDecodeRows<B> {
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)?;
if bounded_stream {
limits.check_size(width, height)?;
if line > limits.alloc_usize() {
return Err(CodecError::LimitExceeded("one PNG row exceeds max_alloc".into()));
}
} else {
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"));
@@ -234,11 +241,35 @@ impl<B: AsRef<[u8]>> PngDecodeRows<B> {
/// 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, CodecError> {
Self::open(bytes, limits, false)
Self::open(bytes, limits, false, false)
}
/// Opens a bounded row stream without charging a full image that is never retained.
/// Dimension, pixel-count, plausibility, decoder and metadata limits remain unchanged;
/// `max_alloc` also bounds each row group. It is not a total process-memory limit.
/// Unlike [`Self::new_streaming`], a streamable image may exceed the full-image budget.
/// Non-streamable images still require that budget, as do [`Self::step`] and
/// [`Self::reserve_output`]. The caller must bound any output it retains in its sink.
pub fn new_bounded_streaming(bytes: B, limits: &Limits) -> Result<Self, CodecError> {
let decoder = Self::open(bytes, limits, false, true)?;
if !decoder.streamable() {
decoder.check_full_output()?;
}
Ok(decoder)
}
fn check_full_output(&self) -> Result<(), CodecError> {
let height = u32::try_from(self.height).map_err(|_| err("image too large"))?;
self.limits.check(self.width, height, self.layout, self.sample)?;
if self.size > self.limits.alloc_usize() {
return Err(CodecError::LimitExceeded("PNG output exceeds max_alloc".into()));
}
Ok(())
}
/// Reserves the buffer [`PngDecodeRows::step`] decodes the whole image into.
pub fn reserve_output(&mut self) -> Result<(), CodecError> {
self.check_full_output()?;
let size = self.size;
self.buf.try_reserve_exact(size).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {size} bytes for the image")))
}
@@ -271,6 +302,7 @@ impl<B: AsRef<[u8]>> PngDecodeRows<B> {
/// 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<Option<Image>, CodecError> {
self.check_full_output()?;
if self.done {
return Err(err("the image was already decoded"));
}
@@ -338,7 +370,9 @@ impl<B: AsRef<[u8]>> PngDecodeRows<B> {
/// `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.
/// `eXIf` after the image data may set one) is for the caller to apply. A new group size
/// must exceed any partial group retained from an earlier yield; otherwise this returns
/// an error without consuming another row.
pub fn step_stream<E: From<CodecError>>(
&mut self,
group: u32,
@@ -348,12 +382,29 @@ impl<B: AsRef<[u8]>> PngDecodeRows<B> {
if self.done {
return Err(err("the image was already decoded").into());
}
let group = usize::try_from(group.max(1)).unwrap_or(usize::MAX);
// Tail EXIF may report an orientation after rows have already been delivered;
// that belongs in the returned extras, not a rejection of metadata continuation.
if self.rows == 0 && !self.tail && !self.streamable() {
return Err(err("this PNG requires whole-image decoding").into());
}
let group = usize::try_from(group.max(1)).unwrap_or(usize::MAX).min(self.height);
if self.rows - self.group_start >= group {
return Err(err("PNG row group is smaller than rows held from the previous step").into());
}
let group_bytes = group.checked_mul(self.line).ok_or_else(|| err("PNG row group is too large"))?;
if group_bytes > self.limits.alloc_usize() {
return Err(CodecError::LimitExceeded("PNG row group exceeds max_alloc".into()).into());
}
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"))?;
// A caller may change its group size between yields. Bound the actual held
// bytes as well as the requested group before growing the row buffer.
if end > self.limits.alloc_usize() {
return Err(CodecError::LimitExceeded("PNG row group exceeds max_alloc".into()).into());
}
if self.buf.len() < end {
self.buf
.try_reserve_exact(end - self.buf.len())
@@ -368,7 +419,7 @@ impl<B: AsRef<[u8]>> PngDecodeRows<B> {
to_native_endian(row);
}
self.rows += 1;
if self.rows - self.group_start == group || self.rows == self.height {
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;
+12 -2
View File
@@ -44,6 +44,8 @@ pub enum Format {
Iff,
/// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`].
Jp2,
/// JPEG-LS (`.jls`): lossless and near-lossless gray and RGB.
JpegLs,
}
/// What a format can hold **and** what this crate reads/writes for it.
@@ -113,7 +115,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::
impl Format {
/// Every format known to the crate (enabled or not).
pub const ALL: [Format; 24] = [
pub const ALL: [Format; 25] = [
Format::Png,
Format::Jpeg,
Format::Jp2,
@@ -138,6 +140,7 @@ impl Format {
Format::Cineon,
Format::Dicom,
Format::Iff,
Format::JpegLs,
];
pub fn caps(self) -> FormatCaps {
@@ -176,6 +179,7 @@ impl Format {
Format::Dicom => "DICOM",
Format::Iff => "Amiga IFF",
Format::Jp2 => "JPEG 2000",
Format::JpegLs => "JPEG-LS",
}
}
@@ -207,6 +211,7 @@ impl Format {
Format::Dicom => &["dcm", "dicom", "dc3"],
Format::Iff => &["iff", "lbm", "ilbm"],
Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"],
Format::JpegLs => &["jls"],
}
}
@@ -236,13 +241,14 @@ impl Format {
Format::Dicom => "application/dicom",
Format::Iff => "image/x-iff",
Format::Jp2 => "image/jp2",
Format::JpegLs => "image/jls",
}
}
/// 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::Jpeg | Format::Gif | Format::Iff | Format::JpegLs => Some((65535, 65535)),
Format::WebP => Some((16384, 16384)),
Format::Ico => Some((256, 256)),
_ => None,
@@ -310,6 +316,7 @@ pub fn caps(format: Format) -> FormatCaps {
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 },
Format::JpegLs => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base },
Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base },
}
}
@@ -321,6 +328,9 @@ pub fn detect(bytes: &[u8]) -> Option<Format> {
if b.starts_with(b"\x89PNG\r\n\x1a\n") {
return Some(Format::Png);
}
if crate::codecs::jpegls::looks_like_jpegls(b) {
return Some(Format::JpegLs);
}
if b.starts_with(&[0xFF, 0xD8, 0xFF]) {
return Some(Format::Jpeg);
}
+5 -1
View File
@@ -16,6 +16,7 @@
mod allocation;
pub mod apng;
pub mod bmp_probe;
pub mod cicp;
mod codecs;
mod error;
@@ -28,6 +29,7 @@ mod inspection;
mod lab;
mod options;
pub mod orientation;
pub mod png_probe;
pub mod resolution;
pub mod source_settings;
pub mod web;
@@ -63,7 +65,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_
pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding};
pub use half::f16;
use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm};
use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm};
/// Detect the format and decode with default [`Limits`].
pub fn decode(bytes: &[u8]) -> Result<Image, CodecError> {
@@ -102,6 +104,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res
Format::Cineon => dpx::decode_cineon(bytes, l),
Format::Dicom => dicom::decode(bytes, l),
Format::Iff => iff::decode(bytes, l),
Format::JpegLs => jpegls::decode(bytes, l),
Format::OpenExr => exr::decode(bytes, l),
Format::Heif => heif::decode(bytes, l, opts.keep_orientation),
Format::Jp2 => jp2::decode(bytes, l),
@@ -458,6 +461,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result<Vec
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::JpegLs => jpegls::encode(image, plan),
Format::OpenExr => exr::encode(image, plan, opts),
Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")),
Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")),
+190
View File
@@ -0,0 +1,190 @@
//! Header-only PNG inspection without inflating image data.
use crate::{CodecError, Format};
fn err(message: &str) -> CodecError {
CodecError::malformed(Format::Png, message)
}
/// Dimensions declared in a PNG IHDR prefix, without checking the rest of the file.
/// Useful only for an early allocation limit; use [`header`] for structural inspection.
pub fn declared_dimensions(b: &[u8]) -> Option<(u32, u32)> {
if !b.starts_with(b"\x89PNG\r\n\x1a\n") || b.get(12..16) != Some(b"IHDR") {
return None;
}
let w = u32::from_be_bytes(b.get(16..20)?.try_into().ok()?);
let h = u32::from_be_bytes(b.get(20..24)?.try_into().ok()?);
Some((w, h))
}
/// Stored dimensions and EXIF orientation. This does not validate CRCs or compressed pixels.
pub fn header(b: &[u8]) -> Result<(u32, u32, u16), CodecError> {
header_with_max_pixels(b, u64::MAX)
}
/// As [`header`], refusing images over `max_pixels` before scanning their chunks.
pub fn header_with_max_pixels(b: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), CodecError> {
if !b.starts_with(b"\x89PNG\r\n\x1a\n") {
return Err(err("bad signature"));
}
let be32 = |p: usize| b.get(p..p.checked_add(4)?).map(|v| u32::from_be_bytes([v[0], v[1], v[2], v[3]]));
if be32(8) != Some(13) || b.get(12..16) != Some(b"IHDR") {
return Err(err("missing IHDR"));
}
let ihdr = b.get(16..29).ok_or_else(|| err("truncated IHDR"))?;
let (w, h) = (u32::from_be_bytes([ihdr[0], ihdr[1], ihdr[2], ihdr[3]]), u32::from_be_bytes([ihdr[4], ihdr[5], ihdr[6], ihdr[7]]));
let (depth, color, compression, filter, interlace) = (ihdr[8], ihdr[9], ihdr[10], ihdr[11], ihdr[12]);
if w > i32::MAX as u32 || h > i32::MAX as u32 {
return Err(err("dimensions out of range"));
}
if w == 0 || h == 0 {
return Err(err("zero dimension"));
}
if u64::from(w) * u64::from(h) > max_pixels {
return Err(CodecError::LimitExceeded("PNG pixel count".into()));
}
let depth_ok = match color {
0 => matches!(depth, 1 | 2 | 4 | 8 | 16),
3 => matches!(depth, 1 | 2 | 4 | 8),
2 | 4 | 6 => matches!(depth, 8 | 16),
_ => false,
};
if !depth_ok || compression != 0 || filter != 0 || interlace > 1 {
return Err(err("invalid IHDR"));
}
let (mut p, mut palette, mut idat, mut after_idat, mut exif) = (8usize, false, false, false, None);
while let Some(len) = be32(p) {
let Some(kind) = p.checked_add(4).and_then(|k| b.get(k..k.checked_add(4)?)) else { break };
if idat && kind != b"IDAT" {
// a chunk follows the image data: the IDAT run is complete
after_idat = true;
}
if kind == b"IEND" {
break;
}
let start = p.saturating_add(8);
let data = (len <= i32::MAX as u32).then(|| start.checked_add(len as usize).and_then(|end| b.get(start..end))).flatten();
let Some(data) = data else {
if after_idat {
break; // a damaged chunk after the image data: a decode ignores it too
}
return Err(err("truncated"));
};
match kind {
b"PLTE" => palette = true,
b"IDAT" if after_idat => {} // a stray IDAT after other chunks: the first run is the image
b"IDAT" => {
if color == 3 && !palette {
return Err(err("indexed image without a palette"));
}
idat = true;
}
b"eXIf" if exif.is_none() => exif = Some(data.strip_prefix(b"Exif\0\0").unwrap_or(data)),
_ => {}
}
p = start.saturating_add(data.len()).saturating_add(4);
}
if !idat {
return Err(err("no image data"));
}
if !after_idat {
return Err(err("truncated image data"));
}
Ok((w, h, exif.map_or(1, crate::exif_orientation)))
}
/// Container color declarations preceding image data. Raw values leave color policy to callers.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ColorChunks {
pub srgb: bool,
pub gamma: Option<f32>,
pub chromaticities: Option<[f64; 8]>,
}
/// Read bounded color chunks before image data; malformed tails are ignored.
pub fn color_chunks(b: &[u8]) -> Option<ColorChunks> {
if !b.starts_with(b"\x89PNG\r\n\x1a\n") {
return None;
}
let mut result = ColorChunks::default();
let mut p = 8usize;
while let Some(length) = b.get(p..p.checked_add(4)?) {
let len = u32::from_be_bytes(length.try_into().ok()?) as usize;
let start = p.checked_add(8)?;
let Some(kind) = b.get(p.checked_add(4)?..start) else { break };
let Some(payload) = start.checked_add(len).and_then(|end| b.get(start..end)) else { break };
match kind {
b"sRGB" => result.srgb = true,
b"gAMA" if payload.len() == 4 => {
let v = u32::from_be_bytes(payload.try_into().ok()?) as f32 / 100_000.0;
if v > 0.0 && v.is_finite() {
result.gamma = Some(v);
}
}
b"cHRM" if payload.len() == 32 => {
let mut vals = [0.0; 8];
for (i, dst) in vals.iter_mut().enumerate() {
let raw = i32::from_be_bytes(payload[i * 4..i * 4 + 4].try_into().ok()?);
*dst = f64::from(raw) / 100_000.0;
}
result.chromaticities = Some(vals);
}
b"IDAT" | b"IEND" => break,
_ => {}
}
let Some(next) = start.checked_add(len).and_then(|end| end.checked_add(4)) else { break };
p = next;
}
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
fn chunk(out: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) {
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
out.extend_from_slice(kind);
out.extend_from_slice(data);
out.extend_from_slice(&[0; 4]); // Header inspection deliberately does not validate CRCs.
}
fn png(color: u8) -> Vec<u8> {
let mut bytes = b"\x89PNG\r\n\x1a\n".to_vec();
let mut ihdr = Vec::new();
ihdr.extend_from_slice(&2u32.to_be_bytes());
ihdr.extend_from_slice(&3u32.to_be_bytes());
ihdr.extend_from_slice(&[8, color, 0, 0, 0]);
chunk(&mut bytes, b"IHDR", &ihdr);
bytes
}
#[test]
fn header_checks_structure_and_reads_late_orientation_without_crc_validation() {
let mut bytes = png(2);
chunk(&mut bytes, b"IDAT", &[1]);
let exif = b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00";
chunk(&mut bytes, b"eXIf", exif);
chunk(&mut bytes, b"IEND", &[]);
assert_eq!(header(&bytes).ok(), Some((2, 3, 6)));
assert!(matches!(header_with_max_pixels(&bytes, 5), Err(CodecError::LimitExceeded(_))));
assert_eq!(declared_dimensions(&bytes), Some((2, 3)));
assert!(header(&bytes[..34]).is_err());
let mut indexed = png(3);
chunk(&mut indexed, b"IDAT", &[1]);
chunk(&mut indexed, b"IEND", &[]);
assert!(header(&indexed).is_err());
indexed[25] = 7;
assert!(header(&indexed).is_err());
}
#[test]
fn reads_color_chunks_only_before_image_data() {
let mut bytes = png(2);
chunk(&mut bytes, b"gAMA", &45455u32.to_be_bytes());
chunk(&mut bytes, b"IDAT", &[1]);
chunk(&mut bytes, b"sRGB", &[0]);
let color = color_chunks(&bytes).unwrap();
assert_eq!(color.gamma, Some(0.45455));
assert!(!color.srgb);
assert!(color_chunks(b"not png").is_none());
}
}
@@ -62,6 +62,7 @@ fn default_build_formats_all_symmetric() {
Format::Dpx,
Format::Cineon,
Format::Iff,
Format::JpegLs,
];
for f in expected {
assert!(caps(f).read && caps(f).write, "{f:?}");
@@ -218,6 +219,21 @@ fn detect_magic_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_jpeg_ls() {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
let img = Image::from_u8(5, 3, ChannelLayout::Gray, (0..15).collect()).unwrap();
let bytes = encode(&img, Format::JpegLs, &EncodeOptions::default()).unwrap();
assert_eq!(detect(&bytes), Some(Format::JpegLs));
assert_eq!(from_extension("scan.JLS"), Some(Format::JpegLs));
assert_eq!(decode(&bytes).unwrap().data(), img.data());
assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::JpegLs));
assert_eq!(Format::JpegLs.max_dimensions(), Some((65535, 65535)));
// Plain JPEG still is JPEG: SOI, then a baseline frame header.
assert_eq!(detect(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11]), Some(Format::Jpeg));
assert!(decode_as(Format::JpegLs, &[0xFF, 0xD8, 0xFF, 0xF7]).is_err());
}
#[test]
fn detect_magic_avif() {
assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif));
@@ -0,0 +1,103 @@
//! JPEG-LS against ffmpeg as the reference (oracle; skipped when ffmpeg is absent, a failure with
//! `CRAFT_MEDIA_REQUIRE_ORACLES=1`):
//! - files ffmpeg's encoder writes (8- and 16-bit gray: smooth, noisy and flat images, which
//! exercise the regular, run and escape-coded paths) decode to the pixels ffmpeg decodes;
//! - files our encoder writes (gray 8 and 16 bits, RGB 8 bits) decode in ffmpeg to our pixels (ffmpeg
//! cannot decode 16-bit RGB JPEG-LS, so that case is only round-tripped through our own decoder);
//! - ffmpeg writes RGB line-interleaved, which is reported as unsupported.
use craft_image_codecs::{ChannelLayout, CodecError, EncodeOptions, Format, Image, SampleType, decode, encode};
use std::path::Path;
use std::process::Command;
const W: u32 = 66;
const H: u32 = 40;
fn run(ffmpeg: &Path, args: &[&str]) -> Vec<u8> {
let out = Command::new(ffmpeg).args(["-v", "error", "-y"]).args(args).output().expect("run ffmpeg");
assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr));
out.stdout
}
/// `(name, lavfi source)`: smooth, noisy and flat pictures of W x H.
fn sources() -> Vec<(&'static str, String)> {
let s = format!("{W}x{H}");
vec![
("testsrc", format!("testsrc2=size={s}:rate=1")),
("noise", format!("nullsrc=size={s}:rate=1,geq=lum='random(1)*255':cb=128:cr=128")),
("flat", format!("color=c=0x336699:size={s}:rate=1")),
("gradient", format!("gradients=size={s}:rate=1:seed=7")),
]
}
#[test]
fn ffmpeg_files_decode_to_ffmpegs_pixels() {
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("jpegls");
for (name, src) in sources() {
for (fmt, ext_bits) in [("gray", 8), ("gray16le", 16)] {
let file = dir.join(format!("ffmpeg_{name}_{ext_bits}.jls"));
let f = file.to_str().expect("path");
run(&ffmpeg, &["-f", "lavfi", "-i", &src, "-frames:v", "1", "-pix_fmt", fmt, f]);
let theirs = run(&ffmpeg, &["-i", f, "-f", "rawvideo", "-pix_fmt", fmt, "-"]);
let ours = decode(&std::fs::read(&file).expect("read")).unwrap_or_else(|e| panic!("{name} {fmt}: {e}"));
assert_eq!((ours.width(), ours.height(), ours.layout()), (W, H, ChannelLayout::Gray), "{name} {fmt}");
let want_sample = if ext_bits == 8 { SampleType::U8 } else { SampleType::U16 };
assert_eq!(ours.sample_type(), want_sample);
let ours_bytes: Vec<u8> = if ext_bits == 8 {
ours.data().to_vec()
} else {
ours.data().as_chunks::<2>().0.iter().flat_map(|b| u16::from_ne_bytes(*b).to_le_bytes()).collect()
};
assert!(ours_bytes == theirs, "{name} {fmt}: pixels differ from ffmpeg's decode");
}
}
}
#[test]
fn ffmpegs_rgb_is_line_interleaved_and_unsupported() {
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("jpegls");
let file = dir.join("ffmpeg_rgb.jls");
run(
&ffmpeg,
&["-f", "lavfi", "-i", &format!("testsrc2=size={W}x{H}:rate=1"), "-frames:v", "1", "-pix_fmt", "rgb24", file.to_str().expect("path")],
);
assert!(matches!(decode(&std::fs::read(&file).expect("read")), Err(CodecError::Unsupported { .. })));
}
#[test]
fn ffmpeg_decodes_our_files_to_our_pixels() {
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("jpegls");
for (name, src) in sources() {
for (layout, fmt8, fmt16) in [(ChannelLayout::Gray, "gray", "gray16le"), (ChannelLayout::Rgb, "rgb24", "rgb48le")] {
for bits in [8, 16] {
let fmt = if bits == 8 { fmt8 } else { fmt16 };
// Source pixels from ffmpeg, so the test image is realistic.
let raw = run(&ffmpeg, &["-f", "lavfi", "-i", &src, "-frames:v", "1", "-pix_fmt", fmt, "-f", "rawvideo", "-"]);
let img = if bits == 8 {
Image::from_u8(W, H, layout, raw).expect("image")
} else {
let samples: Vec<u16> = raw.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)).collect();
Image::from_u16(W, H, layout, &samples).expect("image")
};
let bytes = encode(&img, Format::JpegLs, &EncodeOptions::default()).expect("encode");
assert_eq!(decode(&bytes).expect("our own file decodes").data(), img.data(), "{name} {fmt}: round trip");
let file = dir.join(format!("ours_{name}_{fmt}.jls"));
std::fs::write(&file, &bytes).expect("write");
// ffmpeg's decoder does not implement 16-bit colour ("Not yet implemented").
if layout == ChannelLayout::Rgb && bits == 16 {
continue;
}
let theirs = run(&ffmpeg, &["-i", file.to_str().expect("path"), "-f", "rawvideo", "-pix_fmt", fmt, "-"]);
let ours_bytes: Vec<u8> = if bits == 8 {
img.data().to_vec()
} else {
img.data().as_chunks::<2>().0.iter().flat_map(|b| u16::from_ne_bytes(*b).to_le_bytes()).collect()
};
assert!(theirs == ours_bytes, "{name} {fmt}: ffmpeg decodes our file to other pixels");
}
}
}
}
+1
View File
@@ -14,6 +14,7 @@ mod exr_stream;
mod fidelity;
mod gif_frames;
mod heif;
mod jpegls_oracle;
mod limits;
mod malformed;
mod metadata;
+94
View File
@@ -665,3 +665,97 @@ fn step_stream_gives_the_pixels_and_extras_of_decode() {
fn dec_line(w: u32, layout: ChannelLayout, sample: SampleType) -> usize {
w as usize * layout.channels() * sample.bytes()
}
#[test]
fn bounded_png_stream_does_not_charge_unretained_full_frame() {
let image = synth(256, 1024, ChannelLayout::Rgba, SampleType::U16, 77, 0.5);
let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap();
let limits = Limits { max_alloc: 512 * 1024, ..Limits::default() };
assert!(image.data().len() > limits.max_alloc as usize);
assert!(PngDecodeRows::new(bytes.as_slice(), &limits).is_err());
assert!(PngDecodeRows::new_streaming(bytes.as_slice(), &limits).is_err());
let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &limits).unwrap();
assert!(decoder.streamable());
// Opening a stream must not become a way to bypass whole-image allocation guards.
assert!(decoder.reserve_output().is_err());
assert!(decoder.step(&mut || true).is_err());
let mut next = 0;
let mut sink = |first: u32, rows: &[u8]| -> Result<(), CodecError> {
assert_eq!(first as usize, next);
assert!(rows.len() <= 3 * 256 * 8);
let start = next * 256 * 8;
assert_eq!(rows, &image.data()[start..start + rows.len()]);
next += rows.len() / (256 * 8);
Ok(())
};
while decoder.step_stream(3, &mut || false, &mut sink).unwrap().is_none() {}
assert_eq!(next, 1024);
}
#[test]
fn bounded_png_stream_keeps_group_dimension_and_fallback_limits() {
let image = synth(256, 1024, ChannelLayout::Rgba, SampleType::U16, 81, 0.5);
let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap();
let limits = Limits { max_alloc: 512 * 1024, ..Limits::default() };
let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &limits).unwrap();
assert!(decoder.step_stream::<CodecError>(1024, &mut || true, &mut |_, _| panic!("oversized group reached sink")).is_err());
for strict in [
Limits { max_width: 255, ..limits },
Limits { max_height: 1023, ..limits },
Limits { max_pixels: 256 * 1024 - 1, ..limits },
Limits { max_alloc: 1024, ..limits },
] {
assert!(PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &strict).is_err());
}
let adam7 = interlaced(&image);
assert!(PngDecodeRows::new_bounded_streaming(adam7.as_slice(), &limits).is_err());
}
#[test]
fn bounded_png_stream_rejects_shrinking_below_buffered_rows() {
let image = synth(16, 8, ChannelLayout::Rgba, SampleType::U16, 83, 0.5);
let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap();
let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &Limits::default()).unwrap();
for _ in 0..3 {
assert!(decoder.step_stream::<CodecError>(4, &mut || false, &mut |_, _| panic!("group is incomplete")).unwrap().is_none());
}
assert!(decoder.step_stream::<CodecError>(1, &mut || true, &mut |_, _| panic!("changed group reached sink")).is_err());
let mut output = Vec::new();
decoder
.step_stream::<CodecError>(4, &mut || true, &mut |_, rows| {
output.extend_from_slice(rows);
Ok(())
})
.unwrap();
assert_eq!(output, image.data());
}
#[test]
fn bounded_png_stream_preserves_late_exif_across_metadata_yields() {
let image = synth(16, 8, ChannelLayout::Rgba, SampleType::U16, 89, 0.5);
let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap();
let mut exif = sample_exif();
exif[18..22].copy_from_slice(&6u32.to_le_bytes());
let mut chunks = vec![png_chunk(b"eXIf", &exif)];
for i in 0..150 {
chunks.push(png_chunk(b"tEXt", format!("key{i}\0value").as_bytes()));
}
let bytes = with_chunks_before_iend(&bytes, &chunks);
let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &Limits::default()).unwrap();
assert!(decoder.streamable());
let mut output = Vec::new();
let extras = loop {
if let Some(extras) = decoder
.step_stream::<CodecError>(2, &mut || false, &mut |_, rows| {
output.extend_from_slice(rows);
Ok(())
})
.unwrap()
{
break extras;
}
};
assert_eq!(output, image.data());
assert_eq!(extras.orientation, 6);
assert_eq!(extras.meta.text.len(), 150);
}
+58
View File
@@ -195,3 +195,61 @@ where
let responses: Vec<_> = batch.iter().filter_map(|v| one(v, limits, &mut on_request, &mut on_notification)).collect();
Ok((!responses.is_empty()).then_some(Value::Array(responses)))
}
/// Process a permissive legacy JSON-RPC envelope. This mode accepts arbitrary IDs and
/// parameter shapes, ignores response envelopes, and retains legacy diagnostic messages.
/// The caller supplies dispatch and notification callbacks, and should bound transport input.
pub fn process_legacy<F, N>(value: &Value, mut on_request: F, mut on_notification: N) -> Option<Value>
where
F: FnMut(Request<'_>) -> Result<Value, RpcError>,
N: FnMut(Request<'_>),
{
fn one<F, N>(value: &Value, on_request: &mut F, on_notification: &mut N) -> Option<Value>
where
F: FnMut(Request<'_>) -> Result<Value, RpcError>,
N: FnMut(Request<'_>),
{
let Some(object) = value.as_object() else { return Some(error_response(Value::Null, RpcError::new(-32600, "message must be an object"))) };
let id = object.get("id");
let Some(method) = object.get("method").and_then(Value::as_str) else {
if object.contains_key("result") || object.contains_key("error") {
return None;
}
return Some(error_response(id.cloned().unwrap_or(Value::Null), RpcError::new(-32600, "missing `method`")));
};
let request = Request { id, method, params: object.get("params") };
let Some(id) = id else {
on_notification(request);
return None;
};
Some(match on_request(request) {
Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
Err(error) => error_response(id.clone(), error),
})
}
if let Some(batch) = value.as_array() {
if batch.is_empty() {
return Some(error_response(Value::Null, RpcError::new(-32600, "empty batch")));
}
let replies: Vec<_> = batch.iter().filter_map(|v| one(v, &mut on_request, &mut on_notification)).collect();
(!replies.is_empty()).then_some(Value::Array(replies))
} else {
one(value, &mut on_request, &mut on_notification)
}
}
#[cfg(test)]
mod legacy_tests {
use super::*;
#[test]
fn preserves_legacy_batch_routing_and_errors() {
let input = json!([{"id": [1], "method":"echo", "params":5}, {"method":"notice"}, {"result":1}, {}, 7]);
let seen = std::cell::Cell::new(false);
let result = process_legacy(&input, |r| Ok(r.params.cloned().unwrap_or(Value::Null)), |_| seen.set(true)).unwrap();
assert!(seen.get());
assert_eq!(
result,
json!([{"jsonrpc":"2.0","id":[1],"result":5}, {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"missing `method`"}}, {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"message must be an object"}}])
);
assert_eq!(process_legacy(&json!([]), |_| Ok(Value::Null), |_| {}).unwrap()["error"]["message"], "empty batch");
}
}
+71 -3
View File
@@ -34,11 +34,27 @@ pub struct Session {
policy: Policy,
state: State,
capabilities: Value,
versions: Vec<String>,
permissive_initialize: bool,
server_info: Option<Value>,
instructions: Option<String>,
initialized: bool,
}
impl Session {
pub fn new(name: impl Into<String>, version: impl Into<String>, policy: Policy) -> Self {
Self { name: name.into(), version: version.into(), policy, state: State::New, capabilities: json!({"tools": {}}) }
Self {
name: name.into(),
version: version.into(),
policy,
state: State::New,
capabilities: json!({"tools": {}}),
versions: vec![PROTOCOL_VERSION.into()],
permissive_initialize: false,
server_info: None,
instructions: None,
initialized: false,
}
}
/// Set the server capabilities returned at initialization. The value must be an object.
@@ -50,6 +66,27 @@ impl Session {
Ok(self)
}
/// Configure a permissive legacy server: absent or malformed initialize parameters are
/// accepted, supported client versions are echoed, and unknown versions use the first entry.
pub fn with_legacy_initialize(mut self, versions: &[&str], server_info: Value) -> Self {
if !versions.is_empty() {
self.versions = versions.iter().map(|v| (*v).to_owned()).collect();
}
self.permissive_initialize = true;
self.server_info = Some(server_info);
self
}
/// Update connection-specific instructions returned by subsequent initialize requests.
pub fn set_instructions(&mut self, instructions: String) {
self.instructions = Some(instructions);
}
/// Whether an initialized notification has been observed, including before initialize.
pub fn is_initialized(&self) -> bool {
self.initialized
}
pub fn state(&self) -> State {
self.state
}
@@ -64,6 +101,18 @@ impl Session {
if self.policy == Policy::Strict && (in_batch || self.state != State::New) {
return Some(Err(RpcError::new(-32600, "Invalid Request")));
}
if self.permissive_initialize {
let asked = request.params.and_then(|p| p.get("protocolVersion")).and_then(Value::as_str);
let version = asked
.filter(|v| self.versions.iter().any(|supported| supported == v))
.unwrap_or_else(|| self.versions.first().map(String::as_str).unwrap_or(PROTOCOL_VERSION));
let mut result = json!({"protocolVersion": version, "capabilities": self.capabilities, "serverInfo": self.server_info});
if let Some(instructions) = &self.instructions {
result["instructions"] = json!(instructions);
}
self.state = State::AwaitingInitialized;
return Some(Ok(result));
}
let Some(params) = request.params.and_then(Value::as_object) else {
return Some(Err(invalid_initialize()));
};
@@ -89,8 +138,11 @@ impl Session {
/// Observe a JSON-RPC notification; notifications never receive replies.
pub fn notification(&mut self, request: Request<'_>) {
if request.method == "notifications/initialized" && self.state == State::AwaitingInitialized {
self.state = State::Ready;
if request.method == "notifications/initialized" {
self.initialized = true;
if self.state == State::AwaitingInitialized {
self.state = State::Ready;
}
}
}
}
@@ -127,6 +179,22 @@ mod tests {
Request { id: None, method, params }
}
#[test]
fn legacy_initialization_negotiates_and_accepts_missing_parameters() {
let mut s = Session::new("test", "1", Policy::Compatible).with_legacy_initialize(&["new", "old"], json!({"name":"custom", "title":"Title"}));
s.notification(call("notifications/initialized", None));
assert!(s.is_initialized());
s.set_instructions("help".into());
for (params, expected) in
[(None, "new"), (Some(json!({"protocolVersion":"old"})), "old"), (Some(json!({"protocolVersion":"unknown"})), "new")]
{
let result = s.request(call("initialize", params.as_ref()), false).unwrap().unwrap();
assert_eq!(result["protocolVersion"], expected);
assert_eq!(result["serverInfo"]["title"], "Title");
assert_eq!(result["instructions"], "help");
}
}
#[test]
fn compatible_negotiation_preserves_existing_clients() {
let mut s = Session::new("test", "1", Policy::Compatible);
+93
View File
@@ -0,0 +1,93 @@
//! Compatibility decoding for legacy transports that accepted mixed Base64 alphabets and padding.
//!
//! This is deliberately permissive and must not be used for signatures, credentials or other
//! contexts requiring canonical Base64. Use [`crate::base64`] for strict RFC 4648 decoding.
use crate::base64::DecodeError;
/// Decode standard or URL-safe Base64, accepting either alphabet even in the same input.
/// Spaces, CR, LF and `=` are ignored wherever they occur; an incomplete final sextet is
/// discarded. Other bytes are errors. Output allocation is bounded by the input length.
pub fn decode(input: impl AsRef<[u8]>) -> Result<Vec<u8>, DecodeError> {
let input = input.as_ref();
let mut out = Vec::new();
let capacity = (input.len() / 4) * 3 + (input.len() % 4) * 3 / 4;
out.try_reserve(capacity).map_err(|_| DecodeError)?;
let (mut acc, mut bits) = (0u32, 0u32);
for c in input {
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'+' | b'-' => 62,
b'/' | b'_' => 63,
b'=' | b'\n' | b'\r' | b' ' => continue,
_ => return Err(DecodeError),
};
acc = (acc << 6) | u32::from(v);
bits += 6;
if bits >= 8 {
bits -= 8;
out.push((acc >> bits) as u8);
acc &= (1 << bits) - 1;
}
}
Ok(out)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn accepts_mixed_alphabets_and_ignores_padding_anywhere() {
assert_eq!(decode("+_8=").unwrap(), [251, 255]);
assert_eq!(decode("Z=m 9\nv\r").unwrap(), b"foo");
assert_eq!(decode("A").unwrap(), b"");
assert_eq!(decode("AB").unwrap(), [0]);
assert!(decode("Zm9v\t").is_err());
assert!(decode("Zm9v!").is_err());
assert!(crate::base64::STANDARD.decode("Z=m9v").is_err());
}
// Differential against CADCraft's pre-cutover decoder, including malformed legacy inputs.
fn legacy(s: &str) -> Option<Vec<u8>> {
let mut out = Vec::new();
let (mut acc, mut bits) = (0u32, 0u32);
for c in s.bytes() {
let value = 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'+' | b'-' => 62,
b'/' | b'_' => 63,
b'=' | b'\n' | b'\r' | b' ' => continue,
_ => return None,
};
acc = (acc << 6) | u32::from(value);
bits += 6;
if bits >= 8 {
bits -= 8;
out.push((acc >> bits) as u8);
}
}
Some(out)
}
#[test]
fn matches_the_legacy_decoder_on_seeded_mixed_inputs() {
let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_= \r\n!\t";
let mut seed = 0x1234_5678u32;
for n in 0..50_000 {
let mut text = String::new();
for _ in 0..n % 45 {
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
text.push(char::from(alphabet[(seed as usize) % alphabet.len()]));
}
assert_eq!(decode(&text).ok(), legacy(&text), "{text:?}");
}
}
#[test]
fn round_trip_rfc_vectors_with_the_shared_encoder() {
for data in [&b""[..], b"f", b"fo", b"foo", b"foobar", &[0, 255, 127]] {
assert_eq!(decode(crate::base64::STANDARD.encode(data)).unwrap(), data);
}
}
}
+1
View File
@@ -28,6 +28,7 @@
//! says) is the caller's: this crate has no notion of a mailbox or a document.
pub mod base64;
pub mod base64_compat;
pub mod charset;
#[cfg(feature = "detect")]
pub mod detect;
+3
View File
@@ -150,3 +150,6 @@ mod tests {
assert_eq!(t.style.size, Some(12.0));
}
}
/// Editable type-tool envelope with PhotoCraft-compatible recovery and geometry limits.
pub mod envelope;
+309
View File
@@ -0,0 +1,309 @@
//! Editable PSD Type Tool Object Setting (`TySh`) envelope, extracted from PhotoCraft.
//! App-specific text styles, layout, units and editing policy stay with the consumer.
//! Parsing intentionally retains PhotoCraft's optional-warp and partial-bounds recovery.
//! Existing read-only [`super::TypeLayer`] behavior is unchanged.
use crate::descriptor::{Class, Descriptor, Id, ReferenceItem, UnicodeString, Value as D, VersionedDescriptor};
use crate::error::{PsdError, Result};
/// Maximum encoded envelope size, bounding hostile input and serialized output.
pub const MAX_BYTES: usize = 8 * 1024 * 1024;
/// Parsed `TySh` contents.
#[derive(Clone, Debug, PartialEq)]
pub struct TySh {
/// Affine transform in PSD order: xx, xy, yx, yy, tx, ty.
pub transform: [f64; 6],
/// Text descriptor, including text content and styling data.
pub text: Descriptor,
/// Optional warp descriptor retained from the Type Tool block.
pub warp: Option<Descriptor>,
/// The four trailing big-endian f32 values (left, top, right, bottom) after the warp
/// descriptor. Photoshop writes zeros for unwarped type and the text-space bounds (the
/// `bounds` descriptor's rectangle) for warped type.
pub bounds: [f32; 4],
}
/// Whether a type transform can reach rendering. Pixel coordinates use i32 in the originating PhotoCraft consumer, so non-finite and unrepresentable affine terms are rejected (#4758).
pub fn transform_is_safe(t: &[f64; 6]) -> bool {
t.iter().all(|v| v.is_finite() && v.abs() <= f64::from(i32::MAX))
}
/// Parses a `TySh` block.
pub fn parse_tysh(data: &[u8]) -> Option<TySh> {
if data.len() > MAX_BYTES {
return None;
}
let f = |at: usize| data.get(at..at + 8).map(|b| f64::from_be_bytes(b.try_into().unwrap_or([0; 8])));
let mut m = [0.0; 6];
for (i, v) in m.iter_mut().enumerate() {
*v = f(2 + i * 8)?;
}
if !transform_is_safe(&m) {
return None;
}
let (text, used) = VersionedDescriptor::parse_prefix(data.get(52..)?).ok()?;
let rest = data.get(52 + used..).unwrap_or(&[]);
let (warp, wused) = match rest.get(2..).and_then(|r| VersionedDescriptor::parse_prefix(r).ok()) {
Some((w, n)) => (Some(w.descriptor), n + 2),
None => (None, 0),
};
let tail = rest.get(wused..).unwrap_or(&[]);
let mut bounds = [0f32; 4];
for (i, b) in bounds.iter_mut().enumerate() {
if let Some(x) = tail.get(i * 4..i * 4 + 4) {
*b = f32::from_be_bytes(x.try_into().unwrap_or([0; 4]));
}
}
if bounds.iter().any(|x| !x.is_finite()) {
return None;
}
Some(TySh { transform: m, text: text.descriptor, warp, bounds })
}
/// `tysh` moved by (`dx`, `dy`): only the transform's translation changes. The text, its styles,
/// the warp and the trailing bounds (which are in text space) stay byte for byte, so a moved
/// Photoshop type layer keeps everything Photoshop wrote. `None` when the block is too short.
pub fn translate_tysh(tysh: &[u8], dx: f64, dy: f64) -> Option<Vec<u8>> {
if tysh.len() > MAX_BYTES || tysh.len() < 50 {
return None;
}
let mut transform = [0.0; 6];
for (i, x) in transform.iter_mut().enumerate() {
*x = f64::from_be_bytes(tysh.get(2 + i * 8..10 + i * 8)?.try_into().ok()?);
}
transform[4] += dx;
transform[5] += dy;
if !transform_is_safe(&transform) {
return None;
}
let mut out = Vec::new();
out.try_reserve_exact(tysh.len()).ok()?;
out.extend_from_slice(tysh);
// Version (2 bytes), then xx, xy, yx, yy, tx, ty as big-endian f64: tx at 34, ty at 42.
for (at, d) in [(34, dx), (42, dy)] {
let b = out.get_mut(at..at + 8)?;
let v = f64::from_be_bytes(b.try_into().ok()?) + d;
b.copy_from_slice(&v.to_be_bytes());
}
Some(out)
}
/// Serializes a `TySh` block.
pub fn write_tysh(t: &TySh) -> Result<Vec<u8>> {
if !transform_is_safe(&t.transform) || t.bounds.iter().any(|x| !x.is_finite()) {
return Err(PsdError::LimitExceeded("TySh geometry"));
}
let default;
let warp = match &t.warp {
Some(w) => w,
None => {
default = default_warp();
&default
}
};
let mut budget = Budget(78); // versions, transform and trailing bounds
budget.descriptor(&t.text, 0)?;
budget.descriptor(warp, 0)?;
let mut v = Vec::new();
v.try_reserve_exact(budget.0).map_err(|_| PsdError::OutOfMemory("TySh output"))?;
v.extend_from_slice(&1u16.to_be_bytes());
for x in t.transform {
v.extend_from_slice(&x.to_be_bytes());
}
v.extend_from_slice(&50u16.to_be_bytes());
v.extend_from_slice(&16u32.to_be_bytes());
t.text.write(&mut v);
v.extend_from_slice(&1u16.to_be_bytes());
v.extend_from_slice(&16u32.to_be_bytes());
warp.write(&mut v);
for b in t.bounds {
v.extend_from_slice(&b.to_be_bytes());
}
Ok(v)
}
// Count before cloning or serializing: bound both recursive trees and total output.
struct Budget(usize);
impl Budget {
fn add(&mut self, n: usize) -> Result<()> {
self.0 = self.0.checked_add(n).filter(|n| *n <= MAX_BYTES).ok_or(PsdError::LimitExceeded("TySh bytes"))?;
Ok(())
}
fn unicode(&mut self, s: &UnicodeString) -> Result<()> {
self.add(4)?;
self.add(s.0.len().checked_mul(2).ok_or(PsdError::LimitExceeded("TySh string"))?)
}
fn id(&mut self, id: &Id) -> Result<()> {
self.add(4)?;
self.add(id.as_bytes().len())
}
fn class(&mut self, c: &Class) -> Result<()> {
self.unicode(&c.name)?;
self.id(&c.class_id)
}
fn descriptor(&mut self, d: &Descriptor, depth: usize) -> Result<()> {
if depth > 32 {
return Err(PsdError::LimitExceeded("TySh depth"));
}
self.unicode(&d.name)?;
self.id(&d.class_id)?;
self.add(4)?;
for (k, v) in &d.items {
self.id(k)?;
self.value(v, depth + 1)?;
}
Ok(())
}
fn value(&mut self, v: &D, depth: usize) -> Result<()> {
if depth > 32 {
return Err(PsdError::LimitExceeded("TySh depth"));
}
self.add(4)?;
match v {
D::Descriptor(d) | D::GlobalObject(d) => self.descriptor(d, depth),
D::ObjectArray(o) => {
self.add(4)?;
self.descriptor(&o.body, depth)
}
D::List(xs) => {
self.add(4)?;
for x in xs {
self.value(x, depth + 1)?;
}
Ok(())
}
D::Reference(xs) => {
self.add(4)?;
for x in xs {
self.add(4)?;
match x {
ReferenceItem::Property { class, key } => {
self.class(class)?;
self.id(key)?;
}
ReferenceItem::Class(c) => self.class(c)?,
ReferenceItem::Enumerated { class, type_id, value } => {
self.class(class)?;
self.id(type_id)?;
self.id(value)?;
}
ReferenceItem::Offset { class, .. } | ReferenceItem::Identifier { class, .. } | ReferenceItem::Index { class, .. } => {
self.class(class)?;
self.add(4)?;
}
ReferenceItem::Name { class, name } => {
self.class(class)?;
self.unicode(name)?;
}
}
}
Ok(())
}
D::Text(x) => self.unicode(x),
D::Class(c) | D::GlobalClass(c) => self.class(c),
D::Enumerated { type_id, value } => {
self.id(type_id)?;
self.id(value)
}
D::UnitFloats { values, .. } => {
self.add(8)?;
self.add(values.len().checked_mul(8).ok_or(PsdError::LimitExceeded("TySh values"))?)
}
D::RawData(x) | D::Alias(x) | D::Path(x) => {
self.add(4)?;
self.add(x.len())
}
D::Double(_) | D::LargeInteger(_) => self.add(8),
D::UnitFloat { .. } => self.add(12),
D::Integer(_) => self.add(4),
D::Boolean(_) => self.add(1),
}
}
}
fn default_warp() -> Descriptor {
Descriptor::new("warp")
.with("warpStyle", enumv("warpStyle", "warpNone"))
.with("warpValue", D::Double(0.0))
.with("warpPerspective", D::Double(0.0))
.with("warpPerspectiveOther", D::Double(0.0))
.with("warpRotate", enumv("Ornt", "Hrzn"))
}
fn enumv(t: &str, v: &str) -> D {
D::Enumerated { type_id: Id::new(t), value: Id::new(v) }
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> TySh {
TySh {
transform: [1.0, 0.0, 0.0, 1.0, 25.0, 30.0],
text: Descriptor::new("TxLr").with("EngineData", D::RawData(b"preserved".to_vec())),
warp: Some(default_warp()),
bounds: [-1.0, 2.0, 30.0, 40.0],
}
}
#[test]
fn roundtrip_preserves_descriptors_transform_and_bounds() {
let t = sample();
let b = write_tysh(&t).unwrap();
assert_eq!(parse_tysh(&b), Some(t));
let shifted = translate_tysh(&b, 7.0, -3.0).unwrap();
assert_eq!(&shifted[..34], &b[..34]);
assert_eq!(&shifted[50..], &b[50..]);
assert_eq!(parse_tysh(&shifted).unwrap().transform, [1.0, 0.0, 0.0, 1.0, 32.0, 27.0]);
}
#[test]
fn truncations_and_unsafe_transforms_preserve_recovery_contract() {
let mut t = sample();
let bytes = write_tysh(&t).unwrap();
let (_, n) = VersionedDescriptor::parse_prefix(&bytes[52..]).unwrap();
for cut in 0..52 + n {
assert!(parse_tysh(&bytes[..cut]).is_none(), "{cut}");
}
let short = parse_tysh(&bytes[..52 + n]).unwrap();
assert!(short.warp.is_none());
assert_eq!(short.bounds, [0.0; 4]);
for cut in 0..50 {
assert!(translate_tysh(&bytes[..cut], 1.0, 2.0).is_none());
}
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, f64::from(i32::MAX) + 1.0] {
for i in 0..6 {
t = sample();
t.transform[i] = bad;
assert!(write_tysh(&t).is_err());
}
}
t = sample();
t.warp = None;
assert!(parse_tysh(&write_tysh(&t).unwrap()).unwrap().warp.is_some());
let partial = parse_tysh(&bytes[..bytes.len() - 4]).unwrap();
assert_eq!(partial.bounds, [-1.0, 2.0, 30.0, 0.0]);
}
#[test]
fn hostile_output_and_translations_are_refused_before_copying() {
let bytes = write_tysh(&sample()).unwrap();
for bad in [f64::NAN, f64::INFINITY, f64::MAX] {
assert!(translate_tysh(&bytes, bad, 0.0).is_none());
}
let mut t = sample();
t.bounds[0] = f32::NAN;
assert!(write_tysh(&t).is_err());
t = sample();
t.text = Descriptor::new("TxLr").with("data", D::RawData(vec![0; MAX_BYTES]));
assert!(write_tysh(&t).is_err());
let oversized = vec![0; MAX_BYTES + 1];
assert!(translate_tysh(&oversized, 0.0, 0.0).is_none());
assert!(parse_tysh(&oversized).is_none());
let mut nested = D::Integer(1);
for _ in 0..34 {
nested = D::List(vec![nested]);
}
t = sample();
t.text = Descriptor::new("TxLr").with("nest", nested);
assert!(write_tysh(&t).is_err());
}
}
+1
View File
@@ -12,6 +12,7 @@ workspace = true
[dependencies]
craft-xml = { workspace = true }
quick-xml = { version = "0.41", default-features = false }
craft-image-codecs = { workspace = true }
# SVGZ (gzip), pure-Rust backend.
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
+56
View File
@@ -0,0 +1,56 @@
//! SVG polyline and triangle path attributes for display-list exporters.
use crate::write::num;
use std::fmt::{self, Display};
/// A fixed-precision polyline attribute streamed into an SVG writer.
pub struct Points<I> {
iter: I,
decimals: usize,
}
/// Format points without allocating an intermediate attribute string. Precision is capped at 32.
pub fn points<I: Iterator<Item = [f64; 2]> + Clone>(iter: I, decimals: usize) -> Points<I> {
Points { iter, decimals: decimals.min(32) }
}
impl<I: Iterator<Item = [f64; 2]> + Clone> Display for Points<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for [x, y] in self.iter.clone() {
write!(f, "{},{} ", num(x, self.decimals), num(y, self.decimals))?;
}
Ok(())
}
}
/// A fixed-precision sequence of closed triangle subpaths.
pub struct Triangles<I> {
iter: I,
decimals: usize,
}
/// Format independent triangles without allocating an intermediate attribute string. Precision is capped at 32.
pub fn triangles<I: Iterator<Item = [[f64; 2]; 3]> + Clone>(iter: I, decimals: usize) -> Triangles<I> {
Triangles { iter, decimals: decimals.min(32) }
}
impl<I: Iterator<Item = [[f64; 2]; 3]> + Clone> Display for Triangles<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for [[ax, ay], [bx, by], [cx, cy]] in self.iter.clone() {
write!(
f,
"M{} {}L{} {}L{} {}Z",
num(ax, self.decimals),
num(ay, self.decimals),
num(bx, self.decimals),
num(by, self.decimals),
num(cx, self.decimals),
num(cy, self.decimals)
)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_precision_attributes_keep_independent_triangles() {
assert_eq!(points([[1.0, 2.0], [3.0, 4.0]].into_iter(), 2).to_string(), "1.00,2.00 3.00,4.00 ");
assert_eq!(triangles([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]].into_iter(), 1).to_string(), "M1.0 2.0L3.0 4.0L5.0 6.0Z");
}
}
+13 -3
View File
@@ -178,6 +178,10 @@ pub fn too_wide(svg: &str, most: usize, most_work: usize) -> bool {
pub struct Prescan<'s> {
/// Whether the elements nest deeper than the cap. The pass stops there.
pub too_deep: bool,
/// Deepest element nesting observed (markup entities count ten times).
pub depth: usize,
/// Deepest stack of unclosed source elements, excluding self-closing leaves.
pub open_depth: usize,
/// The text with each data URL in an element's attribute values left out: a `data:` value's
/// quotes kept, a `url(…)`'s parentheses and quotes. Borrowed when there is none, and the
/// whole text when `too_deep`. An embedded picture is a data URL and most of the text when
@@ -250,7 +254,7 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> {
(b.len(), false)
};
// (now, deepest) of the elements, and of the markup inside the document type.
let (mut elements, mut entities) = ((0usize, 0usize), (0usize, 0usize));
let (mut elements, mut entities, mut open_depth) = ((0usize, 0usize), (0usize, 0usize), 0usize);
let mut doctype_end = 0;
let mut i = 0;
while let Some(at) = b.get(i..).and_then(|rest| rest.iter().position(|c| *c == b'<')).map(|at| i + at) {
@@ -287,8 +291,14 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> {
end
};
if elements.1.saturating_add(entities.1.saturating_mul(10)) > most {
return Prescan { too_deep: true, markers: Cow::Borrowed(svg) };
return Prescan {
too_deep: true,
depth: elements.1.saturating_add(entities.1.saturating_mul(10)),
open_depth,
markers: Cow::Borrowed(svg),
};
}
open_depth = open_depth.max(elements.0);
}
let markers = if data_values.is_empty() {
Cow::Borrowed(svg)
@@ -303,7 +313,7 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> {
text.push_str(svg.get(from..).unwrap_or_default());
Cow::Owned(text)
};
Prescan { too_deep: false, markers }
Prescan { too_deep: false, depth: elements.1.saturating_add(entities.1.saturating_mul(10)), open_depth, markers }
}
#[cfg(test)]
+3
View File
@@ -18,7 +18,10 @@
#![forbid(unsafe_code)]
pub mod color;
pub mod drawing_write;
pub mod guard;
pub mod neutral_document;
pub mod path_data;
pub mod paths;
pub mod preflight;
pub mod sniff;
+407
View File
@@ -0,0 +1,407 @@
//! A bounded SVG document walk that preserves geometric primitive kinds for CAD import.
//!
//! Coordinates remain in SVG user units. Each shape carries the composed transform into
//! millimetres with the page Y axis pointing up; consumers map shapes into their own model.
use crate::path_data::{self, Point, Subpath};
use quick_xml::events::{BytesStart, Event as XmlEvent};
use quick_xml::{Reader, XmlVersion};
use std::collections::BTreeMap;
const MAX_DEPTH: usize = 64;
const MAX_SHAPES: usize = 500_000;
const MAX_POINTS: usize = 8_000_000;
const MAX_POLY_POINTS: usize = 100_001;
const SKIP: &[&[u8]] = &[
b"defs",
b"symbol",
b"clipPath",
b"mask",
b"pattern",
b"marker",
b"linearGradient",
b"radialGradient",
b"filter",
b"style",
b"text",
b"image",
b"title",
b"desc",
b"metadata",
];
const CONTAINERS: &[&[u8]] = &[b"g", b"svg", b"a", b"switch"];
/// Input failure or resource bound.
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct Error(pub String);
/// A shape's original geometry before its affine transform.
#[derive(Debug, Clone, PartialEq)]
pub enum Primitive {
Rect { x: f64, y: f64, width: f64, height: f64 },
Circle { center: Point, radius: f64 },
Ellipse { center: Point, rx: f64, ry: f64 },
Line { a: Point, b: Point },
Polyline { points: Vec<Point>, closed: bool },
Path(Vec<Subpath>),
}
/// A primitive with its layer and SVG affine matrix `[a,b,c,d,e,f]`.
#[derive(Debug, Clone, PartialEq)]
pub struct Shape {
pub primitive: Primitive,
pub transform: [f64; 6],
pub layer: String,
}
/// The shapes, encountered layer names, and skipped-feature counts. `visit` streams shapes
/// to its sink, so its returned `shapes` is empty; `import` collects them.
#[derive(Debug, Default, PartialEq)]
pub struct Document {
pub shapes: Vec<Shape>,
pub layers: Vec<String>,
pub skipped: BTreeMap<Skipped, usize>,
}
/// Features left out by this geometry-only import.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Skipped {
Text,
Image,
Use,
Style,
RoundedCorners,
}
fn attr(e: &BytesStart<'_>, name: &[u8]) -> Option<String> {
for a in e.attributes() {
let Ok(a) = a else { return None };
if a.key.into_inner() == name {
return a.normalized_value(XmlVersion::Explicit1_0).ok().map(|v| v.into_owned());
}
}
None
}
fn hidden(e: &BytesStart<'_>) -> bool {
attr(e, b"display").as_deref() == Some("none") || attr(e, b"style").is_some_and(|s| s.replace(' ', "").contains("display:none"))
}
fn length(s: &str) -> Option<f64> {
let s = s.trim();
let cut = s.find(|c: char| !(c.is_ascii_digit() || matches!(c, '.' | '-' | '+' | 'e' | 'E'))).unwrap_or(s.len());
let (n, unit) = s.split_at(cut);
let v: f64 = n.trim().parse().ok()?;
if !v.is_finite() {
return None;
}
Some(
v * match unit.trim() {
"" | "px" | "mm" => 1.0,
"cm" => 10.0,
"in" => 25.4,
"pt" => 25.4 / 72.0,
"pc" => 25.4 / 6.0,
_ => 25.4 / 96.0,
},
)
}
fn num(e: &BytesStart<'_>, name: &[u8], default: f64) -> f64 {
attr(e, name).as_deref().and_then(length).unwrap_or(default)
}
const ID: [f64; 6] = [1., 0., 0., 1., 0., 0.];
fn mul(a: [f64; 6], b: [f64; 6]) -> [f64; 6] {
[
a[0] * b[0] + a[2] * b[1],
a[1] * b[0] + a[3] * b[1],
a[0] * b[2] + a[2] * b[3],
a[1] * b[2] + a[3] * b[3],
a[0] * b[4] + a[2] * b[5] + a[4],
a[1] * b[4] + a[3] * b[5] + a[5],
]
}
fn translate(x: f64, y: f64) -> [f64; 6] {
[1., 0., 0., 1., x, y]
}
fn scale(x: f64, y: f64) -> [f64; 6] {
[x, 0., 0., y, 0., 0.]
}
fn rotate(a: f64) -> [f64; 6] {
let (s, c) = a.to_radians().sin_cos();
[c, s, -s, c, 0., 0.]
}
fn numbers(s: &str) -> impl Iterator<Item = f64> + '_ {
path_data::Numbers::new(s)
}
fn transform(s: &str) -> Option<[f64; 6]> {
let mut out = None;
let mut rest = s.trim();
while !rest.is_empty() {
let open = rest.find('(')?;
let name = rest[..open].trim();
let close = rest[open..].find(')')? + open;
// No transform function accepts more than six arguments.
let args: Vec<_> = numbers(&rest[open + 1..close]).take(7).collect();
let m = match (name, args.as_slice()) {
("matrix", [a, b, c, d, e, f]) => [*a, *b, *c, *d, *e, *f],
("translate", [x]) => translate(*x, 0.),
("translate", [x, y]) => translate(*x, *y),
("scale", [x]) => scale(*x, *x),
("scale", [x, y]) => scale(*x, *y),
("rotate", [a]) => rotate(*a),
("rotate", [a, x, y]) => mul(mul(translate(*x, *y), rotate(*a)), translate(-x, -y)),
("skewX", [a]) => [1., 0., a.to_radians().tan(), 1., 0., 0.],
("skewY", [a]) => [1., a.to_radians().tan(), 0., 1., 0., 0.],
_ => return None,
};
out = Some(out.map_or(m, |prev| mul(prev, m)));
rest = rest[close + 1..].trim_start_matches(|c: char| c == ',' || c.is_whitespace());
}
out
}
fn viewport(e: &BytesStart<'_>) -> [f64; 6] {
let vb = attr(e, b"viewBox").and_then(|raw| {
let n: Vec<_> = numbers(&raw).take(5).collect();
match n.as_slice() {
[x, y, w, h] if *w > 0. && *h > 0. => Some((*x, *y, *w, *h)),
_ => None,
}
});
let root = |name: &[u8]| {
let raw = attr(e, name)?;
let mm = length(&raw)?;
let unit = raw.trim().trim_start_matches(|c: char| c.is_ascii_digit() || matches!(c, '.' | '-' | '+' | 'e' | 'E')).trim();
Some(if matches!(unit, "" | "px") { mm * 25.4 / 96. } else { mm })
};
let unit = match (vb, root(b"width"), root(b"height")) {
(Some((_, _, w, _)), Some(x), _) if x > 0. => x / w,
(Some((_, _, _, h)), _, Some(y)) if y > 0. => y / h,
_ => 25.4 / 96.,
};
let (x, y, h) = vb.map_or((0., 0., 0.), |(x, y, _, h)| (x, y, h));
[unit, 0., 0., -unit, -x * unit, (y + h) * unit]
}
fn layer_name(raw: &str) -> String {
let s: String = raw.trim().chars().map(|c| if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '$') { c } else { '_' }).take(128).collect();
let s = s.trim_matches('_');
if s.is_empty() { "SVG".into() } else { s.into() }
}
/// A layer declaration or a drawable primitive in document order.
pub enum Event {
Layer(String),
Shape(Shape),
}
struct Walker<'a> {
doc: Document,
stack: Vec<[f64; 6]>,
layer: String,
skip: usize,
points: usize,
shapes: usize,
sink: &'a mut dyn FnMut(Event) -> Result<(), Error>,
}
impl Walker<'_> {
fn current(&self) -> [f64; 6] {
self.stack.last().copied().unwrap_or(ID)
}
fn note(&mut self, what: Skipped) {
*self.doc.skipped.entry(what).or_default() += 1;
}
fn skipped(name: &[u8]) -> Option<Skipped> {
match name {
b"text" => Some(Skipped::Text),
b"image" => Some(Skipped::Image),
_ => None,
}
}
fn start(&mut self, name: &[u8], e: &BytesStart<'_>) -> Result<(), Error> {
if name == b"style" {
self.note(Skipped::Style);
}
if self.skip > 0 {
self.skip += 1;
return Ok(());
}
if hidden(e) {
self.skip = 1;
return Ok(());
}
if SKIP.contains(&name) {
if let Some(s) = Self::skipped(name) {
self.note(s);
}
self.skip = 1;
return Ok(());
}
if !CONTAINERS.contains(&name) {
return self.element(name, e);
}
if name == b"svg"
&& self.stack.len() == 1
&& let Some(root) = self.stack.first_mut()
{
*root = viewport(e);
}
if name == b"g"
&& attr(e, b"inkscape:groupmode").as_deref() == Some("layer")
&& let Some(raw) = attr(e, b"inkscape:label").or_else(|| attr(e, b"id"))
{
let layer = layer_name(&raw);
if layer == "0" || self.doc.layers.contains(&layer) {
self.layer = layer;
} else if self.doc.layers.len() < 255 {
self.doc.layers.push(layer.clone());
(self.sink)(Event::Layer(layer.clone()))?;
self.layer = layer;
}
}
if self.stack.len() >= MAX_DEPTH {
return Err(Error(format!("SVG: more than {MAX_DEPTH} nested groups")));
}
let next = attr(e, b"transform").as_deref().and_then(transform).map_or(self.current(), |t| mul(self.current(), t));
self.stack.push(next);
Ok(())
}
fn element(&mut self, name: &[u8], e: &BytesStart<'_>) -> Result<(), Error> {
if self.skip > 0 {
return Ok(());
}
if SKIP.contains(&name) || hidden(e) {
if !hidden(e)
&& let Some(s) = Self::skipped(name)
{
self.note(s);
}
return Ok(());
}
let p = |x, y| Point::new(num(e, x, 0.), num(e, y, 0.));
let primitive = match name {
b"use" => {
self.note(Skipped::Use);
return Ok(());
}
b"rect" => {
let (x, y, w, h) = (num(e, b"x", 0.), num(e, b"y", 0.), num(e, b"width", 0.), num(e, b"height", 0.));
if w <= 0. || h <= 0. {
return Ok(());
}
if num(e, b"rx", 0.) > 0. || num(e, b"ry", 0.) > 0. {
self.note(Skipped::RoundedCorners);
}
Primitive::Rect { x, y, width: w, height: h }
}
b"circle" => {
let r = num(e, b"r", 0.);
if r <= 0. {
return Ok(());
}
Primitive::Circle { center: p(b"cx", b"cy"), radius: r }
}
b"ellipse" => {
let (rx, ry) = (num(e, b"rx", 0.), num(e, b"ry", 0.));
if rx <= 0. || ry <= 0. {
return Ok(());
}
Primitive::Ellipse { center: p(b"cx", b"cy"), rx, ry }
}
b"line" => Primitive::Line { a: p(b"x1", b"y1"), b: p(b"x2", b"y2") },
b"polyline" | b"polygon" => {
let Some(raw) = attr(e, b"points") else { return Ok(()) };
let n: Vec<_> = numbers(&raw).take(MAX_POLY_POINTS * 2).collect();
let pts: Vec<_> = n.as_chunks::<2>().0.iter().map(|[x, y]| Point::new(*x, *y)).collect();
if pts.len() < 2 {
return Ok(());
}
Primitive::Polyline { points: pts, closed: name == b"polygon" }
}
b"path" => {
let Some(raw) = attr(e, b"d") else { return Ok(()) };
Primitive::Path(path_data::parse_path(&raw).map_err(|e| Error(e.to_string()))?)
}
_ => return Ok(()),
};
let cost = match &primitive {
Primitive::Polyline { points, .. } => points.len(),
Primitive::Path(paths) => paths.iter().map(|p| p.segs.len()).sum(),
_ => 1,
};
self.points = self.points.checked_add(cost).filter(|n| *n <= MAX_POINTS).ok_or_else(|| Error("SVG: too much generated geometry".into()))?;
if self.shapes >= MAX_SHAPES {
return Err(Error(format!("SVG: more than {MAX_SHAPES} elements")));
}
let matrix = attr(e, b"transform").as_deref().and_then(transform).map_or(self.current(), |t| mul(self.current(), t));
self.shapes += 1;
(self.sink)(Event::Shape(Shape { primitive, transform: matrix, layer: self.layer.clone() }))
}
}
/// Walk supported SVG geometry without retaining a second copy of the document's shapes.
/// The sink receives each layer before its first shape and each shape in input order.
pub fn visit(text: &str, sink: &mut dyn FnMut(Event) -> Result<(), Error>) -> Result<Document, Error> {
let mut walker = Walker { doc: Document::default(), stack: vec![ID], layer: "0".into(), skip: 0, points: 0, shapes: 0, sink };
let mut reader = Reader::from_str(text);
reader.config_mut().trim_text(true);
loop {
match reader.read_event().map_err(|e| Error(format!("SVG: {e}")))? {
XmlEvent::Start(e) => walker.start(e.name().into_inner(), &e)?,
XmlEvent::Empty(e) => walker.element(e.name().into_inner(), &e)?,
XmlEvent::End(e) => {
if walker.skip > 0 {
walker.skip -= 1;
} else if CONTAINERS.contains(&e.name().into_inner()) && walker.stack.len() > 1 {
walker.stack.pop();
}
}
XmlEvent::Eof => break,
_ => {}
}
}
Ok(walker.doc)
}
/// Read the supported geometry into an owned document.
pub fn import(text: &str) -> Result<Document, Error> {
let mut shapes = Vec::new();
let mut doc = visit(text, &mut |event| {
if let Event::Shape(shape) = event {
shapes.push(shape);
}
Ok(())
})?;
doc.shapes = shapes;
Ok(doc)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn keeps_primitives_transform_layers_and_skips() {
let svg = r##"<svg width="20mm" height="10mm" viewBox="0 0 20 10"><g inkscape:groupmode="layer" inkscape:label="cut" transform="translate(2,1)"><circle cx="3" cy="4" r="2"/><ellipse cx="1" cy="2" rx="3" ry="1"/><path d="M0 0 A1 1 0 011 1"/></g><text>x</text><use href="#x"/></svg>"##;
let d = import(svg).unwrap();
assert_eq!(d.layers, ["cut"]);
assert_eq!(d.shapes.len(), 3);
assert_eq!(d.shapes[0].primitive, Primitive::Circle { center: Point::new(3., 4.), radius: 2. });
assert_eq!(d.shapes[0].transform, [1., 0., 0., -1., 2., 9.]);
assert!(matches!(d.shapes[1].primitive, Primitive::Ellipse { .. }));
assert!(matches!(&d.shapes[2].primitive,Primitive::Path(p) if matches!(p[0].segs[0],path_data::Segment::Arc { .. })));
assert_eq!(d.skipped.get(&Skipped::Text), Some(&1));
assert_eq!(d.skipped.get(&Skipped::Use), Some(&1));
}
#[test]
fn streaming_delivers_layers_before_shapes_and_stops_on_error() {
let s = r#"<svg><g inkscape:groupmode="layer" inkscape:label="cut"><circle r="1"/><circle r="2"/></g></svg>"#;
let mut events = Vec::new();
let result = visit(s, &mut |event| {
match event {
Event::Layer(name) => events.push(name),
Event::Shape(_) => {
events.push("shape".into());
return Err(Error("stop".into()));
}
}
Ok(())
});
assert!(result.is_err());
assert_eq!(events, ["cut", "shape"]);
}
#[test]
fn refuses_excessive_nesting() {
let s = format!("<svg>{}{}</svg>", "<g>".repeat(100), "</g>".repeat(100));
assert!(import(&s).is_err());
}
}
+456
View File
@@ -0,0 +1,456 @@
//! SVG path data as neutral line, cubic and elliptical arc segments.
//!
//! Relative commands and smooth controls are resolved, while arcs remain arcs.
//! The caller chooses how to map the result to its own geometry model.
use std::ops::{Add, Mul, Sub};
/// A point in the path's original user coordinate system.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
/// Horizontal coordinate.
pub x: f64,
/// Vertical coordinate.
pub y: f64,
}
impl Point {
/// The origin.
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
/// Construct a point.
pub const fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
impl Add for Point {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self::new(self.x + rhs.x, self.y + rhs.y)
}
}
impl Sub for Point {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self::new(self.x - rhs.x, self.y - rhs.y)
}
}
impl Mul<f64> for Point {
type Output = Self;
fn mul(self, rhs: f64) -> Self {
Self::new(self.x * rhs, self.y * rhs)
}
}
/// Invalid or oversized path data.
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct Error(pub String);
type Result<T> = std::result::Result<T, Error>;
const MAX_PATH_SEGMENTS: usize = 100_000;
const MAX_CURVE_POINTS: usize = 100_001;
pub(crate) struct Numbers<'a> {
s: &'a [u8],
i: usize,
}
impl<'a> Numbers<'a> {
pub(crate) fn new(s: &'a str) -> Self {
Self { s: s.as_bytes(), i: 0 }
}
fn skip_seps(&mut self) {
while matches!(self.s.get(self.i), Some(b' ' | b'\t' | b'\r' | b'\n' | b',')) {
self.i += 1;
}
}
fn next_num(&mut self) -> Option<f64> {
self.skip_seps();
let start = self.i;
if matches!(self.s.get(self.i), Some(b'+' | b'-')) {
self.i += 1;
}
let mut digits = 0;
let mut dot = false;
let mut exp = false;
while let Some(c) = self.s.get(self.i) {
match c {
b'0'..=b'9' => {
digits += 1;
self.i += 1;
}
b'.' if !dot && !exp => {
dot = true;
self.i += 1;
}
b'e' | b'E' if !exp && digits > 0 => {
exp = true;
self.i += 1;
if matches!(self.s.get(self.i), Some(b'+' | b'-')) {
self.i += 1;
}
}
_ => break,
}
}
if digits == 0 {
self.i = start;
return None;
}
std::str::from_utf8(&self.s[start..self.i]).ok()?.parse::<f64>().ok().filter(|v| v.is_finite())
}
}
impl Iterator for Numbers<'_> {
type Item = f64;
fn next(&mut self) -> Option<f64> {
self.next_num()
}
}
/// One `M …` run of a `d` attribute.
#[derive(Debug, Clone, PartialEq)]
pub struct Subpath {
/// Start point.
pub start: Point,
/// Segments in drawing order.
pub segs: Vec<Segment>,
/// Whether a close-path command closed this subpath.
pub closed: bool,
}
/// A path segment; arc values retain the SVG endpoint parameterization.
#[derive(Debug, Clone, PartialEq)]
pub enum Segment {
Line(Point),
Cubic(Point, Point, Point),
Arc { rx: f64, ry: f64, rot: f64, large: bool, sweep: bool, end: Point },
}
/// Tokens of a `d` attribute: command letters and numbers, in order.
enum Tok {
Cmd(u8),
Num(f64),
}
struct Tokenizer<'a> {
s: &'a [u8],
i: usize,
/// Start of the most recently returned token.
tok_start: usize,
}
impl<'a> Tokenizer<'a> {
fn new(s: &'a str) -> Self {
Tokenizer { s: s.as_bytes(), i: 0, tok_start: 0 }
}
/// Read an arc flag: a single `0` or `1` character, which needs no separator after it.
fn next_flag(&mut self) -> Option<f64> {
let mut nums = Numbers { s: self.s, i: self.i };
nums.skip_seps();
let v = match self.s.get(nums.i)? {
b'0' => 0.0,
b'1' => 1.0,
_ => return None,
};
self.i = nums.i + 1;
Some(v)
}
fn next_tok(&mut self) -> Option<Tok> {
let mut nums = Numbers { s: self.s, i: self.i };
nums.skip_seps();
self.i = nums.i;
self.tok_start = self.i;
let c = self.s.get(self.i).copied()?;
if c.is_ascii_alphabetic() {
self.i += 1;
return Some(Tok::Cmd(c));
}
let tok = nums.next_num().map(Tok::Num);
self.i = nums.i;
tok
}
}
/// Split `d` into subpaths, resolving relative commands and the S/T control reflections.
pub fn parse_path(d: &str) -> Result<Vec<Subpath>> {
let mut tk = Tokenizer::new(d);
let mut lookahead: Option<Tok> = None;
let mut cmd = 0u8;
let mut cur = Point::ZERO;
let mut start = Point::ZERO;
let mut segs: Vec<Segment> = Vec::new();
let mut out: Vec<Subpath> = Vec::new();
let closed = false;
let mut prev_cubic: Option<Point> = None;
let mut prev_quad: Option<Point> = None;
let mut segments = 0usize;
let next_num = |tk: &mut Tokenizer, look: &mut Option<Tok>| -> Result<f64> {
match look.take() {
Some(Tok::Num(v)) => Ok(v),
Some(other @ Tok::Cmd(_)) => {
*look = Some(other);
Err(Error("SVG: path command is missing coordinates".into()))
}
None => match tk.next_tok() {
Some(Tok::Num(v)) => Ok(v),
Some(other @ Tok::Cmd(_)) => {
*look = Some(other);
Err(Error("SVG: path command is missing coordinates".into()))
}
None => Err(Error("SVG: path command is missing coordinates".into())),
},
}
};
while let Some(tok) = lookahead.take().or_else(|| tk.next_tok()) {
let rel = match tok {
Tok::Cmd(c) => {
if c.eq_ignore_ascii_case(&b'z') {
if !segs.is_empty() {
out.push(Subpath { start, segs, closed: true });
segs = Vec::new();
}
cur = start;
cmd = 0;
continue;
}
cmd = c;
c.is_ascii_lowercase()
}
tok @ Tok::Num(_) => {
if cmd == 0 {
return Err(Error("SVG: path data does not start with a command".into()));
}
lookahead = Some(tok);
cmd.is_ascii_lowercase()
}
};
let abs = cmd.to_ascii_uppercase();
let count = match abs {
b'M' | b'L' | b'T' => 2,
b'H' | b'V' => 1,
b'C' => 6,
b'S' | b'Q' => 4,
b'A' => 7,
_ => return Err(Error(format!("SVG: unknown path command `{}`", cmd as char))),
};
let mut args = [0.0f64; 7];
for (idx, slot) in args.iter_mut().enumerate().take(count) {
if abs == b'A' && (idx == 3 || idx == 4) {
// Flags are single characters; re-scan a pending number token from its start.
if matches!(lookahead, Some(Tok::Num(_))) {
lookahead = None;
tk.i = tk.tok_start;
}
if lookahead.is_none() {
*slot = tk.next_flag().ok_or_else(|| Error("SVG: path command is missing coordinates".into()))?;
continue;
}
}
*slot = next_num(&mut tk, &mut lookahead)?;
}
let point = |x: f64, y: f64| if rel { cur + Point::new(x, y) } else { Point::new(x, y) };
match abs {
b'M' => {
if !segs.is_empty() {
out.push(Subpath { start, segs, closed: false });
segs = Vec::new();
}
cur = point(args[0], args[1]);
start = cur;
cmd = if rel { b'l' } else { b'L' };
prev_cubic = None;
prev_quad = None;
}
b'L' => {
cur = point(args[0], args[1]);
segs.push(Segment::Line(cur));
prev_cubic = None;
prev_quad = None;
}
b'H' => {
cur = Point::new(if rel { cur.x + args[0] } else { args[0] }, cur.y);
segs.push(Segment::Line(cur));
prev_cubic = None;
prev_quad = None;
}
b'V' => {
cur = Point::new(cur.x, if rel { cur.y + args[0] } else { args[0] });
segs.push(Segment::Line(cur));
prev_cubic = None;
prev_quad = None;
}
b'C' => {
let (c1, c2, p) = (point(args[0], args[1]), point(args[2], args[3]), point(args[4], args[5]));
segs.push(Segment::Cubic(c1, c2, p));
prev_cubic = Some(c2);
prev_quad = None;
cur = p;
}
b'S' => {
let c1 = prev_cubic.map_or(cur, |c2| cur * 2.0 - c2);
let (c2, p) = (point(args[0], args[1]), point(args[2], args[3]));
segs.push(Segment::Cubic(c1, c2, p));
prev_cubic = Some(c2);
prev_quad = None;
cur = p;
}
b'Q' => {
let (q, p) = (point(args[0], args[1]), point(args[2], args[3]));
segs.push(Segment::Cubic(cur + (q - cur) * (2.0 / 3.0), p + (q - p) * (2.0 / 3.0), p));
prev_quad = Some(q);
prev_cubic = None;
cur = p;
}
b'T' => {
let reflect = prev_quad.map_or(cur, |q| cur * 2.0 - q);
let p = point(args[0], args[1]);
segs.push(Segment::Cubic(cur + (reflect - cur) * (2.0 / 3.0), p + (reflect - p) * (2.0 / 3.0), p));
prev_quad = Some(reflect);
prev_cubic = None;
cur = p;
}
b'A' => {
let end = point(args[5], args[6]);
segs.push(Segment::Arc { rx: args[0].abs(), ry: args[1].abs(), rot: args[2], large: args[3] != 0.0, sweep: args[4] != 0.0, end });
cur = end;
prev_cubic = None;
prev_quad = None;
}
_ => return Err(Error("SVG: unsupported path command".into())),
}
segments += 1;
if segments > MAX_PATH_SEGMENTS {
return Err(Error("SVG: path has too many segments".into()));
}
if segs.len() > MAX_CURVE_POINTS {
return Err(Error("SVG: path has too many segments".into()));
}
}
if !segs.is_empty() {
out.push(Subpath { start, segs, closed });
}
Ok(out)
}
/// The centre and signed sweep of a circular arc from `p0` to `p1` in a Y-up frame, per the
/// SVG endpoint parameterisation (F.6.5). Radii that cannot span the chord are scaled up,
/// as the specification requires.
pub fn arc_center_circular(p0: Point, p1: Point, r: f64, large: bool, sweep: bool) -> Option<(Point, f64)> {
if r <= 0.0 || !r.is_finite() {
return None;
}
let d = p1 - p0;
let dist = d.x.hypot(d.y);
let r = if dist > 2.0 * r { dist / 2.0 } else { r };
if dist <= f64::EPSILON {
return None;
}
let mid = (p0 + p1) * 0.5;
let h = (r * r - (dist * dist) / 4.0).max(0.0).sqrt();
let n = Point::new(-d.y / dist, d.x / dist);
let sign = if large != sweep { 1.0 } else { -1.0 };
let center = mid + n * (h * sign);
let a0 = (p0.y - center.y).atan2(p0.x - center.x);
let a1 = (p1.y - center.y).atan2(p1.x - center.x);
let ccw = (a1 - a0).rem_euclid(std::f64::consts::TAU);
Some((center, pick_sweep(ccw, large, sweep)))
}
/// The signed sweep (CCW positive) for a chord sweep `ccw` in `[0, 2π)`: the centre is
/// already chosen so that the magnitude matches `large`, so only the direction is left.
fn pick_sweep(ccw: f64, large: bool, sweep: bool) -> f64 {
let tau = std::f64::consts::TAU;
let eps = f64::EPSILON;
if sweep {
if ccw <= eps { if large { tau } else { eps } } else { ccw }
} else if ccw >= tau - eps {
if large { -tau } else { -eps }
} else {
ccw - tau
}
}
/// Endpoint → centre parameterisation for a rotated elliptical arc (SVG F.6.5), in the
/// frame the points are given in. Returns the centre, the start parameter and the signed
/// sweep.
pub fn elliptical_params(p0: Point, p1: Point, rx: f64, ry: f64, phi: f64, large: bool, sweep: bool) -> Option<(Point, f64, f64)> {
if rx <= 0.0 || ry <= 0.0 {
return None;
}
let (sphi, cphi) = phi.sin_cos();
let dx = (p0.x - p1.x) / 2.0;
let dy = (p0.y - p1.y) / 2.0;
let x1 = cphi * dx + sphi * dy;
let y1 = -sphi * dx + cphi * dy;
let (rx, ry) = {
let lambda = x1 * x1 / (rx * rx) + y1 * y1 / (ry * ry);
if lambda > 1.0 {
let s = lambda.sqrt();
(rx * s, ry * s)
} else {
(rx, ry)
}
};
let num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1;
let den = rx * rx * y1 * y1 + ry * ry * x1 * x1;
let coef = if sweep != large { 1.0 } else { -1.0 } * (num.max(0.0) / den.max(f64::MIN_POSITIVE)).sqrt();
let cxp = coef * rx * y1 / ry;
let cyp = -coef * ry * x1 / rx;
let center = Point::new(cphi * cxp - sphi * cyp + (p0.x + p1.x) / 2.0, sphi * cxp + cphi * cyp + (p0.y + p1.y) / 2.0);
let angle = |ux: f64, uy: f64, vx: f64, vy: f64| {
let dot = ux * vx + uy * vy;
let det = ux * vy - uy * vx;
let denom = (ux.hypot(uy) * vx.hypot(vy)).max(f64::MIN_POSITIVE);
(dot / denom).clamp(-1.0, 1.0).acos().copysign(det)
};
let (ux, uy) = ((x1 - cxp) / rx, (y1 - cyp) / ry);
let (vx, vy) = ((-x1 - cxp) / rx, (-y1 - cyp) / ry);
let theta = angle(1.0, 0.0, ux, uy);
let ccw = angle(ux, uy, vx, vy).rem_euclid(std::f64::consts::TAU);
Some((center, theta, pick_sweep(ccw, large, sweep)))
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn resolves_relative_and_smooth_commands_without_flattening_arcs() {
let paths = parse_path("M1 2 l3 4 C5 6 7 8 9 10 s2 3 4 5 A6 7 30 0110 12 z").unwrap();
assert_eq!(paths.len(), 1);
assert!(paths[0].closed);
assert_eq!(paths[0].start, Point::new(1.0, 2.0));
assert_eq!(paths[0].segs[0], Segment::Line(Point::new(4.0, 6.0)));
assert_eq!(paths[0].segs[2], Segment::Cubic(Point::new(11.0, 12.0), Point::new(11.0, 13.0), Point::new(13.0, 15.0)));
assert_eq!(paths[0].segs[3], Segment::Arc { rx: 6.0, ry: 7.0, rot: 30.0, large: false, sweep: true, end: Point::new(10.0, 12.0) });
}
#[test]
fn smooth_quadratic_consumes_both_coordinates() {
let paths = parse_path("M0 0 Q3 3 6 0 T12 0").unwrap();
assert_eq!(paths[0].segs[1], Segment::Cubic(Point::new(8.0, -2.0), Point::new(10.0, -2.0), Point::new(12.0, 0.0)));
}
#[test]
fn arc_centers_keep_radius_correction_and_sweep() {
let (center, delta) = arc_center_circular(Point::new(0.0, 0.0), Point::new(10.0, 0.0), 2.0, false, true).unwrap();
assert!((center.x - 5.0).abs() < 1e-10);
assert!(center.y.abs() < 1e-10);
assert!((delta - std::f64::consts::PI).abs() < 1e-10);
let (center, start, sweep) = elliptical_params(Point::new(0.0, 0.0), Point::new(10.0, 0.0), 2.0, 1.0, 0.0, false, true).unwrap();
assert!((center.x - 5.0).abs() < 1e-10);
assert!(center.y.abs() < 1e-10 && start.is_finite());
assert!((sweep - std::f64::consts::PI).abs() < 1e-10);
}
#[test]
fn rejects_invalid_flags_and_limits_segments() {
assert!(parse_path("M0 0 A1 1 0 5 1 2 2").is_err());
assert!(parse_path("M0 0 L").is_err());
let excessive = format!("M0 0{}", " L1 1".repeat(MAX_PATH_SEGMENTS));
assert!(parse_path(&excessive).is_err());
}
}
+37 -2
View File
@@ -71,6 +71,12 @@ pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result<Cow<'a, str>, Error>
/// Parses already-decoded SVG text and checks it against `limits`.
pub fn parse_text<'a>(svg: &'a str, limits: &Limits) -> Result<roxmltree::Document<'a>, Error> {
parse_text_with_depth(svg, limits).map(|(xml, _, _)| xml)
}
/// [`parse_text`], with the source nesting: the deepest element nesting (markup entities count
/// ten times) and the deepest stack of open elements (a self-closing leaf opens none).
pub fn parse_text_with_depth<'a>(svg: &'a str, limits: &Limits) -> Result<(roxmltree::Document<'a>, usize, usize), Error> {
if svg.len() > limits.bytes {
return Err(Error::Text(crate::svgz::Error::TooLarge(limits.bytes)));
}
@@ -83,9 +89,38 @@ pub fn parse_text<'a>(svg: &'a str, limits: &Limits) -> Result<roxmltree::Docume
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) {
let prescan = crate::guard::prescan(svg, limits.depth);
if prescan.too_deep {
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()))
let xml = roxmltree::Document::parse_with_options(svg, options).map_err(|e| Error::Xml(e.to_string()))?;
Ok((xml, prescan.depth, prescan.open_depth))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parsed_tree_reports_source_nesting_depth() -> Result<(), Error> {
let limits = Limits { depth: 4, ..Default::default() };
let (document, depth, open_depth) = parse_text_with_depth("<svg><g><path/></g></svg>", &limits)?;
assert!(document.root_element().has_tag_name("svg"));
// Every element is a level (svg, g, path); a self-closing leaf opens none (svg, g).
assert_eq!(depth, 3);
assert_eq!(open_depth, 2);
let simple = parse_text("<svg/>", &limits)?;
assert!(simple.root_element().has_tag_name("svg"));
Ok(())
}
#[test]
fn source_depth_and_xml_errors_keep_their_specific_kind() {
let shallow = Limits { depth: 1, ..Default::default() };
assert!(matches!(parse_text_with_depth("<svg><g></g></svg>", &shallow), Err(Error::TooDeep(1))));
let normal = Limits::default();
assert!(matches!(parse_text_with_depth("<svg><g></svg>", &normal), Err(Error::Xml(_))));
}
}
+104 -20
View File
@@ -112,13 +112,19 @@ pub enum Error {
/// usvg's own error.
#[error("invalid SVG: {0}")]
Parse(String),
/// The root viewport is missing or has dimensions this SVG helper cannot preserve.
#[error("invalid SVG root viewport: {0}")]
Viewport(String),
/// The rewritten SVG would exceed the caller's output byte budget.
#[error("rewritten SVG exceeds {0} bytes")]
OutputTooLarge(usize),
}
/// The SVG text of the bytes of an SVG or SVGZ file, checked against `limits`: what [`parse`]
/// hands usvg. For a caller that edits the text before it parses ([`parse_text`]).
pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result<Cow<'a, str>, Error> {
let text = svgz::text(bytes, limits.bytes)?;
checked(&text, limits)?;
preflight(&text, limits)?;
Ok(text)
}
@@ -130,8 +136,7 @@ pub fn parse(bytes: &[u8], limits: &Limits, options: &usvg::Options<'_>) -> Resu
/// [`parse`] for SVG text the caller already holds.
pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Result<usvg::Tree, Error> {
let xml = checked(svg, limits)?;
usvg::Tree::from_xmltree(&xml, options).map_err(|e| Error::Parse(e.to_string()))
preflight(svg, limits)?.parse(options)
}
/// Read the root SVG's unitless width and height without constructing a rendering tree.
@@ -139,8 +144,8 @@ pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Re
/// Lengths with units, percentages, missing dimensions, and non-SVG roots return `Ok(None)`;
/// malformed or over-limit XML returns the same bounded parsing error as [`parse_text`].
pub fn root_unitless_size(svg: &str, limits: &Limits) -> Result<Option<(f64, f64)>, Error> {
let xml = checked(svg, limits)?;
let root = xml.root_element();
let checked = preflight(svg, limits)?;
let root = checked.xml.root_element();
if root.tag_name().name() != "svg" {
return Ok(None);
}
@@ -152,8 +157,30 @@ pub fn root_unitless_size(svg: &str, limits: &Limits) -> Result<Option<(f64, f64
Ok(Some((width, height)))
}
/// Every check on SVG text, cheapest first; the XML it parses to on the way.
fn checked<'s>(svg: &'s str, limits: &Limits) -> Result<roxmltree::Document<'s>, Error> {
/// SVG text after shared byte, entity, attribute, XML and expansion-depth checks.
/// Keep it opaque so consumers use the same checked tree and don't reparse XML locally.
pub struct Preflight<'s> {
xml: roxmltree::Document<'s>,
depth: usize,
/// [`height`]: the nesting usvg converts, references followed.
height: usize,
}
impl Preflight<'_> {
/// The deepest stack of open source elements, before references are followed; a
/// self-closing leaf opens no level (`<svg><g><path/></g></svg>` is 2).
pub fn depth(&self) -> usize {
self.depth
}
/// Converts this already-checked XML into the configured usvg tree.
pub fn parse(self, options: &usvg::Options<'_>) -> Result<usvg::Tree, Error> {
usvg::Tree::from_xmltree(&self.xml, options).map_err(|e| Error::Parse(e.to_string()))
}
}
/// Checks already-decoded SVG text once, retaining the parsed XML for both depth policy and tree conversion.
pub fn preflight<'s>(svg: &'s str, limits: &Limits) -> Result<Preflight<'s>, Error> {
let preflight_limits = crate::preflight::Limits {
bytes: limits.bytes,
depth: limits.depth,
@@ -163,7 +190,7 @@ fn checked<'s>(svg: &'s str, limits: &Limits) -> Result<roxmltree::Document<'s>,
attribute_work: limits.attribute_work,
xml_nodes: limits.xml_nodes,
};
let xml = crate::preflight::parse_text(svg, &preflight_limits).map_err(|e| match e {
let (xml, _, depth) = crate::preflight::parse_text_with_depth(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,
@@ -171,10 +198,69 @@ fn checked<'s>(svg: &'s str, limits: &Limits) -> Result<roxmltree::Document<'s>,
crate::preflight::Error::TooWide(attributes) => Error::TooWide(attributes),
crate::preflight::Error::Xml(msg) => Error::Xml(msg),
})?;
if height(&xml, limits.tree_depth)?.is_none() {
let Some(height) = height(&xml, limits.tree_depth)? else {
return Err(Error::TooDeep(limits.tree_depth));
};
Ok(Preflight { xml, depth, height })
}
/// Dimensions of an SVG viewport in SVG user units.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Viewport {
pub width: f64,
pub height: f64,
}
/// Wraps a validated SVG root in a new viewport while preserving the original root as content.
/// The original root must have finite, positive pixel or point dimensions, and the wrapped
/// output must still pass [`preflight`] under `limits`.
pub fn rewrite_root_viewport(svg: &str, target: Viewport, limits: &Limits, output_limit: usize) -> Result<String, Error> {
if !target.width.is_finite() || !target.height.is_finite() || target.width <= 0.0 || target.height <= 0.0 {
return Err(Error::Viewport("target dimensions must be finite and positive".into()));
}
Ok(xml)
let checked = preflight(svg, limits)?;
let root = checked.xml.root_element();
if root.tag_name().name() != "svg" {
return Err(Error::Viewport("root element is not svg".into()));
}
let root_dimension = |key: &str| {
let value = root.attribute(key)?;
let value = value.trim();
let (number, scale) = match value.strip_suffix("pt") {
Some(points) => (points, 4.0 / 3.0),
None => (value.strip_suffix("px").unwrap_or(value), 1.0),
};
number.parse::<f64>().ok().map(|n| n * scale).filter(|n| n.is_finite() && *n > 0.0)
};
let (width, height) = root_dimension("width")
.zip(root_dimension("height"))
.ok_or_else(|| Error::Viewport("source has no finite pixel or point dimensions".into()))?;
let range = root.range();
let preamble = svg.get(..range.start).ok_or_else(|| Error::Viewport("invalid XML preamble range".into()))?;
let inner = svg.get(range).ok_or_else(|| Error::Viewport("invalid root element range".into()))?;
let prefix = format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}" viewBox="0 0 {} {}" preserveAspectRatio="none">"#,
target.width, target.height, width, height
);
let len = preamble
.len()
.checked_add(prefix.len())
.and_then(|n| n.checked_add(inner.len()))
.and_then(|n| n.checked_add(6))
.ok_or(Error::OutputTooLarge(output_limit))?;
if len > output_limit {
return Err(Error::OutputTooLarge(output_limit));
}
let mut output = String::new();
output.try_reserve_exact(len).map_err(|_| Error::OutputTooLarge(output_limit))?;
output.push_str(preamble);
output.push_str(&prefix);
output.push_str(inner);
output.push_str("</svg>");
// The wrapper adds a level, so a source at the depth limit can come out over it: check the
// output under the limits `parse_text` applies rather than return text it would refuse.
preflight(&output, limits)?;
Ok(output)
}
/// Properties whose value an element's descendants use too: the paint server they name is
@@ -376,8 +462,8 @@ impl ImageResolver {
/// Nested SVGs are checked automatically. The scan shares the normal XML limits and
/// streaming-validates URL encoding without allocating another decoded image buffer.
pub fn check_document(&self, svg: &str) -> Result<(), Error> {
let xml = checked(svg, &self.limits)?;
account_invalid_data_urls(&xml, &self.used);
let checked = preflight(svg, &self.limits)?;
account_invalid_data_urls(&checked.xml, &self.used);
Ok(())
}
@@ -520,8 +606,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'
return None;
}
used.bytes.fetch_add(text.len(), Ordering::Relaxed);
let xml = match checked(&text, limits) {
Ok(xml) => xml,
let checked = match preflight(&text, limits) {
Ok(checked) => checked,
// Decoding UTF-16 or Latin-1 can exceed the text cap after the
// raw/inflated bytes fitted. This is a resource refusal, not invalid XML.
Err(Error::Text(svgz::Error::TooLarge(_))) => {
@@ -538,10 +624,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'
return None;
}
};
let Some(xml_depth) = height(&xml, limits.tree_depth).ok().flatten() else {
used.svg_limit.fetch_add(1, Ordering::Relaxed);
return None;
};
// The nesting usvg converts, references followed, not the source depth alone.
let xml_depth = checked.height;
let total_depth = used.xml_depth.load(Ordering::Relaxed).checked_add(xml_depth);
let Some(total_depth) = total_depth.filter(|depth| *depth <= limits.tree_depth) else {
used.svg_limit.fetch_add(1, Ordering::Relaxed);
@@ -550,8 +634,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'
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);
account_invalid_data_urls(&xml, used);
let tree = usvg::Tree::from_xmltree(&xml, options);
account_invalid_data_urls(&checked.xml, used);
let tree = checked.parse(options);
used.xml_depth.store(previous_depth, Ordering::Relaxed);
used.depth.fetch_sub(1, Ordering::Relaxed);
match tree {
+103 -2
View File
@@ -292,7 +292,7 @@ mod resource_limit_usage {
assert_eq!(svgz::inflate(&data, cap).unwrap().len(), cap, "raw bytes fit exactly");
assert_eq!(svgz::text(&data, cap).unwrap().len(), decoded.len(), "decoding itself succeeds");
let limits = Limits { bytes: cap, embedded_bytes: decoded.len(), ..shallow() };
assert!(matches!(checked(&decoded, &limits), Err(Error::Text(svgz::Error::TooLarge(n))) if n == cap));
assert!(matches!(preflight(&decoded, &limits), Err(Error::Text(svgz::Error::TooLarge(n))) if n == cap));
let image = data_image(&data);
let (found, usage) = resolved(&svg(&image), limits, 1);
assert!(found.is_empty(), "{encoding}, gzip {gzip}");
@@ -312,7 +312,7 @@ mod resource_limit_usage {
let decoded = svgz::decode(&raw).unwrap();
let limits = Limits { bytes: decoded.len(), embedded_bytes: raw.len(), ..shallow() };
assert!(decoded.len() > limits.embedded_bytes);
checked(&decoded, &limits).unwrap(); // The independent per-image cap allows this complete XML.
preflight(&decoded, &limits).unwrap(); // The independent per-image cap allows this complete XML.
for gzip in [false, true] {
let data = if gzip { svgz::compress(&raw) } else { raw.clone() };
assert_eq!(svgz::inflate(&data, limits.embedded_bytes).unwrap().len(), raw.len());
@@ -584,3 +584,104 @@ mod invalid_image_accounting {
assert_eq!(report.raster_limit_images, 1);
}
}
#[test]
fn preflight_keeps_source_depth_and_parses_the_checked_tree() {
let text = svg("<g><rect width='1' height='1'/></g>");
let checked = preflight(&text, &Limits::default()).unwrap();
// svg and g are open; the self-closing rect opens no level.
assert_eq!(checked.depth(), 2);
let tree = checked.parse(&options()).unwrap();
assert_eq!(tree.size().width(), 10.0);
assert!(matches!(preflight(&svg(&groups(30)), &shallow()), Err(Error::TooDeep(24))));
}
#[test]
fn preflight_matches_parse_text_refusals() {
for text in ["<svg", "<svg xmlns='http://www.w3.org/2000/svg'><g></svg>"] {
assert!(matches!(preflight(text, &Limits::default()), Err(Error::Xml(_))));
assert!(matches!(parse_text(text, &Limits::default(), &options()), Err(Error::Xml(_))));
}
}
#[test]
fn rewrite_root_viewport_wraps_the_root_and_keeps_the_preamble() {
let source = concat!(
"<?xml version='1.0'?>\n<!DOCTYPE svg [<!ENTITY w '4'>]>\n",
"<svg xmlns='http://www.w3.org/2000/svg' width='20' height='10' viewBox='0 0 2 1'>",
"<rect width='&w;' height='1'/></svg>"
);
let out = rewrite_root_viewport(source, Viewport { width: 40.0, height: 30.0 }, &Limits::default(), 4096).unwrap();
assert!(out.starts_with("<?xml version='1.0'?>\n<!DOCTYPE svg [<!ENTITY w '4'>]>\n<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"40\" height=\"30\" viewBox=\"0 0 20 10\" preserveAspectRatio=\"none\"><svg "));
assert!(out.ends_with("</svg></svg>"));
let tree = parse_text(&out, &Limits::default(), &options()).unwrap();
assert_eq!((tree.size().width(), tree.size().height()), (40.0, 30.0));
}
#[test]
fn rewrite_root_viewport_reads_points_and_pixels() {
let points = rewrite_root_viewport(
"<svg xmlns='http://www.w3.org/2000/svg' width='72pt' height='36pt'/>",
Viewport { width: 1.0, height: 1.0 },
&Limits::default(),
4096,
)
.unwrap();
assert!(points.contains("viewBox=\"0 0 96 48\""));
let pixels = rewrite_root_viewport(
"<svg xmlns='http://www.w3.org/2000/svg' width='12px' height='8'/>",
Viewport { width: 1.0, height: 1.0 },
&Limits::default(),
4096,
)
.unwrap();
assert!(pixels.contains("viewBox=\"0 0 12 8\""));
}
#[test]
fn rewrite_root_viewport_refusals() {
let limits = Limits::default();
let one = Viewport { width: 1.0, height: 1.0 };
let sized = "<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'/>";
for target in [
Viewport { width: f64::NAN, height: 1.0 },
Viewport { width: 1.0, height: f64::INFINITY },
Viewport { width: 0.0, height: 1.0 },
Viewport { width: 1.0, height: -2.0 },
] {
assert!(matches!(rewrite_root_viewport(sized, target, &limits, 4096), Err(Error::Viewport(_))));
}
assert!(matches!(rewrite_root_viewport("<html width='10' height='10'/>", one, &limits, 4096), Err(Error::Viewport(_))));
for source in [
"<svg xmlns='http://www.w3.org/2000/svg'/>",
"<svg xmlns='http://www.w3.org/2000/svg' width='50%' height='10'/>",
"<svg xmlns='http://www.w3.org/2000/svg' width='10mm' height='10'/>",
"<svg xmlns='http://www.w3.org/2000/svg' width='0' height='10'/>",
"<svg xmlns='http://www.w3.org/2000/svg' width='1e400' height='10'/>",
] {
assert!(matches!(rewrite_root_viewport(source, one, &limits, 4096), Err(Error::Viewport(_))), "{source}");
}
assert!(matches!(rewrite_root_viewport("<svg", one, &limits, 4096), Err(Error::Xml(_))));
let fits = rewrite_root_viewport(sized, one, &limits, 4096).unwrap();
assert_eq!(rewrite_root_viewport(sized, one, &limits, fits.len()).unwrap(), fits);
assert!(matches!(rewrite_root_viewport(sized, one, &limits, fits.len() - 1), Err(Error::OutputTooLarge(n)) if n == fits.len() - 1));
}
#[test]
fn rewrite_root_viewport_output_stays_within_the_limits() {
let limits = shallow();
let one = Viewport { width: 1.0, height: 1.0 };
let mut refused_at_the_limit = false;
for n in 0..limits.depth {
let source = svg(&groups(n));
if preflight(&source, &limits).is_err() {
break;
}
match rewrite_root_viewport(&source, one, &limits, 1 << 20) {
Ok(out) => assert!(parse_text(&out, &limits, &options()).is_ok(), "{n} groups"),
Err(Error::TooDeep(_)) => refused_at_the_limit = true,
Err(e) => panic!("{n} groups: {e:?}"),
}
}
assert!(refused_at_the_limit, "a source at the depth limit is refused, not wrapped past it");
}
+27 -1
View File
@@ -86,18 +86,36 @@ impl Attribute {
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 } }
}
/// A translate/scale transform with three decimal places for positions and four for scales.
pub fn translate_scale_fixed(x: f64, y: f64, scale_x: f64, scale_y: f64) -> Self {
Self::text("transform", format!("translate({} {}) scale({} {})", num(x, 3), num(y, 3), num(scale_x, 4), num(scale_y, 4)))
}
}
impl Scene {
/// Serialize the scene as an SVG 1.1 document.
pub fn write(&self) -> String {
self.write_impl(false)
}
/// Serialize the root viewBox with three decimal places for stable fixed-width output.
pub fn write_fixed_view_box(&self) -> String {
self.write_impl(true)
}
fn write_impl(&self, fixed: bool) -> String {
let mut writer = SvgWriter::new();
let view_box = if fixed {
format!("{} {} {} {}", num(self.view_box[0], 3), num(self.view_box[1], 3), num(self.view_box[2], 3), num(self.view_box[3], 3))
} else {
view_box_data(self.view_box)
};
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))
.attr("viewBox", view_box)
.open();
write_elements(&mut writer, &self.elements, 1);
writer.line(0);
@@ -307,6 +325,14 @@ mod tests {
p
}
#[test]
fn scene_can_keep_fixed_view_box_without_changing_default_output() {
let scene = Scene { width_mm: 100.0, height_mm: 150.0, view_box: [0.0, 0.0, 100.0, 150.0], elements: vec![] };
assert!(scene.write().contains("viewBox=\"0 0 100 150\""));
assert!(scene.write_fixed_view_box().contains("viewBox=\"0.000 0.000 100.000 150.000\""));
assert_eq!(Attribute::translate_scale_fixed(6.0, 46.0, 0.5, -0.5).value, Value::Text("translate(6.000 46.000) scale(0.5000 -0.5000)".into()));
}
#[test]
fn numbers_have_fixed_decimals_and_are_never_non_finite() {
assert_eq!(num(1.5, 3).to_string(), "1.500");
+17
View File
@@ -0,0 +1,17 @@
[package]
name = "craft-tcraft"
description = "Streaming binary payloads of TextureCraft project archives"
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 = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
+14
View File
@@ -0,0 +1,14 @@
# craft-tcraft
Streaming neutral `TCTX` binary payloads used inside TextureCraft `.tcraft` projects. Sparse
paint data stores 64×64 tiles of little-endian `u16` components; baked maps store dense
little-endian `f32` values. This crate does not yet define the project JSON schema or ZIP
envelope and does not depend on TextureCraft's app types.
`read_tiles` validates the expected dimensions and components, bounds tile count, rejects
out-of-range and repeated tile positions, and delivers one owned tile at a time. `read_baked`
validates dimensions and declared length and decodes in bounded chunks. Both consume the same
caller-owned `craft_zip::Budget` used for the surrounding project and reject trailing data.
`write_tiles` and `write_baked` stream their bodies rather than copying an entire image; a caller
must discard an incomplete output if a write fails. Limits and ownership rules beyond this
binary format remain with the project reader.
+428
View File
@@ -0,0 +1,428 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
#![forbid(unsafe_code)]
//! Streaming `TCTX` tile and baked-map payloads in TextureCraft `.tcraft` archives.
//!
//! This codec handles the neutral binary payload, not the application's project model or ZIP
//! envelope. Reads use a caller-owned budget shared with other archive entries. No reader opens
//! a file or reserves memory based on a claimed payload before checking it.
use std::collections::{BTreeMap, HashSet};
use std::io::{self, Read, Write};
use craft_zip::Budget;
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const TILE_SIDE: u32 = 64;
pub const TILE_AREA: usize = 64 * 64;
pub const HEADER_BYTES: u64 = 24;
pub const VERSION: u32 = 1;
const MAGIC: &[u8; 4] = b"TCTX";
const CHUNK: usize = 1 << 16;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid TCTX payload: {0}")]
Invalid(String),
#[error("TCTX version {0} is newer than supported")]
NewerVersion(u32),
#[error("TCTX budget: {0}")]
Budget(#[from] craft_zip::Exceeded),
#[error("TCTX limit: {0}")]
Limit(String),
#[error("TCTX I/O: {0}")]
Io(#[from] io::Error),
}
/// Neutral top-level project schema. Nested app-domain values and unknown fields remain JSON
/// so they round-trip without requiring TextureCraft's in-memory model.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct ProjectDocument {
pub format_version: u32,
pub name: String,
pub mesh: String,
pub normal_format: Value,
pub texture_sets: Vec<Value>,
pub layers: BTreeMap<String, Value>,
pub next_id: u64,
pub display: Value,
#[serde(default)]
pub cameras: Vec<Value>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
impl Serialize for ProjectDocument {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::{Error as _, SerializeMap as _};
const RESERVED: &[&str] = &["format_version", "name", "mesh", "normal_format", "texture_sets", "layers", "next_id", "display", "cameras"];
if self.extra.keys().any(|key| RESERVED.contains(&key.as_str())) {
return Err(S::Error::custom("an extra project field duplicates a known field"));
}
let mut map = serializer.serialize_map(Some(9 + self.extra.len()))?;
map.serialize_entry("format_version", &self.format_version)?;
map.serialize_entry("name", &self.name)?;
map.serialize_entry("mesh", &self.mesh)?;
map.serialize_entry("normal_format", &self.normal_format)?;
map.serialize_entry("texture_sets", &self.texture_sets)?;
map.serialize_entry("layers", &self.layers)?;
map.serialize_entry("next_id", &self.next_id)?;
map.serialize_entry("display", &self.display)?;
map.serialize_entry("cameras", &self.cameras)?;
for (key, value) in &self.extra {
map.serialize_entry(key, value)?;
}
map.end()
}
}
/// Read only the version before an application applies its own recovery policy. Serde's
/// structured parse rejects a duplicate or invalid version field, like the project reader.
pub fn project_version(bytes: &[u8]) -> Result<u32, serde_json::Error> {
#[derive(Deserialize)]
struct Version {
format_version: u32,
}
Ok(serde_json::from_slice::<Version>(bytes)?.format_version)
}
/// Width, height and number of components expected by the enclosing project schema.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Layout {
pub width: u32,
pub height: u32,
pub components: usize,
}
#[derive(Clone, Copy, Debug)]
struct Header {
layout: Layout,
count: u32,
}
fn charge(budget: &mut Budget, n: u64) -> Result<(), Error> {
budget.charge(n).map_err(Error::from)
}
fn read_header(reader: &mut (impl Read + ?Sized), budget: &mut Budget) -> Result<Header, Error> {
charge(budget, HEADER_BYTES)?;
let mut bytes = [0u8; HEADER_BYTES as usize];
reader.read_exact(&mut bytes)?;
if bytes.get(..4) != Some(MAGIC.as_slice()) {
return Err(Error::Invalid("bad magic".into()));
}
let word = |i: usize| bytes.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])).unwrap_or(0);
let version = word(1);
if version > VERSION {
return Err(Error::NewerVersion(version));
}
Ok(Header { layout: Layout { width: word(2), height: word(3), components: word(4) as usize }, count: word(5) })
}
fn write_header(writer: &mut (impl Write + ?Sized), layout: Layout, count: u32) -> Result<(), Error> {
let components = u32::try_from(layout.components).map_err(|_| Error::Limit("too many components".into()))?;
writer.write_all(MAGIC)?;
for value in [VERSION, layout.width, layout.height, components, count] {
writer.write_all(&value.to_le_bytes())?;
}
Ok(())
}
fn expect_end(reader: &mut (impl Read + ?Sized)) -> Result<(), Error> {
let mut byte = [0u8; 1];
if reader.read(&mut byte)? != 0 {
return Err(Error::Invalid("trailing data".into()));
}
Ok(())
}
fn tile_dimensions(layout: Layout) -> Result<(u32, u32, usize), Error> {
if layout.width == 0 || layout.height == 0 || layout.components == 0 {
return Err(Error::Invalid("empty tile layout".into()));
}
let x = layout.width.div_ceil(TILE_SIDE);
let y = layout.height.div_ceil(TILE_SIDE);
let bytes = TILE_AREA.checked_mul(layout.components).and_then(|n| n.checked_mul(2)).ok_or_else(|| Error::Limit("tile is too large".into()))?;
Ok((x, y, bytes))
}
/// Decode tiles one at a time, transferring each tile to the callback.
/// Positions and duplicate tiles are checked before delivery; the entry must end exactly.
pub fn read_tiles(
reader: &mut (impl Read + ?Sized),
expected: Layout,
max_texels: u64,
budget: &mut Budget,
mut tile: impl FnMut(u32, u32, Vec<u16>) -> Result<(), Error>,
) -> Result<(), Error> {
let header = read_header(reader, budget)?;
let texels = u64::from(expected.width) * u64::from(expected.height);
if texels == 0 || texels > max_texels {
return Err(Error::Limit("tile image exceeds its texel limit".into()));
}
if header.layout != expected {
return Err(Error::Invalid("tile layout differs from the project".into()));
}
let (tiles_x, tiles_y, bytes_per_tile) = tile_dimensions(expected)?;
if u64::from(header.count) > u64::from(tiles_x) * u64::from(tiles_y) {
return Err(Error::Invalid("more tiles than the image holds".into()));
}
let mut bytes = Vec::new();
let mut seen = HashSet::new();
for _ in 0..header.count {
let payload = u64::try_from(bytes_per_tile).map_err(|_| Error::Limit("tile is too large".into()))?.saturating_add(8);
charge(budget, payload)?;
if bytes.is_empty() {
bytes.try_reserve_exact(bytes_per_tile).map_err(|_| Error::Limit("tile allocation failed".into()))?;
bytes.resize(bytes_per_tile, 0);
}
let mut pos = [0u8; 8];
reader.read_exact(&mut pos)?;
let x = u32::from_le_bytes([pos[0], pos[1], pos[2], pos[3]]);
let y = u32::from_le_bytes([pos[4], pos[5], pos[6], pos[7]]);
if x >= tiles_x || y >= tiles_y || seen.contains(&(x, y)) {
return Err(Error::Invalid(format!("bad or repeated tile ({x}, {y})")));
}
seen.try_reserve(1).map_err(|_| Error::Limit("tile index allocation failed".into()))?;
seen.insert((x, y));
reader.read_exact(&mut bytes)?;
let mut values = Vec::new();
values.try_reserve_exact(bytes_per_tile / 2).map_err(|_| Error::Limit("tile allocation failed".into()))?;
values.extend(bytes.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)));
tile(x, y, values)?;
}
expect_end(reader)
}
/// Write tiles in caller order, without buffering the image. A failure may leave a partial
/// entry; callers writing a project should discard the incomplete output.
pub fn write_tiles<'a>(
writer: &mut (impl Write + ?Sized),
layout: Layout,
count: u32,
tiles: impl IntoIterator<Item = (u32, u32, &'a [u16])>,
) -> Result<(), Error> {
let (tiles_x, tiles_y, bytes_per_tile) = tile_dimensions(layout)?;
if u64::from(count) > u64::from(tiles_x) * u64::from(tiles_y) {
return Err(Error::Invalid("more tiles than the image holds".into()));
}
write_header(writer, layout, count)?;
let mut seen = HashSet::new();
let mut bytes = Vec::new();
let mut written = 0u32;
for (x, y, values) in tiles {
if written >= count || x >= tiles_x || y >= tiles_y || seen.contains(&(x, y)) || values.len() != bytes_per_tile / 2 {
return Err(Error::Invalid("bad, repeated or incorrectly sized tile".into()));
}
seen.try_reserve(1).map_err(|_| Error::Limit("tile index allocation failed".into()))?;
seen.insert((x, y));
if bytes.is_empty() {
bytes.try_reserve_exact(bytes_per_tile).map_err(|_| Error::Limit("tile allocation failed".into()))?;
}
bytes.clear();
bytes.extend(values.iter().flat_map(|v| v.to_le_bytes()));
writer.write_all(&x.to_le_bytes())?;
writer.write_all(&y.to_le_bytes())?;
writer.write_all(&bytes)?;
written += 1;
}
if written != count {
return Err(Error::Invalid("tile count differs from its data".into()));
}
Ok(())
}
/// Read a dense f32 map, validating declared size and charging its shared budget first.
pub fn read_baked(
reader: &mut (impl Read + ?Sized),
declared_bytes: u64,
components: usize,
max_texels: u64,
budget: &mut Budget,
) -> Result<(Layout, Vec<f32>), Error> {
let header = read_header(reader, budget)?;
let layout = header.layout;
let texels = u64::from(layout.width) * u64::from(layout.height);
if texels == 0 || texels > max_texels || layout.components != components || components == 0 {
return Err(Error::Invalid("bad baked-map dimensions or components".into()));
}
let count = texels.checked_mul(components as u64).ok_or_else(|| Error::Limit("too many map values".into()))?;
let payload = count.checked_mul(4).ok_or_else(|| Error::Limit("map is too large".into()))?;
if declared_bytes != HEADER_BYTES.checked_add(payload).ok_or_else(|| Error::Limit("map is too large".into()))? {
return Err(Error::Invalid(format!("{declared_bytes} bytes for a {}×{}×{} map", layout.width, layout.height, layout.components)));
}
charge(budget, payload)?;
let count = usize::try_from(count).map_err(|_| Error::Limit("too many map values".into()))?;
let mut values = Vec::new();
let chunk_bytes = CHUNK.min(count) * 4;
let mut bytes = Vec::new();
bytes.try_reserve_exact(chunk_bytes).map_err(|_| Error::Limit("map chunk allocation failed".into()))?;
bytes.resize(chunk_bytes, 0);
while values.len() < count {
let n = CHUNK.min(count - values.len());
let chunk = bytes.get_mut(..n * 4).ok_or_else(|| Error::Invalid("bad map chunk".into()))?;
reader.read_exact(chunk)?;
values.try_reserve(n).map_err(|_| Error::Limit("map allocation failed".into()))?;
values.extend(chunk.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)));
}
expect_end(reader)?;
Ok((layout, values))
}
/// Write a dense f32 map in bounded chunks without constructing a second copy of its payload.
pub fn write_baked(writer: &mut (impl Write + ?Sized), layout: Layout, values: &[f32]) -> Result<(), Error> {
let count = u64::from(layout.width).checked_mul(u64::from(layout.height)).and_then(|n| n.checked_mul(layout.components as u64));
if layout.width == 0 || layout.height == 0 || layout.components == 0 || count != Some(values.len() as u64) {
return Err(Error::Invalid("map dimensions differ from its data".into()));
}
write_header(writer, layout, 0)?;
let mut bytes = Vec::new();
bytes.try_reserve_exact(CHUNK * 4).map_err(|_| Error::Limit("map chunk allocation failed".into()))?;
for chunk in values.chunks(CHUNK) {
bytes.clear();
bytes.extend(chunk.iter().flat_map(|v| v.to_le_bytes()));
writer.write_all(&bytes)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
fn header(layout: Layout, count: u32) -> Vec<u8> {
let mut bytes = Vec::new();
write_header(&mut bytes, layout, count).unwrap();
bytes
}
#[test]
fn project_schema_keeps_nested_and_future_fields() {
let json = serde_json::json!({
"format_version": 2, "name": "Stone", "mesh": "mesh.obj", "normal_format": "openGl",
"texture_sets": [{"name": "main", "futureSet": {"keep": true}}],
"layers": {"1": {"kind": "paint", "futureLayer": [1, 2]}}, "next_id": 2,
"display": {"futureDisplay": 4}, "mesh_file": "source.obj", "futureProject": [3]
});
let bytes = serde_json::to_vec(&json).unwrap();
assert_eq!(project_version(&bytes).unwrap(), 2);
let document: ProjectDocument = serde_json::from_slice(&bytes).unwrap();
let mut expected = json;
expected["cameras"] = serde_json::json!([]);
assert_eq!(serde_json::to_value(&document).unwrap(), expected);
let mut duplicate = document;
duplicate.extra.insert("mesh".into(), serde_json::json!("other.obj"));
assert!(serde_json::to_vec(&duplicate).is_err());
assert!(project_version(br#"{"format_version":1,"format_version":2}"#).is_err());
assert!(project_version(br#"{"format_version":-1}"#).is_err());
}
#[test]
fn baked_map_matches_the_native_little_endian_wire_layout() {
let layout = Layout { width: 2, height: 1, components: 1 };
let mut expected = b"TCTX".to_vec();
for value in [1u32, 2, 1, 1, 0] {
expected.extend_from_slice(&value.to_le_bytes());
}
expected.extend_from_slice(&1.0f32.to_le_bytes());
expected.extend_from_slice(&(-0.5f32).to_le_bytes());
let mut actual = Vec::new();
write_baked(&mut actual, layout, &[1.0, -0.5]).unwrap();
assert_eq!(actual, expected);
let (decoded, values) = read_baked(&mut Cursor::new(&expected), expected.len() as u64, 1, 2, &mut Budget::new("project", 32)).unwrap();
assert_eq!(decoded, layout);
assert_eq!(values, [1.0, -0.5]);
expected[4..8].copy_from_slice(&2u32.to_le_bytes());
assert!(matches!(read_baked(&mut Cursor::new(&expected), 32, 1, 2, &mut Budget::new("project", 32)), Err(Error::NewerVersion(2))));
}
#[test]
fn dense_maps_round_trip_bit_exactly_in_bounded_chunks() {
let layout = Layout { width: 300, height: 300, components: 1 };
let mut values = vec![0.25; 90_000];
values[123] = f32::from_bits(0x7fc0_1234);
let mut bytes = Vec::new();
write_baked(&mut bytes, layout, &values).unwrap();
let mut budget = Budget::new("project", bytes.len() as u64);
let (decoded_layout, decoded) = read_baked(&mut Cursor::new(&bytes), bytes.len() as u64, 1, 100_000, &mut budget).unwrap();
assert_eq!(decoded_layout, layout);
assert!(decoded.iter().zip(values.iter()).all(|(a, b)| a.to_bits() == b.to_bits()));
assert_eq!(budget.remaining(), 0);
}
#[test]
fn dense_maps_refuse_false_lengths_limits_and_trailing_data() {
let layout = Layout { width: 2, height: 2, components: 1 };
let mut bytes = Vec::new();
write_baked(&mut bytes, layout, &[1.0; 4]).unwrap();
let mut budget = Budget::new("project", 100);
assert!(read_baked(&mut Cursor::new(&bytes), 999, 1, 4, &mut budget).is_err());
let mut budget = Budget::new("project", 39);
assert!(matches!(read_baked(&mut Cursor::new(&bytes), 40, 1, 4, &mut budget), Err(Error::Budget(_))));
let mut extra = bytes.clone();
extra.push(0);
let mut budget = Budget::new("project", 100);
assert!(read_baked(&mut Cursor::new(extra), 40, 1, 4, &mut budget).is_err());
}
#[test]
fn hostile_headers_and_tiles_are_rejected_without_trusting_counts() {
let layout = Layout { width: 64, height: 64, components: 1 };
assert!(matches!(
read_tiles(&mut Cursor::new(header(layout, 0)), layout, 4095, &mut Budget::new("project", 100), |_, _, _| Ok(())),
Err(Error::Limit(_))
));
let mut bad_magic = header(layout, 0);
bad_magic[0] = b'X';
assert!(read_tiles(&mut Cursor::new(bad_magic), layout, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err());
assert!(read_tiles(&mut Cursor::new(header(layout, 2)), layout, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err());
let other = Layout { width: 65, ..layout };
assert!(read_tiles(&mut Cursor::new(header(layout, 0)), other, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err());
let mut outside = header(layout, 1);
outside.extend_from_slice(&1u32.to_le_bytes());
outside.extend_from_slice(&0u32.to_le_bytes());
assert!(read_tiles(&mut Cursor::new(outside), layout, u64::MAX, &mut Budget::new("project", 10_000), |_, _, _| Ok(())).is_err());
let mut truncated = header(layout, 1);
truncated.extend_from_slice(&[0; 8]);
assert!(read_tiles(&mut Cursor::new(truncated), layout, u64::MAX, &mut Budget::new("project", 10_000), |_, _, _| Ok(())).is_err());
let mut extra = header(layout, 0);
extra.push(1);
assert!(read_tiles(&mut Cursor::new(extra), layout, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err());
}
#[test]
fn sparse_tiles_stream_and_reject_duplicates_and_short_budget() {
let layout = Layout { width: 128, height: 64, components: 1 };
let mut bytes = header(layout, 2);
for x in [0u32, 1] {
bytes.extend_from_slice(&x.to_le_bytes());
bytes.extend_from_slice(&0u32.to_le_bytes());
for _ in 0..TILE_AREA {
bytes.extend_from_slice(&(x as u16 + 1).to_le_bytes());
}
}
let first = vec![1u16; TILE_AREA];
let second = vec![2u16; TILE_AREA];
let mut written = Vec::new();
write_tiles(&mut written, layout, 2, [(0, 0, first.as_slice()), (1, 0, second.as_slice())]).unwrap();
assert_eq!(written, bytes, "the streamed writer keeps the native wire layout");
assert!(write_tiles(&mut Vec::new(), layout, 2, [(0, 0, first.as_slice())]).is_err());
let mut seen = Vec::new();
let mut budget = Budget::new("project", bytes.len() as u64);
read_tiles(&mut Cursor::new(&bytes), layout, u64::MAX, &mut budget, |x, y, tile| {
seen.push((x, y, tile.first().copied()));
Ok(())
})
.unwrap();
assert_eq!(seen, [(0, 0, Some(1)), (1, 0, Some(2))]);
let mut duplicate = bytes.clone();
let second = HEADER_BYTES as usize + 8 + TILE_AREA * 2;
duplicate[second..second + 4].copy_from_slice(&0u32.to_le_bytes());
assert!(read_tiles(&mut Cursor::new(duplicate), layout, u64::MAX, &mut Budget::new("project", u64::MAX), |_, _, _| Ok(())).is_err());
assert!(matches!(
read_tiles(&mut Cursor::new(bytes), layout, u64::MAX, &mut Budget::new("project", HEADER_BYTES), |_, _, _| Ok(())),
Err(Error::Budget(_))
));
}
}
+3
View File
@@ -15,3 +15,6 @@ flate2 = { version = "1", default-features = false, features = ["rust_backend"]
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
[dev-dependencies]
proptest = { workspace = true }
+12 -1
View File
@@ -34,6 +34,7 @@
//! [`sniff`] reads: a reader of a compressed file decompresses it with its own limits first.
pub mod base64;
pub mod stream;
use std::collections::{BTreeMap, HashMap};
use std::fmt;
@@ -769,10 +770,20 @@ impl<'a> Tail<'a> {
/// Write what follows the JSON: [`BLOB_MAGIC`] and the entries, or nothing when there are none.
pub fn write_to(&self, mut w: impl Write) -> io::Result<()> {
self.write_to_with(&mut w, |_, _| Ok(()))
}
/// Write the marker and parts, calling `before_part` before each payload. A compressed
/// writer can use the MIME type to switch between compressed and stored deflate blocks.
pub fn write_to_with<W: Write>(&self, mut w: W, mut before_part: impl FnMut(&mut W, &str) -> io::Result<()>) -> io::Result<()> {
if self.parts.is_empty() {
return Ok(());
}
w.write_all(BLOB_MAGIC)?;
self.parts.iter().try_for_each(|p| w.write_all(p.bytes))
for part in &self.parts {
before_part(&mut w, &part.mime)?;
w.write_all(part.bytes)?;
}
Ok(())
}
}
+302
View File
@@ -0,0 +1,302 @@
//! Streaming helpers for the native JSON envelope.
//!
//! Field values are emitted in bounded chunks. The decoded JSON and tail framing stay the same
//! as VectorCraft's previous writer, but compressed bytes can differ when a deflate backend's
//! output depends on input chunking. Gzip readers still validate the CRC and size trailer.
//!
//! Copyright (c) 2026 ArtCraft Team and the VectorCraft contributors.
//! Extracted from VectorCraft, licensed under MIT OR Apache-2.0.
use serde::Serialize;
use std::collections::{BTreeMap, HashSet};
use std::io::{self, Write};
/// Native file metadata for streaming a JSON envelope. Asset entries should be prepared using
/// the same [`crate::Tail`] whose parts will be written after the JSON.
pub struct FileHeader<'a> {
/// Native format version to write.
pub version: u32,
/// Application generator string.
pub generator: &'a str,
/// Optional saved PDF compatibility preference.
pub pdf_compatible: Option<bool>,
/// Optional inline preview.
pub preview: Option<&'a crate::Image>,
/// Embedded profiles keyed by profile name.
pub profiles: &'a BTreeMap<&'a str, crate::Image>,
/// Optional PDF entry.
pub pdf: Option<&'a crate::Image>,
}
/// Write the native JSON envelope, streaming the document through `document`. The callback
/// returns the image entries after serializing the document, allowing it to collect image keys
/// without keeping a copy of the document JSON. It must write one complete JSON value.
pub fn write_file_json<'a>(
mut out: impl Write,
header: &FileHeader<'a>,
pretty: bool,
document: impl FnOnce(&mut dyn Write) -> io::Result<BTreeMap<&'a str, crate::Image>>,
) -> io::Result<()> {
if !(crate::MIN_VERSION..=crate::VERSION).contains(&header.version) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "unsupported native file version"));
}
let mut env = Envelope::new(pretty);
let format = if header.version < 3 { crate::LEGACY_EXTENSION } else { crate::EXTENSION };
env.field(&mut out, "format", &format)?;
env.field(&mut out, "version", &header.version)?;
env.field(&mut out, "generator", &header.generator)?;
if let Some(compatible) = header.pdf_compatible {
env.field(&mut out, "pdfCompatible", &compatible)?;
}
if let Some(preview) = header.preview {
env.field(&mut out, "preview", preview)?;
}
if !header.profiles.is_empty() {
env.field(&mut out, "profiles", header.profiles)?;
}
if let Some(pdf) = header.pdf {
env.field(&mut out, "pdf", pdf)?;
}
env.key(&mut out, "document")?;
let images = document(&mut Indent { inner: &mut out, on: pretty })?;
env.field(&mut out, "images", &images)?;
env.finish(&mut out)
}
/// Indents JSON text written through it one level (two spaces after each newline), as a member
/// of the file's top-level object; newlines only occur between tokens, inside strings they are
/// escaped.
pub struct Indent<W: Write> {
pub inner: W,
pub on: bool,
}
impl<W: Write> Write for Indent<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.write_all(buf)?;
Ok(buf.len())
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
if !self.on {
return self.inner.write_all(buf);
}
let mut lines = buf.split(|&b| b == b'\n');
self.inner.write_all(lines.next().unwrap_or_default())?;
for line in lines {
self.inner.write_all(b"\n ")?;
self.inner.write_all(line)?;
}
Ok(())
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
/// The member that names an image.
const TAG: &[u8] = b"\"key\":";
/// Passes JSON text through, collecting the string values of every `"key": "…"` member at any
/// depth (what the images a document uses are named by). Quotes inside JSON strings are escaped,
/// so every match is a real member.
pub struct KeyScan<W: Write> {
pub inner: W,
pub keys: HashSet<String>,
/// Looking for keys (else it only passes the text on).
pub active: bool,
/// How much of [`TAG`] the last bytes matched.
matched: usize,
/// Right after a tag: skipping whitespace to its value.
after_tag: bool,
/// The value string so far, quotes included.
value: Option<Vec<u8>>,
escaped: bool,
}
impl<W: Write> KeyScan<W> {
pub fn new(inner: W) -> Self {
Self { inner, keys: HashSet::new(), active: true, matched: 0, after_tag: false, value: None, escaped: false }
}
fn scan(&mut self, buf: &[u8]) {
if !self.active {
return;
}
let mut i = 0;
while let Some(&b) = buf.get(i) {
// Nothing in progress: skip to the next quote.
if self.matched == 0 && !self.after_tag && self.value.is_none() {
match buf.get(i..).and_then(|r| r.iter().position(|&c| c == b'"')) {
Some(at) => i += at,
None => return,
}
self.matched = 1;
i += 1;
continue;
}
self.step(b);
i += 1;
}
}
fn step(&mut self, b: u8) {
if let Some(v) = self.value.as_mut() {
v.push(b);
if self.escaped {
self.escaped = false;
} else if b == b'\\' {
self.escaped = true;
} else if b == b'"'
&& let Some(v) = self.value.take()
&& let Ok(s) = serde_json::from_slice::<String>(&v)
{
self.keys.insert(s);
}
} else if self.after_tag && !b.is_ascii_whitespace() {
self.after_tag = false;
if b == b'"' {
self.value = Some(vec![b]);
}
}
if TAG.get(self.matched) == Some(&b) {
self.matched += 1;
if self.matched == TAG.len() {
self.matched = 0;
self.after_tag = true;
self.value = None;
self.escaped = false;
}
} else {
self.matched = usize::from(b == b'"');
}
}
}
impl<W: Write> Write for KeyScan<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.write_all(buf)?;
Ok(buf.len())
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.scan(buf);
self.inner.write_all(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
/// The file's top-level object, written member by member (the document is streamed in).
pub struct Envelope {
pretty: bool,
first: bool,
}
impl Envelope {
/// Start an object with optional indentation.
pub fn new(pretty: bool) -> Self {
Self { pretty, first: true }
}
/// Serialize a member directly to the output without buffering the full value.
pub fn field(&mut self, out: &mut impl Write, key: &str, value: &impl Serialize) -> io::Result<()> {
self.key(out, key)?;
// serde_json emits small writes; keep compression and underlying I/O efficient while
// bounding temporary memory independently of the serialized table size.
let mut buffered = io::BufWriter::with_capacity(64 << 10, out);
let indented = Indent { inner: &mut buffered, on: self.pretty };
if self.pretty { serde_json::to_writer_pretty(indented, value) } else { serde_json::to_writer(indented, value) }.map_err(io::Error::other)?;
buffered.into_inner().map(|_| ()).map_err(|e| e.into_error())
}
/// The start of member `key` (a plain name): its value follows, indented one level when
/// pretty (as serde_json would: see [`Indent`]).
pub fn key(&mut self, out: &mut impl Write, key: &str) -> io::Result<()> {
let sep: &[u8] = if std::mem::take(&mut self.first) { b"{" } else { b"," };
let indent: &[u8] = if self.pretty { b"\n " } else { b"" };
out.write_all(sep)?;
out.write_all(indent)?;
serde_json::to_writer(&mut *out, key).map_err(io::Error::other)?;
out.write_all(if self.pretty { b": " } else { b":" })
}
/// Close the object.
pub fn finish(self, out: &mut impl Write) -> io::Result<()> {
if self.first {
return out.write_all(b"{}");
}
out.write_all(if self.pretty { b"\n}" } else { b"}" })
}
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;
/// How the keys were found before saves were streamed: in the whole JSON text at once.
fn string_members(json: &[u8]) -> HashSet<String> {
let mut out = HashSet::new();
let mut rest = json;
while let Some(at) = rest.windows(TAG.len()).position(|w| w == TAG) {
rest = rest.get(at + TAG.len()..).unwrap_or_default();
let value = rest.trim_ascii_start();
if value.first() == Some(&b'"')
&& let Some(Ok(s)) = serde_json::Deserializer::from_slice(value).into_iter::<String>().next()
{
out.insert(s);
}
}
out
}
/// The keys `KeyScan` finds in `json` written in pieces of `chunk` bytes.
fn scanned(json: &[u8], chunk: usize) -> HashSet<String> {
let mut scan = KeyScan::new(Vec::new());
for piece in json.chunks(chunk.max(1)) {
scan.write_all(piece).unwrap();
}
assert_eq!(scan.inner, json, "the text passes through");
scan.keys
}
#[test]
fn finds_the_keys_the_whole_text_search_found() {
let v = serde_json::json!({"key": "a", "x": [{"key": "b \"q\" \\ é"}, {"monkey": "no"}, {"k": "key"}], "s": "\"key\": \"no\"", "key2": "n", "o": {"key": 3}});
for json in [serde_json::to_vec(&v).unwrap(), serde_json::to_vec_pretty(&v).unwrap()] {
let want = string_members(&json);
assert_eq!(want, HashSet::from(["a".to_string(), "b \"q\" \\ é".to_string()]));
for chunk in [1, 2, 3, 7, 64, json.len()] {
assert_eq!(scanned(&json, chunk), want, "chunk {chunk}");
}
}
}
fn arb_json() -> impl Strategy<Value = serde_json::Value> {
let key = prop::sample::select(vec!["key", "k", "kkey", "key ", "x"]).prop_map(str::to_string);
let leaf = prop_oneof![
Just(serde_json::Value::Null),
any::<i32>().prop_map(serde_json::Value::from),
"[a-z\"\\\\: ké\n]{0,8}".prop_map(serde_json::Value::from),
];
leaf.prop_recursive(4, 32, 6, move |inner| {
prop_oneof![
prop::collection::vec(inner.clone(), 0..5).prop_map(serde_json::Value::from),
prop::collection::vec((key.clone(), inner), 0..5).prop_map(|m| serde_json::Value::Object(m.into_iter().collect())),
]
})
}
proptest! {
#[test]
fn scanning_in_pieces_finds_what_the_whole_text_search_found(v in arb_json(), chunk in 1usize..40, pretty in any::<bool>()) {
let json = if pretty { serde_json::to_vec_pretty(&v) } else { serde_json::to_vec(&v) }.unwrap();
prop_assert_eq!(scanned(&json, chunk), string_members(&json));
}
}
}
+182
View File
@@ -0,0 +1,182 @@
use craft_vectorcraft_format::stream::{Envelope, Indent, KeyScan};
use std::io::{self, Write};
#[test]
fn envelope_streams_nested_json_and_escapes_keys() {
for pretty in [false, true] {
let mut bytes = Vec::new();
let mut env = Envelope::new(pretty);
env.field(&mut bytes, "quoted\"key", &vec![1, 2]).unwrap();
env.key(&mut bytes, "document").unwrap();
let mut output = Indent { inner: &mut bytes, on: pretty };
if pretty {
serde_json::to_writer_pretty(&mut output, &serde_json::json!({"nested": [3,4]})).unwrap();
} else {
serde_json::to_writer(&mut output, &serde_json::json!({"nested": [3,4]})).unwrap();
}
env.finish(&mut bytes).unwrap();
let value: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(value, serde_json::json!({"quoted\"key": [1, 2], "document": {"nested": [3,4]}}));
let mut expected = Vec::new();
let empty = Envelope::new(pretty);
empty.finish(&mut expected).unwrap();
assert_eq!(expected, b"{}");
}
}
#[test]
fn scan_survives_every_chunk_boundary_and_forwards_data() {
let json = br#"{"key":"one","nested":[{"key":"two \\\"quoted\\\""},{"key":42}],"other":"x"}"#;
for chunk in 1..=json.len() {
let mut scan = KeyScan::new(Vec::new());
for part in json.chunks(chunk) {
scan.write_all(part).unwrap();
}
assert_eq!(scan.inner, json);
assert!(scan.keys.contains("one"));
assert_eq!(scan.keys.len(), 2);
}
}
struct Broken;
impl Write for Broken {
fn write(&mut self, _: &[u8]) -> io::Result<usize> {
Err(io::Error::other("failure"))
}
fn flush(&mut self) -> io::Result<()> {
Err(io::Error::other("failure"))
}
}
#[test]
fn forwards_writer_errors() {
assert!(Envelope::new(false).field(&mut Broken, "x", &1).is_err());
assert!(KeyScan::new(Broken).write_all(b"{}").is_err());
assert!(Indent { inner: Broken, on: true }.write_all(b"x\ny").is_err());
}
#[test]
fn tail_calls_hook_in_order_and_propagates_errors() {
use craft_vectorcraft_format::{BLOB_MAGIC, INLINE_MAX, Tail};
let first = vec![1; INLINE_MAX];
let second = vec![2; INLINE_MAX];
let mut tail = Tail::new(4);
tail.image("image/png", &first, false);
tail.image("application/pdf", &second, false);
let mut out = Vec::new();
let mut seen = Vec::new();
tail.write_to_with(&mut out, |_, mime| {
seen.push(mime.to_owned());
Ok(())
})
.unwrap();
assert_eq!(seen, ["image/png", "application/pdf"]);
assert_eq!(out, [BLOB_MAGIC, &first, &second].concat());
assert!(tail.write_to_with(Vec::new(), |_, _| Err(io::Error::other("failure"))).is_err());
let mut called = false;
Tail::new(4)
.write_to_with(Vec::new(), |_, _| {
called = true;
Ok(())
})
.unwrap();
assert!(!called);
}
#[test]
fn native_schema_order_and_versions() {
use craft_vectorcraft_format::stream::{FileHeader, write_file_json};
let profiles = Default::default();
for (version, name) in [(1, "drawcraft"), (4, "vectorcraft")] {
for pretty in [false, true] {
let header = FileHeader { version, generator: "test", pdf_compatible: Some(false), preview: None, profiles: &profiles, pdf: None };
let mut out = Vec::new();
write_file_json(&mut out, &header, pretty, |w| {
w.write_all(if pretty { b"{\n \"key\": \"image\"\n}" } else { b"{\"key\":\"image\"}" })?;
Ok(Default::default())
})
.unwrap();
let value: serde_json::Value = serde_json::from_slice(&out).unwrap();
assert_eq!(value["format"], name);
assert_eq!(value["pdfCompatible"], false);
let text = std::str::from_utf8(&out).unwrap();
assert!(text.find("\"format\"").unwrap() < text.find("\"version\"").unwrap());
assert!(text.find("\"document\"").unwrap() < text.find("\"images\"").unwrap());
}
}
let header = FileHeader { version: 0, generator: "test", pdf_compatible: None, preview: None, profiles: &profiles, pdf: None };
assert!(write_file_json(Vec::new(), &header, false, |_| Ok(Default::default())).is_err());
}
// Oracle for the previous app writer: each member was first serialized to a Vec, then
// indented and appended. Compare decoded file bytes, not backend-dependent gzip bytes.
#[test]
fn streamed_native_file_matches_previous_framing() {
use craft_vectorcraft_format::{
Tail,
stream::{FileHeader, write_file_json},
};
use flate2::{Compression, read::GzDecoder, write::GzEncoder};
use std::collections::BTreeMap;
use std::io::Read;
fn old_field(out: &mut Vec<u8>, first: &mut bool, pretty: bool, key: &str, value: &impl serde::Serialize) {
out.extend_from_slice(if std::mem::take(first) { b"{" } else { b"," });
if pretty {
out.extend_from_slice(b"\n ");
}
out.extend_from_slice(format!("\"{key}\"{}", if pretty { ": " } else { ":" }).as_bytes());
let json = if pretty { serde_json::to_vec_pretty(value) } else { serde_json::to_vec(value) }.unwrap();
let mut indent = Indent { inner: out, on: pretty };
indent.write_all(&json).unwrap();
}
let payload = vec![42; craft_vectorcraft_format::INLINE_MAX];
for version in [1, 3, 4] {
for pretty in [false, true] {
let mut tail = Tail::new(version);
let image = tail.image("image/png", &payload, false);
let profile = tail.image("application/vnd.iccprofile", &payload, false);
let pdf = tail.image("application/pdf", &payload, false);
let profiles = BTreeMap::from([("profile", profile)]);
let images = BTreeMap::from([("image", image)]);
let document = serde_json::json!({"layers": [{"key": "image", "name": "A"}]});
let header = FileHeader {
version,
generator: "VectorCraft test",
pdf_compatible: Some(true),
preview: None,
profiles: &profiles,
pdf: Some(&pdf),
};
let mut actual = Vec::new();
write_file_json(&mut actual, &header, pretty, |w| {
if pretty { serde_json::to_writer_pretty(w, &document) } else { serde_json::to_writer(w, &document) }.map_err(io::Error::other)?;
Ok(images.clone())
})
.unwrap();
tail.write_to(&mut actual).unwrap();
let mut old = Vec::new();
let mut first = true;
let format = if version < 3 { "drawcraft" } else { "vectorcraft" };
old_field(&mut old, &mut first, pretty, "format", &format);
old_field(&mut old, &mut first, pretty, "version", &version);
old_field(&mut old, &mut first, pretty, "generator", &"VectorCraft test");
old_field(&mut old, &mut first, pretty, "pdfCompatible", &true);
old_field(&mut old, &mut first, pretty, "profiles", &profiles);
old_field(&mut old, &mut first, pretty, "pdf", &pdf);
old_field(&mut old, &mut first, pretty, "document", &document);
old_field(&mut old, &mut first, pretty, "images", &images);
old.extend_from_slice(if pretty { b"\n}" } else { b"}" });
tail.write_to(&mut old).unwrap();
assert_eq!(actual, old, "version {version}, pretty {pretty}");
for bytes in [&old, &actual] {
let mut gzip = GzEncoder::new(Vec::new(), Compression::default());
gzip.write_all(bytes).unwrap();
let mut decoded = Vec::new();
GzDecoder::new(gzip.finish().unwrap().as_slice()).read_to_end(&mut decoded).unwrap();
assert_eq!(decoded, old);
}
}
}
}
+6
View File
@@ -34,6 +34,8 @@ 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"]
# FFV1 lossless video (RFC 9043): version 3, range coder, decoder and encoder.
ffv1 = []
# Apple ProRes (SMPTE RDD 36): decoder and encoder.
prores = ["craft-media/bitstream"]
# H.264 / AVC: decoder and encoder; `h264-decode` or `h264-encode` for one of them.
@@ -143,6 +145,10 @@ required-features = ["mpeg4-encode"]
name = "mpeg2"
required-features = ["mpeg2-decode"]
[[test]]
name = "ffv1"
required-features = ["ffv1"]
[[test]]
name = "prores"
required-features = ["prores"]
+52
View File
@@ -0,0 +1,52 @@
# FFV1
Clean-room FFV1 lossless video codec, written from RFC 9043. Feature `ffv1`.
## What is supported
| | Decoder | Encoder |
|---|---|---|
| Version | 3 (the one with a configuration record) | 3 |
| Coder | range coder, default or custom state transition table | range coder, default table |
| Colour | YCbCr (any 2^n chroma subsampling, gray, optional transparency), RGB through the JPEG 2000 reversible colour transform (optional transparency) | the same, RGB up to 15 bits |
| Depth | 8 to 16 bits per sample | 8 to 16 bits (RGB 8 to 15) |
| Slices | any raster whose boundaries are multiples of the chroma subsampling | the same |
| Frames | keyframes | keyframes only (`intra` 1) |
| Error detection | slice and configuration record CRCs, checked | written |
Not supported, and reported as `Error::Unsupported`: the Golomb-Rice coder (`coder_type` 0),
versions 0 and 1 (parameters inside each keyframe, no configuration record), inter frames
(`keyframe` 0), and slice rasters whose boundaries fall inside a chroma sample.
## Using it
The configuration record goes in the container (Matroska `CodecPrivate`, the AVI `strf`
extension, an MP4 `glbl` box); each frame is one packet.
```text
let enc = Encoder::new(&template_frame, EncoderConfig::default())?;
let config = enc.config_record();
let packet = enc.encode(&frame)?;
let dec = Decoder::new(&config, width, height)?;
let frame = dec.decode(&packet)?;
```
## Notes on the format
- The Parameters in the configuration record are range coded with the *default* state transition
table. A custom table (`coder_type` 2) applies to the slices only.
- Each Quantization Table starts from fresh states; a Quantization Table Set is five of them.
- A slice is one range-coded stream: for the first slice the `keyframe` bit, then the slice header,
then the samples. Cb and Cr share one set of context states; transparency has its own.
- The context is the sum of five quantised differences. With the default tables this encoder
writes 15125 contexts; FFmpeg refuses tables that multiply to more than 32768, so the encoder
stays below that although the RFC's limit (on half the product) is looser.
- The encoder ends a stream with the top of its final range, so bytes after it (a CRC in the
configuration record) cannot change what decodes.
## Verification
`tests/ffv1.rs`: round trips over every shape, hostile input (every truncation and bit flip),
and ffmpeg as the oracle in both directions (skipped without ffmpeg, a failure with
`CRAFT_MEDIA_REQUIRE_ORACLES=1`).
+929
View File
@@ -0,0 +1,929 @@
//! FFV1 lossless video codec (RFC 9043): version 3, range coder, intra-only, YCbCr and RGB (JPEG 2000
//! RCT), 8 to 16 bits per sample, any number of slices.
//!
//! - [`Decoder`]: built from the *configuration record* a container stores (Matroska `CodecPrivate`,
//! AVI `strf` extension, MP4 `glbl`) and the frame size; [`Decoder::decode`] turns one packet into a
//! [`Frame`]. Slice CRCs are checked.
//! - [`Encoder`]: [`Encoder::config_record`] gives the bytes for the container, [`Encoder::encode`]
//! one packet per frame; every frame is a keyframe.
//!
//! Not supported (reported as [`Error::Unsupported`]): the Golomb-Rice coder (`coder_type` 0),
//! versions 0 and 1 (no configuration record, parameters in every keyframe), inter frames
//! (`intra` 0 with `keyframe` 0), and slice rasters whose boundaries are not multiples of the
//! chroma subsampling. See `src/ffv1/README.md`.
//!
//! Implemented from RFC 9043. Frames are planar `u16`: for YCbCr, `[Y, Cb, Cr]` (plus an optional
//! transparency plane); for RGB, `[R, G, B]` (plus transparency), all at full size.
mod rc;
use crate::budget::{DECODE_MEMORY_BUDGET, PictureShape};
use rc::{Decoder as Rac, Encoder as RacEnc, Transitions};
const CONTEXT_SIZE: usize = 32;
const MAX_CONTEXT_INPUTS: usize = 5;
const MAX_CONTEXTS: u64 = 32768;
/// Codec errors.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
/// The data ends early or its sizes do not add up.
#[error("truncated FFV1 data")]
Truncated,
/// The data is not valid FFV1.
#[error("invalid FFV1 data: {0}")]
Invalid(&'static str),
/// Valid FFV1 that this module does not decode.
#[error("unsupported FFV1 feature: {0}")]
Unsupported(&'static str),
/// A slice or the configuration record failed its CRC.
#[error("FFV1 CRC mismatch in {0}")]
Crc(&'static str),
/// The encoder was given a frame or settings it cannot encode.
#[error("invalid encoder input: {0}")]
Input(String),
}
/// `Result` of this module.
pub type Result<T> = std::result::Result<T, Error>;
/// The colour space coded (RFC 9043 table 8).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Colorspace {
/// YCbCr, coded plane by plane without a pixel transform.
YCbCr,
/// RGB through the JPEG 2000 reversible colour transform, coded line by line.
Rgb,
}
/// One frame of planar samples.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame {
/// Width in pixels.
pub width: u32,
/// Height in pixels.
pub height: u32,
/// Colour space of the planes.
pub colorspace: Colorspace,
/// Bits per sample, 8 to 16.
pub bit_depth: u8,
/// log2 of the horizontal and vertical chroma subsampling (YCbCr only; 0, 0 is 4:4:4).
pub chroma_shift: (u8, u8),
/// The planes, in the order the module doc gives.
pub planes: Vec<Vec<u16>>,
}
impl Frame {
/// Whether the planes are Y, Cb, Cr (or R, G, B) rather than Y (gray) alone, or with transparency.
fn plane_count_ok(&self) -> bool {
matches!(self.planes.len(), 1..=4)
}
fn dims(&self, plane: usize, chroma_planes: bool) -> (usize, usize) {
plane_dims(self.width as usize, self.height as usize, self.colorspace, self.chroma_shift, plane, chroma_planes)
}
}
/// `(width, height)` of plane `p` for a full frame (or a slice) of `w` x `h`.
fn plane_dims(w: usize, h: usize, cs: Colorspace, shift: (u8, u8), p: usize, chroma_planes: bool) -> (usize, usize) {
if cs == Colorspace::YCbCr && chroma_planes && (p == 1 || p == 2) { (w.div_ceil(1 << shift.0), h.div_ceil(1 << shift.1)) } else { (w, h) }
}
/// One Quantization Table Set (section 4.1).
#[derive(Clone)]
struct QuantSet {
tables: Vec<[i32; 256]>,
context_count: usize,
}
/// The parsed "Parameters" of a stream.
#[derive(Clone)]
struct Params {
coder_type: u32,
transitions: Transitions,
colorspace: Colorspace,
bits: u32,
chroma_planes: bool,
shift: (u8, u8),
extra_plane: bool,
num_h: u32,
num_v: u32,
sets: Vec<QuantSet>,
initial_states: Vec<Option<Vec<[u8; CONTEXT_SIZE]>>>,
ec: bool,
intra: bool,
}
impl Params {
fn plane_count(&self) -> usize {
1 + if self.chroma_planes { 2 } else { 0 } + usize::from(self.extra_plane)
}
/// Quantization Table Set index slot of plane `p` (section 3.6).
fn qset_slot(&self, p: usize) -> usize {
match p {
0 => 0,
1 | 2 if self.chroma_planes => 1,
_ => 2,
}
}
}
fn crc32(data: &[u8]) -> u32 {
// The IEEE polynomial 0x04C11DB7, initial value 0, no reflection, no final inversion.
let mut crc = 0u32;
for &b in data {
crc ^= u32::from(b) << 24;
for _ in 0..8 {
crc = if crc & 0x8000_0000 != 0 { crc << 1 ^ 0x04C1_1DB7 } else { crc << 1 };
}
}
crc
}
// ---- Parameters ----
fn read_quant_set(rac: &mut Rac, t: &Transitions) -> Result<QuantSet> {
let mut scale: u64 = 1;
let mut tables = Vec::with_capacity(MAX_CONTEXT_INPUTS);
for _ in 0..MAX_CONTEXT_INPUTS {
let mut table = [0i32; 256];
// Each quantization table starts from fresh states.
let mut state = [128u8; CONTEXT_SIZE];
let mut v: i64 = 0;
let mut k = 0usize;
while k < 128 {
let len = rac.symbol(&mut state, false, t) as usize + 1;
if k + len > 128 {
return Err(Error::Invalid("quantization table run passes its half"));
}
for _ in 0..len {
table[k] = i32::try_from(scale as i64 * v).map_err(|_| Error::Invalid("quantization table overflows"))?;
k += 1;
}
v += 1;
}
for k in 1..128 {
table[256 - k] = -table[k];
}
table[128] = -table[127];
tables.push(table);
scale = scale.checked_mul(2 * v as u64 - 1).filter(|&s| s <= 2 * MAX_CONTEXTS).ok_or(Error::Invalid("too many contexts"))?;
}
let context_count = scale.div_ceil(2);
if context_count > MAX_CONTEXTS {
return Err(Error::Invalid("too many contexts"));
}
Ok(QuantSet { tables, context_count: context_count as usize })
}
fn parse_params(rac: &mut Rac) -> Result<Params> {
let mut state = [128u8; CONTEXT_SIZE];
// Until the custom table is known, the default table decodes the fields before it.
let t0 = Transitions::default_table();
let version = rac.symbol(&mut state, false, &t0);
if version != 3 {
return Err(Error::Unsupported("only FFV1 version 3 has a configuration record; versions 0 and 1 are not supported"));
}
let _micro = rac.symbol(&mut state, false, &t0);
let coder_type = rac.symbol(&mut state, false, &t0) as u32;
let mut delta = [0i32; 256];
if coder_type > 1 {
for d in delta.iter_mut().skip(1) {
*d = rac.symbol(&mut state, true, &t0);
}
}
let transitions = Transitions::new(&delta);
if coder_type == 0 {
return Err(Error::Unsupported("the Golomb-Rice coder (coder_type 0)"));
}
if coder_type > 2 {
return Err(Error::Invalid("unknown coder_type"));
}
let colorspace = match rac.symbol(&mut state, false, &t0) {
0 => Colorspace::YCbCr,
1 => Colorspace::Rgb,
_ => return Err(Error::Unsupported("colorspace_type")),
};
let bits = rac.symbol(&mut state, false, &t0) as u32;
let bits = if bits == 0 { 8 } else { bits };
let chroma_planes = rac.bit(&mut state[0], &t0);
let (h, v) = (rac.symbol(&mut state, false, &t0), rac.symbol(&mut state, false, &t0));
let extra_plane = rac.bit(&mut state[0], &t0);
let num_h = rac.symbol(&mut state, false, &t0) as u32 + 1;
let num_v = rac.symbol(&mut state, false, &t0) as u32 + 1;
let set_count = rac.symbol(&mut state, false, &t0);
if !(8..=16).contains(&bits) || h > 3 || v > 3 || h < 0 || v < 0 || num_h > 1024 || num_v > 1024 || !(1..=8).contains(&set_count) {
return Err(Error::Invalid("parameter out of range"));
}
if colorspace == Colorspace::Rgb && (!chroma_planes || h != 0 || v != 0) {
return Err(Error::Unsupported("RGB with chroma_planes 0 or subsampling"));
}
let mut sets = Vec::new();
for _ in 0..set_count {
// The Parameters are range coded with the default table; the custom one is for the slices.
sets.push(read_quant_set(rac, &t0)?);
}
let mut initial_states = Vec::new();
for set in &sets {
if rac.bit(&mut state[0], &t0) {
let mut all = Vec::with_capacity(set.context_count);
let mut prev = [128u8; CONTEXT_SIZE];
for _ in 0..set.context_count {
let mut cur = [0u8; CONTEXT_SIZE];
for k in 0..CONTEXT_SIZE {
let d = rac.symbol(&mut state, true, &t0);
cur[k] = (i32::from(prev[k]) + d) as u8;
}
prev = cur;
all.push(cur);
}
initial_states.push(Some(all));
} else {
initial_states.push(None);
}
}
let ec = rac.symbol(&mut state, false, &t0) != 0;
let intra = rac.symbol(&mut state, false, &t0) != 0;
Ok(Params {
coder_type,
transitions,
colorspace,
bits,
chroma_planes,
shift: (h as u8, v as u8),
extra_plane,
num_h,
num_v,
sets,
initial_states,
ec,
intra,
})
}
/// The default quantisation tables of this encoder: 11 levels for the two gradients next to the
/// sample and 5 for the other three, which makes 15125 contexts (FFmpeg refuses more than 32768).
fn default_quant_set() -> QuantSet {
// |difference| thresholds: the first |d| of each level above 0 (levels 0..=5 and 0..=2).
let first_of_level: [&[usize]; MAX_CONTEXT_INPUTS] = [&[1, 2, 4, 8, 16], &[1, 2, 4, 8, 16], &[1, 3], &[1, 3], &[1, 3]];
let mut scale: i32 = 1;
let mut tables = Vec::new();
let mut counts = [0usize; MAX_CONTEXT_INPUTS];
for (j, firsts) in first_of_level.iter().enumerate() {
let mut table = [0i32; 256];
for (k, slot) in table.iter_mut().take(128).enumerate() {
let level = firsts.iter().filter(|&&f| k >= f).count() as i32;
*slot = scale * level;
}
for k in 1..128 {
table[256 - k] = -table[k];
}
table[128] = -table[127];
counts[j] = firsts.len() + 1;
scale *= 2 * counts[j] as i32 - 1;
tables.push(table);
}
QuantSet { tables, context_count: (scale as usize).div_ceil(2) }
}
/// Run lengths (`len - 1` per step) that describe the first half of a table, as written to the stream.
fn quant_runs(table: &[i32; 256]) -> Vec<i32> {
let mut runs = Vec::new();
let mut k = 0usize;
while k < 128 {
let start = k;
while k < 128 && table[k] == table[start] {
k += 1;
}
runs.push((k - start) as i32 - 1);
}
runs
}
fn write_params(enc: &mut RacEnc, p: &Params) {
let t = Transitions::default_table();
let mut state = [128u8; CONTEXT_SIZE];
enc.symbol(&mut state, 3, false, &t); // version
enc.symbol(&mut state, 4, false, &t); // micro_version: first stable variant
enc.symbol(&mut state, p.coder_type as i32, false, &t);
enc.symbol(&mut state, if p.colorspace == Colorspace::YCbCr { 0 } else { 1 }, false, &t);
enc.symbol(&mut state, p.bits as i32, false, &t);
enc.bit(p.chroma_planes, &mut state[0], &t);
enc.symbol(&mut state, i32::from(p.shift.0), false, &t);
enc.symbol(&mut state, i32::from(p.shift.1), false, &t);
enc.bit(p.extra_plane, &mut state[0], &t);
enc.symbol(&mut state, p.num_h as i32 - 1, false, &t);
enc.symbol(&mut state, p.num_v as i32 - 1, false, &t);
enc.symbol(&mut state, p.sets.len() as i32, false, &t);
for set in &p.sets {
for table in &set.tables {
let mut qstate = [128u8; CONTEXT_SIZE];
for run in quant_runs(table) {
enc.symbol(&mut qstate, run, false, &t);
}
}
}
for _ in &p.sets {
enc.bit(false, &mut state[0], &t); // states_coded
}
enc.symbol(&mut state, i32::from(p.ec), false, &t);
enc.symbol(&mut state, i32::from(p.intra), false, &t);
}
// ---- slices ----
/// A slice's place in the frame, in pixels.
#[derive(Clone, Copy)]
struct SliceRect {
x: usize,
y: usize,
w: usize,
h: usize,
}
fn slice_rect(p: &Params, width: usize, height: usize, sx: usize, sy: usize, sw: usize, sh: usize) -> Result<SliceRect> {
let (nh, nv) = (p.num_h as usize, p.num_v as usize);
if sx + sw > nh || sy + sh > nv || sw == 0 || sh == 0 {
return Err(Error::Invalid("slice outside the slice raster"));
}
let x0 = sx * width / nh;
let x1 = (sx + sw) * width / nh;
let y0 = sy * height / nv;
let y1 = (sy + sh) * height / nv;
if x1 <= x0 || y1 <= y0 {
return Err(Error::Invalid("empty slice"));
}
Ok(SliceRect { x: x0, y: y0, w: x1 - x0, h: y1 - y0 })
}
/// The samples of one plane of a slice with the border of RFC 9043 section 3.1 around them: two
/// columns to the left, one to the right, two rows above.
struct Border {
buf: Vec<i32>,
stride: usize,
w: usize,
}
impl Border {
fn new(w: usize, h: usize) -> Border {
let stride = w + 3;
Border { buf: vec![0; stride * (h + 2)], stride, w }
}
fn at(&self, x: usize, y: usize) -> usize {
(y + 2) * self.stride + x + 2
}
/// Set up the left border of row `y` (the column left of it repeats the row above's first sample).
fn start_row(&mut self, y: usize) {
let row = (y + 2) * self.stride;
self.buf[row] = 0;
self.buf[row + 1] = if y > 0 { self.buf[(y + 1) * self.stride + 2] } else { 0 };
}
/// The right border of row `y` repeats its last sample.
fn end_row(&mut self, y: usize) {
let row = (y + 2) * self.stride;
self.buf[row + self.w + 2] = self.buf[row + self.w + 1];
}
/// `(context, predictor)` for sample `(x, y)`; `context` is signed (section 3.5).
fn context(&self, x: usize, y: usize, tables: &[[i32; 256]], sixteen_bit_ycbcr: bool) -> (i32, i32) {
let i = self.at(x, y);
let (b, s) = (&self.buf, self.stride);
let (l, ll, t, tl, tr, tt) = (b[i - 1], b[i - 2], b[i - s], b[i - s - 1], b[i - s + 1], b[i - 2 * s]);
let ctx = tables[0][((l - tl) & 255) as usize]
+ tables[1][((tl - t) & 255) as usize]
+ tables[2][((t - tr) & 255) as usize]
+ tables[3][((ll - l) & 255) as usize]
+ tables[4][((tt - t) & 255) as usize];
let (pl, pt, ptl) = if sixteen_bit_ycbcr {
let s16 = |v: i32| if v >= 32768 { v - 65536 } else { v };
(s16(l), s16(t), s16(tl))
} else {
(l, t, tl)
};
(ctx, median(pl, pt, pl + pt - ptl))
}
}
fn median(a: i32, b: i32, c: i32) -> i32 {
a.max(b).min(a.min(b).max(c))
}
/// The reversible colour transform bits: a plane's samples have `bits + 1` bits.
fn rct_swapped(p: &Params) -> bool {
p.colorspace == Colorspace::Rgb && (9..=15).contains(&p.bits) && !p.extra_plane
}
fn coded_bits(p: &Params) -> u32 {
if p.colorspace == Colorspace::Rgb { p.bits + 1 } else { p.bits }
}
/// One plane's samples inside a slice, and which class of states codes them.
struct PlaneCoder {
border: Border,
slot: usize,
}
/// The adaptive states of one plane class: Y, Cb and Cr together, then transparency (section 3.6).
struct ClassStates {
set: usize,
states: Vec<[u8; CONTEXT_SIZE]>,
}
fn make_coders(p: &Params, rect: SliceRect, set_idx: &[usize]) -> Result<(Vec<PlaneCoder>, Vec<ClassStates>)> {
let mut planes = Vec::new();
for pl in 0..p.plane_count() {
let (w, h) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes);
planes.push(PlaneCoder { border: Border::new(w, h), slot: p.qset_slot(pl) });
}
let mut classes = Vec::new();
for slot in 0..=planes.iter().map(|c| c.slot).max().unwrap_or(0) {
let set = *set_idx.get(slot).ok_or(Error::Invalid("missing quant table set index"))?;
let q = p.sets.get(set).ok_or(Error::Invalid("quant table set index out of range"))?;
let states = match p.initial_states.get(set) {
Some(Some(init)) => init.clone(),
_ => vec![[128u8; CONTEXT_SIZE]; q.context_count],
};
classes.push(ClassStates { set, states });
}
Ok((planes, classes))
}
/// The number of Quantization Table Set indexes in a slice header (section 4.6.5, version 3).
fn set_index_count(p: &Params) -> usize {
2 + usize::from(p.extra_plane)
}
fn check_frame_size(p: &Params, w: u32, h: u32) -> Result<()> {
if w == 0 || h == 0 {
return Err(Error::Invalid("empty frame"));
}
let shape = PictureShape {
width: u64::from(w),
height: u64::from(h),
shift_x: u32::from(p.shift.0.min(1)),
shift_y: u32::from(p.shift.1.min(1)),
chroma_planes: if p.chroma_planes { 2 } else { 0 },
bytes_per_sample: 2,
};
// The decoded planes and, while a slice decodes, its bordered `i32` copy (2 x the samples at most).
if !shape.fits(5, DECODE_MEMORY_BUDGET) {
return Err(Error::Unsupported("frame size above the memory budget"));
}
if p.shift.0 > 1 || p.shift.1 > 1 {
// Subsampling beyond 2:1 is legal FFV1 (e.g. 4:1:1) but the budget model above is for 2:1.
let extra = (w as u64).checked_mul(h as u64).ok_or(Error::Invalid("frame too large"))?;
if extra > 1 << 28 {
return Err(Error::Unsupported("frame size above the memory budget"));
}
}
Ok(())
}
/// A decoder for one stream.
pub struct Decoder {
params: Params,
width: u32,
height: u32,
}
impl Decoder {
/// Build a decoder from the container's configuration record and the frame size.
pub fn new(config_record: &[u8], width: u32, height: u32) -> Result<Decoder> {
if config_record.len() < 5 {
return Err(Error::Truncated);
}
let (body, _crc) = config_record.split_at(config_record.len() - 4);
if crc32(config_record) != 0 {
return Err(Error::Crc("the configuration record"));
}
let mut rac = Rac::new(body);
let params = parse_params(&mut rac)?;
if rac.consumed() > body.len() + 2 {
return Err(Error::Truncated);
}
check_frame_size(&params, width, height)?;
Ok(Decoder { params, width, height })
}
/// Decode one frame (a packet from the container).
pub fn decode(&self, packet: &[u8]) -> Result<Frame> {
let p = &self.params;
let (w, h) = (self.width as usize, self.height as usize);
let planes = p.plane_count();
let mut frame = Frame {
width: self.width,
height: self.height,
colorspace: p.colorspace,
bit_depth: p.bits as u8,
chroma_shift: if p.chroma_planes { p.shift } else { (0, 0) },
planes: Vec::with_capacity(planes),
};
for pl in 0..planes {
let (pw, ph) = plane_dims(w, h, p.colorspace, p.shift, pl, p.chroma_planes);
let mut v = Vec::new();
v.try_reserve_exact(pw * ph).map_err(|_| Error::Unsupported("frame size above the memory budget"))?;
v.resize(pw * ph, 0u16);
frame.planes.push(v);
}
// Slices are found from the end of the packet through their footers (section 4.9).
let trailer = if p.ec { 8 } else { 3 };
let slice_count = p.num_h as usize * p.num_v as usize;
let mut end = packet.len();
let mut spans = Vec::with_capacity(slice_count);
for _ in 0..slice_count {
if end < trailer {
return Err(Error::Truncated);
}
let f = end - trailer;
let size = usize::from(packet[f]) << 16 | usize::from(packet[f + 1]) << 8 | usize::from(packet[f + 2]);
let start = f.checked_sub(size).ok_or(Error::Truncated)?;
spans.push((start, end));
end = start;
}
spans.reverse();
for (n, &(start, stop)) in spans.iter().enumerate() {
let slice = &packet[start..stop];
if p.ec && crc32(slice) != 0 {
return Err(Error::Crc("a slice"));
}
self.decode_slice(slice, n == 0, &mut frame)?;
}
Ok(frame)
}
fn decode_slice(&self, slice: &[u8], first: bool, frame: &mut Frame) -> Result<()> {
let p = &self.params;
let t = &p.transitions;
let data = &slice[..slice.len() - if p.ec { 8 } else { 3 }];
let mut rac = Rac::new(data);
// The keyframe flag is the first symbol of the first slice.
// The whole slice, header included, is coded with the stream's state transition table.
let t0 = t;
if first {
let mut ks = 128u8;
if !rac.bit(&mut ks, t0) {
return Err(Error::Unsupported("inter frames (keyframe 0)"));
}
}
let mut hs = [128u8; CONTEXT_SIZE];
let sx = rac.symbol(&mut hs, false, t0) as usize;
let sy = rac.symbol(&mut hs, false, t0) as usize;
let sw = rac.symbol(&mut hs, false, t0) as usize + 1;
let sh = rac.symbol(&mut hs, false, t0) as usize + 1;
let set_idx: Vec<usize> = (0..set_index_count(p)).map(|_| rac.symbol(&mut hs, false, t0).max(0) as usize).collect();
let _picture_structure = rac.symbol(&mut hs, false, t0);
let _sar = (rac.symbol(&mut hs, false, t0), rac.symbol(&mut hs, false, t0));
let rect = slice_rect(p, self.width as usize, self.height as usize, sx, sy, sw, sh)?;
let (sx_mask, sy_mask) = ((1usize << p.shift.0) - 1, (1usize << p.shift.1) - 1);
if p.colorspace == Colorspace::YCbCr && p.chroma_planes && (rect.x & sx_mask != 0 || rect.y & sy_mask != 0) {
return Err(Error::Unsupported("slice boundaries that are not multiples of the chroma subsampling"));
}
let (mut coders, mut classes) = make_coders(p, rect, &set_idx)?;
let bits = coded_bits(p);
let mask = (1i32 << bits) - 1;
let sixteen = p.colorspace == Colorspace::YCbCr && p.bits == 16;
let read = |rac: &mut Rac, c: &mut PlaneCoder, cls: &mut ClassStates, x: usize, y: usize| -> i32 {
let q = &p.sets[cls.set];
let (ctx, pred) = c.border.context(x, y, &q.tables, sixteen);
let (idx, neg) = if ctx < 0 { ((-ctx) as usize, true) } else { (ctx as usize, false) };
let mut d = rac.symbol(&mut cls.states[idx.min(q.context_count - 1)], true, t);
if neg {
d = -d;
}
let v = (pred + d) & mask;
let at = c.border.at(x, y);
c.border.buf[at] = v;
v
};
match p.colorspace {
Colorspace::YCbCr => {
for (pl, c) in coders.iter_mut().enumerate() {
let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes);
for y in 0..ph {
c.border.start_row(y);
for x in 0..pw {
read(&mut rac, c, &mut classes[c.slot], x, y);
}
c.border.end_row(y);
}
}
}
Colorspace::Rgb => {
for y in 0..rect.h {
for c in coders.iter_mut() {
c.border.start_row(y);
for x in 0..rect.w {
read(&mut rac, c, &mut classes[c.slot], x, y);
}
c.border.end_row(y);
}
}
}
}
self.store(&coders, rect, frame);
Ok(())
}
/// Copy a decoded slice into the frame, undoing the colour transform for RGB.
fn store(&self, coders: &[PlaneCoder], rect: SliceRect, frame: &mut Frame) {
let p = &self.params;
if p.colorspace == Colorspace::YCbCr {
for (pl, c) in coders.iter().enumerate() {
let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes);
let (fw, _) = plane_dims(self.width as usize, self.height as usize, p.colorspace, p.shift, pl, p.chroma_planes);
let (ox, oy) = if p.chroma_planes && (pl == 1 || pl == 2) { (rect.x >> p.shift.0, rect.y >> p.shift.1) } else { (rect.x, rect.y) };
for y in 0..ph {
for x in 0..pw {
frame.planes[pl][(oy + y) * fw + ox + x] = c.border.buf[c.border.at(x, y)] as u16;
}
}
}
return;
}
let swapped = rct_swapped(p);
let off = 1i32 << p.bits;
let fw = self.width as usize;
for y in 0..rect.h {
for x in 0..rect.w {
let get = |i: usize| coders[i].border.buf[coders[i].border.at(x, y)];
let (yy, cb, cr) = (get(0), get(1) - off, get(2) - off);
let (r, g, b);
if swapped {
let bb = yy - ((cb + cr) >> 2);
r = cr + bb;
g = cb + bb;
b = bb;
} else {
let gg = yy - ((cb + cr) >> 2);
r = cr + gg;
g = gg;
b = cb + gg;
}
let at = (rect.y + y) * fw + rect.x + x;
frame.planes[0][at] = r as u16;
frame.planes[1][at] = g as u16;
frame.planes[2][at] = b as u16;
if coders.len() > 3 {
frame.planes[3][at] = get(3) as u16;
}
}
}
}
}
/// Encoder settings.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EncoderConfig {
/// Slices across the frame.
pub slices_h: u32,
/// Slices down the frame.
pub slices_v: u32,
/// Write a CRC in every slice (error detection).
pub slice_crc: bool,
}
impl Default for EncoderConfig {
fn default() -> Self {
EncoderConfig { slices_h: 1, slices_v: 1, slice_crc: true }
}
}
/// An encoder for frames of one fixed format.
pub struct Encoder {
params: Params,
width: u32,
height: u32,
}
impl Encoder {
/// An encoder for frames shaped like `template` (its samples are not used).
pub fn new(template: &Frame, cfg: EncoderConfig) -> Result<Encoder> {
if !template.plane_count_ok() || template.width == 0 || template.height == 0 {
return Err(Error::Input("a frame has 1 to 4 planes and a nonzero size".into()));
}
if !(8..=16).contains(&template.bit_depth) {
return Err(Error::Input("bit depth must be 8 to 16".into()));
}
if cfg.slices_h == 0
|| cfg.slices_v == 0
|| cfg.slices_h > 1024
|| cfg.slices_v > 1024
|| cfg.slices_h > template.width
|| cfg.slices_v > template.height
{
return Err(Error::Input("invalid slice raster".into()));
}
let n = template.planes.len();
let (chroma_planes, extra_plane) = match (template.colorspace, n) {
(Colorspace::YCbCr, 1) => (false, false),
(Colorspace::YCbCr, 2) => (false, true),
(Colorspace::YCbCr, 3) => (true, false),
(Colorspace::YCbCr, _) => (true, true),
(Colorspace::Rgb, 3) => (true, false),
(Colorspace::Rgb, 4) => (true, true),
(Colorspace::Rgb, _) => return Err(Error::Input("RGB frames have 3 or 4 planes".into())),
};
let shift = if template.colorspace == Colorspace::YCbCr && chroma_planes { template.chroma_shift } else { (0, 0) };
if shift.0 > 3 || shift.1 > 3 {
return Err(Error::Input("chroma subsampling above 8:1".into()));
}
if template.colorspace == Colorspace::Rgb && template.bit_depth > 15 {
return Err(Error::Input("RGB above 15 bits needs 17-bit coded samples".into()));
}
let params = Params {
coder_type: 1,
transitions: Transitions::default_table(),
colorspace: template.colorspace,
bits: u32::from(template.bit_depth),
chroma_planes,
shift,
extra_plane,
num_h: cfg.slices_h,
num_v: cfg.slices_v,
sets: vec![default_quant_set()],
initial_states: vec![None],
ec: cfg.slice_crc,
intra: true,
};
// Every slice boundary must be a multiple of the chroma subsampling, so the decoder accepts it.
for i in 0..=cfg.slices_h as usize {
if params.chroma_planes && (i * template.width as usize / cfg.slices_h as usize) & ((1 << shift.0) - 1) != 0 && i != cfg.slices_h as usize
{
return Err(Error::Input("slice boundaries must be multiples of the chroma subsampling".into()));
}
}
for i in 0..cfg.slices_v as usize {
if params.chroma_planes && (i * template.height as usize / cfg.slices_v as usize) & ((1 << shift.1) - 1) != 0 {
return Err(Error::Input("slice boundaries must be multiples of the chroma subsampling".into()));
}
}
Ok(Encoder { params, width: template.width, height: template.height })
}
/// The configuration record for the container (`CodecPrivate` and the like).
pub fn config_record(&self) -> Vec<u8> {
let mut enc = RacEnc::new();
write_params(&mut enc, &self.params);
let mut out = enc.finish();
let crc = crc32(&out);
out.extend_from_slice(&crc.to_be_bytes());
out
}
/// Encode one frame as a keyframe packet.
pub fn encode(&self, frame: &Frame) -> Result<Vec<u8>> {
let p = &self.params;
if frame.width != self.width || frame.height != self.height || frame.colorspace != p.colorspace || u32::from(frame.bit_depth) != p.bits {
return Err(Error::Input("the frame does not match the encoder's format".into()));
}
if frame.planes.len() != p.plane_count() {
return Err(Error::Input("the frame has the wrong number of planes".into()));
}
let shift = if p.chroma_planes { p.shift } else { (0, 0) };
if p.colorspace == Colorspace::YCbCr && p.chroma_planes && frame.chroma_shift != shift {
return Err(Error::Input("the chroma subsampling does not match".into()));
}
for (pl, plane) in frame.planes.iter().enumerate() {
let (w, h) = frame.dims(pl, p.chroma_planes);
if plane.len() != w * h {
return Err(Error::Input(format!("plane {pl} has {} samples, expected {}", plane.len(), w * h)));
}
if plane.iter().any(|&v| u32::from(v) >> p.bits != 0) {
return Err(Error::Input(format!("plane {pl} has a sample above {} bits", p.bits)));
}
}
let mut out = Vec::new();
for sy in 0..p.num_v as usize {
for sx in 0..p.num_h as usize {
let rect = slice_rect(p, self.width as usize, self.height as usize, sx, sy, 1, 1)?;
let mut slice = self.encode_slice(frame, rect, sx, sy, out.is_empty())?;
let size = slice.len();
slice.extend_from_slice(&(size as u32).to_be_bytes()[1..]);
if p.ec {
slice.push(0); // error_status
let crc = crc32(&slice);
slice.extend_from_slice(&crc.to_be_bytes());
}
out.extend_from_slice(&slice);
}
}
Ok(out)
}
fn encode_slice(&self, frame: &Frame, rect: SliceRect, sx: usize, sy: usize, first: bool) -> Result<Vec<u8>> {
let p = &self.params;
let t = &p.transitions;
let t0 = t;
let mut rac = RacEnc::new();
if first {
let mut ks = 128u8;
rac.bit(true, &mut ks, t0);
}
let mut hs = [128u8; CONTEXT_SIZE];
for v in [sx as i32, sy as i32, 0, 0] {
rac.symbol(&mut hs, v, false, t0);
}
let set_idx = vec![0usize; set_index_count(p)];
for &s in &set_idx {
rac.symbol(&mut hs, s as i32, false, t0);
}
for v in [0, 0, 0] {
rac.symbol(&mut hs, v, false, t0); // picture_structure, sar_num, sar_den
}
let (mut coders, mut classes) = make_coders(p, rect, &set_idx)?;
let bits = coded_bits(p);
let half = 1i32 << (bits - 1);
let mask = (1i32 << bits) - 1;
let sixteen = p.colorspace == Colorspace::YCbCr && p.bits == 16;
// The samples to code, one bordered plane's worth per coder.
let values = self.slice_values(frame, rect);
let write = |rac: &mut RacEnc, c: &mut PlaneCoder, cls: &mut ClassStates, x: usize, y: usize, sample: i32| {
let q = &p.sets[cls.set];
let (ctx, pred) = c.border.context(x, y, &q.tables, sixteen);
let mut d = ((sample - pred + half) & mask) - half;
let (idx, neg) = if ctx < 0 { ((-ctx) as usize, true) } else { (ctx as usize, false) };
if neg {
d = -d;
}
rac.symbol(&mut cls.states[idx.min(q.context_count - 1)], d, true, t);
let at = c.border.at(x, y);
c.border.buf[at] = sample;
};
match p.colorspace {
Colorspace::YCbCr => {
for (pl, c) in coders.iter_mut().enumerate() {
let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes);
for y in 0..ph {
c.border.start_row(y);
for x in 0..pw {
write(&mut rac, c, &mut classes[c.slot], x, y, values[pl][y * pw + x]);
}
c.border.end_row(y);
}
}
}
Colorspace::Rgb => {
for y in 0..rect.h {
for (pl, c) in coders.iter_mut().enumerate() {
c.border.start_row(y);
for x in 0..rect.w {
write(&mut rac, c, &mut classes[c.slot], x, y, values[pl][y * rect.w + x]);
}
c.border.end_row(y);
}
}
}
}
Ok(rac.finish())
}
/// The slice's samples per coded plane (after the colour transform for RGB).
fn slice_values(&self, frame: &Frame, rect: SliceRect) -> Vec<Vec<i32>> {
let p = &self.params;
let fw = self.width as usize;
if p.colorspace == Colorspace::YCbCr {
return (0..p.plane_count())
.map(|pl| {
let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes);
let (full_w, _) = plane_dims(fw, self.height as usize, p.colorspace, p.shift, pl, p.chroma_planes);
let (ox, oy) =
if p.chroma_planes && (pl == 1 || pl == 2) { (rect.x >> p.shift.0, rect.y >> p.shift.1) } else { (rect.x, rect.y) };
let mut v = Vec::with_capacity(pw * ph);
for y in 0..ph {
for x in 0..pw {
v.push(i32::from(frame.planes[pl][(oy + y) * full_w + ox + x]));
}
}
v
})
.collect();
}
let swapped = rct_swapped(p);
let off = 1i32 << p.bits;
let mut planes = vec![Vec::with_capacity(rect.w * rect.h); p.plane_count()];
for y in 0..rect.h {
for x in 0..rect.w {
let at = (rect.y + y) * fw + rect.x + x;
let (r, g, b) = (i32::from(frame.planes[0][at]), i32::from(frame.planes[1][at]), i32::from(frame.planes[2][at]));
let (yy, cb, cr) = if swapped {
let (cb, cr) = (g - b, r - b);
(b + ((cb + cr) >> 2), cb, cr)
} else {
let (cb, cr) = (b - g, r - g);
(g + ((cb + cr) >> 2), cb, cr)
};
planes[0].push(yy);
planes[1].push(cb + off);
planes[2].push(cr + off);
if p.extra_plane {
planes[3].push(i32::from(frame.planes[3][at]));
}
}
}
planes
}
}
+229
View File
@@ -0,0 +1,229 @@
//! The FFV1 range coder (RFC 9043 section 3.8.1): binary decisions with adaptive 8-bit states,
//! and the signed/unsigned integer coding built on them.
/// RFC 9043 Figure 24: the default state transition table for a coded 1.
const DEFAULT_ONE_STATE: [u8; 256] = [
0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82,
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140,
141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195,
196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223,
224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0,
0, 0, 0, 0,
];
/// The state machine: the next state after a coded 0 or 1.
#[derive(Clone)]
pub(crate) struct Transitions {
one: [u8; 256],
zero: [u8; 256],
}
impl Transitions {
/// The default table plus `delta` (`state_transition_delta`, indexed 1 to 255).
pub(crate) fn new(delta: &[i32; 256]) -> Transitions {
let mut one = [0u8; 256];
for (i, o) in one.iter_mut().enumerate() {
*o = (i32::from(DEFAULT_ONE_STATE[i]) + delta[i]) as u8;
}
let mut zero = [0u8; 256];
for i in 1..256 {
zero[i] = (256 - i32::from(one[256 - i])) as u8;
}
Transitions { one, zero }
}
pub(crate) fn default_table() -> Transitions {
Transitions::new(&[0; 256])
}
}
/// Reads binary decisions from `data`; bytes past its end read as 0 (the "Closed" mode of 3.8.1.1.1).
pub(crate) struct Decoder<'a> {
data: &'a [u8],
pos: usize,
low: u32,
range: u32,
}
impl<'a> Decoder<'a> {
pub(crate) fn new(data: &'a [u8]) -> Decoder<'a> {
let b0 = u32::from(data.first().copied().unwrap_or(0));
let b1 = u32::from(data.get(1).copied().unwrap_or(0));
let mut low = b0 << 8 | b1;
let range = 0xFF00;
if low >= range {
low = range;
}
Decoder { data, pos: 2, low, range }
}
/// Bytes the coder has read so far.
pub(crate) fn consumed(&self) -> usize {
self.pos
}
fn refill(&mut self) {
if self.range < 256 {
self.range <<= 8;
self.low <<= 8;
self.low += u32::from(self.data.get(self.pos).copied().unwrap_or(0));
self.pos += 1;
}
}
pub(crate) fn bit(&mut self, state: &mut u8, t: &Transitions) -> bool {
let off = (self.range * u32::from(*state)) >> 8;
self.range -= off;
if self.low < self.range {
*state = t.zero[usize::from(*state)];
self.refill();
false
} else {
self.low -= self.range;
*state = t.one[usize::from(*state)];
self.range = off;
self.refill();
true
}
}
/// RFC 9043 Figure 21: `get_symbol`.
pub(crate) fn symbol(&mut self, state: &mut [u8; 32], signed: bool, t: &Transitions) -> i32 {
if self.bit(&mut state[0], t) {
return 0;
}
let mut e = 0usize;
while self.bit(&mut state[1 + e.min(9)], t) {
e += 1;
if e > 24 {
// No valid stream codes a magnitude this large; stop instead of spinning on garbage.
return 0;
}
}
let mut a = 1i32;
for i in (0..e).rev() {
a = a * 2 + i32::from(self.bit(&mut state[22 + i.min(9)], t));
}
if signed && self.bit(&mut state[11 + e.min(10)], t) { -a } else { a }
}
}
/// Writes binary decisions; the inverse of [`Decoder`].
pub(crate) struct Encoder {
out: Vec<u8>,
low: u32,
range: u32,
}
impl Encoder {
pub(crate) fn new() -> Encoder {
Encoder { out: Vec::new(), low: 0, range: 0xFF00 }
}
fn shift(&mut self) {
while self.range < 256 {
// Bits 15..8 of `low` are final once a carry (bit 16) is added to the bytes before them.
let carry = self.low >> 16;
let byte = ((self.low >> 8) & 0xFF) as u8;
if carry != 0 {
for b in self.out.iter_mut().rev() {
let (v, over) = b.overflowing_add(1);
*b = v;
if !over {
break;
}
}
}
self.out.push(byte);
self.low = (self.low & 0xFF) << 8;
self.range <<= 8;
}
}
pub(crate) fn bit(&mut self, bit: bool, state: &mut u8, t: &Transitions) {
let off = (self.range * u32::from(*state)) >> 8;
if bit {
self.low += self.range - off;
self.range = off;
*state = t.one[usize::from(*state)];
} else {
self.range -= off;
*state = t.zero[usize::from(*state)];
}
self.shift();
}
pub(crate) fn symbol(&mut self, state: &mut [u8; 32], v: i32, signed: bool, t: &Transitions) {
if v == 0 {
self.bit(true, &mut state[0], t);
return;
}
self.bit(false, &mut state[0], t);
let a = v.unsigned_abs();
let e = (31 - a.leading_zeros()) as usize;
for i in 0..e {
self.bit(true, &mut state[1 + i.min(9)], t);
}
self.bit(false, &mut state[1 + e.min(9)], t);
for i in (0..e).rev() {
self.bit((a >> i) & 1 == 1, &mut state[22 + i.min(9)], t);
}
if signed {
self.bit(v < 0, &mut state[11 + e.min(10)], t);
}
}
/// Finish the stream with the two bytes of the *top* of the final range: whatever bytes follow
/// (zeros, or a CRC in a container's configuration record) still decode inside the range.
pub(crate) fn finish(mut self) -> Vec<u8> {
self.low += self.range - 1;
let carry = self.low >> 16;
if carry != 0 {
for b in self.out.iter_mut().rev() {
let (v, over) = b.overflowing_add(1);
*b = v;
if !over {
break;
}
}
}
self.out.push(((self.low >> 8) & 0xFF) as u8);
self.out.push((self.low & 0xFF) as u8);
self.out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn symbols_round_trip_through_the_range_coder() {
let t = Transitions::default_table();
let values: Vec<i32> = (0..4000).map(|i| ((i * 7919) % 2001) - 1000).chain([0, 0, 0, 65535, -65535, 1, -1]).collect();
let mut enc = Encoder::new();
let mut st = [128u8; 32];
for &v in &values {
enc.symbol(&mut st, v, true, &t);
}
let bytes = enc.finish();
let mut dec = Decoder::new(&bytes);
let mut st = [128u8; 32];
for &v in &values {
assert_eq!(dec.symbol(&mut st, true, &t), v);
}
}
#[test]
fn transition_tables_follow_the_rfc() {
let t = Transitions::default_table();
assert_eq!(t.one[8], 20);
assert_eq!(t.one[247], 248);
// zero_state_i = 256 - one_state_(256 - i)
assert_eq!(t.zero[128], (256 - i32::from(t.one[128])) as u8);
assert_eq!(t.zero[1], (256 - i32::from(t.one[255])) as u8);
}
}
+4
View File
@@ -10,6 +10,7 @@
//! | `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-decode`) and MPEG-2 I/P/B encoder (`mpeg2-encode`) |
//! | `mpeg4` | [`mpeg4`] | MPEG-4 Part 2 video (ISO/IEC 14496-2, Xvid/DivX): Simple profile encoder (`mpeg4-encode`) |
//! | `ffv1` | [`ffv1`] | FFV1 lossless video (RFC 9043): version 3, range coder, decoder and encoder |
//! | `prores` | [`prores`] | Apple ProRes (SMPTE RDD 36): decoder and encoder |
//! | `h264` | [`h264`] | ITU-T H.264 / AVC: decoder (`h264-decode`) and encoder (`h264-encode`) |
//! | `hevc` | [`hevc`] | ITU-T H.265 / HEVC: decoder (`hevc-decode`) and encoder (`hevc-encode`) |
@@ -41,6 +42,9 @@ pub mod mpeg2;
#[cfg(feature = "mpeg4-encode")]
pub mod mpeg4;
#[cfg(feature = "ffv1")]
pub mod ffv1;
#[cfg(feature = "prores")]
pub mod prores;
+304
View File
@@ -0,0 +1,304 @@
//! FFV1 tests: round trips, hostile input, and ffmpeg as the reference (oracle; skipped when ffmpeg
//! is absent, a failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`).
//!
//! - Our packets, in an AVI with our configuration record, decode in ffmpeg to our source pixels.
//! - ffmpeg's FFV1 files (version 3, range coder, several pixel formats and slice counts) decode
//! with our decoder to ffmpeg's pixels.
use craft_media::avi::{AviWriter, MuxOptions, StreamSpec, VideoSpec};
use craft_video::ffv1::{Colorspace, Decoder, Encoder, EncoderConfig, Error, Frame};
use std::path::Path;
use std::process::Command;
fn noise(seed: &mut u32, mask: u32) -> u16 {
*seed ^= *seed << 13;
*seed ^= *seed >> 17;
*seed ^= *seed << 5;
(*seed & mask) as u16
}
/// A frame of smooth gradients, noise bursts and flat areas.
fn frame(w: u32, h: u32, cs: Colorspace, bits: u8, shift: (u8, u8), planes: usize) -> Frame {
let max = (1u32 << bits) - 1;
let mut seed = 0x9E37_79B9u32;
let mut ps = Vec::new();
for p in 0..planes {
let (pw, ph) = if cs == Colorspace::YCbCr && (p == 1 || p == 2) && planes >= 3 {
((w as usize).div_ceil(1 << shift.0), (h as usize).div_ceil(1 << shift.1))
} else {
(w as usize, h as usize)
};
let mut v = Vec::with_capacity(pw * ph);
for y in 0..ph {
for x in 0..pw {
let s = match (y / 11 + p) % 3 {
0 => ((x * 5 + y * 3 + p * 40) as u32 * max / 600) % (max + 1),
1 => noise(&mut seed, max) as u32,
_ => max / 3,
};
v.push(s as u16);
}
}
ps.push(v);
}
Frame { width: w, height: h, colorspace: cs, bit_depth: bits, chroma_shift: shift, planes: ps }
}
fn round_trip(f: &Frame, cfg: EncoderConfig) -> Frame {
let enc = Encoder::new(f, cfg).expect("encoder");
let config = enc.config_record();
let packet = enc.encode(f).expect("encode");
let dec = Decoder::new(&config, f.width, f.height).expect("decoder");
dec.decode(&packet).expect("decode")
}
#[test]
fn round_trips_every_shape() {
for (cs, bits, shift, planes) in [
(Colorspace::YCbCr, 8u8, (0u8, 0u8), 1usize),
(Colorspace::YCbCr, 8, (1, 1), 3),
(Colorspace::YCbCr, 8, (1, 0), 3),
(Colorspace::YCbCr, 8, (0, 0), 4),
(Colorspace::YCbCr, 10, (1, 1), 3),
(Colorspace::YCbCr, 12, (0, 0), 3),
(Colorspace::YCbCr, 16, (1, 0), 3),
(Colorspace::YCbCr, 16, (0, 0), 1),
(Colorspace::Rgb, 8, (0, 0), 3),
(Colorspace::Rgb, 8, (0, 0), 4),
(Colorspace::Rgb, 10, (0, 0), 3),
(Colorspace::Rgb, 12, (0, 0), 4),
(Colorspace::Rgb, 15, (0, 0), 3),
] {
for (w, h) in [(1, 1), (3, 5), (64, 48), (67, 41)] {
let f = frame(w, h, cs, bits, shift, planes);
for crc in [true, false] {
let back = round_trip(&f, EncoderConfig { slice_crc: crc, ..EncoderConfig::default() });
assert_eq!(back.planes, f.planes, "{cs:?} {bits}-bit {shift:?} {planes} planes {w}x{h} crc={crc}");
}
}
}
}
#[test]
fn round_trips_with_several_slices() {
let f = frame(96, 64, Colorspace::YCbCr, 8, (1, 1), 3);
for (sh, sv) in [(2, 1), (1, 2), (3, 2), (4, 4)] {
let back = round_trip(&f, EncoderConfig { slices_h: sh, slices_v: sv, slice_crc: true });
assert_eq!(back.planes, f.planes, "{sh}x{sv} slices");
}
let rgb = frame(50, 30, Colorspace::Rgb, 8, (0, 0), 3);
assert_eq!(round_trip(&rgb, EncoderConfig { slices_h: 3, slices_v: 2, slice_crc: true }).planes, rgb.planes);
}
#[test]
fn encoder_refuses_what_it_cannot_write() {
let f = frame(8, 8, Colorspace::YCbCr, 8, (1, 1), 3);
assert!(Encoder::new(&f, EncoderConfig { slices_h: 0, ..EncoderConfig::default() }).is_err());
assert!(Encoder::new(&f, EncoderConfig { slices_h: 9, ..EncoderConfig::default() }).is_err());
let mut bad = f.clone();
bad.bit_depth = 7;
assert!(Encoder::new(&bad, EncoderConfig::default()).is_err());
let mut rgb16 = frame(8, 8, Colorspace::Rgb, 8, (0, 0), 3);
rgb16.bit_depth = 16;
assert!(Encoder::new(&rgb16, EncoderConfig::default()).is_err());
let enc = Encoder::new(&f, EncoderConfig::default()).expect("encoder");
let mut wrong = f.clone();
wrong.planes[1].pop();
assert!(enc.encode(&wrong).is_err());
let mut over = f;
over.planes[0][0] = 300;
assert!(enc.encode(&over).is_err());
}
#[test]
fn hostile_input_is_an_error_not_a_panic() {
let f = frame(24, 18, Colorspace::YCbCr, 8, (1, 1), 3);
let enc = Encoder::new(&f, EncoderConfig { slices_h: 2, slices_v: 1, slice_crc: true }).expect("encoder");
let config = enc.config_record();
let packet = enc.encode(&f).expect("encode");
// Every truncation of the configuration record and of the packet.
for n in 0..config.len() {
let _ = Decoder::new(&config[..n], 24, 18);
}
let dec = Decoder::new(&config, 24, 18).expect("decoder");
for n in 0..packet.len() {
let _ = dec.decode(&packet[..n]);
}
// Every single-bit flip: the CRCs catch them in a packet, and none panics.
for i in 0..packet.len() {
for bit in 0..8 {
let mut c = packet.clone();
c[i] ^= 1 << bit;
assert!(dec.decode(&c).is_err(), "bit {bit} of byte {i} went unnoticed");
}
}
for i in 0..config.len() {
for bit in 0..8 {
let mut c = config.clone();
c[i] ^= 1 << bit;
assert!(matches!(Decoder::new(&c, 24, 18), Err(Error::Crc(_))), "config bit {bit} of byte {i}");
}
}
// Without slice CRCs, flips may decode to wrong pixels but must not panic.
let enc = Encoder::new(&f, EncoderConfig { slice_crc: false, ..EncoderConfig::default() }).expect("encoder");
let (config, packet) = (enc.config_record(), enc.encode(&f).expect("encode"));
let dec = Decoder::new(&config, 24, 18).expect("decoder");
for i in 0..packet.len() {
for bit in 0..8 {
let mut c = packet.clone();
c[i] ^= 1 << bit;
let _ = dec.decode(&c);
}
}
// A huge frame size is refused before any allocation.
assert!(Decoder::new(&config, u32::MAX, u32::MAX).is_err());
}
// ---- ffmpeg ----
fn ffmpeg_run(ff: &Path, args: &[&str]) -> Vec<u8> {
let out = Command::new(ff).args(["-v", "error", "-nostdin", "-y"]).args(args).output().expect("run ffmpeg");
assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr));
out.stdout
}
fn raw_bytes(f: &Frame) -> Vec<u8> {
let mut out = Vec::new();
for p in &f.planes {
for &v in p {
if f.bit_depth == 8 {
out.push(v as u8);
} else {
out.extend_from_slice(&v.to_le_bytes());
}
}
}
out
}
/// `(ffmpeg pix_fmt, colourspace, bits, chroma shift, planes)`; RGB planes are G, B, R in ffmpeg's gbrp.
type Format = (&'static str, Colorspace, u8, (u8, u8), usize);
const FORMATS: &[Format] = &[
("gray", Colorspace::YCbCr, 8, (0, 0), 1),
("yuv420p", Colorspace::YCbCr, 8, (1, 1), 3),
("yuv422p", Colorspace::YCbCr, 8, (1, 0), 3),
("yuv444p", Colorspace::YCbCr, 8, (0, 0), 3),
("yuv420p10le", Colorspace::YCbCr, 10, (1, 1), 3),
("yuv444p12le", Colorspace::YCbCr, 12, (0, 0), 3),
("gray16le", Colorspace::YCbCr, 16, (0, 0), 1),
("yuv444p16le", Colorspace::YCbCr, 16, (0, 0), 3),
("gbrp", Colorspace::Rgb, 8, (0, 0), 3),
("gbrp10le", Colorspace::Rgb, 10, (0, 0), 3),
("gbrp12le", Colorspace::Rgb, 12, (0, 0), 3),
("yuva444p", Colorspace::YCbCr, 8, (0, 0), 4),
("gbrap", Colorspace::Rgb, 8, (0, 0), 4),
("gbrap12le", Colorspace::Rgb, 12, (0, 0), 4),
];
/// Formats only ffmpeg writes: our encoder stops at 15-bit RGB (the transform needs 17 bits).
const DECODE_ONLY: &[Format] = &[("gbrp16le", Colorspace::Rgb, 16, (0, 0), 3), ("yuv411p", Colorspace::YCbCr, 8, (2, 0), 3)];
/// ffmpeg's planar RGB is G, B, R (then alpha); ours is R, G, B (then alpha).
fn gbr_order(planes: &[Vec<u16>]) -> Vec<Vec<u16>> {
let mut v = vec![planes[1].clone(), planes[2].clone(), planes[0].clone()];
v.extend(planes[3..].iter().cloned());
v
}
fn avi_with(frame: &Frame, config: &[u8], packet: &[u8]) -> Vec<u8> {
let mut spec = VideoSpec::new(*b"FFV1", frame.width, frame.height, 25, 1);
spec.format.extra = config.to_vec();
let mut w = AviWriter::new(std::io::Cursor::new(Vec::new()), vec![StreamSpec::Video(spec)], MuxOptions::default()).expect("writer");
w.write_chunk(0, packet, true).expect("chunk");
w.finish().expect("finish").into_inner()
}
#[test]
fn ffmpeg_decodes_our_packets_to_our_pixels() {
let Some(ff) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("ffv1");
for &(pix_fmt, cs, bits, shift, planes) in FORMATS {
for (slices_h, slices_v) in [(1, 1), (2, 2), (3, 2)] {
let f = frame(96, 48, cs, bits, shift, planes);
let enc = Encoder::new(&f, EncoderConfig { slices_h, slices_v, slice_crc: true }).expect("encoder");
let avi = avi_with(&f, &enc.config_record(), &enc.encode(&f).expect("encode"));
let path = dir.join(format!("ours_{pix_fmt}_{slices_h}x{slices_v}.avi"));
std::fs::write(&path, avi).expect("write");
let theirs = ffmpeg_run(&ff, &["-i", path.to_str().expect("path"), "-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]);
// gbrp is planar G, B, R; our RGB planes are R, G, B.
let mut want = f.clone();
if cs == Colorspace::Rgb {
want.planes = gbr_order(&f.planes);
}
assert!(theirs == raw_bytes(&want), "{pix_fmt} {slices_h}x{slices_v}: ffmpeg's pixels differ from ours");
}
}
}
#[test]
fn our_decoder_reads_ffmpegs_files() {
let Some(ff) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("ffv1");
for &(pix_fmt, cs, bits, shift, planes) in FORMATS.iter().chain(DECODE_ONLY) {
for slices in ["1", "4", "6"] {
for (ctx, extra) in [("0", false), ("1", true)] {
let path = dir.join(format!("ffmpeg_{pix_fmt}_s{slices}_c{ctx}.avi"));
let lavfi = "testsrc2=size=96x48:rate=1";
ffmpeg_run(
&ff,
&[
"-f",
"lavfi",
"-i",
lavfi,
"-frames:v",
"1",
"-vf",
&format!("format={pix_fmt}"),
"-c:v",
"ffv1",
"-level",
"3",
"-coder",
"1",
"-context",
ctx,
"-slices",
slices,
"-slicecrc",
"1",
"-g",
"1",
path.to_str().expect("path"),
],
);
let _ = extra;
let file = std::fs::read(&path).expect("read");
let avi = craft_media::avi::open(&file[..]).expect("open avi");
let stream = &avi.streams[0];
let config = stream.video.as_ref().expect("video").extra.clone();
let chunk = stream.chunks[0];
let packet = file[chunk.offset as usize..chunk.offset as usize + chunk.size as usize].to_vec();
let dec = Decoder::new(&config, 96, 48).unwrap_or_else(|e| panic!("{pix_fmt} s{slices} c{ctx}: config: {e}"));
let got = dec.decode(&packet).unwrap_or_else(|e| panic!("{pix_fmt} s{slices} c{ctx}: decode: {e}"));
let theirs = ffmpeg_run(&ff, &["-i", path.to_str().expect("path"), "-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]);
let mut want = got.clone();
if cs == Colorspace::Rgb {
want.planes = gbr_order(&got.planes);
}
assert_eq!((got.colorspace, got.bit_depth, got.chroma_shift, got.planes.len()), (cs, bits, shift, planes), "{pix_fmt}");
if theirs != raw_bytes(&want) {
let mut off = 0;
for (i, p) in want.planes.iter().enumerate() {
let n = p.len() * if bits == 8 { 1 } else { 2 };
let same = theirs[off..off + n] == raw_bytes(&Frame { planes: vec![p.clone()], ..want.clone() })[..];
eprintln!("{pix_fmt} s{slices} c{ctx}: plane {i} {}", if same { "equal" } else { "DIFFERENT" });
off += n;
}
panic!("{pix_fmt} s{slices} c{ctx}: our pixels differ from ffmpeg's");
}
}
}
}
}