mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-21 20:38:15 +00:00
Merge pull request #4298 from storytold/codex/shared-png-consumer
Use shared PNG header and color inspection
This commit is contained in:
Generated
+23
-43
@@ -1204,7 +1204,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9b
|
||||
[[package]]
|
||||
name = "craft-crw"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1300,11 +1300,11 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4#947dc5fb0ec436c0841f560eac0d65c6ad055ed4"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
|
||||
dependencies = [
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"crc32fast",
|
||||
"exr",
|
||||
"flate2",
|
||||
@@ -1343,7 +1343,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4#947dc5fb0ec436c0841f560eac0d65c6ad055ed4"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1381,23 +1381,11 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.0.1",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4#947dc5fb0ec436c0841f560eac0d65c6ad055ed4"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.0.1",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
@@ -1443,12 +1431,12 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-raw"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-crw",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"jxl-oxide",
|
||||
"miniz_oxide 0.9.1",
|
||||
"rayon",
|
||||
@@ -1470,16 +1458,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4#947dc5fb0ec436c0841f560eac0d65c6ad055ed4"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
@@ -3990,11 +3969,12 @@ dependencies = [
|
||||
"craft-gif",
|
||||
"craft-heif",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f)",
|
||||
"craft-jxl",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-psd",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-video",
|
||||
"dhat",
|
||||
"jpeg-encoder 0.7.1",
|
||||
@@ -4075,7 +4055,7 @@ dependencies = [
|
||||
"craft-mime",
|
||||
"craft-pdf",
|
||||
"craft-plist",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"crc32fast",
|
||||
"fs4",
|
||||
"harfrust 0.13.3",
|
||||
@@ -4232,10 +4212,10 @@ version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-heif",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-psd",
|
||||
"craft-raw",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"lightcraft-geom",
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -4246,7 +4226,7 @@ dependencies = [
|
||||
name = "lightcraft-pipeline"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"dhat",
|
||||
"lightcraft-color",
|
||||
"lightcraft-develop",
|
||||
@@ -4265,7 +4245,7 @@ dependencies = [
|
||||
name = "lightcraft-preview"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"lightcraft-raster",
|
||||
"log",
|
||||
"siphasher",
|
||||
@@ -4288,10 +4268,10 @@ name = "lightcraft-raw"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"craft-jxl",
|
||||
"craft-raw",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"dhat",
|
||||
"jixel",
|
||||
"lightcraft-color",
|
||||
@@ -4311,7 +4291,7 @@ dependencies = [
|
||||
name = "lightcraft-scenes"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=947dc5fb0ec436c0841f560eac0d65c6ad055ed4)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
|
||||
"lightcraft-color",
|
||||
"lightcraft-raster",
|
||||
"rayon",
|
||||
|
||||
+4
-5
@@ -22,10 +22,9 @@ craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "b77af389f3
|
||||
craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "478c50560d3c6fad3d613af2a30b35c3bc9f4c0f", default-features = false, features = ["decode", "encode"] }
|
||||
jixel = "=0.3.4"
|
||||
# Photo metadata: EXIF, IPTC-IIM, XMP packets and GPX track logs (craft-libs#949)
|
||||
craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
|
||||
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "478c50560d3c6fad3d613af2a30b35c3bc9f4c0f" }
|
||||
# Shared TIFF preflight plus image codec compatibility (craft-libs#1630).
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "947dc5fb0ec436c0841f560eac0d65c6ad055ed4" }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
|
||||
craft-video = { git = "https://github.com/storytold/craft-libs", rev = "5c91598ccc86537a779f438d520fa6406c26efe6", features = ["av1", "av1-large-encode", "threads"] }
|
||||
# Isolate AVIF threading from the existing PNG/TIFF/WebP shared revision.
|
||||
craft-avif-encode = { package = "craft-image-codecs", git = "https://github.com/storytold/craft-libs", rev = "5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a", default-features = false, features = ["avif", "threads"] }
|
||||
@@ -48,10 +47,10 @@ lightcraft-wincolor = { path = "crates/wincolor" }
|
||||
lightcraft-color = { path = "crates/color" }
|
||||
lightcraft-raster = { path = "crates/raster" }
|
||||
# TIFF/IFD: the shared reader and writer (moved from crates/tiff; craft-libs#783).
|
||||
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
|
||||
# Camera raw files, the format layer: every maker but CR3, ARW, RAF, RW2 and ORF (craft-libs#862 and the maker PRs).
|
||||
# On the same commit as craft-tiff: its errors and IFDs are shared.
|
||||
craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
|
||||
lightcraft-raw = { path = "crates/raw" }
|
||||
lightcraft-codecs = { path = "crates/codecs" }
|
||||
lightcraft-meta = { path = "crates/meta" }
|
||||
|
||||
@@ -16,6 +16,7 @@ craft-gainmap = { workspace = true }
|
||||
craft-cms = { workspace = true }
|
||||
craft-jpeg = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
craft-metadata = { workspace = true }
|
||||
craft-tiff = { workspace = true }
|
||||
craft-gif = { workspace = true }
|
||||
craft-psd = { workspace = true }
|
||||
|
||||
@@ -80,6 +80,10 @@ impl<'a> Tiff<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub use craft_metadata::compat_orientation::minimal_exif;
|
||||
pub(crate) use craft_metadata::compat_orientation::summarize;
|
||||
|
||||
#[cfg(test)]
|
||||
/// What the decoder cares about from an EXIF (TIFF-structured) blob.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub(crate) struct ExifSummary {
|
||||
@@ -90,7 +94,8 @@ pub(crate) struct ExifSummary {
|
||||
pub adobe_rgb_hint: bool,
|
||||
}
|
||||
|
||||
pub(crate) fn summarize(blob: &[u8]) -> ExifSummary {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn legacy_reference(blob: &[u8]) -> ExifSummary {
|
||||
let mut s = ExifSummary::default();
|
||||
let Some(t) = Tiff::new(blob) else { return s };
|
||||
let Some(ifd0) = t.first_ifd() else { return s };
|
||||
@@ -130,23 +135,6 @@ pub(crate) fn summarize(blob: &[u8]) -> ExifSummary {
|
||||
s
|
||||
}
|
||||
|
||||
/// Build a tiny EXIF blob (little-endian TIFF) with just an orientation tag. Used by tests and by
|
||||
/// callers that need to stamp orientation on export when no source EXIF exists.
|
||||
pub fn minimal_exif(orientation: u16) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(26);
|
||||
v.extend_from_slice(b"II");
|
||||
v.extend_from_slice(&42u16.to_le_bytes());
|
||||
v.extend_from_slice(&8u32.to_le_bytes());
|
||||
v.extend_from_slice(&1u16.to_le_bytes());
|
||||
v.extend_from_slice(&0x0112u16.to_le_bytes());
|
||||
v.extend_from_slice(&3u16.to_le_bytes());
|
||||
v.extend_from_slice(&1u32.to_le_bytes());
|
||||
v.extend_from_slice(&orientation.to_le_bytes());
|
||||
v.extend_from_slice(&[0, 0]);
|
||||
v.extend_from_slice(&0u32.to_le_bytes());
|
||||
v
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -160,7 +148,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn garbage_ok() {
|
||||
assert_eq!(summarize(b"II*\0\xff\xff\xff\xff"), ExifSummary::default());
|
||||
assert_eq!(summarize(&[]), ExifSummary::default());
|
||||
assert_eq!(legacy_reference(b"II*\0\xff\xff\xff\xff"), ExifSummary::default());
|
||||
assert_eq!(legacy_reference(&[]), ExifSummary::default());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,7 @@ fn err(e: impl std::fmt::Display) -> Error {
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
if bytes.starts_with(b"\x89PNG\r\n\x1a\n") && bytes.get(12..16) == Some(b"IHDR") && bytes.len() >= 24 {
|
||||
let w = u32::from_be_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]);
|
||||
let h = u32::from_be_bytes([bytes[20], bytes[21], bytes[22], bytes[23]]);
|
||||
if let Some((w, h)) = craft_image_codecs::png_probe::declared_dimensions(bytes) {
|
||||
check_size(F, w as u64, h as u64, opts)?;
|
||||
}
|
||||
let ic_opts = craft_image_codecs::DecodeOptions {
|
||||
@@ -52,106 +50,24 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
finish(F, raw, meta, (w, h), opts)
|
||||
}
|
||||
|
||||
/// Stored dimensions and EXIF orientation (`eXIf`, wherever it is) from the chunk headers, without
|
||||
/// inflating the image data. Refused, as a decode would refuse it: an invalid IHDR, an indexed
|
||||
/// image without a palette, no IDAT, or a file that ends before the chunk after the last IDAT
|
||||
/// begins (truncated image data). Chunk CRCs and the deflate stream are not checked.
|
||||
/// Stored dimensions and EXIF orientation without inflating pixels.
|
||||
pub(crate) fn header(b: &[u8]) -> Result<(u32, u32, u16)> {
|
||||
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"));
|
||||
}
|
||||
check_size(F, w as u64, h as u64, &DecodeOptions::default())?;
|
||||
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
|
||||
let (w, h, orientation) =
|
||||
craft_image_codecs::png_probe::header_with_max_pixels(b, DecodeOptions::default().max_pixels).map_err(|error| match error {
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) => {
|
||||
let (w, h) = craft_image_codecs::png_probe::declared_dimensions(b).unwrap_or((0, 0));
|
||||
Error::TooLarge(w as u64, h as u64)
|
||||
}
|
||||
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(crate::exif::summarize).unwrap_or_default().orientation.unwrap_or(1)))
|
||||
other => err(other),
|
||||
})?;
|
||||
Ok((w, h, orientation))
|
||||
}
|
||||
|
||||
fn container_hint_from_chunks(b: &[u8]) -> Option<SourceSpace> {
|
||||
if !b.starts_with(b"\x89PNG\r\n\x1a\n") {
|
||||
return None;
|
||||
}
|
||||
let mut has_srgb = false;
|
||||
let mut gamma: Option<f32> = None;
|
||||
let mut chrm: Option<[f64; 8]> = None;
|
||||
let mut p = 8usize;
|
||||
while let Some(len_bytes) = b.get(p..p + 4) {
|
||||
let len = u32::from_be_bytes([len_bytes[0], len_bytes[1], len_bytes[2], len_bytes[3]]) as usize;
|
||||
let Some(kind) = b.get(p + 4..p + 8) else { break };
|
||||
let Some(payload) = b.get(p + 8..p + 8 + len) else { break };
|
||||
if kind == b"sRGB" {
|
||||
has_srgb = true;
|
||||
} else if kind == b"gAMA" && payload.len() == 4 {
|
||||
let v = u32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]) as f32 / 100_000.0;
|
||||
if v > 0.0 && v.is_finite() {
|
||||
gamma = Some(v);
|
||||
}
|
||||
} else if kind == b"cHRM" && payload.len() == 32 {
|
||||
let mut vals = [0.0f64; 8];
|
||||
for (i, dst) in vals.iter_mut().enumerate() {
|
||||
let raw = i32::from_be_bytes([payload[i * 4], payload[i * 4 + 1], payload[i * 4 + 2], payload[i * 4 + 3]]);
|
||||
*dst = raw as f64 / 100_000.0;
|
||||
}
|
||||
chrm = Some(vals);
|
||||
} else if kind == b"IDAT" || kind == b"IEND" {
|
||||
break;
|
||||
}
|
||||
p = p.saturating_add(12).saturating_add(len);
|
||||
}
|
||||
let chunks = craft_image_codecs::png_probe::color_chunks(b)?;
|
||||
let has_srgb = chunks.srgb;
|
||||
let gamma = chunks.gamma;
|
||||
let chrm = chunks.chromaticities;
|
||||
if has_srgb {
|
||||
return Some(SourceSpace::named(NamedSpace::Srgb, SpaceOrigin::Container));
|
||||
}
|
||||
@@ -190,6 +106,50 @@ fn rgb_to_xyz_d50_checked(space: &RgbSpace) -> Option<Mat3> {
|
||||
mod tests {
|
||||
use crate::DecodeOptions;
|
||||
|
||||
/// An IFD0 with two Orientation entries (1, then 6): `read_header` and `decode` read it with
|
||||
/// the same reader, so they agree on the orientation.
|
||||
#[test]
|
||||
fn header_and_decode_agree_on_conflicting_orientation_entries() {
|
||||
let image = craft_image_codecs::Image::from_u8(2, 3, craft_image_codecs::ChannelLayout::Gray, vec![0; 6]).unwrap();
|
||||
let base = craft_image_codecs::encode(&image, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
let mut exif = b"II*\0\x08\0\0\0\x02\0".to_vec();
|
||||
for value in [1u16, 6] {
|
||||
exif.extend_from_slice(&0x0112u16.to_le_bytes());
|
||||
exif.extend_from_slice(&3u16.to_le_bytes());
|
||||
exif.extend_from_slice(&1u32.to_le_bytes());
|
||||
exif.extend_from_slice(&value.to_le_bytes());
|
||||
exif.extend_from_slice(&[0, 0]);
|
||||
}
|
||||
exif.extend_from_slice(&0u32.to_le_bytes());
|
||||
let mut png = base.clone();
|
||||
let iend = png.len() - 12;
|
||||
png.splice(iend..iend, png_chunk(*b"eXIf", &exif));
|
||||
let header = crate::read_header(&png).unwrap();
|
||||
let decoded = crate::decode(&png, DecodeOptions::default()).unwrap();
|
||||
assert_eq!((header.width, header.height), (2, 3));
|
||||
assert_eq!(header.orientation, decoded.orientation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exif_orientation_mutation_parity() {
|
||||
let image = craft_image_codecs::Image::from_u8(1, 1, craft_image_codecs::ChannelLayout::Gray, vec![0]).unwrap();
|
||||
let base = craft_image_codecs::encode(&image, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
let exif = crate::exif::minimal_exif(6);
|
||||
for at in 0..exif.len() {
|
||||
for value in [0, 255] {
|
||||
let mut data = exif.clone();
|
||||
data[at] = value;
|
||||
let mut png = base.clone();
|
||||
let iend = png.len() - 12;
|
||||
png.splice(iend..iend, png_chunk(*b"eXIf", &data));
|
||||
let old = crate::exif::legacy_reference(&data);
|
||||
let shared = crate::exif::summarize(&data);
|
||||
assert_eq!((shared.orientation, shared.thumbnail, shared.adobe_rgb_hint), (old.orientation, old.thumbnail, old.adobe_rgb_hint));
|
||||
assert_eq!(super::header(&png).unwrap().2, old.orientation.unwrap_or(1), "at {at}, value {value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A compressed text chunk is expanded by `png` through our own cap: an XMP document
|
||||
/// bigger than it is dropped, not inflated. Compressing an 8 MiB string is a few
|
||||
/// kilobytes — the reverse of what the unbounded `get_text()` path allowed.
|
||||
|
||||
@@ -101,14 +101,17 @@ fn cr3_file(coding: &[u8], sample: &[u8]) -> Vec<u8> {
|
||||
let mut co64 = vec![0u8; 4];
|
||||
co64.extend(1u32.to_be_bytes()); // entry count
|
||||
co64.extend([0u8; 8]); // sample offset, patched once the layout is known
|
||||
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
|
||||
// One sample in chunk 1, using sample description 1. The shared parser
|
||||
// deliberately refuses sample access without this mapping.
|
||||
let stsc: Vec<u8> = [0u32, 1, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect();
|
||||
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsc", &stsc), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
|
||||
let trak = box_bytes(b"trak", &box_bytes(b"mdia", &box_bytes(b"minf", &box_bytes(b"stbl", &tables))));
|
||||
let mut file = box_bytes(b"ftyp", b"crx \0\0\0\x01crx isom");
|
||||
file.extend(box_bytes(b"moov", &trak));
|
||||
let offset = file.len();
|
||||
file.extend_from_slice(sample);
|
||||
let at = file.len() - sample.len() - 8; // the co64 offset field ends the moov
|
||||
let at = file.len() - 8; // the co64 offset field ends the moov
|
||||
let offset = file.len() + 8; // samples belong to the mdat payload, after its header
|
||||
file[at..at + 8].copy_from_slice(&u64::try_from(offset).unwrap().to_be_bytes());
|
||||
file.extend(box_bytes(b"mdat", sample));
|
||||
file
|
||||
}
|
||||
|
||||
|
||||
@@ -399,7 +399,10 @@ fn track_box(entry: &[u8], offset: u64, size: u32) -> Vec<u8> {
|
||||
let mut co64 = vec![0u8; 4];
|
||||
co64.extend(1u32.to_be_bytes());
|
||||
co64.extend(offset.to_be_bytes());
|
||||
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
|
||||
// One sample in chunk 1, using sample description 1. The shared parser
|
||||
// deliberately refuses sample access without this mapping.
|
||||
let stsc: Vec<u8> = [0u32, 1, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect();
|
||||
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsc", &stsc), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
|
||||
box_bytes(b"trak", &box_bytes(b"mdia", &box_bytes(b"minf", &box_bytes(b"stbl", &tables))))
|
||||
}
|
||||
|
||||
@@ -410,11 +413,10 @@ fn cr3_file(entry: &[u8], sample: &[u8]) -> Vec<u8> {
|
||||
// The track length does not depend on the offset value (always a 64-bit field), so it can be
|
||||
// measured with a placeholder before the real offset is known.
|
||||
let track_len = track_box(entry, 0, sample.len() as u32).len();
|
||||
let offset = (ftyp.len() + 8 + track_len + 7) & !7;
|
||||
let offset = ftyp.len() + 8 + track_len + 8; // moov and mdat headers
|
||||
let mut file = ftyp;
|
||||
file.extend(box_bytes(b"moov", &track_box(entry, offset as u64, sample.len() as u32)));
|
||||
file.resize(offset, 0);
|
||||
file.extend_from_slice(sample);
|
||||
file.extend(box_bytes(b"mdat", sample));
|
||||
file
|
||||
}
|
||||
|
||||
|
||||
@@ -1045,7 +1045,8 @@ fn malformed_semantic_masks_are_skipped() {
|
||||
assert_eq!(decode(&bytes).unwrap().cpp, 3);
|
||||
let masks = lightcraft_raw::semantic_masks(&bytes);
|
||||
let names: Vec<&str> = masks.iter().map(|m| m.name.as_str()).collect();
|
||||
assert_eq!(names, ["short strip", "sub area too small"]);
|
||||
// Truncated strip pixels are rejected by the shared reader instead of zero-filled.
|
||||
assert_eq!(names, ["sub area too small"]);
|
||||
assert!(masks.iter().all(|m| m.sub_area.is_none()), "sub areas the mask doesn't fit are ignored");
|
||||
for m in &masks {
|
||||
let d = m.developed(Rect::new(0, 0, 8, 6), Rect::new(0, 0, 8, 6)).unwrap();
|
||||
|
||||
@@ -202,7 +202,10 @@ mod cr3 {
|
||||
let mut co64 = vec![0u8; 4];
|
||||
co64.extend(1u32.to_be_bytes());
|
||||
co64.extend(offset.to_be_bytes());
|
||||
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
|
||||
// One sample in chunk 1, using sample description 1. The shared parser
|
||||
// deliberately refuses sample access without this mapping.
|
||||
let stsc: Vec<u8> = [0u32, 1, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect();
|
||||
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsc", &stsc), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
|
||||
box_bytes(b"trak", &box_bytes(b"mdia", &box_bytes(b"minf", &box_bytes(b"stbl", &tables))))
|
||||
}
|
||||
|
||||
@@ -231,11 +234,10 @@ mod cr3 {
|
||||
let entry = box_bytes(b"CRAW", &craw);
|
||||
let ftyp = box_bytes(b"ftyp", b"crx \0\0\0\x01crx isom");
|
||||
let track_len = track(&entry, 0, sample.len() as u32).len();
|
||||
let offset = (ftyp.len() + 8 + track_len + 7) & !7;
|
||||
let offset = ftyp.len() + 8 + track_len + 8; // moov and mdat headers
|
||||
let mut out = ftyp;
|
||||
out.extend(box_bytes(b"moov", &track(&entry, offset as u64, sample.len() as u32)));
|
||||
out.resize(offset, 0);
|
||||
out.extend_from_slice(sample);
|
||||
out.extend(box_bytes(b"mdat", sample));
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user