mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
Merge pull request #1077 from vangel76/dng-decode-plus
#1065: DNG raw settings, faster decode, opcode lists, lossy JPEG DNG
This commit is contained in:
+92
-18
@@ -1,6 +1,9 @@
|
||||
//! The raw image data of a DNG: finding the main raw IFD, and reading its strips or tiles
|
||||
//! (uncompressed or lossless JPEG) into one array of 16-bit samples.
|
||||
|
||||
#[cfg(feature = "threads")]
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::tiff::{Ifd, Tiff, tags};
|
||||
use crate::{Error, Result, ljpeg};
|
||||
|
||||
@@ -21,7 +24,7 @@ pub struct RawLayout {
|
||||
pub height: usize,
|
||||
pub samples_per_pixel: usize,
|
||||
pub bits: u32,
|
||||
/// 1 uncompressed, 7 lossless JPEG.
|
||||
/// 1 uncompressed, 7 lossless JPEG, 34892 lossy JPEG.
|
||||
pub compression: u64,
|
||||
pub photometric: u64,
|
||||
}
|
||||
@@ -107,7 +110,12 @@ pub fn layout(t: &Tiff, ifd: &Ifd) -> Result<RawLayout> {
|
||||
}
|
||||
7 => {}
|
||||
8 => return Err(Error::Unsupported("deflate-compressed DNG".into())),
|
||||
34892 => return Err(Error::Unsupported("lossy JPEG DNG".into())),
|
||||
// DNG 1.4: lossy JPEG only for 8-bit LinearRaw (or mask) data
|
||||
34892 if photometric != PHOTOMETRIC_LINEAR_RAW => {
|
||||
return Err(Error::Unsupported("lossy JPEG DNG of CFA data (DNG allows it only for 8-bit LinearRaw)".into()));
|
||||
}
|
||||
34892 if bits != 8 => return Err(Error::Unsupported(format!("lossy JPEG DNG with {bits}-bit samples (DNG allows only 8)"))),
|
||||
34892 => {}
|
||||
52546 => return Err(Error::Unsupported("JPEG XL DNG".into())),
|
||||
c => return Err(Error::Unsupported(format!("DNG compression {c}"))),
|
||||
}
|
||||
@@ -210,6 +218,7 @@ fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result<Vec<u16>>
|
||||
let need = c.w * c.h * l.samples_per_pixel;
|
||||
match l.compression {
|
||||
1 => unpack(c.bytes, c.w, c.h, l.samples_per_pixel, l.bits, big_endian),
|
||||
34892 => lossy_jpeg(c.bytes, need, l.samples_per_pixel),
|
||||
_ => {
|
||||
// the JPEG's samples fill the chunk in raster order (its own width × components may
|
||||
// differ from the chunk's, e.g. two components of half the width)
|
||||
@@ -226,6 +235,41 @@ fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result<Vec<u16>>
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode a baseline (lossy) JPEG tile or strip (Compression 34892) into `need` 8-bit samples:
|
||||
/// one component (greyscale) or three (colour, converted from YCbCr by the JPEG decoder as the
|
||||
/// JPEG says), in raster order.
|
||||
fn lossy_jpeg(bytes: &[u8], need: usize, spp: usize) -> Result<Vec<u16>> {
|
||||
use image::ImageDecoder;
|
||||
let bad = |_| Error::Malformed("lossy JPEG tile");
|
||||
let decode = || -> Result<Vec<u16>> {
|
||||
let mut d = image::codecs::jpeg::JpegDecoder::new(std::io::Cursor::new(bytes)).map_err(bad)?;
|
||||
let channels = match d.color_type() {
|
||||
image::ColorType::L8 => 1usize,
|
||||
image::ColorType::Rgb8 => 3,
|
||||
c => return Err(Error::Unsupported(format!("lossy JPEG DNG tile of colour type {c:?}"))),
|
||||
};
|
||||
if channels != spp {
|
||||
return Err(Error::Malformed("lossy JPEG components differ from SamplesPerPixel"));
|
||||
}
|
||||
let (w, h) = d.dimensions();
|
||||
let total = (w as usize).checked_mul(h as usize).and_then(|v| v.checked_mul(channels)).ok_or(Error::TooLarge)?;
|
||||
if total < need {
|
||||
return Err(Error::Malformed("lossy JPEG smaller than its tile or strip"));
|
||||
}
|
||||
if total > need.saturating_mul(4).saturating_add(65_536) || total > MAX_SAMPLES {
|
||||
return Err(Error::TooLarge);
|
||||
}
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_alloc = Some(total as u64 * 4 + (1 << 20));
|
||||
d.set_limits(limits).map_err(bad)?;
|
||||
let mut buf = vec![0u8; total];
|
||||
d.read_image(&mut buf).map_err(bad)?;
|
||||
Ok(buf.iter().take(need).map(|&v| u16::from(v)).collect())
|
||||
};
|
||||
// a third-party decoder: a panic in it is an error for this file, not a crash
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(decode)).unwrap_or(Err(Error::Malformed("lossy JPEG decoder failed")))
|
||||
}
|
||||
|
||||
/// Read the raw image of `ifd`.
|
||||
pub fn read(t: &Tiff, ifd: &Ifd) -> Result<RawImage> {
|
||||
let l = layout(t, ifd)?;
|
||||
@@ -235,9 +279,12 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result<RawImage> {
|
||||
return Err(Error::TooLarge);
|
||||
}
|
||||
let chunks = chunks(t, ifd, &l)?;
|
||||
// every sample takes at least one bit of file (a Huffman code is at least one bit long)
|
||||
// every sample takes at least one bit of file (a Huffman code is at least one bit long);
|
||||
// lossy JPEG at least two bits per 8 × 8 block and component (a DC code and an end of
|
||||
// block), so a tiny file cannot claim a huge picture
|
||||
let per_byte = if l.compression == 34892 { 256 } else { 8 };
|
||||
let coded: usize = chunks.iter().map(|c| c.bytes.len()).fold(0usize, usize::saturating_add);
|
||||
if total.div_ceil(8) > coded {
|
||||
if total.div_ceil(per_byte) > coded {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
let mut decoded_samples = 0usize;
|
||||
@@ -247,25 +294,52 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result<RawImage> {
|
||||
if n > MAX_SAMPLES || decoded_samples > MAX_DECODED_TILE_SAMPLES {
|
||||
return Err(Error::TooLarge);
|
||||
}
|
||||
if n.div_ceil(8) > c.bytes.len() {
|
||||
if n.div_ceil(per_byte) > c.bytes.len() {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
}
|
||||
let mut data = vec![0u16; total];
|
||||
let row_len = l.width * spp;
|
||||
for c in &chunks {
|
||||
// Copy each padded tile immediately. Retaining every decoded tile until assembly lets
|
||||
// repeated ranges multiply memory beyond the final raw frame's bounded allocation.
|
||||
let d = decode_chunk(c, &l, t.big_endian)?;
|
||||
let cw = c.w.min(l.width - c.x);
|
||||
let ch = c.h.min(l.height - c.y);
|
||||
for r in 0..ch {
|
||||
let src = d.get(r * c.w * spp..(r * c.w + cw) * spp).ok_or(Error::Truncated)?;
|
||||
let at = (c.y + r) * row_len + c.x * spp;
|
||||
data.get_mut(at..at + cw * spp).ok_or(Error::Truncated)?.copy_from_slice(src);
|
||||
let raw = |data| RawImage { width: l.width, height: l.height, samples_per_pixel: spp, bits: l.bits, data };
|
||||
// one chunk covering the image (a single strip): its samples are the image, no copy
|
||||
if let [c] = chunks.as_slice()
|
||||
&& (c.x, c.y, c.w) == (0, 0, l.width)
|
||||
&& c.h >= l.height
|
||||
{
|
||||
let mut d = decode_chunk(c, &l, t.big_endian)?;
|
||||
d.truncate(total);
|
||||
if d.len() == total {
|
||||
return Ok(raw(d));
|
||||
}
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
Ok(RawImage { width: l.width, height: l.height, samples_per_pixel: spp, bits: l.bits, data })
|
||||
// bands of one row of tiles (or one strip): decoded in parallel, each band copying its own
|
||||
// chunks into place
|
||||
let row_len = l.width * spp;
|
||||
let (Some(first), true) = (chunks.first(), row_len > 0) else { return Err(Error::Malformed("no strips or tiles")) };
|
||||
let (band_rows, across) = (first.h.max(1), l.width.div_ceil(first.w.max(1)));
|
||||
let mut data = vec![0u16; total];
|
||||
let band = |k: usize, out: &mut [u16]| -> Result<()> {
|
||||
let mine = chunks.get(k * across..((k + 1) * across).min(chunks.len())).unwrap_or(&[]);
|
||||
#[cfg(feature = "threads")]
|
||||
let decoded: Vec<Result<Vec<u16>>> = mine.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect();
|
||||
#[cfg(not(feature = "threads"))]
|
||||
let decoded: Vec<Result<Vec<u16>>> = mine.iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect();
|
||||
for (c, d) in mine.iter().zip(decoded) {
|
||||
let d = d?;
|
||||
let cw = c.w.min(l.width.saturating_sub(c.x));
|
||||
let ch = c.h.min(l.height.saturating_sub(c.y));
|
||||
for r in 0..ch {
|
||||
let src = d.get(r * c.w * spp..(r * c.w + cw) * spp).ok_or(Error::Truncated)?;
|
||||
let at = r * row_len + c.x * spp;
|
||||
out.get_mut(at..at + cw * spp).ok_or(Error::Truncated)?.copy_from_slice(src);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
#[cfg(feature = "threads")]
|
||||
data.par_chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?;
|
||||
#[cfg(not(feature = "threads"))]
|
||||
data.chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?;
|
||||
Ok(raw(data))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user