Merge remote-tracking branch 'origin/codex/legacy-exif-orientation' into w-1613

# Conflicts:
#	crates/image-codecs/src/lib.rs
This commit is contained in:
FredApps committed 2026-11-16 00:53:34 +01:00
commit d0bba656d1
215 files changed
+25673 -1970

No files matched your search

+80
View File
@@ -247,3 +247,83 @@ fn read_le24(bytes: &[u8], at: usize) -> Option<u32> {
let b = bytes.get(at..at.checked_add(3)?)?;
Some(u32::from_le_bytes([b[0], b[1], b[2], 0]))
}
/// Probe the stored dimensions of the first PNG, JPEG, BMP or TIFF image, without decoding
/// pixels or applying orientation. TIFF uses the first directory even if it is a thumbnail.
/// The caller's limits bound both the header decoder and the declared output dimensions.
pub fn probe_stored_size(bytes: &[u8], limits: &crate::Limits) -> Result<(u32, u32), CodecError> {
use std::io::Cursor;
let format = crate::detect(bytes).ok_or(CodecError::UnknownFormat)?;
if !matches!(format, Format::Png | Format::Jpeg | Format::Bmp | Format::Tiff) {
return Err(CodecError::unsupported(format, "stored-size probe is not available for this format"));
}
// The JPEG header reader owns a copy of its input before applying image limits.
if format == Format::Jpeg && u64::try_from(bytes.len()).map_or(true, |len| len > limits.max_alloc) {
return Err(CodecError::malformed(format, "JPEG input exceeds the allocation budget"));
}
let image_format = match format {
Format::Png => image::ImageFormat::Png,
Format::Jpeg => image::ImageFormat::Jpeg,
Format::Bmp => image::ImageFormat::Bmp,
Format::Tiff => image::ImageFormat::Tiff,
_ => return Err(CodecError::unsupported(format, "stored-size probe is not available for this format")),
};
// Preserve detect's BigTIFF selection; image's signature guesser only knows classic TIFF.
let mut reader = image::ImageReader::with_format(Cursor::new(bytes), image_format);
let mut image_limits = image::Limits::default();
image_limits.max_image_width = Some(limits.max_width);
image_limits.max_image_height = Some(limits.max_height);
image_limits.max_alloc = Some(limits.max_alloc);
reader.limits(image_limits);
let (w, h) = reader.into_dimensions().map_err(|e| CodecError::malformed(format, e))?;
limits.check_size(w, h)?;
Ok((w, h))
}
#[cfg(test)]
mod stored_size_tests {
#![allow(clippy::unwrap_used)]
use super::*;
use std::io::Cursor;
#[test]
fn stored_probe_uses_caller_limits_without_decoding() {
let mut bmp = Cursor::new(Vec::new());
image::write_buffer_with_format(&mut bmp, &vec![0; 60_000 * 3], 1, 60_000, image::ExtendedColorType::Rgb8, image::ImageFormat::Bmp).unwrap();
let bytes = bmp.into_inner();
let limits = crate::Limits::new(u32::MAX, u32::MAX, 128_000_000, 2_048_000_000);
assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (1, 60_000));
assert!(probe_stored_size(&bytes, &crate::Limits::new(u32::MAX, 50_000, u64::MAX, 2_048_000_000)).is_err());
assert!(probe_stored_size(&bytes[..20], &limits).is_err());
}
#[test]
fn bigtiff_stored_dimensions_preserve_detected_format() {
let mut out = Cursor::new(Vec::new());
tiff::encoder::TiffEncoder::new_big(&mut out).unwrap().write_image::<tiff::encoder::colortype::RGB8>(3, 2, &[10; 18]).unwrap();
let bytes = out.into_inner();
assert_eq!(crate::detect(&bytes), Some(Format::Tiff));
assert_eq!(probe_stored_size(&bytes, &crate::Limits::default()).unwrap(), (3, 2));
}
#[test]
fn jpeg_input_copy_is_bounded_by_allocation_budget() {
let mut out = Cursor::new(Vec::new());
image::write_buffer_with_format(&mut out, &[10; 18], 3, 2, image::ExtendedColorType::Rgb8, image::ImageFormat::Jpeg).unwrap();
let bytes = out.into_inner();
let mut limits = crate::Limits { max_alloc: bytes.len() as u64, ..Default::default() };
assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (3, 2));
limits.max_alloc -= 1;
assert!(probe_stored_size(&bytes, &limits).is_err());
}
#[test]
fn common_stored_headers_are_probed() {
let limits = crate::Limits::new(100, 100, 10_000, 1 << 20);
for format in [image::ImageFormat::Png, image::ImageFormat::Jpeg, image::ImageFormat::Bmp, image::ImageFormat::Tiff] {
let mut out = Cursor::new(Vec::new());
image::write_buffer_with_format(&mut out, &[10; 18], 3, 2, image::ExtendedColorType::Rgb8, format).unwrap();
assert_eq!(probe_stored_size(&out.into_inner(), &limits).unwrap(), (3, 2), "{format:?}");
}
}
}
+145
View File
@@ -0,0 +1,145 @@
//! Header-only BMP dimensions and pixel-data coverage, without allocating a canvas.
/// Coverage of the declared pixel array. Row byte counts include row padding; RLE coverage
/// counts the pixels drawn by commands and may be smaller than the canvas.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Coverage {
pub pixels: u64,
pub row_bytes: Option<(u64, u64)>,
}
/// Inspect pixel data for the given dimensions without decoding a bitmap. The row tuple is
/// `(available, needed)`; callers choose their own sparse-canvas and allocation policy.
pub fn coverage(bytes: &[u8], w: u32, h: u32) -> Coverage {
match bmp_layout(bytes) {
BmpLayout::Rle { offset, rle4 } => Coverage { pixels: bmp_rle_pixels(bytes, offset, rle4, w, h), row_bytes: None },
BmpLayout::Rows { offset, bits } => {
let needed = u64::from(w).saturating_mul(u64::from(bits)).div_ceil(32).saturating_mul(4).saturating_mul(u64::from(h));
let held = bytes.len().saturating_sub(offset) as u64;
Coverage { pixels: u64::from(w) * u64::from(h), row_bytes: Some((held, needed)) }
}
}
}
enum BmpLayout {
/// RLE8 or RLE4 commands.
Rle { offset: usize, rle4: bool },
/// Rows of `bits` per pixel, each padded to 4 bytes.
Rows { offset: usize, bits: u16 },
}
/// The layout the decoder reads, from the file header and the bitmap header: a 12-byte core header
/// (bits per pixel at 24, never compressed) or a 40-byte or larger one (bits at 28, compression at
/// 30). Fields the decoder would have refused are read as 1 bit per pixel at offset 0, the most
/// lenient layout.
fn bmp_layout(bytes: &[u8]) -> BmpLayout {
let u16_at = |i: usize| bytes.get(i..i.checked_add(2)?).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes);
let u32_at = |i: usize| bytes.get(i..i.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes);
let offset = u32_at(10).and_then(|o| usize::try_from(o).ok()).unwrap_or(0);
let (bits, compression) = match u32_at(14) {
Some(12) => (u16_at(24), Some(0)),
_ => (u16_at(28), u32_at(30)),
};
match compression {
Some(1) => BmpLayout::Rle { offset, rle4: false },
Some(2) => BmpLayout::Rle { offset, rle4: true },
_ => BmpLayout::Rows { offset, bits: bits.filter(|&b| b > 0).unwrap_or(1) },
}
}
/// The pixels an RLE8 or RLE4 BMP's commands draw on a `w` × `h` canvas, read the way the decoder
/// paints them: a run is clipped at the end of its row, an end-of-row code moves to the next row, a
/// delta skips pixels (left blank), and an end-of-bitmap code, a truncated command or the last row
/// ends the picture. Every command takes at least 2 bytes, so the walk is linear in the file.
fn bmp_rle_pixels(bytes: &[u8], offset: usize, rle4: bool, w: u32, h: u32) -> u64 {
let (w, h) = (u64::from(w), u64::from(h));
let (mut pos, mut x, mut y, mut covered) = (offset, 0u64, 0u64, 0u64);
while y < h {
let (Some(&count), Some(&value)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else {
break;
};
pos = pos.saturating_add(2);
match (count, value) {
// end of row
(0, 0) => (x, y) = (0, y.saturating_add(1)),
// end of bitmap
(0, 1) => break,
// delta: right and down
(0, 2) => {
let (Some(&dx), Some(&dy)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else {
break;
};
pos = pos.saturating_add(2);
(x, y) = (x.saturating_add(u64::from(dx)), y.saturating_add(u64::from(dy)));
}
// absolute: `value` pixels stored one per byte (RLE8) or nibble (RLE4), padded to 2 bytes
(0, n) => {
let len = if rle4 { usize::from(n).div_ceil(2) } else { usize::from(n) };
pos = pos.saturating_add(len + (len & 1));
if pos > bytes.len() {
break;
}
covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x)));
x = x.saturating_add(u64::from(n));
}
// a run of `count` pixels
(n, _) => {
covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x)));
x = x.saturating_add(u64::from(n));
}
}
}
covered
}
/// Reads BMP `(width, height)` from the DIB header without allocating.
pub fn dimensions(bytes: &[u8]) -> Option<(u32, u32)> {
if bytes.get(0..2) != Some(b"BM") {
return None;
}
let dib = u32::from_le_bytes(bytes.get(14..18)?.try_into().ok()?);
let (w, h) = if dib == 12 {
(u16::from_le_bytes(bytes.get(18..20)?.try_into().ok()?) as u32, u16::from_le_bytes(bytes.get(20..22)?.try_into().ok()?) as u32)
} else {
let w = i32::from_le_bytes(bytes.get(18..22)?.try_into().ok()?);
let h = i32::from_le_bytes(bytes.get(22..26)?.try_into().ok()?);
(u32::try_from(w).ok()?, h.unsigned_abs())
};
Some((w, h))
}
#[cfg(test)]
mod tests {
use super::*;
fn bmp(compression: u32, pixels: &[u8]) -> Vec<u8> {
let mut b = vec![0; 54];
b[..2].copy_from_slice(b"BM");
b[10..14].copy_from_slice(&54u32.to_le_bytes());
b[14..18].copy_from_slice(&40u32.to_le_bytes());
b[18..22].copy_from_slice(&4i32.to_le_bytes());
b[22..26].copy_from_slice(&2i32.to_le_bytes());
b[28..30].copy_from_slice(&8u16.to_le_bytes());
b[30..34].copy_from_slice(&compression.to_le_bytes());
b.extend_from_slice(pixels);
b
}
#[test]
fn dimensions_and_padded_rows() {
let b = bmp(0, &[0; 7]);
assert_eq!(dimensions(&b), Some((4, 2)));
assert_eq!(coverage(&b, 4, 2), Coverage { pixels: 8, row_bytes: Some((7, 8)) });
assert_eq!(dimensions(&b[..20]), None);
}
#[test]
fn rle_counts_clipped_runs_deltas_and_complete_absolute_blocks() {
let b = bmp(1, &[3, 7, 0, 0, 0, 2, 1, 0, 0, 3, 1, 2, 3, 0, 0, 1]);
assert_eq!(coverage(&b, 4, 2).pixels, 6);
let truncated = bmp(1, &[0, 4, 1, 2]);
assert_eq!(coverage(&truncated, 4, 2).pixels, 0);
let nibble = bmp(2, &[0, 3, 0x12, 0x30, 0, 1]);
assert_eq!(coverage(&nibble, 4, 2).pixels, 3);
}
}
+1 -1
View File
@@ -99,7 +99,7 @@ pub fn linear_to_pq(linear: f32) -> f32 {
/// Relative luminance weights of the RGB primaries `code` (BT.2020's for anything else, the
/// space BT.2100 defines HLG in).
fn luma_weights(code: u16) -> [f32; 3] {
pub(crate) fn luma_weights(code: u16) -> [f32; 3] {
match code {
1 => [0.2126, 0.7152, 0.0722],
12 => [0.2290, 0.6917, 0.0793],
+3
View File
@@ -1,4 +1,6 @@
pub(crate) mod avif;
#[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))]
pub(crate) mod avif_decode;
pub(crate) mod dds;
pub(crate) mod dds_bc;
pub(crate) mod deep_exr;
@@ -13,6 +15,7 @@ pub(crate) mod heif;
pub(crate) mod iff;
pub(crate) mod jp2;
pub(crate) mod jpeg;
pub(crate) mod jpegls;
pub(crate) mod jxl;
pub(crate) mod pcx;
pub(crate) mod png;
File diff suppressed because it is too large. Load diff
+41 -2
View File
@@ -195,6 +195,27 @@ enum DataEnd {
/// measured on these, not on the file length, which APP padding can inflate without
/// adding image data.
fn data_end(b: &[u8]) -> (DataEnd, u64) {
data_end_mode(b, false)
}
/// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused.
pub(crate) fn reaches_eoi(b: &[u8]) -> bool {
data_end_mode(b, true).0 == DataEnd::Complete
}
fn find_ff(b: &[u8]) -> Option<usize> {
const ONES: u64 = 0x0101_0101_0101_0101;
let (words, tail) = b.as_chunks::<8>();
for (i, word) in words.iter().enumerate() {
let x = !u64::from_ne_bytes(*word);
if x.wrapping_sub(ONES) & !x & (ONES << 7) != 0 {
return word.iter().position(|&v| v == 0xff).map(|j| i * 8 + j);
}
}
tail.iter().position(|&v| v == 0xff).map(|j| words.len() * 8 + j)
}
fn data_end_mode(b: &[u8], strict: bool) -> (DataEnd, u64) {
// Where the bytes run out: inside the image data once a scan has held some.
let cut = |scan_data: bool, scanned: u64| (if scan_data { DataEnd::Truncated } else { DataEnd::Empty }, scanned);
let mut i = 2; // past SOI
@@ -207,7 +228,7 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) {
// segments after the stray bytes (padding among them) are still recognised. Stray
// bytes are not scan data, so they never count (junk would buy budget as padding
// did); with no marker left, the image data ends here.
let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { return cut(scan_data, scanned) };
let Some(p) = b.get(i..).and_then(find_ff) else { return cut(scan_data, scanned) };
i += p;
continue;
}
@@ -216,15 +237,19 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) {
0xD9 => return (DataEnd::Complete, scanned),
0xFF => i += 1,
0x01 | 0xD0..=0xD8 => i += 2,
0x00 if strict => i += 2,
_ => {
let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else {
return cut(scan_data, scanned);
};
if strict && len < 2 {
return cut(scan_data, scanned);
}
i = i.saturating_add(2 + usize::from(len));
if code == 0xDA {
let start = i;
loop {
let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else {
let Some(p) = b.get(i..).and_then(find_ff) else {
// EOF inside this scan: everything from the scan start to the end
// of the file is scan data (inside a declared segment it may not
// be, so only count what remains).
@@ -653,6 +678,20 @@ mod tests {
}
/// `info` reads the frame from the markers, and which decoder the file goes to.
#[test]
fn ff_search_matches_plain_search() {
let mut b: Vec<u8> = (0..300u32).map(|i| (i * 7 % 255) as u8).collect();
for at in [None, Some(0), Some(7), Some(8), Some(15), Some(203), Some(296), Some(299)] {
if let Some(i) = at {
b[i] = 0xff;
}
for start in [0, 1, 5, 9, 290, 299, 300] {
let s = &b[start..];
assert_eq!(find_ff(s), s.iter().position(|&v| v == 0xff));
}
}
}
#[test]
fn info_reads_the_frame_and_the_decoder() {
let img = Image::from_raw(37, 21, ChannelLayout::Rgb, SampleType::U8, vec![128; 37 * 21 * 3]).unwrap();
+891
View File
@@ -0,0 +1,891 @@
//! JPEG-LS (ITU-T T.87 | ISO/IEC 14495-1, "LOCO-I"): lossless and near-lossless gray and RGB
//! images, 2 to 16 bits per sample.
//!
//! Read: baseline JPEG-LS with the default or `LSE`-preset coding parameters, any `NEAR`, and
//! scans that code one component each (`ILV` 0), which is also how this module writes. Line- and
//! sample-interleaved scans, colour transforms and mapping tables are reported as unsupported.
//! Write: lossless (`NEAR` 0), 8 or 16 bits, gray or RGB, one scan per component.
//!
//! Samples of 9 to 15 bits are scaled to 16 bits on read (the top bits are repeated into the low
//! ones), so a decoded image always spans the full range of its sample type.
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::format::Format;
use crate::image::{ChannelLayout, Image, SampleType};
use crate::options::Limits;
const F: Format = Format::JpegLs;
const SOF55: u8 = 0xF7;
const LSE: u8 = 0xF8;
const RESET_DEFAULT: i32 = 64;
const MIN_C: i32 = -128;
const MAX_C: i32 = 127;
/// `J[RUNindex]`: the log2 of the run length a run-index step codes.
const J: [u32; 32] = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15];
/// Context slots: the regular contexts (365 of the 405 slots the index below can reach), then the two
/// run-interruption contexts.
const RI_BASE: usize = 405;
const CONTEXTS: usize = RI_BASE + 2;
fn bad(reason: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, reason)
}
/// Whether `b` is a JPEG-LS file: an SOI followed by marker segments up to an SOF55 frame header.
pub(crate) fn looks_like_jpegls(b: &[u8]) -> bool {
if !b.starts_with(&[0xFF, 0xD8]) {
return false;
}
let mut pos = 2;
for _ in 0..64 {
let (Some(&0xFF), Some(&marker)) = (b.get(pos), b.get(pos + 1)) else { return false };
match marker {
SOF55 => return true,
0xDA | 0xD9 => return false,
0xC0..=0xCF | 0xDB..=0xDF => return false,
0x01 | 0xD0..=0xD7 | 0xFF => pos += if marker == 0xFF { 1 } else { 2 },
_ => {
let Some(len) = b.get(pos + 2..pos + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { return false };
pos += 2 + len;
}
}
}
false
}
/// The coding parameters of one scan, derived as T.87 section C.2 specifies.
#[derive(Clone, Copy)]
struct Params {
maxval: i32,
near: i32,
range: i32,
qbpp: u32,
limit: u32,
t1: i32,
t2: i32,
t3: i32,
reset: i32,
}
fn ceil_log2(v: i32) -> u32 {
let mut n = 0;
while (1i64 << n) < i64::from(v) {
n += 1;
}
n
}
impl Params {
/// `preset` holds the `LSE` values (`maxval`, `t1`, `t2`, `t3`, `reset`), 0 where absent.
fn new(precision: u32, near: i32, preset: [i32; 5]) -> Result<Params, CodecError> {
let default_max = (1i32 << precision) - 1;
let maxval = if preset[0] == 0 { default_max } else { preset[0] };
if maxval < 1 || maxval > default_max || near < 0 || near > (maxval / 2).min(255) {
return Err(bad("invalid MAXVAL or NEAR"));
}
let clamp = |v: i32, lo: i32, hi: i32| if v > hi || v < lo { lo } else { v };
let (d1, d2, d3) = if maxval >= 128 {
let factor = (maxval.min(4095) + 128) / 256;
let t1 = clamp(factor + 2 + 3 * near, near + 1, maxval);
let t2 = clamp(factor * 4 + 3 + 5 * near, t1, maxval);
let t3 = clamp(factor * 17 + 4 + 7 * near, t2, maxval);
(t1, t2, t3)
} else {
let factor = 256 / (maxval + 1);
let t1 = clamp((3 / factor + 3 * near).max(2), near + 1, maxval);
let t2 = clamp((7 / factor + 5 * near).max(3), t1, maxval);
let t3 = clamp((21 / factor + 7 * near).max(4), t2, maxval);
(t1, t2, t3)
};
let t1 = if preset[1] == 0 { d1 } else { preset[1] };
let t2 = if preset[2] == 0 { d2 } else { preset[2] };
let t3 = if preset[3] == 0 { d3 } else { preset[3] };
let reset = if preset[4] == 0 { RESET_DEFAULT } else { preset[4] };
if !(near < t1 && t1 <= t2 && t2 <= t3 && t3 <= maxval) || !(3..=255).contains(&reset) {
return Err(bad("invalid threshold or RESET preset"));
}
let range = (maxval + 2 * near) / (2 * near + 1) + 1;
let bpp = ceil_log2(maxval + 1).max(2);
Ok(Params { maxval, near, range, qbpp: ceil_log2(range), limit: 2 * (bpp + bpp.max(8)), t1, t2, t3, reset })
}
}
/// Adaptive state of one scan: contexts, bias and the run index.
struct State {
a: [i32; CONTEXTS],
b: [i32; CONTEXTS],
c: [i32; CONTEXTS],
n: [i32; CONTEXTS],
nn: [i32; 2],
run_index: usize,
}
impl State {
fn new(p: &Params) -> State {
let a0 = ((p.range + 32) / 64).max(2);
State { a: [a0; CONTEXTS], b: [0; CONTEXTS], c: [0; CONTEXTS], n: [1; CONTEXTS], nn: [0; 2], run_index: 0 }
}
}
fn quantise_gradient(d: i32, p: &Params) -> i32 {
if d <= -p.t3 {
-4
} else if d <= -p.t2 {
-3
} else if d <= -p.t1 {
-2
} else if d < -p.near {
-1
} else if d <= p.near {
0
} else if d < p.t1 {
1
} else if d < p.t2 {
2
} else if d < p.t3 {
3
} else {
4
}
}
fn predict(ra: i32, rb: i32, rc: i32) -> i32 {
if rc >= ra.max(rb) {
ra.min(rb)
} else if rc <= ra.min(rb) {
ra.max(rb)
} else {
ra + rb - rc
}
}
/// The error quantised for `NEAR` and reduced into the modulo range, as the encoder codes it.
fn reduce(mut e: i32, p: &Params) -> i32 {
if p.near > 0 {
e = if e > 0 { (e + p.near) / (2 * p.near + 1) } else { -(p.near - e) / (2 * p.near + 1) };
}
if e < 0 {
e += p.range;
}
if e >= (p.range + 1) / 2 {
e -= p.range;
}
e
}
/// The reconstructed sample for a decoded (sign-adjusted) error.
fn reconstruct(px: i32, sign: i32, e: i32, p: &Params) -> i32 {
let mut r = px + sign * e * (2 * p.near + 1);
if r < -p.near {
r += p.range * (2 * p.near + 1);
} else if r > p.maxval + p.near {
r -= p.range * (2 * p.near + 1);
}
r.clamp(0, p.maxval)
}
fn golomb_k(n: i32, a: i32) -> u32 {
let mut k = 0;
while (i64::from(n) << k) < i64::from(a) {
k += 1;
}
k
}
/// Bits of the entropy-coded segment, with T.87's stuffing: after an `0xFF` byte the next byte
/// carries only 7 bits.
struct BitReader<'a> {
data: &'a [u8],
pos: usize,
cur: u32,
left: u32,
prev_ff: bool,
}
impl<'a> BitReader<'a> {
fn new(data: &'a [u8]) -> Self {
BitReader { data, pos: 0, cur: 0, left: 0, prev_ff: false }
}
fn bit(&mut self) -> Result<u32, CodecError> {
if self.left == 0 {
let byte = *self.data.get(self.pos).ok_or_else(|| bad("the entropy-coded data ends early"))?;
self.pos += 1;
if self.prev_ff {
if byte & 0x80 != 0 {
return Err(bad("a marker inside entropy-coded data"));
}
self.cur = u32::from(byte);
self.left = 7;
} else {
self.cur = u32::from(byte);
self.left = 8;
}
self.prev_ff = byte == 0xFF;
}
self.left -= 1;
Ok((self.cur >> self.left) & 1)
}
fn bits(&mut self, n: u32) -> Result<u32, CodecError> {
let mut v = 0;
for _ in 0..n {
v = v << 1 | self.bit()?;
}
Ok(v)
}
/// A limited-length Golomb code: the mapped error.
fn golomb(&mut self, k: u32, limit: u32, qbpp: u32) -> Result<i32, CodecError> {
let max_zeros = limit.checked_sub(qbpp + 1).ok_or_else(|| bad("invalid LIMIT"))?;
let mut zeros = 0;
while self.bit()? == 0 {
zeros += 1;
if zeros > max_zeros {
return Err(bad("a Golomb code is longer than LIMIT"));
}
}
let v = if zeros < max_zeros { (zeros << k) + self.bits(k)? } else { self.bits(qbpp)? + 1 };
i32::try_from(v).map_err(|_| bad("mapped error out of range"))
}
}
/// Decode one component scan of `w` x `h` samples into `out` (row-major).
fn decode_scan(data: &[u8], w: usize, h: usize, p: &Params, out: &mut [u16]) -> Result<(), CodecError> {
let mut r = BitReader::new(data);
let mut st = State::new(p);
let mut prev = vec![0i32; w + 2];
let mut cur = vec![0i32; w + 2];
for y in 0..h {
// Ra at the first sample is Rb; the slot at index 0 is Rc there, left over from the line above.
cur[0] = prev[1];
if y > 0 {
prev[w + 1] = prev[w];
}
let mut x = 0usize;
while x < w {
let (ra, rb, rc, rd) = (cur[x], prev[x + 1], prev[x], if y == 0 { 0 } else { prev[x + 2] });
let (q1, q2, q3) = (quantise_gradient(rd - rb, p), quantise_gradient(rb - rc, p), quantise_gradient(rc - ra, p));
if q1 == 0 && q2 == 0 && q3 == 0 {
x = decode_run(&mut r, &mut st, p, &prev, &mut cur, ra, x, w)?;
continue;
}
let (sign, q1, q2, q3) = if q1 < 0 || (q1 == 0 && (q2 < 0 || (q2 == 0 && q3 < 0))) { (-1, -q1, -q2, -q3) } else { (1, q1, q2, q3) };
let q = (q1 * 81 + (q2 + 4) * 9 + (q3 + 4)) as usize;
let k = golomb_k(st.n[q], st.a[q]);
let m = r.golomb(k, p.limit, p.qbpp)?;
let swap = p.near == 0 && k == 0 && 2 * st.b[q] <= -st.n[q];
let e = if swap {
if m & 1 == 1 { (m - 1) >> 1 } else { -(m >> 1) - 1 }
} else if m & 1 == 0 {
m >> 1
} else {
-((m + 1) >> 1)
};
let px = (predict(ra, rb, rc) + sign * st.c[q]).clamp(0, p.maxval);
let rx = reconstruct(px, sign, e, p);
update_regular(&mut st, q, e, p);
cur[x + 1] = rx;
x += 1;
}
let row = &mut out[y * w..(y + 1) * w];
for (o, &v) in row.iter_mut().zip(&cur[1..=w]) {
*o = v as u16;
}
// The line just decoded becomes the line above; its slot 0 is the Rc of the next line's first sample.
std::mem::swap(&mut prev, &mut cur);
}
Ok(())
}
fn update_regular(st: &mut State, q: usize, e: i32, p: &Params) {
st.b[q] += e * (2 * p.near + 1);
st.a[q] += e.abs();
if st.n[q] == p.reset {
st.a[q] >>= 1;
st.b[q] = if st.b[q] >= 0 { st.b[q] >> 1 } else { -((1 - st.b[q]) >> 1) };
st.n[q] >>= 1;
}
st.n[q] += 1;
if st.b[q] <= -st.n[q] {
if st.c[q] > MIN_C {
st.c[q] -= 1;
}
st.b[q] += st.n[q];
if st.b[q] <= -st.n[q] {
st.b[q] = -st.n[q] + 1;
}
} else if st.b[q] > 0 {
if st.c[q] < MAX_C {
st.c[q] += 1;
}
st.b[q] -= st.n[q];
if st.b[q] > 0 {
st.b[q] = 0;
}
}
}
/// Run-interruption variables update shared by both directions.
fn update_run(st: &mut State, q: usize, e: i32, em: i32, ri_type: i32, p: &Params) {
let i = q - RI_BASE;
if e < 0 {
st.nn[i] += 1;
}
st.a[q] += (em + 1 - ri_type) >> 1;
if st.n[q] == p.reset {
st.a[q] >>= 1;
st.n[q] >>= 1;
st.nn[i] >>= 1;
}
st.n[q] += 1;
}
/// The run-interruption Golomb parameter and the mapping flag of an error.
fn run_k(st: &State, q: usize, ri_type: i32) -> u32 {
let temp = if ri_type == 1 { st.a[q] + (st.n[q] >> 1) } else { st.a[q] };
golomb_k(st.n[q], temp)
}
#[allow(clippy::too_many_arguments)]
fn decode_run(
r: &mut BitReader,
st: &mut State,
p: &Params,
prev: &[i32],
cur: &mut [i32],
ra: i32,
x: usize,
w: usize,
) -> Result<usize, CodecError> {
let remaining = w - x;
let mut run = 0usize;
while r.bit()? == 1 {
let step = 1usize << J[st.run_index];
let count = step.min(remaining - run);
run += count;
if count == step && st.run_index < 31 {
st.run_index += 1;
}
if run == remaining {
break;
}
}
if run < remaining {
let rest = r.bits(J[st.run_index])? as usize;
run += rest;
if run > remaining {
return Err(bad("a run passes the end of the line"));
}
}
for v in &mut cur[x + 1..x + 1 + run] {
*v = ra;
}
if run == remaining {
return Ok(w);
}
// The sample that ended the run: its own Rb, the sample above it.
let rb = prev[x + run + 1];
let ri_type = i32::from((ra - rb).abs() <= p.near);
let q = RI_BASE + ri_type as usize;
let k = run_k(st, q, ri_type);
let limit = p.limit - J[st.run_index] - 1;
let em = r.golomb(k, limit, p.qbpp)?;
let tmp = em + ri_type;
let cond = k == 0 && 2 * st.nn[ri_type as usize] < st.n[q];
let map = tmp & 1;
let mag = (tmp + map) / 2;
let e = if (cond && map == 1) || (!cond && map == 0) { mag } else { -mag };
let (px, sign) = if ri_type == 1 { (ra, 1) } else { (rb, if ra > rb { -1 } else { 1 }) };
// The decoder holds the sign-adjusted error; the sign flips the reconstruction.
let rx = reconstruct(px, sign, e, p);
update_run(st, q, e, em, ri_type, p);
if st.run_index > 0 {
st.run_index -= 1;
}
cur[x + run + 1] = rx;
Ok(x + run + 1)
}
// ---- encoder ----
struct BitWriter {
out: Vec<u8>,
cur: u32,
used: u32,
cap: u32,
}
impl BitWriter {
fn new() -> Self {
BitWriter { out: Vec::new(), cur: 0, used: 0, cap: 8 }
}
fn put(&mut self, value: u32, n: u32) {
for i in (0..n).rev() {
// A run of more than 32 zero bits (a long escape code) has no bits of `value` to shift out.
let bit = if i < 32 { (value >> i) & 1 } else { 0 };
self.cur = self.cur << 1 | bit;
self.used += 1;
if self.used == self.cap {
let byte = self.cur as u8;
self.out.push(byte);
self.cap = if byte == 0xFF { 7 } else { 8 };
self.cur = 0;
self.used = 0;
}
}
}
/// Pad the last byte with zero bits; a closing 0xFF is followed by a zero byte.
fn finish(mut self) -> Vec<u8> {
if self.used > 0 {
let pad = self.cap - self.used;
self.put(0, pad);
}
if self.out.last() == Some(&0xFF) {
self.out.push(0);
}
self.out
}
fn golomb(&mut self, m: i32, k: u32, limit: u32, qbpp: u32) {
let m = m as u32;
let max_zeros = limit - qbpp - 1;
let q = m >> k;
if q < max_zeros {
self.put(0, q);
self.put(1, 1);
self.put(m & ((1 << k) - 1), k);
} else {
self.put(0, max_zeros);
self.put(1, 1);
self.put(m - 1, qbpp);
}
}
}
fn encode_scan(plane: &[u16], w: usize, h: usize, p: &Params) -> Vec<u8> {
let mut wr = BitWriter::new();
let mut st = State::new(p);
let mut prev = vec![0i32; w + 2];
let mut cur = vec![0i32; w + 2];
for y in 0..h {
cur[0] = prev[1];
if y > 0 {
prev[w + 1] = prev[w];
}
let line = &plane[y * w..(y + 1) * w];
let mut x = 0usize;
while x < w {
let (ra, rb, rc, rd) = (cur[x], prev[x + 1], prev[x], if y == 0 { 0 } else { prev[x + 2] });
let ix = i32::from(line[x]);
let (q1, q2, q3) = (quantise_gradient(rd - rb, p), quantise_gradient(rb - rc, p), quantise_gradient(rc - ra, p));
if q1 == 0 && q2 == 0 && q3 == 0 {
x = encode_run(&mut wr, &mut st, p, line, &prev, &mut cur, ra, x);
continue;
}
let (sign, q1, q2, q3) = if q1 < 0 || (q1 == 0 && (q2 < 0 || (q2 == 0 && q3 < 0))) { (-1, -q1, -q2, -q3) } else { (1, q1, q2, q3) };
let q = (q1 * 81 + (q2 + 4) * 9 + (q3 + 4)) as usize;
let px = (predict(ra, rb, rc) + sign * st.c[q]).clamp(0, p.maxval);
let e = reduce(sign * (ix - px), p);
let k = golomb_k(st.n[q], st.a[q]);
let swap = p.near == 0 && k == 0 && 2 * st.b[q] <= -st.n[q];
let m = if swap {
if e >= 0 { 2 * e + 1 } else { -2 * (e + 1) }
} else if e >= 0 {
2 * e
} else {
-2 * e - 1
};
wr.golomb(m, k, p.limit, p.qbpp);
cur[x + 1] = reconstruct(px, sign, e, p);
update_regular(&mut st, q, e, p);
x += 1;
}
std::mem::swap(&mut prev, &mut cur);
}
wr.finish()
}
#[allow(clippy::too_many_arguments)]
fn encode_run(wr: &mut BitWriter, st: &mut State, p: &Params, line: &[u16], prev: &[i32], cur: &mut [i32], ra: i32, x: usize) -> usize {
let w = line.len();
let mut run = 0usize;
while x + run < w && (i32::from(line[x + run]) - ra).abs() <= p.near {
cur[x + run + 1] = ra;
run += 1;
}
let mut count = run;
while count >= 1usize << J[st.run_index] {
wr.put(1, 1);
count -= 1usize << J[st.run_index];
if st.run_index < 31 {
st.run_index += 1;
}
}
if x + run == w {
if count > 0 {
wr.put(1, 1);
}
return w;
}
wr.put(0, 1);
wr.put(count as u32, J[st.run_index]);
let ix = i32::from(line[x + run]);
let rb = prev[x + run + 1];
let ri_type = i32::from((ra - rb).abs() <= p.near);
let q = RI_BASE + ri_type as usize;
let (px, sign) = if ri_type == 1 { (ra, 1) } else { (rb, if ra > rb { -1 } else { 1 }) };
let e = reduce(sign * (ix - px), p);
let k = run_k(st, q, ri_type);
let n_neg = st.nn[ri_type as usize];
let map = i32::from((k == 0 && e > 0 && 2 * n_neg < st.n[q]) || (e < 0 && 2 * n_neg >= st.n[q]) || (e < 0 && k != 0));
let em = 2 * e.abs() - ri_type - map;
wr.golomb(em, k, p.limit - J[st.run_index] - 1, p.qbpp);
cur[x + run + 1] = reconstruct(px, sign, e, p);
update_run(st, q, e, em, ri_type, p);
if st.run_index > 0 {
st.run_index -= 1;
}
x + run + 1
}
/// Writes a lossless JPEG-LS file: 8 or 16 bits, gray or RGB, one scan per component.
pub(crate) fn encode(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
let enc = |msg: String| CodecError::encode(F, msg);
let img = src.converted(plan.layout, plan.sample);
let nc = match img.layout() {
ChannelLayout::Gray => 1,
ChannelLayout::Rgb => 3,
l => return Err(enc(format!("no JPEG-LS layout for {l:?}"))),
};
let precision: u32 = match img.sample_type() {
SampleType::U8 => 8,
SampleType::U16 => 16,
s => return Err(enc(format!("JPEG-LS stores 8 or 16 bits per sample, not {s:?}"))),
};
let (w, h) = img.dimensions();
let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else {
return Err(enc(format!("dimensions exceed 65535x65535: {w}x{h}")));
};
let (wu, hu) = (w as usize, h as usize);
let data = img.data();
if data.len() != wu * hu * nc * (precision as usize / 8) {
return Err(enc("the image has the wrong number of samples".into()));
}
let params = Params::new(precision, 0, [0; 5]).map_err(|_| enc("invalid coding parameters".into()))?;
let mut out = vec![0xFF, 0xD8, 0xFF, SOF55];
out.extend_from_slice(&((8 + 3 * nc) as u16).to_be_bytes());
out.push(precision as u8);
out.extend_from_slice(&h16.to_be_bytes());
out.extend_from_slice(&w16.to_be_bytes());
out.push(nc as u8);
for c in 0..nc {
out.extend_from_slice(&[c as u8 + 1, 0x11, 0]);
}
let mut plane = vec![0u16; wu * hu];
for c in 0..nc {
for (i, v) in plane.iter_mut().enumerate() {
let at = i * nc + c;
*v = if precision == 8 { u16::from(data[at]) } else { u16::from_ne_bytes([data[2 * at], data[2 * at + 1]]) };
}
out.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, c as u8 + 1, 0, 0, 0, 0]);
out.extend_from_slice(&encode_scan(&plane, wu, hu, &params));
}
out.extend_from_slice(&[0xFF, 0xD9]);
Ok(out)
}
struct Frame {
precision: u32,
width: usize,
height: usize,
components: Vec<u8>,
}
fn be16(b: &[u8], i: usize) -> Result<usize, CodecError> {
b.get(i..i + 2).map(|s| usize::from(u16::from_be_bytes([s[0], s[1]]))).ok_or_else(|| bad("truncated header"))
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if !looks_like_jpegls(b) {
return Err(bad("not a JPEG-LS file"));
}
let mut pos = 2;
let mut frame: Option<Frame> = None;
let mut preset = [0i32; 5];
let mut planes: Vec<Vec<u16>> = Vec::new();
let mut scans_done = 0;
loop {
// Find the next marker.
while b.get(pos) != Some(&0xFF) {
if pos >= b.len() {
return finish(frame, planes, scans_done);
}
pos += 1;
}
while b.get(pos + 1) == Some(&0xFF) {
pos += 1;
}
let Some(&marker) = b.get(pos + 1) else { return finish(frame, planes, scans_done) };
if marker == 0xD9 {
return finish(frame, planes, scans_done);
}
if marker == 0x00 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 {
pos += 2;
continue;
}
let len = be16(b, pos + 2)?;
let seg = b.get(pos + 4..pos + 2 + len).ok_or_else(|| bad("truncated marker segment"))?;
match marker {
SOF55 => {
if frame.is_some() || seg.len() < 6 {
return Err(bad("bad or repeated frame header"));
}
let (precision, height, width, nf) = (u32::from(seg[0]), be16(seg, 1)?, be16(seg, 3)?, usize::from(seg[5]));
if !(2..=16).contains(&precision) || width == 0 || height == 0 || !matches!(nf, 1 | 3) || seg.len() < 6 + 3 * nf {
return Err(bad("unsupported frame header"));
}
if height == 0 {
return Err(CodecError::unsupported(F, "a frame height set later by a DNL marker"));
}
let layout = if nf == 1 { ChannelLayout::Gray } else { ChannelLayout::Rgb };
let sample = if precision <= 8 { SampleType::U8 } else { SampleType::U16 };
limits.check(width as u32, height as u32, layout, sample)?;
// Each scan retains u16 samples even when the final image is 8-bit.
limits.check_bytes(width as u32, height as u32, 2)?;
let components = (0..nf).map(|i| seg[6 + 3 * i]).collect();
frame = Some(Frame { precision, width, height, components });
}
LSE => {
if seg.first() == Some(&1) && seg.len() >= 11 {
for (i, p) in preset.iter_mut().enumerate() {
*p = be16(seg, 1 + 2 * i)? as i32;
}
} else if seg.first() != Some(&1) {
return Err(CodecError::unsupported(F, "mapping tables and colour transforms (LSE)"));
}
}
0xDA => {
let f = frame.as_ref().ok_or_else(|| bad("a scan before the frame header"))?;
let ns = usize::from(*seg.first().ok_or_else(|| bad("empty scan header"))?);
if seg.len() < 1 + 2 * ns + 3 || ns != 1 {
return Err(CodecError::unsupported(F, "interleaved scans (ILV 1 and 2)"));
}
let (cs, tm, near, ilv) = (seg[1], seg[2], i32::from(seg[3]), seg[4]);
if tm != 0 {
return Err(CodecError::unsupported(F, "mapping tables"));
}
if ilv != 0 && f.components.len() > 1 {
return Err(CodecError::unsupported(F, "interleaved scans (ILV 1 and 2)"));
}
if seg[5] & 0x0f != 0 {
return Err(CodecError::unsupported(F, "a point transform"));
}
let idx = f.components.iter().position(|&c| c == cs).ok_or_else(|| bad("a scan names an unknown component"))?;
if planes.len() <= idx {
planes.resize(f.components.len(), Vec::new());
}
if !planes[idx].is_empty() {
return Err(bad("a component is coded twice"));
}
let params = Params::new(f.precision, near, preset)?;
let start = pos + 2 + len;
// The scan ends at the next real marker: an 0xFF followed by a byte with its top bit set.
let mut end = start;
while end < b.len() {
if b[end] == 0xFF && b.get(end + 1).is_none_or(|&n| n & 0x80 != 0) {
break;
}
end += 1;
}
let mut samples = Vec::new();
samples.try_reserve_exact(f.width * f.height).map_err(|_| bad("pixel buffer is too large"))?;
samples.resize(f.width * f.height, 0u16);
decode_scan(&b[start..end], f.width, f.height, &params, &mut samples)?;
planes[idx] = samples;
scans_done += 1;
pos = end;
continue;
}
_ => {}
}
pos += 2 + len;
}
}
fn finish(frame: Option<Frame>, planes: Vec<Vec<u16>>, scans: usize) -> Result<Image, CodecError> {
let f = frame.ok_or_else(|| bad("no frame header"))?;
if scans != f.components.len() || planes.iter().any(Vec::is_empty) || planes.len() != f.components.len() {
return Err(bad("the file ends before every component is coded"));
}
let (w, h, nc) = (f.width, f.height, f.components.len());
let layout = if nc == 1 { ChannelLayout::Gray } else { ChannelLayout::Rgb };
let p = f.precision;
let scale = |v: u16| -> u16 {
if p == 16 {
v
} else {
let v = u32::from(v);
((v << (16 - p)) | (v >> (2 * p).saturating_sub(16).min(15))) as u16
}
};
if p <= 8 {
let to8 = |v: u16| -> u8 { if p == 8 { v as u8 } else { (u32::from(v) * 255 / ((1u32 << p) - 1)) as u8 } };
let mut data = Vec::with_capacity(w * h * nc);
for i in 0..w * h {
for plane in &planes {
data.push(to8(plane[i]));
}
}
Image::from_u8(w as u32, h as u32, layout, data)
} else {
let mut data = Vec::with_capacity(w * h * nc);
for i in 0..w * h {
for plane in &planes {
data.push(scale(plane[i]));
}
}
Image::from_u16(w as u32, h as u32, layout, &data)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn noise_u16(n: usize, seed: &mut u32, mask: u32) -> Vec<u16> {
(0..n)
.map(|_| {
*seed ^= *seed << 13;
*seed ^= *seed >> 17;
*seed ^= *seed << 5;
(*seed & mask) as u16
})
.collect()
}
fn image(w: u32, h: u32, layout: ChannelLayout, wide: bool, kind: u8) -> Image {
let n = w as usize * h as usize * layout.channels();
let mut seed = 0x1234_5679u32;
let samples: Vec<u16> = match kind {
0 => noise_u16(n, &mut seed, if wide { 0xFFFF } else { 0xFF }),
1 => vec![if wide { 40_000 } else { 100 }; n],
_ => (0..n).map(|i| if wide { ((i / 3) * 37 % 65_536) as u16 } else { ((i / 3) % 251) as u16 }).collect(),
};
if wide {
Image::from_u16(w, h, layout, &samples).unwrap()
} else {
Image::from_u8(w, h, layout, samples.iter().map(|&v| v as u8).collect()).unwrap()
}
}
fn plan(img: &Image) -> Plan {
Plan { layout: img.layout(), sample: img.sample_type() }
}
#[test]
fn low_precision_samples_span_the_full_output_range() {
for precision in 2..=7 {
let max = (1u16 << precision) - 1;
let f = Frame { precision, width: usize::from(max) + 1, height: 1, components: vec![1] };
let image = finish(Some(f), vec![(0..=max).collect()], 1).unwrap();
let expected: Vec<u8> = (0..=max).map(|v| (u32::from(v) * 255 / u32::from(max)) as u8).collect();
assert_eq!(image.data(), expected);
}
}
#[test]
fn eight_bit_scan_intermediate_respects_allocation_limit() {
let img = image(8, 8, ChannelLayout::Gray, false, 0);
let bytes = encode(&img, plan(&img)).unwrap();
assert!(matches!(decode(&bytes, &Limits::new(8, 8, 64, 127)), Err(CodecError::LimitExceeded(_))));
assert!(decode(&bytes, &Limits::new(8, 8, 64, 128)).is_ok());
}
#[test]
fn round_trips_noise_flat_and_smooth_images() {
for layout in [ChannelLayout::Gray, ChannelLayout::Rgb] {
for wide in [false, true] {
for kind in 0..3 {
// Widths around the run-length steps, and single-pixel dimensions.
for (w, h) in [(1, 1), (1, 9), (2, 3), (7, 5), (33, 4), (300, 3), (1025, 2)] {
let img = image(w, h, layout, wide, kind);
let bytes = encode(&img, plan(&img)).unwrap();
assert!(looks_like_jpegls(&bytes));
let back = decode(&bytes, &Limits::default()).unwrap_or_else(|e| panic!("{layout:?} wide={wide} kind={kind} {w}x{h}: {e}"));
assert_eq!(back.data(), img.data(), "{layout:?} wide={wide} kind={kind} {w}x{h}");
}
}
}
}
}
#[test]
fn default_parameters_follow_the_standard() {
let p = Params::new(8, 0, [0; 5]).unwrap();
assert_eq!((p.maxval, p.range, p.qbpp, p.limit, p.t1, p.t2, p.t3, p.reset), (255, 256, 8, 32, 3, 7, 21, 64));
let p = Params::new(16, 0, [0; 5]).unwrap();
assert_eq!((p.maxval, p.range, p.qbpp, p.limit, p.t1, p.t2, p.t3), (65535, 65536, 16, 64, 18, 67, 276));
let p = Params::new(8, 3, [0; 5]).unwrap();
assert_eq!((p.range, p.t1), (38, 12)); // RANGE = floor((255 + 2 * 3) / 7) + 1
assert!(Params::new(8, 200, [0; 5]).is_err());
}
#[test]
fn detection_tells_jpeg_ls_from_jpeg() {
let img = image(4, 4, ChannelLayout::Gray, false, 0);
let ls = encode(&img, plan(&img)).unwrap();
assert!(looks_like_jpegls(&ls));
// A baseline JPEG starts SOI, APP0 / DQT, ... SOF0: not JPEG-LS.
assert!(!looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xC0, 0, 11]));
assert!(!looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11]));
// SOF55 after an application segment still is.
assert!(looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xE8, 0, 4, 0, 0, 0xFF, 0xF7, 0, 11]));
assert!(!looks_like_jpegls(&[0xFF, 0xD8]));
}
#[test]
fn interleaved_scans_and_tables_are_unsupported() {
let img = image(8, 4, ChannelLayout::Rgb, false, 2);
let good = encode(&img, plan(&img)).unwrap();
// The scan header body is Ns, Cs, Tm, NEAR, ILV, point transform, after the marker and length.
let sos = good.windows(2).position(|w| w == [0xFF, 0xDA]).unwrap() + 4;
let mut line = good.clone();
line[sos + 4] = 1;
assert!(matches!(decode(&line, &Limits::default()), Err(CodecError::Unsupported { .. })));
let mut mapped = good.clone();
mapped[sos + 2] = 1;
assert!(matches!(decode(&mapped, &Limits::default()), Err(CodecError::Unsupported { .. })));
let mut transform = good;
transform[sos + 5] = 1;
assert!(matches!(decode(&transform, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
#[test]
fn hostile_input_is_an_error_not_a_panic() {
for (w, h, layout, wide) in [(23, 9, ChannelLayout::Gray, false), (11, 7, ChannelLayout::Rgb, true)] {
let img = image(w, h, layout, wide, 0);
let good = encode(&img, plan(&img)).unwrap();
for len in 0..good.len() {
let _ = decode(&good[..len], &Limits::default());
}
// Flip every bit of the entropy-coded data and of the headers, one at a time.
for i in 0..good.len() {
for bit in 0..8 {
let mut c = good.clone();
c[i] ^= 1 << bit;
let _ = decode(&c, &Limits::default());
}
}
}
// A huge frame is stopped by the limits or the length check before any allocation.
let img = image(4, 4, ChannelLayout::Gray, false, 0);
let mut huge = encode(&img, plan(&img)).unwrap();
let sof = huge.windows(2).position(|w| w == [0xFF, 0xF7]).unwrap();
huge[sof + 5..sof + 9].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]);
assert!(decode(&huge, &Limits::default()).is_err());
}
}
+7 -55
View File
@@ -5,66 +5,18 @@
//! ColorSpace = sRGB as sRGB, unless Preferences › File Handling › Ignore EXIF Profile Tag is on.
//! Anything malformed, truncated or missing reads as no tag.
use crate::orientation::{Order, ifd0_entries, tiff_body};
/// IFD0's pointer to the Exif sub-IFD.
const TAG_EXIF_IFD: u16 = 0x8769;
/// The ColorSpace tag in the Exif sub-IFD.
const TAG_COLOR_SPACE: u16 = 0xA001;
const TYPE_SHORT: u16 = 3;
const TYPE_LONG: u16 = 4;
/// TIFF type 13: an IFD offset (some writers use it for sub-IFD pointers).
const TYPE_IFD: u16 = 13;
/// Bytes per classic TIFF IFD entry; EXIF is never BigTIFF.
const ENTRY: usize = 12;
/// No IFD can start inside the 8-byte TIFF header.
const HEADER: usize = 8;
/// The ColorSpace value meaning sRGB.
pub const EXIF_COLOR_SPACE_SRGB: u16 = 1;
/// The EXIF ColorSpace value of a TIFF-structured block (an EXIF payload with or without the
/// `Exif\0\0` prefix), or `None` when it has none or it is malformed.
pub fn exif_color_space(exif: &[u8]) -> Option<u16> {
let b = tiff_body(exif);
let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?;
if big_tiff {
return None;
}
let ptr = find(order, b, first, count, entry_bytes, TAG_EXIF_IFD)?;
if !matches!(order.u16(b, ptr.checked_add(2)?)?, TYPE_LONG | TYPE_IFD) || order.u32(b, ptr.checked_add(4)?)? != 1 {
return None;
}
let ifd = usize::try_from(order.u32(b, ptr.checked_add(8)?)?).ok()?;
if ifd < HEADER {
return None;
}
let n = usize::from(order.u16(b, ifd)?);
let first = ifd.checked_add(2)?;
// The whole table must be present.
if first.checked_add(n.checked_mul(ENTRY)?)? > b.len() {
return None;
}
let e = find(order, b, first, n, ENTRY, TAG_COLOR_SPACE)?;
if order.u32(b, e.checked_add(4)?)? != 1 {
return None;
}
match order.u16(b, e.checked_add(2)?)? {
TYPE_SHORT => order.u16(b, e.checked_add(8)?),
TYPE_LONG => u16::try_from(order.u32(b, e.checked_add(8)?)?).ok(),
_ => None,
}
}
/// Offset of the first entry tagged `tag` in the IFD whose `count` entries start at `first`.
fn find(order: Order, b: &[u8], first: usize, count: usize, entry_bytes: usize, tag: u16) -> Option<usize> {
(0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(tag))
}
// Share parsing and malformed-input policy with every craft-metadata caller.
pub use craft_metadata::edit::{EXIF_COLOR_SPACE_SRGB, exif_color_space};
#[cfg(test)]
mod tests {
use super::*;
const TAG_EXIF_IFD: u16 = 0x8769;
const TAG_COLOR_SPACE: u16 = 0xA001;
const TYPE_SHORT: u16 = 3;
const TYPE_LONG: u16 = 4;
/// A little- or big-endian EXIF block: IFD0 holding only the Exif pointer, then an Exif IFD
/// holding `entries` (tag, type, count, value).
fn exif(big_endian: bool, entries: &[(u16, u16, u32, u32)]) -> Vec<u8> {
+21 -6
View File
@@ -26,7 +26,8 @@ pub enum Format {
OpenExr,
/// Radiance RGBE.
Hdr,
/// Encode-only behind the `avif` feature; see [`ASYMMETRIC_EXCEPTIONS`].
/// Encoded behind the `avif` feature and decoded behind `avif-decode` (native targets); see
/// [`ASYMMETRIC_EXCEPTIONS`] for a build with only one of them.
Avif,
/// HEIF/HEIC (HEVC-coded): read-only; see [`ASYMMETRIC_EXCEPTIONS`].
Heif,
@@ -44,6 +45,8 @@ pub enum Format {
Iff,
/// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`].
Jp2,
/// JPEG-LS (`.jls`): lossless and near-lossless gray and RGB.
JpegLs,
}
/// What a format can hold **and** what this crate reads/writes for it.
@@ -89,8 +92,10 @@ pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[
),
(
Format::Avif,
"AVIF encode uses ravif (pure Rust) but decoding requires dav1d (C); read stays unsupported \
until a pure-Rust AV1 decoder is viable. Only enabled with the non-default `avif` feature.",
"AVIF encode (ravif and rav1e, pure Rust) is behind the non-default `avif` feature and AVIF \
decode (craft-video's clean-room pure-Rust AV1 decoder) behind the non-default \
`avif-decode` feature, native targets only (on wasm32 a decoder panic would abort). A build \
with only one of them reads or writes AVIF, not both.",
),
(Format::Jxl, "Lossless JPEG XL export uses the pure-Rust zune-jpegxl encoder; JPEG XL reading is tracked separately and is not yet available."),
(
@@ -113,7 +118,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::
impl Format {
/// Every format known to the crate (enabled or not).
pub const ALL: [Format; 24] = [
pub const ALL: [Format; 25] = [
Format::Png,
Format::Jpeg,
Format::Jp2,
@@ -138,6 +143,7 @@ impl Format {
Format::Cineon,
Format::Dicom,
Format::Iff,
Format::JpegLs,
];
pub fn caps(self) -> FormatCaps {
@@ -176,6 +182,7 @@ impl Format {
Format::Dicom => "DICOM",
Format::Iff => "Amiga IFF",
Format::Jp2 => "JPEG 2000",
Format::JpegLs => "JPEG-LS",
}
}
@@ -207,6 +214,7 @@ impl Format {
Format::Dicom => &["dcm", "dicom", "dc3"],
Format::Iff => &["iff", "lbm", "ilbm"],
Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"],
Format::JpegLs => &["jls"],
}
}
@@ -236,13 +244,14 @@ impl Format {
Format::Dicom => "application/dicom",
Format::Iff => "image/x-iff",
Format::Jp2 => "image/jp2",
Format::JpegLs => "image/jls",
}
}
/// Maximum encodable dimensions, if the format has a hard limit.
pub fn max_dimensions(self) -> Option<(u32, u32)> {
match self {
Format::Jpeg | Format::Gif | Format::Iff => Some((65535, 65535)),
Format::Jpeg | Format::Gif | Format::Iff | Format::JpegLs => Some((65535, 65535)),
Format::WebP => Some((16384, 16384)),
Format::Ico => Some((256, 256)),
_ => None,
@@ -298,7 +307,9 @@ pub fn caps(format: Format) -> FormatCaps {
Format::Qoi => base,
Format::OpenExr => FormatCaps { depths: &[S::F16, S::F32], layouts: RGB_GRAY, ..base },
Format::Hdr => FormatCaps { depths: &[S::F32], layouts: &[L::Rgb], alpha: false, lossy: true, ..base },
Format::Avif => FormatCaps { read: false, write: cfg!(feature = "avif"), lossy: true, ..base },
Format::Avif => {
FormatCaps { read: cfg!(all(feature = "avif-decode", not(target_arch = "wasm32"))), write: cfg!(feature = "avif"), lossy: true, ..base }
}
Format::Heif => {
FormatCaps { read: cfg!(feature = "heif"), write: false, depths: &[S::U8, S::U16], icc: true, exif: true, xmp: true, lossy: true, ..base }
}
@@ -310,6 +321,7 @@ pub fn caps(format: Format) -> FormatCaps {
Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base },
Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base },
Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base },
Format::JpegLs => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base },
Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base },
}
}
@@ -321,6 +333,9 @@ pub fn detect(bytes: &[u8]) -> Option<Format> {
if b.starts_with(b"\x89PNG\r\n\x1a\n") {
return Some(Format::Png);
}
if crate::codecs::jpegls::looks_like_jpegls(b) {
return Some(Format::JpegLs);
}
if b.starts_with(&[0xFF, 0xD8, 0xFF]) {
return Some(Format::Jpeg);
}
+82
View File
@@ -0,0 +1,82 @@
//! Validated Samsung SEF trailer repair for JPEGs missing their final EOI marker.
use std::borrow::Cow;
/// Whether the first JPEG image reaches an EOI through its marker and scan structure.
pub fn reaches_eoi(bytes: &[u8]) -> bool {
crate::codecs::jpeg::reaches_eoi(bytes)
}
/// Borrow ordinary input unchanged. For a validated Samsung SEF trailer following an open JPEG
/// scan, remove that trailer and append the missing EOI. The owned copy is made only on repair.
pub fn repair(bytes: &[u8]) -> Cow<'_, [u8]> {
let Some(image) = image_end(bytes).and_then(|end| bytes.get(..end)) else { return Cow::Borrowed(bytes) };
if reaches_eoi(image) {
return Cow::Borrowed(bytes);
}
let mut fixed = Vec::with_capacity(image.len().saturating_add(2));
fixed.extend_from_slice(image);
fixed.extend_from_slice(&[0xff, 0xd9]);
let in_scan = craft_metadata::inspect_jpeg(&fixed).is_some_and(|m| m.first_scan_components > 0) && reaches_eoi(&fixed);
if in_scan { Cow::Owned(fixed) } else { Cow::Borrowed(bytes) }
}
fn image_end(b: &[u8]) -> Option<usize> {
let le32 = |at: usize| b.get(at..at.checked_add(4)?)?.try_into().ok().map(|v| u32::from_le_bytes(v) as usize);
let tail = b.len().checked_sub(8)?;
if b.get(tail.checked_add(4)?..)? != b"SEFT" {
return None;
}
let dir = tail.checked_sub(le32(tail)?)?;
if b.get(dir..dir.checked_add(4)?)? != b"SEFH" {
return None;
}
let count = le32(dir.checked_add(8)?)?;
let entries = dir.checked_add(12)?;
if count == 0 || count > tail.checked_sub(entries)? / 12 {
return None;
}
let mut start = dir;
for i in 0..count {
let entry = entries.checked_add(i.checked_mul(12)?)?;
let block = dir.checked_sub(le32(entry.checked_add(4)?)?)?;
let len = le32(entry.checked_add(8)?)?;
let kind = |at: usize| b.get(at.checked_add(2)?..at.checked_add(4)?);
if len < 4 || block.checked_add(len)? > dir || kind(block)? != kind(entry)? {
return None;
}
start = start.min(block);
}
Some(start)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn repairs_only_valid_sef_after_an_open_scan() {
let image = [0xff, 0xd8, 0xff, 0xc0, 0, 8, 8, 0, 1, 0, 1, 1, 0xff, 0xda, 0, 3, 1, 0x12, 0xff, 0, 0x34];
let mut b = image.to_vec();
let block = [0, 0, 0xe0, 0x08, 1, 2, 3, 4];
b.extend_from_slice(&block);
b.extend_from_slice(b"SEFH");
b.extend_from_slice(&106u32.to_le_bytes());
b.extend_from_slice(&1u32.to_le_bytes());
b.extend_from_slice(&[0, 0, 0xe0, 0x08]);
b.extend_from_slice(&(block.len() as u32).to_le_bytes());
b.extend_from_slice(&(block.len() as u32).to_le_bytes());
b.extend_from_slice(&24u32.to_le_bytes());
b.extend_from_slice(b"SEFT");
let mut expected = image.to_vec();
expected.extend_from_slice(&[0xff, 0xd9]);
assert_eq!(repair(&b).as_ref(), expected);
assert!(matches!(repair(&image), Cow::Borrowed(_)));
b[image.len() + 2] = 0;
assert!(matches!(repair(&b), Cow::Borrowed(_)));
}
#[test]
fn eoi_requires_valid_lengths_and_skips_stuffed_bytes() {
assert!(reaches_eoi(&[0xff, 0xd8, 0xff, 0, 0xff, 0xd9]));
assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 1, 0xff, 0xd9]));
assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 4, 0xff, 0xd9]));
}
}
+24 -9
View File
@@ -16,6 +16,7 @@
mod allocation;
pub mod apng;
pub mod bmp_probe;
pub mod cicp;
mod codecs;
mod error;
@@ -25,15 +26,17 @@ mod fidelity;
mod format;
mod image;
mod inspection;
pub mod jpeg_sef;
mod lab;
mod options;
pub mod orientation;
pub mod png_probe;
pub mod resolution;
pub mod source_settings;
pub mod tiff_probe;
pub mod web;
pub mod webp_probe;
pub use crate::allocation::probe_allocation_size;
pub use crate::allocation::{probe_allocation_size, probe_stored_size};
pub use crate::cicp::Cicp;
pub use crate::cicp::ContentLightLevel;
pub use crate::codecs::avif::{HDR_REFERENCE_WHITE_NITS, HdrTransfer, encode_avif_hdr, encode_avif_with_speed};
@@ -65,7 +68,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_
pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding};
pub use half::f16;
use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm};
use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm};
/// Detect the format and decode with default [`Limits`].
pub fn decode(bytes: &[u8]) -> Result<Image, CodecError> {
@@ -104,11 +107,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::JpegLs => jpegls::decode(bytes, l),
Format::OpenExr => exr::decode(bytes, l),
Format::Heif => heif::decode(bytes, l, opts.keep_orientation),
Format::Jp2 => jp2::decode(bytes, l),
Format::Jxl => Err(CodecError::unsupported(format, "JPEG XL reading is not yet available; export is supported")),
Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l),
#[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))]
Format::Avif => codecs::avif_decode::decode(bytes, l, opts.keep_orientation),
#[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))]
Format::Avif => via_image::decode(format, bytes, l),
Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::decode(format, bytes, l),
}?;
finish_format(format, bytes, img, opts)
}
@@ -165,10 +173,11 @@ fn finish_format(format: Format, bytes: &[u8], mut img: Image, opts: &DecodeOpti
}
// Turn the pixels upright, like Photoshop: a TIFF records it in the decoded page's own
// directory, the others in their EXIF block. The metadata is rewritten to Orientation = 1
// on the way. HEIF keeps it in its container, and its decoder has already applied it.
// on the way. HEIF and AVIF keep it in their container, and their decoders have already
// applied it.
let o = match format {
Format::Tiff => tiff::orientation(bytes, None),
Format::Heif => 1,
Format::Heif | Format::Avif => 1,
Format::Dpx => dpx::orientation(bytes),
Format::Cineon => dpx::cineon_orientation(bytes),
_ => img.meta.exif.as_deref().map_or(1, exif_orientation),
@@ -197,10 +206,11 @@ pub fn decode_png_rows<E: From<CodecError>>(
}
/// The upright pixel size [`decode`] would return, read from the file's header without decoding
/// pixels, for WebP, OpenEXR (flat) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same
/// pixels, for WebP, OpenEXR (flat), AVIF (with `avif-decode`) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same
/// header reads and size checks as the decoder (default [`Limits`]) and swaps the size for an
/// EXIF orientation of 5–8, so wherever [`decode`] succeeds the two agree. Other formats (and
/// deep EXR or AVIF) return [`CodecError::Unsupported`]: decode those to learn their size.
/// EXIF orientation of 5–8 (AVIF: its container's `clap` and `irot`), so wherever [`decode`]
/// succeeds the two agree. Other formats (and deep EXR) return [`CodecError::Unsupported`]: decode
/// those to learn their size.
pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> {
let format = detect(bytes).ok_or(CodecError::UnknownFormat)?;
if !caps(format).read {
@@ -210,7 +220,11 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> {
let (w, h, exif) = match format {
Format::WebP => webp::probe(bytes, l)?,
Format::OpenExr => exr::probe(bytes, l).map(|(w, h)| (w, h, None))?,
Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::probe(format, bytes, l)?,
#[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))]
Format::Avif => codecs::avif_decode::probe(bytes, l).map(|(w, h)| (w, h, None))?,
#[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))]
Format::Avif => via_image::probe(format, bytes, l)?,
Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::probe(format, bytes, l)?,
_ => return Err(CodecError::unsupported(format, "no header-only size probe")),
};
let o = exif.as_deref().map_or(1, exif_orientation);
@@ -460,6 +474,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result<Vec
Format::Cineon => dpx::encode_cineon(image, plan),
Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")),
Format::Iff => iff::encode(image, plan),
Format::JpegLs => jpegls::encode(image, plan),
Format::OpenExr => exr::encode(image, plan, opts),
Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")),
Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")),
+5 -206
View File
@@ -20,213 +20,12 @@ use std::borrow::Cow;
use crate::error::CodecError;
use crate::image::Image;
/// The TIFF/EXIF Orientation tag.
const TAG_ORIENTATION: u16 = 274;
/// Field types accepted for the Orientation tag: SHORT (the spec) and LONG
/// (written by some cameras and tools).
const TYPE_SHORT: u16 = 3;
const TYPE_LONG: u16 = 4;
/// Bytes per IFD entry.
const ENTRY: usize = 12;
/// The TIFF header is 8 bytes, so no IFD can start before it.
const HEADER: usize = 8;
/// BigTIFF uses a 16-byte header, 20-byte entries, and 8-byte offsets.
const BIG_ENTRY: usize = 20;
const BIG_HEADER: usize = 16;
// Keep byte-level EXIF/XMP handling in one implementation so fixes apply to all callers.
pub use craft_metadata::edit::{exif_orientation, upright_exif, upright_xmp};
/// Byte order of a TIFF structure.
#[derive(Clone, Copy)]
pub(crate) enum Order {
Little,
Big,
}
impl Order {
pub(crate) fn u16(self, b: &[u8], at: usize) -> Option<u16> {
let s: [u8; 2] = b.get(at..at.checked_add(2)?)?.try_into().ok()?;
Some(match self {
Order::Little => u16::from_le_bytes(s),
Order::Big => u16::from_be_bytes(s),
})
}
pub(crate) fn u32(self, b: &[u8], at: usize) -> Option<u32> {
let s: [u8; 4] = b.get(at..at.checked_add(4)?)?.try_into().ok()?;
Some(match self {
Order::Little => u32::from_le_bytes(s),
Order::Big => u32::from_be_bytes(s),
})
}
pub(crate) fn u64(self, b: &[u8], at: usize) -> Option<u64> {
let s: [u8; 8] = b.get(at..at.checked_add(8)?)?.try_into().ok()?;
Some(match self {
Order::Little => u64::from_le_bytes(s),
Order::Big => u64::from_be_bytes(s),
})
}
}
/// The TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped).
pub(crate) fn tiff_body(b: &[u8]) -> &[u8] {
b.strip_prefix(b"Exif\0\0").unwrap_or(b)
}
/// IFD0 of a TIFF structure: byte order, entry layout, and entry offsets.
///
/// The full table and next-IFD pointer must be present, and IFD0 must start
/// after the corresponding TIFF or BigTIFF header.
pub(crate) fn ifd0_entries(b: &[u8]) -> Option<(Order, usize, usize, usize, bool)> {
let (order, big_tiff) = match b.get(0..4)? {
[b'I', b'I', 42, 0] => (Order::Little, false),
[b'M', b'M', 0, 42] => (Order::Big, false),
[b'I', b'I', 43, 0] => (Order::Little, true),
[b'M', b'M', 0, 43] => (Order::Big, true),
_ => return None,
};
let (ifd, count_bytes, entry_bytes, next_ifd_bytes, header) = if big_tiff {
if order.u16(b, 4)? != 8 || order.u16(b, 6)? != 0 {
return None;
}
(usize::try_from(order.u64(b, 8)?).ok()?, 8usize, BIG_ENTRY, 8usize, BIG_HEADER)
} else {
(usize::try_from(order.u32(b, 4)?).ok()?, 2usize, ENTRY, 4usize, HEADER)
};
if ifd < header {
return None;
}
let count = if big_tiff { usize::try_from(order.u64(b, ifd)?).ok()? } else { usize::from(order.u16(b, ifd)?) };
let first = ifd.checked_add(count_bytes)?;
let next_ifd = first.checked_add(count.checked_mul(entry_bytes)?)?;
if next_ifd.checked_add(next_ifd_bytes)? > b.len() {
return None;
}
// In bounds: every entry ends at or before `next_ifd`.
Some((order, first, count, entry_bytes, big_tiff))
}
/// A well-formed Orientation entry's value: where it sits and its field type.
#[derive(Clone, Copy)]
struct Value {
at: usize,
ty: u16,
}
impl Value {
/// The stored value (a SHORT or LONG, left-justified in the 4-byte field).
fn read(self, order: Order, b: &[u8]) -> Option<u32> {
match self.ty {
TYPE_SHORT => order.u16(b, self.at).map(u32::from),
_ => order.u32(b, self.at),
}
}
/// 1 in this entry's own type, width and byte order.
fn one(self, order: Order) -> Vec<u8> {
match (self.ty, order) {
(TYPE_SHORT, Order::Little) => 1u16.to_le_bytes().to_vec(),
(TYPE_SHORT, Order::Big) => 1u16.to_be_bytes().to_vec(),
(_, Order::Little) => 1u32.to_le_bytes().to_vec(),
(_, Order::Big) => 1u32.to_be_bytes().to_vec(),
}
}
}
/// The value of the entry at `e` if it is a well-formed Orientation entry:
/// exactly one SHORT or LONG.
fn orientation_value(order: Order, b: &[u8], e: usize, big_tiff: bool) -> Option<Value> {
let count = if big_tiff { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) };
if order.u16(b, e)? != TAG_ORIENTATION || count != 1 {
return None;
}
let ty = order.u16(b, e.checked_add(2)?)?;
let value_offset = if big_tiff { 12 } else { 8 };
matches!(ty, TYPE_SHORT | TYPE_LONG).then_some(Value { at: e.checked_add(value_offset)?, ty })
}
/// Locates the Orientation value in IFD0.
///
/// The **first** Orientation entry wins (like libexif and Photoshop); if that
/// one is malformed (count ≠ 1, or neither SHORT nor LONG) the tag is ignored,
/// even when a later duplicate is well formed.
fn find_entry(b: &[u8]) -> Option<(Order, Value)> {
let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?;
let e = (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(TAG_ORIENTATION))?;
Some((order, orientation_value(order, b, e, big_tiff)?))
}
/// The orientation (1–8) recorded in a TIFF-structured block: an EXIF payload
/// (with or without the `Exif\0\0` prefix) or a whole TIFF file. Missing,
/// malformed or out-of-range values give 1.
pub fn exif_orientation(exif: &[u8]) -> u16 {
let b = tiff_body(exif);
find_entry(b).and_then(|(order, v)| v.read(order, b)).and_then(|v| u16::try_from(v).ok()).filter(|v| (1..=8).contains(v)).unwrap_or(1)
}
/// `exif` with its Orientation rewritten to 1, borrowed when there is nothing
/// to change (no well-formed tag, already 1, or unparseable).
///
/// Only the value bytes of each well-formed Orientation entry change, written
/// as 1 in the entry's existing type and width (SHORT or LONG); every other
/// byte (other tags, their offset-based values, sub-IFDs) is kept as is.
/// Well-formed duplicates are all set to 1, so a reader that lets a later
/// duplicate win cannot rotate the upright pixels either.
pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> {
let body = tiff_body(exif);
let prefix = exif.len() - body.len();
let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else {
return Cow::Borrowed(exif);
};
let values: Vec<Value> = (0..count)
.filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?))
.filter_map(|e| orientation_value(order, body, e, big_tiff))
.collect();
if values.iter().all(|v| v.read(order, body) == Some(1)) {
return Cow::Borrowed(exif);
}
let mut out = exif.to_vec();
for v in values {
let one = v.one(order);
let slot = prefix.checked_add(v.at).and_then(|at| out.get_mut(at..at.checked_add(one.len())?));
if let Some(slot) = slot {
slot.copy_from_slice(&one);
}
}
Cow::Owned(out)
}
/// `xmp` with any `tiff:Orientation` (attribute or element form) set to 1,
/// borrowed when there is nothing to change.
pub fn upright_xmp(xmp: &str) -> Cow<'_, str> {
const KEY: &str = "tiff:Orientation";
if !xmp.contains(KEY) {
return Cow::Borrowed(xmp);
}
let mut out = String::with_capacity(xmp.len());
let mut rest = xmp;
let mut changed = false;
while let Some(i) = rest.find(KEY) {
let (head, tail) = rest.split_at(i + KEY.len());
out.push_str(head);
rest = tail;
// `tiff:Orientation="6"`, `tiff:Orientation='6'` or `<tiff:Orientation>6<`.
let open = rest.char_indices().take_while(|&(_, c)| c.is_ascii_whitespace() || matches!(c, '=' | '"' | '\'' | '>')).last();
let Some((at, c)) = open else { continue };
if !matches!(c, '"' | '\'' | '>') {
continue;
}
let start = at + 1;
let digits = rest.get(start..).map_or(0, |s| s.bytes().take_while(u8::is_ascii_digit).count());
if digits == 0 || rest.get(start..start + digits) == Some("1") {
continue;
}
out.push_str(rest.get(..start).unwrap_or_default());
out.push('1');
rest = rest.get(start + digits..).unwrap_or_default();
changed = true;
}
out.push_str(rest);
if changed { Cow::Owned(out) } else { Cow::Borrowed(xmp) }
/// TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped).
pub(crate) fn tiff_body(bytes: &[u8]) -> &[u8] {
bytes.strip_prefix(b"Exif\0\0").unwrap_or(bytes)
}
/// Source pixel index for output pixel `(0, y2)` and the step per output column, for
+101
View File
@@ -0,0 +1,101 @@
//! First-directory TIFF inspection without decoding pixels.
/// A header inspection failure, distinguishing caller limits from unsupported decoder layouts.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Dimensions exceed the caller's pixel limit.
#[error("TIFF dimensions {width}x{height} exceed the pixel limit")]
TooLarge {
/// Stored width.
width: u32,
/// Stored height.
height: u32,
},
/// The shared TIFF decoder does not support this layout.
#[error("unsupported TIFF: {0}")]
Unsupported(&'static str),
/// The file's header, directory or image-data bounds are malformed.
#[error("malformed TIFF: {0}")]
Malformed(String),
}
/// First-directory values needed to preflight decoding.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DecodeInfo {
/// Stored width.
pub width: u32,
/// Stored height.
pub height: u32,
/// First sample's bit depth, defaulting to eight.
pub bit_depth: u8,
/// Raw orientation tag, if present.
pub orientation: Option<u16>,
}
fn first_directory(bytes: &[u8], max_pixels: u64) -> Result<(craft_tiff::Ifd, craft_tiff::image::ImageInfo), Error> {
let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?;
if !complete {
return Err(Error::Malformed("truncated TIFF".into()));
}
let ifd = tiff.ifds.into_iter().next().ok_or_else(|| Error::Malformed("no IFD".into()))?;
let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?;
if info.width == 0 || info.height == 0 {
return Err(Error::Malformed("zero dimension".into()));
}
if u64::from(info.width) * u64::from(info.height) > max_pixels {
return Err(Error::TooLarge { width: info.width, height: info.height });
}
Ok((ifd, info))
}
/// Parse the first directory and enforce the pixel limit before decoding.
/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder.
pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result<DecodeInfo, Error> {
let (ifd, info) = first_directory(bytes, max_pixels)?;
Ok(DecodeInfo {
width: info.width,
height: info.height,
bit_depth: info.bits_per_sample.first().copied().unwrap_or(8) as u8,
orientation: ifd.u16(craft_tiff::tags::ORIENTATION),
})
}
/// Stored dimensions and orientation of the first directory, after checking the layouts and
/// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed.
pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> {
let (ifd, info) = first_directory(bytes, max_pixels)?;
let bits = info.bits_per_sample.first().copied().unwrap_or(1);
if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 {
return Err(Error::Unsupported("planar TIFF with samples narrower than a byte"));
}
if !(matches!(info.photometric, 0 | 1 | 2 | 3 | 5) || info.photometric == 6 && info.compression == 7) {
return Err(Error::Unsupported("colour type"));
}
if !matches!(info.compression, 1 | 4 | 5 | 7 | 8 | 32773 | 32946) {
return Err(Error::Unsupported("compression method"));
}
let len = bytes.len() as u64;
if info.offsets.is_empty()
|| info.offsets.iter().zip(&info.byte_counts).any(|(&o, &c)| c > 0 && (o >= len || (info.compression == 1 && o.saturating_add(c) > len)))
{
return Err(Error::Malformed("truncated: image data past the end of the file".into()));
}
let orientation = ifd.u16(craft_tiff::tags::ORIENTATION).filter(|o| (1..=8).contains(o)).unwrap_or(1);
Ok((info.width, info.height, orientation))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_first_directory_and_enforces_limits_and_bounds() {
let image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap();
let bytes = crate::encode(&image, crate::Format::Tiff, &crate::EncodeOptions::default()).unwrap();
assert_eq!(header(&bytes, 6).unwrap(), (2, 3, 1));
let info = decode_info(&bytes, 6).unwrap();
assert_eq!((info.width, info.height, info.bit_depth), (2, 3, 8));
assert!(matches!(decode_info(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 })));
assert!(matches!(header(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 })));
assert!(header(&bytes[..8], 6).is_err());
}
}