mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
craft-tiff: direct YCbCr RGBA decoding with explicit colour policy (#1967)
This commit is contained in:
1 parent
0fb022f4f1
commit
bd0ed73e02
2 files changed
+146
-3
No files matched your search
@@ -199,6 +199,103 @@ pub fn expand_units(units: &[u8], width: u32, height: u32, (h, v): (u32, u32)) -
|
||||
/// uncompressed or PackBits; anything else is [`TiffError::Invalid`]. More than `max_pixels`
|
||||
/// pixels is [`TiffError::Limit`], and a file too small to hold the image is refused before the data units are allocated.
|
||||
pub fn decode_rgb8(data: &[u8], ifd: &Ifd, max_pixels: u64) -> crate::Result<Vec<u8>> {
|
||||
let DecodedUnits { width, height, sub, units } = decode_units(data, ifd, max_pixels)?;
|
||||
let mut rgb = expand_units(&units, width, height, sub).ok_or_else(|| TiffError::Invalid("YCbCr image: data units".into()))?;
|
||||
YCbCr::from_ifd(ifd, 255).convert_u8(&mut rgb, 3);
|
||||
Ok(rgb)
|
||||
}
|
||||
|
||||
/// Colour interpretation for direct RGBA decoding.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ColorPolicy {
|
||||
/// Existing shared/libtiff conversion; unusable colour tags use defaults.
|
||||
Libtiff,
|
||||
/// Literal TIFF 6.0 green equation, with malformed colour tags rejected.
|
||||
/// Retains the historical FilmCraft interpretation, including non-unit-sum weights.
|
||||
TiffEquation,
|
||||
}
|
||||
|
||||
/// Decode directly into opaque RGBA without an intermediate RGB image.
|
||||
/// Uses the same strip/subsampling/PackBits validation as [`decode_rgb8`].
|
||||
/// Output allocation is checked and fallible. Orientation is left to the caller.
|
||||
pub fn decode_rgba8(data: &[u8], ifd: &Ifd, max_pixels: u64, policy: ColorPolicy) -> crate::Result<Vec<u8>> {
|
||||
let color = match policy {
|
||||
ColorPolicy::Libtiff => YCbCr::from_ifd(ifd, 255),
|
||||
ColorPolicy::TiffEquation => strict_color(ifd)?,
|
||||
};
|
||||
let DecodedUnits { width, height, sub: (sh, sv), units } = decode_units(data, ifd, max_pixels)?;
|
||||
let len = u64::from(width)
|
||||
.checked_mul(u64::from(height))
|
||||
.and_then(|n| n.checked_mul(4))
|
||||
.filter(|&n| n <= isize::MAX as u64)
|
||||
.and_then(|n| usize::try_from(n).ok())
|
||||
.ok_or(TiffError::Limit("pixel count"))?;
|
||||
let mut rgba = Vec::new();
|
||||
rgba.try_reserve_exact(len).map_err(|_| TiffError::Limit("memory"))?;
|
||||
rgba.resize(len, 255);
|
||||
let (w, h, sh, sv) = (width as usize, height as usize, sh as usize, sv as usize);
|
||||
let units_x = w.div_ceil(sh);
|
||||
let tables = (policy == ColorPolicy::Libtiff).then(|| color.tables(255));
|
||||
for (i, unit) in units.chunks_exact(sh * sv + 2).enumerate() {
|
||||
let (ux, uy) = (i % units_x, i / units_x);
|
||||
let (luma, chroma) = unit.split_at(sh * sv);
|
||||
let [cb, cr] = <[u8; 2]>::try_from(chroma).map_err(|_| TiffError::Invalid("YCbCr data unit".into()))?;
|
||||
for (k, &y) in luma.iter().enumerate() {
|
||||
let (x, row) = (ux * sh + k % sh, uy * sv + k / sh);
|
||||
if x < w && row < h {
|
||||
let rgb = match &tables {
|
||||
Some(t) => t.rgb(usize::from(y), usize::from(cb), usize::from(cr)).map(|v| v as u8),
|
||||
None => tiff_rgb(&color, y, cb, cr),
|
||||
};
|
||||
let at = (row * w + x) * 4;
|
||||
if let Some(px) = rgba.get_mut(at..at + 3) {
|
||||
px.copy_from_slice(&rgb);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(rgba)
|
||||
}
|
||||
|
||||
fn strict_color(ifd: &Ifd) -> crate::Result<YCbCr> {
|
||||
let defaults = YCbCr::defaults(255);
|
||||
let luma = match ifd.f64s(YCBCR_COEFFICIENTS) {
|
||||
None => defaults.luma,
|
||||
Some(v) => v.get(..3).and_then(|v| v.try_into().ok()).ok_or_else(|| TiffError::Invalid("invalid YCbCrCoefficients".into()))?,
|
||||
};
|
||||
let reference_black_white = match ifd.f64s(REFERENCE_BLACK_WHITE) {
|
||||
None => defaults.reference_black_white,
|
||||
Some(v) => v.get(..6).and_then(|v| v.try_into().ok()).ok_or_else(|| TiffError::Invalid("invalid ReferenceBlackWhite".into()))?,
|
||||
};
|
||||
if luma.iter().any(|v| !v.is_finite()) || luma[1].abs() < 1e-6 {
|
||||
return Err(TiffError::Invalid("invalid YCbCrCoefficients".into()));
|
||||
}
|
||||
if reference_black_white.iter().any(|v| !v.is_finite()) || reference_black_white.as_chunks::<2>().0.iter().any(|[b, w]| (w - b).abs() < 1e-6) {
|
||||
return Err(TiffError::Invalid("invalid ReferenceBlackWhite".into()));
|
||||
}
|
||||
Ok(YCbCr { luma, reference_black_white })
|
||||
}
|
||||
|
||||
fn tiff_rgb(color: &YCbCr, y: u8, cb: u8, cr: u8) -> [u8; 3] {
|
||||
let [lr, lg, lb] = color.luma;
|
||||
let [yb, yw, cbb, cbw, crb, crw] = color.reference_black_white;
|
||||
let y = (f64::from(y) - yb) * 255.0 / (yw - yb);
|
||||
let cb = (f64::from(cb) - cbb) * 127.0 / (cbw - cbb);
|
||||
let cr = (f64::from(cr) - crb) * 127.0 / (crw - crb);
|
||||
let r = cr * (2.0 - 2.0 * lr) + y;
|
||||
let b = cb * (2.0 - 2.0 * lb) + y;
|
||||
let g = (y - lb * b - lr * r) / lg;
|
||||
[r, g, b].map(|v| v.round().clamp(0.0, 255.0) as u8)
|
||||
}
|
||||
|
||||
struct DecodedUnits {
|
||||
width: u32,
|
||||
height: u32,
|
||||
sub: (u32, u32),
|
||||
units: Vec<u8>,
|
||||
}
|
||||
|
||||
fn decode_units(data: &[u8], ifd: &Ifd, max_pixels: u64) -> crate::Result<DecodedUnits> {
|
||||
// TiffError has no "unsupported" variant; adding one would break exhaustive matches downstream.
|
||||
let unsupported = |what: &str| TiffError::Invalid(format!("unsupported YCbCr image with {what}"));
|
||||
let invalid = |what: &str| TiffError::Invalid(format!("YCbCr image: {what}"));
|
||||
@@ -265,7 +362,5 @@ pub fn decode_rgb8(data: &[u8], ifd: &Ifd, max_pixels: u64) -> crate::Result<Vec
|
||||
if units.len() < need {
|
||||
return Err(TiffError::Truncated(data.len() as u64));
|
||||
}
|
||||
let mut rgb = expand_units(&units, width, height, sub).ok_or_else(|| invalid("data units"))?;
|
||||
YCbCr::from_ifd(ifd, 255).convert_u8(&mut rgb, 3);
|
||||
Ok(rgb)
|
||||
Ok(DecodedUnits { width, height, sub, units })
|
||||
}
|
||||
@@ -207,3 +207,51 @@ fn limits_and_hostile_files() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rgba_decodes_all_subsampling_without_rgb_intermediate_semantics() {
|
||||
for sh in [1u16, 2, 4] {
|
||||
for sv in [1u16, 2, 4] {
|
||||
let (w, h) = (5u32, 3u32);
|
||||
let count = w.div_ceil(u32::from(sh)) * h.div_ceil(u32::from(sv));
|
||||
let mut units = Vec::new();
|
||||
for i in 0..count {
|
||||
units.extend(std::iter::repeat_n(20 + i as u8, usize::from(sh) * usize::from(sv)));
|
||||
units.extend([128, 128]);
|
||||
}
|
||||
for packed in [false, true] {
|
||||
let bytes = file(w, h, (sh, sv), &units, packed, &[]);
|
||||
let t = Tiff::parse(&bytes).unwrap();
|
||||
let rgb = ycbcr::decode_rgb8(&bytes, &t.ifds[0], 15).unwrap();
|
||||
let expected: Vec<u8> = rgb.as_chunks::<3>().0.iter().flat_map(|px| [px[0], px[1], px[2], 255]).collect();
|
||||
for policy in [ycbcr::ColorPolicy::Libtiff, ycbcr::ColorPolicy::TiffEquation] {
|
||||
assert_eq!(ycbcr::decode_rgba8(&bytes, &t.ifds[0], 15, policy).unwrap(), expected);
|
||||
assert_eq!(ycbcr::decode_rgba8(&bytes, &t.ifds[0], 14, policy), Err(TiffError::Limit("pixel count")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_tiff_policy_keeps_literal_green_equation_and_rejects_bad_tags() {
|
||||
use ycbcr::ColorPolicy::{Libtiff, TiffEquation};
|
||||
let run = |extra: &[(u16, Value)], policy| {
|
||||
let bytes = file(1, 1, (1, 1), &[128, 128, 128], false, extra);
|
||||
let t = Tiff::parse(&bytes).unwrap();
|
||||
ycbcr::decode_rgba8(&bytes, &t.ifds[0], 1, policy)
|
||||
};
|
||||
let weights = [(YCBCR_COEFFICIENTS, Value::Rational(vec![(4, 10); 3]))];
|
||||
assert_eq!(run(&weights, Libtiff).unwrap(), [128, 128, 128, 255]);
|
||||
assert_eq!(run(&weights, TiffEquation).unwrap(), [128, 64, 128, 255]);
|
||||
for bad in [
|
||||
(YCBCR_COEFFICIENTS, Value::Rational(vec![(1, 2)])),
|
||||
(YCBCR_COEFFICIENTS, Value::Double(vec![0.3, 0.0, 0.7])),
|
||||
(YCBCR_COEFFICIENTS, Value::Double(vec![f64::NAN, 0.5, 0.5])),
|
||||
(REFERENCE_BLACK_WHITE, Value::Rational(vec![(0, 1); 6])),
|
||||
(REFERENCE_BLACK_WHITE, Value::Double(vec![0.0, 255.0])),
|
||||
] {
|
||||
assert!(matches!(run(std::slice::from_ref(&bad), TiffEquation), Err(TiffError::Invalid(_))));
|
||||
assert!(run(&[bad], Libtiff).is_ok());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user