Merge main into i778/image-codecs-avif-decode

# Conflicts:
#	crates/image-codecs/README.md
This commit is contained in:
FredApps
2026-11-15 21:34:48 +01:00
116 changed files with 15389 additions and 395 deletions
+3
View File
@@ -14,3 +14,6 @@ crates/font-store/index/sources.json -text
# The build fixture verifies these license files are copied byte-for-byte.
crates/fonts-build/tests/fixture/craft-fonts/fonts/**/OFL.txt -text
# DXF drawing goldens assert exact writer bytes, including LF on every platform.
crates/dxf/tests/data/drawing_*.dxf -text
+1
View File
@@ -6,6 +6,7 @@ author, source and license. Library code and original assets are under the repos
| File | Author | Source | License |
|---|---|---|---|
| `licenses/jpeg-encoder-0.6.1/`, `crates/image-codecs/NOTICE` | jpeg-encoder contributors and the IJG, libjpeg-turbo and SIMD authors named in the retained notices | [jpeg-encoder 0.6.1](https://crates.io/crates/jpeg-encoder/0.6.1), used unmodified; source checksums in `SOURCE.json` | (MIT OR Apache-2.0) AND IJG |
| `crates/smime/testdata/` | MailCraft contributors; RFC 9216 example certificates | [MailCraft at 240d1d42](https://github.com/UCS-Care-Group/mailcraft/tree/240d1d42fe394cf3274402b180663ba9b43ca180/crates/smime/testdata), restored unmodified; provenance in the directory README | MIT OR Apache-2.0 (source repository) |
| `licenses/dwg/acadrust-MPL-2.0.txt`, `licenses/dwg/acadrust-THIRD_PARTY_NOTICES.md` | `acadrust` contributors; ACadSharp authors for the noted ported routines | [acadrust 0.6.6](https://github.com/hakanaktt/acadrust), used unmodified as `craft-dwg`'s optional native dependency | MPL-2.0; embedded ACadSharp notice is MIT; see both retained files |
| `crates/cms/profiles/photocraft-coated-cmyk.icc` | PhotoCraft contributors; generated by `craft_cms::synth::coated_cmyk` | [PhotoCraft `crates/cms`](https://github.com/storytold/photocraft/tree/b3222ee8e7e6bcb4799c37f904c595f20f9baf5e/crates/cms) | CC0-1.0 |
Generated
+36 -7
View File
@@ -1425,6 +1425,7 @@ version = "0.1.0"
name = "craft-captions"
version = "0.1.0"
dependencies = [
"craft-media-testkit",
"craft-timeline",
"craft-xml",
"proptest",
@@ -1843,6 +1844,13 @@ dependencies = [
"image",
]
[[package]]
name = "craft-gzip"
version = "0.1.0"
dependencies = [
"flate2",
]
[[package]]
name = "craft-hdr"
version = "0.1.0"
@@ -1919,6 +1927,7 @@ dependencies = [
"avif-serialize",
"craft-heif",
"craft-jpeg",
"craft-media-testkit",
"craft-metadata",
"craft-video",
"crc32fast",
@@ -2075,6 +2084,14 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-mcp"
version = "0.1.0"
dependencies = [
"craft-jsonrpc",
"serde_json",
]
[[package]]
name = "craft-media"
version = "0.1.0"
@@ -2375,13 +2392,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"
@@ -2399,6 +2409,13 @@ dependencies = [
"zbus",
]
[[package]]
name = "craft-plist"
version = "0.1.0"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-plotter"
version = "0.1.0"
@@ -2696,6 +2713,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",
@@ -2712,6 +2730,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"
@@ -2841,6 +2869,7 @@ name = "craft-vectorcraft-format"
version = "0.1.0"
dependencies = [
"flate2",
"proptest",
"serde",
"serde_json",
"thiserror 2.0.21",
+3
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" }
@@ -91,6 +92,7 @@ craft-jmap = { path = "crates/jmap" }
craft-ical = { path = "crates/ical" }
craft-jpeg = { path = "crates/jpeg" }
craft-jsonrpc = { path = "crates/jsonrpc" }
craft-mcp = { path = "crates/mcp" }
craft-jxl = { path = "crates/jxl" }
craft-kiwi = { path = "crates/kiwi" }
craft-keymap = { path = "crates/keymap" }
@@ -145,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" }
+123
View File
@@ -296,3 +296,126 @@ implementation, https://github.com/macosforge/alac at c38887c5c5e64a4b31108733bd
(LICENSE-APACHE). The files are modified: translated to Rust, with bounds checks on hostile input,
wrapping integer arithmetic, the unrolled predictor and per-width matrix routines folded into one,
and the average bit rate filled in. Those files are Apache-2.0 only.
JPEG encoding dependency: jpeg-encoder 0.6.1
Source: https://crates.io/crates/jpeg-encoder/0.6.1
Declared license: (MIT OR Apache-2.0) AND IJG.
This software is based in part on the work of the Independent JPEG Group.
The MIT license and unaltered upstream IJG and SIMD notices follow. The Apache-2.0
alternative is also retained in licenses/jpeg-encoder-0.6.1 at the repository root.
The dependency is used unmodified; no encoder implementation is vendored here.
LICENSE-MIT
Copyright (c) 2021 Volker Ströbel <volkerstroebel@mysurdity.de>
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Original src/fdct.rs opening notice
/*
* Ported from mozjpeg to rust
*
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1991-1996, Thomas G. Lane.
* libjpeg-turbo Modifications:
* Copyright (C) 2015, 2020, D. R. Commander.
*
* Conditions of distribution and use:
* In plain English:
*
* 1. We don't promise that this software works. (But if you find any bugs,
* please let us know!)
* 2. You can use this software for whatever you want. You don't have to pay us.
* 3. You may not pretend that you wrote this software. If you use it in a
* program, you must acknowledge somewhere in your documentation that
* you've used the IJG code.
*
* In legalese:
*
* The authors make NO WARRANTY or representation, either express or implied,
* with respect to this software, its quality, accuracy, merchantability, or
* fitness for a particular purpose. This software is provided "AS IS", and you,
* its user, assume the entire risk as to its quality and accuracy.
*
* This software is copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
* All Rights Reserved except as specified below.
*
* Permission is hereby granted to use, copy, modify, and distribute this
* software (or portions thereof) for any purpose, without fee, subject to these
* conditions:
* (1) If any part of the source code for this software is distributed, then this
* README file must be included, with this copyright and no-warranty notice
* unaltered; and any additions, deletions, or changes to the original files
* must be clearly indicated in accompanying documentation.
* (2) If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the work of
* the Independent JPEG Group".
* (3) Permission for use of this software is granted only if the user accepts
* full responsibility for any undesirable consequences; the authors accept
* NO LIABILITY for damages of any kind.
*
* These conditions apply to any software derived from or based on the IJG code,
* not just to the unmodified library. If you use our work, you ought to
* acknowledge us.
*
* Permission is NOT granted for the use of any IJG author's name or company name
* in advertising or publicity relating to this software or products derived from
* it. This software may be referred to only as "the Independent JPEG Group's
* software".
*
* We specifically permit and encourage the use of this software as the basis of
* commercial products, provided that all warranty or liability claims are
* assumed by the product vendor.
*
* This file contains a slower but more accurate integer implementation of the
* forward DCT (Discrete Cosine Transform).
*
* A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
* on each column. Direct algorithms are also available, but they are
* much more complex and seem not to be any faster when reduced to code.
*
* This implementation is based on an algorithm described in
* C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
* Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
* Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
* The primary algorithm described there uses 11 multiplies and 29 adds.
* We use their alternate method with 12 multiplies and 32 adds.
* The advantage of this method is that no data path contains more than one
* multiplication; this allows a very simple and accurate implementation in
* scaled fixed-point arithmetic, with a minimal number of shifts.
*/
Original src/avx2/fdct.rs opening notice
/*
* Ported from mozjpeg / jfdctint-avx2.asm to rust
* Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
* Copyright (C) 2009, 2016, 2018, 2020, D. R. Commander.
*
* Based on the x86 SIMD extension for IJG JPEG library
* Copyright (C) 1999-2006, MIYASAKA Masaru.
*/
+16
View File
@@ -108,3 +108,19 @@ fn objects_nested_too_deeply_are_an_error() {
let e = store::read(&chain(250)).unwrap_err();
assert!(e.contains("nested too deeply"), "{e}");
}
/// Many distinct property IDs still reject a duplicate without quadratic scanning.
#[test]
fn duplicate_after_many_distinct_properties_is_rejected() {
let mut props = vec![0x4C, 0x20];
props.extend_from_slice(&8193u16.to_le_bytes());
for id in (0..8192u16).chain(std::iter::once(7)) {
props.extend_from_slice(&id.to_le_bytes());
props.extend_from_slice(&0x82u16.to_le_bytes());
props.extend_from_slice(&0u16.to_le_bytes());
}
let mut writer = Writer::new(Version::V3);
writer.stream(Writer::ROOT, "properties", props).unwrap();
let e = store::read(&writer.finish()).unwrap_err();
assert!(e.contains("duplicate property id"), "{e}");
}
+3 -1
View File
@@ -461,11 +461,13 @@ impl R<'_> {
if at > props.len() {
return Err(bad("truncated property index"));
}
// A properties stream can name tens of thousands of ids: do not rescan all earlier ids.
let mut seen = std::collections::HashSet::with_capacity(n);
for i in 0..n {
let (Some(pid), Some(sf), Some(len)) = (rd_u16(&props, 4 + i * 6), rd_u16(&props, 6 + i * 6), rd_u16(&props, 8 + i * 6)) else {
return Err(bad("truncated property index"));
};
if o.props.iter().any(|(previous, _)| *previous == pid) {
if !seen.insert(pid) {
return Err(bad("duplicate property id"));
}
let v = props.get(at..at + len as usize).ok_or_else(|| bad("truncated property value"))?;
+1 -1
View File
@@ -21,7 +21,7 @@
//! transition's name) are not in the specification; they were first written by FilmCraft and keep
//! their names so that existing files read back.
pub(crate) mod bento;
pub mod bento;
mod read;
mod write;
+18 -7
View File
@@ -35,8 +35,8 @@
use std::collections::{BTreeMap, HashMap};
pub(crate) const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7];
pub(crate) const LABEL_SIZE: usize = 24;
pub const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7];
pub const LABEL_SIZE: usize = 24;
// standard object ids
const TOC_OBJECT: u32 = 1;
@@ -82,7 +82,7 @@ const MAX_VALUE_BYTES: usize = 16 << 20;
const MAX_NAME_BYTES: usize = 4 << 20;
/// Builds a Bento container.
pub(crate) struct Writer {
pub struct Writer {
data: Vec<u8>,
names: HashMap<(u32, String), u32>,
next: u32,
@@ -90,6 +90,12 @@ pub(crate) struct Writer {
entries: Vec<(u32, u32, u32, Vec<Seg>)>,
}
impl Default for Writer {
fn default() -> Self {
Self::new()
}
}
impl Writer {
pub fn new() -> Writer {
Writer { data: Vec::new(), names: HashMap::new(), next: MIN_USER_ID, entries: Vec::new() }
@@ -217,9 +223,9 @@ pub(crate) struct Prop {
}
/// A parsed Bento container.
pub(crate) struct Container<'a> {
pub struct Container<'a> {
data: &'a [u8],
pub objects: BTreeMap<u32, Vec<Prop>>,
pub(crate) objects: BTreeMap<u32, Vec<Prop>>,
names: HashMap<u32, String>,
by_name: HashMap<String, u32>,
}
@@ -229,11 +235,16 @@ fn be32(b: &[u8], at: usize) -> Option<u32> {
}
/// Whether `b` ends with a Bento label.
pub(crate) fn sniff(b: &[u8]) -> bool {
pub fn sniff(b: &[u8]) -> bool {
b.len() >= LABEL_SIZE && b[b.len() - LABEL_SIZE..b.len() - LABEL_SIZE + 8] == MAGIC
}
impl<'a> Container<'a> {
/// Object identifiers in ascending order.
pub fn object_ids(&self) -> impl Iterator<Item = u32> + '_ {
self.objects.keys().copied()
}
pub fn open(data: &'a [u8]) -> Result<Container<'a>, String> {
if !sniff(data) {
return Err("no Bento label at the end of the file".into());
@@ -324,7 +335,7 @@ impl<'a> Container<'a> {
}
/// The bytes of a value.
pub fn value(&self, p: &Prop) -> Result<Vec<u8>, String> {
pub(crate) fn value(&self, p: &Prop) -> Result<Vec<u8>, String> {
let total = p.segs.iter().try_fold(0usize, |total, s| {
let len = match s {
Seg::Imm(b) => b.len(),
+27
View File
@@ -0,0 +1,27 @@
use craft_aaf::omf::bento::{Container, Writer, sniff};
#[test]
fn external_consumer_reads_named_values_and_references() {
let mut writer = Writer::new();
let first = writer.new_object();
let second = writer.new_object();
writer.set(first, "test:value", "test:type", b"long value");
writer.set_references(first, &[second]);
let bytes = writer.finish();
assert!(sniff(&bytes));
let container = Container::open(&bytes).unwrap();
assert!(container.object_ids().any(|id| id == first));
assert_eq!(container.get(first, "test:value").as_deref(), Some(b"long value".as_slice()));
assert_eq!(container.resolve(first, second), second);
assert!(Container::open(&bytes[..bytes.len() - 1]).is_err());
}
#[test]
fn default_writer_builds_a_readable_empty_container() -> Result<(), String> {
let bytes = Writer::default().finish();
let container = Container::open(&bytes)?;
let ids: Vec<_> = container.object_ids().collect();
assert!(!ids.is_empty());
assert!(ids.windows(2).all(|pair| pair[0] < pair[1]));
Ok(())
}
+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"]
+54
View File
@@ -343,6 +343,51 @@ impl AudioSpecificConfig {
}
}
/// Why an AudioSpecificConfig prefix cannot be written as an ADTS header.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdtsConfigError {
/// Fewer than two bytes were supplied.
Short,
/// The prefix names a profile, indexed rate or channel configuration ADTS cannot carry.
Unsupported {
/// Audio object type from the prefix.
object_type: u8,
/// Sampling frequency index from the prefix.
sf_index: u8,
/// Channel configuration from the prefix.
channel_config: u8,
},
}
/// ADTS fixed fields taken from the prefix of an AudioSpecificConfig.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AdtsConfig {
/// Audio object type, 1 through 4.
pub object_type: u8,
/// Indexed sample rate, 0 through 12.
pub sf_index: u8,
/// Channel configuration, 1 through 7.
pub channel_config: u8,
}
impl AdtsConfig {
/// Parse only the first two ASC bytes. Unlike a decoder's full ASC parser, this accepts all
/// four object types representable by ADTS and rejects configurations needing a PCE.
pub fn from_asc_prefix(asc: &[u8]) -> std::result::Result<Self, AdtsConfigError> {
let (Some(&a), Some(&b)) = (asc.first(), asc.get(1)) else { return Err(AdtsConfigError::Short) };
let object_type = a >> 3;
let sf_index = ((a & 7) << 1) | (b >> 7);
let channel_config = (b >> 3) & 15;
if !(1..=4).contains(&object_type) || sf_index > 12 || !(1..=7).contains(&channel_config) {
return Err(AdtsConfigError::Unsupported { object_type, sf_index, channel_config });
}
Ok(Self { object_type, sf_index, channel_config })
}
/// Write a no-CRC ADTS header with variable-rate buffer fullness for one payload.
pub fn header(self, payload_len: usize) -> Result<[u8; 7]> {
AdtsHeader::write(self.object_type, self.sf_index, self.channel_config, payload_len, 0x7ff)
}
}
/// A parsed ADTS header.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -519,6 +564,15 @@ mod tests {
assert_eq!(AudioSpecificConfig::parse(&asc.to_bytes().unwrap()).unwrap(), asc);
}
#[test]
fn adts_config_preserves_prefix_and_frame_limits() {
let config = AdtsConfig::from_asc_prefix(&[0x12, 0x10]).unwrap();
assert_eq!(config.header(100).unwrap(), AdtsHeader::write(2, 4, 2, 100, 0x7ff).unwrap());
assert!(config.header(8185).is_err());
assert_eq!(AdtsConfig::from_asc_prefix(&[]), Err(AdtsConfigError::Short));
assert!(matches!(AdtsConfig::from_asc_prefix(&[0x2a, 0x10]), Err(AdtsConfigError::Unsupported { object_type: 5, .. })));
}
#[test]
fn adts_headers_refuse_fields_they_cannot_hold() {
for (object_type, sf_index, channel_config) in [(0, 4, 2), (5, 4, 2), (29, 4, 2), (2, 13, 2), (2, 15, 2), (2, 4, 8), (2, 4, 11), (2, 4, 12)] {
+1 -1
View File
@@ -60,7 +60,7 @@ mod tns;
#[cfg(feature = "aac-decode")]
pub(crate) use config::split_adts_upto;
pub use config::{AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts};
pub use config::{AdtsConfig, AdtsConfigError, AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts};
#[cfg(feature = "aac-decode")]
pub use decoder::Decoder;
#[cfg(feature = "aac-encode")]
+7 -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"))]
@@ -66,6 +69,9 @@ mod wav;
feature = "vorbis-encode"
))]
mod write;
/// 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");
}
}
+2
View File
@@ -19,3 +19,5 @@ thiserror = { workspace = true }
proptest = { workspace = true }
# A second XML reader for the tests: checks of written TTML, and the comparison in tests/it/ttml_xml_views.rs.
roxmltree = "0.21"
# ffmpeg as the reference for the ASS tests (tests/it/ass.rs).
craft-media-testkit = { workspace = true }
+1
View File
@@ -55,6 +55,7 @@ lists at most 20 characters per stream.
| EBU STL `.stl` | yes | yes | EBU Tech 3264-E: GSI block + 128-byte TTI blocks, `STL25.01` (25 fps) or `STL30.01` (read/written as 29.97 fps, non-drop labels), ISO/IEC 6937 Latin text (accents as diacritic + letter), italics / underline codes as `<i>` / `<u>`, extension blocks for long subtitles, start-of-programme (TCP) subtracted. |
| TTML `.ttml` (IMSC1) | yes | yes | Writes the IMSC1 Text profile (TTML2 namespaces) with the document's language as `xml:lang` (read back on import): `<p>` per cue, `<br/>`, styled `<span>`s for `<i>` / `<b>` / `<u>`, `ttm:agent` speakers, exact frame times (`123f`) at the sequence rate. Reads any TTML (TTML1, TTML2, the 2006 `ttaf1` drafts): clock / frame / offset / tick times, `dur`, nested time containers (`body` / `div` begins offset and ends clip their contents; `seq` containers are read as `par` with a warning), timed spans, referenced styles. |
| DFXP `.dfxp` | yes | yes | TTML1 with the DFXP presentation profile; clock times in milliseconds (frame-exact after `snap_to_frames`). |
| ASS / SSA `.ass`, `.ssa` | yes | yes (ASS v4+) | The `[Events]` section: `Dialogue:` lines through the file's `Format:` line, `Name` as the speaker, `\N`, `\n`, `\h` and the `\i`, `\b`, `\u` override tags kept (as `<i>`, `<b>`, `<u>`); other override tags and drawing lines are dropped. Times are centiseconds. Written files have one `Default` style. |
All cue times are exact `Tick`s. Milliseconds convert exactly (1 ms = 254 016 000 ticks); SCC
frames map through `FrameRate::FPS_29_97`. `Document::snap_to_frames` snaps cues to a frame grid and
+244
View File
@@ -0,0 +1,244 @@
//! Advanced SubStation Alpha and SubStation Alpha (`.ass`, `.ssa`).
//!
//! Reading takes the `[Events]` section: its `Format:` line gives the field order (ASS v4+ and SSA
//! v4 differ in the first field only), each `Dialogue:` line becomes a cue; `Comment:` lines and
//! the other sections are skipped. A cue gets the `Name` field as its speaker. The text keeps what
//! the neutral model can show: `\N` and `\n` become a line break and a space, `\h` a no-break
//! space, and the override tags `\i`, `\b` and `\u` become `<i>`, `<b>` and `<u>`. Every other
//! override tag (colours, fonts, positions, karaoke, animation) is dropped, and a line that draws
//! vector shapes (`\p1`) is skipped with a warning.
//!
//! Writing gives a minimal ASS v4+ file: one `Default` style, one `Dialogue:` line per cue with
//! times to the centisecond. Braces in the text, which ASS reads as override blocks, are written
//! as the full-width `{` and `}`.
use crate::time::{TICKS_PER_SECOND, Tick};
use crate::{Cue, Document, Error, Result};
const TICKS_PER_CS: i64 = TICKS_PER_SECOND / 100;
/// `H:MM:SS.cc`, the form SSA and ASS use (any number of fraction digits is read).
fn parse_time(s: &str) -> Option<Tick> {
let s = s.trim();
let (hms, frac) = s.split_once('.').unwrap_or((s, ""));
let mut parts = hms.split(':');
let (h, m, sec) = (parts.next()?, parts.next()?, parts.next()?);
if parts.next().is_some() {
return None;
}
let num = |p: &str| -> Option<i64> { (!p.is_empty() && p.len() <= 9 && p.bytes().all(|b| b.is_ascii_digit())).then(|| p.parse().ok()).flatten() };
let (h, m, sec) = (num(h)?, num(m)?, num(sec)?);
if m >= 60 || sec >= 60 || h > 9999 {
return None;
}
let mut ticks = ((h * 60 + m) * 60 + sec) * TICKS_PER_SECOND;
if !frac.is_empty() {
if frac.len() > 9 || !frac.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
let d: i64 = frac.parse().ok()?;
ticks += (i128::from(d) * i128::from(TICKS_PER_SECOND) / 10i128.pow(frac.len() as u32)) as i64;
}
Some(Tick(ticks))
}
fn format_time(t: Tick) -> String {
let cs = (i128::from(t.0.max(0)) + i128::from(TICKS_PER_CS) / 2) / i128::from(TICKS_PER_CS);
let (h, m, s, c) = (cs / 360_000, (cs / 6000) % 60, (cs / 100) % 60, cs % 100);
format!("{h}:{m:02}:{s:02}.{c:02}")
}
/// The Text field of a Dialogue line as cue text; `None` when the line draws shapes.
fn convert_text(raw: &str) -> Option<String> {
let mut out = String::new();
// Open `<i>`, `<b>`, `<u>` in the order they were opened.
let mut open: Vec<char> = Vec::new();
let mut chars = raw.chars().peekable();
let mut drawing = false;
let mut dropped_drawing = false;
while let Some(c) = chars.next() {
match c {
'{' => {
let mut block = String::new();
let mut closed = false;
for b in chars.by_ref() {
if b == '}' {
closed = true;
break;
}
block.push(b);
}
if !closed {
// An unterminated block is literal text.
out.push('{');
out.push_str(&block);
continue;
}
for tag in block.split('\\').skip(1) {
let tag = tag.trim_end();
let (name, val) = tag.split_at(tag.find(|c: char| !c.is_ascii_alphabetic()).unwrap_or(tag.len()));
match name {
"i" | "b" | "u" => {
let letter = name.chars().next().unwrap_or('i');
let on = val.trim() != "0";
if on && !open.contains(&letter) {
open.push(letter);
out.push_str(&format!("<{letter}>"));
} else if !on && open.contains(&letter) {
// Close back to it, reopening what was opened after.
let at = open.iter().position(|&o| o == letter).unwrap_or(0);
let after: Vec<char> = open.split_off(at + 1);
for o in after.iter().rev() {
out.push_str(&format!("</{o}>"));
}
open.pop();
out.push_str(&format!("</{letter}>"));
for o in after {
open.push(o);
out.push_str(&format!("<{o}>"));
}
}
}
"p" => drawing = val.trim().parse::<u32>().unwrap_or(0) != 0,
_ => {}
}
}
}
'\\' => match chars.peek() {
Some('N') => {
chars.next();
out.push('\n');
}
Some('n') => {
chars.next();
out.push(' ');
}
Some('h') => {
chars.next();
out.push('\u{a0}');
}
_ => out.push('\\'),
},
c => {
if drawing {
dropped_drawing = true;
} else {
out.push(c);
}
}
}
}
if dropped_drawing && out.trim().is_empty() {
return None;
}
while let Some(o) = open.pop() {
out.push_str(&format!("</{o}>"));
}
Some(out)
}
pub fn parse(text: &str) -> Result<Document> {
let text = text.strip_prefix('\u{feff}').unwrap_or(text);
let mut doc = Document::default();
let mut in_events = false;
let mut saw_events = false;
// Column of each field in a Dialogue line.
let mut cols: Vec<String> =
["layer", "start", "end", "style", "name", "marginl", "marginr", "marginv", "effect", "text"].map(String::from).to_vec();
let mut drawings = 0usize;
for (n, line) in text.lines().enumerate() {
let line = line.trim();
if line.starts_with('[') && line.ends_with(']') {
in_events = line.eq_ignore_ascii_case("[events]");
saw_events |= in_events;
continue;
}
if !in_events {
continue;
}
let Some((key, rest)) = line.split_once(':') else { continue };
let rest = rest.trim_start();
match key.trim().to_ascii_lowercase().as_str() {
"format" => cols = rest.split(',').map(|f| f.trim().to_ascii_lowercase()).collect(),
"dialogue" => {
// The last column (Text) keeps its commas.
let fields: Vec<&str> = rest.splitn(cols.len(), ',').collect();
let get = |name: &str| cols.iter().position(|c| c == name).and_then(|i| fields.get(i)).copied();
let (Some(start), Some(end), Some(raw)) = (get("start").and_then(parse_time), get("end").and_then(parse_time), get("text")) else {
doc.warnings.push(format!("line {}: not a Dialogue line this reader understands", n + 1));
continue;
};
let Some(text) = convert_text(raw) else {
drawings += 1;
continue;
};
let name = get("name").map(str::trim).filter(|s| !s.is_empty());
doc.cues.push(Cue { start, end, text, speaker: name.map(str::to_string), ..Default::default() });
}
_ => {}
}
}
if !saw_events {
return Err(Error::NotFormat("ASS"));
}
if drawings > 0 {
doc.warnings.push(format!("{drawings} vector drawing line(s) were skipped"));
}
Ok(doc)
}
fn escape(text: &str) -> String {
let mut out = String::with_capacity(text.len() + 8);
let mut rest = text;
while !rest.is_empty() {
if let Some(r) = rest.strip_prefix("\r\n") {
out.push_str("\\N");
rest = r;
} else if let Some(r) = rest.strip_prefix("<i>") {
out.push_str("{\\i1}");
rest = r;
} else if let Some(r) = rest.strip_prefix("</i>") {
out.push_str("{\\i0}");
rest = r;
} else if let Some(r) = rest.strip_prefix("<b>") {
out.push_str("{\\b1}");
rest = r;
} else if let Some(r) = rest.strip_prefix("</b>") {
out.push_str("{\\b0}");
rest = r;
} else if let Some(r) = rest.strip_prefix("<u>") {
out.push_str("{\\u1}");
rest = r;
} else if let Some(r) = rest.strip_prefix("</u>") {
out.push_str("{\\u0}");
rest = r;
} else if let Some(c) = rest.chars().next() {
match c {
'\n' | '\r' => out.push_str("\\N"),
// Empty override block separates a literal slash from N/n/h.
'\\' => out.push_str("\\{}"),
'{' => out.push('\u{ff5b}'),
'}' => out.push('\u{ff5d}'),
'\u{a0}' => out.push_str("\\h"),
c => out.push(c),
}
rest = &rest[c.len_utf8()..];
}
}
out
}
pub fn write(doc: &Document) -> String {
let mut out = String::from(
"[Script Info]\n; Written by craft-captions\nScriptType: v4.00+\nWrapStyle: 0\nScaledBorderAndShadow: yes\nPlayResX: 384\nPlayResY: 288\n\n\
[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n\
Style: Default,Arial,16,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,1\n\n\
[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n",
);
for c in &doc.cues {
// A comma in the Name field would shift the columns; the other fields are fixed text.
let name = c.speaker.as_deref().unwrap_or("").replace([',', '\n', '\r'], " ");
out.push_str(&format!("Dialogue: 0,{},{},Default,{},0,0,0,,{}\n", format_time(c.start), format_time(c.end), name, escape(&c.text)));
}
out
}
+14 -3
View File
@@ -4,7 +4,7 @@
//! - **Files:** [SubRip](srt) (`.srt`), [WebVTT](vtt) (`.vtt`), [Scenarist SCC](scc) (`.scc`,
//! CEA-608 pop-on, roll-up and paint-on on read; pop-on on write), [MacCaption MCC](mcc)
//! (`.mcc`, SMPTE ST 334-2 packets with CEA-608 and CEA-708), [EBU STL](stl) (`.stl`, EBU
//! Tech 3264) and [TTML](ttml) (`.ttml` IMSC1 Text profile, `.dfxp` DFXP) parse into a
//! Tech 3264), [ASS/SSA](ass) (`.ass`, `.ssa`) and [TTML](ttml) (`.ttml` IMSC1 Text profile, `.dfxp` DFXP) parse into a
//! [`Document`] of [`Cue`]s and write back. Readers are forgiving (BOM, UTF-16, Windows-1252
//! fallback, CRLF/CR, missing SRT indexes, `,` or `.` milliseconds, missing hours, no blank
//! line between cues); writers are strict. WebVTT cue identifiers, cue settings, `<v Speaker>`
@@ -32,6 +32,7 @@
#[doc = include_str!("../README.md")]
struct ReadmeExamples;
pub mod ass;
pub mod cea608;
pub mod mcc;
pub mod scc;
@@ -69,10 +70,12 @@ pub enum Format {
Ttml,
/// DFXP (TTML1).
Dfxp,
/// Advanced SubStation Alpha (also reads SubStation Alpha).
Ass,
}
impl Format {
pub const ALL: [Format; 7] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp];
pub const ALL: [Format; 8] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp, Format::Ass];
pub fn name(self) -> &'static str {
match self {
Format::Srt => "SubRip (SRT)",
@@ -82,6 +85,7 @@ impl Format {
Format::Stl => "EBU STL",
Format::Ttml => "TTML (IMSC1)",
Format::Dfxp => "DFXP (TTML1)",
Format::Ass => "Advanced SubStation Alpha (ASS)",
}
}
pub fn extension(self) -> &'static str {
@@ -93,6 +97,7 @@ impl Format {
Format::Stl => "stl",
Format::Ttml => "ttml",
Format::Dfxp => "dfxp",
Format::Ass => "ass",
}
}
pub fn from_name(s: &str) -> Option<Format> {
@@ -104,6 +109,7 @@ impl Format {
"stl" | "ebu-stl" | "ebustl" | "ebu" => Some(Format::Stl),
"ttml" | "imsc" | "imsc1" | "xml" => Some(Format::Ttml),
"dfxp" => Some(Format::Dfxp),
"ass" | "ssa" => Some(Format::Ass),
_ => None,
}
}
@@ -251,6 +257,9 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option<Format> {
if head.starts_with("File Format=MacCaption_MCC") {
return Some(Format::Mcc);
}
if head.starts_with("[Script Info]") {
return Some(Format::Ass);
}
let ext = ext.map(|e| e.trim_start_matches('.').to_ascii_lowercase());
if (head.starts_with('<') && (head.contains("<tt ") || head.contains("<tt>") || head.contains(":tt ")))
&& (head.contains("ns/ttml") || head.contains("ttaf1"))
@@ -262,7 +271,7 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option<Format> {
match ext.as_deref() {
Some("srt") if has_arrow_timing || head.is_empty() => Some(Format::Srt),
Some("vtt") if has_arrow_timing => Some(Format::WebVtt),
Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml") => None,
Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa") => None,
_ if has_arrow_timing => Some(Format::Srt),
_ => None,
}
@@ -296,6 +305,7 @@ fn parse_unbounded(bytes: &[u8], format: Format) -> Result<Document> {
Format::Scc => scc::parse(&text),
Format::Mcc => mcc::parse(&text),
Format::Ttml | Format::Dfxp => ttml::parse(&text),
Format::Ass => ass::parse(&text),
Format::Stl => stl::parse(bytes),
}
}
@@ -333,6 +343,7 @@ pub fn write(doc: &Document, format: Format, opts: WriteOptions) -> Vec<u8> {
Format::Stl => stl::write(doc, opts.rate, opts.program),
Format::Ttml => ttml::write(doc, ttml::Flavor::Imsc1, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(),
Format::Dfxp => ttml::write(doc, ttml::Flavor::Dfxp, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(),
Format::Ass => ass::write(doc).into_bytes(),
}
}
+142
View File
@@ -0,0 +1,142 @@
//! SubStation Alpha / Advanced SubStation Alpha: reading, writing, and ffmpeg as the reference
//! (oracle; skipped when ffmpeg is absent, a failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`).
use craft_captions::{Cue, Document, Format, Tick, WriteOptions, detect, parse, write};
use std::process::Command;
const T: i64 = craft_captions::TICKS_PER_SECOND;
fn cs(n: i64) -> Tick {
Tick(n * (T / 100))
}
const SAMPLE: &str = "\u{feff}[Script Info]\n; a comment\nTitle: Test\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname\nStyle: Default,Arial\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nComment: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,not a cue\nDialogue: 0,0:00:01.50,0:00:03.25,Default,Bob,0,0,0,,Hello, {\\i1}world{\\i0}\\Nsecond {\\b1}line{\\b0}\nDialogue: 0,1:02:03.04,1:02:04.5,Default,,0,0,0,,{\\pos(10,20)\\c&H0000FF&}Red\\hspace\\nsoft\nDialogue: 0,0:00:09.00,0:00:10.00,Default,,0,0,0,,{\\p1}m 0 0 l 100 0 100 100{\\p0}\n";
#[test]
fn reads_dialogue_lines_with_commas_overrides_and_line_breaks() {
let doc = parse(SAMPLE.as_bytes(), Format::Ass).unwrap();
assert_eq!(doc.cues.len(), 2);
assert_eq!(doc.cues[0].start, cs(150));
assert_eq!(doc.cues[0].end, cs(325));
assert_eq!(doc.cues[0].text, "Hello, <i>world</i>\nsecond <b>line</b>");
assert_eq!(doc.cues[0].speaker.as_deref(), Some("Bob"));
// 1:02:03.04, a one-digit fraction, an override block dropped, \h and \n.
assert_eq!(doc.cues[1].start, Tick((3600 + 120 + 3) * T + 4 * (T / 100)));
assert_eq!(doc.cues[1].end, Tick((3600 + 120 + 4) * T + T / 2));
assert_eq!(doc.cues[1].text, "Red\u{a0}space soft");
assert!(doc.warnings.iter().any(|w| w.contains("vector drawing")), "{:?}", doc.warnings);
}
#[test]
fn ssa_v4_files_read_through_their_own_format_line() {
let ssa = "[Script Info]\nScriptType: v4.00\n\n[V4 Styles]\nFormat: Name\n\n[Events]\nFormat: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: Marked=0,0:00:01.00,0:00:02.00,*Default,,0000,0000,0000,,Old style\n";
let doc = parse(ssa.as_bytes(), Format::Ass).unwrap();
assert_eq!((doc.cues[0].start, doc.cues[0].end, doc.cues[0].text.as_str()), (cs(100), cs(200), "Old style"));
}
#[test]
fn write_then_read_keeps_times_text_and_speaker() {
let cues = vec![
Cue { start: cs(100), end: cs(250), text: "Line one\n<i>line two</i>".into(), speaker: Some("Ann".into()), ..Default::default() },
Cue {
start: Tick(7200 * T + 99 * (T / 100)),
end: Tick(7203 * T),
text: "<b>bold</b> and <u>under</u> {braces}".into(),
..Default::default()
},
];
let doc = Document { cues, ..Default::default() };
let bytes = write(&doc, Format::Ass, WriteOptions::default());
assert_eq!(detect(&bytes, Some("ass")), Some(Format::Ass));
let back = parse(&bytes, Format::Ass).unwrap();
assert_eq!(back.cues[0], doc.cues[0]);
assert_eq!(back.cues[1].start, doc.cues[1].start);
// Braces are written full-width, as ASS reads `{` as the start of an override block.
assert_eq!(back.cues[1].text, "<b>bold</b> and <u>under</u> \u{ff5b}braces\u{ff5d}");
}
#[test]
fn files_without_an_events_section_are_not_ass() {
assert!(parse(b"[Script Info]\nTitle: x\n", Format::Ass).is_err());
assert!(parse(b"hello", Format::Ass).is_err());
assert_eq!(detect(b"hello", Some("ass")), None);
}
#[test]
fn bad_lines_are_skipped_with_a_warning() {
let text = "[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: 0,garbage,0:00:02.00,Default,,0,0,0,,x\nDialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,ok\n";
let doc = parse(text.as_bytes(), Format::Ass).unwrap();
assert_eq!(doc.cues.len(), 1);
assert_eq!(doc.warnings.len(), 1);
}
fn ffmpeg_run(ff: &std::path::Path, args: &[&str]) {
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));
}
const SRT: &str = "1\n00:00:01,500 --> 00:00:03,250\nHello, <i>world</i>\nsecond <b>line</b>\n\n2\n00:00:04,000 --> 00:00:05,990\nPlain, with a comma\n\n3\n01:00:00,010 --> 01:00:02,999\n<u>Under</u> and <i>italic <b>both</b></i>\n";
#[test]
fn we_read_what_ffmpeg_writes() {
let Some(ff) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("captions_ass");
let (srt, ass) = (dir.join("in.srt"), dir.join("ffmpeg.ass"));
std::fs::write(&srt, SRT).expect("write");
ffmpeg_run(&ff, &["-i", srt.to_str().expect("path"), ass.to_str().expect("path")]);
let from_ass = parse(&std::fs::read(&ass).expect("read"), Format::Ass).expect("parse ass");
let from_srt = parse(SRT.as_bytes(), Format::Srt).expect("parse srt");
assert_eq!(from_ass.cues.len(), from_srt.cues.len());
for (a, s) in from_ass.cues.iter().zip(&from_srt.cues) {
// ASS times are centiseconds; the SRT ones round to them.
assert_eq!(a.start, cs((s.start.0 + T / 200) / (T / 100)), "start of {:?}", s.text);
assert_eq!(a.end, cs((s.end.0 + T / 200) / (T / 100)), "end of {:?}", s.text);
assert_eq!(a.text, s.text);
}
}
#[test]
fn ffmpeg_reads_what_we_write() {
let Some(ff) = craft_media_testkit::ffmpeg() else { return };
let dir = craft_media_testkit::fixture_subdir("captions_ass");
let mut doc = parse(SRT.as_bytes(), Format::Srt).expect("parse srt");
doc.cues.push(Cue { start: cs(400_000), end: cs(400_100), text: r"C:\New\notes\hello and \N \n \h".into(), ..Default::default() });
let (ass, srt) = (dir.join("ours.ass"), dir.join("back.srt"));
std::fs::write(&ass, write(&doc, Format::Ass, WriteOptions::default())).expect("write");
ffmpeg_run(&ff, &["-i", ass.to_str().expect("path"), srt.to_str().expect("path")]);
let theirs = parse(&std::fs::read(&srt).expect("read"), Format::Srt).expect("parse back");
assert_eq!(theirs.cues.len(), doc.cues.len());
for (t, d) in theirs.cues.iter().zip(&doc.cues) {
assert_eq!(t.start, cs((d.start.0 + T / 200) / (T / 100)));
assert_eq!(t.end, cs((d.end.0 + T / 200) / (T / 100)));
assert_eq!(t.text, d.text, "text of the cue at {:?}", d.start);
}
}
#[test]
fn literal_backslashes_survive_ass_round_trip() {
let text = r"C:\New\notes\hello and \N \n \h";
let doc = Document { cues: vec![Cue { start: cs(100), end: cs(200), text: text.into(), ..Default::default() }], ..Default::default() };
let bytes = write(&doc, Format::Ass, WriteOptions::default());
assert_eq!(parse(&bytes, Format::Ass).unwrap().cues[0].text, text);
}
#[test]
fn carriage_returns_cannot_create_dialogue_records() {
let doc = Document {
cues: vec![Cue {
start: cs(100),
end: cs(200),
text: "first\rsecond\r\nthird".into(),
speaker: Some("Ann\rOther".into()),
..Default::default()
}],
..Default::default()
};
let bytes = write(&doc, Format::Ass, WriteOptions::default());
assert!(!bytes.contains(&b'\r'));
let back = parse(&bytes, Format::Ass).unwrap();
assert_eq!(back.cues.len(), 1);
assert_eq!(back.cues[0].text, "first\nsecond\nthird");
assert_eq!(back.cues[0].speaker.as_deref(), Some("Ann Other"));
}
+1
View File
@@ -1,3 +1,4 @@
mod ass;
mod limits;
mod literal_roundtrips;
mod mcc_708_timing;
+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;
+47 -7
View File
@@ -531,8 +531,7 @@ pub fn write_cube(l: &LutFile) -> String {
s.push_str(&format!("DOMAIN_MAX {} {} {}\n", l.domain_max[0], l.domain_max[1], l.domain_max[2]));
}
s.push_str(&format!("LUT_3D_SIZE {}\n", l.size));
write_cube_rows(&mut s, l);
s
cube_rows(s, l.data.as_chunks::<3>().0.iter().copied())
}
/// Writes a `.cube` file with a creator comment, section comments and an explicit input domain.
@@ -549,14 +548,33 @@ pub fn write_cube_commented(l: &LutFile, creator: &str) -> String {
s.push_str(&format!("DOMAIN_MAX {} {} {}\n", l.domain_max[0], l.domain_max[1], l.domain_max[2]));
}
s.push_str("\n#LUT data points\n");
write_cube_rows(&mut s, l);
cube_rows(s, l.data.as_chunks::<3>().0.iter().copied())
}
fn cube_rows(mut s: String, rows: impl Iterator<Item = [f32; 3]>) -> String {
for c in rows {
s.push_str(&format!("{:.6} {:.6} {:.6}\n", c[0], c[1], c[2]));
}
s
}
fn write_cube_rows(s: &mut String, l: &LutFile) {
for c in l.data.as_chunks::<3>().0 {
s.push_str(&format!("{:.6} {:.6} {:.6}\n", c[0], c[1], c[2]));
}
/// Annotated identity-domain CUBE export, streamed in red-fastest order without a table copy.
/// Callers supply exactly `size³` rows for a valid size (2..=[`MAX_SIZE`]), as with [`write_cube`].
/// Integer-depth exports clamp/round to 1/32768 steps; floating exports clamp without rounding.
pub fn write_cube_export(creator: &str, title: &str, size: usize, integer_depth: bool, rows: impl Iterator<Item = [f32; 3]>) -> String {
let s = format!(
"#Created by: {creator}\nTITLE \"{}\"\n\n#LUT size\nLUT_3D_SIZE {size}\n\n#data domain\nDOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n\n#LUT data points\n",
title.replace('"', "'")
);
cube_rows(
s,
rows.map(|c| {
c.map(|v| {
let v = v.clamp(0.0, 1.0);
if integer_depth { (v * 32768.0).round() / 32768.0 } else { v }
})
}),
)
}
fn luma(c: [f32; 3]) -> f32 {
@@ -1345,4 +1363,26 @@ LUT_3D_SIZE 2
assert_eq!(back.domain_min, l.domain_min, "the written table must mean the same");
assert_eq!(back.domain_max, l.domain_max);
}
#[test]
fn annotated_cube_export_preserves_header_and_integer_precision() {
let mut lut = LutFile::identity(2).unwrap();
lut.title = "My \"Look\"".into();
lut.data[..3].copy_from_slice(&[1.0 / 6.0, 5.0 / 6.0, 160.0 / 255.0]);
let output = write_cube_export("PhotoCraft", &lut.title, lut.size, true, lut.data.as_chunks::<3>().0.iter().copied());
assert!(output.starts_with("#Created by: PhotoCraft\nTITLE \"My 'Look'\"\n\n#LUT size\nLUT_3D_SIZE 2\n\n#data domain\nDOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n\n#LUT data points\n0.166656 0.833344 0.627441\n"));
assert_eq!(output.lines().count(), 11 + 8);
let parsed = parse_cube(&output).unwrap();
assert_eq!(parsed.size, 2);
assert_eq!(&parsed.data[3..], &lut.data[3..]);
let float = write_cube_export("PhotoCraft", &lut.title, lut.size, false, lut.data.as_chunks::<3>().0.iter().copied());
assert!(float.contains("0.166667 0.833333 0.627451\n"));
}
#[test]
fn annotated_empty_title_and_float_clamping() {
let text = write_cube_export("Exporter", "", 2, false, std::iter::repeat_n([-0.5, 0.25, 1.5], 8));
assert!(text.contains("TITLE \"\"\n"));
let back = parse_cube(&text).unwrap();
assert_eq!(back.data, [0.0, 0.25, 1.0].repeat(8));
}
}
+774
View File
@@ -0,0 +1,774 @@
//! Incremental ASCII DXF file writer used by VectorCraft.
//!
//! This is the file skeleton around a pre-written `ENTITIES` section: group codes, handles, and
//! the HEADER / TABLES / BLOCKS / OBJECTS sections CAD apps expect. It is a different API from
//! [`crate::model::write`], which serializes a parsed [`crate::model`] drawing as tags.
//!
//! Moved from `vectorcraft-cad` (`crates/cad/src/writer.rs`). The bytes a [`Drawing`] writes must
//! stay the same: VectorCraft's export tests treat this as the format oracle.
//!
//! DXF R13 and later give every object a handle and subclass markers; R2000 and later add layouts
//! and plot styles.
use std::fmt::Write as _;
pub use crate::model::write::Format;
/// An object handle (written in hex).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Handle(pub u32);
/// The handles of the fixed objects every drawing has. Entities and table entries count up from
/// [`FIRST_FREE`].
pub mod fixed {
use super::Handle;
pub const LAYER_TABLE: Handle = Handle(0x1);
pub const LTYPE_TABLE: Handle = Handle(0x2);
pub const APPID_TABLE: Handle = Handle(0x3);
pub const DIMSTYLE_TABLE: Handle = Handle(0x4);
pub const STYLE_TABLE: Handle = Handle(0x5);
pub const UCS_TABLE: Handle = Handle(0x6);
pub const VIEW_TABLE: Handle = Handle(0x7);
pub const VPORT_TABLE: Handle = Handle(0x8);
pub const BLOCK_RECORD_TABLE: Handle = Handle(0x9);
pub const ROOT: Handle = Handle(0xA);
pub const GROUPS: Handle = Handle(0xB);
pub const LAYOUTS: Handle = Handle(0xC);
pub const MODEL_RECORD: Handle = Handle(0xD);
pub const PAPER_RECORD: Handle = Handle(0xE);
pub const MODEL_BLOCK: Handle = Handle(0xF);
pub const MODEL_END: Handle = Handle(0x10);
pub const PAPER_BLOCK: Handle = Handle(0x11);
pub const PAPER_END: Handle = Handle(0x12);
pub const MODEL_LAYOUT: Handle = Handle(0x13);
pub const PAPER_LAYOUT: Handle = Handle(0x14);
pub const ACTIVE_VPORT: Handle = Handle(0x15);
pub const ACAD_APPID: Handle = Handle(0x16);
pub const STANDARD_DIMSTYLE: Handle = Handle(0x17);
pub const IMAGE_DICT: Handle = Handle(0x18);
pub const IMAGE_VARS: Handle = Handle(0x19);
pub const PLOT_STYLES: Handle = Handle(0x1A);
pub const PLOT_NORMAL: Handle = Handle(0x1B);
pub const FIRST_FREE: u32 = 0x30;
}
/// A number as DXF writes it: up to 6 decimals, no trailing zeros, 0 for anything not finite.
pub fn num(v: f64) -> String {
if !v.is_finite() {
return "0".into();
}
let s = format!("{v:.6}");
let s = s.trim_end_matches('0').trim_end_matches('.');
if s == "-0" || s.is_empty() { "0".into() } else { s.into() }
}
/// Text as a DXF string value: one line, `^` escaped, and before DXF 2007 (which reads UTF-8)
/// every character outside ASCII as `\U+XXXX`.
pub fn text(s: &str, utf8: bool) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'\r' | '\n' | '\t' => out.push(' '),
c if c.is_control() => {}
'^' => out.push_str("^ "),
c if !utf8 && !c.is_ascii() => match u16::try_from(u32::from(c)) {
Ok(u) => _ = write!(out, "\\U+{u:04X}"),
Err(_) => out.push('?'),
},
c => out.push(c),
}
}
out
}
/// Group-code/value pairs in the order they are written.
pub struct Codes {
pub v: Format,
out: String,
}
impl Codes {
pub fn new(v: Format) -> Self {
Self { v, out: String::new() }
}
pub fn into_string(self) -> String {
self.out
}
pub fn append(&mut self, other: &str) {
self.out.push_str(other);
}
/// A value written as it is (names and keywords the writer itself chose).
pub fn raw(&mut self, code: u16, value: &str) {
_ = writeln!(self.out, "{code:>3}\n{value}");
}
/// Text from the document, encoded for the version (see [`text`]).
pub fn str(&mut self, code: u16, value: &str) {
let t = text(value, self.v.utf8());
self.raw(code, &t);
}
pub fn int(&mut self, code: u16, value: i64) {
_ = writeln!(self.out, "{code:>3}\n{value}");
}
pub fn num(&mut self, code: u16, value: f64) {
_ = writeln!(self.out, "{code:>3}\n{}", num(value));
}
pub fn handle(&mut self, code: u16, h: Handle) {
_ = writeln!(self.out, "{code:>3}\n{:X}", h.0);
}
/// A 2D point at `code` (x), `code + 10` (y).
pub fn xy(&mut self, code: u16, x: f64, y: f64) {
self.num(code, x);
self.num(code + 10, y);
}
/// A 3D point at `code`, `code + 10`, `code + 20` (z = 0).
pub fn xyz(&mut self, code: u16, x: f64, y: f64) {
self.xy(code, x, y);
self.num(code + 20, 0.0);
}
/// The extrusion direction of planar entities: the z axis.
pub fn normal(&mut self) {
self.num(210, 0.0);
self.num(220, 0.0);
self.num(230, 1.0);
}
/// The start of an object: its type, and from R13 its handle, owner and first subclass.
fn object(&mut self, kind: &str, h: Handle, owner: Handle, subclass: &str) {
self.raw(0, kind);
if self.v.handles() {
self.handle(if kind == "DIMSTYLE" { 105 } else { 5 }, h);
self.handle(330, owner);
self.raw(100, subclass);
}
}
/// A table entry: the shared record subclass, then the table's own (from R13).
fn record(&mut self, kind: &str, h: Handle, table: Handle, subclass: &str) {
self.object(kind, h, table, "AcDbSymbolTableRecord");
if self.v.handles() {
self.raw(100, subclass);
}
}
fn section(&mut self, name: &str) {
self.raw(0, "SECTION");
self.raw(2, name);
}
fn end_section(&mut self) {
self.raw(0, "ENDSEC");
}
fn table(&mut self, name: &str, h: Handle, count: usize) {
self.raw(0, "TABLE");
self.raw(2, name);
if self.v.handles() {
self.handle(5, h);
self.raw(330, "0");
self.raw(100, "AcDbSymbolTable");
}
self.int(70, count as i64);
if name == "DIMSTYLE" && self.v.r2000() {
self.raw(100, "AcDbDimStyleTable");
}
}
fn end_table(&mut self) {
self.raw(0, "ENDTAB");
}
}
/// How an entity is drawn: its colour index, true colour, lineweight, linetype and transparency.
#[derive(Clone, Debug, Default)]
pub struct EntityStyle {
pub aci: u8,
/// `0xRRGGBB` (DXF 2004 and later).
pub rgb: Option<u32>,
/// Hundredths of a millimetre (DXF 2000 and later).
pub lineweight: Option<i32>,
pub linetype: Option<String>,
/// Opacity 0–255 (DXF 2004 and later; opaque is left out).
pub alpha: Option<u8>,
}
impl Codes {
/// The start of an entity of `kind` owned by `owner` (model space, or a polyline for its
/// vertices) on `layer` with its style, ending with `subclass` (none when empty).
pub fn entity(&mut self, kind: &str, h: Handle, owner: Handle, layer: &str, style: &EntityStyle, subclass: &str) {
self.object(kind, h, owner, "AcDbEntity");
self.str(8, layer);
if let Some(lt) = &style.linetype {
self.str(6, lt);
}
self.int(62, i64::from(style.aci));
if self.v.r2000()
&& let Some(lw) = style.lineweight
{
self.int(370, i64::from(lw));
}
if self.v.true_color() {
if let Some(rgb) = style.rgb {
self.int(420, i64::from(rgb));
}
if let Some(a) = style.alpha {
self.int(440, 0x0200_0000 | i64::from(a));
}
}
if self.v.handles() && !subclass.is_empty() {
self.raw(100, subclass);
}
}
}
/// A DXF layer.
#[derive(Clone, Debug)]
pub struct Layer {
pub name: String,
pub aci: u8,
/// Switched off (a hidden layer).
pub off: bool,
pub locked: bool,
/// Plotted (printable).
pub plot: bool,
}
/// A dash pattern: dash, gap, dash… lengths in drawing units.
#[derive(Clone, Debug)]
pub struct LineType {
pub name: String,
pub pattern: Vec<f64>,
}
/// A placed image file and the image entities that show it.
#[derive(Clone, Debug)]
pub struct ImageDef {
pub name: String,
pub size: (u32, u32),
pub handle: Handle,
/// `(reactor, image entity)` per image entity.
pub reactors: Vec<(Handle, Handle)>,
}
/// Everything around the entities, collected while they were written.
pub struct Drawing {
pub version: Format,
pub layers: Vec<Layer>,
pub linetypes: Vec<LineType>,
/// Text styles: (style name, font family).
pub styles: Vec<(String, String)>,
pub images: Vec<ImageDef>,
/// The entities' group codes.
pub entities: String,
/// `[x0, y0, x1, y1]` of everything written (drawing units).
pub extents: [f64; 4],
/// The export region (drawing units).
pub limits: [f64; 4],
/// `$INSUNITS`.
pub insunits: u8,
pub metric: bool,
/// The next free handle.
pub next: u32,
}
/// The text style every drawing has.
pub const STANDARD_STYLE: &str = "Standard";
impl Drawing {
/// The whole file.
pub fn write(self) -> String {
let v = self.version;
let mut c = Codes::new(v);
// Table entries get the handles after the entities'.
let mut next = self.next;
let mut take = |n: usize| {
let start = next;
next = next.saturating_add(u32::try_from(n).unwrap_or(u32::MAX));
start
};
let ltype_base = take(3 + self.linetypes.len());
let layer_base = take(1 + self.layers.len());
let style_base = take(1 + self.styles.len());
self.header(&mut c, next);
if v.handles() {
self.classes(&mut c);
}
c.section("TABLES");
self.vport(&mut c);
self.ltypes(&mut c, ltype_base);
self.layer_table(&mut c, layer_base);
self.style_table(&mut c, style_base);
if v.handles() {
for (name, h) in [("VIEW", fixed::VIEW_TABLE), ("UCS", fixed::UCS_TABLE)] {
c.table(name, h, 0);
c.end_table();
}
c.table("APPID", fixed::APPID_TABLE, 1);
c.record("APPID", fixed::ACAD_APPID, fixed::APPID_TABLE, "AcDbRegAppTableRecord");
c.raw(2, "ACAD");
c.int(70, 0);
c.end_table();
c.table("DIMSTYLE", fixed::DIMSTYLE_TABLE, 1);
c.record("DIMSTYLE", fixed::STANDARD_DIMSTYLE, fixed::DIMSTYLE_TABLE, "AcDbDimStyleTableRecord");
c.raw(2, "Standard");
c.int(70, 0);
c.end_table();
c.table("BLOCK_RECORD", fixed::BLOCK_RECORD_TABLE, 2);
for (name, h, layout) in
[("*Model_Space", fixed::MODEL_RECORD, fixed::MODEL_LAYOUT), ("*Paper_Space", fixed::PAPER_RECORD, fixed::PAPER_LAYOUT)]
{
c.record("BLOCK_RECORD", h, fixed::BLOCK_RECORD_TABLE, "AcDbBlockTableRecord");
c.raw(2, name);
if v.r2000() {
c.handle(340, layout);
c.int(70, 0);
c.int(280, 1);
c.int(281, 0);
}
}
c.end_table();
}
c.end_section();
if v.handles() {
c.section("BLOCKS");
for (name, record, begin, end) in [
("*Model_Space", fixed::MODEL_RECORD, fixed::MODEL_BLOCK, fixed::MODEL_END),
("*Paper_Space", fixed::PAPER_RECORD, fixed::PAPER_BLOCK, fixed::PAPER_END),
] {
c.object("BLOCK", begin, record, "AcDbEntity");
c.raw(8, "0");
c.raw(100, "AcDbBlockBegin");
c.raw(2, name);
c.int(70, 0);
c.xyz(10, 0.0, 0.0);
c.raw(3, name);
c.raw(1, "");
c.object("ENDBLK", end, record, "AcDbEntity");
c.raw(8, "0");
c.raw(100, "AcDbBlockEnd");
}
c.end_section();
}
c.section("ENTITIES");
c.append(&self.entities);
c.end_section();
if v.handles() {
self.objects(&mut c);
}
c.raw(0, "EOF");
c.into_string()
}
fn header(&self, c: &mut Codes, handseed: u32) {
let v = self.version;
let var = |c: &mut Codes, name: &str| c.raw(9, name);
c.section("HEADER");
var(c, "$ACADVER");
c.raw(1, v.acadver());
var(c, "$DWGCODEPAGE");
c.raw(3, "ANSI_1252");
var(c, "$INSBASE");
c.xyz(10, 0.0, 0.0);
let [x0, y0, x1, y1] = self.extents;
var(c, "$EXTMIN");
c.xyz(10, x0, y0);
var(c, "$EXTMAX");
c.xyz(10, x1, y1);
let [lx0, ly0, lx1, ly1] = self.limits;
var(c, "$LIMMIN");
c.xy(10, lx0, ly0);
var(c, "$LIMMAX");
c.xy(10, lx1, ly1);
if v.r2000() {
var(c, "$LWDISPLAY");
c.int(290, 1);
var(c, "$INSUNITS");
c.int(70, i64::from(self.insunits));
var(c, "$MEASUREMENT");
c.int(70, i64::from(self.metric));
}
if v.handles() {
var(c, "$HANDSEED");
c.handle(5, Handle(handseed));
}
c.end_section();
}
fn classes(&self, c: &mut Codes) {
let mut classes: Vec<(&str, &str, &str, i64, bool)> = vec![];
if self.version.r2000() {
classes.extend([
("ACDBDICTIONARYWDFLT", "AcDbDictionaryWithDefault", "ObjectDBX Classes", 0, false),
("ACDBPLACEHOLDER", "AcDbPlaceHolder", "ObjectDBX Classes", 0, false),
("LAYOUT", "AcDbLayout", "ObjectDBX Classes", 0, false),
]);
}
if !self.images.is_empty() {
classes.extend([
("RASTERVARIABLES", "AcDbRasterVariables", "ISM", 0, false),
("IMAGE", "AcDbRasterImage", "ISM", 2175, true),
("IMAGEDEF", "AcDbRasterImageDef", "ISM", 0, false),
("IMAGEDEF_REACTOR", "AcDbRasterImageDefReactor", "ISM", 1, false),
]);
}
c.section("CLASSES");
for (name, cpp, app, flags, entity) in classes {
c.raw(0, "CLASS");
c.raw(1, name);
c.raw(2, cpp);
c.raw(3, app);
c.int(90, flags);
if self.version.true_color() {
c.int(91, 0);
}
c.int(280, 0);
c.int(281, i64::from(entity));
}
c.end_section();
}
/// The active viewport, zoomed to the drawing's extents.
fn vport(&self, c: &mut Codes) {
let [x0, y0, x1, y1] = self.extents;
c.table("VPORT", fixed::VPORT_TABLE, 1);
c.record("VPORT", fixed::ACTIVE_VPORT, fixed::VPORT_TABLE, "AcDbViewportTableRecord");
c.raw(2, if self.version.handles() { "*Active" } else { "*ACTIVE" });
c.int(70, 0);
c.xy(10, 0.0, 0.0);
c.xy(11, 1.0, 1.0);
c.xy(12, (x0 + x1) / 2.0, (y0 + y1) / 2.0);
c.xy(13, 0.0, 0.0);
c.xy(14, 1.0, 1.0);
c.xy(15, 10.0, 10.0);
// The view direction looks down the z axis: (0, 0, 1), each code once.
c.xy(16, 0.0, 0.0);
c.num(36, 1.0);
c.xyz(17, 0.0, 0.0);
let (w, h) = ((x1 - x0).abs(), (y1 - y0).abs());
// The view's height, with a margin; its width follows from the aspect ratio.
c.num(40, (h.max(w / 1.5) * 1.1).max(1.0));
c.num(41, 1.5);
c.num(42, 50.0);
c.num(43, 0.0);
c.num(44, 0.0);
c.num(50, 0.0);
c.num(51, 0.0);
for (code, value) in [(71, 0), (72, 1000), (73, 1), (74, 3), (75, 0), (76, 0), (77, 0), (78, 0)] {
c.int(code, value);
}
if self.version.r2000() {
c.int(281, 0);
c.int(65, 0);
c.num(146, 0.0);
}
c.end_table();
}
fn ltypes(&self, c: &mut Codes, base: u32) {
c.table("LTYPE", fixed::LTYPE_TABLE, 3 + self.linetypes.len());
let solid = [("ByBlock", ""), ("ByLayer", ""), ("Continuous", "Solid line")];
let rows = solid.iter().map(|(n, d)| (*n, *d, &[][..])).chain(self.linetypes.iter().map(|l| (l.name.as_str(), "", l.pattern.as_slice())));
for (i, (name, desc, pattern)) in rows.enumerate() {
c.record("LTYPE", Handle(base.saturating_add(i as u32)), fixed::LTYPE_TABLE, "AcDbLinetypeTableRecord");
c.str(2, name);
c.int(70, 0);
c.str(3, if pattern.is_empty() { desc } else { "Dashed" });
c.int(72, 65);
c.int(73, pattern.len() as i64);
c.num(40, pattern.iter().map(|v| v.abs()).sum());
for v in pattern {
c.num(49, *v);
if self.version.handles() {
c.int(74, 0);
}
}
}
c.end_table();
}
fn layer_table(&self, c: &mut Codes, base: u32) {
let zero = Layer { name: "0".into(), aci: 7, off: false, locked: false, plot: true };
c.table("LAYER", fixed::LAYER_TABLE, 1 + self.layers.len());
for (i, l) in std::iter::once(&zero).chain(&self.layers).enumerate() {
c.record("LAYER", Handle(base.saturating_add(i as u32)), fixed::LAYER_TABLE, "AcDbLayerTableRecord");
c.str(2, &l.name);
c.int(70, if l.locked { 4 } else { 0 });
// A negative colour switches the layer off.
let aci = i64::from(l.aci.max(1));
c.int(62, if l.off { -aci } else { aci });
c.raw(6, "Continuous");
if self.version.r2000() {
c.int(290, i64::from(l.plot));
c.int(370, -3);
c.handle(390, fixed::PLOT_NORMAL);
}
}
c.end_table();
}
fn style_table(&self, c: &mut Codes, base: u32) {
c.table("STYLE", fixed::STYLE_TABLE, 1 + self.styles.len());
let standard = (STANDARD_STYLE.to_string(), String::new());
for (i, (name, family)) in std::iter::once(&standard).chain(&self.styles).enumerate() {
c.record("STYLE", Handle(base.saturating_add(i as u32)), fixed::STYLE_TABLE, "AcDbTextStyleTableRecord");
c.str(2, name);
c.int(70, 0);
c.num(40, 0.0);
c.num(41, 1.0);
c.num(50, 0.0);
c.int(71, 0);
c.num(42, 2.5);
// The font file; from 2000 the family name too, which is how CAD apps find a TrueType font.
if family.is_empty() {
c.raw(3, "txt");
} else {
c.str(3, &format!("{family}.ttf"));
}
c.raw(4, "");
if self.version.r2000() && !family.is_empty() {
c.raw(1001, "ACAD");
c.str(1000, family);
c.int(1071, 0);
}
}
c.end_table();
}
fn objects(&self, c: &mut Codes) {
let v = self.version;
let images = !self.images.is_empty();
c.section("OBJECTS");
let dict = |c: &mut Codes, h: Handle, owner: Handle, entries: &[(&str, Handle)]| {
c.object("DICTIONARY", h, owner, "AcDbDictionary");
c.int(281, 1);
for (name, e) in entries {
c.str(3, name);
c.handle(350, *e);
}
};
let mut root = vec![("ACAD_GROUP", fixed::GROUPS)];
if images {
root.extend([("ACAD_IMAGE_DICT", fixed::IMAGE_DICT), ("ACAD_IMAGE_VARS", fixed::IMAGE_VARS)]);
}
if v.r2000() {
root.extend([("ACAD_LAYOUT", fixed::LAYOUTS), ("ACAD_PLOTSTYLENAME", fixed::PLOT_STYLES)]);
}
dict(c, fixed::ROOT, Handle(0), &root);
dict(c, fixed::GROUPS, fixed::ROOT, &[]);
if v.r2000() {
dict(c, fixed::LAYOUTS, fixed::ROOT, &[("Layout1", fixed::PAPER_LAYOUT), ("Model", fixed::MODEL_LAYOUT)]);
c.object("ACDBDICTIONARYWDFLT", fixed::PLOT_STYLES, fixed::ROOT, "AcDbDictionary");
c.int(281, 1);
c.raw(3, "Normal");
c.handle(350, fixed::PLOT_NORMAL);
c.raw(100, "AcDbDictionaryWithDefault");
c.handle(340, fixed::PLOT_NORMAL);
// A placeholder has no data and no subclass marker of its own.
c.raw(0, "ACDBPLACEHOLDER");
c.handle(5, fixed::PLOT_NORMAL);
c.handle(330, fixed::PLOT_STYLES);
self.layout(c, fixed::MODEL_LAYOUT, "Model", 0, fixed::MODEL_RECORD);
self.layout(c, fixed::PAPER_LAYOUT, "Layout1", 1, fixed::PAPER_RECORD);
}
if images {
c.object("RASTERVARIABLES", fixed::IMAGE_VARS, fixed::ROOT, "AcDbRasterVariables");
c.int(90, 0);
c.int(70, 0);
c.int(71, 1);
c.int(72, 0);
let keys: Vec<String> = self.images.iter().map(|i| format!("IMAGE_{:X}", i.handle.0)).collect();
let entries: Vec<(&str, Handle)> = keys.iter().zip(&self.images).map(|(key, image)| (key.as_str(), image.handle)).collect();
dict(c, fixed::IMAGE_DICT, fixed::ROOT, &entries);
for img in &self.images {
c.raw(0, "IMAGEDEF");
c.handle(5, img.handle);
c.raw(102, "{ACAD_REACTORS");
c.handle(330, fixed::IMAGE_DICT);
for (reactor, _) in &img.reactors {
c.handle(330, *reactor);
}
c.raw(102, "}");
c.handle(330, fixed::IMAGE_DICT);
c.raw(100, "AcDbRasterImageDef");
c.int(90, 0);
c.str(1, &img.name);
c.xy(10, f64::from(img.size.0), f64::from(img.size.1));
c.xy(11, 1.0, 1.0);
c.int(280, 1);
c.int(281, 0);
for (reactor, image) in &img.reactors {
c.object("IMAGEDEF_REACTOR", *reactor, *image, "AcDbRasterImageDefReactor");
c.int(90, 2);
c.handle(330, *image);
}
}
}
c.end_section();
}
/// A layout (DXF 2000 and later): its plot settings, then the layout itself.
fn layout(&self, c: &mut Codes, h: Handle, name: &str, tab: i64, record: Handle) {
let [x0, y0, x1, y1] = self.extents;
c.object("LAYOUT", h, fixed::LAYOUTS, "AcDbPlotSettings");
c.raw(1, "");
c.raw(2, "none_device");
c.raw(4, "");
c.raw(6, "");
for code in 40..=49 {
c.num(code, 0.0);
}
c.num(140, 0.0);
c.num(141, 0.0);
c.num(142, 1.0);
c.num(143, 1.0);
// Plot flags; 1024 marks the model space layout.
c.int(70, if record == fixed::MODEL_RECORD { 688 | 1024 } else { 688 });
c.int(72, 1);
c.int(73, 0);
c.int(74, 5);
c.raw(7, "");
c.int(75, 16);
if self.version.true_color() {
// Shade plot mode (as displayed), resolution level (normal) and custom DPI.
c.int(76, 0);
c.int(77, 2);
c.int(78, 300);
}
c.num(147, 1.0);
// The paper image origin is 148 (x) and 149 (y), not a point at 148 and 158.
c.num(148, 0.0);
c.num(149, 0.0);
c.raw(100, "AcDbLayout");
c.raw(1, name);
c.int(70, 1);
c.int(71, tab);
let [lx0, ly0, lx1, ly1] = self.limits;
c.xy(10, lx0, ly0);
c.xy(11, lx1, ly1);
c.xyz(12, 0.0, 0.0);
c.xyz(14, x0, y0);
c.xyz(15, x1, y1);
c.num(146, 0.0);
c.xyz(13, 0.0, 0.0);
c.xyz(16, 1.0, 0.0);
c.xyz(17, 0.0, 1.0);
c.int(76, 1);
c.handle(330, record);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn numbers_and_text_are_written_plainly() {
assert_eq!(num(1.5), "1.5");
assert_eq!(num(-0.0000001), "0");
assert_eq!(num(2.0), "2");
assert_eq!(num(f64::NAN), "0");
assert_eq!(text("a^b\nc", true), "a^ b c");
assert_eq!(text("Größe 😀", false), "Gr\\U+00F6\\U+00DFe ?");
assert_eq!(text("Größe", true), "Größe");
}
/// A line from (0, 0) to (10, 20) on layer `Layer1`, in the style scene.rs writes entities.
fn line_entities(v: Format) -> String {
let mut e = Codes::new(v);
let style = EntityStyle { aci: 1, rgb: Some(0xFF0000), lineweight: Some(25), linetype: None, alpha: None };
e.entity("LINE", Handle(fixed::FIRST_FREE), fixed::MODEL_RECORD, "Layer1", &style, "AcDbLine");
e.xyz(10, 0.0, 0.0);
e.xyz(11, 10.0, 20.0);
e.into_string()
}
fn sample(v: Format, with_line: bool) -> Drawing {
Drawing {
version: v,
layers: vec![Layer { name: "Layer1".into(), aci: 1, off: false, locked: false, plot: true }],
linetypes: vec![],
styles: vec![],
images: vec![],
entities: if with_line { line_entities(v) } else { String::new() },
extents: [0.0, 0.0, 200.0, 100.0],
limits: [0.0, 0.0, 200.0, 100.0],
insunits: 4,
metric: true,
next: fixed::FIRST_FREE + if with_line { 1 } else { 0 },
}
}
fn assert_golden(name: &str, got: &str) {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data").join(name);
if std::env::var_os("WRITE_DXF_GOLDENS").is_some() {
std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
std::fs::write(&path, got).expect("write golden");
return;
}
let expected = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
assert_eq!(got, expected, "bytes changed versus {}", path.display());
}
use std::path::PathBuf;
#[test]
fn empty_and_line_drawings_match_goldens() {
for (name, v, line) in [
("drawing_empty_r12.dxf", Format::R12, false),
("drawing_empty_r2018.dxf", Format::R2018, false),
("drawing_line_r12.dxf", Format::R12, true),
("drawing_line_r2000.dxf", Format::R2000, true),
("drawing_line_r2018.dxf", Format::R2018, true),
] {
assert_golden(name, &sample(v, line).write());
}
}
#[test]
fn entity_preserves_byblock_colour() {
for v in Format::ALL {
let mut c = Codes::new(v);
c.entity("LINE", Handle(fixed::FIRST_FREE), fixed::MODEL_RECORD, "0", &EntityStyle::default(), "AcDbLine");
assert!(c.into_string().contains(" 62\n0\n"), "{v:?}");
}
}
#[test]
fn image_dictionary_keys_are_independent_of_filenames() {
let mut drawing = sample(Format::R2018, false);
for (index, name) in [r"C:\images\scan,1.png", "scan,1.png"].into_iter().enumerate() {
drawing.images.push(ImageDef { name: name.into(), size: (10, 20), handle: Handle(fixed::FIRST_FREE + index as u32), reactors: vec![] });
}
drawing.next = fixed::FIRST_FREE + 2;
let images = drawing.images.clone();
let output = drawing.write();
for image in &images {
assert!(output.contains(&format!(" 3\nIMAGE_{:X}\n350\n{:X}\n", image.handle.0, image.handle.0)));
assert!(output.contains(&format!(" 1\n{}\n", image.name)));
assert!(!output.contains(&format!(" 3\n{}\n", image.name)));
}
}
#[test]
fn every_format_writes_its_acadver_and_ends_at_eof() {
for v in Format::ALL {
let s = sample(v, true).write();
assert!(s.contains(&format!(" 1\n{}\n", v.acadver())), "{v:?}");
assert!(s.ends_with(" 0\nEOF\n"), "{v:?}");
if v.handles() {
assert!(s.contains(" 0\nSECTION\n 2\nCLASSES\n"), "{v:?}");
} else {
assert!(!s.contains("\nCLASSES\n"), "{v:?}");
}
}
}
}
+5 -1
View File
@@ -3,14 +3,18 @@
//! DXF is a sequence of (group code, value) pairs. This crate parses ASCII and binary DXF into a
//! flat list of [`Tag`]s, splits them into sections and entities, and writes tags back. [`model`]
//! reads the tags as a drawing (header variables, tables, blocks, entities, objects); mapping
//! that onto an application's own document is left to the caller. Written from Autodesk's
//! that onto an application's own document is left to the caller. [`drawing`] writes an ASCII
//! file around a pre-built `ENTITIES` section (VectorCraft's writer). Written from Autodesk's
//! published DXF Reference; depends on no other Craft crate. Moved from CADCraft's `cadcraft-dxf`.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
#![forbid(unsafe_code)]
use std::io::Write as _;
pub mod drawing;
pub mod model;
pub mod mtext;
pub mod mtext_tokens;
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));
}
}
}
+132
View File
@@ -0,0 +1,132 @@
//! Streaming lexical tokens for MTEXT inline markup.
//!
//! Apply [`crate::mtext::unescape_codes`] first when Unicode/MIF escapes should become literal
//! characters. Tokens borrow their source and scanning allocates no memory. The consumer owns
//! formatting, line layout, paragraph policy and how unknown commands are displayed.
/// How a stacked-text argument finds its closing semicolon.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StackMode {
/// The first semicolon ends the stack, as plain-text extraction historically did.
Plain,
/// A backslash quotes the next character, as the structured layout parser does.
Structured,
}
/// One lexical unit. Unknown commands retain their letter so the caller chooses compatibility.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Token<'a> {
Char(char),
OpenGroup,
CloseGroup,
Control(char),
Argument(char, &'a str),
Stack(&'a str),
Unknown(char),
}
/// A bounded-memory walk of UTF-8 MTEXT markup.
pub struct Tokens<'a> {
source: &'a str,
offset: usize,
stack_mode: StackMode,
}
impl<'a> Tokens<'a> {
pub fn new(source: &'a str, stack_mode: StackMode) -> Self {
Self { source, offset: 0, stack_mode }
}
fn next_char(&mut self) -> Option<char> {
let c = self.source.get(self.offset..)?.chars().next()?;
self.offset += c.len_utf8();
Some(c)
}
fn argument(&mut self, structured: bool) -> &'a str {
let start = self.offset;
let mut end = start;
while let Some(c) = self.next_char() {
if c == ';' {
return self.source.get(start..end).unwrap_or_default();
}
if structured && c == '\\' {
let _ = self.next_char();
}
end = self.offset;
}
self.source.get(start..end).unwrap_or_default()
}
}
impl<'a> Iterator for Tokens<'a> {
type Item = Token<'a>;
fn next(&mut self) -> Option<Self::Item> {
let c = self.next_char()?;
Some(match c {
'{' => Token::OpenGroup,
'}' => Token::CloseGroup,
'\\' => {
let code = self.next_char()?;
match code {
'S' => Token::Stack(self.argument(self.stack_mode == StackMode::Structured)),
c if crate::mtext::takes_arg(c) => Token::Argument(c, self.argument(false)),
c @ ('P' | 'N' | '~' | '\\' | '{' | '}' | 'L' | 'l' | 'O' | 'o' | 'K' | 'k') => Token::Control(c),
c => Token::Unknown(c),
}
}
c => Token::Char(c),
})
}
}
/// Split a structured stack into top, bottom and separator, unquoting escaped characters.
pub fn split_stack(raw: &str) -> (String, String, Option<char>) {
let (mut top, mut bottom, mut separator) = (String::new(), String::new(), None);
let mut chars = raw.chars();
while let Some(c) = chars.next() {
if c == '\\' {
if let Some(c) = chars.next() {
if separator.is_none() {
top.push(c);
} else {
bottom.push(c);
}
}
} else if separator.is_none() && matches!(c, '/' | '#' | '^') {
separator = Some(c);
} else if separator.is_none() {
top.push(c);
} else {
bottom.push(c);
}
}
(top, bottom, separator)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tokens_borrow_text_and_preserve_unknown_commands() {
let got: Vec<_> = Tokens::new(r"A\Z\P{\H2x;x}\S1/2;", StackMode::Structured).collect();
assert_eq!(
got,
[
Token::Char('A'),
Token::Unknown('Z'),
Token::Control('P'),
Token::OpenGroup,
Token::Argument('H', "2x"),
Token::Char('x'),
Token::CloseGroup,
Token::Stack("1/2")
]
);
assert_eq!(split_stack("1/2"), ("1".into(), "2".into(), Some('/')));
}
#[test]
fn stacked_text_uses_the_requested_terminator_rules() {
let text = r"\Sa\;b/c;X";
assert_eq!(Tokens::new(text, StackMode::Plain).next(), Some(Token::Stack(r"a\")));
assert_eq!(Tokens::new(text, StackMode::Structured).next(), Some(Token::Stack(r"a\;b/c")));
assert_eq!(split_stack(r"a\;b/c"), ("a;b".into(), "c".into(), Some('/')));
}
#[test]
fn missing_terminator_and_trailing_backslash_are_bounded() {
assert_eq!(Tokens::new("\\H2x", StackMode::Structured).collect::<Vec<_>>(), [Token::Argument('H', "2x")]);
assert_eq!(Tokens::new("a\\", StackMode::Structured).collect::<Vec<_>>(), [Token::Char('a')]);
}
}
+250
View File
@@ -0,0 +1,250 @@
0
SECTION
2
HEADER
9
$ACADVER
1
AC1009
9
$DWGCODEPAGE
3
ANSI_1252
9
$INSBASE
10
0
20
0
30
0
9
$EXTMIN
10
0
20
0
30
0
9
$EXTMAX
10
200
20
100
30
0
9
$LIMMIN
10
0
20
0
9
$LIMMAX
10
200
20
100
0
ENDSEC
0
SECTION
2
TABLES
0
TABLE
2
VPORT
70
1
0
VPORT
2
*ACTIVE
70
0
10
0
20
0
11
1
21
1
12
100
22
50
13
0
23
0
14
1
24
1
15
10
25
10
16
0
26
0
36
1
17
0
27
0
37
0
40
146.666667
41
1.5
42
50
43
0
44
0
50
0
51
0
71
0
72
1000
73
1
74
3
75
0
76
0
77
0
78
0
0
ENDTAB
0
TABLE
2
LTYPE
70
3
0
LTYPE
2
ByBlock
70
0
3
72
65
73
0
40
0
0
LTYPE
2
ByLayer
70
0
3
72
65
73
0
40
0
0
LTYPE
2
Continuous
70
0
3
Solid line
72
65
73
0
40
0
0
ENDTAB
0
TABLE
2
LAYER
70
2
0
LAYER
2
0
70
0
62
7
6
Continuous
0
LAYER
2
Layer1
70
0
62
1
6
Continuous
0
ENDTAB
0
TABLE
2
STYLE
70
1
0
STYLE
2
Standard
70
0
40
0
41
1
50
0
71
0
42
2.5
3
txt
4
0
ENDTAB
0
ENDSEC
0
SECTION
2
ENTITIES
0
ENDSEC
0
EOF
@@ -0,0 +1,972 @@
0
SECTION
2
HEADER
9
$ACADVER
1
AC1032
9
$DWGCODEPAGE
3
ANSI_1252
9
$INSBASE
10
0
20
0
30
0
9
$EXTMIN
10
0
20
0
30
0
9
$EXTMAX
10
200
20
100
30
0
9
$LIMMIN
10
0
20
0
9
$LIMMAX
10
200
20
100
9
$LWDISPLAY
290
1
9
$INSUNITS
70
4
9
$MEASUREMENT
70
1
9
$HANDSEED
5
36
0
ENDSEC
0
SECTION
2
CLASSES
0
CLASS
1
ACDBDICTIONARYWDFLT
2
AcDbDictionaryWithDefault
3
ObjectDBX Classes
90
0
91
0
280
0
281
0
0
CLASS
1
ACDBPLACEHOLDER
2
AcDbPlaceHolder
3
ObjectDBX Classes
90
0
91
0
280
0
281
0
0
CLASS
1
LAYOUT
2
AcDbLayout
3
ObjectDBX Classes
90
0
91
0
280
0
281
0
0
ENDSEC
0
SECTION
2
TABLES
0
TABLE
2
VPORT
5
8
330
0
100
AcDbSymbolTable
70
1
0
VPORT
5
15
330
8
100
AcDbSymbolTableRecord
100
AcDbViewportTableRecord
2
*Active
70
0
10
0
20
0
11
1
21
1
12
100
22
50
13
0
23
0
14
1
24
1
15
10
25
10
16
0
26
0
36
1
17
0
27
0
37
0
40
146.666667
41
1.5
42
50
43
0
44
0
50
0
51
0
71
0
72
1000
73
1
74
3
75
0
76
0
77
0
78
0
281
0
65
0
146
0
0
ENDTAB
0
TABLE
2
LTYPE
5
2
330
0
100
AcDbSymbolTable
70
3
0
LTYPE
5
30
330
2
100
AcDbSymbolTableRecord
100
AcDbLinetypeTableRecord
2
ByBlock
70
0
3
72
65
73
0
40
0
0
LTYPE
5
31
330
2
100
AcDbSymbolTableRecord
100
AcDbLinetypeTableRecord
2
ByLayer
70
0
3
72
65
73
0
40
0
0
LTYPE
5
32
330
2
100
AcDbSymbolTableRecord
100
AcDbLinetypeTableRecord
2
Continuous
70
0
3
Solid line
72
65
73
0
40
0
0
ENDTAB
0
TABLE
2
LAYER
5
1
330
0
100
AcDbSymbolTable
70
2
0
LAYER
5
33
330
1
100
AcDbSymbolTableRecord
100
AcDbLayerTableRecord
2
0
70
0
62
7
6
Continuous
290
1
370
-3
390
1B
0
LAYER
5
34
330
1
100
AcDbSymbolTableRecord
100
AcDbLayerTableRecord
2
Layer1
70
0
62
1
6
Continuous
290
1
370
-3
390
1B
0
ENDTAB
0
TABLE
2
STYLE
5
5
330
0
100
AcDbSymbolTable
70
1
0
STYLE
5
35
330
5
100
AcDbSymbolTableRecord
100
AcDbTextStyleTableRecord
2
Standard
70
0
40
0
41
1
50
0
71
0
42
2.5
3
txt
4
0
ENDTAB
0
TABLE
2
VIEW
5
7
330
0
100
AcDbSymbolTable
70
0
0
ENDTAB
0
TABLE
2
UCS
5
6
330
0
100
AcDbSymbolTable
70
0
0
ENDTAB
0
TABLE
2
APPID
5
3
330
0
100
AcDbSymbolTable
70
1
0
APPID
5
16
330
3
100
AcDbSymbolTableRecord
100
AcDbRegAppTableRecord
2
ACAD
70
0
0
ENDTAB
0
TABLE
2
DIMSTYLE
5
4
330
0
100
AcDbSymbolTable
70
1
100
AcDbDimStyleTable
0
DIMSTYLE
105
17
330
4
100
AcDbSymbolTableRecord
100
AcDbDimStyleTableRecord
2
Standard
70
0
0
ENDTAB
0
TABLE
2
BLOCK_RECORD
5
9
330
0
100
AcDbSymbolTable
70
2
0
BLOCK_RECORD
5
D
330
9
100
AcDbSymbolTableRecord
100
AcDbBlockTableRecord
2
*Model_Space
340
13
70
0
280
1
281
0
0
BLOCK_RECORD
5
E
330
9
100
AcDbSymbolTableRecord
100
AcDbBlockTableRecord
2
*Paper_Space
340
14
70
0
280
1
281
0
0
ENDTAB
0
ENDSEC
0
SECTION
2
BLOCKS
0
BLOCK
5
F
330
D
100
AcDbEntity
8
0
100
AcDbBlockBegin
2
*Model_Space
70
0
10
0
20
0
30
0
3
*Model_Space
1
0
ENDBLK
5
10
330
D
100
AcDbEntity
8
0
100
AcDbBlockEnd
0
BLOCK
5
11
330
E
100
AcDbEntity
8
0
100
AcDbBlockBegin
2
*Paper_Space
70
0
10
0
20
0
30
0
3
*Paper_Space
1
0
ENDBLK
5
12
330
E
100
AcDbEntity
8
0
100
AcDbBlockEnd
0
ENDSEC
0
SECTION
2
ENTITIES
0
ENDSEC
0
SECTION
2
OBJECTS
0
DICTIONARY
5
A
330
0
100
AcDbDictionary
281
1
3
ACAD_GROUP
350
B
3
ACAD_LAYOUT
350
C
3
ACAD_PLOTSTYLENAME
350
1A
0
DICTIONARY
5
B
330
A
100
AcDbDictionary
281
1
0
DICTIONARY
5
C
330
A
100
AcDbDictionary
281
1
3
Layout1
350
14
3
Model
350
13
0
ACDBDICTIONARYWDFLT
5
1A
330
A
100
AcDbDictionary
281
1
3
Normal
350
1B
100
AcDbDictionaryWithDefault
340
1B
0
ACDBPLACEHOLDER
5
1B
330
1A
0
LAYOUT
5
13
330
C
100
AcDbPlotSettings
1
2
none_device
4
6
40
0
41
0
42
0
43
0
44
0
45
0
46
0
47
0
48
0
49
0
140
0
141
0
142
1
143
1
70
1712
72
1
73
0
74
5
7
75
16
76
0
77
2
78
300
147
1
148
0
149
0
100
AcDbLayout
1
Model
70
1
71
0
10
0
20
0
11
200
21
100
12
0
22
0
32
0
14
0
24
0
34
0
15
200
25
100
35
0
146
0
13
0
23
0
33
0
16
1
26
0
36
0
17
0
27
1
37
0
76
1
330
D
0
LAYOUT
5
14
330
C
100
AcDbPlotSettings
1
2
none_device
4
6
40
0
41
0
42
0
43
0
44
0
45
0
46
0
47
0
48
0
49
0
140
0
141
0
142
1
143
1
70
688
72
1
73
0
74
5
7
75
16
76
0
77
2
78
300
147
1
148
0
149
0
100
AcDbLayout
1
Layout1
70
1
71
1
10
0
20
0
11
200
21
100
12
0
22
0
32
0
14
0
24
0
34
0
15
200
25
100
35
0
146
0
13
0
23
0
33
0
16
1
26
0
36
0
17
0
27
1
37
0
76
1
330
E
0
ENDSEC
0
EOF
+268
View File
@@ -0,0 +1,268 @@
0
SECTION
2
HEADER
9
$ACADVER
1
AC1009
9
$DWGCODEPAGE
3
ANSI_1252
9
$INSBASE
10
0
20
0
30
0
9
$EXTMIN
10
0
20
0
30
0
9
$EXTMAX
10
200
20
100
30
0
9
$LIMMIN
10
0
20
0
9
$LIMMAX
10
200
20
100
0
ENDSEC
0
SECTION
2
TABLES
0
TABLE
2
VPORT
70
1
0
VPORT
2
*ACTIVE
70
0
10
0
20
0
11
1
21
1
12
100
22
50
13
0
23
0
14
1
24
1
15
10
25
10
16
0
26
0
36
1
17
0
27
0
37
0
40
146.666667
41
1.5
42
50
43
0
44
0
50
0
51
0
71
0
72
1000
73
1
74
3
75
0
76
0
77
0
78
0
0
ENDTAB
0
TABLE
2
LTYPE
70
3
0
LTYPE
2
ByBlock
70
0
3
72
65
73
0
40
0
0
LTYPE
2
ByLayer
70
0
3
72
65
73
0
40
0
0
LTYPE
2
Continuous
70
0
3
Solid line
72
65
73
0
40
0
0
ENDTAB
0
TABLE
2
LAYER
70
2
0
LAYER
2
0
70
0
62
7
6
Continuous
0
LAYER
2
Layer1
70
0
62
1
6
Continuous
0
ENDTAB
0
TABLE
2
STYLE
70
1
0
STYLE
2
Standard
70
0
40
0
41
1
50
0
71
0
42
2.5
3
txt
4
0
ENDTAB
0
ENDSEC
0
SECTION
2
ENTITIES
0
LINE
8
Layer1
62
1
10
0
20
0
30
0
11
10
21
20
31
0
0
ENDSEC
0
EOF
@@ -0,0 +1,982 @@
0
SECTION
2
HEADER
9
$ACADVER
1
AC1015
9
$DWGCODEPAGE
3
ANSI_1252
9
$INSBASE
10
0
20
0
30
0
9
$EXTMIN
10
0
20
0
30
0
9
$EXTMAX
10
200
20
100
30
0
9
$LIMMIN
10
0
20
0
9
$LIMMAX
10
200
20
100
9
$LWDISPLAY
290
1
9
$INSUNITS
70
4
9
$MEASUREMENT
70
1
9
$HANDSEED
5
37
0
ENDSEC
0
SECTION
2
CLASSES
0
CLASS
1
ACDBDICTIONARYWDFLT
2
AcDbDictionaryWithDefault
3
ObjectDBX Classes
90
0
280
0
281
0
0
CLASS
1
ACDBPLACEHOLDER
2
AcDbPlaceHolder
3
ObjectDBX Classes
90
0
280
0
281
0
0
CLASS
1
LAYOUT
2
AcDbLayout
3
ObjectDBX Classes
90
0
280
0
281
0
0
ENDSEC
0
SECTION
2
TABLES
0
TABLE
2
VPORT
5
8
330
0
100
AcDbSymbolTable
70
1
0
VPORT
5
15
330
8
100
AcDbSymbolTableRecord
100
AcDbViewportTableRecord
2
*Active
70
0
10
0
20
0
11
1
21
1
12
100
22
50
13
0
23
0
14
1
24
1
15
10
25
10
16
0
26
0
36
1
17
0
27
0
37
0
40
146.666667
41
1.5
42
50
43
0
44
0
50
0
51
0
71
0
72
1000
73
1
74
3
75
0
76
0
77
0
78
0
281
0
65
0
146
0
0
ENDTAB
0
TABLE
2
LTYPE
5
2
330
0
100
AcDbSymbolTable
70
3
0
LTYPE
5
31
330
2
100
AcDbSymbolTableRecord
100
AcDbLinetypeTableRecord
2
ByBlock
70
0
3
72
65
73
0
40
0
0
LTYPE
5
32
330
2
100
AcDbSymbolTableRecord
100
AcDbLinetypeTableRecord
2
ByLayer
70
0
3
72
65
73
0
40
0
0
LTYPE
5
33
330
2
100
AcDbSymbolTableRecord
100
AcDbLinetypeTableRecord
2
Continuous
70
0
3
Solid line
72
65
73
0
40
0
0
ENDTAB
0
TABLE
2
LAYER
5
1
330
0
100
AcDbSymbolTable
70
2
0
LAYER
5
34
330
1
100
AcDbSymbolTableRecord
100
AcDbLayerTableRecord
2
0
70
0
62
7
6
Continuous
290
1
370
-3
390
1B
0
LAYER
5
35
330
1
100
AcDbSymbolTableRecord
100
AcDbLayerTableRecord
2
Layer1
70
0
62
1
6
Continuous
290
1
370
-3
390
1B
0
ENDTAB
0
TABLE
2
STYLE
5
5
330
0
100
AcDbSymbolTable
70
1
0
STYLE
5
36
330
5
100
AcDbSymbolTableRecord
100
AcDbTextStyleTableRecord
2
Standard
70
0
40
0
41
1
50
0
71
0
42
2.5
3
txt
4
0
ENDTAB
0
TABLE
2
VIEW
5
7
330
0
100
AcDbSymbolTable
70
0
0
ENDTAB
0
TABLE
2
UCS
5
6
330
0
100
AcDbSymbolTable
70
0
0
ENDTAB
0
TABLE
2
APPID
5
3
330
0
100
AcDbSymbolTable
70
1
0
APPID
5
16
330
3
100
AcDbSymbolTableRecord
100
AcDbRegAppTableRecord
2
ACAD
70
0
0
ENDTAB
0
TABLE
2
DIMSTYLE
5
4
330
0
100
AcDbSymbolTable
70
1
100
AcDbDimStyleTable
0
DIMSTYLE
105
17
330
4
100
AcDbSymbolTableRecord
100
AcDbDimStyleTableRecord
2
Standard
70
0
0
ENDTAB
0
TABLE
2
BLOCK_RECORD
5
9
330
0
100
AcDbSymbolTable
70
2
0
BLOCK_RECORD
5
D
330
9
100
AcDbSymbolTableRecord
100
AcDbBlockTableRecord
2
*Model_Space
340
13
70
0
280
1
281
0
0
BLOCK_RECORD
5
E
330
9
100
AcDbSymbolTableRecord
100
AcDbBlockTableRecord
2
*Paper_Space
340
14
70
0
280
1
281
0
0
ENDTAB
0
ENDSEC
0
SECTION
2
BLOCKS
0
BLOCK
5
F
330
D
100
AcDbEntity
8
0
100
AcDbBlockBegin
2
*Model_Space
70
0
10
0
20
0
30
0
3
*Model_Space
1
0
ENDBLK
5
10
330
D
100
AcDbEntity
8
0
100
AcDbBlockEnd
0
BLOCK
5
11
330
E
100
AcDbEntity
8
0
100
AcDbBlockBegin
2
*Paper_Space
70
0
10
0
20
0
30
0
3
*Paper_Space
1
0
ENDBLK
5
12
330
E
100
AcDbEntity
8
0
100
AcDbBlockEnd
0
ENDSEC
0
SECTION
2
ENTITIES
0
LINE
5
30
330
D
100
AcDbEntity
8
Layer1
62
1
370
25
100
AcDbLine
10
0
20
0
30
0
11
10
21
20
31
0
0
ENDSEC
0
SECTION
2
OBJECTS
0
DICTIONARY
5
A
330
0
100
AcDbDictionary
281
1
3
ACAD_GROUP
350
B
3
ACAD_LAYOUT
350
C
3
ACAD_PLOTSTYLENAME
350
1A
0
DICTIONARY
5
B
330
A
100
AcDbDictionary
281
1
0
DICTIONARY
5
C
330
A
100
AcDbDictionary
281
1
3
Layout1
350
14
3
Model
350
13
0
ACDBDICTIONARYWDFLT
5
1A
330
A
100
AcDbDictionary
281
1
3
Normal
350
1B
100
AcDbDictionaryWithDefault
340
1B
0
ACDBPLACEHOLDER
5
1B
330
1A
0
LAYOUT
5
13
330
C
100
AcDbPlotSettings
1
2
none_device
4
6
40
0
41
0
42
0
43
0
44
0
45
0
46
0
47
0
48
0
49
0
140
0
141
0
142
1
143
1
70
1712
72
1
73
0
74
5
7
75
16
147
1
148
0
149
0
100
AcDbLayout
1
Model
70
1
71
0
10
0
20
0
11
200
21
100
12
0
22
0
32
0
14
0
24
0
34
0
15
200
25
100
35
0
146
0
13
0
23
0
33
0
16
1
26
0
36
0
17
0
27
1
37
0
76
1
330
D
0
LAYOUT
5
14
330
C
100
AcDbPlotSettings
1
2
none_device
4
6
40
0
41
0
42
0
43
0
44
0
45
0
46
0
47
0
48
0
49
0
140
0
141
0
142
1
143
1
70
688
72
1
73
0
74
5
7
75
16
147
1
148
0
149
0
100
AcDbLayout
1
Layout1
70
1
71
1
10
0
20
0
11
200
21
100
12
0
22
0
32
0
14
0
24
0
34
0
15
200
25
100
35
0
146
0
13
0
23
0
33
0
16
1
26
0
36
0
17
0
27
1
37
0
76
1
330
E
0
ENDSEC
0
EOF
File diff suppressed because it is too large Load Diff
+3
View File
@@ -18,6 +18,9 @@
//! - [`stream`]: one flat scan-line part read block by block, for pictures larger than memory.
//! - [`cryptomatte_samples`]: a file's Cryptomatte layers and their per-pixel (ID, coverage) ranks.
/// RFC 1950/1951 compressor for ZIP-compressed EXR chunks.
pub mod zlib;
pub mod channels;
pub mod cryptomatte;
#[cfg(feature = "decode")]
+795
View File
@@ -0,0 +1,795 @@
//! A small DEFLATE encoder (RFC 1951) in a zlib wrapper (RFC 1950), written from the RFCs for the
//! ZIP-compressed OpenEXR chunks.
//!
//! Greedy LZ77 over a 32 KB window (hash chains of bounded length), then each block of tokens is
//! written as whichever of stored, fixed-Huffman or dynamic-Huffman is smallest. Huffman code
//! lengths are limited (15 bits, 7 for the code-length code) by halving the symbol frequencies
//! until the tree fits. Every code has at least two symbols, so all codes are complete. The
//! output favours simplicity and correctness over ratio; worst-case expansion is the stored-block
//! overhead (5 bytes per 64 KB).
use std::cmp::Reverse;
use std::collections::BinaryHeap;
const WINDOW: usize = 32 * 1024;
const MIN_MATCH: usize = 3;
const MAX_MATCH: usize = 258;
/// Hash chain candidates tried per position.
const MAX_CHAIN: usize = 48;
/// Tokens per block (a new set of Huffman codes after this many).
const BLOCK_TOKENS: usize = 1 << 15;
const MAX_STORED: usize = 65_535;
const LEN_BASE: [u16; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258];
const LEN_EXTRA: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0];
const DIST_BASE: [u16; 30] =
[1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577];
const DIST_EXTRA: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
/// Order in which the code-length code's lengths are sent.
const CL_ORDER: [usize; 19] = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
/// `data` compressed as a zlib stream (CMF 0x78: deflate, 32 KB window). `tables` are the match
/// finder's hash tables, kept by the caller across the chunks of a picture; the output does not
/// depend on what they held before.
pub fn zlib(data: &[u8], tables: &mut Tables) -> Vec<u8> {
let mut w = Bits { out: Vec::with_capacity(data.len() / 2 + 64), acc: 0, n: 0 };
w.out.extend_from_slice(&[0x78, 0x9C]); // (0x78 << 8 | 0x9C) % 31 == 0
deflate(data, &mut w, tables);
w.align();
w.out.extend_from_slice(&adler32(data).to_be_bytes());
w.out
}
/// Adler-32 (RFC 1950 §9); 5552 bytes is the longest run that cannot overflow the sums.
fn adler32(data: &[u8]) -> u32 {
let (mut a, mut b) = (1_u32, 0_u32);
for c in data.chunks(5552) {
for &x in c {
a += u32::from(x);
b += a;
}
a %= 65_521;
b %= 65_521;
}
(b << 16) | a
}
/// LSB-first bit writer.
struct Bits {
out: Vec<u8>,
acc: u64,
n: u32,
}
impl Bits {
/// The low `n` bits of `v` (n ≤ 32).
fn put(&mut self, v: u32, n: u32) {
let mask = (1_u64 << n) - 1;
self.acc |= (u64::from(v) & mask) << self.n;
self.n += n;
while self.n >= 8 {
self.out.push(self.acc as u8);
self.acc >>= 8;
self.n -= 8;
}
}
fn align(&mut self) {
if self.n > 0 {
self.out.push(self.acc as u8);
}
self.acc = 0;
self.n = 0;
}
}
#[derive(Clone, Copy)]
enum Tok {
Lit(u8),
Match { len: u16, dist: u16 },
}
/// Index into [`LEN_BASE`] of a match length (3..=258).
fn len_code(len: u16) -> usize {
LEN_BASE.partition_point(|&b| b <= len).saturating_sub(1)
}
/// Index into [`DIST_BASE`] of a distance (1..=32768).
fn dist_code(dist: u16) -> usize {
DIST_BASE.partition_point(|&b| b <= dist).saturating_sub(1)
}
/// The match finder's hash heads and chains, reused from one input to the next.
///
/// A slot holds `base + p + 1` for position `p` of the input that wrote it; anything at or below
/// the current input's `base` was left by an earlier input and reads as empty. So the tables are
/// never cleared between inputs: an OpenEXR ZIPS frame is one input per scan line, and zeroing
/// 256 KiB of tables for each 23 KB UHD line cost more memory traffic than the data itself.
#[derive(Default)]
pub struct Tables {
head: Vec<u32>,
prev: Vec<u32>,
/// Bytes compressed through these tables since they were last cleared.
base: u32,
}
/// LZ77 match finder: hash heads and chains over the window.
struct Matcher<'a> {
data: &'a [u8],
head: &'a mut [u32],
prev: &'a mut [u32],
bits: u32,
/// `prev` slots used by this input, minus one (a power of two minus one).
mask: usize,
base: u32,
}
impl<'a> Matcher<'a> {
/// A matcher over `data` (shorter than `u32::MAX` bytes), or None when it is not.
fn new(data: &'a [u8], t: &'a mut Tables) -> Option<Self> {
// hash width and chain span follow the input's size, as they did when every input had
// tables of its own (the output depends on them), so short chunks stay cheap to hash
let span = data.len().clamp(256, WINDOW).next_power_of_two();
let bits = span.trailing_zeros().clamp(8, 15);
let n = u32::try_from(data.len()).ok().filter(|&n| n < u32::MAX)?;
if t.base.checked_add(n).and_then(|b| b.checked_add(1)).is_none() {
// positions would no longer fit after `base`: start over from empty tables
t.head.fill(0);
t.prev.fill(0);
t.base = 0;
}
// grown slots are 0, which is never above `base`, so they read as empty
if t.head.len() < 1 << bits {
t.head.resize(1 << bits, 0);
}
if t.prev.len() < span {
t.prev.resize(span, 0);
}
let base = t.base;
t.base += n;
let (head, prev) = (t.head.get_mut(..1 << bits)?, t.prev.get_mut(..span)?);
Some(Matcher { data, head, prev, bits, mask: span - 1, base })
}
/// Position `p` of this input from a stored slot, or None for an empty or earlier input's slot.
fn pos(&self, slot: u32) -> Option<usize> {
slot.checked_sub(self.base)?.checked_sub(1).map(|p| p as usize)
}
fn hash(&self, p: usize) -> Option<usize> {
let b = self.data.get(p..p + MIN_MATCH)?;
let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize)
}
/// Record position `p` (stored as `p + 1`; 0 = empty).
fn insert(&mut self, p: usize) {
let Some(h) = self.hash(p) else { return };
let (Some(head), Ok(p32)) = (self.head.get(h).copied(), u32::try_from(p)) else { return };
if let Some(slot) = self.prev.get_mut(p & self.mask) {
*slot = head;
}
if let Some(slot) = self.head.get_mut(h) {
// `new` reserved `base + len + 1` within u32
*slot = self.base + p32 + 1;
}
}
/// The longest earlier match at `p`: (length, distance), length 0 if none.
fn longest(&self, p: usize) -> (usize, usize) {
let Some(h) = self.hash(p) else { return (0, 0) };
let max = self.data.len().saturating_sub(p).min(MAX_MATCH);
let Some(here) = self.data.get(p..p + max) else { return (0, 0) };
let (mut best, mut best_dist) = (0, 0);
let mut cand = self.head.get(h).and_then(|&s| self.pos(s));
for _ in 0..MAX_CHAIN {
let Some(c) = cand else { break };
if c >= p || p - c > WINDOW {
break;
}
// a candidate that differs at `best` cannot be longer than `best`: skip comparing it
if best == 0 || self.data.get(c + best) == here.get(best) {
let l = self.data.get(c..).unwrap_or_default().iter().zip(here).take_while(|(a, b)| a == b).count();
if l > best {
(best, best_dist) = (l, p - c);
if l == max {
break;
}
}
}
let Some(next) = self.prev.get(c & self.mask).and_then(|&s| self.pos(s)) else { break };
// chains only go back; anything else is a stale slot
if next >= c {
break;
}
cand = Some(next);
}
(best, best_dist)
}
}
/// The deflate blocks of `data`, the last one final.
fn deflate(data: &[u8], w: &mut Bits, tables: &mut Tables) {
if data.is_empty() {
// a final fixed-Huffman block holding only end-of-block (code 0000000)
w.put(1, 1);
w.put(1, 2);
w.put(0, 7);
return;
}
// positions are kept in u32; longer inputs (never an OpenEXR chunk) go out stored
let mut m = Matcher::new(data, tables);
let mut toks = Vec::with_capacity(BLOCK_TOKENS.min(data.len()) + 1);
let (mut start, mut i) = (0, 0);
while i < data.len() {
let (len, dist) = m.as_ref().map_or((0, 0), |m| m.longest(i));
let step = if len >= MIN_MATCH {
toks.push(Tok::Match { len: len as u16, dist: dist as u16 });
len
} else {
toks.push(Tok::Lit(data[i]));
1
};
if let Some(m) = m.as_mut() {
for p in i..i + step {
m.insert(p);
}
}
i += step;
if toks.len() >= BLOCK_TOKENS || i == data.len() {
block(w, &toks, &data[start..i], i == data.len());
toks.clear();
start = i;
}
}
}
/// Bits the tokens take with the given code lengths (EOB included).
fn data_bits(lf: &[u32], df: &[u32], ll: &[u8], dl: &[u8]) -> u64 {
let mut n = 0_u64;
for (s, (&f, &l)) in lf.iter().zip(ll).enumerate() {
let extra = s.checked_sub(257).and_then(|k| LEN_EXTRA.get(k)).copied().unwrap_or(0);
n += u64::from(f) * u64::from(l + extra);
}
for (d, (&f, &l)) in df.iter().zip(dl).enumerate() {
n += u64::from(f) * u64::from(l + DIST_EXTRA.get(d).copied().unwrap_or(0));
}
n
}
/// One block (split into several stored blocks when stored is smallest and `raw` is long).
fn block(w: &mut Bits, toks: &[Tok], raw: &[u8], last: bool) {
let mut lf = [0_u32; 286];
let mut df = [0_u32; 30];
for t in toks {
match *t {
Tok::Lit(b) => lf[b as usize] += 1,
Tok::Match { len, dist } => {
lf[257 + len_code(len)] += 1;
df[dist_code(dist)] += 1;
}
}
}
lf[256] += 1;
// dynamic codes and their header
let ll = lengths(&lf, 15);
let dl = lengths(&df, 15);
let hlit = ll.iter().rposition(|&l| l > 0).map_or(257, |p| (p + 1).max(257));
let hdist = dl.iter().rposition(|&l| l > 0).map_or(1, |p| p + 1);
let seq = rle_lengths(&[&ll[..hlit], &dl[..hdist]].concat());
let mut cf = [0_u32; 19];
for &(s, _) in &seq {
cf[s as usize] += 1;
}
let cl = lengths(&cf, 7);
let hclen = CL_ORDER.iter().rposition(|&s| cl[s] > 0).map_or(4, |p| (p + 1).max(4));
let seq_bits: u64 =
seq.iter().map(|&(s, _)| u64::from(cl[s as usize]) + [2_u64, 3, 7].get((s as usize).wrapping_sub(16)).copied().unwrap_or(0)).sum();
let dyn_bits = 3 + 14 + 3 * hclen as u64 + seq_bits + data_bits(&lf, &df, &ll, &dl);
let (fll, fdl) = fixed_lengths();
let fixed_bits = 3 + data_bits(&lf, &df, &fll, &fdl);
// per stored piece: header, at most 7 padding bits, LEN and NLEN
let pieces = raw.len().div_ceil(MAX_STORED).max(1) as u64;
let stored_bits = pieces * (3 + 7 + 32) + 8 * raw.len() as u64;
if stored_bits <= fixed_bits.min(dyn_bits) {
let n = raw.chunks(MAX_STORED).count();
for (k, piece) in raw.chunks(MAX_STORED).enumerate() {
w.put(u32::from(last && k + 1 == n), 1);
w.put(0, 2);
w.align();
let len = piece.len() as u16;
w.out.extend_from_slice(&len.to_le_bytes());
w.out.extend_from_slice(&(!len).to_le_bytes());
w.out.extend_from_slice(piece);
}
return;
}
w.put(u32::from(last), 1);
if fixed_bits <= dyn_bits {
w.put(1, 2);
write_tokens(w, toks, &fll, &fdl);
return;
}
w.put(2, 2);
w.put((hlit - 257) as u32, 5);
w.put((hdist - 1) as u32, 5);
w.put((hclen - 4) as u32, 4);
for &s in &CL_ORDER[..hclen] {
w.put(u32::from(cl[s]), 3);
}
let cc = codes(&cl);
for &(s, x) in &seq {
w.put(u32::from(cc[s as usize]), u32::from(cl[s as usize]));
match s {
16 => w.put(u32::from(x), 2),
17 => w.put(u32::from(x), 3),
18 => w.put(u32::from(x), 7),
_ => {}
}
}
write_tokens(w, toks, &ll, &dl);
}
/// The fixed Huffman code lengths (RFC 1951 §3.2.6).
fn fixed_lengths() -> ([u8; 288], [u8; 30]) {
let mut ll = [8_u8; 288];
ll[144..256].fill(9);
ll[256..280].fill(7);
(ll, [5; 30])
}
fn write_tokens(w: &mut Bits, toks: &[Tok], ll: &[u8], dl: &[u8]) {
let lc = codes(ll);
let dc = codes(dl);
let sym = |w: &mut Bits, s: usize| w.put(u32::from(lc[s]), u32::from(ll[s]));
for t in toks {
match *t {
Tok::Lit(b) => sym(w, b as usize),
Tok::Match { len, dist } => {
let k = len_code(len);
sym(w, 257 + k);
w.put(u32::from(len - LEN_BASE[k]), u32::from(LEN_EXTRA[k]));
let d = dist_code(dist);
w.put(u32::from(dc[d]), u32::from(dl[d]));
w.put(u32::from(dist - DIST_BASE[d]), u32::from(DIST_EXTRA[d]));
}
}
}
sym(w, 256);
}
/// Canonical Huffman codes for `lens`, bit-reversed for the LSB-first writer.
fn codes(lens: &[u8]) -> Vec<u16> {
let mut count = [0_u32; 16];
for &l in lens {
if (1..16).contains(&l) {
count[l as usize] += 1;
}
}
let mut next = [0_u32; 16];
let mut code = 0;
for bits in 1..16 {
code = (code + count[bits - 1]) << 1;
next[bits] = code;
}
lens.iter()
.map(|&l| {
if !(1..16).contains(&l) {
return 0;
}
let c = next[l as usize];
next[l as usize] += 1;
(c.reverse_bits() >> (32 - u32::from(l))) as u16
})
.collect()
}
/// Code lengths for `freq`, none longer than `limit`; at least two symbols always get a code.
fn lengths(freq: &[u32], limit: u8) -> Vec<u8> {
let mut f: Vec<u64> = freq.iter().map(|&x| u64::from(x)).collect();
let mut used = f.iter().filter(|&&x| x > 0).count();
for x in f.iter_mut() {
if used >= 2 {
break;
}
if *x == 0 {
*x = 1;
used += 1;
}
}
// halving converges: once every used frequency is 1 the tree is balanced (depth ≤ 9 here)
for _ in 0..66 {
let l = huffman(&f);
if l.iter().all(|&d| d <= limit) {
return l;
}
for x in f.iter_mut().filter(|x| **x > 0) {
*x = (*x >> 1).max(1);
}
}
huffman(&f)
}
/// Huffman tree depths of the symbols with non-zero frequency (0 for the rest).
fn huffman(f: &[u64]) -> Vec<u8> {
let n = f.len();
let mut heap: BinaryHeap<Reverse<(u64, usize)>> = f.iter().enumerate().filter(|(_, w)| **w > 0).map(|(i, &w)| Reverse((w, i))).collect();
let mut parent = vec![usize::MAX; 2 * n];
let mut next = n;
while heap.len() > 1 {
let (Some(Reverse((wa, a))), Some(Reverse((wb, b)))) = (heap.pop(), heap.pop()) else { break };
parent[a] = next;
parent[b] = next;
heap.push(Reverse((wa + wb, next)));
next += 1;
}
// parents always come after their children, so walk down from the root
let mut depth = vec![0_u32; next];
for i in (0..next).rev() {
if let Some(&p) = parent.get(i).filter(|&&p| p < next) {
depth[i] = depth[p].saturating_add(1);
}
}
depth[..n].iter().map(|&d| d.min(u32::from(u8::MAX)) as u8).collect()
}
/// Code lengths run-length coded with the code-length alphabet: (symbol, extra-bits value).
fn rle_lengths(lens: &[u8]) -> Vec<(u8, u8)> {
let mut out = Vec::new();
let mut i = 0;
while i < lens.len() {
let l = lens[i];
let run = lens[i..].iter().take_while(|&&x| x == l).count();
let mut r = run;
if l == 0 {
while r >= 11 {
let k = r.min(138);
out.push((18, (k - 11) as u8));
r -= k;
}
if r >= 3 {
out.push((17, (r - 3) as u8));
r = 0;
}
} else {
out.push((l, 0));
r -= 1;
while r >= 3 {
let k = r.min(6);
out.push((16, (k - 3) as u8));
r -= k;
}
}
out.extend(std::iter::repeat_n((l, 0), r));
i += run;
}
out
}
#[cfg(test)]
mod tests {
use super::*;
/// A minimal inflater (RFC 1951), only to check the encoder; returns None on any error.
pub(crate) fn inflate(z: &[u8]) -> Option<Vec<u8>> {
let (cmf, flg) = (*z.first()?, *z.get(1)?);
if cmf & 0x0F != 8 || (u16::from(cmf) << 8 | u16::from(flg)) % 31 != 0 || flg & 0x20 != 0 {
return None;
}
let mut pos = 16_usize; // bit position
let bit = |pos: &mut usize| -> Option<u32> {
let b = (*z.get(*pos / 8)? >> (*pos % 8)) & 1;
*pos += 1;
Some(u32::from(b))
};
let bits = |pos: &mut usize, n: u32| -> Option<u32> {
let mut v = 0;
for k in 0..n {
v |= bit(pos)? << k;
}
Some(v)
};
// decode one symbol with canonical lengths (slow but simple)
let decode = |pos: &mut usize, lens: &[u8]| -> Option<usize> {
let (mut code, mut first, mut index) = (0_i32, 0_i32, 0_i32);
let mut count = [0_i32; 16];
for &l in lens {
count[l as usize] += 1;
}
let mut sorted: Vec<usize> = (0..lens.len()).filter(|&s| lens[s] > 0).collect();
sorted.sort_by_key(|&s| (lens[s], s));
for &c in count.iter().skip(1) {
code |= bit(pos)? as i32;
if code - c < first {
return sorted.get((index + code - first) as usize).copied();
}
index += c;
first = (first + c) << 1;
code <<= 1;
}
None
};
let mut out = Vec::new();
loop {
let last = bit(&mut pos)?;
match bits(&mut pos, 2)? {
0 => {
pos = pos.div_ceil(8) * 8;
let at = pos / 8;
let len = u16::from_le_bytes([*z.get(at)?, *z.get(at + 1)?]);
let nlen = u16::from_le_bytes([*z.get(at + 2)?, *z.get(at + 3)?]);
if len != !nlen {
return None;
}
out.extend_from_slice(z.get(at + 4..at + 4 + len as usize)?);
pos = (at + 4 + len as usize) * 8;
}
t @ (1 | 2) => {
let (ll, dl) = if t == 1 {
let (a, b) = fixed_lengths();
(a.to_vec(), b.to_vec())
} else {
let hlit = bits(&mut pos, 5)? as usize + 257;
let hdist = bits(&mut pos, 5)? as usize + 1;
let hclen = bits(&mut pos, 4)? as usize + 4;
let mut cl = [0_u8; 19];
for &s in &CL_ORDER[..hclen] {
cl[s] = bits(&mut pos, 3)? as u8;
}
let mut lens = Vec::new();
while lens.len() < hlit + hdist {
match decode(&mut pos, &cl)? {
s @ 0..=15 => lens.push(s as u8),
16 => {
let p = *lens.last()?;
lens.extend(std::iter::repeat_n(p, 3 + bits(&mut pos, 2)? as usize));
}
17 => lens.extend(std::iter::repeat_n(0, 3 + bits(&mut pos, 3)? as usize)),
_ => lens.extend(std::iter::repeat_n(0, 11 + bits(&mut pos, 7)? as usize)),
}
}
if lens.len() != hlit + hdist {
return None;
}
(lens[..hlit].to_vec(), lens[hlit..].to_vec())
};
loop {
let s = decode(&mut pos, &ll)?;
if s < 256 {
out.push(s as u8);
} else if s == 256 {
break;
} else {
let k = s - 257;
let len = *LEN_BASE.get(k)? as usize + bits(&mut pos, u32::from(LEN_EXTRA[k]))? as usize;
let d = decode(&mut pos, &dl)?;
let dist = *DIST_BASE.get(d)? as usize + bits(&mut pos, u32::from(DIST_EXTRA[d]))? as usize;
let from = out.len().checked_sub(dist)?;
for j in 0..len {
out.push(out[from + j]);
}
}
}
}
_ => return None,
}
if last == 1 {
break;
}
}
let at = pos.div_ceil(8);
let sum = u32::from_be_bytes(z.get(at..at + 4)?.try_into().ok()?);
(sum == adler32(&out) && at + 4 == z.len()).then_some(out)
}
/// A deterministic pseudo-random byte stream.
fn noise(n: usize, seed: u32) -> Vec<u8> {
let mut s = seed | 1;
(0..n)
.map(|_| {
s ^= s << 13;
s ^= s >> 17;
s ^= s << 5;
s as u8
})
.collect()
}
#[test]
fn round_trips() {
let mut cases: Vec<Vec<u8>> = vec![vec![], vec![7], vec![1, 2], vec![0; 1], vec![0; 300_000], b"abcabcabcabcabcabd".to_vec()];
cases.push(noise(100_000, 9)); // incompressible: stored blocks
cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect()); // few symbols: dynamic
cases.push((0..200_000_u32).map(|i| ((i / 7) % 251) as u8).collect()); // long repeats
// text-like: a small vocabulary repeated at varying distances
let words: [&[u8]; 6] = [b"red ", b"green ", b"blue ", b"alpha ", b"half ", b"linear "];
cases.push(noise(40_000, 5).iter().flat_map(|b| words[*b as usize % 6].iter().copied()).collect());
// skewed frequencies: Huffman lengths beyond 15 before limiting
let mut fib = Vec::new();
let (mut a, mut b) = (1_usize, 1_usize);
for s in 0..25_u8 {
fib.extend(std::iter::repeat_n(s.wrapping_mul(37), a.min(200_000)));
(a, b) = (b, a + b);
}
let r = noise(fib.len() * 4, 11);
for i in (1..fib.len()).rev() {
let j = u32::from_le_bytes(r[i * 4..i * 4 + 4].try_into().unwrap()) as usize % (i + 1);
fib.swap(i, j);
}
cases.push(fib);
for (k, data) in cases.iter().enumerate() {
let z = zlib(data, &mut Tables::default());
assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "case {k}");
// worst case: stored overhead only
assert!(z.len() <= data.len() + (data.len().div_ceil(BLOCK_TOKENS) + 1) * 5 + 6, "case {k}: {} > {}", z.len(), data.len());
}
// repetitive data actually compresses
assert!(zlib(&cases[4], &mut Tables::default()).len() < 2_000);
assert!(zlib(&cases[8], &mut Tables::default()).len() < 20_000);
}
#[test]
fn length_limits_hold() {
// Fibonacci frequencies give the deepest possible Huffman tree
let mut f = vec![0_u32; 286];
let (mut a, mut b) = (1_u32, 1_u32);
for x in f.iter_mut().take(30) {
*x = a;
(a, b) = (b, a.saturating_add(b));
}
let l = lengths(&f, 15);
assert!(l.iter().all(|&d| d <= 15));
// a complete code: Kraft sum exactly 1
let kraft: f64 = l.iter().filter(|&&d| d > 0).map(|&d| 0.5_f64.powi(i32::from(d))).sum();
assert!((kraft - 1.0).abs() < 1e-12);
// one used symbol still gets a two-symbol code
let l = lengths(&[0, 5, 0], 7);
assert_eq!(l.iter().filter(|&&d| d == 1).count(), 2);
}
#[test]
fn adler_matches_rfc_example() {
// the classic test vector for "Wikipedia"
assert_eq!(adler32(b"Wikipedia"), 0x11E6_0398);
assert_eq!(adler32(&[]), 1);
}
/// The match finder before #4659: tables of its own per input, every chain candidate compared
/// from its first byte.
struct OldMatcher<'a> {
data: &'a [u8],
head: Vec<u32>,
prev: Vec<u32>,
bits: u32,
}
impl<'a> OldMatcher<'a> {
/// Tables of its own, sized to `data`.
fn new(data: &'a [u8]) -> Self {
let span = data.len().clamp(256, WINDOW).next_power_of_two();
let bits = span.trailing_zeros().clamp(8, 15);
OldMatcher { data, head: vec![0; 1 << bits], prev: vec![0; span], bits }
}
/// The hash of the three bytes at `p`, None near the end.
fn hash(&self, p: usize) -> Option<usize> {
let b = self.data.get(p..p + MIN_MATCH)?;
let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize)
}
/// Record position `p` (stored as `p + 1`; 0 = empty).
fn insert(&mut self, p: usize) {
let Some(h) = self.hash(p) else { return };
let mask = self.prev.len() - 1;
self.prev[p & mask] = self.head[h];
self.head[h] = p as u32 + 1;
}
/// The longest earlier match at `p`: (length, distance), length 0 if none.
fn longest(&self, p: usize) -> (usize, usize) {
let Some(h) = self.hash(p) else { return (0, 0) };
let max = (self.data.len() - p).min(MAX_MATCH);
let here = &self.data[p..p + max];
let mask = self.prev.len() - 1;
let (mut best, mut best_dist) = (0, 0);
let mut cand = self.head[h] as usize;
for _ in 0..MAX_CHAIN {
let Some(c) = cand.checked_sub(1) else { break };
if c >= p || p - c > WINDOW {
break;
}
let l = self.data[c..].iter().zip(here).take_while(|(a, b)| a == b).count();
if l > best {
(best, best_dist) = (l, p - c);
if l == max {
break;
}
}
let next = self.prev[c & mask] as usize;
if next == 0 || next > c {
break;
}
cand = next;
}
(best, best_dist)
}
}
/// Inputs shaped like OpenEXR chunks (predicted half-float scan lines, single and 16-line)
/// and the awkward cases: empty, tiny, incompressible, long runs, few symbols.
fn chunk_cases() -> Vec<Vec<u8>> {
let line: Vec<u8> = (0..23_040_u32).map(|i| (128 + ((i / 3) % 7) as i32 - 3 + (i % 5 == 0) as i32) as u8).collect();
let mut cases = vec![line.clone(), line.iter().rev().copied().collect(), vec![], vec![9], b"abcabcabcabd".to_vec()];
cases.push(line.iter().cycle().take(16 * 23_040).copied().collect()); // ZIP: 16 lines, longer than the window
cases.push(noise(23_040, 4));
cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect());
cases.push(vec![0; 300_000]);
cases.push(line.clone());
cases.push(noise(300, 7));
cases
}
/// The matcher's greedy parse of `data`: what `deflate` codes.
fn parse<F: FnMut(usize) -> (usize, usize), G: FnMut(usize)>(data: &[u8], mut longest: F, mut insert: G) -> Vec<(usize, usize)> {
let (mut out, mut i) = (Vec::new(), 0);
while i < data.len() {
let (len, dist) = longest(i);
let step = if len >= MIN_MATCH { len } else { 1 };
out.push(if len >= MIN_MATCH { (len, dist) } else { (0, 0) });
for p in i..i + step {
insert(p);
}
i += step;
}
out
}
/// Shared tables and the candidate skip find exactly the matches the previous matcher found.
#[test]
fn shared_tables_find_the_matches_the_old_per_chunk_tables_found() {
let mut tables = Tables::default();
for (k, data) in chunk_cases().iter().enumerate() {
let old = std::cell::RefCell::new(OldMatcher::new(data));
let want = parse(data, |p| old.borrow().longest(p), |p| old.borrow_mut().insert(p));
let new = std::cell::RefCell::new(Matcher::new(data, &mut tables).expect("fits"));
let got = parse(data, |p| new.borrow().longest(p), |p| new.borrow_mut().insert(p));
assert!(got == want, "chunk {k}: the parse differs");
}
}
/// Shared tables compress to the bytes fresh ones give, also when positions run out and they start over.
#[test]
fn shared_tables_give_the_bytes_of_fresh_ones() {
let mut shared = Tables::default();
for (k, data) in chunk_cases().iter().enumerate() {
let z = zlib(data, &mut shared);
assert!(z == zlib(data, &mut Tables::default()), "chunk {k}");
assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "chunk {k}");
}
// positions about to run past u32: the tables start over and the bytes stay the same
for gap in [0_u32, 1, 2, 23_040, 23_041, 1 << 20] {
shared.base = u32::MAX - gap;
for (k, data) in chunk_cases().iter().enumerate() {
assert!(zlib(data, &mut shared) == zlib(data, &mut Tables::default()), "gap {gap}, chunk {k}");
}
}
}
/// Later chunks use the same tables instead of allocating new ones.
#[test]
fn the_tables_are_reused_across_chunks() {
let mut tables = Tables::default();
let line = &chunk_cases()[0];
zlib(line, &mut tables);
let (head, prev) = (tables.head.as_ptr(), tables.prev.as_ptr());
for _ in 0..4 {
zlib(line, &mut tables);
assert!(tables.head.as_ptr() == head && tables.prev.as_ptr() == prev, "a chunk allocated new tables");
}
assert_eq!(tables.base as usize, 5 * line.len());
}
}
+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
@@ -51,6 +51,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"] }
+123
View File
@@ -0,0 +1,123 @@
JPEG encoding dependency: jpeg-encoder 0.6.1
Source: https://crates.io/crates/jpeg-encoder/0.6.1
Declared license: (MIT OR Apache-2.0) AND IJG.
This software is based in part on the work of the Independent JPEG Group.
The MIT license and unaltered upstream IJG and SIMD notices follow. The Apache-2.0
alternative is also retained in licenses/jpeg-encoder-0.6.1 at the repository root
(https://github.com/storytold/craft-libs/tree/main/licenses/jpeg-encoder-0.6.1).
The dependency is used unmodified; no encoder implementation is vendored here.
LICENSE-MIT
Copyright (c) 2021 Volker Ströbel <volkerstroebel@mysurdity.de>
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Original src/fdct.rs opening notice
/*
* Ported from mozjpeg to rust
*
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1991-1996, Thomas G. Lane.
* libjpeg-turbo Modifications:
* Copyright (C) 2015, 2020, D. R. Commander.
*
* Conditions of distribution and use:
* In plain English:
*
* 1. We don't promise that this software works. (But if you find any bugs,
* please let us know!)
* 2. You can use this software for whatever you want. You don't have to pay us.
* 3. You may not pretend that you wrote this software. If you use it in a
* program, you must acknowledge somewhere in your documentation that
* you've used the IJG code.
*
* In legalese:
*
* The authors make NO WARRANTY or representation, either express or implied,
* with respect to this software, its quality, accuracy, merchantability, or
* fitness for a particular purpose. This software is provided "AS IS", and you,
* its user, assume the entire risk as to its quality and accuracy.
*
* This software is copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
* All Rights Reserved except as specified below.
*
* Permission is hereby granted to use, copy, modify, and distribute this
* software (or portions thereof) for any purpose, without fee, subject to these
* conditions:
* (1) If any part of the source code for this software is distributed, then this
* README file must be included, with this copyright and no-warranty notice
* unaltered; and any additions, deletions, or changes to the original files
* must be clearly indicated in accompanying documentation.
* (2) If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the work of
* the Independent JPEG Group".
* (3) Permission for use of this software is granted only if the user accepts
* full responsibility for any undesirable consequences; the authors accept
* NO LIABILITY for damages of any kind.
*
* These conditions apply to any software derived from or based on the IJG code,
* not just to the unmodified library. If you use our work, you ought to
* acknowledge us.
*
* Permission is NOT granted for the use of any IJG author's name or company name
* in advertising or publicity relating to this software or products derived from
* it. This software may be referred to only as "the Independent JPEG Group's
* software".
*
* We specifically permit and encourage the use of this software as the basis of
* commercial products, provided that all warranty or liability claims are
* assumed by the product vendor.
*
* This file contains a slower but more accurate integer implementation of the
* forward DCT (Discrete Cosine Transform).
*
* A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
* on each column. Direct algorithms are also available, but they are
* much more complex and seem not to be any faster when reduced to code.
*
* This implementation is based on an algorithm described in
* C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
* Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
* Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
* The primary algorithm described there uses 11 multiplies and 29 adds.
* We use their alternate method with 12 multiplies and 32 adds.
* The advantage of this method is that no data path contains more than one
* multiplication; this allows a very simple and accurate implementation in
* scaled fixed-point arithmetic, with a minimal number of shifts.
*/
Original src/avx2/fdct.rs opening notice
/*
* Ported from mozjpeg / jfdctint-avx2.asm to rust
* Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
* Copyright (C) 2009, 2016, 2018, 2020, D. R. Commander.
*
* Based on the x86 SIMD extension for IJG JPEG library
* Copyright (C) 1999-2006, MIYASAKA Masaru.
*/
+10
View File
@@ -57,6 +57,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 | with feature `avif-decode` (native only) | only with feature `avif` | U8, U16 (10/12-bit decodes to U16; PQ/HLG to linear F32) | RGB, RGBA | yes (auxiliary alpha) | read (`colr` prof/rICC) | read | read | no | no | yes | `craft-video` AV1 decoder; `image` + `ravif` to write |
| 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` |
@@ -350,3 +351,12 @@ folder, copy the five files from that commit into `corpus/exr/`, or run
at commit `74330157cb601573ae4523a95bb9132822fc3918` and copy its `corpus/exr/` here, as CI does.
The images are not committed here; OpenEXR distributes them under its
[license](https://github.com/AcademySoftwareFoundation/openexr/blob/69b2604fc76e370615438bdc8d2cd95b9349c12e/LICENSE.md).
## Redistribution notices
JPEG encoding uses `jpeg-encoder` 0.6.1, including work from the Independent JPEG Group.
Retain this crate's [NOTICE](NOTICE) with binary distributions using that encoder. It includes
its MIT license, the IJG acknowledgement and original IJG/SIMD notices. Both upstream license
alternatives and source checksums are retained in
[`licenses/jpeg-encoder-0.6.1`](https://github.com/storytold/craft-libs/tree/main/licenses/jpeg-encoder-0.6.1)
at the repository root.
+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
@@ -15,6 +15,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;
+41 -2
View File
@@ -195,6 +195,27 @@ enum DataEnd {
/// measured on these, not on the file length, which APP padding can inflate without
/// adding image data.
fn data_end(b: &[u8]) -> (DataEnd, u64) {
data_end_mode(b, false)
}
/// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused.
pub(crate) fn reaches_eoi(b: &[u8]) -> bool {
data_end_mode(b, true).0 == DataEnd::Complete
}
fn find_ff(b: &[u8]) -> Option<usize> {
const ONES: u64 = 0x0101_0101_0101_0101;
let (words, tail) = b.as_chunks::<8>();
for (i, word) in words.iter().enumerate() {
let x = !u64::from_ne_bytes(*word);
if x.wrapping_sub(ONES) & !x & (ONES << 7) != 0 {
return word.iter().position(|&v| v == 0xff).map(|j| i * 8 + j);
}
}
tail.iter().position(|&v| v == 0xff).map(|j| words.len() * 8 + j)
}
fn data_end_mode(b: &[u8], strict: bool) -> (DataEnd, u64) {
// Where the bytes run out: inside the image data once a scan has held some.
let cut = |scan_data: bool, scanned: u64| (if scan_data { DataEnd::Truncated } else { DataEnd::Empty }, scanned);
let mut i = 2; // past SOI
@@ -207,7 +228,7 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) {
// segments after the stray bytes (padding among them) are still recognised. Stray
// bytes are not scan data, so they never count (junk would buy budget as padding
// did); with no marker left, the image data ends here.
let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { return cut(scan_data, scanned) };
let Some(p) = b.get(i..).and_then(find_ff) else { return cut(scan_data, scanned) };
i += p;
continue;
}
@@ -216,15 +237,19 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) {
0xD9 => return (DataEnd::Complete, scanned),
0xFF => i += 1,
0x01 | 0xD0..=0xD8 => i += 2,
0x00 if strict => i += 2,
_ => {
let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else {
return cut(scan_data, scanned);
};
if strict && len < 2 {
return cut(scan_data, scanned);
}
i = i.saturating_add(2 + usize::from(len));
if code == 0xDA {
let start = i;
loop {
let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else {
let Some(p) = b.get(i..).and_then(find_ff) else {
// EOF inside this scan: everything from the scan start to the end
// of the file is scan data (inside a declared segment it may not
// be, so only count what remains).
@@ -653,6 +678,20 @@ mod tests {
}
/// `info` reads the frame from the markers, and which decoder the file goes to.
#[test]
fn ff_search_matches_plain_search() {
let mut b: Vec<u8> = (0..300u32).map(|i| (i * 7 % 255) as u8).collect();
for at in [None, Some(0), Some(7), Some(8), Some(15), Some(203), Some(296), Some(299)] {
if let Some(i) = at {
b[i] = 0xff;
}
for start in [0, 1, 5, 9, 290, 299, 300] {
let s = &b[start..];
assert_eq!(find_ff(s), s.iter().position(|&v| v == 0xff));
}
}
}
#[test]
fn info_reads_the_frame_and_the_decoder() {
let img = Image::from_raw(37, 21, ChannelLayout::Rgb, SampleType::U8, vec![128; 37 * 21 * 3]).unwrap();
+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;
+7 -55
View File
@@ -5,66 +5,18 @@
//! ColorSpace = sRGB as sRGB, unless Preferences › File Handling › Ignore EXIF Profile Tag is on.
//! Anything malformed, truncated or missing reads as no tag.
use crate::orientation::{Order, ifd0_entries, tiff_body};
/// IFD0's pointer to the Exif sub-IFD.
const TAG_EXIF_IFD: u16 = 0x8769;
/// The ColorSpace tag in the Exif sub-IFD.
const TAG_COLOR_SPACE: u16 = 0xA001;
const TYPE_SHORT: u16 = 3;
const TYPE_LONG: u16 = 4;
/// TIFF type 13: an IFD offset (some writers use it for sub-IFD pointers).
const TYPE_IFD: u16 = 13;
/// Bytes per classic TIFF IFD entry; EXIF is never BigTIFF.
const ENTRY: usize = 12;
/// No IFD can start inside the 8-byte TIFF header.
const HEADER: usize = 8;
/// The ColorSpace value meaning sRGB.
pub const EXIF_COLOR_SPACE_SRGB: u16 = 1;
/// The EXIF ColorSpace value of a TIFF-structured block (an EXIF payload with or without the
/// `Exif\0\0` prefix), or `None` when it has none or it is malformed.
pub fn exif_color_space(exif: &[u8]) -> Option<u16> {
let b = tiff_body(exif);
let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?;
if big_tiff {
return None;
}
let ptr = find(order, b, first, count, entry_bytes, TAG_EXIF_IFD)?;
if !matches!(order.u16(b, ptr.checked_add(2)?)?, TYPE_LONG | TYPE_IFD) || order.u32(b, ptr.checked_add(4)?)? != 1 {
return None;
}
let ifd = usize::try_from(order.u32(b, ptr.checked_add(8)?)?).ok()?;
if ifd < HEADER {
return None;
}
let n = usize::from(order.u16(b, ifd)?);
let first = ifd.checked_add(2)?;
// The whole table must be present.
if first.checked_add(n.checked_mul(ENTRY)?)? > b.len() {
return None;
}
let e = find(order, b, first, n, ENTRY, TAG_COLOR_SPACE)?;
if order.u32(b, e.checked_add(4)?)? != 1 {
return None;
}
match order.u16(b, e.checked_add(2)?)? {
TYPE_SHORT => order.u16(b, e.checked_add(8)?),
TYPE_LONG => u16::try_from(order.u32(b, e.checked_add(8)?)?).ok(),
_ => None,
}
}
/// Offset of the first entry tagged `tag` in the IFD whose `count` entries start at `first`.
fn find(order: Order, b: &[u8], first: usize, count: usize, entry_bytes: usize, tag: u16) -> Option<usize> {
(0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(tag))
}
// Share parsing and malformed-input policy with every craft-metadata caller.
pub use craft_metadata::edit::{EXIF_COLOR_SPACE_SRGB, exif_color_space};
#[cfg(test)]
mod tests {
use super::*;
const TAG_EXIF_IFD: u16 = 0x8769;
const TAG_COLOR_SPACE: u16 = 0xA001;
const TYPE_SHORT: u16 = 3;
const TYPE_LONG: u16 = 4;
/// A little- or big-endian EXIF block: IFD0 holding only the Exif pointer, then an Exif IFD
/// holding `entries` (tag, type, count, value).
fn exif(big_endian: bool, entries: &[(u16, u16, u32, u32)]) -> Vec<u8> {
+12 -2
View File
@@ -45,6 +45,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.
@@ -116,7 +118,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,
@@ -141,6 +143,7 @@ impl Format {
Format::Cineon,
Format::Dicom,
Format::Iff,
Format::JpegLs,
];
pub fn caps(self) -> FormatCaps {
@@ -179,6 +182,7 @@ impl Format {
Format::Dicom => "DICOM",
Format::Iff => "Amiga IFF",
Format::Jp2 => "JPEG 2000",
Format::JpegLs => "JPEG-LS",
}
}
@@ -210,6 +214,7 @@ impl Format {
Format::Dicom => &["dcm", "dicom", "dc3"],
Format::Iff => &["iff", "lbm", "ilbm"],
Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"],
Format::JpegLs => &["jls"],
}
}
@@ -239,13 +244,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,
@@ -315,6 +321,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 },
}
}
@@ -326,6 +333,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);
}
+82
View File
@@ -0,0 +1,82 @@
//! Validated Samsung SEF trailer repair for JPEGs missing their final EOI marker.
use std::borrow::Cow;
/// Whether the first JPEG image reaches an EOI through its marker and scan structure.
pub fn reaches_eoi(bytes: &[u8]) -> bool {
crate::codecs::jpeg::reaches_eoi(bytes)
}
/// Borrow ordinary input unchanged. For a validated Samsung SEF trailer following an open JPEG
/// scan, remove that trailer and append the missing EOI. The owned copy is made only on repair.
pub fn repair(bytes: &[u8]) -> Cow<'_, [u8]> {
let Some(image) = image_end(bytes).and_then(|end| bytes.get(..end)) else { return Cow::Borrowed(bytes) };
if reaches_eoi(image) {
return Cow::Borrowed(bytes);
}
let mut fixed = Vec::with_capacity(image.len().saturating_add(2));
fixed.extend_from_slice(image);
fixed.extend_from_slice(&[0xff, 0xd9]);
let in_scan = craft_metadata::inspect_jpeg(&fixed).is_some_and(|m| m.first_scan_components > 0) && reaches_eoi(&fixed);
if in_scan { Cow::Owned(fixed) } else { Cow::Borrowed(bytes) }
}
fn image_end(b: &[u8]) -> Option<usize> {
let le32 = |at: usize| b.get(at..at.checked_add(4)?)?.try_into().ok().map(|v| u32::from_le_bytes(v) as usize);
let tail = b.len().checked_sub(8)?;
if b.get(tail.checked_add(4)?..)? != b"SEFT" {
return None;
}
let dir = tail.checked_sub(le32(tail)?)?;
if b.get(dir..dir.checked_add(4)?)? != b"SEFH" {
return None;
}
let count = le32(dir.checked_add(8)?)?;
let entries = dir.checked_add(12)?;
if count == 0 || count > tail.checked_sub(entries)? / 12 {
return None;
}
let mut start = dir;
for i in 0..count {
let entry = entries.checked_add(i.checked_mul(12)?)?;
let block = dir.checked_sub(le32(entry.checked_add(4)?)?)?;
let len = le32(entry.checked_add(8)?)?;
let kind = |at: usize| b.get(at.checked_add(2)?..at.checked_add(4)?);
if len < 4 || block.checked_add(len)? > dir || kind(block)? != kind(entry)? {
return None;
}
start = start.min(block);
}
Some(start)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn repairs_only_valid_sef_after_an_open_scan() {
let image = [0xff, 0xd8, 0xff, 0xc0, 0, 8, 8, 0, 1, 0, 1, 1, 0xff, 0xda, 0, 3, 1, 0x12, 0xff, 0, 0x34];
let mut b = image.to_vec();
let block = [0, 0, 0xe0, 0x08, 1, 2, 3, 4];
b.extend_from_slice(&block);
b.extend_from_slice(b"SEFH");
b.extend_from_slice(&106u32.to_le_bytes());
b.extend_from_slice(&1u32.to_le_bytes());
b.extend_from_slice(&[0, 0, 0xe0, 0x08]);
b.extend_from_slice(&(block.len() as u32).to_le_bytes());
b.extend_from_slice(&(block.len() as u32).to_le_bytes());
b.extend_from_slice(&24u32.to_le_bytes());
b.extend_from_slice(b"SEFT");
let mut expected = image.to_vec();
expected.extend_from_slice(&[0xff, 0xd9]);
assert_eq!(repair(&b).as_ref(), expected);
assert!(matches!(repair(&image), Cow::Borrowed(_)));
b[image.len() + 2] = 0;
assert!(matches!(repair(&b), Cow::Borrowed(_)));
}
#[test]
fn eoi_requires_valid_lengths_and_skips_stuffed_bytes() {
assert!(reaches_eoi(&[0xff, 0xd8, 0xff, 0, 0xff, 0xd9]));
assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 1, 0xff, 0xd9]));
assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 4, 0xff, 0xd9]));
}
}
+6 -1
View File
@@ -16,6 +16,7 @@
mod allocation;
pub mod apng;
pub mod bmp_probe;
pub mod cicp;
mod codecs;
mod error;
@@ -25,9 +26,11 @@ mod fidelity;
mod format;
mod image;
mod inspection;
pub mod jpeg_sef;
mod lab;
mod options;
pub mod orientation;
pub mod png_probe;
pub mod resolution;
pub mod source_settings;
pub mod web;
@@ -63,7 +66,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 +105,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),
@@ -468,6 +472,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")),
+5 -206
View File
@@ -20,213 +20,12 @@ use std::borrow::Cow;
use crate::error::CodecError;
use crate::image::Image;
/// The TIFF/EXIF Orientation tag.
const TAG_ORIENTATION: u16 = 274;
/// Field types accepted for the Orientation tag: SHORT (the spec) and LONG
/// (written by some cameras and tools).
const TYPE_SHORT: u16 = 3;
const TYPE_LONG: u16 = 4;
/// Bytes per IFD entry.
const ENTRY: usize = 12;
/// The TIFF header is 8 bytes, so no IFD can start before it.
const HEADER: usize = 8;
/// BigTIFF uses a 16-byte header, 20-byte entries, and 8-byte offsets.
const BIG_ENTRY: usize = 20;
const BIG_HEADER: usize = 16;
// Keep byte-level EXIF/XMP handling in one implementation so fixes apply to all callers.
pub use craft_metadata::edit::{exif_orientation, upright_exif, upright_xmp};
/// Byte order of a TIFF structure.
#[derive(Clone, Copy)]
pub(crate) enum Order {
Little,
Big,
}
impl Order {
pub(crate) fn u16(self, b: &[u8], at: usize) -> Option<u16> {
let s: [u8; 2] = b.get(at..at.checked_add(2)?)?.try_into().ok()?;
Some(match self {
Order::Little => u16::from_le_bytes(s),
Order::Big => u16::from_be_bytes(s),
})
}
pub(crate) fn u32(self, b: &[u8], at: usize) -> Option<u32> {
let s: [u8; 4] = b.get(at..at.checked_add(4)?)?.try_into().ok()?;
Some(match self {
Order::Little => u32::from_le_bytes(s),
Order::Big => u32::from_be_bytes(s),
})
}
pub(crate) fn u64(self, b: &[u8], at: usize) -> Option<u64> {
let s: [u8; 8] = b.get(at..at.checked_add(8)?)?.try_into().ok()?;
Some(match self {
Order::Little => u64::from_le_bytes(s),
Order::Big => u64::from_be_bytes(s),
})
}
}
/// The TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped).
pub(crate) fn tiff_body(b: &[u8]) -> &[u8] {
b.strip_prefix(b"Exif\0\0").unwrap_or(b)
}
/// IFD0 of a TIFF structure: byte order, entry layout, and entry offsets.
///
/// The full table and next-IFD pointer must be present, and IFD0 must start
/// after the corresponding TIFF or BigTIFF header.
pub(crate) fn ifd0_entries(b: &[u8]) -> Option<(Order, usize, usize, usize, bool)> {
let (order, big_tiff) = match b.get(0..4)? {
[b'I', b'I', 42, 0] => (Order::Little, false),
[b'M', b'M', 0, 42] => (Order::Big, false),
[b'I', b'I', 43, 0] => (Order::Little, true),
[b'M', b'M', 0, 43] => (Order::Big, true),
_ => return None,
};
let (ifd, count_bytes, entry_bytes, next_ifd_bytes, header) = if big_tiff {
if order.u16(b, 4)? != 8 || order.u16(b, 6)? != 0 {
return None;
}
(usize::try_from(order.u64(b, 8)?).ok()?, 8usize, BIG_ENTRY, 8usize, BIG_HEADER)
} else {
(usize::try_from(order.u32(b, 4)?).ok()?, 2usize, ENTRY, 4usize, HEADER)
};
if ifd < header {
return None;
}
let count = if big_tiff { usize::try_from(order.u64(b, ifd)?).ok()? } else { usize::from(order.u16(b, ifd)?) };
let first = ifd.checked_add(count_bytes)?;
let next_ifd = first.checked_add(count.checked_mul(entry_bytes)?)?;
if next_ifd.checked_add(next_ifd_bytes)? > b.len() {
return None;
}
// In bounds: every entry ends at or before `next_ifd`.
Some((order, first, count, entry_bytes, big_tiff))
}
/// A well-formed Orientation entry's value: where it sits and its field type.
#[derive(Clone, Copy)]
struct Value {
at: usize,
ty: u16,
}
impl Value {
/// The stored value (a SHORT or LONG, left-justified in the 4-byte field).
fn read(self, order: Order, b: &[u8]) -> Option<u32> {
match self.ty {
TYPE_SHORT => order.u16(b, self.at).map(u32::from),
_ => order.u32(b, self.at),
}
}
/// 1 in this entry's own type, width and byte order.
fn one(self, order: Order) -> Vec<u8> {
match (self.ty, order) {
(TYPE_SHORT, Order::Little) => 1u16.to_le_bytes().to_vec(),
(TYPE_SHORT, Order::Big) => 1u16.to_be_bytes().to_vec(),
(_, Order::Little) => 1u32.to_le_bytes().to_vec(),
(_, Order::Big) => 1u32.to_be_bytes().to_vec(),
}
}
}
/// The value of the entry at `e` if it is a well-formed Orientation entry:
/// exactly one SHORT or LONG.
fn orientation_value(order: Order, b: &[u8], e: usize, big_tiff: bool) -> Option<Value> {
let count = if big_tiff { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) };
if order.u16(b, e)? != TAG_ORIENTATION || count != 1 {
return None;
}
let ty = order.u16(b, e.checked_add(2)?)?;
let value_offset = if big_tiff { 12 } else { 8 };
matches!(ty, TYPE_SHORT | TYPE_LONG).then_some(Value { at: e.checked_add(value_offset)?, ty })
}
/// Locates the Orientation value in IFD0.
///
/// The **first** Orientation entry wins (like libexif and Photoshop); if that
/// one is malformed (count ≠ 1, or neither SHORT nor LONG) the tag is ignored,
/// even when a later duplicate is well formed.
fn find_entry(b: &[u8]) -> Option<(Order, Value)> {
let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?;
let e = (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(TAG_ORIENTATION))?;
Some((order, orientation_value(order, b, e, big_tiff)?))
}
/// The orientation (1–8) recorded in a TIFF-structured block: an EXIF payload
/// (with or without the `Exif\0\0` prefix) or a whole TIFF file. Missing,
/// malformed or out-of-range values give 1.
pub fn exif_orientation(exif: &[u8]) -> u16 {
let b = tiff_body(exif);
find_entry(b).and_then(|(order, v)| v.read(order, b)).and_then(|v| u16::try_from(v).ok()).filter(|v| (1..=8).contains(v)).unwrap_or(1)
}
/// `exif` with its Orientation rewritten to 1, borrowed when there is nothing
/// to change (no well-formed tag, already 1, or unparseable).
///
/// Only the value bytes of each well-formed Orientation entry change, written
/// as 1 in the entry's existing type and width (SHORT or LONG); every other
/// byte (other tags, their offset-based values, sub-IFDs) is kept as is.
/// Well-formed duplicates are all set to 1, so a reader that lets a later
/// duplicate win cannot rotate the upright pixels either.
pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> {
let body = tiff_body(exif);
let prefix = exif.len() - body.len();
let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else {
return Cow::Borrowed(exif);
};
let values: Vec<Value> = (0..count)
.filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?))
.filter_map(|e| orientation_value(order, body, e, big_tiff))
.collect();
if values.iter().all(|v| v.read(order, body) == Some(1)) {
return Cow::Borrowed(exif);
}
let mut out = exif.to_vec();
for v in values {
let one = v.one(order);
let slot = prefix.checked_add(v.at).and_then(|at| out.get_mut(at..at.checked_add(one.len())?));
if let Some(slot) = slot {
slot.copy_from_slice(&one);
}
}
Cow::Owned(out)
}
/// `xmp` with any `tiff:Orientation` (attribute or element form) set to 1,
/// borrowed when there is nothing to change.
pub fn upright_xmp(xmp: &str) -> Cow<'_, str> {
const KEY: &str = "tiff:Orientation";
if !xmp.contains(KEY) {
return Cow::Borrowed(xmp);
}
let mut out = String::with_capacity(xmp.len());
let mut rest = xmp;
let mut changed = false;
while let Some(i) = rest.find(KEY) {
let (head, tail) = rest.split_at(i + KEY.len());
out.push_str(head);
rest = tail;
// `tiff:Orientation="6"`, `tiff:Orientation='6'` or `<tiff:Orientation>6<`.
let open = rest.char_indices().take_while(|&(_, c)| c.is_ascii_whitespace() || matches!(c, '=' | '"' | '\'' | '>')).last();
let Some((at, c)) = open else { continue };
if !matches!(c, '"' | '\'' | '>') {
continue;
}
let start = at + 1;
let digits = rest.get(start..).map_or(0, |s| s.bytes().take_while(u8::is_ascii_digit).count());
if digits == 0 || rest.get(start..start + digits) == Some("1") {
continue;
}
out.push_str(rest.get(..start).unwrap_or_default());
out.push('1');
rest = rest.get(start + digits..).unwrap_or_default();
changed = true;
}
out.push_str(rest);
if changed { Cow::Owned(out) } else { Cow::Borrowed(xmp) }
/// TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped).
pub(crate) fn tiff_body(bytes: &[u8]) -> &[u8] {
bytes.strip_prefix(b"Exif\0\0").unwrap_or(bytes)
}
/// Source pixel index for output pixel `(0, y2)` and the step per output column, for
+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:?}");
@@ -223,6 +224,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");
}
}
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "craft-mcp"
description = "Transport-independent MCP server initialization and tool envelopes"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
craft-jsonrpc = { workspace = true }
serde_json = { workspace = true }
+17
View File
@@ -0,0 +1,17 @@
# craft-mcp
Transport-independent MCP 2025-03-26 server initialization and tool result helpers.
Use one `Session` per client connection with `craft-jsonrpc` for message parsing and
batch dispatch. The caller owns transport, tool definitions and execution, response
size limits, and shutdown. Pass the outer envelope's batch status to `Session::request`.
`Policy::Compatible` retains existing permissive clients: tool calls can precede
initialization, initialization can occur in a batch, and client information needs only
to be an object. `Policy::Strict` requires initialization first (except ping),
rejects batched or repeated initialization, and requires client name and version.
Both respond with the sole supported version, 2025-03-26, even if the client offered
a different version. An initialized notification moves the session to `Ready`.
This crate does not enforce every MCP lifecycle or capability rule; applications
must validate any additional requirements they depend on.
See the [MCP lifecycle specification](https://modelcontextprotocol.io/specification/2025-03-26/basic/lifecycle).
+275
View File
@@ -0,0 +1,275 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
//! MCP 2025-03-26 server initialization and tool envelopes, independent of transport and tools.
//! The caller owns routing, transport and JSON-RPC batching. Compatibility mode preserves servers
//! that accepted calls before initialization and initialize in a batch; strict mode enforces the
//! initialization ordering and batch prohibition. Create one session per connection.
use craft_jsonrpc::{Request, RpcError};
use serde_json::{Value, json};
pub const PROTOCOL_VERSION: &str = "2025-03-26";
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Policy {
/// Preserve existing permissive servers, including minimal client information.
#[default]
Compatible,
/// Enforce the initialize request and client identity required by MCP.
Strict,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum State {
#[default]
New,
AwaitingInitialized,
Ready,
}
/// State for one connection. Unknown requests are left to the caller's router.
pub struct Session {
name: String,
version: String,
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": {}}),
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.
pub fn with_capabilities(mut self, capabilities: Value) -> Result<Self, RpcError> {
if !capabilities.is_object() {
return Err(RpcError::new(-32602, "Capabilities must be an object"));
}
self.capabilities = capabilities;
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
}
/// Handles `initialize` and `ping`. Returns `None` for a request the app should route.
/// `in_batch` must describe the outer JSON-RPC envelope.
pub fn request(&mut self, request: Request<'_>, in_batch: bool) -> Option<Result<Value, RpcError>> {
if request.method == "ping" {
return Some(Ok(json!({})));
}
if request.method == "initialize" {
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()));
};
let valid = params.get("protocolVersion").and_then(Value::as_str).is_some()
&& params.get("capabilities").is_some_and(Value::is_object)
&& params.get("clientInfo").is_some_and(Value::is_object);
let identity = params.get("clientInfo").and_then(Value::as_object);
let strict_identity = identity
.is_some_and(|info| info.get("name").and_then(Value::as_str).is_some() && info.get("version").and_then(Value::as_str).is_some());
if !valid || (self.policy == Policy::Strict && !strict_identity) {
return Some(Err(invalid_initialize()));
}
self.state = State::AwaitingInitialized;
return Some(Ok(
json!({"protocolVersion": PROTOCOL_VERSION, "capabilities": self.capabilities, "serverInfo": {"name": self.name, "version": self.version}}),
));
}
if self.policy == Policy::Strict && self.state == State::New {
return Some(Err(RpcError::new(-32600, "Server not initialized")));
}
None
}
/// Observe a JSON-RPC notification; notifications never receive replies.
pub fn notification(&mut self, request: Request<'_>) {
if request.method == "notifications/initialized" {
self.initialized = true;
if self.state == State::AwaitingInitialized {
self.state = State::Ready;
}
}
}
}
fn invalid_initialize() -> RpcError {
RpcError::new(-32602, "Invalid initialize parameters")
}
/// Validate MCP tool-call parameters without owning the application's tool routing.
pub fn tool_call(params: &Value) -> Result<(&str, Value), RpcError> {
let name = params.get("name").and_then(Value::as_str).ok_or_else(|| RpcError::new(-32602, "Missing tool name"))?;
let arguments = params.get("arguments").cloned().unwrap_or_else(|| json!({}));
if !arguments.is_object() {
return Err(RpcError::new(-32602, "Tool arguments must be an object"));
}
Ok((name, arguments))
}
/// Wrap an application-provided tool list in the MCP result envelope.
pub fn tool_list(tools: Value) -> Value {
json!({"tools": tools})
}
/// Construct the text result currently used by Craft's MCP tools.
pub fn text_tool_result(text: &str, is_error: bool) -> Value {
json!({"content": [{"type": "text", "text": text}], "isError": is_error})
}
#[cfg(test)]
mod tests {
use super::*;
fn call<'a>(method: &'a str, params: Option<&'a Value>) -> Request<'a> {
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);
assert!(s.request(call("tools/list", None), false).is_none());
let p = json!({"protocolVersion":"old", "capabilities":{}, "clientInfo":{}});
let r = s.request(call("initialize", Some(&p)), true).unwrap().unwrap();
assert_eq!(r["protocolVersion"], PROTOCOL_VERSION);
assert_eq!(r["serverInfo"]["name"], "test");
assert_eq!(s.state(), State::AwaitingInitialized);
s.notification(call("notifications/initialized", None));
assert_eq!(s.state(), State::Ready);
}
#[test]
fn strict_rejects_batch_and_calls_before_initialization() {
let mut s = Session::new("test", "1", Policy::Strict);
assert_eq!(s.request(call("tools/list", None), false).unwrap().unwrap_err().code, -32600);
assert!(s.request(call("ping", None), false).unwrap().is_ok());
let p = json!({"protocolVersion":PROTOCOL_VERSION, "capabilities":{}, "clientInfo":{"name":"client", "version":"1"}});
assert!(s.request(call("initialize", Some(&p)), true).unwrap().is_err());
assert_eq!(s.state(), State::New);
assert!(s.request(call("initialize", Some(&p)), false).unwrap().is_ok());
assert!(s.request(call("initialize", Some(&p)), false).unwrap().is_err());
s.notification(call("notifications/initialized", None));
assert_eq!(s.state(), State::Ready);
}
#[test]
fn capabilities_are_configurable_and_must_be_an_object() {
assert!(Session::new("test", "1", Policy::Compatible).with_capabilities(json!([])).is_err());
let mut session = Session::new("test", "1", Policy::Compatible).with_capabilities(json!({"resources": {"subscribe": true}})).unwrap();
let params = json!({"protocolVersion": "2025-03-26", "capabilities": {}, "clientInfo": {}});
let result = session.request(call("initialize", Some(&params)), false).unwrap().unwrap();
assert_eq!(result["capabilities"], json!({"resources": {"subscribe": true}}));
}
#[test]
fn malformed_initialize_does_not_change_state() {
let mut s = Session::new("test", "1", Policy::Strict);
for p in [json!({}), json!({"protocolVersion":"x", "capabilities":{}, "clientInfo":{}})] {
assert_eq!(s.request(call("initialize", Some(&p)), false).unwrap().unwrap_err().code, -32602);
assert_eq!(s.state(), State::New);
}
}
#[test]
fn tool_envelopes_validate_arguments_and_preserve_results() {
let p = json!({"name":"open"});
assert_eq!(tool_call(&p).unwrap(), ("open", json!({})));
assert_eq!(tool_call(&json!({"name":"open", "arguments":[]})).unwrap_err().code, -32602);
assert_eq!(tool_call(&json!({})).unwrap_err().code, -32602);
assert_eq!(text_tool_result("bad", true), json!({"content":[{"type":"text", "text":"bad"}], "isError":true}));
}
#[test]
fn jsonrpc_batch_preserves_ids_and_skips_notifications() {
use std::cell::RefCell;
let session = RefCell::new(Session::new("test", "1", Policy::Compatible));
let input = json!([
{"jsonrpc":"2.0", "id":1, "method":"initialize", "params":{"protocolVersion":"2025-03-26", "capabilities":{}, "clientInfo":{}}},
{"jsonrpc":"2.0", "method":"notifications/initialized"},
{"jsonrpc":"2.0", "id":"tools", "method":"tools/list"}
]);
let reply = craft_jsonrpc::process(
&input,
craft_jsonrpc::Limits::default(),
|r| session.borrow_mut().request(r, true).unwrap_or_else(|| Ok(tool_list(json!([])))),
|r| session.borrow_mut().notification(r),
)
.unwrap()
.unwrap();
assert_eq!(reply.as_array().unwrap().len(), 2);
assert_eq!(reply[0]["id"], 1);
assert_eq!(reply[1]["id"], "tools");
assert_eq!(reply[1]["result"]["tools"], json!([]));
assert_eq!(session.borrow().state(), State::Ready);
}
}
+9 -2
View File
@@ -655,6 +655,7 @@ Implemented from the SMPTE documents (editions used):
| SMPTE ST 386 | 2004 | D-10 (IMX) mapping: container label, sound element type |
| SMPTE ST 2019-4 | 2009 | VC-3 (DNxHD / DNxHR) mapping |
| SMPTE RDD 44 | 2017 | Apple ProRes mapping |
| Adobe CinemaDNG Image Data Format Specification | 1.0.0.0 (September 2009), §7 | CinemaDNG in MXF: frame-wrapped DNG picture elements (element type 42h), the essence container labels for CFA / LinearRaw content, RGBA descriptor; the labels are the specification's **provisional** values from Adobe's experimental range (byte 9 = 0Fh), not SMPTE-registered |
| SMPTE RP 224 / RP 210 | registers | labels: data definitions, picture coding, essence containers, operational patterns; property labels of the primer pack (writer) |
| SMPTE ST 330 | 2011 | basic UMIDs of the written packages |
@@ -696,7 +697,10 @@ let pcm = mxf.read_pcm(&file, a, 48_000, 1920)?; // planar f32, sample-exact
orders by the bitstream (the AVC picture order counts).
- **Codecs.** From the picture essence coding label, then the essence container label, then the
essence bytes: AVC / AVC-Intra, VC-3, ProRes (profile from the label), MPEG-2,
MPEG-4 visual, JPEG 2000, DV, uncompressed.
MPEG-4 visual, JPEG 2000, DV, uncompressed, CinemaDNG (the CinemaDNG container labels, the
registered MXF-GC TIFF/EP container and TIFF/EP picture coding labels, a 42h picture element
type, or a TIFF header with the DNGVersion tag at the start of the essence; the DNG pictures
themselves are decoded by `craft-raw`).
- **Sound.** PCM chunks (frame- or clip-wrapped) with sample counts; `read_pcm` decodes
little-endian 8/16/24/32-bit PCM (wave / AES3 descriptors) and ST 331 AES3 elements (D-10).
- **Timecode.** The material package timecode component (start frame count, rounded base,
@@ -769,7 +773,10 @@ w.finish()?;
element types 0x0C / 0x0D), ProRes (RDD 44: coding label profile byte, container `02 1C`),
AVC byte stream (ST 381-3: container `02 10 60`, coding label from `profile_idc`; Annex B access
units with in-band SPS / PPS — the AVC sub-descriptor is not written), Broadcast Wave PCM
(ST 382: container `02 06 01/02`, element types 0x01 / 0x02).
(ST 382: container `02 06 01/02`, element types 0x01 / 0x02), CinemaDNG (frame-wrapped DNG
datasets, element type 0x42, the provisional container label `0F 0A 0D 0B 02 7F 01/02 00`, an
RGBA descriptor with pixel layout and reference levels of the decoded 16-bit RGB picture and no
coding label; OP-Atom is refused because it is clip-wrapped here and DNG datasets cannot be).
- `write_opatom_pcm(&OpAtomPcm, &[i32])` builds a complete in-memory OP-Atom PCM file (AAF media
consolidation); `encode_pcm` interleaves planar f32 as little-endian PCM.
+95 -3
View File
@@ -27,6 +27,9 @@ pub enum Codec {
Dv,
/// Uncompressed pictures (ST 384).
Uncompressed,
/// CinemaDNG: one DNG (TIFF) dataset per frame-wrapped picture element (CinemaDNG Image Data
/// Format Specification §7), or TIFF/EP pictures under the registered MXF-GC TIFF/EP labels.
CinemaDng,
/// Linear PCM, little-endian (Broadcast Wave and AES3 descriptors, ST 382).
Pcm,
/// 8-channel AES3 element data (ST 331, D-10 sound).
@@ -46,13 +49,14 @@ impl Codec {
Codec::Jpeg2000 => "JPEG 2000",
Codec::Dv => "DV",
Codec::Uncompressed => "Uncompressed",
Codec::CinemaDng => "CinemaDNG",
Codec::Pcm => "PCM",
Codec::Aes3Element => "AES3 PCM",
Codec::Unknown => "unknown",
}
}
/// The crate-wide identifier: VC-3 is [`CodecId::Dnxhd`], PCM and AES3 element data are
/// [`CodecId::Pcm`]; JPEG 2000, DV and uncompressed pictures have no identifier.
/// [`CodecId::Pcm`]; JPEG 2000, DV, uncompressed and CinemaDNG pictures have no identifier.
///
/// [`CodecId::Dnxhd`]: crate::CodecId::Dnxhd
/// [`CodecId::Pcm`]: crate::CodecId::Pcm
@@ -65,15 +69,35 @@ impl Codec {
Codec::Mpeg2 => Id::Mpeg2Video,
Codec::Mpeg4Visual => Id::Mpeg4Video,
Codec::Pcm | Codec::Aes3Element => Id::Pcm,
Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed | Codec::Unknown => Id::Unknown,
Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed | Codec::CinemaDng | Codec::Unknown => Id::Unknown,
}
}
/// Every picture is coded independently.
pub fn intra_only(&self) -> bool {
matches!(self, Codec::Avc { intra: true } | Codec::Vc3 | Codec::ProRes { .. } | Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed)
matches!(
self,
Codec::Avc { intra: true } | Codec::Vc3 | Codec::ProRes { .. } | Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed | Codec::CinemaDng
)
}
}
/// The CinemaDNG essence container labels (CinemaDNG Image Data Format Specification 1.0.0.0,
/// §7.2.1): provisional, unregistered labels from Adobe's experimental range (byte 9 = 0F h),
/// `…0F 0A 0D 0B 02 7F cc 00` with content kind `cc` 01 (CFA) or 02 (LinearRaw).
pub const CINEMADNG_CONTAINER_PREFIX: [u8; 6] = [0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F];
/// Element type (byte 15 of the picture element key) of a frame-wrapped CinemaDNG picture: the
/// "TIFF" type of ST 331 (CinemaDNG specification §7.1.1.2).
pub const CINEMADNG_ELEMENT_TYPE: u8 = 0x42;
/// Whether `ul` is a CinemaDNG essence container label (CFA or LinearRaw content), or the
/// registered MXF-GC TIFF/EP container (`…0D 01 03 01 02 14 ww 00`, RP 224).
pub fn is_cinemadng_container(ul: &Ul) -> bool {
ul.is_smpte()
&& (ul.item_starts_with(&CINEMADNG_CONTAINER_PREFIX) && matches!(ul.0[14], 0x01 | 0x02)
|| ul.item_starts_with(&[0x0D, 0x01, 0x03, 0x01, 0x02, 0x14]))
}
/// Identify a picture codec from the picture essence coding label and the essence container label.
pub fn identify_picture(coding: Option<Ul>, container: Option<Ul>, mpeg_descriptor: bool) -> Codec {
if let Some(c) = coding.filter(Ul::is_smpte) {
@@ -81,6 +105,9 @@ pub fn identify_picture(coding: Option<Ul>, container: Option<Ul>, mpeg_descript
// 04 01 02 02 .. : compressed picture coding
if b[8..12] == [0x04, 0x01, 0x02, 0x02] {
match (b[12], b[13]) {
// TIFF/EP Profile 2 picture coding (RP 224: 03 02 01 01 uncompressed CFA … 04
// compressed LinearRaw), the coding of DNG pictures
(0x03, 0x02) if b[14] == 0x01 => return Codec::CinemaDng,
// 01: MPEG compression (ISO/IEC 13818-2 profiles, 14496-2, 14496-10)
(0x01, 0x01..=0x1F) => return Codec::Mpeg2,
(0x01, 0x20..=0x2F) => return Codec::Mpeg4Visual,
@@ -99,6 +126,9 @@ pub fn identify_picture(coding: Option<Ul>, container: Option<Ul>, mpeg_descript
return Codec::Uncompressed;
}
}
if container.is_some_and(|c| is_cinemadng_container(&c)) {
return Codec::CinemaDng;
}
if let Some(c) = container.filter(Ul::is_smpte)
&& c.item_starts_with(&[0x0D, 0x01, 0x03, 0x01, 0x02])
{
@@ -117,8 +147,38 @@ pub fn identify_picture(coding: Option<Ul>, container: Option<Ul>, mpeg_descript
if mpeg_descriptor { Codec::Mpeg2 } else { Codec::Unknown }
}
/// Read a TIFF `u16` at offset `o`. The end offset is computed with checked arithmetic, so an
/// untrusted offset near `usize::MAX` (a 32-bit target with IFD offset `0xFFFF_FFFF`) is
/// out of bounds instead of overflowing.
fn tiff_u16(b: &[u8], le: bool, o: usize) -> Option<u16> {
let v = b.get(o..o.checked_add(2)?)?;
Some(if le { u16::from_le_bytes([v[0], v[1]]) } else { u16::from_be_bytes([v[0], v[1]]) })
}
/// DNG evidence in the leading bytes of a TIFF: a TIFF 6.0 §2 header whose first IFD, when it lies
/// within `b`, holds the DNGVersion tag (DNG 1.x §2, tag 50706). A plain TIFF has no such tag.
fn is_dng(b: &[u8]) -> bool {
let le = match b.get(..4) {
Some(h) if h == b"II*\0" => true,
Some(h) if h == b"MM\0*" => false,
_ => return false,
};
let u16_at = |o: usize| tiff_u16(b, le, o);
let Some(ifd) = b.get(4..8).map(|v| if le { u32::from_le_bytes([v[0], v[1], v[2], v[3]]) } else { u32::from_be_bytes([v[0], v[1], v[2], v[3]]) })
else {
return false;
};
let Some(count) = usize::try_from(ifd).ok().and_then(|o| u16_at(o).map(|n| (o, n))) else {
return false;
};
(0..usize::from(count.1)).any(|i| u16_at(count.0 + 2 + i * 12) == Some(50706))
}
/// Identify a picture codec from the first bytes of an essence element.
pub fn sniff_picture(b: &[u8]) -> Option<Codec> {
if is_dng(b) {
return Some(Codec::CinemaDng);
}
if b.len() >= 8 && &b[4..8] == b"icpf" {
return Some(Codec::ProRes { profile: None });
}
@@ -367,6 +427,29 @@ mod tests {
assert_eq!(identify_picture(None, None, false), Codec::Unknown);
}
#[test]
fn tiff_offset_near_usize_max_is_out_of_bounds() {
let b = [0x49, 0x49, 0x2A, 0x00, 0xFF, 0xFF, 0xFF, 0xFF];
assert_eq!(tiff_u16(&b, true, usize::MAX), None);
assert_eq!(tiff_u16(&b, true, usize::MAX - 1), None);
assert_eq!(tiff_u16(&b, true, 0), Some(0x4949));
assert!(!is_dng(&b));
}
#[test]
fn identifies_cinemadng() {
// CinemaDNG specification §7.2.1: CFA and LinearRaw containers (any registry version byte)
let adobe = |kind: u8| Ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F, kind, 0x00]);
assert_eq!(identify_picture(None, Some(adobe(1)), false), Codec::CinemaDng);
assert_eq!(identify_picture(None, Some(adobe(2)), false), Codec::CinemaDng);
assert_eq!(identify_picture(None, Some(adobe(3)), false), Codec::Unknown);
// RP 224: MXF-GC frame-wrapped TIFF/EP container, TIFF/EP picture coding
assert_eq!(identify_picture(None, Some(ul(&[0x0D, 1, 3, 1, 2, 0x14, 1])), false), Codec::CinemaDng);
assert_eq!(identify_picture(Some(ul(&[4, 1, 2, 2, 3, 2, 1, 3])), None, false), Codec::CinemaDng);
assert!(Codec::CinemaDng.intra_only());
assert_eq!(Codec::CinemaDng.name(), "CinemaDNG");
}
#[test]
fn sniffs_essence() {
assert_eq!(sniff_picture(&[0, 0, 0, 1, 0x09, 0x10]), Some(Codec::Avc { intra: false }));
@@ -374,6 +457,15 @@ mod tests {
assert_eq!(sniff_picture(&[0, 0, 2, 0x80, 1]), Some(Codec::Vc3));
assert_eq!(sniff_picture(&[0, 0, 0, 9, b'i', b'c', b'p', b'f']), Some(Codec::ProRes { profile: None }));
assert_eq!(sniff_picture(&[1, 2, 3]), None);
// a TIFF header alone is not DNG; an IFD0 with the DNGVersion tag (50706 = 0xC612) is
let tiff_le = |tag: u16| [b"II*\0\x08\0\0\0\x01\0".as_slice(), &tag.to_le_bytes(), &[1, 0, 4, 0, 0, 0, 1, 4, 0, 0], &[0; 8]].concat();
let tiff_be = |tag: u16| [b"MM\0*\0\0\0\x08\0\x01".as_slice(), &tag.to_be_bytes(), &[0, 1, 0, 0, 0, 4, 1, 4, 0, 0], &[0; 8]].concat();
assert_eq!(sniff_picture(&tiff_le(0xC612)), Some(Codec::CinemaDng));
assert_eq!(sniff_picture(&tiff_be(0xC612)), Some(Codec::CinemaDng));
assert_eq!(sniff_picture(&tiff_le(0x0100)), None);
assert_eq!(sniff_picture(&tiff_be(0x0100)), None);
assert_eq!(sniff_picture(b"II*\0\x08\0\0\0"), None);
assert_eq!(sniff_picture(b"II*\0"), None);
}
#[test]
+56 -5
View File
@@ -571,8 +571,32 @@ fn descriptor_kind(s: &Set) -> Option<TrackKind> {
}
}
// PictureInfo has one depth for all color components. Do not invent one for a
// malformed layout or unequal RGB precisions. PixelLayout is eight code/size pairs.
fn rgba_component_depth(layout: &[u8]) -> u32 {
if layout.len() != 16 {
return 0;
}
let mut depths = [0u8; 3];
for pair in layout.as_chunks::<2>().0 {
let index = match pair[0] {
b'R' => 0,
b'G' => 1,
b'B' => 2,
0 => break,
_ => continue,
};
if depths[index] != 0 || pair[1] == 0 {
return 0;
}
depths[index] = pair[1];
}
if depths[0] != 0 && depths.iter().all(|&depth| depth == depths[0]) { u32::from(depths[0]) } else { 0 }
}
fn picture_info(d: &Set) -> PictureInfo {
let u = |t| d.u(t).unwrap_or(0) as u32;
let rgba = d.kind() == Some(st::RGBA_DESCRIPTOR);
PictureInfo {
stored_width: u(tag::STORED_WIDTH),
stored_height: u(tag::STORED_HEIGHT),
@@ -581,13 +605,13 @@ fn picture_info(d: &Set) -> PictureInfo {
frame_layout: u(tag::FRAME_LAYOUT) as u8,
field_dominance: u(tag::FIELD_DOMINANCE) as u8,
aspect_ratio: d.rational(tag::ASPECT_RATIO).unwrap_or_default(),
component_depth: u(tag::COMPONENT_DEPTH),
component_depth: if rgba { d.get(tag::PIXEL_LAYOUT).map_or(0, rgba_component_depth) } else { u(tag::COMPONENT_DEPTH) },
horizontal_subsampling: u(tag::HORIZONTAL_SUBSAMPLING),
vertical_subsampling: u(tag::VERTICAL_SUBSAMPLING),
black_ref_level: d.u(tag::BLACK_REF_LEVEL).map(|v| v as u32),
white_ref_level: d.u(tag::WHITE_REF_LEVEL).map(|v| v as u32),
black_ref_level: d.u(if rgba { tag::COMPONENT_MIN_REF } else { tag::BLACK_REF_LEVEL }).map(|v| v as u32),
white_ref_level: d.u(if rgba { tag::COMPONENT_MAX_REF } else { tag::WHITE_REF_LEVEL }).map(|v| v as u32),
alpha_depth: u(tag::ALPHA_SAMPLE_DEPTH),
rgba: d.kind() == Some(st::RGBA_DESCRIPTOR),
rgba,
picture_coding: d.ul(tag::PICTURE_ESSENCE_CODING),
transfer_characteristic: d.ul(tag::TRANSFER_CHARACTERISTIC),
coding_equations: d.ul(tag::CODING_EQUATIONS),
@@ -910,7 +934,7 @@ fn build_track(
}
// A wrong or missing label: trust the essence bytes.
if let Some(first) = elements.first() {
let head = read_upto(src, first.value_offset, 64)?;
let head = read_upto(src, first.value_offset, 4096)?;
if let Some(sniffed) = sniff_picture(&head) {
let agree = std::mem::discriminant(&sniffed) == std::mem::discriminant(&codec);
if !agree && !matches!(codec, Codec::Avc { intra: true }) {
@@ -918,6 +942,12 @@ fn build_track(
}
}
}
// CinemaDNG §7.1.1.2: a "TIFF" picture element type names DNG pictures
if codec == Codec::Unknown
&& elements.first().is_some_and(|e| e.key.0[12] == 0x15 && e.key.0[14] == super::essence::CINEMADNG_ELEMENT_TYPE)
{
codec = Codec::CinemaDng;
}
t.codec = codec;
t.picture = Some(pi);
build_pictures(src, &mut t, elements, index, warnings)?;
@@ -1337,3 +1367,24 @@ mod fallback_tracks_tests {
assert_eq!(tracks.len(), 1);
}
}
#[cfg(test)]
mod rgba_layout_tests {
use super::rgba_component_depth;
#[test]
fn only_complete_uniform_rgb_layouts_have_one_depth() {
let mut layout = [0; 16];
layout[..8].copy_from_slice(&[b'B', 16, b'G', 16, b'R', 16, b'A', 8]);
assert_eq!(rgba_component_depth(&layout), 16);
assert_eq!(rgba_component_depth(&layout[..15]), 0);
layout[3] = 8;
assert_eq!(rgba_component_depth(&layout), 0);
layout[3] = 16;
layout[4] = b'G';
assert_eq!(rgba_component_depth(&layout), 0);
layout[4] = 0;
assert_eq!(rgba_component_depth(&layout), 0);
assert_eq!(rgba_component_depth(&[0; 16]), 0);
}
}
+2
View File
@@ -204,6 +204,8 @@ pub mod tag {
pub const WHITE_REF_LEVEL: u16 = 0x3305;
pub const ALPHA_SAMPLE_DEPTH: u16 = 0x3309;
pub const PIXEL_LAYOUT: u16 = 0x3401;
pub const COMPONENT_MAX_REF: u16 = 0x3406;
pub const COMPONENT_MIN_REF: u16 = 0x3407;
pub const AUDIO_SAMPLING_RATE: u16 = 0x3D03;
pub const LOCKED: u16 = 0x3D02;
pub const CHANNEL_COUNT: u16 = 0x3D07;
+3 -2
View File
@@ -2,7 +2,8 @@
//! ST 377-1 (file format: KLV, partitions, primer pack, header metadata, index tables, random
//! index pack), ST 378 (OP1a), ST 390 (OP-Atom), ST 379-1/2 (generic container), ST 381-1 (MPEG
//! video mapping), ST 381-3 (AVC mapping), ST 382 (AES3 / Broadcast Wave audio mapping), ST 331
//! (AES3 element data, D-10 sound), ST 2019-4 (VC-3 mapping) and RDD 44 (ProRes mapping).
//! (AES3 element data, D-10 sound), ST 2019-4 (VC-3 mapping), RDD 44 (ProRes mapping) and the CinemaDNG
//! specification's MXF mapping (frame-wrapped DNG pictures).
//!
//! [`open`] walks the file's KLV packets (keys and lengths only; essence payloads are never read
//! while opening), parses the partition packs, the primer pack and header metadata of the most
@@ -32,7 +33,7 @@ mod meta;
pub mod write;
pub use crate::source::{ByteSource, ReadSeekSource};
pub use essence::{Codec, PictureInfo, SoundFormat, SoundInfo, aes3_element_samples, decode_pcm};
pub use essence::{CINEMADNG_CONTAINER_PREFIX, CINEMADNG_ELEMENT_TYPE, Codec, PictureInfo, SoundFormat, SoundInfo, aes3_element_samples, decode_pcm};
pub use file::{Chunk, EssenceTrack, MxfFile, OperationalPattern, Partition, PartitionKind, Sample, Timecode, TrackKind, Wrapping, open};
pub use index::{IndexEntry, IndexSegment};
pub use klv::{Rational, Ul};
+47 -7
View File
@@ -107,6 +107,10 @@ pub enum PictureCoding {
ProRes { profile: u8 },
/// H.264 / AVC Annex B byte stream; `profile_idc` from the SPS (66, 77, 100, …).
Avc { profile_idc: u8, intra: bool },
/// CinemaDNG: one DNG dataset per picture (CinemaDNG specification §7), frame-wrapped only,
/// with an RGBA descriptor of the decoded 16-bit RGB image. `linear_raw`: the pictures are
/// LinearRaw (PhotometricInterpretation 34892) rather than CFA (32803).
CinemaDng { linear_raw: bool },
}
impl PictureCoding {
@@ -116,9 +120,10 @@ impl PictureCoding {
_ => true,
}
}
/// Picture essence coding label.
fn coding_label(&self) -> [u8; 16] {
match *self {
/// Picture essence coding label (None: CinemaDNG, which has no registered one, §7.2.2.2).
fn coding_label(&self) -> Option<[u8; 16]> {
Some(match *self {
PictureCoding::CinemaDng { .. } => return None,
PictureCoding::Vc3 { cid } => {
let code = if (1235..=1234 + 0x7F).contains(&cid) { (cid - 1234) as u8 } else { 0 };
[0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x0A, 0x04, 0x01, 0x02, 0x02, 0x71, code, 0x00, 0x00]
@@ -145,7 +150,7 @@ impl PictureCoding {
};
[0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x0A, 0x04, 0x01, 0x02, 0x02, 0x01, b13, b14, 0x01]
}
}
})
}
/// (essence container label, element type) for frame or clip wrapping.
fn container(&self, clip: bool) -> ([u8; 16], u8) {
@@ -154,6 +159,12 @@ impl PictureCoding {
PictureCoding::Vc3 { .. } => (essence_container(0x0A, 0x11, w, 0x00), if clip { 0x0D } else { 0x0C }),
PictureCoding::ProRes { .. } => (essence_container(0x0D, 0x1C, w, 0x00), if clip { 0x18 } else { 0x17 }),
PictureCoding::Avc { .. } => (essence_container(0x0A, 0x10, 0x60, w), if clip { 0x16 } else { 0x15 }),
// CinemaDNG §7.2.1 (provisional label) and §7.1.1.2 (element type 42h); always frame
// wrapped (`MxfWriter::new` refuses clip wrapping)
PictureCoding::CinemaDng { linear_raw } => (
[0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F, if *linear_raw { 0x02 } else { 0x01 }, 0x00],
0x42,
),
}
}
}
@@ -202,6 +213,8 @@ impl PictureDesc {
if matches!(cid, 1235 | 1241 | 1250 | 1256 | 1270 | 1271) { 10 } else { 8 },
if matches!(cid, 1256 | 1270) { (1, 1) } else { (2, 1) },
),
// §7.2.2.3: the decoded image is described as 16 bits per component
PictureCoding::CinemaDng { .. } => (16, (1, 1)),
};
PictureDesc {
coding,
@@ -589,6 +602,19 @@ impl<W: Write + Seek> MxfWriter<W> {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid PCM sound description"));
}
let clip = cfg.pattern == Pattern::OpAtom;
if clip && matches!(cfg.picture.as_ref().map(|p| p.coding), Some(PictureCoding::CinemaDng { .. })) {
// CinemaDNG §7.1.1: a TIFF dataset has no length field, so DNG pictures can't be clip wrapped
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"CinemaDNG pictures are frame wrapped only (this writer's OP-Atom is clip wrapped)",
));
}
if let Some(p) = &cfg.picture {
// CinemaDNG §7.2.2.3 fixes the decoded image at 16 bits per component
if matches!(p.coding, PictureCoding::CinemaDng { .. }) && p.depth != 16 {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "CinemaDNG pictures are described at 16 bits per component"));
}
}
let mut tracks = Vec::new();
let mut id = cfg.first_track_id.max(2);
if let Some(p) = &cfg.picture {
@@ -985,7 +1011,8 @@ impl<W: Write + Seek> MxfWriter<W> {
TrackKind::Picture => {
// Picture tracks exist only with a picture configuration.
let Some(p) = cfg.picture.as_ref() else { continue };
let mut s = Set::new(0x28, uid);
let rgba = matches!(p.coding, PictureCoding::CinemaDng { .. });
let mut s = Set::new(if rgba { 0x29 } else { 0x28 }, uid);
if multiple {
s.u32(0x3006, t.id);
}
@@ -1020,8 +1047,21 @@ impl<W: Write + Seek> MxfWriter<W> {
// field dominance: the temporally first field (1 top, 2 bottom)
s.u8(0x3212, if p.bottom_field_first { 2 } else { 1 });
}
s.p(0x3201, &p.coding.coding_label())
.p(0x3210, &tcl)
if let Some(label) = p.coding.coding_label() {
s.p(0x3201, &label);
}
if rgba {
// RGBA descriptor (ST 377-1 G.2.40): pixel layout R, G, B of 16 bits each and
// the component reference levels. CinemaDNG §7.2.2.3 fixes the decoded
// image at 16 bits; `MxfWriter::new` rejects any other `PictureDesc::depth`.
let mut layout = [0u8; 16];
layout[..6].copy_from_slice(&[b'R', 16, b'G', 16, b'B', 16]);
let max = u32::from(u16::MAX);
s.p(0x3401, &layout).u32(0x3406, max).u32(0x3407, 0);
s.write(&mut out);
continue;
}
s.p(0x3210, &tcl)
.p(0x321A, &ce)
.p(0x3219, &cp)
.u32(0x3301, depth)
+4
View File
@@ -120,6 +120,10 @@ pub const PRIMER: &[(u16, [u8; 16])] = &[
(0x3304, ul(&[0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x05, 0x03, 0x03])),
(0x3305, ul(&[0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x05, 0x03, 0x04])),
(0x3306, ul(&[0x01, 0x01, 0x01, 0x02, 0x04, 0x01, 0x05, 0x03, 0x05])),
// RGBA picture essence descriptor (ST 377-1; RP 210): PixelLayout, ComponentMaxRef, ComponentMinRef
(0x3401, ul(&[0x01, 0x01, 0x01, 0x02, 0x04, 0x01, 0x05, 0x03, 0x06])),
(0x3406, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x05, 0x03, 0x0B])),
(0x3407, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x05, 0x03, 0x0C])),
(0x3D03, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x02, 0x03, 0x01, 0x01, 0x01])),
(0x3D02, ul(&[0x01, 0x01, 0x01, 0x04, 0x04, 0x02, 0x03, 0x01, 0x04])),
(0x3D07, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x02, 0x01, 0x01, 0x04])),
+54
View File
@@ -401,6 +401,60 @@ fn long_pcm_reads_backed_by_the_file_are_not_capped() {
assert!(m.read_pcm(&f, 0, 0, frames + (1 << 24)).is_err());
}
/// A TIFF-headed stand-in for a DNG dataset (the demuxer only carries the bytes).
fn dng_like(i: usize) -> Vec<u8> {
let mut v = b"II*\0\x08\0\0\0".to_vec();
v.extend(std::iter::repeat_n(i as u8 ^ 0x33, 300 + 11 * i));
v
}
#[test]
fn cinemadng_op1a_reads_back() {
for linear_raw in [false, true] {
let mut cfg = op1a_config(Rational::new(24, 1), PictureCoding::CinemaDng { linear_raw });
cfg.sound.truncate(1);
let mut w = MxfWriter::new(Cursor::new(Vec::new()), cfg).unwrap();
for i in 0..6 {
w.push_picture(&dng_like(i), FrameInfo::intra()).unwrap();
w.push_sound(0, &pcm16(i * 2000, 2000)).unwrap();
}
let f = w.finish().unwrap().into_inner();
// CinemaDNG §7.1.1.2: picture element key 06.0E.2B.34.01.02.01.01.0D.01.03.01.15.01.42.01
let key = [0x06, 0x0E, 0x2B, 0x34, 0x01, 0x02, 0x01, 0x01, 0x0D, 0x01, 0x03, 0x01, 0x15, 0x01, 0x42, 0x01];
assert_eq!(f.windows(16).filter(|k| *k == key).count(), 6);
// §7.2.1: the essence container label of the content kind
let ec = [0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F, if linear_raw { 2 } else { 1 }, 0x00];
assert!(f.windows(16).any(|k| k == ec));
let m = open(&f).unwrap();
assert!(m.warnings.is_empty(), "{:?}", m.warnings);
let v = m.track_of_kind(TrackKind::Picture).unwrap();
let t = &m.tracks[v];
assert_eq!(t.codec, Codec::CinemaDng);
assert_eq!(t.wrapping, Wrapping::Frame);
assert_eq!(t.samples.len(), 6);
let p = t.picture.as_ref().unwrap();
assert!(p.rgba, "RGBA picture essence descriptor (§7.2.2)");
assert_eq!(p.component_depth, 16);
assert_eq!(p.black_ref_level, Some(0));
assert_eq!(p.white_ref_level, Some(65_535));
assert!(p.full_range());
assert_eq!((p.frame_width(), p.frame_height()), (64, 36));
assert!(p.picture_coding.is_none());
for i in 0..6 {
assert!(t.samples[i].key);
assert_eq!(m.read_sample(&f, v, i).unwrap(), dng_like(i));
}
assert_eq!(m.timecode.unwrap().format(), "01:02:30:00");
}
}
#[test]
fn cinemadng_is_never_clip_wrapped() {
let mut cfg = WriterConfig::new(Pattern::OpAtom, Rational::new(24, 1), PackageIds::from_seed("t", "T"));
cfg.picture = Some(PictureDesc::new(PictureCoding::CinemaDng { linear_raw: false }, 64, 36));
assert!(MxfWriter::new(Cursor::new(Vec::new()), cfg).is_err());
}
/// One `push_sound` of a long clip must not cost more than the clip itself: draining the buffered
/// PCM from the front for every edit unit made this quadratic (about 16 s for 10 minutes).
#[test]
+179 -36
View File
@@ -84,22 +84,83 @@ pub fn embedded(bytes: &[u8]) -> Embedded {
const XMP_SIG: &[u8] = b"http://ns.adobe.com/xap/1.0/\0";
const XMP_EXT_SIG: &[u8] = b"http://ns.adobe.com/xmp/extension/\0";
/// JPEG frame, marker and metadata details collected before the first scan.
#[derive(Debug, Default)]
pub struct JpegInspection {
/// Stored frame width.
pub width: u32,
/// Stored frame height.
pub height: u32,
/// Number of frame components.
pub components: u8,
/// Sample precision.
pub precision: u8,
/// Start-of-frame marker byte.
pub sof: u8,
/// Components in the first scan.
pub first_scan_components: u8,
/// Number of APP1 segments.
pub app1: u32,
/// First EXIF TIFF payload.
pub exif: Option<Vec<u8>>,
/// Offset of the EXIF TIFF payload.
pub exif_offset: usize,
/// First standard XMP packet.
pub xmp: Option<String>,
/// Reassembled ICC profile.
pub icc: Option<Vec<u8>>,
/// ICC chunks were malformed or inconsistent.
pub icc_invalid: bool,
/// Adobe APP14 transform byte.
pub adobe_transform: Option<u8>,
/// MPF TIFF offset and payload.
pub mpf: Option<(usize, Vec<u8>)>,
/// ISO gain-map APP2 payload following its identifier.
pub iso_gainmap: Option<Vec<u8>>,
valid: bool,
}
impl JpegInspection {
/// Whether metadata identifies this image as a gain map.
pub fn is_gain_map(&self) -> bool {
self.iso_gainmap.as_ref().is_some_and(|p| p.len() > 4) || self.xmp.as_deref().is_some_and(|x| x.contains("hdrgm:GainMapMax"))
}
}
/// Inspect markers before the first scan, tolerating garbage between segments and truncated
/// final payloads. Returns `None` for a missing SOI or an invalid segment length.
pub fn inspect_jpeg(b: &[u8]) -> Option<JpegInspection> {
if !b.starts_with(&[0xff, 0xd8]) {
return None;
}
let (embedded, _, mut inspection) = jpeg_segments_inner(b, false, true);
if !inspection.valid {
return None;
}
inspection.exif = embedded.exif;
inspection.xmp = embedded.xmp.map(|x| x.trim_end_matches('\0').to_string());
inspection.icc = embedded.icc;
Some(inspection)
}
/// Walk JPEG markers up to SOS and collect APP1 Exif / XMP, APP2 ICC and APP13 IPTC.
pub fn jpeg_segments(b: &[u8]) -> Embedded {
jpeg_segments_inner(b, false).0
jpeg_segments_inner(b, false, false).0
}
/// Walk JPEG metadata and return every ordinary XMP APP1 packet in segment order, along with the
/// usual [`Embedded`] projection (which retains only the first ordinary packet).
pub fn jpeg_segments_with_all_xmp(b: &[u8]) -> (Embedded, Vec<String>) {
jpeg_segments_inner(b, true)
let (embedded, packets, _) = jpeg_segments_inner(b, true, false);
(embedded, packets)
}
fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec<String>) {
fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embedded, Vec<String>, JpegInspection) {
let mut out = Embedded::default();
let mut xmp_packets = Vec::new();
let mut inspection = JpegInspection { valid: true, ..Default::default() };
if !b.starts_with(&[0xff, 0xd8]) {
return (out, xmp_packets);
return (out, xmp_packets, inspection);
}
let mut icc_parts: [Option<&[u8]>; 255] = [None; 255];
let mut icc_total = None;
@@ -110,6 +171,10 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec<String
let mut i = 2usize;
while i + 4 <= b.len() {
if b[i] != 0xff {
if inspect {
i += 1;
continue;
}
break;
}
let marker = b[i + 1];
@@ -122,10 +187,14 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec<String
continue;
}
if marker == 0xd9 || marker == 0xda {
if inspect && marker == 0xda {
inspection.first_scan_components = b.get(i + 4).copied().unwrap_or(0);
}
break;
}
let len = u16::from_be_bytes([b[i + 2], b[i + 3]]) as usize;
if len < 2 {
inspection.valid = false;
break;
}
let Some(start) = i.checked_add(4) else { break };
@@ -133,8 +202,13 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec<String
let complete = declared_end <= b.len();
let end = declared_end.min(b.len());
let Some(payload) = b.get(start..end) else { break };
if inspect {
inspect_marker(&mut inspection, marker, payload, start, out.exif.is_none());
}
match marker {
0xe1 if payload.starts_with(b"Exif\0") && out.exif.is_none() => out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()),
0xe1 if payload.starts_with(if inspect { b"Exif\0\0" } else { b"Exif\0" }) && out.exif.is_none() => {
out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec())
}
0xe1 if payload.starts_with(XMP_SIG) && out.xmp.is_none() => {
let packet = String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned();
if collect_all_xmp {
@@ -145,7 +219,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec<String
0xe1 if payload.starts_with(XMP_SIG) && collect_all_xmp => {
xmp_packets.push(String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned());
}
0xe1 if payload.starts_with(XMP_EXT_SIG) => {
0xe1 if !inspect && payload.starts_with(XMP_EXT_SIG) => {
// GUID (32) + full length (4) + offset (4) + data
let p = &payload[XMP_EXT_SIG.len()..];
if p.len() >= 40 {
@@ -156,35 +230,9 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec<String
}
}
0xe2 if payload.starts_with(b"ICC_PROFILE\0") => {
if !complete || payload.len() < 14 {
icc_invalid = true;
} else if let Some(header) = payload.get(12..14) {
let (Some(&sequence), Some(&total)) = (header.first(), header.get(1)) else {
icc_invalid = true;
i = end;
continue;
};
if sequence == 0 || total == 0 || sequence > total || icc_total.is_some_and(|expected| expected != total) {
icc_invalid = true;
} else {
icc_total = Some(total);
if let Some(slot) = icc_parts.get_mut(usize::from(sequence - 1)) {
if slot.is_some() {
icc_invalid = true;
} else if let Some(data) = payload.get(14..) {
*slot = Some(data);
} else {
icc_invalid = true;
}
} else {
icc_invalid = true;
}
}
} else {
icc_invalid = true;
}
collect_icc_part(payload, complete, &mut icc_parts, &mut icc_total, &mut icc_invalid);
}
0xed if payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]),
0xed if !inspect && payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]),
_ => {}
}
i = end;
@@ -214,8 +262,61 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec<String
out.icc = Some(profile);
}
}
attach_extension(&mut out, b, ext, &ext_headers);
(out, xmp_packets)
inspection.icc_invalid = icc_invalid;
if !inspect {
attach_extension(&mut out, b, ext, &ext_headers);
}
(out, xmp_packets, inspection)
}
fn collect_icc_part<'a>(payload: &'a [u8], complete: bool, parts: &mut [Option<&'a [u8]>; 255], total: &mut Option<u8>, invalid: &mut bool) {
if !complete || payload.len() < 14 {
*invalid = true;
return;
}
let (sequence, count) = (payload[12], payload[13]);
if sequence == 0 || count == 0 || sequence > count || total.is_some_and(|expected| expected != count) {
*invalid = true;
return;
}
*total = Some(count);
let Some(slot) = parts.get_mut(usize::from(sequence - 1)) else {
*invalid = true;
return;
};
if slot.is_some() {
*invalid = true;
} else {
*slot = payload.get(14..);
}
}
fn inspect_marker(info: &mut JpegInspection, marker: u8, payload: &[u8], start: usize, needs_exif: bool) {
if (0xc0..=0xcf).contains(&marker) && !matches!(marker, 0xc4 | 0xc8 | 0xcc) && payload.len() >= 6 && info.components == 0 {
info.precision = payload[0];
info.sof = marker;
info.height = u32::from(u16::from_be_bytes([payload[1], payload[2]]));
info.width = u32::from(u16::from_be_bytes([payload[3], payload[4]]));
info.components = payload[5];
}
if marker == 0xe1 {
info.app1 = info.app1.saturating_add(1);
}
if marker == 0xe1 && payload.starts_with(b"Exif\0\0") && needs_exif {
info.exif_offset = start + 6;
}
if marker == 0xe2 && payload.starts_with(b"MPF\0") && info.mpf.is_none() {
info.mpf = Some((start + 4, payload[4..].to_vec()));
}
if marker == 0xe2
&& info.iso_gainmap.is_none()
&& let Some(data) = payload.strip_prefix(b"urn:iso:std:iso:ts:21496:-1\0")
{
info.iso_gainmap = Some(data.to_vec());
}
if marker == 0xee && payload.starts_with(b"Adobe") && payload.len() >= 12 {
info.adobe_transform = Some(payload[11]);
}
}
/// Put the Extended XMP of JPEG `b` into `out`: when the standard packet links one, only that GUID's
@@ -865,3 +966,45 @@ mod tests {
}
}
}
#[cfg(test)]
mod jpeg_inspection_tests {
use super::*;
fn segment(out: &mut Vec<u8>, marker: u8, data: &[u8]) {
out.extend_from_slice(&[0xff, marker]);
out.extend_from_slice(&((data.len() as u16) + 2).to_be_bytes());
out.extend_from_slice(data);
}
#[test]
fn reports_mpf_gainmap_and_adobe_transform() {
let mut b = vec![0xff, 0xd8];
segment(&mut b, 0xe2, b"MPF\0IIabc");
segment(&mut b, 0xe2, b"urn:iso:std:iso:ts:21496:-1\0abcde");
segment(&mut b, 0xee, b"Adobe\0\0\0\0\0\0\x02");
b.extend_from_slice(&[0xff, 0xd9]);
let info = inspect_jpeg(&b).unwrap();
let (offset, data) = info.mpf.unwrap();
assert_eq!(&b[offset..offset + data.len()], data);
assert_eq!(info.iso_gainmap.as_deref(), Some(&b"abcde"[..]));
assert_eq!(info.adobe_transform, Some(2));
}
#[test]
fn tolerant_walk_keeps_offsets_frame_and_icc_chunks() {
let mut b = vec![0xff, 0xd8, 0x44];
segment(&mut b, 0xe1, b"Exif\0\0II");
segment(&mut b, 0xe2, b"ICC_PROFILE\0\x02\x02B");
segment(&mut b, 0xe2, b"ICC_PROFILE\0\x01\x02A");
segment(&mut b, 0xc0, &[8, 0, 2, 0, 3, 3]);
b.extend_from_slice(&[0xff, 0xda, 0, 3, 3]);
let info = inspect_jpeg(&b).unwrap();
assert_eq!((info.width, info.height, info.components, info.first_scan_components), (3, 2, 3, 3));
assert_eq!(info.app1, 1);
assert_eq!(&b[info.exif_offset..info.exif_offset + 2], b"II");
assert_eq!(info.icc.as_deref(), Some(&b"AB"[..]));
assert!(!info.icc_invalid);
// The existing strict API still stops at the garbage byte.
assert!(jpeg_segments(&b).exif.is_none());
assert!(inspect_jpeg(&[0xff, 0xd8, 0xff, 0xe1, 0, 1]).is_none());
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ pub mod tags;
mod xmp;
mod xmp_merge;
pub use container::{Embedded, embedded, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks};
pub use container::{Embedded, JpegInspection, embedded, inspect_jpeg, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks};
pub use datetime::{DatePrecision, DateTime};
pub use exif::{from_tiff, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif};
pub use focus::FocusPoint;
+35 -1
View File
@@ -113,7 +113,40 @@ pub fn images(jpeg: &[u8]) -> Vec<MpfImage> {
};
// offsets count from the byte-order field, which follows the signature
let Some(base) = segs.iter().find(|s| jpeg.get(s.1..s.2) == Some(payload)).map(|s| s.1 + SIGNATURE.len()) else { return Vec::new() };
let Some(tiff) = payload.get(SIGNATURE.len()..).and_then(|t| Tiff::parse(t).ok()) else { return Vec::new() };
let Some(index) = payload.get(SIGNATURE.len()..) else { return Vec::new() };
parse_index(index, base)
}
/// Read an already located MPF TIFF index. `base` is the index's offset in the containing file.
/// Entries are bounded to [`MAX_ENTRIES`] and returned without reading image payloads.
pub fn images_from_index(index: &[u8], base: usize) -> Vec<MpfImage> {
// The bounded legacy reader requires a complete classic first IFD, while the generic TIFF
// parser can salvage partial directories. Preserve that refusal for located MPF indices.
let le = match index.get(..2) {
Some(b"II") => true,
Some(b"MM") => false,
_ => return Vec::new(),
};
let read16 =
|at: usize| index.get(at..at.checked_add(2)?).map(|b| if le { u16::from_le_bytes([b[0], b[1]]) } else { u16::from_be_bytes([b[0], b[1]]) });
let read32 = |at: usize| {
index
.get(at..at.checked_add(4)?)
.map(|b| if le { u32::from_le_bytes([b[0], b[1], b[2], b[3]]) } else { u32::from_be_bytes([b[0], b[1], b[2], b[3]]) })
};
if read16(2) != Some(42) {
return Vec::new();
}
let Some(at) = read32(4).and_then(|v| usize::try_from(v).ok()) else { return Vec::new() };
let Some(count) = read16(at).map(usize::from).filter(|n| *n <= 4096) else { return Vec::new() };
if at.checked_add(2).and_then(|p| count.checked_mul(12).and_then(|n| p.checked_add(n))).is_none_or(|end| end > index.len()) {
return Vec::new();
}
parse_index(index, base)
}
fn parse_index(index: &[u8], base: usize) -> Vec<MpfImage> {
let Some(tiff) = Tiff::parse(index).ok() else { return Vec::new() };
let Some(list) = tiff.ifds.first().and_then(|i| i.bytes(MP_ENTRY)) else { return Vec::new() };
let u32_at = |rec: &[u8], at: usize| rec.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).map(|s| tiff.order.u32(s) as usize);
let mut out = Vec::new();
@@ -226,6 +259,7 @@ mod tests {
let segs = segments(&file).unwrap();
assert_eq!(segs.iter().map(|s| s.0).collect::<Vec<_>>(), [0xe0, 0xe1, 0xe2, 0xdb]);
let (_, a, e) = segs[2];
assert_eq!(images_from_index(&file[a + SIGNATURE.len()..e], a + SIGNATURE.len()), found);
let mut without = file[..a - 4].to_vec();
without.extend_from_slice(&file[e..found[0].offset]);
assert_eq!(without, primary);
+19 -6
View File
@@ -8,8 +8,9 @@
//! counts it; an unpadded one refuses `=`), a length that can be, and the unused bits of the last
//! symbol zero (so every value has one spelling). A caller that reads mail, where line breaks and
//! missing padding are everyday, cleans its input first, as it always has; the strictness is what
//! the signature, key and credential checks want. No input panics, and the output is never longer
//! than three quarters of the input.
//! the signature, key and credential checks want. [`Config::decode_optional_padding`] explicitly
//! permits missing terminal padding without relaxing alphabet, padding placement or unused bits.
//! No input panics, and the output is never longer than three quarters of the input.
use std::fmt;
@@ -108,9 +109,20 @@ impl Config {
/// The bytes of `text`, which must be exactly base64 of this config's kind (see the module).
pub fn decode(self, text: impl AsRef<[u8]>) -> Result<Vec<u8>, DecodeError> {
let text = text.as_ref();
let (symbols, pad) = if self.padded {
if text.len() % 4 != 0 {
self.decode_inner(text.as_ref(), false)
}
/// Decode this alphabet with zero, partial or complete terminal padding, regardless of the
/// config's encoding padding. Whitespace, junk, internal/excess padding and nonzero unused
/// bits still fail. Use this only when the calling format permits missing padding; [`Self::decode`]
/// remains strict for signatures and credentials that require one spelling.
pub fn decode_optional_padding(self, text: impl AsRef<[u8]>) -> Result<Vec<u8>, DecodeError> {
self.decode_inner(text.as_ref(), true)
}
fn decode_inner(self, text: &[u8], optional_padding: bool) -> Result<Vec<u8>, DecodeError> {
let (symbols, pad) = if self.padded || optional_padding {
if !optional_padding && !text.len().is_multiple_of(4) {
return Err(DecodeError);
}
let pad = text.iter().rev().take(2).take_while(|c| **c == b'=').count();
@@ -120,7 +132,8 @@ impl Config {
};
// A last group of one symbol holds under eight bits; with padding, `=` fills it to four.
let rest = symbols.len() % 4;
if rest == 1 || (self.padded && pad != (4 - rest) % 4) {
let needed = (4 - rest) % 4;
if rest == 1 || (optional_padding && pad > needed) || (!optional_padding && self.padded && pad != needed) {
return Err(DecodeError);
}
let mut out = Vec::with_capacity(symbols.len().saturating_mul(3) / 4);
+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);
}
}
}
+42
View File
@@ -79,3 +79,45 @@ proptest! {
}
}
}
/// Missing padding is opt-in; other canonical validation stays unchanged.
#[test]
fn optional_padding_keeps_alphabet_placement_and_tail_bit_checks() {
for cfg in CONFIGS {
for (input, expected) in [("", ""), ("Zg", "f"), ("Zg=", "f"), ("Zg==", "f"), ("Zm8", "fo"), ("Zm8=", "fo"), ("Zm9v", "foo")] {
assert_eq!(cfg.decode_optional_padding(input).unwrap(), expected.as_bytes());
}
for bad in ["=", "====", "Z", "Z===", "Zg===", "Zm8==", "Zm9v=", "QQ==QUJD", "Zm=v", "Zh", "Zh=", "Zh==", "Zm9", "Zm9=", "Zg\n", " Zg", "é"]
{
assert!(cfg.decode_optional_padding(bad).is_err(), "{cfg:?}: {bad:?}");
}
}
assert_eq!(STANDARD.decode_optional_padding("+/8").unwrap(), [251, 255]);
assert_eq!(URL_SAFE.decode_optional_padding("-_8=").unwrap(), [251, 255]);
assert!(STANDARD.decode_optional_padding("-_8=").is_err());
assert!(URL_SAFE.decode_optional_padding("+/8=").is_err());
assert!(STANDARD.decode("Zg=").is_err());
assert!(STANDARD_NO_PAD.decode("Zg==").is_err());
}
proptest! {
#[test]
fn optional_padding_accepts_only_a_suffix_of_required_padding(
data in proptest::collection::vec(any::<u8>(), 0..400),
junk in proptest::collection::vec(any::<u8>(), 0..128),
) {
for cfg in [STANDARD, URL_SAFE] {
let encoded = cfg.encode(&data);
let bare = encoded.trim_end_matches('=');
for length in bare.len()..=encoded.len() {
prop_assert_eq!(cfg.decode_optional_padding(&encoded[..length]).ok(), Some(data.clone()));
}
if let Ok(decoded) = cfg.decode_optional_padding(&junk) {
let canonical = cfg.encode(decoded);
let bare = canonical.trim_end_matches('=');
prop_assert!(junk.len() >= bare.len() && junk.len() <= canonical.len());
prop_assert_eq!(canonical.as_bytes().get(..junk.len()), Some(junk.as_slice()));
}
}
}
}
+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;
+116
View File
@@ -0,0 +1,116 @@
//! Bounded two-plane PSD Displace (`DspD`) payloads.
//! App filter settings, image conversion and external-path policy belong to the caller.
//! Ported from PhotoCraft `crates/io/src/displace_map.rs` (MIT/Apache-2.0).
const HEADER: usize = 10;
const MODE_TWO_PLANES: u16 = 3;
/// Maximum supported decoded pixel count (8K by 8K).
pub const MAX_MAP_PIXELS: usize = 1 << 26;
/// A parsed embedded map: size and the two 8-bit planes (borrowed from the blob).
#[derive(Debug, PartialEq)]
pub struct EmbeddedMap<'a> {
/// Width in pixels.
pub width: usize,
/// Height in pixels.
pub height: usize,
/// Horizontal displacement samples.
pub horizontal: &'a [u8],
/// Vertical displacement samples.
pub vertical: &'a [u8],
}
/// Parses an embedded displacement map; `None` for any other mode, a zero or oversized map, or a
/// blob shorter than the header plus both planes.
pub fn parse_map(data: &[u8]) -> Option<EmbeddedMap<'_>> {
let u32_at = |i: usize| Some(u32::from_le_bytes(data.get(i..i + 4)?.try_into().ok()?));
let height = usize::try_from(u32_at(0)?).ok()?;
let width = usize::try_from(u32_at(4)?).ok()?;
let mode = u16::from_le_bytes(data.get(8..HEADER)?.try_into().ok()?);
let n = width.checked_mul(height)?;
if mode != MODE_TWO_PLANES || n == 0 || n > MAX_MAP_PIXELS {
return None;
}
let horizontal = data.get(HEADER..HEADER.checked_add(n)?)?;
let vertical = data.get(HEADER + n..HEADER.checked_add(n.checked_mul(2)?)?)?;
Some(EmbeddedMap { width, height, horizontal, vertical })
}
/// Writes a mode-3 payload from exact-sized 8-bit planes, without padding.
/// Returns `None` for empty/oversized dimensions, mismatched planes or allocation failure.
/// Callers retain their own (possibly lower) embedding and image-conversion budgets.
pub fn write_map(width: usize, height: usize, horizontal: &[u8], vertical: &[u8]) -> Option<Vec<u8>> {
write_map_iter(width, height, horizontal.iter().copied(), vertical.iter().copied())
}
/// Writes exact-sized sample iterators without allocating intermediate planes.
/// Iterators must terminate after exactly width times height samples; extra or missing
/// samples are rejected. At most that many samples plus one are consumed per plane.
pub fn write_map_iter(
width: usize,
height: usize,
mut horizontal: impl Iterator<Item = u8>,
mut vertical: impl Iterator<Item = u8>,
) -> Option<Vec<u8>> {
let n = width.checked_mul(height)?;
if n == 0 || n > MAX_MAP_PIXELS {
return None;
}
let width = u32::try_from(width).ok()?;
let height = u32::try_from(height).ok()?;
let len = HEADER.checked_add(n.checked_mul(2)?)?;
let mut out = Vec::new();
out.try_reserve_exact(len).ok()?;
out.extend_from_slice(&height.to_le_bytes());
out.extend_from_slice(&width.to_le_bytes());
out.extend_from_slice(&MODE_TWO_PLANES.to_le_bytes());
for samples in [&mut horizontal as &mut dyn Iterator<Item = u8>, &mut vertical] {
for _ in 0..n {
out.push(samples.next()?);
}
if samples.next().is_some() {
return None;
}
}
Some(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_planes_roundtrip_and_padding_is_ignored() {
let mut bytes = write_map(2, 1, &[0, 255], &[128, 64]).unwrap();
assert_eq!(bytes, [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 255, 128, 64]);
bytes.extend_from_slice(&[99, 88]);
let m = parse_map(&bytes).unwrap();
assert_eq!((m.width, m.height), (2, 1));
assert_eq!(m.horizontal, &[0, 255]);
assert_eq!(m.vertical, &[128, 64]);
}
#[test]
fn truncated_unsupported_and_overflow_are_rejected() {
let bytes = write_map(2, 1, &[0, 255], &[128, 64]).unwrap();
for end in 0..bytes.len() {
assert!(parse_map(&bytes[..end]).is_none());
}
let mut invalid = bytes.clone();
invalid[8] = 4;
assert!(parse_map(&invalid).is_none());
invalid[8] = 3;
invalid[..8].fill(255);
assert!(parse_map(&invalid).is_none());
invalid[..8].fill(0);
assert!(parse_map(&invalid).is_none());
invalid[..4].copy_from_slice(&1u32.to_le_bytes());
invalid[4..8].copy_from_slice(&((MAX_MAP_PIXELS + 1) as u32).to_le_bytes());
assert!(parse_map(&invalid).is_none());
assert!(write_map_iter(2, 1, std::iter::repeat(0), std::iter::repeat(0)).is_none());
assert!(write_map(usize::MAX, 2, &[], &[]).is_none());
assert!(write_map(MAX_MAP_PIXELS + 1, 1, &[], &[]).is_none());
assert!(write_map(2, 1, &[0], &[0, 0]).is_none());
assert!(write_map(2, 1, &[0, 0], &[0, 0, 0]).is_none());
}
}
+151
View File
@@ -0,0 +1,151 @@
//! Flat PSD export from borrowed interleaved samples, without an image-sized staging buffer.
use crate::{PsdError, Result};
/// Display-encoded samples, interleaved by pixel.
#[derive(Clone, Copy)]
pub enum Samples<'a> {
/// Eight-bit samples.
U8(&'a [u8]),
/// Sixteen-bit samples.
U16(&'a [u16]),
}
/// Optional resources embedded in a flat PSD.
#[derive(Clone, Copy, Default)]
pub struct Metadata<'a> {
/// ICC profile bytes.
pub icc: Option<&'a [u8]>,
/// TIFF-structured EXIF bytes.
pub exif: Option<&'a [u8]>,
/// XMP document text.
pub xmp: Option<&'a str>,
/// Resolution in pixels per inch.
pub ppi: Option<u16>,
}
/// Serialize a flat PSD. Input channels are gray, gray-alpha, RGB or RGBA; alpha is discarded.
/// Eight-bit rows use PackBits; sixteen-bit samples are stored raw and big-endian. The output
/// buffer and one eight-bit row are held at once; there is no image-sized pixel copy.
pub fn write(width: u32, height: u32, channels: u8, samples: Samples<'_>, meta: Metadata<'_>) -> Result<Vec<u8>> {
if width == 0 || height == 0 || width > 30_000 || height > 30_000 || !(1..=4).contains(&channels) {
return Err(PsdError::invalid("invalid flat PSD dimensions or channels"));
}
let n = (width as usize).checked_mul(height as usize).ok_or(PsdError::LimitExceeded("image dimensions"))?;
let ch = usize::from(channels);
let needed = n.checked_mul(ch).ok_or(PsdError::LimitExceeded("sample count"))?;
let (depth, have) = match samples {
Samples::U8(s) => (8u16, s.len()),
Samples::U16(s) => (16u16, s.len()),
};
if have < needed {
return Err(PsdError::invalid("sample buffer too short"));
}
let color = if ch <= 2 { 1usize } else { 3 };
let mut out = Vec::new();
out.extend_from_slice(b"8BPS");
out.extend_from_slice(&1u16.to_be_bytes());
out.extend_from_slice(&[0; 6]);
out.extend_from_slice(&(color as u16).to_be_bytes());
out.extend_from_slice(&height.to_be_bytes());
out.extend_from_slice(&width.to_be_bytes());
out.extend_from_slice(&depth.to_be_bytes());
out.extend_from_slice(&(if color == 1 { 1u16 } else { 3 }).to_be_bytes());
out.extend_from_slice(&0u32.to_be_bytes());
let mut resources = Vec::new();
if let Some(ppi) = meta.ppi.filter(|p| *p > 0) {
let fixed = (u32::from(ppi) << 16).to_be_bytes();
let one = 1u16.to_be_bytes();
resource(&mut resources, 1005, &[&fixed[..], &one, &one, &fixed, &one, &one].concat())?;
}
if let Some(data) = meta.icc {
resource(&mut resources, 1039, data)?;
}
if let Some(data) = meta.exif {
resource(&mut resources, 1058, data)?;
}
if let Some(data) = meta.xmp {
resource(&mut resources, 1060, data.as_bytes())?;
}
out.extend_from_slice(&u32::try_from(resources.len()).map_err(|_| PsdError::LimitExceeded("resource section"))?.to_be_bytes());
out.extend_from_slice(&resources);
out.extend_from_slice(&0u32.to_be_bytes());
match samples {
Samples::U8(src) => {
out.extend_from_slice(&1u16.to_be_bytes());
let rows = color.checked_mul(height as usize).ok_or(PsdError::LimitExceeded("row count"))?;
let table = out.len();
let table_end =
table.checked_add(rows.checked_mul(2).ok_or(PsdError::LimitExceeded("row table"))?).ok_or(PsdError::LimitExceeded("row table"))?;
out.try_reserve(rows * 2).map_err(|_| PsdError::OutOfMemory("row table"))?;
out.resize(table_end, 0);
let mut row = Vec::new();
row.try_reserve_exact(width as usize).map_err(|_| PsdError::OutOfMemory("row"))?;
for c in 0..color {
for y in 0..height as usize {
row.clear();
let start = y * width as usize * ch;
row.extend(src[start + c..start + width as usize * ch].iter().step_by(ch).copied());
let before = out.len();
crate::compression::packbits::encode(&row, &mut out);
let count = u16::try_from(out.len() - before).map_err(|_| PsdError::LimitExceeded("PSD row length"))?;
let at = table + (c * height as usize + y) * 2;
out[at..at + 2].copy_from_slice(&count.to_be_bytes());
}
}
}
Samples::U16(src) => {
out.extend_from_slice(&0u16.to_be_bytes());
for c in 0..color {
for sample in src.iter().take(needed).skip(c).step_by(ch) {
out.extend_from_slice(&sample.to_be_bytes());
}
}
}
}
Ok(out)
}
fn resource(out: &mut Vec<u8>, id: u16, data: &[u8]) -> Result<()> {
let len = u32::try_from(data.len()).map_err(|_| PsdError::LimitExceeded("resource length"))?;
out.extend_from_slice(b"8BIM");
out.extend_from_slice(&id.to_be_bytes());
out.extend_from_slice(&[0, 0]);
out.extend_from_slice(&len.to_be_bytes());
out.extend_from_slice(data);
if !data.len().is_multiple_of(2) {
out.push(0);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn writes_rgb_eight_bit_rows_and_preserves_resources() {
let bytes = write(
2,
2,
4,
Samples::U8(&[10, 20, 30, 0, 40, 50, 60, 1, 70, 80, 90, 2, 100, 110, 120, 3]),
Metadata { icc: Some(&[1, 2, 3]), xmp: Some("<x/>"), ppi: Some(300), ..Metadata::default() },
)
.unwrap();
let psd = crate::PsdFile::from_bytes(&bytes).unwrap();
assert_eq!(psd.header.channels, 3);
assert_eq!(psd.decode_merged().unwrap(), [10, 40, 70, 100, 20, 50, 80, 110, 30, 60, 90, 120]);
assert!(psd.resources.iter().any(|r| r.id == 1039 && r.data == [1, 2, 3]));
assert!(psd.resources.iter().any(|r| r.id == 1060 && r.data == b"<x/>"));
}
#[test]
fn writes_raw_sixteen_bit_gray_and_rejects_short_input() {
let bytes = write(2, 1, 2, Samples::U16(&[1, 999, 0xabcd, 0]), Metadata::default()).unwrap();
let psd = crate::PsdFile::from_bytes(&bytes).unwrap();
assert_eq!(psd.header.channels, 1);
assert_eq!(psd.decode_merged().unwrap(), [0, 1, 0xab, 0xcd]);
assert!(write(2, 2, 3, Samples::U8(&[0; 11]), Metadata::default()).is_err());
assert!(write(30_001, 1, 1, Samples::U8(&[]), Metadata::default()).is_err());
}
}
+103
View File
@@ -0,0 +1,103 @@
//! PSD Grid and Guides version 2 (resource 1092) record decoding.
//!
//! Extracted from PhotoCraft `guides_map.rs` at f49564d281bf8892db58ed154767717223aff4f6
//! by Bram Heerink. Document colour preference and resource-retention policy belong to callers.
//! Unknown direction values retain PhotoCraft's vertical interpretation. Grid cycles, reserved
//! bytes and trailing data are not interpreted. Resource 1097 remains opaque except its count.
/// Grid and Guides version 2 resource ID.
pub const GUIDES_V2: u16 = 1092;
/// Associated guide identifiers resource ID.
pub const GUIDE_IDS: u16 = 1097;
/// One version 2 guide record.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct GuideV2 {
/// Signed position in units of 1/32 pixel.
pub location: i32,
/// True for vertical guides (direction byte other than 1).
pub vertical: bool,
/// Stored RGB colour; interpretation of preference colours is app policy.
pub rgb: [u8; 3],
}
fn be_u32(data: &[u8], at: usize) -> Option<u32> {
let b = data.get(at..at.checked_add(4)?)?;
Some(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
}
/// Validate the full declared record region before returning an allocation-free iterator.
/// Returns `None` for an unsupported version, short header or truncated records.
/// Record count cannot cause allocation or work beyond the supplied byte slice.
pub fn parse_v2(data: &[u8]) -> Option<impl ExactSizeIterator<Item = GuideV2> + '_> {
if be_u32(data, 0)? != 2 {
return None;
}
let count = usize::try_from(be_u32(data, 12)?).ok()?;
let end = 16usize.checked_add(count.checked_mul(9)?)?;
let records = data.get(16..end)?;
Some(records.as_chunks::<9>().0.iter().map(|g| GuideV2 {
location: i32::from_be_bytes([g[0], g[1], g[2], g[3]]),
vertical: g[4] != 1,
rgb: [g[6], g[7], g[8]],
}))
}
/// Only the declared entry count in resource 1097; this does not validate its GUID payload.
pub fn ids_count(data: &[u8]) -> Option<u32> {
be_u32(data, 0)
}
#[cfg(test)]
mod tests {
use super::*;
fn fixture() -> Vec<u8> {
let mut v = Vec::new();
for n in [2u32, 576, 576, 2] {
v.extend_from_slice(&n.to_be_bytes());
}
for (position, direction, rgb) in [(512i32, 0u8, [74, 255, 255]), (-32, 1, [1, 2, 3])] {
v.extend_from_slice(&position.to_be_bytes());
v.extend_from_slice(&[direction, 0]);
v.extend_from_slice(&rgb);
}
v
}
#[test]
fn decodes_signed_positions_directions_and_colours() {
let data = fixture();
let actual = parse_v2(&data).map(Iterator::collect::<Vec<_>>);
assert_eq!(
actual,
Some(vec![GuideV2 { location: 512, vertical: true, rgb: [74, 255, 255] }, GuideV2 { location: -32, vertical: false, rgb: [1, 2, 3] },])
);
}
#[test]
fn validates_every_truncation_and_hostile_count_before_iteration() {
let mut data = fixture();
for n in 0..data.len() {
assert!(parse_v2(&data[..n]).is_none());
}
data[12..16].copy_from_slice(&u32::MAX.to_be_bytes());
assert!(parse_v2(&data).is_none());
data[12..16].copy_from_slice(&0u32.to_be_bytes());
assert_eq!(parse_v2(&data).map(|v| v.len()), Some(0));
data[3] = 1;
assert!(parse_v2(&data).is_none());
}
#[test]
fn preserves_unknown_direction_reserved_bytes_and_trailing_data_policy() {
let mut data = fixture();
data[20] = 255;
data[21] = 42;
data.extend_from_slice(&[9; 12]);
assert_eq!(parse_v2(&data).and_then(|mut v| v.next()).map(|g| g.vertical), Some(true));
assert_eq!(parse_v2(&data).map(|v| v.len()), Some(2));
assert_eq!(ids_count(&[0, 0, 0, 2, 255]), Some(2));
assert_eq!(ids_count(&[0, 0, 0]), None);
}
}
+4
View File
@@ -64,14 +64,17 @@ mod codepage;
pub mod compression;
pub mod csh;
pub mod descriptor;
pub mod displace_map;
pub mod engine_data;
pub mod error;
pub mod file;
pub mod filter_effects;
#[cfg(any(test, feature = "testgen"))]
pub mod fixtures;
pub mod flat;
pub mod float;
pub mod grd;
pub mod guides;
pub mod hdr;
pub mod head;
pub mod header;
@@ -79,6 +82,7 @@ pub mod image_data;
mod io;
pub mod layer;
mod lazy;
pub mod merged;
pub mod metadata;
pub mod path;
pub mod patterns;
+251
View File
@@ -0,0 +1,251 @@
//! Borrowed, merged-only PSD reader that skips layer records and decodes selected planes.
use crate::{Head, PsdError, Result};
/// Dimensions in the fixed header fields, for reporting a preflight limit error.
/// This does not validate the PSD signature or remaining header.
pub fn declared_dimensions(bytes: &[u8]) -> Option<(u32, u32)> {
let h = bytes.get(14..18)?;
let w = bytes.get(18..22)?;
Some((u32::from_be_bytes([w[0], w[1], w[2], w[3]]), u32::from_be_bytes([h[0], h[1], h[2], h[3]])))
}
/// The file head and merged-composite location. Layer records are not parsed or allocated.
pub struct Merged<'a> {
/// Header, color-mode data, and image resources.
pub head: Head,
/// Whether the layer count marks merged transparency.
pub transparency: bool,
/// Merged image compression identifier.
pub compression: u16,
bytes: &'a [u8],
data_offset: usize,
}
fn take<'a>(bytes: &'a [u8], at: &mut usize, len: usize) -> Result<&'a [u8]> {
let end = at.checked_add(len).ok_or(PsdError::LimitExceeded("offset"))?;
let data = bytes.get(*at..end).ok_or_else(|| PsdError::invalid("truncated"))?;
*at = end;
Ok(data)
}
fn u16_at(bytes: &[u8], at: &mut usize) -> Result<u16> {
let b = take(bytes, at, 2)?;
Ok(u16::from_be_bytes([b[0], b[1]]))
}
fn u32_at(bytes: &[u8], at: &mut usize) -> Result<u32> {
let b = take(bytes, at, 4)?;
Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
}
fn u64_at(bytes: &[u8], at: &mut usize) -> Result<u64> {
let b = take(bytes, at, 8)?;
Ok(u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]))
}
impl<'a> Merged<'a> {
/// Open the merged section with a pixel limit. This skips layer records, preserving the ability
/// to read a composite when its layer data is not understood. No image planes are allocated.
pub fn open(bytes: &'a [u8], max_pixels: u64) -> Result<Self> {
// Refuse a declared oversized canvas before copying color-mode data or resources.
if let Some((w, h)) = declared_dimensions(bytes)
&& u64::from(w) * u64::from(h) > max_pixels
{
return Err(PsdError::LimitExceeded("pixel count"));
}
let head = Head::from_bytes(bytes)?;
let h = &head.header;
if u64::from(h.width) * u64::from(h.height) > max_pixels {
return Err(PsdError::LimitExceeded("pixel count"));
}
let psb = h.version.is_psb();
let mut at = usize::try_from(head.layer_section_offset).map_err(|_| PsdError::LimitExceeded("offset"))?;
let len = if psb { u64_at(bytes, &mut at)? } else { u64::from(u32_at(bytes, &mut at)?) };
let layer = take(bytes, &mut at, usize::try_from(len).map_err(|_| PsdError::LimitExceeded("layer section"))?)?;
let offset = if psb { 8 } else { 4 };
let transparency = layer.get(offset..offset + 2).is_some_and(|b| i16::from_be_bytes([b[0], b[1]]) < 0);
let compression = u16_at(bytes, &mut at)?;
Ok(Self { head, transparency, compression, bytes, data_offset: at })
}
/// Check that all declared raw data or RLE row payloads fit without decoding them.
pub fn validate_all(&self) -> Result<()> {
let h = &self.head.header;
let bpc = match h.depth {
8 => 1usize,
16 => 2,
32 => 4,
_ => return Err(PsdError::Unsupported("merged depth".into())),
};
let channels = usize::from(h.channels);
let height = h.height as usize;
let mut at = self.data_offset;
match self.compression {
0 => {
let len = (h.width as usize)
.checked_mul(height)
.and_then(|n| n.checked_mul(bpc))
.and_then(|n| n.checked_mul(channels))
.ok_or(PsdError::LimitExceeded("merged data"))?;
take(self.bytes, &mut at, len)?;
}
1 => {
let rows = channels.checked_mul(height).ok_or(PsdError::LimitExceeded("row count"))?;
let entry = if h.version.is_psb() { 4 } else { 2 };
let table_len = rows.checked_mul(entry).ok_or(PsdError::LimitExceeded("row table"))?;
let table = take(self.bytes, &mut at, table_len)?;
let mut total = 0usize;
for chunk in table.chunks_exact(entry) {
let len = if entry == 2 {
usize::from(u16::from_be_bytes([chunk[0], chunk[1]]))
} else {
u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]) as usize
};
total = total.saturating_add(len);
}
take(self.bytes, &mut at, total)?;
}
_ => return Err(PsdError::Unsupported("merged compression".into())),
}
Ok(())
}
/// Decode only the first `used` planes, leaving spot channels untouched. Eight-, sixteen-, and
/// thirty-two-bit raw or PackBits samples are returned as planar big-endian bytes. Before any
/// RLE plane allocation, encoded row lengths must fit the input and the PackBits expansion bound.
pub fn read_planes(&self, used: usize) -> Result<Vec<Vec<u8>>> {
let h = &self.head.header;
let channels = usize::from(h.channels);
if used > channels {
return Err(PsdError::invalid("too few channels"));
}
let bpc = match h.depth {
8 => 1usize,
16 => 2,
32 => 4,
_ => return Err(PsdError::Unsupported("merged depth".into())),
};
let width = h.width as usize;
let height = h.height as usize;
let row = width.checked_mul(bpc).ok_or(PsdError::LimitExceeded("row size"))?;
let plane = row.checked_mul(height).ok_or(PsdError::LimitExceeded("plane size"))?;
let mut at = self.data_offset;
let mut planes = Vec::new();
planes.try_reserve_exact(used).map_err(|_| PsdError::OutOfMemory("planes"))?;
match self.compression {
0 => {
for _ in 0..used {
planes.push(take(self.bytes, &mut at, plane)?.to_vec());
}
}
1 => {
let entry = if h.version.is_psb() { 4 } else { 2 };
let table_len = channels.checked_mul(height).and_then(|v| v.checked_mul(entry)).ok_or(PsdError::LimitExceeded("row table"))?;
let table = take(self.bytes, &mut at, table_len)?;
let count = |i: usize| -> usize {
let pos = i.saturating_mul(entry);
match table.get(pos..pos.saturating_add(entry)) {
Some(&[a, b]) => usize::from(u16::from_be_bytes([a, b])),
Some(&[a, b, c, d]) => u32::from_be_bytes([a, b, c, d]) as usize,
_ => 0,
}
};
let min_row = row.div_ceil(128).saturating_mul(2);
let mut total = 0usize;
for i in 0..used.saturating_mul(height) {
let n = count(i);
if n < min_row {
return Err(PsdError::invalid("RLE row too short for its width"));
}
total = total.saturating_add(n);
}
if total > self.bytes.len().saturating_sub(at) {
return Err(PsdError::invalid("truncated"));
}
for c in 0..used {
let mut decoded = Vec::new();
decoded.try_reserve_exact(plane).map_err(|_| PsdError::OutOfMemory("plane"))?;
decoded.resize(plane, 0);
for (y, row) in decoded.chunks_exact_mut(row).enumerate() {
let encoded = take(self.bytes, &mut at, count(c * height + y))?;
unpackbits(encoded, row)?;
}
planes.push(decoded);
}
}
_ => return Err(PsdError::Unsupported("merged compression".into())),
}
Ok(planes)
}
}
/// Decode one PackBits row. Truncated packets leave the pre-zeroed remainder unchanged; packets
/// that overrun the output are rejected. Trailing encoded data after the row is ignored.
pub fn unpackbits(mut src: &[u8], row: &mut [u8]) -> Result<()> {
let mut at = 0;
while at < row.len() {
let Some((&h, rest)) = src.split_first() else { break };
src = rest;
let h = h as i8;
if h >= 0 {
let n = h as usize + 1;
let lit = src.get(..n).unwrap_or(src);
let dst = row.get_mut(at..at + lit.len()).ok_or_else(|| PsdError::invalid("RLE row overruns its width"))?;
dst.copy_from_slice(lit);
at += lit.len();
src = src.get(n..).unwrap_or(&[]);
} else if h != -128 {
let n = (1 - h as isize) as usize;
let Some((&b, rest)) = src.split_first() else { break };
src = rest;
row.get_mut(at..at + n).ok_or_else(|| PsdError::invalid("RLE row overruns its width"))?.fill(b);
at += n;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn head(channels: u16, compression: u16) -> Vec<u8> {
let mut b = b"8BPS\0\x01\0\0\0\0\0\0".to_vec();
b.extend_from_slice(&channels.to_be_bytes());
b.extend_from_slice(&2u32.to_be_bytes());
b.extend_from_slice(&2u32.to_be_bytes());
b.extend_from_slice(&16u16.to_be_bytes());
b.extend_from_slice(&1u16.to_be_bytes());
b.extend_from_slice(&0u32.to_be_bytes());
b.extend_from_slice(&0u32.to_be_bytes());
b.extend_from_slice(&0u32.to_be_bytes());
b.extend_from_slice(&compression.to_be_bytes());
b
}
#[test]
fn reads_only_selected_rle_plane_without_extra_channel_data() {
let mut b = head(2, 1);
for _ in 0..4 {
b.extend_from_slice(&2u16.to_be_bytes());
}
b.extend_from_slice(&[0xfd, 0x80, 0xfd, 0x80]);
let merged = Merged::open(&b, 4).unwrap();
assert_eq!(merged.read_planes(1).unwrap(), vec![vec![0x80; 8]]);
assert!(merged.read_planes(2).is_err());
assert!(merged.validate_all().is_err());
assert!(Merged::open(&b, 3).is_err());
}
#[test]
fn skips_invalid_layer_contents_and_checks_rle_packets() {
let mut b = head(1, 0);
let layer_offset = 34;
b.splice(layer_offset..layer_offset + 4, [0, 0, 0, 3, 0xff, 0xfe, 0xfd]);
b.extend_from_slice(&[1; 8]);
let merged = Merged::open(&b, 4).unwrap();
assert_eq!(merged.read_planes(1).unwrap(), vec![vec![1; 8]]);
assert!(merged.validate_all().is_ok());
let mut row = [0; 4];
unpackbits(&[3, 7, 8], &mut row).unwrap();
assert_eq!(row, [7, 8, 0, 0]);
assert!(unpackbits(&[0x81, 1], &mut row).is_err());
}
}
+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());
}
}
+3
View File
@@ -97,6 +97,8 @@ pub mod kelvin;
mod kodak;
pub mod lens_metadata;
pub mod ljpeg;
/// Optimized lossless JPEG decoder for streams with line-aligned restarts.
pub mod ljpeg_lines;
#[cfg(feature = "arw")]
mod llvc;
#[cfg(feature = "mef")]
@@ -643,6 +645,7 @@ pub fn orientation(bytes: &[u8]) -> Option<u16> {
}
// A damaged/unsupported pixel IFD must not hide usable orientation metadata from preview
// fallback. CR3 keeps IFD0 in its CMT1 box; TIFF-based raws keep it at the file start.
#[cfg(feature = "cr3")]
if probe(bytes) == Some(RawFormat::Cr3) {
return cr3::orientation(bytes);
}
+543
View File
@@ -0,0 +1,543 @@
//! Lossless JPEG decoding: ITU-T T.81 | ISO/IEC 10918-1 Annex H (process 14, Huffman coded,
//! "SOF3"), the compression DNG calls lossless JPEG (Compression = 7).
//!
//! - Frame header (B.2.2): 2–16-bit precision, 1–4 components, each sampled 1×1.
//! - Huffman tables (B.2.4.2, Annex C) decoded with a 9-bit lookup and the F.2.2.3 code-length
//! walk for longer codes.
//! - Scan header (B.2.3): one scan holding every component, predictor `Ss` 1–7 (Table H.1) and
//! point transform `Al`.
//! - Prediction (H.1.2.1): the first line (and the first line after a restart) predicts from the
//! left neighbour with 2^(P − Pt − 1) for its first sample; later lines predict their first
//! sample from the one above and the rest with the selected predictor. Differences are added
//! modulo 2^16 (H.1.2.2) and the SSSS = 16 category is 32768 with no extra bits.
//! - Restart intervals (DRI, RSTn) that span whole lines.
/// Why a lossless JPEG stream cannot be decoded.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
/// A required byte is missing.
#[error("truncated lossless JPEG")]
Truncated,
/// The sample allocation exceeds the caller's bound.
#[error("lossless JPEG is too large")]
TooLarge,
/// Invalid stream structure.
#[error("{0}")]
Malformed(&'static str),
/// A valid variation outside the line-restart decoder's support.
#[error("{0}")]
Unsupported(String),
}
/// Result of the line-restart decoder.
pub type Result<T> = std::result::Result<T, Error>;
/// A decoded lossless JPEG image.
#[derive(Debug)]
pub struct Decoded {
pub width: usize,
pub height: usize,
pub components: usize,
pub precision: u8,
pub predictor: u8,
/// Samples, interleaved by component, line after line, shifted back up by the point transform.
pub samples: Vec<u16>,
}
const LOOKUP_BITS: u32 = 9;
/// Bits of the combined code + difference lookup.
const FULL_BITS: u32 = 13;
/// A combined lookup entry: bit 8 set, the bits used in 0–7, the difference in 16–31.
const FULL_VALID: u32 = 0x100;
/// A Huffman table for decoding (T.81 Annex C and F.2.2.3).
#[derive(Clone)]
struct Huffman {
/// For codes of up to LOOKUP_BITS bits: (length, value) indexed by the next LOOKUP_BITS bits;
/// length 0 means "longer code".
fast: Vec<(u8, u8)>,
maxcode: [i32; 17],
valptr: [i32; 17],
mincode: [i32; 17],
values: Vec<u8>,
/// For a code and its extra bits that fit in FULL_BITS together (difference categories
/// 0–15): `FULL_VALID | used bits | difference << 16`, indexed by the next FULL_BITS bits;
/// 0 means "decode the slow way".
full: Vec<u32>,
}
impl Huffman {
fn new(bits: &[u8; 16], values: Vec<u8>) -> Result<Huffman> {
// C.2: code sizes and codes
let mut sizes = Vec::new();
for (l, &n) in bits.iter().enumerate() {
for _ in 0..n {
sizes.push(l as u8 + 1);
}
}
if sizes.len() != values.len() || sizes.is_empty() {
return Err(Error::Malformed("lossless JPEG: Huffman table counts"));
}
let mut codes = Vec::with_capacity(sizes.len());
let mut code: u32 = 0;
let mut si = sizes[0];
let mut k = 0;
while k < sizes.len() {
while k < sizes.len() && sizes[k] == si {
codes.push(code);
code += 1;
k += 1;
}
// the codes of one length must fit in that length
if code > (1u32 << si) {
return Err(Error::Malformed("lossless JPEG: Huffman table over-subscribed"));
}
code <<= 1;
si += 1;
}
// F.2.2.3 decoder tables
let mut maxcode = [-1i32; 17];
let mut valptr = [0i32; 17];
let mut mincode = [0i32; 17];
let mut j = 0usize;
for l in 1..=16usize {
let n = usize::from(bits[l - 1]);
if n == 0 {
maxcode[l] = -1;
} else {
valptr[l] = j as i32;
mincode[l] = codes[j] as i32;
j += n;
maxcode[l] = codes[j - 1] as i32;
}
}
let mut fast = vec![(0u8, 0u8); 1 << LOOKUP_BITS];
for (i, (&c, &s)) in codes.iter().zip(&sizes).enumerate() {
let s = u32::from(s);
if s <= LOOKUP_BITS {
let shift = LOOKUP_BITS - s;
let base = (c << shift) as usize;
for f in fast.iter_mut().skip(base).take(1 << shift) {
*f = (s as u8, values[i]);
}
}
}
let mut full = vec![0u32; 1 << FULL_BITS];
for (&c, (&s, &ssss)) in codes.iter().zip(sizes.iter().zip(&values)) {
let (s, ssss) = (u32::from(s), u32::from(ssss));
if ssss >= 16 || s + ssss > FULL_BITS {
continue;
}
let total = s + ssss;
for v in 0..1u32 << ssss {
let diff = match ssss {
0 => 0,
k if v < 1 << (k - 1) => v as i32 - (1 << k) + 1,
_ => v as i32,
};
let entry = FULL_VALID | total | (u32::from(diff as i16 as u16) << 16);
let shift = FULL_BITS - total;
let base = (((c << ssss) | v) << shift) as usize;
for f in full.iter_mut().skip(base).take(1 << shift) {
*f = entry;
}
}
}
Ok(Huffman { fast, maxcode, valptr, mincode, values, full })
}
}
/// Entropy-coded data reader: removes stuffed zero bytes and stops at markers (feeding zeros).
struct Bits<'a> {
data: &'a [u8],
pos: usize,
acc: u64,
n: u32,
/// The marker that ended the data (after 0xFF), if one was met.
marker: Option<u8>,
}
impl<'a> Bits<'a> {
fn new(data: &'a [u8], pos: usize) -> Self {
Bits { data, pos, acc: 0, n: 0, marker: None }
}
fn fill(&mut self) {
// fast path: eight bytes without a 0xFF (no stuffing, no marker) go in at once
if self.marker.is_none()
&& let Some(b) = self.data.get(self.pos..self.pos + 8)
{
let chunk = u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]);
let inv = !chunk;
let has_ff = inv.wrapping_sub(0x0101_0101_0101_0101) & !inv & 0x8080_8080_8080_8080 != 0;
if !has_ff {
let take = (64 - self.n) / 8;
if take > 0 {
self.acc |= (chunk >> (64 - 8 * take)) << (64 - self.n - 8 * take);
self.pos += take as usize;
self.n += 8 * take;
return;
}
}
}
while self.n <= 56 {
let mut byte = 0u8;
if self.marker.is_none() {
match self.data.get(self.pos) {
Some(&0xFF) => match self.data.get(self.pos + 1) {
Some(&0x00) => {
byte = 0xFF;
self.pos += 2;
}
Some(&m) => self.marker = Some(m),
None => self.marker = Some(0xD9),
},
Some(&b) => {
byte = b;
self.pos += 1;
}
None => self.marker = Some(0xD9),
}
}
self.acc |= u64::from(byte) << (56 - self.n);
self.n += 8;
}
}
fn peek(&mut self, k: u32) -> u32 {
if self.n < k {
self.fill();
}
(self.acc >> (64 - k)) as u32
}
fn skip(&mut self, k: u32) {
self.acc <<= k;
self.n -= k;
}
fn bits(&mut self, k: u32) -> u32 {
if k == 0 {
return 0;
}
let v = self.peek(k);
self.skip(k);
v
}
/// Decode one Huffman symbol (F.2.2.3).
fn decode(&mut self, h: &Huffman) -> Result<u8> {
let p = self.peek(LOOKUP_BITS) as usize;
if let Some(&(len, v)) = h.fast.get(p)
&& len > 0
{
self.skip(u32::from(len));
return Ok(v);
}
let mut code = self.bits(1) as i32;
for l in 1..=16usize {
if code <= h.maxcode[l] {
let i = h.valptr[l] + code - h.mincode[l];
return h.values.get(i as usize).copied().ok_or(Error::Malformed("lossless JPEG: bad Huffman code"));
}
code = (code << 1) | self.bits(1) as i32;
}
Err(Error::Malformed("lossless JPEG: bad Huffman code"))
}
/// Skip to the byte after the next RSTn marker, starting a new bit stream there.
fn restart(&mut self) -> Result<()> {
// the decoder may have looked ahead into fill bits; find the marker in the bytes
let mut p = self.pos;
loop {
match (self.data.get(p), self.data.get(p + 1)) {
(Some(0xFF), Some(&m)) if (0xD0..=0xD7).contains(&m) => {
*self = Bits::new(self.data, p + 2);
return Ok(());
}
(Some(_), Some(_)) => p += 1,
_ => return Err(Error::Malformed("lossless JPEG: missing restart marker")),
}
}
}
}
struct Component {
id: u8,
table: usize,
}
/// Decode a lossless JPEG stream. `max_samples` caps the picture (width × height × components).
pub fn decode(data: &[u8], max_samples: usize) -> Result<Decoded> {
if data.get(0..2) != Some(&[0xFF, 0xD8]) {
return Err(Error::Malformed("lossless JPEG: no SOI marker"));
}
let mut pos = 2usize;
let mut tables: [Option<Huffman>; 4] = [None, None, None, None];
let mut frame: Option<(u8, usize, usize, Vec<Component>)> = None;
let mut restart_interval = 0usize;
loop {
// markers may be preceded by fill bytes 0xFF
while data.get(pos) == Some(&0xFF) && data.get(pos + 1) == Some(&0xFF) {
pos += 1;
}
let (Some(&0xFF), Some(&m)) = (data.get(pos), data.get(pos + 1)) else {
return Err(Error::Malformed("lossless JPEG: expected a marker"));
};
pos += 2;
if m == 0xD9 {
return Err(Error::Malformed("lossless JPEG: no scan"));
}
if (0xD0..=0xD8).contains(&m) || m == 0x01 {
continue; // markers without a segment
}
let len_b = data.get(pos..pos + 2).ok_or(Error::Truncated)?;
let len = usize::from(u16::from_be_bytes([len_b[0], len_b[1]]));
if len < 2 {
return Err(Error::Malformed("lossless JPEG: segment length"));
}
let seg = data.get(pos + 2..pos + len).ok_or(Error::Truncated)?;
match m {
0xC3 => {
let [p, y0, y1, x0, x1, nf, rest @ ..] = seg else {
return Err(Error::Malformed("lossless JPEG: short frame header"));
};
let (precision, nf) = (*p, usize::from(*nf));
let height = usize::from(u16::from_be_bytes([*y0, *y1]));
let width = usize::from(u16::from_be_bytes([*x0, *x1]));
if !(2..=16).contains(&precision) {
return Err(Error::Unsupported(format!("lossless JPEG precision {precision}")));
}
if height == 0 {
return Err(Error::Unsupported("lossless JPEG with the height in a DNL marker".into()));
}
if width == 0 || !(1..=4).contains(&nf) || rest.len() < nf * 3 {
return Err(Error::Malformed("lossless JPEG: frame header"));
}
let mut comps = Vec::with_capacity(nf);
for c in rest.as_chunks::<3>().0.iter().take(nf) {
if c[1] != 0x11 {
return Err(Error::Unsupported("subsampled lossless JPEG components".into()));
}
comps.push(Component { id: c[0], table: 0 });
}
frame = Some((precision, width, height, comps));
}
0xC4 => {
let mut s = seg;
while !s.is_empty() {
let [tcth, counts @ ..] = s else { break };
let bits: [u8; 16] = counts.get(..16).and_then(|b| b.try_into().ok()).ok_or(Error::Truncated)?;
let n: usize = bits.iter().map(|&b| usize::from(b)).sum();
let values = counts.get(16..16 + n).ok_or(Error::Truncated)?.to_vec();
let th = usize::from(tcth & 0x0F);
if tcth >> 4 != 0 || th > 3 {
return Err(Error::Malformed("lossless JPEG: Huffman table class or id"));
}
if values.iter().any(|&v| v > 16) {
return Err(Error::Malformed("lossless JPEG: difference category above 16"));
}
tables[th] = Some(Huffman::new(&bits, values)?);
s = counts.get(16 + n..).unwrap_or(&[]);
}
}
0xDD => {
let r = seg.get(0..2).ok_or(Error::Truncated)?;
restart_interval = usize::from(u16::from_be_bytes([r[0], r[1]]));
}
0xDA => {
let Some((precision, width, height, mut comps)) = frame.take() else {
return Err(Error::Malformed("lossless JPEG: scan before the frame header"));
};
let ns = usize::from(*seg.first().ok_or(Error::Truncated)?);
if ns != comps.len() {
return Err(Error::Unsupported("lossless JPEG with more than one scan".into()));
}
for k in 0..ns {
let c = seg.get(1 + 2 * k..3 + 2 * k).ok_or(Error::Truncated)?;
let comp = comps.iter_mut().find(|x| x.id == c[0]).ok_or(Error::Malformed("lossless JPEG: unknown scan component"))?;
comp.table = usize::from(c[1] >> 4);
if comp.table > 3 {
return Err(Error::Malformed("lossless JPEG: Huffman table id"));
}
}
let tail = seg.get(1 + 2 * ns..4 + 2 * ns).ok_or(Error::Truncated)?;
let (predictor, pt) = (tail[0], tail[2] & 0x0F);
if !(1..=7).contains(&predictor) {
return Err(Error::Unsupported(format!("lossless JPEG predictor {predictor}")));
}
if pt >= precision {
return Err(Error::Malformed("lossless JPEG: point transform"));
}
let n = width.checked_mul(height).and_then(|v| v.checked_mul(comps.len())).ok_or(Error::TooLarge)?;
if n > max_samples {
return Err(Error::TooLarge);
}
let mut hufs = Vec::with_capacity(comps.len());
for c in &comps {
hufs.push(tables.get(c.table).and_then(Option::as_ref).ok_or(Error::Malformed("lossless JPEG: missing Huffman table"))?);
}
// every sample costs at least one bit of entropy-coded data (a Huffman code is at
// least one bit long), so a frame bigger than the data cannot be real
if n.div_ceil(8) > data.len().saturating_sub(pos + len) {
return Err(Error::Truncated);
}
let params = ScanParams { width, height, precision, predictor, pt, restart: restart_interval };
let samples = scan(data, pos + len, &hufs, &params)?;
return Ok(Decoded { width, height, components: comps.len(), precision, predictor, samples });
}
0xC0..=0xC2 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => {
return Err(Error::Unsupported("lossy (DCT) JPEG in a DNG".into()));
}
_ => {} // APPn, COM, DQT … are skipped
}
pos += len;
}
}
/// Frame and scan parameters needed to decode one scan.
struct ScanParams {
width: usize,
height: usize,
precision: u8,
predictor: u8,
pt: u8,
/// Restart interval in samples per component (0 = none).
restart: usize,
}
/// The next difference (F.2.2.1 and H.1.2.2): category and extra bits from the combined table
/// when they fit, else symbol by symbol.
#[inline(always)]
fn difference(bits: &mut Bits, h: &Huffman) -> Result<i32> {
let e = h.full.get(bits.peek(FULL_BITS) as usize).copied().unwrap_or(0);
if e & FULL_VALID != 0 {
bits.skip(e & 0xFF);
return Ok(i32::from((e >> 16) as u16 as i16));
}
Ok(match u32::from(bits.decode(h)?) {
0 => 0,
16 => 32768,
s => {
let v = bits.bits(s) as i32;
if v < (1 << (s - 1)) { v - (1 << s) + 1 } else { v }
}
})
}
/// Decode the entropy-coded data of one scan (H.1.2).
fn scan(data: &[u8], start: usize, hufs: &[&Huffman], params: &ScanParams) -> Result<Vec<u16>> {
let ScanParams { width, height, precision, predictor, pt, restart } = *params;
let nc = hufs.len();
let row_len = width * nc;
if restart != 0 && !restart.is_multiple_of(width) {
return Err(Error::Unsupported("lossless JPEG restart interval that splits a line".into()));
}
let lines_per_interval = if restart == 0 { usize::MAX } else { restart / width };
let mut out = vec![0u16; row_len * height];
let mut bits = Bits::new(data, start);
let initial = 1i32 << (precision - pt - 1);
let mut line_in_interval = 0usize;
for y in 0..height {
if line_in_interval == lines_per_interval {
bits.restart()?;
line_in_interval = 0;
}
let (above, cur) = out.split_at_mut(y * row_len);
let cur = cur.get_mut(..row_len).ok_or(Error::TooLarge)?;
let above = if line_in_interval == 0 { None } else { above.get((y - 1) * row_len..) };
match predictor {
1 => line::<1>(&mut bits, hufs, cur, above, initial)?,
2 => line::<2>(&mut bits, hufs, cur, above, initial)?,
3 => line::<3>(&mut bits, hufs, cur, above, initial)?,
4 => line::<4>(&mut bits, hufs, cur, above, initial)?,
5 => line::<5>(&mut bits, hufs, cur, above, initial)?,
6 => line::<6>(&mut bits, hufs, cur, above, initial)?,
_ => line::<7>(&mut bits, hufs, cur, above, initial)?,
}
line_in_interval += 1;
}
if pt > 0 {
for s in &mut out {
*s <<= pt;
}
}
Ok(out)
}
/// One line of samples (H.1.2.1): the first line of a restart interval (`above` is `None`)
/// predicts from the left with `initial` for its first sample; other lines predict their first
/// sample from above and the rest with predictor `P`.
#[inline(always)]
fn line<const P: u8>(bits: &mut Bits, hufs: &[&Huffman], cur: &mut [u16], above: Option<&[u16]>, initial: i32) -> Result<()> {
let nc = hufs.len();
let row_len = cur.len();
let above = match above {
Some(a) => Some(a.get(..row_len).ok_or(Error::TooLarge)?),
None => None,
};
for (c, h) in hufs.iter().enumerate() {
let d = difference(bits, h)?;
let px = match above {
Some(a) => i32::from(a[c]),
None => initial,
};
cur[c] = ((px + d) & 0xFFFF) as u16;
}
let mut i = nc;
match above {
None => {
while i < row_len {
for h in hufs {
let d = difference(bits, h)?;
cur[i] = ((i32::from(cur[i - nc]) + d) & 0xFFFF) as u16;
i += 1;
}
}
}
Some(a) => {
while i < row_len {
for h in hufs {
let d = difference(bits, h)?;
let px = predict(P, i32::from(cur[i - nc]), i32::from(a[i]), i32::from(a[i - nc]));
cur[i] = ((px + d) & 0xFFFF) as u16;
i += 1;
}
}
}
}
Ok(())
}
/// Table H.1 predictors (`ra` left, `rb` above, `rc` above left).
#[inline(always)]
pub fn predict(p: u8, ra: i32, rb: i32, rc: i32) -> i32 {
match p {
1 => ra,
2 => rb,
3 => rc,
4 => ra + rb - rc,
5 => ra + ((rb - rc) >> 1),
6 => rb + ((ra - rc) >> 1),
_ => (ra + rb) >> 1,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn agrees_with_full_decoder_for_line_aligned_restarts() {
let pixels: Vec<u16> = (0..96).map(|i| ((i * 137) % 4096) as u16).collect();
for predictor in 1..=7 {
for restart in [0, 12, 24] {
let bytes = crate::ljpeg::encode(&pixels, 12, 8, 1, 12, predictor, restart);
let fast = decode(&bytes, pixels.len()).unwrap();
let full = crate::ljpeg::decode(&bytes, pixels.len()).unwrap();
assert_eq!(fast.samples, pixels, "predictor {predictor}, restart {restart}");
assert_eq!(fast.samples, full.data);
}
}
let bytes = crate::ljpeg::encode(&pixels, 12, 8, 1, 12, 1, 0);
assert_eq!(decode(&bytes, pixels.len() - 1).unwrap_err(), Error::TooLarge);
}
}
+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(_))
));
}
}

Some files were not shown because too many files have changed in this diff Show More