Merge remote-tracking branch 'origin/main' into image-codecs/jpeg-zune-panic

This commit is contained in:
FredApps committed 2026-11-16 01:33:19 +01:00
commit fc1801a3aa
25 files changed
+1536 -57

No files matched your search

+1 -1
View File
@@ -23,5 +23,5 @@ jobs:
# The codec and format code behind features that are off by default. Without these the
# coverage build never compiles it, and new code there reads as untested.
cargo-features: >-
craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write
craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi
secrets: inherit
Generated
+4
View File
@@ -1903,6 +1903,10 @@ dependencies = [
"proptest",
]
[[package]]
name = "craft-http-url"
version = "0.1.0"
[[package]]
name = "craft-i18n"
version = "0.1.0"
+1
View File
@@ -11,6 +11,7 @@ rust-version = "1.95"
repository = "https://github.com/storytold/craft-libs"
[workspace.dependencies]
craft-http-url = { path = "crates/http-url" }
craft-usd = { path = "crates/usd" }
craft-smime = { path = "crates/smime" }
craft-ber = { path = "crates/ber" }
+171 -42
View File
@@ -150,62 +150,142 @@ fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> {
}
}
/// Adobe / Resolve `.cube`. 1D LUTs (`LUT_1D_SIZE`) are expanded to a 33³ table.
pub fn parse_cube(text: &str) -> Result<LutFile, LutError> {
let mut title = String::new();
let (mut size3, mut size1) = (0usize, 0usize);
let (mut dmin, mut dmax) = ([0.0f32; 3], [1.0f32; 3]);
let mut rows: Vec<f32> = Vec::new();
for line in text.lines() {
// Only a quote at the beginning of a TITLE value protects '#'. Quotes inside an
// unquoted title's comment must not turn that comment into part of the title.
if let Some(quoted) = line
.trim_start()
.strip_prefix("TITLE")
.filter(|rest| rest.starts_with(char::is_whitespace))
.and_then(|rest| rest.trim_start().strip_prefix('"'))
/// A bounded, three-dimensional Cube profile with RGB rows in file order.
/// Unlike [`parse_cube`], this preserves the optional title and does not resample 1D curves.
#[derive(Clone, Debug, PartialEq)]
pub struct Cube3d {
pub title: Option<String>,
pub size: usize,
pub data: Vec<[f32; 3]>,
pub domain_min: [f32; 3],
pub domain_max: [f32; 3],
}
/// Read the simple 3D profile dialect used by camera-profile consumers.
///
/// `max_size` must be in 2..=256; the row budget is checked before each allocation.
/// Whole-line comments and literal titles are preserved. Unknown alphabetic directives
/// are ignored; 1D tables are refused. Finite domains are returned unchanged so the
/// consumer retains its existing policy for equal or reversed endpoints.
/// Numeric fields must consist of exactly three finite numbers (never silently skip tokens).
pub fn parse_cube_3d(text: &str, max_size: usize) -> Result<Cube3d, LutError> {
let p = read_cube(text, Some(max_size))?;
if p.size3 == 0 {
return err("not a 3D .cube LUT (no LUT_3D_SIZE)");
}
let expected = p.size3.checked_pow(3).ok_or_else(|| LutError("LUT size overflow".into()))?;
if p.rows.len() != expected {
return err(format!("expected {expected} entries, found {}", p.rows.len()));
}
Ok(Cube3d { title: p.title, size: p.size3, data: p.rows, domain_min: p.min, domain_max: p.max })
}
struct CubeRows {
title: Option<String>,
size3: usize,
size1: usize,
min: [f32; 3],
max: [f32; 3],
rows: Vec<[f32; 3]>,
}
fn cube_triplet(text: &str) -> Result<[f32; 3], LutError> {
let mut it = text.split_whitespace();
let mut row = [0.0; 3];
for value in &mut row {
*value = it.next().and_then(|s| s.parse::<f32>().ok()).filter(|v| v.is_finite()).ok_or_else(|| LutError(format!("bad line `{text}`")))?;
}
if it.next().is_some() {
return err(format!("bad line `{text}`"));
}
Ok(row)
}
fn read_cube(text: &str, profile_max: Option<usize>) -> Result<CubeRows, LutError> {
if profile_max.is_some_and(|n| !(2..=256).contains(&n)) {
return err("3D profile size limit must be in 2..=256");
}
let mut p = CubeRows { title: None, size3: 0, size1: 0, min: [0.0; 3], max: [1.0; 3], rows: Vec::new() };
let row_limit = profile_max.unwrap_or(256).checked_pow(3).ok_or_else(|| LutError("LUT size overflow".into()))?;
for raw in text.lines() {
let profile = profile_max.is_some();
let line = if profile { raw.trim() } else { raw.split('#').next().unwrap_or("").trim() };
// The standard dialect protects '#' inside quoted titles; profile titles are literal.
if !profile
&& let Some(quoted) =
raw.trim_start().strip_prefix("TITLE").filter(|r| r.starts_with(char::is_whitespace)).and_then(|r| r.trim_start().strip_prefix('"'))
{
let Some((value, _)) = quoted.split_once('"') else { return err("unterminated quoted TITLE") };
title = value.to_string();
let Some((title, _)) = quoted.split_once('"') else { return err("unterminated quoted TITLE") };
p.title = Some(title.to_string());
continue;
}
let line = line.split('#').next().unwrap_or("").trim();
if line.is_empty() {
if line.is_empty() || line.starts_with('#') {
continue;
}
let mut it = line.split_whitespace();
let key = it.next().unwrap_or("");
let nums = |it: std::str::SplitWhitespace<'_>| -> Result<[f32; 3], LutError> {
let v: Vec<f32> = it.filter_map(|s| s.parse().ok()).collect();
if v.len() == 3 { Ok([v[0], v[1], v[2]]) } else { err(format!("bad line `{line}`")) }
let (key, rest) = if profile {
["TITLE", "LUT_3D_SIZE", "LUT_1D_SIZE", "DOMAIN_MIN", "DOMAIN_MAX"]
.into_iter()
.find_map(|key| line.strip_prefix(key).map(|rest| (key, rest.trim())))
.unwrap_or_else(|| line.split_once(char::is_whitespace).unwrap_or((line, "")))
} else {
line.split_once(char::is_whitespace).map_or((line, ""), |(key, rest)| (key, rest.trim()))
};
match key {
"TITLE" => title = line[5..].trim().trim_matches('"').to_string(),
"LUT_3D_SIZE" => size3 = it.next().and_then(|s| s.parse().ok()).ok_or_else(|| LutError("bad LUT_3D_SIZE".into()))?,
"LUT_1D_SIZE" => size1 = it.next().and_then(|s| s.parse().ok()).ok_or_else(|| LutError("bad LUT_1D_SIZE".into()))?,
"DOMAIN_MIN" => dmin = nums(it)?,
"DOMAIN_MAX" => dmax = nums(it)?,
"LUT_3D_INPUT_RANGE" | "LUT_1D_INPUT_RANGE" => {
let v: Vec<f32> = it.filter_map(|s| s.parse().ok()).collect();
if v.len() == 2 {
dmin = [v[0]; 3];
dmax = [v[1]; 3];
"TITLE" => p.title = Some(rest.trim_matches('"').to_string()),
"LUT_3D_SIZE" => {
let value = if profile { rest } else { rest.split_whitespace().next().unwrap_or("") };
p.size3 = value.parse().map_err(|_| LutError(format!("bad LUT_3D_SIZE `{value}`")))?;
let limit = profile_max.unwrap_or(MAX_SIZE);
if !(2..=limit).contains(&p.size3) {
return err(format!("LUT_3D_SIZE {} out of range", p.size3));
}
}
k if k.starts_with(|c: char| c.is_ascii_digit() || c == '-' || c == '+' || c == '.') => {
let mut v = vec![k.parse::<f32>().map_err(|_| LutError(format!("bad number in `{line}`")))?];
for value in it {
v.push(value.parse::<f32>().map_err(|_| LutError(format!("bad number in `{line}`")))?);
"LUT_1D_SIZE" => {
if profile {
return err("1D LUTs are not supported (only 3D .cube files)");
}
if v.len() != 3 {
return err(format!("bad data line `{line}`"));
p.size1 = rest.split_whitespace().next().and_then(|s| s.parse().ok()).ok_or_else(|| LutError("bad LUT_1D_SIZE".into()))?;
if p.size1 > row_limit {
return err("1D LUT exceeds row budget");
}
}
"DOMAIN_MIN" => p.min = cube_triplet(rest).map_err(|_| LutError("bad DOMAIN_MIN".into()))?,
"DOMAIN_MAX" => p.max = cube_triplet(rest).map_err(|_| LutError("bad DOMAIN_MAX".into()))?,
"LUT_3D_INPUT_RANGE" | "LUT_1D_INPUT_RANGE" if !profile => {
let mut it = rest.split_whitespace();
let min = it.next().and_then(|s| s.parse::<f32>().ok());
let max = it.next().and_then(|s| s.parse::<f32>().ok());
if let (Some(min), Some(max), None) = (min, max, it.next()) {
p.min = [min; 3];
p.max = [max; 3];
}
}
_ => {
let numeric = key.starts_with(|c: char| c.is_ascii_digit() || c == '-' || c == '+' || c == '.');
let nonfinite = key.parse::<f32>().is_ok();
if numeric || nonfinite || (profile && !key.starts_with(|c: char| c.is_ascii_alphabetic())) {
let row = cube_triplet(line)?;
if p.rows.len() >= row_limit {
return err("LUT exceeds row budget");
}
// Explicit fallible growth prevents capacity growth past the configured budget.
if p.rows.len() == p.rows.capacity() {
let grow = (p.rows.capacity().max(1)).min(row_limit - p.rows.len());
p.rows.try_reserve_exact(grow).map_err(|_| LutError("LUT allocation failed".into()))?;
}
p.rows.push(row);
}
rows.extend(v);
}
// Unknown keywords (e.g. Resolve's LUT_IN_VIDEO_RANGE) are ignored, as the spec allows.
_ => {}
}
}
Ok(p)
}
/// Adobe / Resolve `.cube`. 1D LUTs (`LUT_1D_SIZE`) are expanded to a 33³ table.
pub fn parse_cube(text: &str) -> Result<LutFile, LutError> {
let p = read_cube(text, None)?;
let (title, size3, size1, dmin, dmax) = (p.title.unwrap_or_default(), p.size3, p.size1, p.min, p.max);
let rows: Vec<f32> = p.rows.into_iter().flatten().collect();
let domain_ok = (0..3).all(|k| dmin[k] < dmax[k] && dmin[k].is_finite() && dmax[k].is_finite());
if (size3 > 0 || size1 >= 2) && !domain_ok {
return err(format!("DOMAIN_MIN {:?} must lie below DOMAIN_MAX {:?}", dmin, dmax));
@@ -1386,3 +1466,52 @@ LUT_3D_SIZE 2
assert_eq!(back.data, [0.0, 0.25, 1.0].repeat(8));
}
}
#[cfg(test)]
mod profile_cube_tests {
use super::*;
fn cube(prefix: &str) -> String {
format!("{prefix}\nLUT_3D_SIZE 2\n{}", "0.25 0.5 0.75\n".repeat(8))
}
#[test]
fn profile_preserves_optional_literal_title_and_domains() {
let no_title = parse_cube_3d(&cube(""), 256).unwrap();
assert_eq!(no_title.title, None);
let value = parse_cube_3d(&cube("TITLE \"look # literal\"\nDOMAIN_MIN 1 0 2\nDOMAIN_MAX 0 0 1"), 256).unwrap();
assert_eq!(value.title.as_deref(), Some("look # literal"));
assert_eq!(value.domain_min, [1.0, 0.0, 2.0]);
assert_eq!(value.domain_max, [0.0, 0.0, 1.0]);
assert_eq!(value.data, vec![[0.25, 0.5, 0.75]; 8]);
assert_eq!(parse_cube_3d(&cube("TITLE \"\""), 256).unwrap().title.as_deref(), Some(""));
assert!(parse_cube(&cube("DOMAIN_MIN 1 0 2\nDOMAIN_MAX 0 0 1")).is_err());
}
#[test]
fn profile_limits_are_checked_without_large_allocations() {
assert!(parse_cube_3d("LUT_3D_SIZE 256", 256).unwrap_err().0.contains("16777216"));
assert!(parse_cube_3d("LUT_3D_SIZE 257", 256).unwrap_err().0.contains("out of range"));
assert!(parse_cube_3d("LUT_3D_SIZE 256", 129).unwrap_err().0.contains("out of range"));
assert!(parse_cube_3d(&cube(""), usize::MAX).is_err());
assert!(parse_cube_3d(&cube(""), 1).is_err());
assert!(parse_cube_3d(&format!("{}0 0 0\n", cube("")), 2).unwrap_err().0.contains("budget"));
assert!(parse_cube_3d("LUT_1D_SIZE 2\n0 0 0\n1 1 1", 256).is_err());
}
#[test]
fn every_numeric_token_must_be_valid_and_finite() {
for row in ["0 ignored 0 0", "0 0 0 ignored", "NaN 0 0", "inf 0 0", "0 0 -inf", "1e39 0 0", "0 0", "0 0 0 0"] {
assert!(parse_cube_3d(&format!("LUT_3D_SIZE 2\n{row}\n{}", "0 0 0\n".repeat(7)), 256).is_err(), "{row}");
assert!(parse_cube_3d(&cube(&format!("DOMAIN_MIN {row}")), 256).is_err(), "{row}");
}
}
#[test]
fn standard_reader_keeps_quoted_comments_and_1d_expansion() {
assert_eq!(parse_cube(&cube("TITLE \"look # preserved\" # comment")).unwrap().title, "look # preserved");
let value = parse_cube("LUT_1D_SIZE 2\n0 0 0\n1 1 1").unwrap();
assert_eq!(value.size, 33);
assert!(parse_cube_3d(&cube("DOMAIN_MIN 0 0 0 # not a whole-line comment"), 256).is_err());
}
}
+11
View File
@@ -0,0 +1,11 @@
[package]
name = "craft-http-url"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license = "Apache-2.0"
repository.workspace = true
description = "Bounded HTTP URL parsing and redirect resolution without network access"
[lints]
workspace = true
+25
View File
@@ -0,0 +1,25 @@
# craft-http-url
HTTP/HTTPS URL parsing, dot-segment removal, relative redirects, HTTPS downgrade rejection,
and query-redacted display. No dependencies or network operations. Parsing rejects inputs
larger than 16 KiB and redirects are bounded before concatenation.
This is not an SSRF filter, DNS resolver or HTTP client. Local/private hosts are allowed.
Applications keep destination permission, authentication, TLS, request/response limits and
redirect-count policy. Public fields preserve the original API; manually constructed values
must not be treated as validated URLs.
## Attribution
Extracted from LightCraft `crates/fetch/src/http.rs` at
`2dcb258ba40738230a07484a95eb27dc84ed66fe` (Apache-2.0).
Original parser: Daniel Voshart, `7fa7506db46d69d609e85e39b338f33786ed0293`.
HTTPS downgrade protection: MarekSpodBiedry, `4a91a1c218bf3d0ac49cc73ebb60621a21ae55e2`.
URL normalization, scheme, fragment and authority fixes: NetterSilver, including
`edc369565eef92161b56b8f1e39ef53629c05489`,
`78f0e6e650444886069f4d4ae4e6593f221b98d4`,
`cee93496587d31c514aaeced3e6b8027cfdb41a0`,
`dee5f7910ad8c24cf28bab528f8d007550af2b23`,
`c3a5d9b7f1f837dc14c01942e3d0293ecbf36345` and
`13849307a9343504339d68b086f1a32cdcbbb457`.
Source-author commits are retained separately in this extraction.
+277
View File
@@ -0,0 +1,277 @@
//! Bounded HTTP/HTTPS URL parsing and redirect resolution, extracted from LightCraft.
//!
//! This is syntax handling, not a network permission or SSRF policy. It permits local/private
//! hosts and performs no DNS, socket or TLS operations. Callers own destination allowlists,
//! credential forwarding, redirect counts, connection deadlines and response limits.
//! Only parsed values have validated fields; constructing public fields bypasses parsing.
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
/// A URL or redirect cannot be represented by this bounded HTTP URL syntax.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UrlError(pub String);
impl std::fmt::Display for UrlError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for UrlError {}
/// A parsed `http://` or `https://` URL.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Url {
pub tls: bool,
pub host: String,
pub port: u16,
/// Path and query, starting with `/`.
pub path: String,
}
/// Remove `.` and `..` segments from the path part of `path` (RFC 3986 section 5.2.4); the query is kept as is.
fn remove_dot_segments(path: &str) -> String {
let (p, query) = match path.find('?') {
Some(i) => (path.get(..i).unwrap_or_default(), path.get(i..).unwrap_or_default()),
None => (path, ""),
};
let mut out: Vec<&str> = Vec::new();
let mut segments = p.split('/').skip(1).peekable();
let mut trailing_slash = false;
while let Some(seg) = segments.next() {
let last = segments.peek().is_none();
match seg {
"." => trailing_slash = last,
".." => {
out.pop();
trailing_slash = last;
}
_ => {
out.push(seg);
trailing_slash = false;
}
}
}
let mut result = format!("/{}", out.join("/"));
if trailing_slash && !result.ends_with('/') {
result.push('/');
}
result.push_str(query);
result
}
/// `s` without the leading `prefix`, matched ASCII case-insensitively (RFC 3986 §3.1).
fn strip_scheme<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
let head = s.get(..prefix.len())?;
head.eq_ignore_ascii_case(prefix).then(|| s.get(prefix.len()..).unwrap_or_default())
}
impl Url {
pub fn parse(s: &str) -> Result<Url, UrlError> {
let bad = |why: &str| UrlError(why.to_string());
if s.len() > 16 * 1024 {
return Err(bad("URL exceeds 16 KiB"));
}
let s = s.trim();
if s.chars().any(|c| c.is_control() || c == ' ') {
return Err(bad("contains spaces or control characters"));
}
let (tls, rest) = if let Some(r) = strip_scheme(s, "https://") {
(true, r)
} else if let Some(r) = strip_scheme(s, "http://") {
(false, r)
} else {
return Err(bad("only http:// and https:// are supported"));
};
let rest = rest.split('#').next().unwrap_or_default();
let (authority, path) = match rest.find(['/', '?']) {
Some(i) => (rest.get(..i).unwrap_or_default(), rest.get(i..).unwrap_or_default()),
None => (rest, ""),
};
let path = if path.starts_with('/') { path.to_string() } else { format!("/{path}") };
let path = remove_dot_segments(&path);
if authority.contains('@') {
return Err(bad("user names and passwords in URLs are not supported"));
}
let (host, port) = if let Some(v6) = authority.strip_prefix('[') {
let end = v6.find(']').ok_or_else(|| bad("unterminated IPv6 address"))?;
let host = v6.get(..end).unwrap_or_default();
let after = v6.get(end + 1..).unwrap_or_default();
if !after.is_empty() && !after.starts_with(':') {
return Err(bad("unexpected text after IPv6 address"));
}
(host.to_string(), after.strip_prefix(':'))
} else {
match authority.rsplit_once(':') {
Some((h, p)) => (h.to_string(), Some(p)),
None => (authority.to_string(), None),
}
};
let port = match port {
Some(p) if p.bytes().all(|b| b.is_ascii_digit()) => p.parse::<u16>().ok().filter(|p| *p != 0).ok_or_else(|| bad("bad port"))?,
Some(_) => return Err(bad("bad port")),
None if tls => 443,
None => 80,
};
if host.is_empty() || !host.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '.' | ':' | '_')) {
return Err(bad("bad host name"));
}
Ok(Url { tls, host: host.to_ascii_lowercase(), port, path })
}
/// `Location` of a redirect, resolved against this URL.
pub fn join(&self, location: &str) -> Result<Url, UrlError> {
// Reject before concatenating a hostile Location with the base URL.
if location.len() > 16 * 1024 || self.host.len().saturating_add(self.path.len()) > 16 * 1024 {
return Err(UrlError("URL exceeds 16 KiB".into()));
}
let l = location.trim();
if strip_scheme(l, "http://").is_some() || strip_scheme(l, "https://").is_some() {
let url = Url::parse(l)?;
if self.tls && !url.tls {
return Err(UrlError("redirect from https to http refused".into()));
}
return Ok(url);
}
// another scheme (`ftp:`, `file:`, `data:`…)
if let Some((scheme, _)) = l.split_once(':')
&& !scheme.is_empty()
&& scheme.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
{
return Err(UrlError(format!("redirect to an unsupported location `{scheme}:`")));
}
let scheme = if self.tls { "https" } else { "http" };
let host = if self.host.contains(':') { format!("[{}]", self.host) } else { self.host.clone() };
let base = format!("{scheme}://{host}:{}", self.port);
if let Some(rest) = l.strip_prefix("//") {
return Url::parse(&format!("{scheme}://{rest}"));
}
if l.starts_with('/') {
return Url::parse(&format!("{base}{l}"));
}
// empty or fragment-only: the same document
if l.is_empty() || l.starts_with('#') {
return Ok(self.clone());
}
// relative to the current directory
let dir = self.path.split('?').next().unwrap_or("/");
// a query-only reference keeps the whole path, file name included (RFC 3986 §5.2.2)
if l.starts_with('?') {
return Url::parse(&format!("{base}{dir}{l}"));
}
let dir = dir.rsplit_once('/').map_or("", |(d, _)| d);
Url::parse(&format!("{base}{dir}/{l}"))
}
/// Authority formatted for an HTTP Host field.
pub fn host_header(&self) -> String {
let host = if self.host.contains(':') { format!("[{}]", self.host) } else { self.host.clone() };
let default = if self.tls { 443 } else { 80 };
if self.port == default { host } else { format!("{host}:{}", self.port) }
}
/// The URL without its query (for messages: a query may hold a signed token).
pub fn display(&self) -> String {
let scheme = if self.tls { "https" } else { "http" };
format!("{scheme}://{}{}", self.host_header(), self.path.split('?').next().unwrap_or("/"))
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::{Url, UrlError};
#[test]
fn fragment_only_or_empty_location_keeps_file_name_and_query() {
let base = Url::parse("https://a.example/x/y/file?q").unwrap();
for l in ["#top", "", " "] {
let u = base.join(l).unwrap();
assert_eq!((u.tls, u.host.as_str(), u.port, u.path.as_str()), (true, "a.example", 443, "/x/y/file?q"), "{l:?}");
}
}
#[test]
fn rejects_oversized_and_control_character_urls() {
let base = Url::parse("https://example.com/path").unwrap();
assert!(base.join(&format!("#{}", "x".repeat(16 * 1024))).is_err());
assert!(base.join(&"x".repeat(16 * 1024 + 1)).is_err());
assert!(Url::parse(&format!("https://example.com/{}", "x".repeat(16 * 1024))).is_err());
assert!(Url::parse("https://example.com/x?secret=abc\r\nInjected: 1").is_err());
}
#[test]
fn join_query_only_keeps_file_name() {
let u = Url::parse("https://a.example/x/y/file?q=1").unwrap();
assert_eq!(u.join("?page=2").unwrap().path, "/x/y/file?page=2");
}
#[test]
fn join_removes_dot_segments() {
let base = Url::parse("https://a.example/x/y/file").unwrap();
for (loc, want) in
[("../z", "/x/z"), ("./z", "/x/y/z"), ("../../../z", "/z"), ("/a/./b/../c", "/a/c"), ("../z?q=1", "/x/z?q=1"), ("..", "/x/")]
{
assert_eq!(base.join(loc).unwrap().path, want, "{loc}");
}
}
#[test]
fn url_scheme_is_matched_case_insensitively() {
let u = Url::parse("HTTPS://A.example/z").unwrap();
assert_eq!((u.tls, u.host.as_str(), u.port, u.path.as_str()), (true, "a.example", 443, "/z"));
assert!(Url::parse("Http://a.example/").is_ok());
let j = Url::parse("https://a.example/start").unwrap().join("HTTPS://cdn.example/file").unwrap();
assert_eq!((j.tls, j.host.as_str(), j.path.as_str()), (true, "cdn.example", "/file"));
}
#[test]
fn parses_urls() {
let u = Url::parse("https://cdn.example.com/models/sam3/model.safetensors?sig=1#x").unwrap();
assert_eq!((u.tls, u.host.as_str(), u.port, u.path.as_str()), (true, "cdn.example.com", 443, "/models/sam3/model.safetensors?sig=1"));
assert_eq!(u.display(), "https://cdn.example.com/models/sam3/model.safetensors");
let u = Url::parse("http://127.0.0.1:8080").unwrap();
assert_eq!((u.tls, u.port, u.path.as_str()), (false, 8080, "/"));
let u = Url::parse("http://[::1]:9/a").unwrap();
assert_eq!((u.host.as_str(), u.port), ("::1", 9));
for bad in ["ftp://x/y", "https://", "https://a b/", "https://u:p@h/", "http://h:0/", "http://h:99999/", "https://h/\r\nX: y", "file:///etc"]
{
assert!(Url::parse(bad).is_err(), "{bad}");
}
}
#[test]
fn rejects_signed_ports() {
for bad in ["http://example.com:+80/x", "http://example.com:-80/x", "http://[::1]:+80/x"] {
assert!(Url::parse(bad).is_err(), "{bad}");
}
assert!(Url::parse("http://a.test/").unwrap().join("http://example.com:+80/x").is_err());
assert_eq!(Url::parse("http://example.com:80/x").unwrap().port, 80);
}
#[test]
fn rejects_text_after_a_bracketed_ipv6_host() {
for bad in ["http://[::1]junk/x", "https://[::1]junk/x", "http://[::1]8080/x", "http://[::1]8080"] {
assert!(matches!(Url::parse(bad), Err(UrlError(_))), "{bad}");
}
let u = Url::parse("http://[::1]:8080/x").unwrap();
assert_eq!((u.host.as_str(), u.port), ("::1", 8080));
let u = Url::parse("http://[::1]/x").unwrap();
assert_eq!((u.host.as_str(), u.port), ("::1", 80));
}
#[test]
fn resolves_redirects() {
let u = Url::parse("https://a.example/x/y/file?q").unwrap();
assert_eq!(u.join("https://b.example/z").unwrap().host, "b.example");
assert_eq!(u.join("/root").unwrap().path, "/root");
assert_eq!(u.join("other").unwrap().path, "/x/y/other");
assert_eq!(u.join("//c.example/p").unwrap().host, "c.example");
assert!(u.join("ftp://nope").is_err());
// no downgrade from https to http, however the redirect is written
assert!(u.join("http://a.example/z").is_err());
let plain = Url::parse("http://127.0.0.1:8080/a").unwrap();
assert!(!plain.join("http://127.0.0.1:9/b").unwrap().tls);
assert!(plain.join("https://b.example/z").unwrap().tls);
}
}
+1
View File
@@ -13,6 +13,7 @@ workspace = true
[features]
corpus = []
default = []
sgi = []
avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"]
heif = ["dep:craft-heif"]
# AVIF decode: craft-video's clean-room pure-Rust AV1 decoder. Native targets only (on wasm32 a
+1
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 |
| SGI (`.sgi`, `.rgb`, `.bw`) | with feature `sgi` | with feature `sgi` (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | `sgi` (off by default) |
| 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` |
+2
View File
@@ -20,6 +20,8 @@ pub(crate) mod jxl;
pub(crate) mod pcx;
pub(crate) mod png;
pub(crate) mod pnm;
#[cfg(feature = "sgi")]
pub(crate) mod sgi;
pub(crate) mod tga;
pub(crate) mod tiff;
pub(crate) mod tiff_exif;
@@ -842,6 +842,10 @@ fn read_metadata(c: &Container) -> (Option<Vec<u8>>, Option<String>) {
/// Decodes the primary image. `keep_orientation` leaves the container's `clap`, `irot` and
/// `imir` unapplied (the stored pixels), as for HEIF.
pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> Result<Image> {
decode_with_depth(bytes, limits, keep_orientation).map(|(image, _)| image)
}
pub(crate) fn decode_with_depth(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> Result<(Image, u8)> {
let c = parse(bytes)?;
let id = c.primary;
let (w, h) = item_size(&c, id)?;
@@ -890,9 +894,9 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> R
// PQ or HLG: linear light in 32-bit float, as for HEIF.
if cicp.is_hdr() {
limits.check(w, h, layout, SampleType::F32)?;
return crate::cicp::linearize(img, cicp);
return crate::cicp::linearize(img, cicp).map(|image| (image, bits));
}
Ok(img)
Ok((img, bits))
}
#[cfg(test)]
@@ -1301,6 +1305,9 @@ mod tests {
let src = Image::from_u8(w as u32, h as u32, ChannelLayout::Rgb, card(w, h, 3)).unwrap();
let file = encode_avif_with_speed(&src, 95, 10).unwrap();
let img = crate::decode(&file).unwrap();
let (with_depth, depth) = crate::decode_avif_with_depth(&file, &crate::DecodeOptions::default()).unwrap();
assert_eq!(depth, 10);
assert_eq!(with_depth, img);
assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (48, 32, ChannelLayout::Rgb, SampleType::U16));
let diff = mean_abs_diff(&rgb8(&img), &card(w, h, 3));
assert!(diff < 2.5, "mean difference {diff}");
+395
View File
@@ -0,0 +1,395 @@
//! SGI image files (`.sgi`, `.rgb`, `.rgba`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and
//! RGBA, verbatim or run-length encoded.
//!
//! Read and write follow the published SGI image file format description: a 512-byte big-endian
//! header, then one plane per channel with the rows stored bottom to top. Palette-mapped files
//! (colormap ID other than 0) are rejected. The writer uses the run-length encoding.
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::Sgi;
const HEADER: usize = 512;
const MAGIC: u16 = 474;
fn bad(reason: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, reason)
}
fn be16(b: &[u8], i: usize) -> u16 {
u16::from_be_bytes([b[i], b[i + 1]])
}
fn be32(b: &[u8], i: usize) -> u32 {
u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]])
}
/// The fields of the header that decoding needs, already checked for plausibility.
struct Header {
rle: bool,
wide: bool,
width: u32,
height: u32,
planes: usize,
}
/// Parse the 512-byte header. `None` when the bytes are not a plausible SGI header.
fn header(b: &[u8]) -> Option<Header> {
if b.len() < HEADER || be16(b, 0) != MAGIC {
return None;
}
let (storage, bpc, dimension) = (b[2], b[3], be16(b, 4));
let (width, height, planes) = (u32::from(be16(b, 6)), u32::from(be16(b, 8)), usize::from(be16(b, 10)));
if storage > 1 || !(1..=2).contains(&bpc) || !(1..=3).contains(&dimension) {
return None;
}
// A one-dimensional image has one row, a two-dimensional one a single plane.
let (height, planes) = match dimension {
1 => (1, 1),
2 => (height, 1),
_ => (height, planes),
};
if width == 0 || height == 0 || !(1..=4).contains(&planes) {
return None;
}
Some(Header { rle: storage == 1, wide: bpc == 2, width, height, planes })
}
/// Whether `b` starts with a plausible SGI header (the magic number plus consistent fields).
pub(crate) fn looks_like_sgi(b: &[u8]) -> bool {
header(b).is_some()
}
fn layout_of(planes: usize) -> ChannelLayout {
match planes {
1 => ChannelLayout::Gray,
2 => ChannelLayout::GrayA,
3 => ChannelLayout::Rgb,
_ => ChannelLayout::Rgba,
}
}
/// Expand one run-length encoded row into exactly `out.len()` samples.
fn read_rle_row(src: &[u8], wide: bool, out: &mut [u16]) -> Result<(), CodecError> {
let step = if wide { 2 } else { 1 };
let sample = |pos: usize| -> Result<u16, CodecError> {
let s = src.get(pos..pos + step).ok_or_else(|| bad("truncated RLE row"))?;
Ok(if wide { u16::from_be_bytes([s[0], s[1]]) } else { u16::from(s[0]) })
};
let (mut pos, mut n) = (0usize, 0usize);
while n < out.len() {
let code = sample(pos)?;
pos += step;
let count = usize::from(code & 0x7f);
if count == 0 {
return Err(bad("RLE row ends before the image width"));
}
if count > out.len() - n {
return Err(bad("RLE packet runs past the end of the row"));
}
if code & 0x80 != 0 {
for slot in &mut out[n..n + count] {
*slot = sample(pos)?;
pos += step;
}
} else {
let value = sample(pos)?;
pos += step;
out[n..n + count].fill(value);
}
n += count;
}
Ok(())
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
let h = header(b).ok_or_else(|| bad("invalid SGI header"))?;
if be32(b, 104) != 0 {
return Err(CodecError::unsupported(F, "palette-mapped SGI files are not supported"));
}
let layout = layout_of(h.planes);
let sample = if h.wide { SampleType::U16 } else { SampleType::U8 };
limits.check(h.width, h.height, layout, sample)?;
let (w, ht, planes) = (h.width as usize, h.height as usize, h.planes);
let rows = ht.checked_mul(planes).ok_or_else(|| bad("image size overflow"))?;
let bps = if h.wide { 2 } else { 1 };
let total = w.checked_mul(rows).ok_or_else(|| bad("image size overflow"))?;
// Reject inputs that cannot hold the declared pixels before allocating anything for them.
let table = if h.rle { rows.checked_mul(8).ok_or_else(|| bad("image size overflow"))? } else { 0 };
let min_len = if h.rle {
// Both offset tables must fit; rows may share compressed data, so the data itself is
// checked row by row against the offsets below.
HEADER + table
} else {
HEADER + total.checked_mul(bps).ok_or_else(|| bad("image size overflow"))?
};
if b.len() < min_len {
return Err(bad("the file is too short for the declared image"));
}
let mut out = Vec::new();
out.try_reserve_exact(total).map_err(|_| bad("pixel buffer is too large"))?;
out.resize(total, 0u16);
let mut row = vec![0u16; w];
for z in 0..planes {
for y in 0..ht {
let index = y + z * ht;
if h.rle {
let start = be32(b, HEADER + index * 4) as usize;
let len = be32(b, HEADER + rows * 4 + index * 4) as usize;
let end = start
.checked_add(len)
.filter(|&e| e <= b.len() && start >= HEADER + table)
.ok_or_else(|| bad("RLE row lies outside the file"))?;
read_rle_row(&b[start..end], h.wide, &mut row)?;
} else {
let start = HEADER + index * w * bps;
let s = &b[start..start + w * bps];
for (x, v) in row.iter_mut().enumerate() {
*v = if h.wide { u16::from_be_bytes([s[2 * x], s[2 * x + 1]]) } else { u16::from(s[x]) };
}
}
// Rows are stored bottom to top; planes are separate.
let top_y = ht - 1 - y;
for (x, &v) in row.iter().enumerate() {
out[(top_y * w + x) * planes + z] = v;
}
}
}
if h.wide {
Image::from_u16(h.width, h.height, layout, &out)
} else {
Image::from_u8(h.width, h.height, layout, out.iter().map(|&v| v as u8).collect())
}
}
/// Append one run-length encoded row (one packet list and a terminator).
fn write_rle_row(row: &[u16], wide: bool, out: &mut Vec<u8>) {
let put = |out: &mut Vec<u8>, v: u16| {
if wide {
out.extend_from_slice(&v.to_be_bytes());
} else {
out.push(v as u8);
}
};
let mut i = 0;
while i < row.len() {
let mut run = 1;
while i + run < row.len() && run < 127 && row[i + run] == row[i] {
run += 1;
}
if run >= 2 {
put(out, run as u16);
put(out, row[i]);
i += run;
} else {
// A literal packet extends until the next run of two or more.
let mut end = i + 1;
while end < row.len() && end - i < 127 && !(end + 1 < row.len() && row[end] == row[end + 1]) {
end += 1;
}
put(out, 0x80 | (end - i) as u16);
for &v in &row[i..end] {
put(out, v);
}
i = end;
}
}
put(out, 0);
}
/// Writes a run-length encoded SGI file: 8 or 16 bits per channel, gray, gray + alpha, RGB or RGBA.
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 wide = match img.sample_type() {
SampleType::U8 => false,
SampleType::U16 => true,
s => return Err(enc(format!("SGI stores 8 or 16 bits per channel, not {s:?}"))),
};
let planes = img.layout().channels();
if !matches!(img.layout(), ChannelLayout::Gray | ChannelLayout::GrayA | ChannelLayout::Rgb | ChannelLayout::Rgba) {
return Err(enc(format!("no SGI layout for {:?}", img.layout())));
}
let (w, ht) = img.dimensions();
let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(ht)) else {
return Err(enc(format!("dimensions exceed 65535x65535: {w}x{ht}")));
};
let (w, ht) = (w as usize, ht as usize);
let bps = if wide { 2 } else { 1 };
let data = img.data();
if data.len() != w * ht * planes * bps {
return Err(enc("the image has the wrong number of samples".into()));
}
let sample_at = |i: usize| -> u16 { if wide { u16::from_ne_bytes([data[2 * i], data[2 * i + 1]]) } else { u16::from(data[i]) } };
let rows = ht * planes;
let table = HEADER;
let mut starts = Vec::with_capacity(rows);
let mut lens = Vec::with_capacity(rows);
let mut body = Vec::new();
let mut row = vec![0u16; w];
let base = u32::try_from(HEADER + rows * 8).map_err(|_| enc("image too large".into()))?;
for z in 0..planes {
for y in 0..ht {
let src_y = ht - 1 - y; // stored bottom to top
for (x, v) in row.iter_mut().enumerate() {
*v = sample_at((src_y * w + x) * planes + z);
}
let before = body.len();
write_rle_row(&row, wide, &mut body);
let start = u32::try_from(base as usize + before).map_err(|_| enc("image too large".into()))?;
starts.push(start);
lens.push((body.len() - before) as u32);
}
}
let mut out = Vec::with_capacity(table + rows * 8 + body.len());
out.extend_from_slice(&MAGIC.to_be_bytes());
out.push(1); // RLE
out.push(bps as u8);
out.extend_from_slice(&(if planes > 1 { 3u16 } else { 2u16 }).to_be_bytes());
out.extend_from_slice(&w16.to_be_bytes());
out.extend_from_slice(&h16.to_be_bytes());
out.extend_from_slice(&(planes as u16).to_be_bytes());
out.extend_from_slice(&0u32.to_be_bytes()); // pixmin
out.extend_from_slice(&(if wide { 65535u32 } else { 255 }).to_be_bytes()); // pixmax
out.resize(HEADER, 0); // dummy, image name, colormap 0, padding
for s in &starts {
out.extend_from_slice(&s.to_be_bytes());
}
for l in &lens {
out.extend_from_slice(&l.to_be_bytes());
}
out.extend_from_slice(&body);
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
fn gradient(w: u32, h: u32, layout: ChannelLayout) -> Image {
let ch = layout.channels();
let data = (0..w as usize * h as usize * ch).map(|i| ((i * 7 + i / 5) % 251) as u8).collect();
Image::from_u8(w, h, layout, data).unwrap()
}
#[test]
fn rle_round_trips_every_layout_in_8_bits() {
for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] {
let img = gradient(37, 11, layout);
let plan = Plan { layout, sample: SampleType::U8 };
let bytes = encode(&img, plan).unwrap();
assert!(looks_like_sgi(&bytes));
let back = decode(&bytes, &Limits::default()).unwrap();
assert_eq!(back.layout(), layout);
assert_eq!(back.data(), img.data());
}
}
#[test]
fn rle_round_trips_16_bits_with_long_runs() {
let (w, h) = (300u32, 4u32);
let samples: Vec<u16> = (0..w * h * 3).map(|i| if (i / 3) % 200 < 150 { 4242 } else { (i * 31) as u16 }).collect();
let img = Image::from_u16(w, h, ChannelLayout::Rgb, &samples).unwrap();
let bytes = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U16 }).unwrap();
let back = decode(&bytes, &Limits::default()).unwrap();
assert_eq!(back.sample_type(), SampleType::U16);
assert_eq!(back.data(), img.data());
}
/// A hand-built verbatim file: two rows, one plane, stored bottom to top.
#[test]
fn verbatim_files_read_bottom_to_top() {
let mut f = vec![0u8; HEADER];
f[0..2].copy_from_slice(&MAGIC.to_be_bytes());
f[2] = 0; // verbatim
f[3] = 1;
f[4..6].copy_from_slice(&2u16.to_be_bytes());
f[6..8].copy_from_slice(&3u16.to_be_bytes()); // x
f[8..10].copy_from_slice(&2u16.to_be_bytes()); // y
f[10..12].copy_from_slice(&1u16.to_be_bytes()); // z
f.extend_from_slice(&[10, 11, 12, 20, 21, 22]); // bottom row first
let img = decode(&f, &Limits::default()).unwrap();
assert_eq!(img.layout(), ChannelLayout::Gray);
assert_eq!(img.data(), &[20, 21, 22, 10, 11, 12]);
}
#[test]
fn hostile_input_is_an_error_not_a_panic() {
let img = gradient(16, 8, ChannelLayout::Rgb);
let good = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U8 }).unwrap();
// Every truncation fails cleanly.
for len in 0..good.len() {
let _ = decode(&good[..len], &Limits::default());
}
// A row table pointing outside the file, and a packet that overruns its row.
let mut bad_table = good.clone();
bad_table[HEADER..HEADER + 4].copy_from_slice(&u32::MAX.to_be_bytes());
assert!(decode(&bad_table, &Limits::default()).is_err());
let mut overrun = good.clone();
let start = be32(&overrun, HEADER) as usize;
overrun[start] = 127; // a run longer than the 16-pixel row
assert!(decode(&overrun, &Limits::default()).is_err());
// A huge declared size is stopped by the limits or the length check, before allocating.
let mut huge = good;
huge[6..8].copy_from_slice(&u16::MAX.to_be_bytes());
huge[8..10].copy_from_slice(&u16::MAX.to_be_bytes());
assert!(decode(&huge, &Limits::default()).is_err());
}
/// A 1-plane, 8-bit RLE file whose `rows` all start where `starts[i]` says, each `len` bytes long.
fn rle_file(w: u16, starts: &[u32], len: u32, payload: &[u8]) -> Vec<u8> {
let mut f = vec![0u8; HEADER];
f[0..2].copy_from_slice(&MAGIC.to_be_bytes());
f[2] = 1;
f[3] = 1;
f[4..6].copy_from_slice(&2u16.to_be_bytes());
f[6..8].copy_from_slice(&w.to_be_bytes());
f[8..10].copy_from_slice(&(starts.len() as u16).to_be_bytes());
f[10..12].copy_from_slice(&1u16.to_be_bytes());
for s in starts {
f.extend_from_slice(&s.to_be_bytes());
}
for _ in starts {
f.extend_from_slice(&len.to_be_bytes());
}
f.extend_from_slice(payload);
f
}
/// The format lets table entries share one compressed row: eight rows, one payload.
#[test]
fn rows_may_share_compressed_data() {
let table = HEADER + 8 * 8;
let f = rle_file(1, &[table as u32; 8], 3, &[0x81, 255, 0]);
assert_eq!(f.len(), table + 3);
let img = decode(&f, &Limits::default()).unwrap();
assert_eq!((img.dimensions(), img.data()), ((1, 8), &[255u8; 8][..]));
}
/// Compressed rows start after both tables: an offset into a table is refused.
#[test]
fn rows_may_not_start_inside_the_offset_tables() {
// 3x1: the tables end at 520; 515 reads the table's own bytes as a packet list.
let f = rle_file(3, &[515], 3, &[0, 0, 0, 0, 0]);
assert!(decode(&f, &Limits::default()).is_err());
}
/// FFmpeg's decoder, run as an oracle, also reads a row that ends with its last packet and no
/// zero terminator; so does this one.
#[test]
fn a_row_without_its_terminator_still_reads() {
let f = rle_file(1, &[520], 2, &[0x81, 42]);
assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[42]);
}
#[test]
fn palette_mapped_files_are_unsupported() {
let img = gradient(4, 4, ChannelLayout::Gray);
let mut f = encode(&img, Plan { layout: ChannelLayout::Gray, sample: SampleType::U8 }).unwrap();
f[104..108].copy_from_slice(&1u32.to_be_bytes());
assert!(matches!(decode(&f, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
}
+15 -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,
/// SGI image files (`.sgi`, `.rgb`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and RGBA.
Sgi,
/// JPEG-LS (`.jls`): lossless and near-lossless gray and RGB.
JpegLs,
}
@@ -118,7 +120,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; 25] = [
pub const ALL: [Format; 26] = [
Format::Png,
Format::Jpeg,
Format::Jp2,
@@ -143,6 +145,7 @@ impl Format {
Format::Cineon,
Format::Dicom,
Format::Iff,
Format::Sgi,
Format::JpegLs,
];
@@ -182,6 +185,7 @@ impl Format {
Format::Dicom => "DICOM",
Format::Iff => "Amiga IFF",
Format::Jp2 => "JPEG 2000",
Format::Sgi => "SGI",
Format::JpegLs => "JPEG-LS",
}
}
@@ -214,6 +218,7 @@ impl Format {
Format::Dicom => &["dcm", "dicom", "dc3"],
Format::Iff => &["iff", "lbm", "ilbm"],
Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"],
Format::Sgi => &["sgi", "rgb", "rgba", "bw"],
Format::JpegLs => &["jls"],
}
}
@@ -244,6 +249,7 @@ impl Format {
Format::Dicom => "application/dicom",
Format::Iff => "image/x-iff",
Format::Jp2 => "image/jp2",
Format::Sgi => "image/x-sgi",
Format::JpegLs => "image/jls",
}
}
@@ -251,7 +257,7 @@ impl Format {
/// 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 | Format::JpegLs => Some((65535, 65535)),
Format::Jpeg | Format::Gif | Format::Iff | Format::Sgi | Format::JpegLs => Some((65535, 65535)),
Format::WebP => Some((16384, 16384)),
Format::Ico => Some((256, 256)),
_ => None,
@@ -321,6 +327,9 @@ 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::Sgi => {
FormatCaps { read: cfg!(feature = "sgi"), write: cfg!(feature = "sgi"), depths: &[S::U8, S::U16], layouts: RGB_GRAY, 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 },
}
@@ -403,6 +412,10 @@ pub fn detect(bytes: &[u8]) -> Option<Format> {
if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") {
return Some(Format::Xpm);
}
#[cfg(feature = "sgi")]
if crate::codecs::sgi::looks_like_sgi(b) {
return Some(Format::Sgi);
}
if crate::codecs::pcx::looks_like_pcx(b) {
return Some(Format::Pcx);
}
+30
View File
@@ -67,6 +67,8 @@ 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;
#[cfg(feature = "sgi")]
use crate::codecs::sgi;
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`].
@@ -106,6 +108,16 @@ 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::Sgi => {
#[cfg(feature = "sgi")]
{
sgi::decode(bytes, l)
}
#[cfg(not(feature = "sgi"))]
{
Err(CodecError::unsupported(format, "SGI support requires the sgi feature"))
}
}
Format::JpegLs => jpegls::decode(bytes, l),
Format::OpenExr => exr::decode(bytes, l),
Format::Heif => heif::decode(bytes, l, opts.keep_orientation),
@@ -120,6 +132,14 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res
finish_format(format, bytes, img, opts)
}
/// Decode AVIF pixels and report their original AV1 bit depth (8, 10 or 12).
/// The returned image may use a wider sample type, particularly for linear HDR.
#[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))]
pub fn decode_avif_with_depth(bytes: &[u8], opts: &DecodeOptions) -> Result<(Image, u8), CodecError> {
let (image, depth) = codecs::avif_decode::decode_with_depth(bytes, &opts.limits, opts.keep_orientation)?;
Ok((finish_format(Format::Avif, bytes, image, opts)?, depth))
}
/// What [`decode_as_with`] does with a PNG decoded in steps by [`PngDecodeRows`] (`img` is its
/// output): the resolution from the metadata, the upright orientation and the final limit check,
/// so the result equals [`decode_as_with`]'s on the same file.
@@ -473,6 +493,16 @@ 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::Sgi => {
#[cfg(feature = "sgi")]
{
sgi::encode(image, plan)
}
#[cfg(not(feature = "sgi"))]
{
Err(CodecError::unsupported(format, "SGI support requires the sgi feature"))
}
}
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")),
@@ -62,6 +62,8 @@ fn default_build_formats_all_symmetric() {
Format::Dpx,
Format::Cineon,
Format::Iff,
#[cfg(feature = "sgi")]
Format::Sgi,
Format::JpegLs,
];
for f in expected {
@@ -224,6 +226,23 @@ 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());
}
#[cfg(feature = "sgi")]
#[test]
fn detect_magic_sgi() {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
let img = Image::from_u8(5, 3, ChannelLayout::Rgba, (0..60).collect()).unwrap();
let bytes = encode(&img, Format::Sgi, &EncodeOptions::default()).unwrap();
assert_eq!(detect(&bytes), Some(Format::Sgi));
assert_eq!(from_extension("scan.RGB"), Some(Format::Sgi));
assert_eq!(from_extension("scan.bw"), Some(Format::Sgi));
assert_eq!(decode(&bytes).unwrap().data(), img.data());
assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Sgi));
assert_eq!(Format::Sgi.max_dimensions(), Some((65535, 65535)));
// The magic number alone, without a full header, is not SGI.
assert_eq!(detect(&[0x01, 0xda, 0, 1, 0, 2]), None);
assert!(decode_as(Format::Sgi, &[0x01, 0xda]).is_err());
}
#[test]
fn detect_magic_jpeg_ls() {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
@@ -328,3 +347,16 @@ fn extensions_unique_across_formats() {
all.dedup();
assert_eq!(all.len(), n);
}
#[cfg(not(feature = "sgi"))]
#[test]
fn sgi_requires_explicit_feature() {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
assert!(!caps(Format::Sgi).read && !caps(Format::Sgi).write);
assert!(decode_as(Format::Sgi, &[]).is_err());
let image = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap();
assert!(encode(&image, Format::Sgi, &EncodeOptions::default()).is_err());
let mut header = vec![0; 512];
header[..12].copy_from_slice(&[1, 218, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1]);
assert_eq!(detect(&header), None);
}
+2 -2
View File
@@ -899,7 +899,7 @@ struct Layer {
impl Layer {
/// The layer and its flat array of `width` numbers per entry.
fn of(geom: &Record, element: &str, data: &str, index: &[&str], width: usize) -> Option<(Layer, Vec<f64>)> {
let e = geom.child(element)?;
let e = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0))?;
let mapping = match e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("ByPolygonVertex") {
"ByPolygonVertex" => Mapping::PolygonVertex,
"ByVertex" | "ByVertice" | "ByControlPoint" => Mapping::Vertex,
@@ -908,7 +908,7 @@ impl Layer {
_ => return None,
};
let reference = e.child("ReferenceInformationType").and_then(|m| m.str_at(0)).unwrap_or("Direct");
let index = (reference != "Direct").then(|| index.iter().find_map(|n| e.child(n)).map(Record::ints)).flatten();
let index = if matches!(reference, "IndexToDirect" | "Index") { Some(index.iter().find_map(|n| e.child(n))?.ints()) } else { None };
let data = e.child(data)?.floats();
Some((Layer { mapping, index, len: data.len() / width }, data))
}
+10
View File
@@ -628,6 +628,16 @@ fn fbx6_model_holds_its_mesh() {
}
}
#[test]
fn layer_zero_takes_precedence_and_an_indexed_layer_needs_indices() {
let layer_one = "LayerElementUV: 1 { MappingInformationType: \"ByPolygonVertex\" ReferenceInformationType: \"Direct\" UV: 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25 }\n";
let text = ASCII.replace("LayerElementUV: 0 {", &format!("{layer_one} LayerElementUV: 0 {{"));
let scene = parse(text.as_bytes()).unwrap();
assert_eq!(scene.meshes[0].uv_sets[0][0], [0.0, 1.0]);
let missing = ASCII.replace("UVIndex: *7", "UnusedIndex: *7");
assert!(parse(missing.as_bytes()).unwrap().meshes[0].uv_sets.is_empty());
}
#[test]
fn optional_inactive_pre_rotation_keeps_legacy_placements() {
let text = "FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Model: 1, \"Model::A\", \"Null\" { Properties70: { P: \"PreRotation\", \"\", \"\", \"\", 0,0,90 } } }";
+372
View File
@@ -0,0 +1,372 @@
//! DNG Camera Profiles (`.dcp`): the user's own camera colour profiles, read at run time from
//! files they supply (DNG specification, chapter 6 "Camera Profiles"). A `.dcp` is a TIFF-structured
//! file whose header carries the magic `RC` (0x4352) in place of 42, with the profile tags in IFD 0:
//! `UniqueCameraModel`, `ProfileName`, the colour/forward matrices and calibration illuminants, the
//! hue/saturation map, look table and tone curve. Nothing here ships a profile.
use craft_tiff::{Ifd, ParseOptions, Tiff, tags as t};
use crate::ColorData;
/// The magic number of a DNG camera profile file (`IIRC` / `MMCR`).
pub const DCP_MAGIC: u16 = 0x4352;
/// A parsed camera profile.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DcpProfile {
/// `ProfileName` (empty when the file has none).
pub name: String,
/// `UniqueCameraModel`: the camera the profile was made for (empty when absent).
pub camera_model: String,
/// The colour model: illuminants, matrices and the profile look (hue/sat map, look table, tone curve).
pub color: ColorData,
}
/// Errors reading a `.dcp`.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum DcpError {
#[error("not a DNG camera profile (.dcp)")]
NotDcp,
#[error("damaged camera profile: {0}")]
Damaged(String),
#[error("the camera profile has no usable colour matrix")]
NoMatrix,
}
/// Is `bytes` a DCP file (by its header)?
pub fn is_dcp(bytes: &[u8]) -> bool {
matches!(bytes.get(0..4), Some([b'I', b'I', b'R', b'C'] | [b'M', b'M', b'C', b'R']))
}
/// The largest `.dcp` file read or parsed (real profiles are kilobytes to a few MB).
pub const MAX_DCP_BYTES: u64 = 64 << 20;
/// Read the `.dcp` file at `path`, reading at most [`MAX_DCP_BYTES`] + 1 bytes: a larger file is refused
/// before it is loaded into memory.
pub fn read_dcp(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
use std::io::Read;
let mut bytes = Vec::new();
std::fs::File::open(path)?.take(MAX_DCP_BYTES.saturating_add(1)).read_to_end(&mut bytes)?;
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "the camera profile is larger than 64 MiB"));
}
Ok(bytes)
}
/// Parse a `.dcp` file. Files above 64 MiB ([`MAX_DCP_BYTES`]) are refused.
pub fn parse_dcp(bytes: &[u8]) -> Result<DcpProfile, DcpError> {
if !is_dcp(bytes) {
return Err(DcpError::NotDcp);
}
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(DcpError::Damaged("larger than 64 MiB".into()));
}
// the TIFF reader only knows 42 / BigTIFF / raw magics: give it a header with 42 in place of RC
let mut data = bytes.to_vec();
let forty_two: [u8; 2] = if bytes.first() == Some(&b'I') { [42, 0] } else { [0, 42] };
if let Some(m) = data.get_mut(2..4) {
m.copy_from_slice(&forty_two);
}
let opts = ParseOptions { follow_children: false, max_ifds: 4, value_budget: Some(64 << 20), ..Default::default() };
let tiff = Tiff::parse_with(&data, &opts).map_err(|e| DcpError::Damaged(e.to_string()))?;
let ifd: &Ifd = tiff.ifds.first().ok_or_else(|| DcpError::Damaged("no IFD".into()))?;
let mut color = crate::dng::color_data(ifd, ifd, tiff.order);
// a profile carries no white balance or calibration of a particular shot
color.as_shot_neutral = None;
color.as_shot_white_xy = None;
color.analog_balance = None;
color.camera_calibration = [None, None];
color.baseline_sharpness = None;
color.profile.gain_table_map = None;
// only BaselineExposureOffset belongs to a profile (BaselineExposure is the camera's)
color.baseline_exposure =
ifd.f64s(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0).clamp(-4.0, 4.0);
// a single-illuminant profile names one illuminant; its second matrix slot stays empty
if color.color_matrix[0].is_none() {
// the first slot is the one every profile must fill (DNG: ColorMatrix1 is required)
return Err(DcpError::NoMatrix);
}
let text = |tag: u16| ifd.string(tag).map(|s| s.trim_matches(char::from(0)).trim().to_string()).unwrap_or_default();
Ok(DcpProfile { name: text(t::PROFILE_NAME), camera_model: text(t::UNIQUE_CAMERA_MODEL), color })
}
/// Write `p` as a `.dcp` file (little-endian): its name, camera, illuminants, matrices, hue/saturation
/// map, look table, tone curve and baseline exposure offset.
pub fn write_dcp(p: &DcpProfile) -> Result<Vec<u8>, DcpError> {
use craft_tiff::writer::srational;
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
let c = &p.color;
// ColorMatrix1 must remain usable after TIFF rational rounding, or our reader rejects it.
let first = c.color_matrix[0].as_ref().ok_or(DcpError::NoMatrix)?;
let serialized = first
.iter()
.flatten()
.map(|v| {
let (n, d) = srational(*v);
f64::from(n) / f64::from(d)
})
.collect();
if first.iter().flatten().any(|v| !v.is_finite()) || crate::dng::mat3(Some(serialized)).is_none() {
return Err(DcpError::NoMatrix);
}
let mut ifd = IfdBuilder::new();
ifd.set(t::UNIQUE_CAMERA_MODEL, Value::Ascii(p.camera_model.clone()));
ifd.set(t::PROFILE_NAME, Value::Ascii(p.name.clone()));
let mat = |m: &crate::Matrix3| Value::SRational(m.iter().flatten().map(|v| srational(*v)).collect());
for i in 0..2 {
if let Some(m) = &c.color_matrix[i] {
ifd.set([t::CALIBRATION_ILLUMINANT_1, t::CALIBRATION_ILLUMINANT_2][i], Value::Short(vec![c.illuminant[i]]));
ifd.set([t::COLOR_MATRIX_1, t::COLOR_MATRIX_2][i], mat(m));
}
if let Some(m) = &c.forward_matrix[i] {
ifd.set([t::FORWARD_MATRIX_1, t::FORWARD_MATRIX_2][i], mat(m));
}
}
let flat = |table: &crate::profile::HsvTable| Value::Float(table.data.iter().flatten().copied().collect());
let dims =
|table: &crate::profile::HsvTable| Value::Long(vec![table.hue_divisions as u32, table.sat_divisions as u32, table.val_divisions as u32]);
if let Some(a) = c.profile.hue_sat_map[0].as_ref() {
ifd.set(t::PROFILE_HUE_SAT_MAP_DIMS, dims(a));
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_1, flat(a));
if a.srgb_value {
ifd.set(t::PROFILE_HUE_SAT_MAP_ENCODING, Value::Long(vec![1]));
}
if let Some(b) = c.profile.hue_sat_map[1]
.as_ref()
.filter(|b| (b.hue_divisions, b.sat_divisions, b.val_divisions) == (a.hue_divisions, a.sat_divisions, a.val_divisions))
{
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_2, flat(b));
}
}
if let Some(l) = &c.profile.look_table {
ifd.set(t::PROFILE_LOOK_TABLE_DIMS, dims(l));
ifd.set(t::PROFILE_LOOK_TABLE_DATA, flat(l));
if l.srgb_value {
ifd.set(t::PROFILE_LOOK_TABLE_ENCODING, Value::Long(vec![1]));
}
}
if let Some(tc) = &c.profile.tone_curve {
ifd.set(t::PROFILE_TONE_CURVE, Value::Float(tc.points.iter().flatten().copied().collect()));
}
if c.baseline_exposure != 0.0 {
ifd.set(t::BASELINE_EXPOSURE_OFFSET, Value::SRational(vec![srational(c.baseline_exposure)]));
}
let mut out = TiffWriter::new(ByteOrder::Little, false).write(&[ifd]).map_err(|e| DcpError::Damaged(e.to_string()))?;
// the DCP magic in place of 42
if let Some(m) = out.get_mut(2..4) {
m.copy_from_slice(&DCP_MAGIC.to_le_bytes());
}
Ok(out)
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn writing_requires_the_first_color_matrix() {
let mut p = DcpProfile { name: "test".into(), camera_model: "test".into(), color: ColorData::default() };
assert_eq!(write_dcp(&p), Err(DcpError::NoMatrix));
let matrix = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
p.color.color_matrix[1] = Some(matrix);
assert_eq!(write_dcp(&p), Err(DcpError::NoMatrix));
p.color.color_matrix[0] = Some(matrix);
let back = parse_dcp(&write_dcp(&p).unwrap()).unwrap();
assert_eq!(back.color.color_matrix, p.color.color_matrix);
}
#[test]
fn writing_rejects_unusable_or_rounding_singular_first_matrices() {
let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
for matrix in [
[[0.0; 3]; 3],
[[1.0, 1.0, 0.0], [1.0, 1.0 + 1e-8, 0.0], [0.0, 0.0, 1.0]],
[[f64::NAN, 0.0, 0.0], identity[1], identity[2]],
[[f64::INFINITY, 0.0, 0.0], identity[1], identity[2]],
] {
let mut p = DcpProfile { name: "test".into(), camera_model: "test".into(), color: ColorData::default() };
p.color.color_matrix[0] = Some(matrix);
assert_eq!(write_dcp(&p), Err(DcpError::NoMatrix));
}
}
#[test]
fn reading_a_profile_stops_past_the_size_limit() {
let dir = std::env::temp_dir().join(format!("lc-read-dcp-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (fits, big) = (dir.join("fits.dcp"), dir.join("big.dcp"));
std::fs::File::create(&fits).unwrap().set_len(MAX_DCP_BYTES).unwrap();
std::fs::File::create(&big).unwrap().set_len(MAX_DCP_BYTES + 1).unwrap();
assert_eq!(read_dcp(&fits).unwrap().len() as u64, MAX_DCP_BYTES);
assert_eq!(read_dcp(&big).unwrap_err().kind(), std::io::ErrorKind::InvalidData);
let _ = std::fs::remove_dir_all(&dir);
}
/// A minimal single-illuminant profile (our own numbers) built in memory: no profile file is
/// committed to the repository.
pub(crate) fn synthetic_dcp(little: bool, with_hue_sat: bool) -> Vec<u8> {
// entries: (tag, type, count, payload bytes)
let mut entries: Vec<(u16, u16, u32, Vec<u8>)> = Vec::new();
let u16b = |v: u16| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() };
let u32b = |v: u32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() };
let srat = |v: f64| {
let (n, d) = ((v * 10000.0).round() as i32, 10000i32);
let mut b = if little { n.to_le_bytes().to_vec() } else { n.to_be_bytes().to_vec() };
b.extend(if little { d.to_le_bytes() } else { d.to_be_bytes() });
b
};
let ascii = |s: &str| {
let mut b = s.as_bytes().to_vec();
b.push(0);
b
};
entries.push((t::UNIQUE_CAMERA_MODEL, 2, 13, ascii("Test Camera1")));
let m = [0.9, -0.3, -0.05, -0.4, 1.2, 0.2, -0.05, 0.15, 0.7];
entries.push((t::COLOR_MATRIX_1, 10, 9, m.iter().flat_map(|v| srat(*v)).collect()));
entries.push((t::CALIBRATION_ILLUMINANT_1, 3, 1, [u16b(21), vec![0, 0]].concat()));
entries.push((t::PROFILE_NAME, 2, 9, ascii("My Look")));
if with_hue_sat {
// 2 hues × 2 sats × 1 value: hue shift, sat scale, value scale per entry (FLOAT)
entries.push((t::PROFILE_HUE_SAT_MAP_DIMS, 4, 3, [u32b(2), u32b(2), u32b(1)].concat()));
let f = |v: f32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() };
entries.push((
t::PROFILE_HUE_SAT_MAP_DATA_1,
11,
12,
[f(0.0), f(1.0), f(1.0), f(5.0), f(1.1), f(1.0), f(0.0), f(1.0), f(1.0), f(-5.0), f(0.9), f(1.0)].concat(),
));
}
entries.sort_by_key(|e| e.0);
let mut out = if little { b"IIRC".to_vec() } else { b"MMCR".to_vec() };
out.extend(u32b(8));
let n = entries.len();
let mut data_off = 8 + 2 + n * 12 + 4;
let mut ifd = u16b(n as u16);
let mut tail = Vec::new();
for (tag, ty, count, payload) in &entries {
ifd.extend(u16b(*tag));
ifd.extend(u16b(*ty));
ifd.extend(u32b(*count));
if payload.len() <= 4 {
let mut p = payload.clone();
p.resize(4, 0);
ifd.extend(p);
} else {
ifd.extend(u32b(data_off as u32));
tail.extend(payload);
data_off += payload.len();
if data_off % 2 == 1 {
tail.push(0);
data_off += 1;
}
}
}
ifd.extend(u32b(0));
out.extend(ifd);
out.extend(tail);
out
}
#[test]
fn reads_a_single_illuminant_profile_in_both_byte_orders() {
for little in [true, false] {
let p = parse_dcp(&synthetic_dcp(little, true)).unwrap();
assert_eq!(p.name, "My Look");
assert_eq!(p.camera_model, "Test Camera1");
assert_eq!(p.color.illuminant, [21, 0]);
let m = p.color.color_matrix[0].unwrap();
assert!((m[1][1] - 1.2).abs() < 1e-4, "{m:?}");
assert!(p.color.color_matrix[1].is_none());
assert!(p.color.profile.hue_sat_map[0].is_some());
assert!(p.color.as_shot_neutral.is_none());
}
assert!(parse_dcp(&synthetic_dcp(true, false)).unwrap().color.profile.hue_sat_map[0].is_none());
}
#[test]
fn rejects_other_files_and_damage_without_panicking() {
assert_eq!(parse_dcp(b"II*\0\x08\0\0\0"), Err(DcpError::NotDcp));
assert_eq!(parse_dcp(b""), Err(DcpError::NotDcp));
let good = synthetic_dcp(true, true);
for cut in [4, 8, 10, 30, good.len() / 2, good.len() - 1] {
let _ = parse_dcp(&good[..cut]);
}
let mut bad = good.clone();
bad[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
assert!(parse_dcp(&bad).is_err());
}
/// The hue/saturation map's header is the user's file too: dimensions far past the data, an entry count
/// claiming 4 billion values, or a data offset past the end drop the map (the matrix still reads) instead of
/// allocating for the claim or reading past the file.
#[test]
fn a_hostile_hue_sat_map_header_drops_the_map() {
let good = synthetic_dcp(true, true);
let at = |needle: &[u8]| good.windows(needle.len()).position(|w| w == needle).unwrap();
let le = |v: &[u32]| v.iter().flat_map(|x| x.to_le_bytes()).collect::<Vec<u8>>();
let dims = at(&le(&[2, 2, 1]));
// the map data's directory entry: tag, FLOAT, count 12, offset
let entry = at(&[t::PROFILE_HUE_SAT_MAP_DATA_1.to_le_bytes(), 11u16.to_le_bytes()].concat());
let patched = |at: usize, bytes: &[u8]| {
let mut b = good.clone();
b[at..at + bytes.len()].copy_from_slice(bytes);
b
};
for (what, bytes) in [
("4096³ dimensions", patched(dims, &le(&[4096, 4096, 4096]))),
("dimensions past u32 products", patched(dims, &le(&[u32::MAX, u32::MAX, u32::MAX]))),
("a zero dimension", patched(dims, &le(&[0, 2, 1]))),
("4 billion values", patched(entry + 4, &le(&[u32::MAX]))),
("data past the end", patched(entry + 8, &le(&[u32::MAX - 16]))),
] {
let p = parse_dcp(&bytes).unwrap_or_else(|e| panic!("{what}: {e:?}"));
assert!(p.color.profile.hue_sat_map[0].is_none(), "{what}: the map is dropped");
assert!(p.color.color_matrix[0].is_some(), "{what}: the matrix still reads");
}
}
#[test]
fn written_profiles_read_back() {
let p = parse_dcp(&synthetic_dcp(false, true)).unwrap();
let back = parse_dcp(&write_dcp(&p).unwrap()).unwrap();
assert_eq!(back.name, p.name);
assert_eq!(back.camera_model, p.camera_model);
assert_eq!(back.color.illuminant, p.color.illuminant);
assert_eq!(back.color.profile.hue_sat_map, p.color.profile.hue_sat_map);
let (a, b) = (back.color.color_matrix[0].unwrap(), p.color.color_matrix[0].unwrap());
assert!(a.iter().flatten().zip(b.iter().flatten()).all(|(x, y)| (x - y).abs() < 1e-6));
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod boundary_tests {
use super::*;
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
fn profile_bytes(offset: f64, matrix: bool) -> Vec<u8> {
let mut ifd = IfdBuilder::new();
if matrix {
ifd.set(t::COLOR_MATRIX_1, Value::SRational(vec![(1, 1), (0, 1), (0, 1), (0, 1), (1, 1), (0, 1), (0, 1), (0, 1), (1, 1)]));
}
ifd.set(t::BASELINE_EXPOSURE_OFFSET, Value::Double(vec![offset]));
ifd.set(t::BASELINE_EXPOSURE, Value::Double(vec![10.0]));
ifd.set(t::AS_SHOT_NEUTRAL, Value::Double(vec![0.5, 1.0, 0.7]));
let mut bytes = TiffWriter::new(ByteOrder::Little, false).write(&[ifd]).unwrap();
bytes[2..4].copy_from_slice(&DCP_MAGIC.to_le_bytes());
bytes
}
#[test]
fn profile_requires_matrix_and_never_keeps_shot_exposure_or_white_balance() {
assert_eq!(parse_dcp(&profile_bytes(0.0, false)), Err(DcpError::NoMatrix));
for (offset, expected) in [(9.0, 4.0), (-9.0, -4.0), (f64::INFINITY, 0.0), (f64::NAN, 0.0)] {
let profile = parse_dcp(&profile_bytes(offset, true)).unwrap();
assert_eq!(profile.color.baseline_exposure, expected);
assert_eq!(profile.color.as_shot_neutral, None);
assert_eq!(profile.color.camera_calibration, [None, None]);
assert_eq!(profile.color.profile.gain_table_map, None);
}
}
}
+1 -1
View File
@@ -91,7 +91,7 @@ fn permute<T: Copy + Default, F: Fn(usize, usize) -> usize>(v: &mut Vec<T>, w: u
}
/// A 3×3 matrix tag: nine finite values that can be inverted.
fn mat3(v: Option<Vec<f64>>) -> Option<Matrix3> {
pub(crate) fn mat3(v: Option<Vec<f64>>) -> Option<Matrix3> {
let v = v?;
if v.len() != 9 || v.iter().any(|x| !x.is_finite()) {
return None;
+1
View File
@@ -465,6 +465,7 @@ mod tests {
color: crate::ColorData::default(),
wb_multipliers: None,
linearized: false,
linearization_table: None,
pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
camera: Default::default(),
+3
View File
@@ -68,6 +68,9 @@ mod crw;
mod crx;
#[cfg(feature = "cr3")]
mod crx_wavelet;
/// DNG camera-profile file parsing and writing.
#[cfg(feature = "dng")]
pub mod dcp;
#[cfg(feature = "dng")]
mod dng;
#[cfg(feature = "dng")]
+73 -1
View File
@@ -181,6 +181,12 @@ pub struct CcittRawOutput {
/// Decodes CCITT Group 3 or Group 4 fax `data` with caller-specified `params` and `max_rows`,
/// returning packed MSB-first rows, rows decoded, input bytes consumed, and any decode error.
///
/// The rows are read as the parameters say, as PostScript's `CCITTFaxDecode` filter reads them:
/// on bytes exactly when `encoded_byte_align` is set, whatever codes the data holds (unlike
/// [`decode_ccitt_within`], which reads Group 3 rows with end-of-line codes by those codes). At
/// most `rows` rows are read when it is above 0; a caller that ends the rows at the end-of-block
/// code alone passes 0.
pub fn decode_ccitt_raw(data: &[u8], p: &CcittParams, max_rows: u32) -> Result<CcittRawOutput, CodecError> {
if p.columns <= 0 || p.columns > i64::from(MAX_CCITT_COLUMNS) {
return Err(CodecError::Corrupt { filter: "CCITTFaxDecode", detail: format!("CCITT columns {} outside 1..={MAX_CCITT_COLUMNS}", p.columns) });
@@ -192,7 +198,7 @@ pub fn decode_ccitt_raw(data: &[u8], p: &CcittParams, max_rows: u32) -> Result<C
rows,
end_of_block: p.end_of_block,
end_of_line: p.end_of_line,
rows_are_byte_aligned: p.encoded_byte_align && (p.k < 0 || !starts_with_end_of_line(data)),
rows_are_byte_aligned: p.encoded_byte_align,
encoding: match p.k {
..0 => hayro_ccitt::EncodingMode::Group4,
0 => hayro_ccitt::EncodingMode::Group3_1D,
@@ -579,6 +585,72 @@ mod tests {
}
}
/// `decode_ccitt_raw` reads rows on bytes exactly when `encoded_byte_align` says, as
/// PostScript's `CCITTFaxDecode` filter (and `hayro-ccitt` itself) reads them, in every layout
/// of Group 3 rows: with or without end-of-line codes, fill before each row or before each
/// code's end, an RTC after the rows, and room for more rows than they hold. The codes in the
/// data don't make rows the parameters put on bytes read unaligned (which read an aligned RTC
/// as one more row, #778).
#[test]
fn raw_group_3_rows_follow_the_parameters() {
let mut cases = 0;
for seed in 0..24u64 {
let (columns, rows) = (8 + (seed as u32 * 37) % 200, 2 + seed as u32 % 20);
let runs = text_like(seed, columns, rows);
for k in [0i64, 1, 2] {
for (codes, fill) in [(false, Fill::None), (false, Fill::RowStart), (true, Fill::None), (true, Fill::RowStart), (true, Fill::CodeEnd)]
{
let mut data = group_3(&runs, codes, k > 0, fill);
if codes {
// RTC: six end-of-line codes (with their 1 tag bits for `k` above 0), each on a byte.
let eol: [u8; 2] = if k > 0 { [0x00, 0x18] } else { [0x00, 0x10] };
(0..6).for_each(|_| data.extend_from_slice(&eol));
}
for (aligned, end_of_line, end_of_block) in [(true, true, true), (true, false, true), (true, true, false), (false, true, true)] {
let p = CcittParams {
k,
columns: i64::from(columns),
end_of_line,
encoded_byte_align: aligned,
end_of_block,
..CcittParams::default()
};
let max_rows = rows + 8;
let raw = decode_ccitt_raw(&data, &p, max_rows).unwrap();
let settings = hayro_ccitt::DecodeSettings {
columns,
rows: max_rows,
end_of_block,
end_of_line,
rows_are_byte_aligned: aligned,
encoding: if k > 0 { hayro_ccitt::EncodingMode::Group3_2D { k: k as u32 } } else { hayro_ccitt::EncodingMode::Group3_1D },
invert_black: false,
};
let mut filter = Rows::new(columns as usize, max_rows as usize, false);
let used = hayro_ccitt::decode(&data, &mut filter, &mut hayro_ccitt::DecoderContext::new(settings));
let case = format!(
"seed {seed}, {columns} × {rows}, k {k}, codes {codes}, fill {fill:?}, aligned {aligned}, eol {end_of_line}, eob {end_of_block}"
);
assert_eq!((&raw.data, raw.rows_decoded as usize), (&filter.out, filter.done), "{case}");
if let Ok(used) = used {
assert_eq!((raw.bytes_consumed, raw.error), (used, None), "{case}");
}
cases += 1;
}
}
}
}
assert_eq!(cases, 24 * 3 * 5 * 4);
// Rows on bytes with their codes, as `fax`'s writers lay them out, read whole: the rows
// and nothing after the RTC.
let runs = text_like(7, 74, 4);
let mut data = group_3(&runs, true, false, Fill::RowStart);
(0..6).for_each(|_| data.extend_from_slice(&[0x00, 0x10]));
let p = CcittParams { k: 0, columns: 74, end_of_line: true, encoded_byte_align: true, ..CcittParams::default() };
let raw = decode_ccitt_raw(&data, &p, 9).unwrap();
assert_eq!((raw.data, raw.rows_decoded), (pack(&runs, 74), 4));
}
proptest::proptest! {
/// Any bitmap, of noise or of runs, decodes to its own bits, through an independent decoder
/// and through `decode_ccitt`.
+3 -3
View File
@@ -46,7 +46,7 @@ assert_eq!((rows, sheet.map(|s| s.children.len())), (3, Ok(0)));
| `max_input` | 256 MiB | `TooLarge` |
| `max_depth` | 256 levels (the root is 1) | `TooDeep` |
| `max_nodes` | 2,000,000 nodes kept in the tree: elements, text stretches, kept CDATA, instructions and comments | `TooManyNodes` |
| `max_attrs` | 1024 on one start tag, namespace declarations and malformed ones included; with canonical names, also the declarations in scope at once | `TooManyAttrs` |
| `max_attrs` | 1024 on one start tag, namespace declarations and malformed ones included; with canonical names or strict reads, also the declarations in scope at once | `TooManyAttrs` |
| `max_attrs_total` | 4,000,000 attributes kept in the tree, over all its elements | `TooManyAttrsTotal` |
A limit exceeded is an error, never a panic and never a truncated tree. `parse_streaming_partial`
@@ -73,9 +73,9 @@ A lenient read keeps what a damaged document had:
- reading stops when the root element ends: nothing after it is looked at;
- a malformed attribute is skipped; bytes that are not UTF-8 become U+FFFD.
A strict read makes each of those an error, and also a second root element and text outside the
A strict read rejects duplicate raw or expanded attribute names (while allowing undeclared prefixes in fragments). It makes each of those an error, and also a second root element and text outside the
root, in the pull reader as in the tree. Markup the tokenizer cannot read (`<a b='1`, an unterminated comment) is an error either
way. A repeated attribute is kept each time in both.
way. A lenient read keeps repeated attributes.
### Text
+53 -2
View File
@@ -1,6 +1,7 @@
//! Reading: the pull reader over `quick-xml`, the tree builder on top of it, and streaming.
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::ops::Range;
use quick_xml::Reader;
@@ -12,6 +13,10 @@ use crate::write::is_xml_char;
/// The namespace the `xml` prefix is always bound to.
pub const XML_NS: &str = "http://www.w3.org/XML/1998/namespace";
/// [`Events::scope_ids`] id of the XML namespace, bound to `xml:` without a declaration.
const XML_NS_ID: usize = usize::MAX;
/// Expanded-name id of an attribute in no namespace (or with an undeclared prefix).
const NO_NS_ID: usize = usize::MAX - 1;
/// What the reader refuses to exceed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -469,6 +474,10 @@ pub struct Events<'o, 'i> {
end_names: bool,
open: Vec<Open>,
scope: Vec<(String, String)>,
/// For each binding in `scope`, an id shared by every binding of an equal URI (the index of
/// the first such binding, or [`XML_NS_ID`]), with the URI's hash. Strict reads compare
/// expanded attribute names by this id instead of copying the URI for each attribute.
scope_ids: Vec<(u64, usize)>,
/// The end of an empty element, due after its start.
pending: bool,
/// Whether a root element has started.
@@ -500,7 +509,7 @@ impl<'o, 'i> Events<'o, 'i> {
cfg.trim_text_start = false;
cfg.trim_text_end = false;
cfg.expand_empty_elements = false;
Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), pending: false, rooted: false, span: 0..0 }
Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), scope_ids: Vec::new(), pending: false, rooted: false, span: 0..0 }
}
/// The bytes of the input the last event was read from. Both events of an empty element
@@ -614,6 +623,7 @@ impl<'o, 'i> Events<'o, 'i> {
fn end(&mut self) -> Event {
let (scope, name) = self.open.pop().map_or((0, String::new()), |o| (o.scope, o.name));
self.scope.truncate(scope);
self.scope_ids.truncate(scope);
Event::End { name }
}
@@ -669,6 +679,7 @@ impl<'o, 'i> Events<'o, 'i> {
if is_ns_decl(key) {
let prefix = self.decode(key.get(6..).unwrap_or_default())?.into_owned();
let uri = self.attr_value(&a.value)?;
self.scope_ids.push(self.uri_id(&uri));
self.scope.push((prefix, uri));
}
}
@@ -680,6 +691,20 @@ impl<'o, 'i> Events<'o, 'i> {
Ok(())
}
/// The id an expanded name uses for `uri`: one hash and at most one full comparison per
/// declaration, so a long URI costs its length once however many attributes use it.
fn uri_id(&self, uri: &str) -> (u64, usize) {
use std::hash::{Hash, Hasher};
let mut h = std::collections::hash_map::DefaultHasher::new();
uri.hash(&mut h);
let hash = h.finish();
if uri == XML_NS {
return (hash, XML_NS_ID);
}
let first = self.scope_ids.iter().zip(&self.scope).position(|((h, _), (_, u))| *h == hash && u == uri);
(hash, first.map_or(self.scope.len(), |i| self.scope_ids.get(i).map_or(i, |(_, id)| *id)))
}
/// In a strict read: is the reader before or after the root element?
fn outside_root(&self) -> bool {
self.opts.strict && self.open.is_empty()
@@ -699,7 +724,7 @@ impl<'o, 'i> Events<'o, 'i> {
_ => None,
};
let scope = self.scope.len();
if canonical.is_some() {
if canonical.is_some() || self.opts.strict {
self.declare(e)?;
}
let raw = e.name();
@@ -707,6 +732,8 @@ impl<'o, 'i> Events<'o, 'i> {
let mut out = Vec::new();
let mut attrs = e.attributes();
attrs.with_checks(false);
let mut raw_names = BTreeSet::new();
let mut expanded_names = BTreeSet::new();
for (seen, a) in attrs.enumerate() {
if seen >= limits.max_attrs {
return Err(Error::TooManyAttrs(limits.max_attrs));
@@ -718,6 +745,30 @@ impl<'o, 'i> Events<'o, 'i> {
};
let key = a.key.as_ref();
let decl = is_ns_decl(key);
if self.opts.strict {
let raw_key = self.decode(key)?;
if !raw_names.insert(raw_key.to_string()) {
return self.syntax(format!("duplicate attribute `{raw_key}`"));
}
if !decl {
// Default namespaces do not apply to attributes. Keep undeclared prefixes
// distinct so standalone fragments retain the reader's existing behaviour.
let expanded = match raw_key.split_once(':') {
Some(("xml", local)) => (XML_NS_ID, local),
Some((prefix, local)) => self
.scope
.iter()
.zip(&self.scope_ids)
.rev()
.find(|((p, _), _)| p == prefix)
.map_or((NO_NS_ID, raw_key.as_ref()), |(_, (_, id))| (*id, local)),
None => (NO_NS_ID, raw_key.as_ref()),
};
if !expanded_names.insert((expanded.0, expanded.1.to_string())) {
return self.syntax(format!("duplicate expanded attribute `{raw_key}`"));
}
}
}
if decl && !self.opts.ns_decls {
continue;
}
+43 -1
View File
@@ -117,7 +117,11 @@ fn an_attribute_flood_is_refused_at_the_limit() {
let opts = opts.with_limits(limits);
for (k, kind) in kinds.iter().enumerate() {
let _ = opts.parse(tag(16, kind).as_bytes());
assert_eq!(opts.parse(tag(17, kind).as_bytes()).err().map(|e| e.is_limit() || (opts.strict && k == 3)), Some(true), "kind {k}");
assert_eq!(
opts.parse(tag(17, kind).as_bytes()).err().map(|e| e.is_limit() || (opts.strict && (k == 1 || k == 3))),
Some(true),
"kind {k}"
);
assert!(opts.first_start(tag(17, kind).as_bytes(), "a").is_none());
}
assert_eq!(opts.parse(tag(17, kinds[0]).as_bytes()), Err(Error::TooManyAttrs(16)));
@@ -524,3 +528,41 @@ fn a_strict_pull_reader_refuses_what_is_outside_the_root() {
// white space, comments and instructions may stand outside the root
assert!(read(strict, b"<?xml version='1.0'?>\r\n <!-- c -->\t<a>x</a>\n<?pi?> ").is_ok());
}
#[test]
fn strict_attributes_are_unique_before_name_rewriting() {
use craft_xml::{Names, Options};
for names in [Names::AsWritten, Names::Local] {
let strict = Options { names, ..Options::default() }.strict();
for xml in [
"<r a='1' a='2'/>",
"<r xmlns:p='urn:x' xmlns:q='urn:x' p:a='1' q:a='2'/>",
"<r xml:a='1' xmlns:p='http://www.w3.org/XML/1998/namespace' p:a='2'/>",
"<r xmlns:p='urn:x'><c xmlns:q='urn:x' p:a='1' q:a='2'/></r>",
] {
assert!(strict.parse(xml.as_bytes()).is_err(), "{xml}");
let mut events = strict.events(xml.as_bytes());
assert!(
loop {
match events.next() {
Ok(Some(_)) => {}
Ok(None) => break false,
Err(_) => break true,
}
},
"{xml}"
);
}
for xml in [
"<r xmlns='urn:x' xmlns:p='urn:x' a='1' p:a='2'/>",
"<r p:a='1' q:a='2'/>",
"<r xmlns:p='urn:x'><c xmlns:p='urn:y' xmlns:q='urn:x' p:a='1' q:a='2'/></r>",
] {
assert!(strict.parse(xml.as_bytes()).is_ok(), "{xml}");
}
assert!(Options { names, ..Options::default() }.parse(b"<r a='1' a='2'/>").is_ok());
}
let strict = Options { limits: craft_xml::Limits { max_attrs: 2, ..craft_xml::Limits::DEFAULT }, ..Options::default() }.strict();
assert!(strict.parse(b"<r xmlns:a='a' xmlns:b='b'><c xmlns:c='c'/></r>").is_err());
assert!(strict.parse(b"<r a='1' b='2' c='3'/>").is_err());
}