use jxl_oxide::image::BitDepth;
use jxl_oxide::{AllocTracker, JxlImage};
use crate::{CodecError, MAX_UNSIZED_OUTPUT, Raster, Result};
fn err(detail: impl Into<String>) -> CodecError {
CodecError::Decode {
codec: "jpeg-xl",
detail: detail.into(),
}
}
pub(crate) fn decode(data: &[u8], max_bytes: usize) -> Result<Raster> {
let limit = max_bytes.min(MAX_UNSIZED_OUTPUT);
let image = JxlImage::builder()
.alloc_tracker(AllocTracker::with_limit(limit.saturating_mul(8)))
.read(std::io::Cursor::new(data))
.map_err(|e| err(e.to_string()))?;
let (w, h) = (image.width(), image.height());
let meta = &image.image_header().metadata;
let colour = if meta.grayscale() { 1u32 } else { 3 };
let channels = colour + u32::try_from(meta.ec_info.len()).unwrap_or(u32::MAX);
let (bits, float) = match meta.bit_depth {
BitDepth::IntegerSample { bits_per_sample } if bits_per_sample <= 8 => (8u32, false),
BitDepth::IntegerSample { bits_per_sample } if bits_per_sample <= 16 => (16, false),
BitDepth::FloatSample {
bits_per_sample, ..
} if bits_per_sample <= 32 => (32, true),
other => {
return Err(CodecError::Unsupported {
codec: "jpeg-xl",
detail: format!("{} bits per sample", other.bits_per_sample()),
});
}
};
let bytes = u64::from(w) * u64::from(h) * u64::from(channels) * u64::from(bits / 8);
if bytes > limit as u64 {
return Err(err(format!(
"header declares an implausible {w}x{h}x{channels} image"
)));
}
let render = image.render_frame(0).map_err(|e| err(e.to_string()))?;
let fb = render.image_all_channels();
if fb.width() != w as usize || fb.height() != h as usize || fb.channels() != channels as usize {
return Err(err(format!(
"rendered {}x{}x{} samples, header declares {w}x{h}x{channels}",
fb.width(),
fb.height(),
fb.channels()
)));
}
let samples = fb.buf();
let data: Vec<u8> = match (bits, float) {
(8, false) => samples
.iter()
.map(|&v| (v * 255.0 + 0.5).clamp(0.0, 255.0) as u8)
.collect(),
(16, false) => samples
.iter()
.flat_map(|&v| ((v * 65535.0 + 0.5).clamp(0.0, 65535.0) as u16).to_le_bytes())
.collect(),
_ => samples.iter().flat_map(|v| v.to_le_bytes()).collect(),
};
Ok(Raster {
width: w,
height: h,
channels,
bits_per_sample: bits,
float,
bgr: false,
data,
})
}