mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-28 01:09:49 +01:00
* Updates: take craft-update from craft-libs main at 6e03f65bf craft-update moves from storytold/craft-libs at 1b34eba41 to 6e03f65bf, craft-libs main with the update fixes from #763 and #803. Cargo.lock records the new revision. The other craft-libs pins are unchanged. * Updates: take craft-update from craft-libs main at e98299546 craft-libs e98299546 includes #923: the check of whether the copy can install itself now runs on the update-check thread instead of in Updater::tick on the UI thread. * Dependencies: take craft-libs main at 3af2280ee3 Every craft-libs crate in the workspace moves to craft-libs main at 3af2280ee3, so the app builds against one craft-libs commit. - crates/codecs: the separate craft-avif-encode alias of craft-image-codecs is removed. With both on one commit, cargo refuses the same package under two names. On native targets the avif feature now enables craft-image-codecs' avif and threads features, so a build without avif, and the wasm32 build, still leave the AV1 encoder out. - crates/ui-egui: GroupLocation has two new fields, flyout_width and flyout_offset (storytold/craft-libs#627); the default dock location sets both to None, as craft-ui does for a new group. * Dependencies: take craft-libs main at 3af718585c Every craft-libs crate moves from 3af2280ee3 to craft-libs main at 3af718585c. craft-image-codecs now depends on the new craft-dds crate. * Flatpak: regenerate cargo sources for craft-libs at 3af718585c cargo-sources.json now lists the craft-libs git sources that Cargo.lock locks: one commit, 3af718585c, for every craft-libs crate, and the new craft-dds crate. It was generated with ./packaging/linux/flatpak/generate-cargo-sources.py. * Dependencies: take craft-libs main at 453b0da782 Every craft-libs crate moves from 3af718585c to craft-libs main at 453b0da782. Cargo.lock changes only the craft-libs source lines, and packaging/linux/flatpak/cargo-sources.json is regenerated with generate-cargo-sources.py for the new commit. craft-update now reads the installed files for a release the app offers on a thread of its own, and the prompt opens at a later tick (storytold/craft-libs#1149). startup_prompt_shows_and_closes offered a release and checked the prompt after one frame, so it failed when that thread had not answered yet. The test now waits for the thread's repaint request and ticks until the prompt opens. * Updates: startup prompt test fails instead of waiting forever The offer's thread sends its answer before it asks for a repaint, so the tick after that repaint reads the answer. The helper now ticks once after the repaint and asserts that the prompt is open, instead of waiting for another repaint that never comes when the answer opens no prompt. The test also gives its updater a pins file that does not exist, so a launcher choice on the machine running the tests does not stop the offer.
469 lines
21 KiB
Rust
469 lines
21 KiB
Rust
//! Encoders: JPEG (`craft-jpeg`), PNG (`png`), TIFF (`tiff`), flat PSD (our own), lossless WebP (`image-webp`) and,
|
||
//! on native targets with the `avif` feature, AVIF (`ravif`/`rav1e`); JPEG XL is in `jxl_encode`.
|
||
//! Metadata (ICC/EXIF/XMP) is embedded where the format allows.
|
||
|
||
use crate::{Error, Result};
|
||
use lightcraft_raster::Rgba8;
|
||
use serde::{Deserialize, Serialize};
|
||
|
||
/// Interleaved sample data. Integer samples are display-encoded (0..=MAX); float samples are
|
||
/// written as-is.
|
||
#[derive(Clone, Copy, Debug)]
|
||
pub enum Samples<'a> {
|
||
U8(&'a [u8]),
|
||
U16(&'a [u16]),
|
||
F32(&'a [f32]),
|
||
}
|
||
|
||
/// An image to encode: `channels` = 1 (gray), 2 (gray + alpha), 3 (RGB) or 4 (RGBA), straight alpha.
|
||
#[derive(Clone, Copy, Debug)]
|
||
pub struct EncodeImage<'a> {
|
||
pub width: u32,
|
||
pub height: u32,
|
||
pub channels: u8,
|
||
pub samples: Samples<'a>,
|
||
}
|
||
|
||
impl<'a> EncodeImage<'a> {
|
||
pub fn rgba8(img: &'a Rgba8) -> Self {
|
||
EncodeImage { width: img.width as u32, height: img.height as u32, channels: 4, samples: Samples::U8(bytemuck::cast_slice(&img.data)) }
|
||
}
|
||
pub fn new(width: u32, height: u32, channels: u8, samples: Samples<'a>) -> Self {
|
||
EncodeImage { width, height, channels, samples }
|
||
}
|
||
|
||
pub(crate) fn validate(&self) -> Result<()> {
|
||
if !(1..=4).contains(&self.channels) {
|
||
return Err(Error::Encode(format!("unsupported channel count {}", self.channels)));
|
||
}
|
||
if self.width == 0 || self.height == 0 {
|
||
return Err(Error::Encode("zero dimension".into()));
|
||
}
|
||
// u128: 2^32 x 2^32 x 4 does not fit u64, and every later `width * height * channels` is bounded by this
|
||
let need = self.width as u128 * self.height as u128 * self.channels as u128;
|
||
if need > usize::MAX as u128 {
|
||
return Err(Error::Encode("image too large".into()));
|
||
}
|
||
let have = match self.samples {
|
||
Samples::U8(s) => s.len(),
|
||
Samples::U16(s) => s.len(),
|
||
Samples::F32(s) => s.len(),
|
||
} as u128;
|
||
if have < need {
|
||
return Err(Error::Encode(format!("sample buffer too short: {have} < {need}")));
|
||
}
|
||
Ok(())
|
||
}
|
||
}
|
||
|
||
/// Metadata to embed.
|
||
#[derive(Clone, Copy, Debug, Default)]
|
||
pub struct EncodeMeta<'a> {
|
||
pub icc: Option<&'a [u8]>,
|
||
/// TIFF-structured EXIF (starting at `II`/`MM`, no `Exif\0\0` prefix).
|
||
pub exif: Option<&'a [u8]>,
|
||
pub xmp: Option<&'a str>,
|
||
/// Print resolution in pixels per inch (JPEG JFIF density, PNG `pHYs`, TIFF X/YResolution).
|
||
pub ppi: Option<u16>,
|
||
}
|
||
|
||
/// JPEG chroma subsampling.
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||
pub enum ChromaSubsampling {
|
||
/// No subsampling (best quality).
|
||
#[default]
|
||
S444,
|
||
S422,
|
||
S420,
|
||
}
|
||
|
||
const XMP_NS: &[u8] = b"http://ns.adobe.com/xap/1.0/\0";
|
||
|
||
/// The APP segments [`encode_jpeg`] writes for `meta` (marker byte, payload): JFIF density, EXIF,
|
||
/// XMP and the ICC profile in chunks, in that order.
|
||
pub(crate) fn jpeg_app_segments(meta: &EncodeMeta) -> Result<Vec<(u8, Vec<u8>)>> {
|
||
let mut segs: Vec<(u8, Vec<u8>)> = Vec::new();
|
||
let ppi = meta.ppi.filter(|p| *p > 0).map_or([0u8, 0, 1, 0, 1], |p| {
|
||
let [hi, lo] = p.to_be_bytes();
|
||
[1, hi, lo, hi, lo]
|
||
});
|
||
segs.push((0xE0, [&b"JFIF\0\x01\x02"[..], &ppi, &[0, 0]].concat()));
|
||
if let Some(exif) = meta.exif {
|
||
if exif.len() > 65_527 {
|
||
return Err(Error::Encode("EXIF larger than one APP1 segment (64 KiB)".into()));
|
||
}
|
||
segs.push((0xE1, [&b"Exif\0\0"[..], exif].concat()));
|
||
}
|
||
if let Some(xmp) = meta.xmp {
|
||
segs.push((0xE1, xmp_segment(xmp)?));
|
||
}
|
||
if let Some(icc) = meta.icc {
|
||
// ICC.1 Annex B.4: "ICC_PROFILE\0", 1-based chunk number, chunk count
|
||
let chunks: Vec<&[u8]> = icc.chunks(65_519).collect();
|
||
if chunks.len() > 255 {
|
||
return Err(Error::Encode("ICC profile too large for JPEG".into()));
|
||
}
|
||
for (i, c) in chunks.iter().enumerate() {
|
||
segs.push((0xE2, [&b"ICC_PROFILE\0"[..], &[i as u8 + 1, chunks.len() as u8], c].concat()));
|
||
}
|
||
}
|
||
Ok(segs)
|
||
}
|
||
|
||
/// An APP1 XMP payload (namespace + packet), refusing packets larger than one segment.
|
||
pub(crate) fn xmp_segment(xmp: &str) -> Result<Vec<u8>> {
|
||
let seg = [XMP_NS, xmp.as_bytes()].concat();
|
||
if seg.len() > 65_533 {
|
||
return Err(Error::Encode("XMP larger than one APP1 segment (extended XMP not supported)".into()));
|
||
}
|
||
Ok(seg)
|
||
}
|
||
|
||
/// Encode baseline JPEG (8-bit; alpha is dropped). `quality` 1..=100.
|
||
pub fn encode_jpeg(img: &EncodeImage, quality: u8, subsampling: ChromaSubsampling, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
img.validate()?;
|
||
let Samples::U8(data) = img.samples else {
|
||
return Err(Error::Encode("JPEG needs 8-bit samples".into()));
|
||
};
|
||
if img.width > u16::MAX as u32 || img.height > u16::MAX as u32 {
|
||
return Err(Error::Encode("JPEG dimensions are limited to 65535".into()));
|
||
}
|
||
let pixels = img.width as usize * img.height as usize;
|
||
let (data, channels): (std::borrow::Cow<'_, [u8]>, usize) = match img.channels {
|
||
1 => (data[..pixels].into(), 1),
|
||
2 => (data.as_chunks::<2>().0.iter().take(pixels).map(|c| c[0]).collect::<Vec<_>>().into(), 1),
|
||
3 => (data[..pixels * 3].into(), 3),
|
||
_ => (data[..pixels * 4].into(), 4),
|
||
};
|
||
let subsampling = match subsampling {
|
||
ChromaSubsampling::S444 => craft_jpeg::ChromaSubsampling::S444,
|
||
ChromaSubsampling::S422 => craft_jpeg::ChromaSubsampling::S422,
|
||
ChromaSubsampling::S420 => craft_jpeg::ChromaSubsampling::S420,
|
||
};
|
||
craft_jpeg::encode_baseline(&data, img.width as usize, img.height as usize, channels, quality, subsampling, &jpeg_app_segments(meta)?)
|
||
.map_err(|e| Error::Encode(e.to_string()))
|
||
}
|
||
|
||
/// `img` (already validated) as a `craft-image-codecs` image. `refuse_float` is the error for
|
||
/// 32-bit float samples, for a format that cannot hold them; `None` converts them too.
|
||
fn shared_image(img: &EncodeImage, refuse_float: Option<&str>) -> Result<craft_image_codecs::Image> {
|
||
let n = img.width as usize * img.height as usize * img.channels as usize;
|
||
let layout = match img.channels {
|
||
1 => craft_image_codecs::ChannelLayout::Gray,
|
||
2 => craft_image_codecs::ChannelLayout::GrayA,
|
||
3 => craft_image_codecs::ChannelLayout::Rgb,
|
||
_ => craft_image_codecs::ChannelLayout::Rgba,
|
||
};
|
||
match (img.samples, refuse_float) {
|
||
(Samples::U8(s), _) => craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()),
|
||
(Samples::U16(s), _) => craft_image_codecs::Image::from_u16(img.width, img.height, layout, &s[..n]),
|
||
(Samples::F32(_), Some(refusal)) => return Err(Error::Encode(refusal.into())),
|
||
(Samples::F32(s), None) => craft_image_codecs::Image::from_f32(img.width, img.height, layout, &s[..n]),
|
||
}
|
||
.map_err(|e| Error::Encode(e.to_string()))
|
||
}
|
||
|
||
/// Encode PNG (8- or 16-bit; float input is rejected — quantize first).
|
||
pub fn encode_png(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
img.validate()?;
|
||
let mut ic_img = shared_image(img, Some("PNG needs 8- or 16-bit samples"))?;
|
||
ic_img.icc = meta.icc.map(|b| b.to_vec());
|
||
ic_img.meta.exif = meta.exif.map(|b| b.to_vec());
|
||
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
|
||
ic_img.meta.dpi = meta.ppi.filter(|p| *p > 0).map(|p| (p as f32, p as f32));
|
||
let ic_opts = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() };
|
||
let mut out = craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::Png, &ic_opts).map_err(|e| Error::Encode(e.to_string()))?;
|
||
if let Some(exif) = meta.exif {
|
||
replace_png_exif_chunk(&mut out, exif);
|
||
}
|
||
Ok(out)
|
||
}
|
||
|
||
/// Replace the `eXIf` chunk in an encoded PNG so caller-supplied EXIF (including non-1
|
||
/// orientation tags) is preserved verbatim.
|
||
fn replace_png_exif_chunk(png: &mut Vec<u8>, exif: &[u8]) {
|
||
let mut p = 8usize;
|
||
while let Some(len_bytes) = png.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 end = p.saturating_add(12).saturating_add(len);
|
||
if end > png.len() {
|
||
break;
|
||
}
|
||
if png.get(p + 4..p + 8) == Some(b"eXIf") {
|
||
png.splice(p..end, png_chunk(*b"eXIf", exif));
|
||
return;
|
||
}
|
||
p = end;
|
||
}
|
||
}
|
||
|
||
/// TIFF compression.
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||
pub enum TiffCompression {
|
||
None,
|
||
Lzw,
|
||
#[default]
|
||
Deflate,
|
||
PackBits,
|
||
}
|
||
|
||
/// Encode TIFF: 8/16-bit integer or 32-bit float, gray/RGB with optional (unassociated) alpha.
|
||
/// ICC and XMP are embedded; EXIF is not (write it through the TIFF writer in `craft-tiff`).
|
||
pub fn encode_tiff(img: &EncodeImage, compression: TiffCompression, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
img.validate()?;
|
||
let mut ic_img = shared_image(img, None)?;
|
||
ic_img.icc = meta.icc.map(|b| b.to_vec());
|
||
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
|
||
ic_img.meta.dpi = meta.ppi.filter(|p| *p > 0).map(|p| (p as f32, p as f32));
|
||
let tiff_compression = match compression {
|
||
TiffCompression::None => craft_image_codecs::TiffCompression::None,
|
||
TiffCompression::Lzw => craft_image_codecs::TiffCompression::Lzw,
|
||
TiffCompression::Deflate => craft_image_codecs::TiffCompression::Deflate,
|
||
TiffCompression::PackBits => craft_image_codecs::TiffCompression::PackBits,
|
||
};
|
||
let ic_opts = craft_image_codecs::EncodeOptions { tiff_compression, ..Default::default() };
|
||
craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::Tiff, &ic_opts).map_err(|e| Error::Encode(e.to_string()))
|
||
}
|
||
|
||
/// Encode a flat PSD (8/16-bit gray or RGB, no layers; alpha dropped): the hand-off an image
|
||
/// editor such as PhotoCraft saves back to in place. ICC, EXIF, XMP and resolution are embedded.
|
||
pub fn encode_psd(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
crate::psd::encode(img, meta)
|
||
}
|
||
|
||
/// Encode lossless WebP (8-bit). ICC/EXIF/XMP are embedded in the extended (VP8X) container.
|
||
pub fn encode_webp_lossless(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
img.validate()?;
|
||
let Samples::U8(s) = img.samples else {
|
||
return Err(Error::Encode("WebP needs 8-bit samples".into()));
|
||
};
|
||
let n = img.width as usize * img.height as usize * img.channels as usize;
|
||
let layout = match img.channels {
|
||
1 => craft_image_codecs::ChannelLayout::Gray,
|
||
2 => craft_image_codecs::ChannelLayout::GrayA,
|
||
3 => craft_image_codecs::ChannelLayout::Rgb,
|
||
_ => craft_image_codecs::ChannelLayout::Rgba,
|
||
};
|
||
let mut ic_img = craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()).map_err(|e| Error::Encode(e.to_string()))?;
|
||
ic_img.icc = meta.icc.map(|b| b.to_vec());
|
||
ic_img.meta.exif = meta.exif.map(|b| b.to_vec());
|
||
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
|
||
let ic_opts = craft_image_codecs::EncodeOptions { webp_lossless: true, ..Default::default() };
|
||
let mut out = craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::WebP, &ic_opts).map_err(|e| Error::Encode(e.to_string()))?;
|
||
if let Some(exif) = meta.exif {
|
||
replace_webp_exif_chunk(&mut out, exif);
|
||
}
|
||
Ok(out)
|
||
}
|
||
|
||
/// Replace the `EXIF` RIFF chunk payload in a WebP file so caller-supplied EXIF (including
|
||
/// non-1 orientation tags) is preserved verbatim.
|
||
fn replace_webp_exif_chunk(webp: &mut Vec<u8>, exif: &[u8]) {
|
||
let mut p = 12usize;
|
||
while let Some(h) = webp.get(p..p.saturating_add(8)) {
|
||
let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize;
|
||
let end = p.saturating_add(8).saturating_add(size);
|
||
let padded_end = end.saturating_add(size & 1);
|
||
if &h[0..4] == b"EXIF" && padded_end <= webp.len() {
|
||
let mut replacement = Vec::with_capacity(8 + exif.len() + (exif.len() & 1));
|
||
replacement.extend_from_slice(b"EXIF");
|
||
replacement.extend_from_slice(&(exif.len() as u32).to_le_bytes());
|
||
replacement.extend_from_slice(exif);
|
||
if exif.len() & 1 == 1 {
|
||
replacement.push(0);
|
||
}
|
||
webp.splice(p..padded_end, replacement);
|
||
let riff_size = (webp.len().saturating_sub(8)) as u32;
|
||
webp[4..8].copy_from_slice(&riff_size.to_le_bytes());
|
||
return;
|
||
}
|
||
p = padded_end;
|
||
}
|
||
}
|
||
|
||
/// Encode AVIF (sRGB, 4:4:4, 10-bit AV1) with `ravif` (rav1e). 8-bit samples are widened by the
|
||
/// encoder; 16-bit samples keep 10 bits of precision (BT.601 full-range YCbCr computed here).
|
||
/// `quality` 1..=100, `speed` 1 (slow) ..= 10. EXIF is embedded; ICC is not supported by the muxer
|
||
/// (the file is tagged sRGB via `nclx`). Returns [`Error::Encode`] for float samples, on wasm32 or
|
||
/// when built without the `avif` feature.
|
||
pub fn encode_avif(img: &EncodeImage, quality: u8, speed: u8, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
img.validate()?;
|
||
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
|
||
{
|
||
let mut ic_img = shared_image(img, Some("AVIF encoder needs 8- or 16-bit samples"))?;
|
||
ic_img.meta.exif = meta.exif.map(|e| e.to_vec());
|
||
craft_image_codecs::encode_avif_with_speed(&ic_img, quality, speed).map_err(|e| Error::Encode(e.to_string()))
|
||
}
|
||
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
|
||
{
|
||
let _ = (quality, speed, meta);
|
||
Err(Error::Encode("AVIF encoding is not available in this build".into()))
|
||
}
|
||
}
|
||
|
||
/// Absolute luminance of SDR white in an HDR file (ITU-R BT.2408 reference white), cd/m².
|
||
pub const HDR_REFERENCE_WHITE_NITS: f32 = 203.0;
|
||
|
||
/// SMPTE ST 2084 (PQ) inverse EOTF: luminance in cd/m² → signal 0..1.
|
||
pub fn pq_encode(nits: f32) -> f32 {
|
||
const M1: f32 = 2610.0 / 16384.0;
|
||
const M2: f32 = 2523.0 / 4096.0 * 128.0;
|
||
const C1: f32 = 3424.0 / 4096.0;
|
||
const C2: f32 = 2413.0 / 4096.0 * 32.0;
|
||
const C3: f32 = 2392.0 / 4096.0 * 32.0;
|
||
let y = (nits / 10_000.0).clamp(0.0, 1.0).powf(M1);
|
||
((C1 + C2 * y) / (1.0 + C3 * y)).powf(M2)
|
||
}
|
||
|
||
/// SMPTE ST 2084 (PQ) EOTF: signal 0..1 → luminance in cd/m².
|
||
pub fn pq_decode(e: f32) -> f32 {
|
||
const M1: f32 = 2610.0 / 16384.0;
|
||
const M2: f32 = 2523.0 / 4096.0 * 128.0;
|
||
const C1: f32 = 3424.0 / 4096.0;
|
||
const C2: f32 = 2413.0 / 4096.0 * 32.0;
|
||
const C3: f32 = 2392.0 / 4096.0 * 32.0;
|
||
let p = e.clamp(0.0, 1.0).powf(1.0 / M2);
|
||
((p - C1).max(0.0) / (C2 - C3 * p)).powf(1.0 / M1) * 10_000.0
|
||
}
|
||
|
||
/// Transfer curve of an HDR file: SMPTE ST 2084 (PQ, absolute) or ITU-R BT.2100 HLG (relative).
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||
pub enum HdrTransfer {
|
||
#[default]
|
||
Pq,
|
||
Hlg,
|
||
}
|
||
|
||
/// Nominal peak of the HLG reference display, cd/m² (ITU-R BT.2100 / BT.2408).
|
||
pub const HLG_REFERENCE_PEAK_NITS: f32 = 1000.0;
|
||
|
||
/// Display-referred linear RGB (SDR white = 1.0, Rec. 2020) → HLG signal 0..1, per BT.2100:
|
||
/// display light normalised to the 1000 cd/m² reference display, the inverse OOTF (system gamma
|
||
/// 1.2, applied on luminance so hue is kept), then the HLG OETF. SDR white lands at signal 0.75,
|
||
/// as BT.2408 asks. Non-finite or negative inputs count as 0.
|
||
pub fn hlg_signal(rgb: [f32; 3]) -> [f32; 3] {
|
||
const K: [f32; 3] = [0.2627, 0.6780, 0.0593];
|
||
const GAMMA: f32 = 1.2;
|
||
let fd = rgb.map(|v| (if v.is_finite() { v.max(0.0) } else { 0.0 }) * (HDR_REFERENCE_WHITE_NITS / HLG_REFERENCE_PEAK_NITS));
|
||
let yd = (K[0] * fd[0] + K[1] * fd[1] + K[2] * fd[2]).min(1.0);
|
||
let scale = if yd > 0.0 { yd.powf((1.0 - GAMMA) / GAMMA) } else { 0.0 };
|
||
fd.map(|v| lightcraft_color::transfer::hlg_encode(f64::from((v * scale).clamp(0.0, 1.0))).clamp(0.0, 1.0) as f32)
|
||
}
|
||
|
||
/// Encode an HDR AVIF with the PQ curve (see [`encode_avif_hdr`]).
|
||
pub fn encode_avif_pq(width: u32, height: u32, rgb: &[f32], quality: u8, speed: u8, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
encode_avif_hdr(width, height, rgb, quality, speed, meta, HdrTransfer::Pq)
|
||
}
|
||
|
||
/// Encode an HDR AVIF: `rgb` is linear light in **Rec. 2020** primaries with SDR white at 1.0
|
||
/// (`width × height × 3`). Written as 10-bit 4:4:4 BT.2020 non-constant-luminance YCbCr with the
|
||
/// `transfer` curve: PQ with SDR white at [`HDR_REFERENCE_WHITE_NITS`] and a content light level
|
||
/// box, or HLG ([`hlg_signal`], no light level box: HLG is relative to the display).
|
||
/// `quality` 1..=100, `speed` 1..=10. Native targets with the `avif` feature only.
|
||
pub fn encode_avif_hdr(width: u32, height: u32, rgb: &[f32], quality: u8, speed: u8, meta: &EncodeMeta, transfer: HdrTransfer) -> Result<Vec<u8>> {
|
||
let (w, h) = (width as usize, height as usize);
|
||
let n = w.checked_mul(h).ok_or_else(|| Error::Encode("image too large".into()))?;
|
||
if n == 0 || n.checked_mul(3).is_none_or(|need| rgb.len() < need) {
|
||
return Err(Error::Encode("HDR AVIF: sample buffer too short".into()));
|
||
}
|
||
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
|
||
{
|
||
let _ = (w, h, n);
|
||
let t = match transfer {
|
||
HdrTransfer::Pq => craft_image_codecs::HdrTransfer::Pq,
|
||
HdrTransfer::Hlg => craft_image_codecs::HdrTransfer::Hlg,
|
||
};
|
||
craft_image_codecs::encode_avif_hdr(width, height, rgb, quality, speed, meta.exif, t).map_err(|e| Error::Encode(e.to_string()))
|
||
}
|
||
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
|
||
{
|
||
let _ = (quality, speed, meta, transfer);
|
||
Err(Error::Encode("AVIF encoding is not available in this build".into()))
|
||
}
|
||
}
|
||
|
||
/// Encode an HDR PNG: `rgb` is linear light in **Rec. 2020** primaries with SDR white at 1.0
|
||
/// (`width × height × 3`), as for [`encode_avif_pq`]. Written as 16-bit RGB with the PQ curve
|
||
/// (SDR white at [`HDR_REFERENCE_WHITE_NITS`]), described by a `cICP` chunk (PNG third edition:
|
||
/// BT.2020 primaries, PQ, RGB, full range) and a `cLLI` content light level chunk (the same
|
||
/// levels the HDR AVIF records). EXIF and XMP are embedded; an ICC profile is not (`cICP`
|
||
/// describes the colour, and readers prefer it). Readers without `cICP` support show the PQ
|
||
/// samples as if they were sRGB (flat, washed out): expected for a PQ PNG, there is no fallback.
|
||
pub fn encode_png_pq(width: u32, height: u32, rgb: &[f32], meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||
let (w, h) = (width as usize, height as usize);
|
||
let n = w.checked_mul(h).ok_or_else(|| Error::Encode("image too large".into()))?;
|
||
let samples = n.checked_mul(3).ok_or_else(|| Error::Encode("image too large".into()))?;
|
||
if n == 0 || rgb.len() < samples {
|
||
return Err(Error::Encode("HDR PNG: sample buffer too short".into()));
|
||
}
|
||
let mut image = craft_image_codecs::Image::from_f32(
|
||
width,
|
||
height,
|
||
craft_image_codecs::ChannelLayout::Rgb,
|
||
rgb.get(..samples).ok_or_else(|| Error::Encode("HDR PNG: sample buffer too short".into()))?,
|
||
)
|
||
.map_err(|e| Error::Encode(e.to_string()))?;
|
||
image.meta.exif = meta.exif.map(ToOwned::to_owned);
|
||
image.meta.xmp = meta.xmp.map(ToOwned::to_owned);
|
||
image.meta.dpi = meta.ppi.filter(|&ppi| ppi > 0).map(|ppi| (f32::from(ppi), f32::from(ppi)));
|
||
let options = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() };
|
||
craft_image_codecs::encode_png_pq(&image, &options).map_err(|e| Error::Encode(e.to_string()))
|
||
}
|
||
|
||
fn png_chunk(kind: [u8; 4], data: &[u8]) -> Vec<u8> {
|
||
let mut out = Vec::with_capacity(12 + data.len());
|
||
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
|
||
out.extend_from_slice(&kind);
|
||
out.extend_from_slice(data);
|
||
let mut crc = 0xFFFF_FFFFu32;
|
||
for &b in &out[4..] {
|
||
crc ^= u32::from(b);
|
||
for _ in 0..8 {
|
||
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
|
||
}
|
||
}
|
||
out.extend_from_slice(&(!crc).to_be_bytes());
|
||
out
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod hlg_tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn dimensions_whose_sample_count_wraps_are_refused() {
|
||
// 2^31 x 2^31 x 4 is 2^64 samples: zero when wrapped, which an empty buffer would satisfy
|
||
let img = EncodeImage::new(1 << 31, 1 << 31, 4, Samples::U8(&[]));
|
||
assert!(img.validate().is_err());
|
||
}
|
||
|
||
#[test]
|
||
fn hlg_reference_white_and_peak() {
|
||
// BT.2408: SDR (HDR reference) white sits at 75 % HLG signal
|
||
for v in hlg_signal([1.0; 3]) {
|
||
assert!((v - 0.75).abs() < 0.005, "{v}");
|
||
}
|
||
// the 1000 cd/m² peak (≈ 4.9× SDR white) reaches full signal; above it clips
|
||
let peak = HLG_REFERENCE_PEAK_NITS / HDR_REFERENCE_WHITE_NITS;
|
||
assert!(hlg_signal([peak; 3]).iter().all(|v| (v - 1.0).abs() < 1e-3));
|
||
assert_eq!(hlg_signal([100.0; 3]), [1.0; 3]);
|
||
// black, negatives and non-finite values are 0, never NaN
|
||
assert_eq!(hlg_signal([0.0, -1.0, f32::NAN]), [0.0; 3]);
|
||
assert!(hlg_signal([f32::INFINITY, 0.0, 0.0]).iter().all(|v| v.is_finite()));
|
||
// monotonic in brightness
|
||
let mut prev = 0.0;
|
||
for i in 1..50 {
|
||
let v = hlg_signal([i as f32 * 0.1; 3])[1];
|
||
assert!(v >= prev);
|
||
prev = v;
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn hdr_avif_rejects_short_buffers() {
|
||
let m = EncodeMeta::default();
|
||
assert!(encode_avif_hdr(4, 4, &[0.5; 10], 80, 10, &m, HdrTransfer::Hlg).is_err());
|
||
assert!(encode_avif_hdr(0, 4, &[], 80, 10, &m, HdrTransfer::Hlg).is_err());
|
||
}
|
||
}
|