use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::coding::CodingTransform;
use crate::color::{
ColorTransform, FloatTransferDecoder, RgbBlockTransform, TransferDecoder, TransferLut,
};
use crate::color_encoding::{RenderingIntent, write_color_encoding_with_icc};
use crate::encoding_context::EncodingContext;
use crate::frame::encode_frame;
use crate::gain_map::{EncodedGainMap, GainMap, encode_gain_map};
use crate::image::{Image3F, Image3Si};
use crate::jpeg::BrotliCompression;
use crate::lossless::rct_simd::{forward_ycocg_in_place, rgb8_to_ycocg};
use crate::lossless::{encode_frame_lossless, encode_frame_lossless_float};
use crate::orientation::Orientation;
use crate::xyb::XybMatrix;
use crate::{ColorEncoding, EncodeError, Grain};
use std::num::NonZero;
use std::sync::Arc;
use std::thread::available_parallelism;
pub(crate) fn checked_buffer_size<T>(
width: usize,
height: usize,
channels: usize,
) -> Result<usize, EncodeError> {
let pixel_size = size_of::<T>();
let total_size = width
.checked_mul(height)
.and_then(|v| v.checked_mul(channels));
_ = total_size
.and_then(|v| v.checked_mul(pixel_size))
.and_then(|v| isize::try_from(v).ok())
.map(|v| v as usize)
.ok_or(EncodeError::DimensionTooLarge {
width: height,
height,
})?;
total_size
.and_then(|v| isize::try_from(v).ok())
.map(|v| v as usize)
.ok_or(EncodeError::DimensionTooLarge {
width: height,
height,
})
}
#[derive(Debug, Clone)]
pub(crate) enum AlphaPlane {
U8(Vec<u8>),
U16 { data: Vec<u16>, bits: u8 },
F32(Vec<i32>),
}
const CODESTREAM_MARKER: u8 = 0x0A;
pub(crate) const MIN_DISTANCE: f32 = 0.05;
pub(crate) const MAX_DIMENSION: usize = 0x3FFF_FFFF;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) enum BitsPerSample {
#[default]
Eight,
Ten,
Twelve,
Sixteen,
F16,
F32,
}
impl BitsPerSample {
pub(crate) fn bits(self) -> u32 {
match self {
BitsPerSample::Eight => 8,
BitsPerSample::Ten => 10,
BitsPerSample::Twelve => 12,
BitsPerSample::Sixteen => 16,
BitsPerSample::F16 => 16,
BitsPerSample::F32 => 32,
}
}
pub(crate) fn is_float(self) -> bool {
matches!(self, BitsPerSample::F16 | BitsPerSample::F32)
}
pub(crate) fn exp_bits(self) -> u32 {
match self {
BitsPerSample::F16 => 5,
BitsPerSample::F32 => 8,
_ => 0,
}
}
}
impl AlphaPlane {
#[inline]
pub(crate) fn from_u8(data: Vec<u8>) -> Self {
Self::U8(data)
}
#[inline]
pub(crate) fn from_u16_10bit(data: Vec<u16>) -> Self {
Self::U16 { data, bits: 10 }
}
#[inline]
pub(crate) fn from_u16_12bit(data: Vec<u16>) -> Self {
Self::U16 { data, bits: 12 }
}
#[inline]
pub(crate) fn from_u16_16bit(data: Vec<u16>) -> Self {
Self::U16 { data, bits: 16 }
}
#[inline]
pub(crate) fn len(&self) -> usize {
match self {
Self::U8(v) => v.len(),
Self::U16 { data, .. } => data.len(),
Self::F32(v) => v.len(),
}
}
pub(crate) fn is_constant(&self) -> bool {
fn same<T: PartialEq>(v: &[T]) -> bool {
v.split_first()
.is_none_or(|(first, rest)| rest.iter().all(|x| x == first))
}
match self {
Self::U8(v) => same(v),
Self::U16 { data, .. } => same(data),
Self::F32(v) => same(v),
}
}
#[inline]
pub(crate) fn bits(&self) -> u8 {
match self {
Self::U8(_) => 8,
Self::U16 { bits, .. } => *bits,
Self::F32(_) => 32,
}
}
#[inline]
pub(crate) fn is_float(&self) -> bool {
matches!(self, Self::F32(_))
}
#[inline]
pub(crate) fn get_i32(&self, idx: usize) -> i32 {
match self {
Self::U8(v) => v[idx] as i32,
Self::U16 { data, .. } => data[idx] as i32,
Self::F32(v) => v[idx],
}
}
pub(crate) fn to_i32_vec(&self, len: usize) -> Vec<i32> {
match self {
Self::U8(data) => data[..len].iter().map(|&v| i32::from(v)).collect(),
Self::U16 { data, .. } => data[..len].iter().map(|&v| i32::from(v)).collect(),
Self::F32(data) => data[..len].to_vec(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct ToneMappingParams {
pub intensity_target: Option<f32>,
pub min_nits: f32,
pub relative_to_max_display: bool,
pub linear_below: f32,
}
impl ToneMappingParams {
fn is_default(&self) -> bool {
self.intensity_target.is_none()
&& self.min_nits == 0.0
&& !self.relative_to_max_display
&& self.linear_below == 0.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[non_exhaustive]
pub enum Speed {
UltraFast,
Fastest,
#[default]
Fast,
Medium,
MediumSlow,
Slow,
ExtraSlow,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Arbiter {
#[default]
Ssimulacra2,
Cvvdp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DecodingSpeed {
Fastest,
Fast,
#[default]
Slow,
}
impl DecodingSpeed {
pub(crate) fn use_weighted_predictor(self) -> bool {
matches!(self, DecodingSpeed::Slow)
}
pub(crate) fn use_ma_trees(self) -> bool {
!matches!(self, DecodingSpeed::Fastest)
}
}
#[derive(Debug, Clone)]
pub struct EncodeConfig {
pub distance: f32,
pub color_encoding: ColorEncoding,
pub icc_profile: Option<Vec<u8>>,
pub exif: Option<Vec<u8>>,
pub xmp: Option<Vec<u8>>,
pub brotli_compression: Option<Arc<dyn BrotliCompression>>,
pub orientation: Orientation,
pub lossless: bool,
pub fill_invisible: bool,
pub alpha_distance: Option<f32>,
pub function_fields: bool,
pub function_fields_portable: bool,
pub progressive: bool,
pub patches: bool,
pub patch_epf: bool,
#[cfg(feature = "splines")]
pub splines: bool,
#[cfg(feature = "splines")]
pub edge_splines: bool,
#[cfg(feature = "splines")]
pub dots: bool,
pub progressive_passes: Option<u32>,
pub progressive_shifts: Option<Vec<u32>>,
pub intensity_target: Option<f32>,
pub min_nits: f32,
pub relative_to_max_display: bool,
pub linear_below: f32,
pub num_threads: usize,
pub speed: Speed,
pub arbiter: Arbiter,
pub decoding_speed: DecodingSpeed,
pub near_lossless: u16,
pub upsampling: bool,
pub lossy_modular: LossyModular,
pub modular_vertical_first: bool,
pub ink_layer: bool,
pub vector_layers: bool,
pub crop_frames: bool,
pub glyph_templates: bool,
pub progressive_dc: bool,
pub progressive_dc_levels: Option<u32>,
pub group_order: GroupOrder,
pub learned_rate: bool,
pub gain_map: Option<GainMap>,
pub coding_transform: CodingTransform,
pub grain: Grain,
pub(crate) templates_active: bool,
pub(crate) patches_lite: bool,
pub(crate) template_probe: Option<std::sync::Arc<std::sync::atomic::AtomicUsize>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GroupOrder {
#[default]
Scanline,
CenterFirst,
Diagonals,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LossyModular {
#[default]
Off,
Auto,
Force,
}
#[derive(Debug, Clone)]
pub(crate) struct EncodeConfigImpl {
pub(crate) distance: f32,
pub(crate) color_encoding: ColorEncoding,
pub(crate) icc_profile: Option<Vec<u8>>,
pub(crate) exif: Option<Vec<u8>>,
pub(crate) xmp: Option<Vec<u8>>,
pub(crate) brotli_compression: Option<Arc<dyn BrotliCompression>>,
pub(crate) orientation: Orientation,
pub(crate) alpha: Option<AlphaPlane>,
pub(crate) black: bool,
pub(crate) bits_per_sample: BitsPerSample,
pub(crate) lossless: bool,
pub(crate) fill_invisible: bool,
pub(crate) progressive: bool,
pub(crate) patches: bool,
#[cfg(feature = "splines")]
pub(crate) splines: bool,
#[cfg(feature = "splines")]
pub(crate) edge_splines: bool,
#[cfg(feature = "splines")]
pub(crate) dots: bool,
pub(crate) progressive_passes: Option<u32>,
pub(crate) progressive_shifts: Option<Vec<u32>>,
pub(crate) grayscale: bool,
pub(crate) intensity_target: Option<f32>,
pub(crate) min_nits: f32,
pub(crate) relative_to_max_display: bool,
pub(crate) linear_below: f32,
pub(crate) num_threads: usize,
pub(crate) speed: Speed,
pub(crate) decoding_speed: DecodingSpeed,
pub(crate) near_lossless: u16,
pub(crate) upsampling: bool,
pub(crate) gain_map: Option<EncodedGainMap>,
pub(crate) grain: Grain,
}
impl Default for EncodeConfig {
fn default() -> Self {
Self {
distance: 1.0,
color_encoding: ColorEncoding::default(),
icc_profile: None,
exif: None,
xmp: None,
brotli_compression: None,
orientation: Orientation::Normal,
lossless: false,
fill_invisible: true,
alpha_distance: None,
function_fields: true,
function_fields_portable: false,
progressive: false,
patches: false,
patch_epf: false,
upsampling: false,
#[cfg(feature = "splines")]
splines: false,
#[cfg(feature = "splines")]
edge_splines: false,
#[cfg(feature = "splines")]
dots: false,
progressive_passes: None,
progressive_shifts: None,
intensity_target: None,
min_nits: 0.0,
relative_to_max_display: false,
linear_below: 0.0,
num_threads: available_parallelism()
.unwrap_or(NonZero::new(1).unwrap())
.get(),
speed: Speed::Fast,
arbiter: Arbiter::Ssimulacra2,
decoding_speed: DecodingSpeed::Slow,
near_lossless: 0,
lossy_modular: LossyModular::Off,
modular_vertical_first: false,
ink_layer: false,
vector_layers: false,
crop_frames: false,
glyph_templates: false,
progressive_dc: false,
progressive_dc_levels: None,
group_order: GroupOrder::Scanline,
learned_rate: true,
gain_map: None,
coding_transform: CodingTransform::Xyb,
grain: Grain::Off,
templates_active: false,
patches_lite: false,
template_probe: None,
}
}
}
impl Default for EncodeConfigImpl {
fn default() -> Self {
Self {
distance: 1.0,
color_encoding: ColorEncoding::default(),
icc_profile: None,
exif: None,
xmp: None,
brotli_compression: None,
orientation: Orientation::Normal,
alpha: None,
black: false,
bits_per_sample: BitsPerSample::Eight,
lossless: false,
fill_invisible: true,
progressive: false,
patches: false,
upsampling: false,
#[cfg(feature = "splines")]
splines: false,
#[cfg(feature = "splines")]
edge_splines: false,
#[cfg(feature = "splines")]
dots: false,
grayscale: false,
progressive_passes: None,
progressive_shifts: None,
intensity_target: None,
min_nits: 0.0,
relative_to_max_display: false,
linear_below: 0.0,
num_threads: available_parallelism()
.unwrap_or(NonZero::new(1).unwrap())
.get(),
speed: Speed::Fast,
decoding_speed: DecodingSpeed::Slow,
near_lossless: 0,
gain_map: None,
grain: Grain::Off,
}
}
}
impl EncodeConfigImpl {
pub(crate) fn with_distance(distance: f32) -> Self {
Self {
distance,
..Self::default()
}
}
pub(crate) fn with_num_threads(mut self, n: usize) -> Self {
self.num_threads = n;
self
}
pub(crate) fn with_icc_profile(mut self, icc: Option<Vec<u8>>) -> Self {
self.icc_profile = icc;
self
}
pub(crate) fn with_exif(mut self, exif: Option<Vec<u8>>) -> Self {
self.exif = exif;
self
}
pub(crate) fn with_xmp(mut self, xmp: Option<Vec<u8>>) -> Self {
self.xmp = xmp;
self
}
pub(crate) fn with_brotli_compression(
mut self,
compressor: Option<Arc<dyn BrotliCompression>>,
) -> Self {
self.brotli_compression = compressor;
self
}
pub(crate) fn with_orientation(mut self, orientation: Orientation) -> Self {
self.orientation = orientation;
self
}
pub(crate) fn with_alpha(mut self, alpha: AlphaPlane) -> Self {
self.alpha = Some(alpha);
self
}
pub(crate) fn with_black(mut self, black: bool) -> Self {
self.black = black;
self
}
pub(crate) fn with_bits_per_sample(mut self, bps: BitsPerSample) -> Self {
self.bits_per_sample = bps;
self
}
pub(crate) fn with_lossless(mut self, lossless: bool) -> Self {
self.lossless = lossless;
self
}
pub(crate) fn with_fill_invisible(mut self, fill_invisible: bool) -> Self {
self.fill_invisible = fill_invisible;
self
}
pub(crate) fn with_progressive(mut self, progressive: bool) -> Self {
self.progressive = progressive;
self
}
pub(crate) fn with_patches(mut self, patches: bool) -> Self {
self.patches = patches;
self
}
#[cfg(feature = "splines")]
pub(crate) fn with_splines(mut self, splines: bool) -> Self {
self.splines = splines;
self
}
#[cfg(feature = "splines")]
pub(crate) fn with_edge_splines(mut self, edge_splines: bool) -> Self {
self.edge_splines = edge_splines;
self
}
#[cfg(feature = "splines")]
pub(crate) fn with_dots(mut self, dots: bool) -> Self {
self.dots = dots;
self
}
pub(crate) fn with_progressive_passes(mut self, passes: Option<u32>) -> Self {
self.progressive_passes = passes;
self
}
pub(crate) fn with_progressive_shifts(mut self, shifts: Option<Vec<u32>>) -> Self {
self.progressive_shifts = shifts;
self
}
pub(crate) fn with_grain(mut self, grain: Grain) -> Self {
self.grain = grain;
self
}
pub(crate) fn with_progressive_from(self, config: &EncodeConfig) -> Self {
let lossless = self.lossless;
#[cfg(feature = "splines")]
let this = self
.with_splines(config.splines)
.with_edge_splines(config.edge_splines)
.with_dots(config.dots);
#[cfg(not(feature = "splines"))]
let this = self;
this.with_progressive(config.progressive)
.with_grain(config.grain)
.with_fill_invisible(config.fill_invisible)
.with_patches(config.patches_in(lossless))
.with_progressive_passes(config.progressive_passes)
.with_progressive_shifts(config.progressive_shifts.clone())
.with_speed(config.speed)
.with_decoding_speed(config.decoding_speed)
}
pub(crate) fn with_speed(mut self, speed: Speed) -> Self {
self.speed = speed;
self
}
pub(crate) fn with_decoding_speed(mut self, decoding_speed: DecodingSpeed) -> Self {
self.decoding_speed = decoding_speed;
self
}
pub(crate) fn with_near_lossless(mut self, max_error: u16) -> Self {
self.near_lossless = max_error;
self
}
pub(crate) fn with_upsampling(mut self, upsampling: bool) -> Self {
self.upsampling = upsampling;
self
}
pub(crate) fn with_grayscale(mut self, grayscale: bool) -> Self {
self.grayscale = grayscale;
self
}
pub(crate) fn with_color_encoding(mut self, enc: ColorEncoding) -> Self {
self.color_encoding = enc;
self
}
pub(crate) fn tone_mapping(&self) -> ToneMappingParams {
ToneMappingParams {
intensity_target: self.intensity_target,
min_nits: self.min_nits,
relative_to_max_display: self.relative_to_max_display,
linear_below: self.linear_below,
}
}
pub(crate) fn with_intensity_target(mut self, nits: Option<f32>) -> Self {
self.intensity_target = nits;
self
}
pub(crate) fn with_gain_map(mut self, gain_map: Option<EncodedGainMap>) -> Self {
self.gain_map = gain_map;
self
}
}
impl EncodeConfig {
pub fn with_distance(mut self, distance: f32) -> Self {
self.distance = distance;
self
}
pub fn with_quality(self, quality: f32) -> Self {
self.with_distance(distance_from_quality(quality))
}
pub fn with_color_encoding(mut self, enc: ColorEncoding) -> Self {
self.color_encoding = enc;
self
}
pub fn with_intensity_target(mut self, nits: f32) -> Self {
self.intensity_target = Some(nits);
self
}
pub fn with_min_nits(mut self, nits: f32) -> Self {
self.min_nits = nits;
self
}
pub fn with_relative_to_max_display(mut self, rel: bool) -> Self {
self.relative_to_max_display = rel;
self
}
pub fn with_linear_below(mut self, v: f32) -> Self {
self.linear_below = v;
self
}
pub(crate) fn xyb_layers_decodable(&self) -> bool {
self.icc_profile.is_none()
}
pub(crate) fn tone_mapping(&self) -> ToneMappingParams {
ToneMappingParams {
intensity_target: self.intensity_target,
min_nits: self.min_nits,
relative_to_max_display: self.relative_to_max_display,
linear_below: self.linear_below,
}
}
pub fn with_icc_profile(mut self, icc: Vec<u8>) -> Self {
self.icc_profile = Some(icc);
self
}
pub fn with_exif(mut self, exif: Vec<u8>) -> Self {
self.exif = Some(exif);
self
}
pub fn with_xmp(mut self, xmp: Vec<u8>) -> Self {
self.xmp = Some(xmp);
self
}
pub fn with_brotli_compression(mut self, compressor: Box<dyn BrotliCompression>) -> Self {
self.brotli_compression = Some(Arc::from(compressor));
self
}
pub fn with_orientation(mut self, orientation: Orientation) -> Self {
self.orientation = orientation;
self
}
pub fn with_lossless(mut self, lossless: bool) -> Self {
self.lossless = lossless;
self
}
pub fn with_fill_invisible(mut self, fill_invisible: bool) -> Self {
self.fill_invisible = fill_invisible;
self
}
pub fn with_alpha_distance(mut self, alpha_distance: f32) -> Self {
self.alpha_distance = Some(alpha_distance);
self
}
pub fn with_function_fields(mut self, enabled: bool) -> Self {
self.function_fields = enabled;
self
}
pub fn with_function_fields_portable(mut self, enabled: bool) -> Self {
self.function_fields_portable = enabled;
self
}
pub fn with_progressive(mut self, progressive: bool) -> Self {
self.progressive = progressive;
self
}
pub fn with_patches(mut self, patches: bool) -> Self {
self.patches = patches;
self.patches_lite = false;
self
}
pub(crate) fn patches_in(&self, lossless: bool) -> bool {
self.patches && !(lossless && self.patches_lite)
}
pub fn with_patch_epf(mut self, enabled: bool) -> Self {
self.patch_epf = enabled;
self
}
#[cfg(feature = "splines")]
pub fn with_splines(mut self, splines: bool) -> Self {
self.splines = splines;
self
}
#[cfg(feature = "splines")]
pub fn with_edge_splines(mut self, edge_splines: bool) -> Self {
self.edge_splines = edge_splines;
self
}
#[cfg(feature = "splines")]
pub fn with_dots(mut self, dots: bool) -> Self {
self.dots = dots;
self
}
pub fn with_coding_transform(mut self, transform: CodingTransform) -> Self {
self.coding_transform = transform;
self
}
pub fn with_grain(mut self, grain: Grain) -> Self {
self.grain = grain;
self
}
pub fn with_lossy_modular(mut self, mode: LossyModular) -> Self {
self.lossy_modular = mode;
self
}
pub fn with_modular_vertical_first(mut self, enabled: bool) -> Self {
self.modular_vertical_first = enabled;
self
}
pub fn with_ink_layer(mut self, ink_layer: bool) -> Self {
self.ink_layer = ink_layer;
self
}
pub fn with_vector_layers(mut self, vector_layers: bool) -> Self {
self.vector_layers = vector_layers;
self
}
pub fn with_glyph_templates(mut self, glyph_templates: bool) -> Self {
self.glyph_templates = glyph_templates;
self
}
pub fn with_crop_frames(mut self, crop_frames: bool) -> Self {
self.crop_frames = crop_frames;
self
}
pub fn with_progressive_dc(mut self, progressive_dc: bool) -> Self {
self.progressive_dc = progressive_dc;
self
}
pub fn with_group_order(mut self, order: GroupOrder) -> Self {
self.group_order = order;
self
}
pub fn with_progressive_dc_levels(mut self, levels: u32) -> Self {
self.progressive_dc_levels = Some(levels);
self
}
pub fn with_learned_rate(mut self, learned_rate: bool) -> Self {
self.learned_rate = learned_rate;
self
}
pub fn with_gain_map(mut self, gain_map: GainMap) -> Self {
self.gain_map = Some(gain_map);
self
}
pub fn with_num_threads(mut self, n: usize) -> Self {
self.num_threads = n;
self
}
pub fn with_decoding_speed(mut self, decoding_speed: DecodingSpeed) -> Self {
self.decoding_speed = decoding_speed;
self
}
pub fn with_near_lossless(mut self, max_error: u16) -> Self {
self.near_lossless = max_error;
self
}
pub fn with_upsampling(mut self, upsampling: bool) -> Self {
self.upsampling = upsampling;
self
}
pub fn with_arbiter(mut self, arbiter: Arbiter) -> Self {
self.arbiter = arbiter;
self
}
pub fn with_speed(mut self, speed: Speed) -> Self {
self.speed = speed;
if speed.effort().patches_default {
self.patches = true;
self.glyph_templates = true;
self.patch_epf = true;
self.patches_lite = false;
} else if speed.effort().patches_lite_default && !self.patches {
self.patches = true;
self.patches_lite = true;
}
if speed.effort().ink_layer_default {
self.ink_layer = true;
}
if speed.effort().vector_layers_default {
self.vector_layers = true;
self.crop_frames = true;
}
self
}
}
pub fn distance_from_quality(quality: f32) -> f32 {
assert!(!quality.is_nan(), "quality must not be NaN");
let q = quality.clamp(0.0, 100.0);
let d = if q >= 99.0 {
0.05 * 2.0f32.powf(100.0 - q)
} else if q >= 90.0 {
10.0f32.powf((99.0 - q) / 9.0 - 1.0)
} else if q >= 30.0 {
0.1 + (100.0 - q) * 0.09
} else {
6.24 + 2.5f32.powf((30.0 - q) / 5.0) / 6.25
};
d.min(25.0)
}
fn apply_yellow_opsin(ctx: &mut EncodingContext, linear: &Image3F, distance: f32) {
if !ctx.speed.effort().yellow_opsin || !ctx.coding.is_xyb() {
return;
}
let selection = crate::yellow_opsin::select_yellow(linear, distance, &ctx.thread_pool);
if let Some(m) = selection.matrix {
ctx.set_xyb_matrix(m, distance, selection.default_channel_weights);
}
ctx.raise_b_qm_scale(selection.b_qm_scale);
}
const ALPHA_SQUEEZE_SCALE: f32 = 4.0;
const MAX_ALPHA_DISTANCE: f32 = 25.0;
fn alpha_learned_trees(config: &EncodeConfig) -> bool {
config.decoding_speed != DecodingSpeed::Fastest
&& matches!(config.speed, Speed::Slow | Speed::ExtraSlow)
}
pub(crate) fn alpha_plane_tree(
speed: Speed,
decoding_speed: DecodingSpeed,
) -> Option<crate::lossless::PlaneTreeBudget> {
use crate::lossless::PlaneTreeBudget;
if decoding_speed == DecodingSpeed::Fastest {
return None;
}
match speed {
Speed::UltraFast => None,
Speed::Fastest | Speed::Fast => Some(PlaneTreeBudget {
samples: 1 << 16,
leaves: 16,
gradient_only: true,
}),
Speed::Medium | Speed::MediumSlow => Some(PlaneTreeBudget {
samples: 1 << 18,
leaves: 256,
gradient_only: false,
}),
Speed::Slow | Speed::ExtraSlow => Some(PlaneTreeBudget::FULL),
}
}
pub(crate) fn lossy_context(
config: &EncodeConfig,
distance: f32,
xyb: XybMatrix,
pixels: usize,
) -> EncodingContext {
let num_threads = if config.speed.effort().full_threads {
config.num_threads
} else {
config.num_threads.min(pixels.div_ceil(64 * 1024).max(1))
};
let mut ctx = EncodingContext::new(config.speed, config.arbiter, xyb, distance, num_threads);
ctx.lossy_modular = config.lossy_modular;
ctx.modular_vertical_first = config.modular_vertical_first;
ctx.progressive_dc = config.progressive_dc;
ctx.progressive_dc_levels = config.progressive_dc_levels;
ctx.group_order = config.group_order;
ctx.learned_rate = config.learned_rate && config.speed.effort().learned_rate;
ctx.grain = config.grain;
let alpha_distance = match config.alpha_distance {
None => distance,
Some(d) if d.is_nan() => 0.0,
Some(d) => d.clamp(0.0, MAX_ALPHA_DISTANCE),
};
ctx.extra_squeeze_distance = ALPHA_SQUEEZE_SCALE * alpha_distance;
ctx.extra_learned_trees = alpha_learned_trees(config);
ctx.extra_plane_tree = alpha_plane_tree(config.speed, config.decoding_speed);
ctx.extra_context_trees = config.decoding_speed != DecodingSpeed::Fastest;
ctx.alpha_patches = config.patches && !config.patches_lite;
ctx.patch_epf = config.patch_epf;
ctx.patch_lite = config.patches_lite;
ctx.glyph_templates = config.templates_active && config.patches;
ctx.template_probe = config.template_probe.clone();
ctx.function_fields = config.function_fields
&& config.speed >= Speed::Medium
&& pixels <= crate::function_fields::MAX_PIXELS
&& config.grain == Grain::Off
&& !config.progressive_dc
&& progressive_schedule(
config.progressive,
config.progressive_passes,
config.progressive_shifts.as_deref(),
) == [0]
&& config.decoding_speed == DecodingSpeed::Slow;
ctx.function_fields_layered = ctx.function_fields && config.xyb_layers_decodable();
ctx.function_fields_portable = config.function_fields_portable;
ctx.function_fields_exact = ctx.function_fields
&& cfg!(feature = "extraslow")
&& config.speed >= Speed::ExtraSlow
&& config.icc_profile.is_none()
&& config.color_encoding.primaries == crate::Primaries::Bt709
&& config.color_encoding.white_point == crate::WhitePoint::D65
&& config.color_encoding.transfer == crate::TransferFunction::Srgb
&& config.intensity_target.is_none_or(|nits| nits <= 255.0);
#[cfg(feature = "splines")]
{
let slow = config.speed.effort().splines && config.decoding_speed == DecodingSpeed::Slow;
ctx.splines = config.splines && slow;
ctx.edge_splines = config.edge_splines && slow;
ctx.dots = config.dots && slow;
}
ctx
}
pub(crate) fn for_each_linear_band<F>(
image: &mut Image3F,
ctx: &EncodingContext,
scratch: &mut CoderScratch,
f: F,
) where
F: Fn(usize, [&mut [f32]; 3]) + Sync,
{
let mut pixel_start = 0;
let mut jobs: Vec<_> = image
.row_bands_mut(ctx.thread_pool.num_threads())
.into_iter()
.map(|band| {
let start = pixel_start;
pixel_start += band[0].len();
Some((start, band))
})
.collect();
if jobs.len() <= 1 {
for job in jobs {
let (start, band) = job.unwrap();
f(start, band);
}
} else {
ctx.thread_pool
.steal_for_each_mut(scratch, &mut jobs, |_i, job, _scratch| {
let (start, band) = job.take().unwrap();
f(start, band);
});
}
}
fn linearize_rgb<T, X, const CHANNELS: usize>(
input: &[T],
width: usize,
height: usize,
ctx: &EncodingContext,
scratch: &mut CoderScratch,
transform: &X,
) -> Image3F
where
T: Copy + Sync,
X: RgbBlockTransform<T>,
{
debug_assert!(CHANNELS >= 3);
let mut linear = Image3F::new(width, height);
for_each_linear_band(&mut linear, ctx, scratch, |pixel_start, [r, g, b]| {
let src = &input[pixel_start * CHANNELS..][..r.len() * CHANNELS];
transform.transform_block(src.as_chunks::<CHANNELS>().0, [r, g, b]);
});
linear
}
fn linearize_crop_base(
input: &[u8],
width: usize,
height: usize,
fills: &[(crate::crop_frames::CropBox, [u8; 3])],
ctx: &EncodingContext,
scratch: &mut CoderScratch,
transform: &ColorTransform<TransferLut>,
) -> Image3F {
const BLOCK: usize = 2048;
let input = input.as_chunks::<3>().0;
let mut linear = Image3F::new(width, height);
for_each_linear_band(&mut linear, ctx, scratch, |pixel_start, [r, g, b]| {
let mut staged = [[0u8; 3]; BLOCK];
let band = &input[pixel_start..][..r.len()];
for (i, (((src, r), g), b)) in band
.chunks(BLOCK)
.zip(r.chunks_mut(BLOCK))
.zip(g.chunks_mut(BLOCK))
.zip(b.chunks_mut(BLOCK))
.enumerate()
{
let start = pixel_start + i * BLOCK;
let end = start + src.len();
let first_row = start / width;
let end_row = end.div_ceil(width);
let mut painted = false;
for &(bounds, color) in fills {
for y in first_row.max(bounds.y0)..end_row.min(bounds.y0 + bounds.height) {
let row_start = y * width + bounds.x0;
let from = start.max(row_start);
let to = end.min(row_start + bounds.width);
if from < to {
if !painted {
staged[..src.len()].copy_from_slice(src);
painted = true;
}
staged[from - start..to - start].fill(color);
}
}
}
let src = if painted { &staged[..src.len()] } else { src };
transform.transform_block(src, [r, g, b]);
}
});
linear
}
fn linearize_gray<T, D, const CHANNELS: usize>(
input: &[T],
width: usize,
height: usize,
ctx: &EncodingContext,
scratch: &mut CoderScratch,
decoder: &D,
) -> Image3F
where
T: Copy + Sync,
D: TransferDecoder<T>,
{
let input = input.as_chunks::<CHANNELS>().0;
let mut linear = Image3F::new(width, height);
for_each_linear_band(&mut linear, ctx, scratch, |pixel_start, [r, g, b]| {
let src = &input[pixel_start..][..r.len()];
for (((r, g), b), pixel) in r
.iter_mut()
.zip(g.iter_mut())
.zip(b.iter_mut())
.zip(src.iter())
{
let linear = decoder.decode(pixel[0]);
*r = linear;
*g = linear;
*b = linear;
}
});
linear
}
const TEMPLATE_MIN_SAVED_SHARE: f64 = 0.002;
pub fn encode_image(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image(input, width, height, config)
});
}
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let race = config.glyph_templates
&& config.patches
&& !config.lossless
&& config.near_lossless == 0
&& config.coding_transform == CodingTransform::Xyb
&& !config.progressive_dc;
if !race {
return encode_image_rgb(input, width, height, config);
}
let probe = Arc::new(AtomicUsize::new(0));
let mut plain_config = config.clone();
plain_config.glyph_templates = false;
plain_config.template_probe = Some(Arc::clone(&probe));
let plain = encode_image_rgb(input, width, height, &plain_config)?;
let saved = probe.load(Ordering::Relaxed) as f64;
if saved < TEMPLATE_MIN_SAVED_SHARE * (width * height) as f64 {
return Ok(plain);
}
let mut templated_config = config.clone();
templated_config.glyph_templates = false;
templated_config.templates_active = true;
Ok(
match encode_lossy_u8_alpha::<3>(input, width, height, &templated_config) {
Ok(templated) if templated.len() < plain.len() => templated,
_ => plain,
},
)
}
fn encode_image_rgb(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
let expected = checked_buffer_size::<u8>(width, height, 3)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected,
actual: input.len(),
});
}
if !config.distance.is_finite() || config.distance <= 0.0 {
return Err(EncodeError::InvalidDistance(config.distance));
}
if config.lossless {
return encode_with_config_loseless(
input,
width,
height,
false,
8,
&EncodeConfigImpl::with_distance(config.distance)
.with_lossless(config.lossless)
.with_progressive(config.progressive)
.with_patches(config.patches_in(config.lossless))
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_speed(config.speed)
.with_decoding_speed(config.decoding_speed)
.with_near_lossless(config.near_lossless)
.with_upsampling(config.upsampling)
.with_num_threads(config.num_threads),
);
}
let vector_source = config
.vector_layers
.then(|| crate::vector_layers::SplitSource::rgb(input, width, height))
.flatten();
let mut vector = vector_source
.as_ref()
.map(|source| VectorSearch::new(source, config.distance));
let (whole, crop_share) = encode_image_arms(input, width, height, config, vector.as_mut())?;
if config.crop_frames
&& config.xyb_layers_decodable()
&& let Some(crop_share) = crop_share
&& config.coding_transform == CodingTransform::Xyb
&& !config.progressive
&& !config.progressive_dc
&& let Some(stack) =
encode_crop_stack(input, width, height, config, whole.len(), crop_share)?
&& (stack.len() as f64) < crop_share * whole.len() as f64
{
return snapped_vector_race(input, vector, width, height, config, stack);
}
snapped_vector_race(input, vector, width, height, config, whole)
}
struct VectorSearch<'source, 'pixels> {
source: &'source crate::vector_layers::SplitSource<'pixels, 3>,
snap_reach: u8,
snapped: std::cell::OnceCell<Option<crate::vector_layers::SplitPlan<'source, 'pixels, 3>>>,
snapped_floor: Option<(u32, Vec<u8>)>,
plain_split: bool,
prefer_flattened: bool,
}
impl<'source, 'pixels> VectorSearch<'source, 'pixels> {
fn new(source: &'source crate::vector_layers::SplitSource<'pixels, 3>, distance: f32) -> Self {
Self {
source,
snap_reach: crate::vector_layers::snap_reach(distance),
snapped: std::cell::OnceCell::new(),
snapped_floor: None,
plain_split: false,
prefer_flattened: false,
}
}
fn split_snapped(&self, steps: u32) -> Option<crate::vector_layers::VectorSplit> {
self.snapped
.get_or_init(|| self.source.prepare(Some(self.snap_reach)))
.as_ref()?
.split_for_retry(steps, self.prefer_flattened)
}
fn encode_snapped_floor(
&mut self,
steps: u32,
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Option<&[u8]>, EncodeError> {
if let Some(split) = self.split_snapped(steps) {
let bytes = encode_vector_layers(split, width, height, config)?.bytes;
let (_, bytes) = self.snapped_floor.insert((steps, bytes));
Ok(Some(bytes))
} else {
Ok(None)
}
}
}
fn snapped_vector_race(
input: &[u8],
search: Option<VectorSearch<'_, '_>>,
width: usize,
height: usize,
config: &EncodeConfig,
current: Vec<u8>,
) -> Result<Vec<u8>, EncodeError> {
if !config.vector_layers
|| config.speed < Speed::Slow
|| config.coding_transform != CodingTransform::Xyb
|| config.progressive
|| config.progressive_dc
{
return Ok(current);
}
let Some(mut search) = search else {
return Ok(current);
};
let nominal = crate::vector_layers::coverage_steps(config.distance);
let floor = crate::vector_layers::coverage_steps(2.0 * config.distance).max(3);
let coarser = [8, 6, 4, 3]
.into_iter()
.filter(|&s| s < nominal && s >= floor);
let mut smallest: Option<Vec<u8>> = None;
for steps in std::iter::once(nominal).chain(coarser) {
let snapped = if let Some((_, bytes)) = search
.snapped_floor
.take_if(|(cached_steps, _)| *cached_steps == steps)
{
bytes
} else {
let Some(split) = search.split_snapped(steps) else {
break;
};
encode_vector_layers(split, width, height, config)?.bytes
};
let limit = if steps == nominal {
current.len()
} else {
current.len() - current.len() / 10
};
if snapped.len() < limit {
return Ok(snapped);
}
if smallest.as_ref().is_none_or(|s| snapped.len() < s.len()) {
smallest = Some(snapped);
}
}
if config.distance >= SNAP_HANDOVER_MIN_DISTANCE
&& let Some(smallest) = smallest
&& smallest.len() < 2 * current.len()
&& !search.plain_split
{
let ratio = smallest.len() as f32 / current.len() as f32;
let mut finer_config = config.clone();
finer_config.distance =
(config.distance * (0.5 + 0.5 * (ratio - 1.0).clamp(0.0, 1.0))).max(MIN_DISTANCE);
let (finer, _) = encode_image_arms(input, width, height, &finer_config, None)?;
return Ok(if smallest.len() <= finer.len() {
smallest
} else {
finer
});
}
Ok(current)
}
const SNAP_HANDOVER_MIN_DISTANCE: f32 = 1.5;
const HANDOVER_MAX_MISS: f64 = 3.0;
const HANDOVER_MAX_RATIO: f64 = 2.5;
const HANDOVER_VECTOR_KEEP: f64 = 1.1;
#[allow(clippy::too_many_arguments)]
fn vector_handover(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
search: &mut VectorSearch<'_, '_>,
plan: crate::vector_layers::SplitPlan<'_, '_, 3>,
steps: u32,
mut smallest: Vec<u8>,
coarsest_coded: bool,
) -> Result<Option<Vec<u8>>, EncodeError> {
if config.speed < Speed::Slow {
return Ok(None);
}
let edges = crate::vector_layers::vardct_edge_histogram(input, width, height);
let budget = |distance| {
crate::vector_layers::VARDCT_BYTES_MARGIN
* crate::vector_layers::vardct_bytes_from_edges(&edges, distance)
};
let coarsest = crate::vector_layers::coverage_steps(2.0 * config.distance).max(3);
if !coarsest_coded
&& coarsest <= steps
&& (smallest.len() as f64) < HANDOVER_MAX_MISS * budget(config.distance)
{
if coarsest < steps
&& let Some(split) = plan.split_for_retry(coarsest, search.prefer_flattened)
{
let bytes = encode_vector_layers(split, width, height, config)?.bytes;
if bytes.len() < smallest.len() {
smallest = bytes;
}
}
if let Some(bytes) = search.encode_snapped_floor(coarsest, width, height, config)?
&& bytes.len() < smallest.len()
{
smallest = bytes.to_vec();
}
}
drop(plan);
let covers = |distance: f32| smallest.len() as f64 <= budget(distance);
let (mut lo, mut hi) = (config.distance / 2.0, config.distance);
if !covers(lo) {
return Ok(None);
}
for _ in 0..6 {
let mid = 0.5 * (lo + hi);
if covers(mid) {
lo = mid;
} else {
hi = mid;
}
}
let scale = 0.5 + 0.5 * (config.distance / lo - 1.0).clamp(0.0, 1.0);
let mut finer_config = config.clone();
finer_config.distance = (config.distance * scale).max(MIN_DISTANCE);
let (finer, _) = encode_image_arms(input, width, height, &finer_config, None)?;
if smallest.len() as f64 > HANDOVER_MAX_RATIO * finer.len() as f64 {
return Ok(None);
}
Ok(Some(
if smallest.len() as f64 <= HANDOVER_VECTOR_KEEP * finer.len() as f64 {
smallest
} else {
finer
},
))
}
fn encode_image_arms(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
search: Option<&mut VectorSearch<'_, '_>>,
) -> Result<(Vec<u8>, Option<f64>), EncodeError> {
let ink_plan = plan_ink(input, width, height, config);
let glyph_text = config.patches && ink_plan.as_ref().is_some_and(|plan| plan.glyph_text);
let race = match search {
Some(search) => match vector_arm(input, width, height, config, search, glyph_text)? {
VectorArm::Stands(bytes) => return Ok((bytes, None)),
VectorArm::Raced(race) => race,
},
None => VectorRace::default(),
};
if config.ink_layer
&& config.xyb_layers_decodable()
&& let Some(ink_plan) = ink_plan
{
return ink_arm(input, width, height, config, ink_plan, race);
}
match (race.vector, race.plain) {
(Some(vector), Some(plain)) => {
let stood = vector.len() > plain.len();
Ok((smaller_or_vector(vector, plain), stood.then_some(1.0)))
}
(Some(vector), None) => Ok((vector, None)),
(None, Some(plain)) => Ok((plain, Some(1.0))),
(None, None) => encode_rgb_frame(input, width, height, config).map(|b| (b, Some(1.0))),
}
}
fn plan_ink(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Option<crate::ink_layer::InkLayers> {
let layers = config.coding_transform == CodingTransform::Xyb && !config.progressive_dc;
let ink_stack = config.ink_layer && config.xyb_layers_decodable();
if !(layers
&& width * height >= 64 * 64
&& (ink_stack || (config.vector_layers && config.patches)))
{
return None;
}
let pool = crate::thread_pool::ThreadPool::new_lossless(config.num_threads);
crate::ink_layer::InkCandidates::new_fast(input, None, width, height, &pool)
.and_then(|candidates| candidates.select(config.distance, &pool))
}
fn encode_without_layers(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
let mut plain_config = config.clone();
plain_config.ink_layer = false;
plain_config.vector_layers = false;
plain_config.crop_frames = false;
encode_image(input, width, height, &plain_config)
}
fn smaller_or_vector(vector: Vec<u8>, other: Vec<u8>) -> Vec<u8> {
if vector.len() <= other.len() {
vector
} else {
other
}
}
#[derive(Default)]
struct VectorRace {
plain: Option<Vec<u8>>,
vector: Option<Vec<u8>>,
modeled_bytes: Option<f64>,
}
enum VectorArm {
Stands(Vec<u8>),
Raced(VectorRace),
}
fn vector_arm(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
search: &mut VectorSearch<'_, '_>,
glyph_text: bool,
) -> Result<VectorArm, EncodeError> {
let mut race = VectorRace::default();
let steps = crate::vector_layers::coverage_steps(config.distance);
let Some(plan) = search.source.prepare(None) else {
return Ok(VectorArm::Raced(race));
};
let Some(split) = plan.split(steps) else {
return Ok(VectorArm::Raced(race));
};
search.plain_split = true;
if glyph_text {
race.plain = Some(encode_without_layers(input, width, height, config)?);
}
let budget = match &race.plain {
Some(plain) => plain.len() as f64,
None => {
let estimate =
crate::vector_layers::vardct_bytes(input, width, height, config.distance);
race.modeled_bytes = Some(estimate);
estimate * crate::vector_layers::VARDCT_BYTES_MARGIN
}
};
let encoded = encode_vector_layers(split, width, height, config)?;
search.prefer_flattened =
config.speed == Speed::Slow && encoded.bytes.len() as f64 <= 0.90 * encoded.layered as f64;
let fit = fit_vector_to_budget(search, &plan, encoded, steps, budget, width, height, config)?;
match (fit.fitted, &race.plain) {
(Some(bytes), None) => return Ok(VectorArm::Stands(bytes)),
(Some(bytes), Some(_)) => race.vector = Some(bytes),
(None, None) => {
if let Some(finer) = vector_handover(
input,
width,
height,
config,
search,
plan,
steps,
fit.smallest,
fit.coarsest_coded,
)? {
return Ok(VectorArm::Stands(finer));
}
}
(None, Some(_)) => {}
}
Ok(VectorArm::Raced(race))
}
struct VectorFit {
fitted: Option<Vec<u8>>,
smallest: Vec<u8>,
coarsest_coded: bool,
}
#[allow(clippy::too_many_arguments)]
fn fit_vector_to_budget(
search: &mut VectorSearch<'_, '_>,
plan: &crate::vector_layers::SplitPlan<'_, '_, 3>,
encoded: VectorEncode,
steps: u32,
budget: f64,
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<VectorFit, EncodeError> {
let VectorEncode {
bytes,
layered,
base,
band,
} = encoded;
let mut fit = VectorFit {
fitted: None,
smallest: Vec::new(),
coarsest_coded: false,
};
if layered as f64 <= budget {
fit.fitted = Some(bytes);
return Ok(fit);
}
fit.smallest = bytes;
if base as f64 >= budget {
return Ok(fit);
}
let room = ((budget - base as f64) / band.max(1) as f64
* crate::vector_layers::band_bytes_ratio(steps))
.max(0.0);
let modeled = (5.0 * room - 1.0).exp2().floor().clamp(1.0, steps as f64) as u32;
let floor = crate::vector_layers::coverage_steps(2.0 * config.distance);
if (floor..steps).contains(&modeled)
&& let Some(split) = plan.split_for_retry(modeled, search.prefer_flattened)
{
fit.fitted = Some(encode_vector_layers(split, width, height, config)?.bytes);
} else if modeled < floor && (3..steps).contains(&floor) {
let best = encode_vector_floor(search, plan, floor, width, height, config)?;
if let Some(best) = &best
&& best.len() < fit.smallest.len()
{
fit.smallest.clone_from(best);
}
fit.coarsest_coded = true;
fit.fitted = best.filter(|b| b.len() as f64 <= budget);
}
Ok(fit)
}
fn encode_vector_floor(
search: &mut VectorSearch<'_, '_>,
plan: &crate::vector_layers::SplitPlan<'_, '_, 3>,
floor: u32,
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Option<Vec<u8>>, EncodeError> {
let plain_floor = plan.split_for_retry(floor, search.prefer_flattened);
let mut best = plain_floor
.map(|split| encode_vector_layers(split, width, height, config))
.transpose()?
.map(|encoded| encoded.bytes);
if let Some(bytes) = search.encode_snapped_floor(floor, width, height, config)?
&& best.as_ref().is_none_or(|b| bytes.len() < b.len())
{
best = Some(bytes.to_vec());
}
Ok(best)
}
fn ink_arm(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
ink_plan: crate::ink_layer::InkLayers,
race: VectorRace,
) -> Result<(Vec<u8>, Option<f64>), EncodeError> {
let VectorRace {
plain,
vector,
modeled_bytes,
} = race;
let (inked, outcome) = select_ink_layered_margin(
ink_plan,
width,
height,
config,
None,
modeled_bytes.unwrap_or_else(|| {
crate::vector_layers::vardct_bytes(input, width, height, config.distance)
}),
|_| match plain {
Some(plain) => Ok(plain),
None => encode_without_layers(input, width, height, config),
},
)?;
let vector_won = vector.as_ref().is_some_and(|v| v.len() <= inked.len());
let chosen = match vector {
Some(vector) => smaller_or_vector(vector, inked),
None => inked,
};
let crop_share = match outcome {
_ if vector_won => None,
InkOutcome::Ordinary => Some(1.0),
InkOutcome::Marginal => Some(INK_CROP_SHARE),
InkOutcome::Decisive => None,
};
Ok((chosen, crop_share))
}
fn encode_rgb_frame(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
let distance = config.distance.max(MIN_DISTANCE);
let transform = ColorTransform::for_integer(
8,
config.color_encoding.transfer,
config.color_encoding.primaries,
config.intensity_target,
);
let ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
let mut scratch = Box::<CoderScratch>::default();
let linear = linearize_rgb::<_, _, 3>(input, width, height, &ctx, &mut scratch, &transform);
let mut ctx = ctx;
ctx.set_coding(config.coding_transform, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
let cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
encode_samples_lossy(
Samples::U8 {
data: input,
stride: 3,
},
false,
linear,
&cfg,
&ctx,
&mut scratch,
)
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum InkOutcome {
Ordinary,
Marginal,
Decisive,
}
fn select_ink_layered_margin(
plan: crate::ink_layer::InkLayers,
width: usize,
height: usize,
config: &EncodeConfig,
alpha: Option<AlphaPlane>,
budget: f64,
ordinary: impl FnOnce(Option<AlphaPlane>) -> Result<Vec<u8>, EncodeError>,
) -> Result<(Vec<u8>, InkOutcome), EncodeError> {
let ordinary_only = |bytes: Vec<u8>| (bytes, InkOutcome::Ordinary);
let stack_outcome = |stack: usize, replaced: f64| {
if stack as f64 <= INK_DECISIVE_SHARE * replaced {
InkOutcome::Decisive
} else {
InkOutcome::Marginal
}
};
if config.distance < 1.0
&& ink_levels(config.distance, 1) > (255 / plan.inks.len().max(1)) as u32
{
drop(plan);
return ordinary(alpha).map(ordinary_only);
}
let glyph_text = plan.glyph_text && config.patches;
let photographic = plan.textured > INK_MODEL_MAX_TEXTURED;
let Some(stack) = InkStack::encode(plan, width, height, config, alpha.as_ref(), budget)? else {
return ordinary(alpha).map(ordinary_only);
};
if !glyph_text && !photographic && stack.len() as f64 <= budget {
let outcome = stack_outcome(stack.len(), budget);
return Ok((stack, outcome));
}
let plain = ordinary(alpha)?;
let step = if glyph_text || photographic {
1.0
} else {
INK_STACK_STEP
};
Ok(if stack.len() as f64 <= plain.len() as f64 * step {
let outcome = stack_outcome(stack.len(), plain.len() as f64);
(stack, outcome)
} else {
(plain, InkOutcome::Ordinary)
})
}
const INK_STACK_STEP: f64 = 1.15;
const INK_DECISIVE_SHARE: f64 = 0.85;
const INK_CROP_SHARE: f64 = 0.99;
const INK_MODEL_MAX_TEXTURED: f32 = 0.2;
fn ink_levels(distance: f32, inks: usize) -> u32 {
(7.0 / distance.max(0.01))
.round()
.clamp(2.0, (255 / inks.max(1)) as f32) as u32
}
struct InkStack {
width: usize,
height: usize,
ctx: EncodingContext,
inks: Vec<[u8; 3]>,
ink: Vec<u8>,
coverage: Vec<u8>,
}
impl InkStack {
fn encode(
mut plan: crate::ink_layer::InkLayers,
width: usize,
height: usize,
config: &EncodeConfig,
alpha: Option<&AlphaPlane>,
budget: f64,
) -> Result<Option<Vec<u8>>, EncodeError> {
let mut scratch = Box::<CoderScratch>::default();
let (ctx, base_frame) =
encode_ink_base(&mut plan, width, height, config, alpha, &mut scratch)?;
let stack = Self {
width,
height,
ctx,
inks: plan.inks,
ink: plan.ink,
coverage: plan.coverage,
};
let budget = budget * crate::vector_layers::VARDCT_BYTES_MARGIN;
let mut levels = ink_levels(config.distance, stack.inks.len());
let floor = if config.distance < 1.0 {
ink_levels(2.0 * config.distance, stack.inks.len())
} else {
1
};
let header = stack.image_header(config, alpha, &mut scratch);
let base_bits = header.bits_written() + base_frame.bits_written();
if base_bits.div_ceil(8) as f64 > budget {
if floor > 1 {
return Ok(None);
}
levels = 1;
}
let transform = ColorTransform::for_integer(
8,
config.color_encoding.transfer,
config.color_encoding.primaries,
config.intensity_target,
);
let mut gain_map = None;
loop {
let ink_frame = stack.encode_coverage(config, &transform, levels, &mut scratch)?;
let codestream_bytes = (base_bits + ink_frame.bits_written()).div_ceil(8);
if codestream_bytes as f64 <= budget || levels == 1 {
let mut w = header.clone();
w.append_bits(&base_frame);
w.append_bits(&ink_frame);
if gain_map.is_none() && config.gain_map.is_some() {
gain_map = encode_gain_map(config)?;
}
let bytes = finalize_container(
w.into_bytes(),
config.exif.as_deref(),
config.xmp.as_deref(),
config.brotli_compression.as_deref(),
false,
gain_map.as_ref(),
)?;
if bytes.len() as f64 <= budget || levels == 1 {
return Ok(Some(bytes));
}
}
if levels == floor {
return Ok(None);
}
levels = [7, 5, 3, 2, 1]
.into_iter()
.find(|&l| l < levels)
.unwrap_or(1)
.max(floor);
}
}
fn image_header(
&self,
config: &EncodeConfig,
alpha: Option<&AlphaPlane>,
scratch: &mut CoderScratch,
) -> BitWriter {
let opaque = alpha.is_none().then(|| AlphaPlane::from_u8(Vec::new()));
let base_alpha = alpha.or(opaque.as_ref()).expect("base alpha");
let mut w = BitWriter::new();
w.write(8, 0xFF);
w.write(8, CODESTREAM_MARKER as u64);
write_size_header(self.width, self.height, &mut w);
write_image_metadata(
&ImageMetadataParams {
tone_mapping: config.tone_mapping(),
color_encoding: &config.color_encoding,
alpha: Some(base_alpha),
black: false,
icc_profile: config.icc_profile.as_deref(),
bps: BitsPerSample::Eight,
lossless: false,
xyb_encoded: true,
xyb: &self.ctx.xyb,
grayscale: false,
orientation: config.orientation,
lossy_modular_16: false,
extra_16: false,
upsampling_kernel: None,
},
scratch,
&mut w,
);
w
}
fn encode_coverage(
&self,
config: &EncodeConfig,
transform: &ColorTransform<TransferLut>,
levels: u32,
scratch: &mut CoderScratch,
) -> Result<BitWriter, EncodeError> {
let mut palette = Vec::with_capacity(1 + self.inks.len() * levels as usize);
palette.push(([0u8; 3], 0u8));
for &ink in &self.inks {
for step in 1..=levels {
palette.push((ink, ((step * 255 + levels / 2) / levels) as u8));
}
}
let steps: [u8; 256] = std::array::from_fn(|a| ((a as u32 * levels + 127) / 255) as u8);
let indices = self.ink.iter().zip(&self.coverage).map(|(&k, &a)| {
let step = usize::from(steps[usize::from(a)]);
if k == 0 || step == 0 {
0
} else {
(usize::from(k) - 1) * levels as usize + step
}
});
encode_ink_frame(
&self.ctx,
transform,
&palette,
indices,
self.width,
self.height,
config,
scratch,
)
}
}
fn encode_ink_base(
plan: &mut crate::ink_layer::InkLayers,
width: usize,
height: usize,
config: &EncodeConfig,
original_alpha: Option<&AlphaPlane>,
scratch: &mut CoderScratch,
) -> Result<(EncodingContext, BitWriter), EncodeError> {
use crate::encoding_context::FrameRole;
let distance = config.distance.max(MIN_DISTANCE);
let transform = ColorTransform::for_integer(
8,
config.color_encoding.transfer,
config.color_encoding.primaries,
config.intensity_target,
);
let pixels = width * height;
let opaque = original_alpha
.is_none()
.then(|| AlphaPlane::from_u8(vec![255; pixels]));
let base_alpha = original_alpha.or(opaque.as_ref()).expect("base alpha");
let mut ctx = lossy_context(config, distance, XybMatrix::SPEC, pixels);
ctx.lossy_modular = LossyModular::Off;
ctx.extra_squeeze_distance = 0.0;
ctx.extra_step = 1;
ctx.frame_role = FrameRole::SavedFirst;
#[cfg(feature = "splines")]
if ctx.edge_splines {
ctx.overlay_coverage = Some(
plan.ink
.iter()
.zip(&plan.coverage)
.map(|(&k, &a)| if k == 0 { 0.0 } else { f32::from(a) / 255.0 })
.collect(),
);
}
let base = std::mem::take(&mut plan.base);
let linear = linearize_rgb::<_, _, 3>(&base, width, height, &ctx, scratch, &transform);
drop(base);
ctx.set_coding(CodingTransform::Xyb, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
if fit_base_overlay(&ctx, distance, width, height) {
let mut ink_xyb = linearize_rgb::<_, _, 3>(
plan.inks.as_flattened(),
plan.inks.len(),
1,
&ctx,
scratch,
&transform,
);
crate::xyb::to_xyb_in_place_with_fn(
ctx.to_xyb_band,
&ctx.xyb,
&mut ink_xyb,
&ctx.thread_pool,
scratch,
);
let mut luma = Vec::with_capacity(plan.inks.len() + 1);
luma.push(0.0);
luma.extend_from_slice(ink_xyb.plane_row(1, 0));
ctx.epf_overlay = Some(crate::epf_filter::EpfOverlay::Ink {
indices: std::mem::take(&mut plan.ink),
coverage: std::mem::take(&mut plan.coverage),
luma,
});
}
let mut frame = BitWriter::new();
let result = encode_frame(
&ctx,
scratch,
distance,
linear,
None,
false,
Some(base_alpha),
config.fill_invisible,
&[0],
config.patches,
true,
&mut frame,
);
if let Some(crate::epf_filter::EpfOverlay::Ink {
indices, coverage, ..
}) = ctx.epf_overlay.take()
{
plan.ink = indices;
plan.coverage = coverage;
}
#[cfg(feature = "splines")]
{
ctx.overlay_coverage = None;
}
result.map(|_| (ctx, frame))
}
fn fit_base_overlay(ctx: &EncodingContext, distance: f32, width: usize, height: usize) -> bool {
ctx.patch_epf
&& ctx.speed.effort().epf_fit
&& (0.5..=24.0).contains(&distance)
&& width >= 16
&& height >= 16
}
fn rgb_to_xyb_lattice_scaled(
matrix: &XybMatrix,
transform: &ColorTransform<TransferLut>,
rgb: &[[u8; 3]],
[luma, co, cg]: [&mut [i32]; 3],
scale: u32,
) {
const BLOCK: usize = 256;
let mut linear = [[0.0; BLOCK]; 3];
for (((pixels, luma), co), cg) in rgb
.chunks(BLOCK)
.zip(luma.chunks_mut(BLOCK))
.zip(co.chunks_mut(BLOCK))
.zip(cg.chunks_mut(BLOCK))
{
transform.transform_block(pixels, linear.each_mut().map(|p| &mut p[..pixels.len()]));
crate::xyb::xyb_lattice_ycocg_scaled(
matrix,
linear.each_ref().map(|p| &p[..pixels.len()]),
[luma, co, cg],
scale,
);
}
}
#[allow(clippy::too_many_arguments)]
fn encode_ink_frame(
ctx: &EncodingContext,
transform: &ColorTransform<TransferLut>,
colors: &[([u8; 3], u8)],
indices: impl Iterator<Item = usize>,
width: usize,
height: usize,
config: &EncodeConfig,
scratch: &mut CoderScratch,
) -> Result<BitWriter, EncodeError> {
let mut rgb = [[0; 3]; 256];
let rgb = &mut rgb[..colors.len()];
for (rgb, &(ink, _)) in rgb.iter_mut().zip(colors) {
*rgb = ink;
}
let mut palette = [[0; 256]; 3];
rgb_to_xyb_lattice_scaled(
&ctx.xyb,
transform,
rgb,
palette.each_mut().map(|p| &mut p[..colors.len()]),
crate::xyb::CROP_LATTICE_SCALE,
);
let [palette_y, palette_co, palette_cg] = &palette;
let mut image = Image3Si::new(width, height);
let mut alpha = vec![0u8; width * height];
let [luma, co, cg] = image.all_planes_mut().map(|plane| plane.as_mut_slice());
let dst = luma.iter_mut().zip(co).zip(cg).zip(&mut alpha);
for ((((luma, co), cg), alpha), index) in dst.zip(indices) {
(*luma, *co, *cg, *alpha) = (
palette_y[index],
palette_co[index],
palette_cg[index],
colors[index].1,
);
}
let alpha = AlphaPlane::from_u8(alpha);
let speed = if config.speed >= Speed::Slow {
Speed::Slow
} else {
Speed::Medium
};
let pool = crate::thread_pool::ThreadPool::new_lossless(config.num_threads);
let mut frame = BitWriter::new();
crate::lossless::encode_frame_lossless_xyb_ink(
&image,
&alpha,
speed,
config.decoding_speed,
&pool,
scratch,
&mut frame,
);
Ok(frame)
}
fn encode_crop_stack(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
whole: usize,
crop_share: f64,
) -> Result<Option<Vec<u8>>, EncodeError> {
use crate::crop_frames;
use crate::encoding_context::FrameRole;
use crate::patches::ModularFrameKind;
struct CropCandidate {
bounds: crop_frames::CropBox,
content: Vec<u8>,
saving: f64,
estimated_bytes: f64,
lossy: bool,
background: [u8; 3],
}
let boxes = crop_frames::plan(input, width, height);
let detail =
if config.speed >= Speed::Slow && config.distance <= crop_frames::DETAIL_MAX_DISTANCE {
crop_frames::plan_detail(input, width, height, &boxes)
} else {
Vec::new()
};
if boxes.is_empty() && detail.is_empty() {
return Ok(None);
}
let distance = config.distance.max(MIN_DISTANCE);
let transform = ColorTransform::for_integer(
8,
config.color_encoding.transfer,
config.color_encoding.primaries,
config.intensity_target,
);
let speed = if config.speed >= Speed::Slow {
Speed::Slow
} else {
Speed::Medium
};
let pool = crate::thread_pool::ThreadPool::new_lossless(config.num_threads);
let mut scratch = Box::<CoderScratch>::default();
let crop_frame = |ctx: &EncodingContext,
rgb: &[u8],
b: &crop_frames::CropBox,
last: bool,
speed: Speed,
scratch: &mut CoderScratch|
-> BitWriter {
let mut image = Image3Si::new(b.width, b.height);
rgb_to_xyb_lattice_scaled(
&ctx.xyb,
&transform,
rgb.as_chunks::<3>().0,
image.all_planes_mut().map(|plane| plane.as_mut_slice()),
crate::xyb::CROP_LATTICE_SCALE,
);
let mut frame = BitWriter::new();
crate::lossless::encode_frame_lossless_xyb_crop(
&image,
ModularFrameKind::XybCrop {
x0: b.x0,
y0: b.y0,
width: b.width,
height: b.height,
last,
},
speed,
config.decoding_speed,
&pool,
scratch,
&mut frame,
);
frame
};
let lossy_frame = |ctx: &EncodingContext,
rgb: &[u8],
b: &crop_frames::CropBox,
last: bool,
scratch: &mut CoderScratch|
-> Option<BitWriter> {
let linear = linearize_rgb::<_, _, 3>(rgb, b.width, b.height, ctx, scratch, &transform);
let mut frame = BitWriter::new();
crate::frame::encode_modular_crop_frame(
ctx,
scratch,
distance,
linear,
[b.x0, b.y0],
last,
&mut frame,
)?;
Some(frame)
};
let spec = lossy_context(config, distance, XybMatrix::SPEC, width * height);
let mut vardct_config = config.clone();
vardct_config.ink_layer = false;
vardct_config.vector_layers = false;
vardct_config.crop_frames = false;
vardct_config.speed = Speed::Fast;
let mut chosen = Vec::new();
for b in &boxes {
let crop = crop_frames::cut(input, width, b);
let model = encode_image(&crop, b.width, b.height, &vardct_config)?.len() as f64;
let content = crop_frames::content(crop, b, config.distance);
let frame = crop_frame(&spec, &content, b, false, Speed::Medium, &mut scratch);
let bytes = frame.bits_written() as f64 / 8.0 + crop_frames::FRAME_OVERHEAD;
if bytes < model {
chosen.push(CropCandidate {
bounds: *b,
content,
saving: model - bytes,
estimated_bytes: bytes,
lossy: false,
background: crop_frames::background_color(input, width, *b),
});
}
}
for b in &detail {
let crop = crop_frames::cut(input, width, b);
let model = encode_image(&crop, b.width, b.height, &vardct_config)?.len() as f64;
let Some(frame) = lossy_frame(&spec, &crop, b, false, &mut scratch) else {
continue;
};
let bytes = frame.bits_written() as f64 / 8.0 + crop_frames::FRAME_OVERHEAD;
if bytes < crop_frames::DETAIL_MARGIN * model {
chosen.push(CropCandidate {
bounds: *b,
content: crop,
saving: model - bytes,
estimated_bytes: bytes,
lossy: true,
background: crop_frames::background_color(input, width, *b),
});
}
}
drop(spec);
for lossy in [false, true] {
let saving: f64 = chosen
.iter()
.filter(|c| c.lossy == lossy)
.map(|c| c.saving)
.sum();
if saving < 0.01 * whole as f64 {
chosen.retain(|c| c.lossy != lossy);
}
}
if chosen.is_empty() {
return Ok(None);
}
let stack = |chosen: &[&CropCandidate],
scratch: &mut CoderScratch|
-> Result<(Option<Vec<u8>>, usize), EncodeError> {
let fills: Vec<_> = chosen.iter().map(|c| (c.bounds, c.background)).collect();
let pixels = width * height;
let mut ctx = lossy_context(config, distance, XybMatrix::SPEC, pixels);
ctx.lossy_modular = LossyModular::Off;
ctx.frame_role = FrameRole::SavedFirst;
ctx.crop_base = true;
let linear = linearize_crop_base(input, width, height, &fills, &ctx, scratch, &transform);
ctx.set_coding(CodingTransform::Xyb, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
if fit_base_overlay(&ctx, distance, width, height) {
let mut coverage = vec![0; pixels];
for (b, _) in &fills {
for row in coverage.chunks_exact_mut(width).skip(b.y0).take(b.height) {
row[b.x0..b.x0 + b.width].fill(1);
}
}
ctx.epf_overlay = Some(crate::epf_filter::EpfOverlay::Crops { coverage });
}
let mut base = BitWriter::new();
encode_frame(
&ctx,
scratch,
distance,
linear,
None,
false,
None,
config.fill_invisible,
&[0],
config.patches,
true,
&mut base,
)?;
ctx.epf_overlay = None;
let base_bytes = base.bits_written() / 8;
if base_bytes >= whole {
return Ok((None, base_bytes));
}
let mut w = BitWriter::new();
w.write(8, 0xFF);
w.write(8, CODESTREAM_MARKER as u64);
write_size_header(width, height, &mut w);
write_image_metadata(
&ImageMetadataParams {
tone_mapping: config.tone_mapping(),
color_encoding: &config.color_encoding,
alpha: None,
black: false,
icc_profile: config.icc_profile.as_deref(),
bps: BitsPerSample::Eight,
lossless: false,
xyb_encoded: true,
xyb: &ctx.xyb,
grayscale: false,
orientation: config.orientation,
lossy_modular_16: false,
extra_16: false,
upsampling_kernel: None,
},
scratch,
&mut w,
);
w.append_bits(&base);
drop(base);
let last = chosen.len() - 1;
for (i, crop) in chosen.iter().enumerate() {
let frame = if crop.lossy {
lossy_frame(&ctx, &crop.content, &crop.bounds, i == last, scratch)
.expect("the crop squeezed when it was priced")
} else {
crop_frame(&ctx, &crop.content, &crop.bounds, i == last, speed, scratch)
};
w.append_bits(&frame);
}
finalize_container(
w.into_bytes(),
config.exif.as_deref(),
config.xmp.as_deref(),
config.brotli_compression.as_deref(),
false,
encode_gain_map(config)?.as_ref(),
)
.map(|bytes| (Some(bytes), base_bytes))
};
let flat: Vec<_> = chosen.iter().filter(|c| !c.lossy).collect();
let lossy: Vec<_> = chosen.iter().filter(|c| c.lossy).collect();
let mut best_stack: Option<Vec<u8>> = None;
let mut flat_stood = false;
if !flat.is_empty() {
let (first, first_base) = stack(&flat, &mut scratch)?;
if first.as_ref().is_some_and(|bytes| bytes.len() < whole) || flat.len() == 1 {
flat_stood = first
.as_ref()
.is_some_and(|bytes| (bytes.len() as f64) < crop_share * whole as f64);
best_stack = first;
} else {
let best = flat
.iter()
.max_by(|a, b| a.saving.total_cmp(&b.saving))
.expect("flat is not empty");
best_stack = if best.saving < 0.03 * whole as f64
|| first_base as f64 + best.estimated_bytes >= whole as f64
{
first
} else {
stack(&[best], &mut scratch)?.0
};
}
}
if lossy.is_empty() {
return Ok(best_stack);
}
let with_detail: Vec<_> = if flat_stood {
chosen.iter().collect()
} else {
lossy
};
let detail_stack = stack(&with_detail, &mut scratch)?.0;
let size = |s: &Option<Vec<u8>>| s.as_ref().map_or(usize::MAX, Vec::len);
Ok(if size(&detail_stack) < size(&best_stack) {
detail_stack
} else {
best_stack
})
}
struct VectorEncode {
bytes: Vec<u8>,
layered: usize,
base: usize,
band: usize,
}
fn encode_vector_layers(
split: crate::vector_layers::VectorSplit,
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<VectorEncode, EncodeError> {
use crate::patches::ModularFrameKind;
let pixels = width * height;
let convert = |rgb: &[u8], image: &mut Image3Si| {
rgb8_to_ycocg(
rgb.as_chunks::<3>().0,
image.all_planes_mut().map(|plane| plane.as_mut_slice()),
);
};
let crate::vector_layers::VectorSplit {
base,
layer,
coverage,
base_alpha,
} = split;
let transparent = base_alpha.is_some();
let mut base_alpha = AlphaPlane::from_u8(base_alpha.unwrap_or_else(|| vec![255; pixels]));
let band = coverage
.iter()
.any(|&a| a != 0)
.then_some((layer, AlphaPlane::from_u8(coverage)));
let mut image = Image3Si::new(width, height);
let mut scratch = Box::new(CoderScratch::lossless());
let mut w = BitWriter::new();
w.write(8, 0xFF);
w.write(8, CODESTREAM_MARKER as u64);
write_size_header(width, height, &mut w);
write_image_metadata(
&ImageMetadataParams {
tone_mapping: config.tone_mapping(),
color_encoding: &config.color_encoding,
alpha: Some(&base_alpha),
black: false,
icc_profile: config.icc_profile.as_deref(),
bps: BitsPerSample::Eight,
lossless: true,
xyb_encoded: false,
xyb: &XybMatrix::SPEC,
grayscale: false,
orientation: config.orientation,
lossy_modular_16: false,
extra_16: false,
upsampling_kernel: None,
},
&mut scratch,
&mut w,
);
let speed = if cfg!(feature = "extraslow") && config.speed >= Speed::ExtraSlow {
Speed::Slow
} else {
Speed::Medium
};
let decoding_speed = if config.decoding_speed == DecodingSpeed::Fastest {
DecodingSpeed::Fastest
} else {
DecodingSpeed::Fast
};
let pool = crate::thread_pool::ThreadPool::new_lossless(config.num_threads);
let header = band.as_ref().map(|_| w.clone());
let start = w.bits_written();
let single_only = transparent && band.is_some();
if !single_only {
convert(&base, &mut image);
crate::lossless::encode_frame_lossless_layer(
&image,
&base_alpha,
if band.is_some() {
ModularFrameKind::RgbSavedFirst
} else {
ModularFrameKind::Regular
},
speed,
decoding_speed,
&pool,
&mut scratch,
&mut w,
);
}
let base_bytes = (w.bits_written() - start) / 8;
if let Some((layer, coverage)) = band.as_ref().filter(|_| !single_only) {
convert(layer, &mut image);
crate::lossless::encode_frame_lossless_layer(
&image,
coverage,
ModularFrameKind::RgbBlendLast,
speed,
decoding_speed,
&pool,
&mut scratch,
&mut w,
);
}
let band_bytes = (w.bits_written() - start) / 8 - base_bytes;
let mut layered_bytes = w.bits_written().div_ceil(8);
if let Some((layer, coverage)) = band {
let mut composite = base;
let AlphaPlane::U8(alpha) = coverage else {
unreachable!()
};
crate::simd::rgb8_blend::blend_in_place(
composite.as_chunks_mut::<3>().0,
layer.as_chunks::<3>().0,
&alpha,
);
if transparent {
let AlphaPlane::U8(out) = &mut base_alpha else {
unreachable!()
};
for (((a, base), layer), &c) in out
.iter_mut()
.zip(composite.as_chunks_mut::<3>().0)
.zip(layer.as_chunks::<3>().0)
.zip(&alpha)
{
if *a == 0 && c != 0 {
*a = c;
*base = *layer;
}
}
}
drop(alpha);
drop(layer);
convert(&composite, &mut image);
drop(composite);
let mut single = header.expect("band header");
crate::lossless::encode_frame_lossless_layer(
&image,
&base_alpha,
ModularFrameKind::Regular,
speed,
decoding_speed,
&pool,
&mut scratch,
&mut single,
);
if single_only {
layered_bytes = single.bits_written().div_ceil(8);
w = single;
} else if single.bits_written() < w.bits_written() {
w = single;
}
}
drop(image);
drop(base_alpha);
let saved = layered_bytes - w.bits_written().div_ceil(8);
let bytes = finalize_container(
w.into_bytes(),
config.exif.as_deref(),
config.xmp.as_deref(),
config.brotli_compression.as_deref(),
false,
encode_gain_map(config)?.as_ref(),
)?;
Ok(VectorEncode {
layered: bytes.len() + saved,
bytes,
base: base_bytes,
band: band_bytes,
})
}
pub fn encode_image_with_alpha(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_with_alpha(input, width, height, config)
});
}
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
let expected = checked_buffer_size::<u8>(width, height, 4)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected,
actual: input.len(),
});
}
if !config.distance.is_finite() || config.distance <= 0.0 {
return Err(EncodeError::InvalidDistance(config.distance));
}
if config.lossless {
return encode_with_config_loseless(
input,
width,
height,
true,
8,
&EncodeConfigImpl::with_distance(config.distance)
.with_lossless(config.lossless)
.with_progressive(config.progressive)
.with_patches(config.patches_in(config.lossless))
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_speed(config.speed)
.with_decoding_speed(config.decoding_speed)
.with_near_lossless(config.near_lossless)
.with_upsampling(config.upsampling)
.with_num_threads(config.num_threads),
);
}
encode_lossy_u8_alpha::<4>(input, width, height, config)
}
fn encode_lossy_u8_alpha<const CHANNELS: usize>(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
let arms = |config: &EncodeConfig| {
encode_lossy_u8_alpha_arms::<CHANNELS>(input, width, height, config)
};
if CHANNELS != 4
|| !config.vector_layers
|| width * height < 64 * 64
|| config.speed < Speed::Slow
|| config.coding_transform != CodingTransform::Xyb
|| config.progressive
|| config.progressive_dc
|| config
.alpha_distance
.is_some_and(|d| d <= 0.0 || d.is_nan())
{
return arms(config);
}
let pixels = input.as_chunks::<4>().0;
let mut alpha: Vec<u8> = pixels.iter().map(|px| px[3]).collect();
crate::alpha_fringe::clear(&mut alpha, width, height, 255);
let Some(source) = crate::vector_layers::SplitSource::rgba(pixels, &alpha, width, height)
else {
return arms(config);
};
let steps = crate::vector_layers::coverage_steps(config.distance);
let snapped = source
.prepare(Some(crate::vector_layers::snap_reach(config.distance)))
.and_then(|plan| plan.split(steps));
let was_snapped = snapped.is_some();
let first = snapped.or_else(|| source.prepare(None).and_then(|plan| plan.split(steps)));
let Some(first) = first else {
return arms(config);
};
let first = encode_vector_layers(first, width, height, config)?.bytes;
{
let mut proxy_config = config.clone();
proxy_config.speed = Speed::Fast;
proxy_config.ink_layer = false;
proxy_config.patches = false;
proxy_config.glyph_templates = false;
proxy_config.vector_layers = false;
proxy_config.crop_frames = false;
let proxy = arms(&proxy_config)?;
if first.len() * 5 <= proxy.len() * 2 {
return Ok(first);
}
}
let current = arms(config)?;
if first.len() < current.len() {
return Ok(first);
}
let mut smallest = first;
if was_snapped && let Some(split) = source.prepare(None).and_then(|plan| plan.split(steps)) {
let vector = encode_vector_layers(split, width, height, config)?.bytes;
if vector.len() < current.len() {
return Ok(vector);
}
if vector.len() < smallest.len() {
smallest = vector;
}
}
drop(source);
drop(alpha);
if config.distance >= SNAP_HANDOVER_MIN_DISTANCE && smallest.len() < 2 * current.len() {
let ratio = smallest.len() as f32 / current.len() as f32;
let mut finer_config = config.clone();
finer_config.distance =
(config.distance * (0.5 + 0.5 * (ratio - 1.0).clamp(0.0, 1.0))).max(MIN_DISTANCE);
let finer = arms(&finer_config)?;
return Ok(if smallest.len() <= finer.len() {
smallest
} else {
finer
});
}
Ok(current)
}
fn encode_lossy_u8_alpha_arms<const CHANNELS: usize>(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
let pixels = input.as_chunks::<CHANNELS>().0;
let lossy_alpha = config.alpha_distance.is_none_or(|d| d > 0.0);
let make_alpha = || {
if CHANNELS == 3 {
vec![255; width * height]
} else {
let mut alpha: Vec<u8> = pixels.iter().map(|px| px[3]).collect();
if lossy_alpha {
crate::alpha_fringe::clear(&mut alpha, width, height, 255);
}
alpha
}
};
let mut alpha = None;
let ordinary = |alpha: Option<AlphaPlane>| {
encode_lossy_u8_alpha_frame::<CHANNELS>(
input,
width,
height,
config,
alpha.unwrap_or_else(|| AlphaPlane::from_u8(make_alpha())),
)
};
if config.ink_layer
&& config.xyb_layers_decodable()
&& config.coding_transform == CodingTransform::Xyb
&& !config.progressive_dc
&& width * height >= 64 * 64
{
let (rgb, mask) = if CHANNELS == 3 {
(std::borrow::Cow::Borrowed(input), None)
} else {
let rgb: Vec<_> = pixels.iter().flat_map(|px| [px[0], px[1], px[2]]).collect();
(
std::borrow::Cow::Owned(rgb),
Some(alpha.insert(make_alpha()).as_slice()),
)
};
let pool = crate::thread_pool::ThreadPool::new_lossless(config.num_threads);
let plan = crate::ink_layer::InkCandidates::new_fast(&rgb, mask, width, height, &pool)
.and_then(|candidates| candidates.select(config.distance, &pool));
drop(pool);
if let Some(plan) = plan {
let budget = crate::vector_layers::vardct_bytes(&rgb, width, height, config.distance);
drop(rgb);
let alpha = alpha.unwrap_or_else(make_alpha);
return select_ink_layered_margin(
plan,
width,
height,
config,
Some(AlphaPlane::from_u8(alpha)),
budget,
ordinary,
)
.map(|(bytes, _)| bytes);
}
}
ordinary(alpha.map(AlphaPlane::from_u8))
}
fn encode_lossy_u8_alpha_frame<const CHANNELS: usize>(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
alpha: AlphaPlane,
) -> Result<Vec<u8>, EncodeError> {
let distance = config.distance.max(MIN_DISTANCE);
let transform = ColorTransform::for_integer(
8,
config.color_encoding.transfer,
config.color_encoding.primaries,
config.intensity_target,
);
let ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
let mut scratch = Box::<CoderScratch>::default();
let linear =
linearize_rgb::<_, _, CHANNELS>(input, width, height, &ctx, &mut scratch, &transform);
let mut ctx = ctx;
ctx.set_coding(config.coding_transform, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
let cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_alpha(alpha)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
encode_samples_lossy(
Samples::U8 {
data: input,
stride: CHANNELS,
},
false,
linear,
&cfg,
&ctx,
&mut scratch,
)
}
pub fn encode_image_with_alpha_10bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_with_alpha_10bit(input, width, height, config)
});
}
encode_high_depth_rgba(input, width, height, true, config, BitsPerSample::Ten)
}
pub fn encode_image_with_alpha_12bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_with_alpha_12bit(input, width, height, config)
});
}
encode_high_depth_rgba(input, width, height, true, config, BitsPerSample::Twelve)
}
pub fn encode_image_with_alpha_16bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_with_alpha_16bit(input, width, height, config)
});
}
encode_high_depth_rgba(input, width, height, true, config, BitsPerSample::Sixteen)
}
pub fn encode_image_16bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_16bit(input, width, height, config)
});
}
encode_high_depth_rgba(input, width, height, false, config, BitsPerSample::Sixteen)
}
pub fn encode_image_10bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_10bit(input, width, height, config)
});
}
encode_high_depth_rgba(input, width, height, false, config, BitsPerSample::Ten)
}
pub fn encode_image_12bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_12bit(input, width, height, config)
});
}
encode_high_depth_rgba(input, width, height, false, config, BitsPerSample::Twelve)
}
pub fn encode_image_gray(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray(input, width, height, config)
});
}
encode_gray_impl::<1>(input, width, height, config)
}
pub fn encode_image_gray_alpha(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_alpha(input, width, height, config)
});
}
let expected = checked_buffer_size::<u8>(width, height, 2)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected: width * height * 2,
actual: input.len(),
});
}
encode_gray_impl::<2>(input, width, height, config)
}
fn encode_gray_impl<const CHANNELS: usize>(
input: &[u8],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
let expected = checked_buffer_size::<u8>(width, height, CHANNELS)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected: width * height,
actual: input.len(),
});
}
if !config.distance.is_finite() || config.distance <= 0.0 {
return Err(EncodeError::InvalidDistance(config.distance));
}
let pixels = input.as_chunks::<CHANNELS>().0;
let alpha = || (CHANNELS == 2).then(|| pixels.iter().map(|px| px[1]).collect::<Vec<_>>());
if config.lossless {
let mut image = Image3Si::try_new_per_plane([(width, height), (0, 0), (0, 0)])?;
for (out, pixel) in image.plane_mut(0).as_mut_slice().iter_mut().zip(pixels) {
*out = pixel[0].to_signed_int(8);
}
crate::lossless::quantize_planes(&mut image, 255, i32::from(config.near_lossless));
let alpha_plane = alpha().map(AlphaPlane::U8);
return encode_lossless_planes(
&image,
&alpha_plane,
8,
&EncodeConfigImpl::with_distance(config.distance)
.with_lossless(true)
.with_grayscale(true)
.with_patches(config.patches_in(true))
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_speed(config.speed)
.with_decoding_speed(config.decoding_speed)
.with_near_lossless(config.near_lossless)
.with_upsampling(config.upsampling)
.with_num_threads(config.num_threads),
);
}
let distance = config.distance.max(MIN_DISTANCE);
let decoder = TransferLut::for_gray(8, config.color_encoding.transfer, config.intensity_target);
let mut ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
ctx.set_coding(config.coding_transform, distance);
let mut scratch = Box::<CoderScratch>::default();
let linear =
linearize_gray::<_, _, CHANNELS>(input, width, height, &ctx, &mut scratch, &decoder);
let mut cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_grayscale(true)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
if let Some(mut a) = alpha() {
if config.alpha_distance.is_none_or(|d| d > 0.0) {
crate::alpha_fringe::clear(&mut a, width, height, 255);
}
cfg = cfg.with_alpha(AlphaPlane::from_u8(a));
}
encode_samples_lossy(
Samples::U8 {
data: input,
stride: CHANNELS,
},
true,
linear,
&cfg,
&ctx,
&mut scratch,
)
}
pub fn encode_image_gray_10bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_10bit(input, width, height, config)
});
}
encode_gray_high_depth_impl::<1>(input, width, height, config, BitsPerSample::Ten)
}
pub fn encode_image_gray_12bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_12bit(input, width, height, config)
});
}
encode_gray_high_depth_impl::<1>(input, width, height, config, BitsPerSample::Twelve)
}
pub fn encode_image_gray_alpha_10bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_alpha_10bit(input, width, height, config)
});
}
let expected = checked_buffer_size::<u16>(width, height, 2)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected: width * height * 2,
actual: input.len(),
});
}
encode_gray_high_depth_impl::<2>(input, width, height, config, BitsPerSample::Ten)
}
pub fn encode_image_gray_alpha_12bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_alpha_12bit(input, width, height, config)
});
}
let expected = checked_buffer_size::<u16>(width, height, 2)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected: width * height * 2,
actual: input.len(),
});
}
encode_gray_high_depth_impl::<2>(input, width, height, config, BitsPerSample::Twelve)
}
pub fn encode_image_gray_16bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_16bit(input, width, height, config)
});
}
encode_gray_high_depth_impl::<1>(input, width, height, config, BitsPerSample::Sixteen)
}
pub fn encode_image_gray_alpha_16bit(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_alpha_16bit(input, width, height, config)
});
}
let expected = checked_buffer_size::<u16>(width, height, 2)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected: width * height * 2,
actual: input.len(),
});
}
encode_gray_high_depth_impl::<2>(input, width, height, config, BitsPerSample::Sixteen)
}
fn encode_gray_high_depth_impl<const CHANNELS: usize>(
input: &[u16],
width: usize,
height: usize,
config: &EncodeConfig,
bps: BitsPerSample,
) -> Result<Vec<u8>, EncodeError> {
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
let expected = checked_buffer_size::<u16>(width, height, CHANNELS)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected,
actual: input.len(),
});
}
if !config.distance.is_finite() || config.distance <= 0.0 {
return Err(EncodeError::InvalidDistance(config.distance));
}
let pixels = input.as_chunks::<CHANNELS>().0;
if config.lossless {
let mut image = Image3Si::try_new_per_plane([(width, height), (0, 0), (0, 0)])?;
for (out, pixel) in image.plane_mut(0).as_mut_slice().iter_mut().zip(pixels) {
*out = pixel[0].to_signed_int(bps.bits() as u8);
}
let alpha_plane = (CHANNELS == 2).then(|| {
let bits = bps.bits() as u8;
let data = pixels
.iter()
.map(|px| px[1].to_signed_int(bits) as u16)
.collect();
AlphaPlane::U16 { data, bits }
});
return encode_lossless_planes(
&image,
&alpha_plane,
bps.bits() as u8,
&EncodeConfigImpl::with_distance(config.distance)
.with_lossless(true)
.with_grayscale(true)
.with_patches(config.patches_in(true))
.with_bits_per_sample(bps)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_speed(config.speed)
.with_decoding_speed(config.decoding_speed)
.with_near_lossless(config.near_lossless)
.with_upsampling(config.upsampling)
.with_num_threads(config.num_threads),
);
}
let distance = config.distance.max(MIN_DISTANCE);
let decoder = TransferLut::for_gray(
bps.bits(),
config.color_encoding.transfer,
config.intensity_target,
);
let mut ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
ctx.set_coding(config.coding_transform, distance);
let mut scratch = Box::<CoderScratch>::default();
let linear =
linearize_gray::<_, _, CHANNELS>(input, width, height, &ctx, &mut scratch, &decoder);
let alpha_plane = (CHANNELS == 2).then(|| {
let mut a: Vec<_> = pixels.iter().map(|px| px[1]).collect();
if config.alpha_distance.is_none_or(|d| d > 0.0) {
crate::alpha_fringe::clear(&mut a, width, height, (1u32 << bps.bits()) - 1);
}
AlphaPlane::U16 {
data: a,
bits: bps.bits() as u8,
}
});
let mut cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_grayscale(true)
.with_bits_per_sample(bps)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
if let Some(ap) = alpha_plane {
cfg = cfg.with_alpha(ap);
}
encode_samples_lossy(
Samples::U16 {
data: input,
stride: CHANNELS,
max: ((1u32 << bps.bits()) - 1) as u16,
},
true,
linear,
&cfg,
&ctx,
&mut scratch,
)
}
fn encode_high_depth_rgba(
input: &[u16],
width: usize,
height: usize,
has_alpha: bool,
config: &EncodeConfig,
bps: BitsPerSample,
) -> Result<Vec<u8>, EncodeError> {
let expected = checked_buffer_size::<u16>(width, height, if has_alpha { 4 } else { 3 })?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected,
actual: input.len(),
});
}
if config.lossless {
return encode_with_config_loseless(
input,
width,
height,
has_alpha,
bps.bits() as u8,
&EncodeConfigImpl::with_distance(config.distance)
.with_lossless(config.lossless)
.with_bits_per_sample(bps)
.with_patches(config.patches_in(config.lossless))
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_speed(config.speed)
.with_decoding_speed(config.decoding_speed)
.with_near_lossless(config.near_lossless)
.with_upsampling(config.upsampling)
.with_num_threads(config.num_threads),
);
}
let distance = config.distance.max(MIN_DISTANCE);
let transform = ColorTransform::for_integer(
bps.bits(),
config.color_encoding.transfer,
config.color_encoding.primaries,
config.intensity_target,
);
let ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
let mut scratch = Box::<CoderScratch>::default();
let bp_max: u16 = if bps.bits() >= 16 {
u16::MAX
} else {
((1u32 << bps.bits()) - 1) as u16
};
if has_alpha {
let linear = linearize_rgb::<_, _, 4>(input, width, height, &ctx, &mut scratch, &transform);
let mut ctx = ctx;
ctx.set_coding(config.coding_transform, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
let mut alpha_plane: Vec<u16> = input
.as_chunks::<4>()
.0
.iter()
.map(|px| px[3].min(bp_max))
.collect();
if config.alpha_distance.is_none_or(|d| d > 0.0) {
crate::alpha_fringe::clear(&mut alpha_plane, width, height, u32::from(bp_max));
}
let cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_alpha(match bps {
BitsPerSample::Ten => AlphaPlane::from_u16_10bit(alpha_plane),
BitsPerSample::Twelve => AlphaPlane::from_u16_12bit(alpha_plane),
BitsPerSample::Sixteen => AlphaPlane::from_u16_16bit(alpha_plane),
BitsPerSample::Eight => unreachable!("high-depth path called with 8-bit bps"),
BitsPerSample::F16 | BitsPerSample::F32 => {
unreachable!("float path does not use the integer alpha match")
}
})
.with_bits_per_sample(bps)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
encode_samples_lossy(
Samples::U16 {
data: input,
stride: 4,
max: bp_max,
},
false,
linear,
&cfg,
&ctx,
&mut scratch,
)
} else {
let linear = linearize_rgb::<_, _, 3>(input, width, height, &ctx, &mut scratch, &transform);
let mut ctx = ctx;
ctx.set_coding(config.coding_transform, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
let cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_bits_per_sample(bps)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
encode_samples_lossy(
Samples::U16 {
data: input,
stride: 3,
max: bp_max,
},
false,
linear,
&cfg,
&ctx,
&mut scratch,
)
}
}
fn encode_f32_lossless_rgba(
input: &[f32],
width: usize,
height: usize,
has_alpha: bool,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
for &v in input.iter() {
if !v.is_finite() || v < 0.0 {
return Err(EncodeError::Unsupported(
"f32 lossless v1 supports only finite non-negative values",
));
}
}
let nchan = if has_alpha { 4 } else { 3 };
let mut image3s = Image3Si::new(width, height);
let mut alpha_bits: Vec<i32> = if has_alpha {
Vec::with_capacity(width * height)
} else {
Vec::new()
};
for (y, row) in input.chunks_exact(width * nchan).enumerate() {
let [r_row, g_row, b_row] = image3s.all_plane_rows_mut(y);
for (x, src) in row.chunks_exact(nchan).enumerate() {
r_row[x] = src[0].to_bits() as i32;
g_row[x] = src[1].to_bits() as i32;
b_row[x] = src[2].to_bits() as i32;
if has_alpha {
alpha_bits.push(src[3].to_bits() as i32);
}
}
}
let alpha = if has_alpha {
Some(AlphaPlane::F32(alpha_bits))
} else {
None
};
let mut w = BitWriter::new();
w.write(8, 0xFF);
w.write(8, CODESTREAM_MARKER as u64);
write_size_header(width, height, &mut w);
{
let mut metadata_scratch = Box::new(CoderScratch::lossless());
write_image_metadata(
&ImageMetadataParams {
tone_mapping: config.tone_mapping(),
color_encoding: &config.color_encoding,
alpha: alpha.as_ref(),
black: false,
icc_profile: config.icc_profile.as_deref(),
bps: BitsPerSample::F32,
lossless: true,
xyb_encoded: false,
xyb: &XybMatrix::SPEC,
grayscale: false,
orientation: config.orientation,
lossy_modular_16: false,
extra_16: false,
upsampling_kernel: None,
},
&mut metadata_scratch,
&mut w,
);
}
encode_frame_lossless_float(&image3s, alpha.as_ref(), config.num_threads, &mut w);
let codestream = w.into_bytes();
let alpha_bits_md = if has_alpha { 32 } else { 0 };
let gain_map = encode_gain_map(config)?;
finalize_container(
codestream,
config.exif.as_deref(),
config.xmp.as_deref(),
config.brotli_compression.as_deref(),
needs_level_10(32, true, alpha_bits_md),
gain_map.as_ref(),
)
}
fn encode_float_rgba(
input: &[f32],
width: usize,
height: usize,
has_alpha: bool,
config: &EncodeConfig,
bps: BitsPerSample,
) -> Result<Vec<u8>, EncodeError> {
let expected = checked_buffer_size::<f32>(width, height, if has_alpha { 4 } else { 3 })?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected,
actual: input.len(),
});
}
if config.lossless && matches!(bps, BitsPerSample::F32) {
return encode_f32_lossless_rgba(input, width, height, has_alpha, config);
}
let distance = config.distance.max(MIN_DISTANCE);
let ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
let mut scratch = Box::<CoderScratch>::default();
let transform = ColorTransform::for_float(
config.color_encoding.transfer,
config.color_encoding.primaries,
config.intensity_target,
);
if has_alpha {
let linear = linearize_rgb::<_, _, 4>(input, width, height, &ctx, &mut scratch, &transform);
let mut ctx = ctx;
ctx.set_coding(config.coding_transform, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
let alpha_plane = input
.as_chunks::<4>()
.0
.iter()
.map(|px| (px[3].clamp(0.0, 1.0) * 65535.0 + 0.5) as u16)
.collect();
let cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_alpha(AlphaPlane::from_u16_16bit(alpha_plane))
.with_bits_per_sample(bps)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
encode_samples_lossy(
Samples::F32 {
data: input,
stride: 4,
},
false,
linear,
&cfg,
&ctx,
&mut scratch,
)
} else {
let linear = linearize_rgb::<_, _, 3>(input, width, height, &ctx, &mut scratch, &transform);
let mut ctx = ctx;
ctx.set_coding(config.coding_transform, distance);
apply_yellow_opsin(&mut ctx, &linear, distance);
let cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_bits_per_sample(bps)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
encode_samples_lossy(
Samples::F32 {
data: input,
stride: 3,
},
false,
linear,
&cfg,
&ctx,
&mut scratch,
)
}
}
fn encode_float_gray(
luma: &[f32],
width: usize,
height: usize,
config: &EncodeConfig,
bps: BitsPerSample,
) -> Result<Vec<u8>, EncodeError> {
let expected = checked_buffer_size::<f32>(width, height, 1)?;
if luma.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected: width * height,
actual: luma.len(),
});
}
let distance = config.distance.max(MIN_DISTANCE);
let mut ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
ctx.set_coding(config.coding_transform, distance);
let mut scratch = Box::<CoderScratch>::default();
let decoder =
FloatTransferDecoder::for_gray(config.color_encoding.transfer, config.intensity_target);
let linear = linearize_gray::<_, _, 1>(luma, width, height, &ctx, &mut scratch, &decoder);
let cfg = EncodeConfigImpl::with_distance(distance)
.with_progressive_from(config)
.with_grayscale(true)
.with_bits_per_sample(bps)
.with_icc_profile(config.icc_profile.clone())
.with_exif(config.exif.clone())
.with_gain_map(encode_gain_map(config)?)
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_color_encoding(config.color_encoding)
.with_intensity_target(config.intensity_target)
.with_num_threads(config.num_threads);
encode_samples_lossy(
Samples::F32 {
data: luma,
stride: 1,
},
true,
linear,
&cfg,
&ctx,
&mut scratch,
)
}
pub fn encode_image_f32(
input: &[f32],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_f32(input, width, height, config)
});
}
encode_float_rgba(input, width, height, false, config, BitsPerSample::F32)
}
pub fn encode_image_f16(
input: &[f32],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_f16(input, width, height, config)
});
}
encode_float_rgba(input, width, height, false, config, BitsPerSample::F16)
}
pub fn encode_image_with_alpha_f32(
input: &[f32],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_with_alpha_f32(input, width, height, config)
});
}
encode_float_rgba(input, width, height, true, config, BitsPerSample::F32)
}
pub fn encode_image_with_alpha_f16(
input: &[f32],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Rgb) {
return with_prepared_config(config, IccTarget::Rgb, |config| {
encode_image_with_alpha_f16(input, width, height, config)
});
}
encode_float_rgba(input, width, height, true, config, BitsPerSample::F16)
}
pub fn encode_image_gray_f32(
input: &[f32],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_f32(input, width, height, config)
});
}
encode_float_gray(input, width, height, config, BitsPerSample::F32)
}
pub fn encode_image_gray_f16(
input: &[f32],
width: usize,
height: usize,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if needs_preparing(config, IccTarget::Gray) {
return with_prepared_config(config, IccTarget::Gray, |config| {
encode_image_gray_f16(input, width, height, config)
});
}
encode_float_gray(input, width, height, config, BitsPerSample::F16)
}
pub(crate) fn progressive_schedule(
progressive: bool,
passes: Option<u32>,
shifts: Option<&[u32]>,
) -> Vec<u32> {
if let Some(s) = shifts {
let valid = !s.is_empty()
&& s.len() <= 11
&& *s.last().unwrap() == 0
&& s.iter().all(|&v| v <= 3)
&& s.array_windows::<2>().all(|w| w[0] >= w[1]);
if valid {
return s.to_vec();
}
}
let n = passes
.map(|p| p.clamp(1, 4))
.unwrap_or(if progressive { 2 } else { 1 }) as usize;
(0..n).rev().map(|p| p as u32).collect()
}
enum Samples<'a> {
U8 { data: &'a [u8], stride: usize },
U16 {
data: &'a [u16],
stride: usize,
max: u16,
},
F32 { data: &'a [f32], stride: usize },
}
impl Samples<'_> {
#[inline]
fn get(&self, px: usize, c: usize) -> f32 {
match *self {
Samples::U8 { data, stride } => f32::from(data[px * stride + c]) * (1.0 / 255.0),
Samples::U16 { data, stride, max } => {
f32::from(data[px * stride + c].min(max)) / f32::from(max)
}
Samples::F32 { data, stride } => data[px * stride + c],
}
}
}
fn encode_samples_lossy(
samples: Samples<'_>,
gray: bool,
linear: Image3F,
config: &EncodeConfigImpl,
ctx: &EncodingContext,
scratch: &mut CoderScratch,
) -> Result<Vec<u8>, EncodeError> {
if ctx.coding.is_xyb() {
return encode_with_context(linear, config, ctx, scratch);
}
let (width, height) = (linear.xsize(), linear.ysize());
let mut coded = Image3F::try_new(width, height)?;
for y in 0..height {
let [p0, p1, p2] = coded.all_plane_rows_mut(y);
for x in 0..width {
let px = y * width + x;
let [r, g, b] = if gray {
[samples.get(px, 0); 3]
} else {
[samples.get(px, 0), samples.get(px, 1), samples.get(px, 2)]
};
[p0[x], p1[x], p2[x]] = ctx.coding.code(r, g, b);
}
}
encode_with_coded_planes(linear, Some(coded), config, ctx, scratch)
}
fn encode_with_context(
input: Image3F,
config: &EncodeConfigImpl,
ctx: &EncodingContext,
scratch: &mut CoderScratch,
) -> Result<Vec<u8>, EncodeError> {
encode_with_coded_planes(input, None, config, ctx, scratch)
}
pub(crate) fn encode_with_coded_planes(
input: Image3F,
coded: Option<Image3F>,
config: &EncodeConfigImpl,
ctx: &EncodingContext,
scratch: &mut CoderScratch,
) -> Result<Vec<u8>, EncodeError> {
let (width, height) = (input.xsize(), input.ysize());
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
if !config.distance.is_finite() || config.distance <= 0.0 {
return Err(EncodeError::InvalidDistance(config.distance));
}
if let Some(alpha) = config.alpha.as_ref() {
let expected = match &alpha {
AlphaPlane::U8(_) => checked_buffer_size::<u8>(width, height, 1)?,
AlphaPlane::U16 { .. } => checked_buffer_size::<u16>(width, height, 1)?,
AlphaPlane::F32(_) => checked_buffer_size::<f32>(width, height, 1)?,
};
if alpha.len() != expected {
return Err(EncodeError::AlphaSizeMismatch {
expected,
actual: alpha.len(),
});
}
}
let distance = config.distance.max(MIN_DISTANCE);
let mut w = BitWriter::new();
w.write(8, 0xFF);
w.write(8, CODESTREAM_MARKER as u64);
write_size_header(width, height, &mut w);
let coeff_shifts = progressive_schedule(
config.progressive,
config.progressive_passes,
config.progressive_shifts.as_deref(),
);
let mut frame = BitWriter::new();
let lossy_modular_16 = encode_frame(
ctx,
scratch,
distance,
input,
coded,
config.grayscale,
config.alpha.as_ref(),
config.fill_invisible && !config.lossless && !config.black,
&coeff_shifts,
config.patches,
!config.bits_per_sample.is_float(),
&mut frame,
)?;
write_image_metadata(
&ImageMetadataParams {
tone_mapping: config.tone_mapping(),
color_encoding: &config.color_encoding,
alpha: config.alpha.as_ref(),
black: config.black,
icc_profile: config.icc_profile.as_deref(),
bps: config.bits_per_sample,
lossless: config.lossless,
xyb_encoded: !config.lossless && ctx.coding.is_xyb(),
xyb: &ctx.xyb,
grayscale: config.grayscale,
orientation: config.orientation,
lossy_modular_16,
extra_16: lossy_modular_16 && ctx.extra_squeeze_distance > 0.0,
upsampling_kernel: None,
},
scratch,
&mut w,
);
w.append(&frame);
let codestream = w.into_bytes();
let alpha_bits = config.alpha.as_ref().map(|a| a.bits() as u32).unwrap_or(0);
finalize_container(
codestream,
config.exif.as_deref(),
config.xmp.as_deref(),
config.brotli_compression.as_deref(),
needs_level_10(config.bits_per_sample.bits(), config.lossless, alpha_bits) || config.black,
config.gain_map.as_ref(),
)
}
pub(crate) trait AsSignedInt {
fn to_signed_int(self, max_bp: u8) -> i32;
}
impl AsSignedInt for u8 {
#[inline]
fn to_signed_int(self, _: u8) -> i32 {
self as i32
}
}
impl AsSignedInt for u16 {
#[inline]
fn to_signed_int(self, max_bp: u8) -> i32 {
let max_colors = ((1u32 << max_bp) - 1) as i32;
(self as i32).min(max_colors)
}
}
pub(crate) fn encode_with_config_loseless<T: AsSignedInt + Copy>(
input: &[T],
width: usize,
height: usize,
has_alpha: bool,
max_bp: u8,
config: &EncodeConfigImpl,
) -> Result<Vec<u8>, EncodeError> {
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
let expected = checked_buffer_size::<T>(width, height, if has_alpha { 4 } else { 3 })?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected,
actual: input.len(),
});
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
if !config.distance.is_finite() || config.distance <= 0.0 {
return Err(EncodeError::InvalidDistance(config.distance));
}
let mut image3s = Image3Si::new(width, height);
let mut alpha_plane: Option<AlphaPlane> = None;
if has_alpha {
if max_bp > 8 {
let mut new_alpha_plane = vec![0u16; width * height];
for (y, (row, alpha_row)) in input
.chunks_exact(width * 4)
.zip(new_alpha_plane.chunks_exact_mut(width))
.enumerate()
{
let [r_row, g_row, b_row] = image3s.all_plane_rows_mut(y);
for ((((r, g), b), src), alpha) in r_row
.iter_mut()
.zip(g_row.iter_mut())
.zip(b_row.iter_mut())
.zip(row.as_chunks::<4>().0.iter())
.zip(alpha_row.iter_mut())
{
*r = src[0].to_signed_int(max_bp);
*g = src[1].to_signed_int(max_bp);
*b = src[2].to_signed_int(max_bp);
*alpha = src[3].to_signed_int(max_bp) as u16;
}
}
alpha_plane = Some(AlphaPlane::U16 {
data: new_alpha_plane,
bits: max_bp,
});
} else {
let mut new_alpha_plane = vec![0u8; width * height];
for (y, (row, alpha_row)) in input
.chunks_exact(width * 4)
.zip(new_alpha_plane.chunks_exact_mut(width))
.enumerate()
{
let [r_row, g_row, b_row] = image3s.all_plane_rows_mut(y);
for ((((r, g), b), src), alpha) in r_row
.iter_mut()
.zip(g_row.iter_mut())
.zip(b_row.iter_mut())
.zip(row.as_chunks::<4>().0.iter())
.zip(alpha_row.iter_mut())
{
*r = src[0].to_signed_int(max_bp);
*g = src[1].to_signed_int(max_bp);
*b = src[2].to_signed_int(max_bp);
*alpha = src[3].to_signed_int(max_bp) as u8;
}
}
alpha_plane = Some(AlphaPlane::U8(new_alpha_plane));
}
} else {
for (y, row) in input.chunks_exact(width * 3).enumerate() {
let [r_row, g_row, b_row] = image3s.all_plane_rows_mut(y);
for (((r, g), b), src) in r_row
.iter_mut()
.zip(g_row.iter_mut())
.zip(b_row.iter_mut())
.zip(row.as_chunks::<3>().0.iter())
{
*r = src[0].to_signed_int(max_bp);
*g = src[1].to_signed_int(max_bp);
*b = src[2].to_signed_int(max_bp);
}
}
}
if config.near_lossless > 0 {
let max_value = ((1u32 << max_bp) - 1) as i32;
crate::lossless::quantize_planes(&mut image3s, max_value, i32::from(config.near_lossless));
}
forward_ycocg_in_place(image3s.all_planes_mut().map(|plane| plane.as_mut_slice()));
encode_lossless_planes(&image3s, &alpha_plane, max_bp, config)
}
fn encode_lossless_planes(
image3s: &Image3Si,
alpha_plane: &Option<AlphaPlane>,
max_bp: u8,
config: &EncodeConfigImpl,
) -> Result<Vec<u8>, EncodeError> {
let (width, height) = (image3s.xsize(), image3s.ysize());
let mut w = BitWriter::new();
w.write(8, 0xFF);
w.write(8, CODESTREAM_MARKER as u64);
write_size_header(width, height, &mut w);
let upsampling = if config.upsampling && !config.progressive {
crate::upsampling::constant_cell_factor(image3s, alpha_plane.as_ref())
} else {
1
};
let downsampled = (upsampling > 1)
.then(|| crate::upsampling::downsample(image3s, alpha_plane.as_ref(), upsampling));
let kernel = (upsampling > 1).then(|| crate::upsampling::nearest_weights(upsampling));
let (image3s, alpha_plane) = match &downsampled {
Some((base, base_alpha)) => (base, base_alpha),
None => (image3s, alpha_plane),
};
let write_metadata = |alpha: Option<&AlphaPlane>, kernel: Option<&[f32]>, w: &mut BitWriter| {
let mut metadata_scratch = Box::new(CoderScratch::lossless());
write_image_metadata(
&ImageMetadataParams {
tone_mapping: config.tone_mapping(),
color_encoding: &config.color_encoding,
alpha,
black: config.black,
icc_profile: config.icc_profile.as_deref(),
bps: config.bits_per_sample,
lossless: config.lossless,
xyb_encoded: !config.lossless,
xyb: &XybMatrix::SPEC,
grayscale: config.grayscale,
orientation: config.orientation,
lossy_modular_16: false,
extra_16: false,
upsampling_kernel: kernel,
},
&mut metadata_scratch,
w,
);
};
write_metadata(alpha_plane.as_ref(), kernel.as_deref(), &mut w);
let alpha_bits = alpha_plane.as_ref().map(|a| a.bits() as u32).unwrap_or(0);
let eff_bits = (max_bp as u32).max(alpha_bits);
let num_color = if config.grayscale { 1 } else { 3 };
encode_frame_lossless(
image3s,
alpha_plane.as_ref(),
crate::lossless::LosslessSettings {
max_bits: eff_bits,
progressive: config.progressive,
num_color,
speed: config.speed,
decoding_speed: config.decoding_speed,
upsampling,
near_lossless: config.near_lossless > 0,
},
config.patches,
config.num_threads,
&mut w,
);
let opaque =
|alpha: &AlphaPlane| matches!(alpha, AlphaPlane::U8(a) if a.iter().all(|&v| v == 255));
let cell_plan = (config.upsampling
&& upsampling == 1
&& alpha_plane.as_ref().is_none_or(opaque)
&& max_bp == 8
&& !config.grayscale
&& !config.progressive)
.then(|| crate::cell_layer::plan(image3s))
.flatten();
if let Some(plan) = cell_plan {
let (base, base_alpha) = crate::cell_layer::base(image3s, &plan);
let (layer, layer_alpha) = crate::cell_layer::layer(image3s, &plan);
let layer_kernel = crate::upsampling::nearest_weights(plan.factor as u32);
let mut layered = BitWriter::new();
layered.write(8, 0xFF);
layered.write(8, CODESTREAM_MARKER as u64);
write_size_header(width, height, &mut layered);
write_metadata(Some(&base_alpha), Some(&layer_kernel), &mut layered);
let pool = crate::thread_pool::ThreadPool::new_lossless(config.num_threads);
let mut scratch = Box::new(CoderScratch::lossless());
crate::lossless::encode_frame_lossless_base(
&base,
&base_alpha,
config.patches,
config.speed,
config.decoding_speed,
config.near_lossless > 0,
&pool,
&mut scratch,
&mut layered,
);
crate::lossless::encode_frame_lossless_core(
&layer,
Some(&layer_alpha),
crate::lossless::LosslessSettings {
upsampling: plan.factor as u32,
near_lossless: config.near_lossless > 0,
..crate::lossless::LosslessSettings::rgb8(config.speed, config.decoding_speed)
},
crate::patches::ModularFrameKind::RgbBlendCrop {
x0: plan.x0,
y0: plan.y0,
width: plan.width,
height: plan.height,
},
true,
&pool,
&mut scratch,
&mut layered,
);
if layered.bits_written() < w.bits_written() {
w = layered;
}
}
let codestream = w.into_bytes();
let alpha_bits = alpha_plane.as_ref().map(|a| a.bits() as u32).unwrap_or(0);
finalize_container(
codestream,
config.exif.as_deref(),
config.xmp.as_deref(),
config.brotli_compression.as_deref(),
needs_level_10(max_bp as u32, true, alpha_bits) || config.black,
config.gain_map.as_ref(),
)
}
fn write_size(size: u32, w: &mut BitWriter) {
let size_minus_one = size - 1;
const BUCKET_BITS: [u32; 4] = [9, 13, 18, 30];
for (selector, &bits) in BUCKET_BITS.iter().enumerate() {
if size_minus_one < (1 << bits) {
w.write(2, selector as u64);
w.write(bits as usize, size_minus_one as u64);
return;
}
}
unreachable!("dimension was bounds-checked against MAX_DIMENSION");
}
fn write_size_header(xsize: usize, ysize: usize, w: &mut BitWriter) {
assert!(
xsize <= MAX_DIMENSION && ysize <= MAX_DIMENSION,
"image too large: max dimension is {MAX_DIMENSION}"
);
w.write(1, 0); write_size(ysize as u32, w);
w.write(3, 0); write_size(xsize as u32, w);
}
pub(crate) fn needs_level_10(bits: u32, lossless: bool, alpha_bits: u32) -> bool {
(lossless && bits >= 16) || alpha_bits >= 16
}
fn push_container_box(out: &mut Vec<u8>, kind: &[u8; 4], contents: &[u8]) {
let size = 8u64 + contents.len() as u64;
if size <= u32::MAX as u64 {
out.extend_from_slice(&(size as u32).to_be_bytes());
out.extend_from_slice(kind);
} else {
out.extend_from_slice(&1u32.to_be_bytes());
out.extend_from_slice(kind);
out.extend_from_slice(&(size + 8).to_be_bytes());
}
out.extend_from_slice(contents);
}
fn push_metadata_box(
out: &mut Vec<u8>,
kind: &[u8; 4],
contents: &[u8],
compressor: Option<&dyn BrotliCompression>,
) -> Result<(), EncodeError> {
if let Some(compressor) = compressor {
let compressed = compressor.compress(contents)?;
let mut brob = Vec::with_capacity(4 + compressed.len());
brob.extend_from_slice(kind);
brob.extend_from_slice(&compressed);
push_container_box(out, b"brob", &brob);
} else {
push_container_box(out, kind, contents);
}
Ok(())
}
pub(crate) fn wrap_jxl_container(
codestream: Vec<u8>,
level: u8,
exif: Option<&[u8]>,
xmp: Option<&[u8]>,
compressor: Option<&dyn BrotliCompression>,
gain_map: Option<&[u8]>,
) -> Result<Vec<u8>, EncodeError> {
let exif_extra = exif.map(|e| e.len() + 12).unwrap_or(0);
let xmp_extra = xmp.map(|x| x.len() + 8).unwrap_or(0);
let gain_map_extra = gain_map.map(|g| g.len() + 8).unwrap_or(0);
let mut out =
Vec::with_capacity(codestream.len() + 41 + exif_extra + xmp_extra + gain_map_extra);
out.extend_from_slice(&[
0, 0, 0, 0x0C, b'J', b'X', b'L', b' ', 0x0D, 0x0A, 0x87, 0x0A,
]);
out.extend_from_slice(&[
0, 0, 0, 0x14, b'f', b't', b'y', b'p', b'j', b'x', b'l', b' ', 0, 0, 0, 0, b'j', b'x',
b'l', b' ',
]);
out.extend_from_slice(&[0, 0, 0, 0x09, b'j', b'x', b'l', b'l', level]);
push_container_box(&mut out, b"jxlc", &codestream);
if let Some(g) = gain_map {
push_container_box(&mut out, b"jhgm", g);
}
if let Some(e) = exif {
let mut contents = Vec::with_capacity(4 + e.len());
contents.extend_from_slice(&0u32.to_be_bytes()); contents.extend_from_slice(e);
push_metadata_box(&mut out, b"Exif", &contents, compressor)?;
}
if let Some(x) = xmp {
push_metadata_box(&mut out, b"xml ", x, compressor)?;
}
Ok(out)
}
fn finalize_container(
codestream: Vec<u8>,
exif: Option<&[u8]>,
xmp: Option<&[u8]>,
compressor: Option<&dyn BrotliCompression>,
need_l10: bool,
gain_map: Option<&EncodedGainMap>,
) -> Result<Vec<u8>, EncodeError> {
let need_l10 = need_l10 || gain_map.is_some_and(|g| g.needs_level_10);
if need_l10 || exif.is_some() || xmp.is_some() || gain_map.is_some() {
wrap_jxl_container(
codestream,
if need_l10 { 10 } else { 5 },
exif,
xmp,
compressor,
gain_map.map(|g| g.bundle.as_slice()),
)
} else {
Ok(codestream)
}
}
#[derive(Clone, Copy)]
pub(crate) enum IccTarget {
Rgb,
Gray,
Keep,
}
fn describable_icc(config: &EncodeConfig, target: IccTarget) -> Option<RenderingIntent> {
let grayscale = match target {
IccTarget::Rgb => false,
IccTarget::Gray => true,
IccTarget::Keep => return None,
};
if config.lossless || config.coding_transform != CodingTransform::Xyb {
return None;
}
crate::icc_match::matching_intent(
config.icc_profile.as_deref()?,
&config.color_encoding,
grayscale,
)
}
pub(crate) fn needs_preparing(config: &EncodeConfig, target: IccTarget) -> bool {
config.gain_map.is_some() || describable_icc(config, target).is_some()
}
pub(crate) fn with_prepared_config(
config: &EncodeConfig,
target: IccTarget,
encode: impl FnOnce(&EncodeConfig) -> Result<Vec<u8>, EncodeError>,
) -> Result<Vec<u8>, EncodeError> {
if let Some(gain_map) = &config.gain_map {
gain_map.validate()?;
}
let mut inner = config.clone();
inner.gain_map = None;
if let Some(intent) = describable_icc(config, target) {
inner.icc_profile = None;
inner.color_encoding.rendering_intent = intent;
}
let bytes = encode(&inner)?;
drop(inner);
match encode_gain_map(config)? {
Some(gain_map) => attach_gain_map(bytes, &gain_map),
None => Ok(bytes),
}
}
fn attach_gain_map(bytes: Vec<u8>, gain_map: &EncodedGainMap) -> Result<Vec<u8>, EncodeError> {
if !bytes.starts_with(&crate::gain_map::CONTAINER_SIGNATURE) {
return finalize_container(bytes, None, None, None, false, Some(gain_map));
}
let mut out = Vec::with_capacity(bytes.len() + gain_map.bundle.len() + 32);
let mut attached = false;
let mut leveled = false;
let mut pos = 0usize;
while pos < bytes.len() {
let malformed = EncodeError::GainMap("internal: malformed container before the gain map");
if pos + 8 > bytes.len() {
return Err(malformed);
}
let small = u32::from_be_bytes(bytes[pos..pos + 4].try_into().unwrap()) as usize;
let kind: [u8; 4] = bytes[pos + 4..pos + 8].try_into().unwrap();
let (header, size) = match small {
0 => (8, bytes.len() - pos),
1 => {
if pos + 16 > bytes.len() {
return Err(malformed);
}
let large = u64::from_be_bytes(bytes[pos + 8..pos + 16].try_into().unwrap());
(16, usize::try_from(large).map_err(|_| malformed.clone())?)
}
n => (8, n),
};
if size < header || pos + size > bytes.len() {
return Err(malformed);
}
let payload = &bytes[pos + header..pos + size];
match &kind {
b"jxll" if gain_map.needs_level_10 => {
push_container_box(&mut out, b"jxll", &[10]);
leveled = true;
}
b"jxlc" => {
if gain_map.needs_level_10 && !leveled {
push_container_box(&mut out, b"jxll", &[10]);
leveled = true;
}
push_container_box(&mut out, b"jxlc", payload);
push_container_box(&mut out, b"jhgm", &gain_map.bundle);
attached = true;
}
_ => out.extend_from_slice(&bytes[pos..pos + size]),
}
pos += size;
}
if attached {
Ok(out)
} else {
Err(EncodeError::GainMap(
"internal: gain map container without a jxlc box",
))
}
}
fn write_int_bit_depth(bits: u32, w: &mut BitWriter) {
w.write(1, 0); match bits {
8 => w.write(2, 0),
10 => w.write(2, 1),
12 => w.write(2, 2),
_ => {
w.write(2, 3); w.write(6, (bits - 1) as u64);
}
}
}
fn write_float_bit_depth(bits: u32, exp_bits: u32, w: &mut BitWriter) {
w.write(1, 1); match bits {
32 => w.write(2, 0),
16 => w.write(2, 1),
24 => w.write(2, 2),
_ => {
w.write(2, 3);
w.write(6, (bits - 1) as u64);
}
}
w.write(4, (exp_bits - 1) as u64);
}
#[derive(Clone, Copy)]
struct ImageMetadataParams<'a> {
tone_mapping: ToneMappingParams,
color_encoding: &'a ColorEncoding,
alpha: Option<&'a AlphaPlane>,
black: bool,
icc_profile: Option<&'a [u8]>,
bps: BitsPerSample,
lossless: bool,
xyb_encoded: bool,
xyb: &'a XybMatrix,
grayscale: bool,
orientation: Orientation,
lossy_modular_16: bool,
extra_16: bool,
upsampling_kernel: Option<&'a [f32]>,
}
fn write_image_metadata(
params: &ImageMetadataParams<'_>,
scratch: &mut CoderScratch,
w: &mut BitWriter,
) {
let ImageMetadataParams {
tone_mapping: tm,
color_encoding,
alpha,
black,
icc_profile,
bps,
lossless,
xyb_encoded,
xyb,
grayscale,
orientation,
lossy_modular_16,
extra_16,
upsampling_kernel,
} = *params;
let tm = ToneMappingParams {
intensity_target: tm
.intensity_target
.or_else(|| crate::color::default_intensity_target(color_encoding.transfer)),
..tm
};
let bps = if lossy_modular_16 {
BitsPerSample::Sixteen
} else {
bps
};
w.write(1, 0); let extra_fields = !tm.is_default() || orientation != Orientation::Normal;
w.write(1, if extra_fields { 1 } else { 0 }); if extra_fields {
w.write(3, orientation.to_u3()); w.write(1, 0); w.write(1, 0); w.write(1, 0); }
if bps.is_float() {
write_float_bit_depth(bps.bits(), bps.exp_bits(), w);
} else {
write_int_bit_depth(bps.bits(), w);
}
let alpha_bits = alpha.map(|a| a.bits() as u32).unwrap_or(0);
let needs_32 = (lossless && bps.bits() >= 16) || alpha_bits >= 16 || lossy_modular_16;
w.write(1, if needs_32 { 0 } else { 1 });
if let Some(alpha) = alpha {
w.write(2, 1); match alpha.bits() {
bits if black => {
w.write(1, 0); w.write(2, 2); w.write(4, 4 - 2); write_int_bit_depth(if extra_16 { 16 } else { bits as u32 }, w);
w.write(2, 0); w.write(2, 0); }
8 if !extra_16 => {
w.write(1, 1); }
bits => {
w.write(1, 0); w.write(2, 0); if alpha.is_float() {
write_float_bit_depth(32, 8, w); } else {
write_int_bit_depth(if extra_16 { 16 } else { bits as u32 }, w);
}
w.write(2, 0); w.write(2, 0); w.write(1, 0); }
}
} else {
w.write(2, 0); }
w.write(1, u64::from(xyb_encoded)); let want_icc = icc_profile.is_some();
write_color_encoding_with_icc(color_encoding, want_icc, grayscale, w);
if extra_fields {
w.write(1, 0);
let it = tm.intensity_target.unwrap_or(255.0);
w.write(16, crate::util::f32_to_f16_bits(it) as u64); w.write(16, crate::util::f32_to_f16_bits(tm.min_nits) as u64); w.write(1, if tm.relative_to_max_display { 1 } else { 0 }); w.write(16, crate::util::f32_to_f16_bits(tm.linear_below) as u64); }
w.write(2, 0); let explicit_opsin = xyb_encoded && !xyb.is_decoder_default();
let upsampling = upsampling_kernel.map_or(1, |kernel| match kernel.len() {
15 => 2,
55 => 4,
_ => 8,
});
if !explicit_opsin && upsampling == 1 {
w.write(1, 1); } else {
w.write(1, 0); if xyb_encoded && !explicit_opsin {
w.write(1, 1); } else if xyb_encoded {
w.write(1, 0); for &v in &xyb.inv {
w.write(16, crate::util::f32_to_f16_bits(v) as u64);
}
for _ in 0..3 {
w.write(16, crate::util::f32_to_f16_bits(-0.003_793_073_4) as u64);
}
for v in crate::group::EXPLICIT_QUANT_BIASES {
w.write(16, crate::util::f32_to_f16_bits(v) as u64);
}
}
if upsampling == 1 {
w.write(3, 0); } else {
w.write(3, (upsampling >> 1) as u64);
for &weight in upsampling_kernel.unwrap_or_default() {
w.write(16, crate::util::f32_to_f16_bits(weight) as u64);
}
}
}
if let Some(icc) = icc_profile {
crate::icc_codec::write_icc_stream(icc, &mut scratch.huffman_pool, w);
}
w.zero_pad_to_byte();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crop_base_staging_matches_filled_rgb_linearization() {
use crate::crop_frames::CropBox;
let (width, height) = (113, 71);
let input: Vec<u8> = (0..width * height * 3)
.map(|i| ((i * 31) ^ (i / 7)) as u8)
.collect();
let fills = [
(
CropBox {
x0: 11,
y0: 9,
width: 67,
height: 52,
},
[200, 100, 51],
),
(
CropBox {
x0: 57,
y0: 37,
width: 41,
height: 9,
},
[2, 5, 19],
),
];
let mut filled = input.clone();
for (bounds, color) in fills {
for y in bounds.y0..bounds.y0 + bounds.height {
filled.as_chunks_mut::<3>().0[y * width + bounds.x0..][..bounds.width].fill(color);
}
}
for threads in [1, 3] {
let config = EncodeConfig::default()
.with_speed(Speed::Slow)
.with_num_threads(threads);
let ctx = lossy_context(&config, 2.0, XybMatrix::SPEC, width * height);
let mut scratch = Box::<CoderScratch>::default();
for (transfer, primaries) in [
(crate::TransferFunction::Srgb, crate::Primaries::Bt709),
(crate::TransferFunction::Srgb, crate::Primaries::Smpte432),
(crate::TransferFunction::Hlg, crate::Primaries::Bt2020),
] {
let transform = ColorTransform::for_integer(8, transfer, primaries, None);
let expected = linearize_rgb::<_, _, 3>(
&filled,
width,
height,
&ctx,
&mut scratch,
&transform,
);
let staged = linearize_crop_base(
&input,
width,
height,
&fills,
&ctx,
&mut scratch,
&transform,
);
for c in 0..3 {
assert!(
staged
.plane_data(c)
.iter()
.zip(expected.plane_data(c))
.all(|(a, b)| a.to_bits() == b.to_bits()),
"{transfer:?} {primaries:?}, {threads} threads, plane {c}",
);
}
}
}
}
fn close(a: f32, b: f32) -> bool {
(a - b).abs() < 1e-4
}
#[test]
fn quality_mapping_anchor_points() {
assert!(close(distance_from_quality(100.0), 0.05));
assert!(close(distance_from_quality(99.0), 0.1));
assert!(close(distance_from_quality(90.0), 1.0));
assert!(close(distance_from_quality(30.0), 6.4));
let d25 = distance_from_quality(25.0);
assert!(d25 > 6.4 && d25 < 7.0, "q=25 -> {d25}");
assert!(close(distance_from_quality(0.0), 25.0));
assert!(close(distance_from_quality(-50.0), 25.0));
assert!(close(distance_from_quality(110.0), 0.05));
}
#[test]
fn quality_mapping_monotone() {
let mut prev = distance_from_quality(0.0);
for q in 1..=100 {
let d = distance_from_quality(q as f32);
assert!(d <= prev, "non-monotonic at q={q}: prev={prev}, d={d}");
prev = d;
}
}
#[test]
fn slow_encodes_short_wide_strips() {
for (width, height, distance) in [
(1598usize, 9usize, 0.5f32),
(1598, 1, 1.0),
(9, 1598, 3.0),
(777, 15, 1.0),
] {
let mut pixels = Vec::with_capacity(width * height * 3);
for y in 0..height {
for x in 0..width {
let v = ((x * 37 + y * 101) ^ (x * y)) as u8;
pixels.extend_from_slice(&[v, v.wrapping_mul(3), 255 - v]);
}
}
let config = EncodeConfig::default()
.with_distance(distance)
.with_speed(Speed::Slow);
let bytes = encode_image(&pixels, width, height, &config)
.unwrap_or_else(|e| panic!("{width}x{height} d={distance}: {e:?}"));
assert!(!bytes.is_empty());
}
}
#[test]
fn default_speed_is_fast() {
assert_eq!(Speed::default(), Speed::Fast);
assert_eq!(EncodeConfig::default().speed, Speed::Fast);
}
#[test]
fn invisible_fill_defaults_on_and_can_be_disabled() {
assert!(EncodeConfig::default().fill_invisible);
let config = EncodeConfig::default().with_fill_invisible(false);
assert!(!config.fill_invisible);
assert!(
!EncodeConfigImpl::default()
.with_progressive_from(&config)
.fill_invisible
);
}
#[test]
fn srgb_linearization_uses_requested_threads() {
use std::collections::HashSet;
use std::sync::{Barrier, Mutex};
let config = EncodeConfig::default().with_num_threads(4);
let ctx = lossy_context(&config, 1.0, XybMatrix::SPEC, 1);
let mut scratch = Box::<CoderScratch>::default();
let mut image = Image3F::new(8, 4);
let barrier = Barrier::new(4);
let threads = Mutex::new(HashSet::new());
for_each_linear_band(&mut image, &ctx, &mut scratch, |_start, _band| {
threads.lock().unwrap().insert(std::thread::current().id());
barrier.wait();
});
assert_eq!(threads.into_inner().unwrap().len(), 4);
}
#[test]
#[should_panic(expected = "quality must not be NaN")]
fn quality_nan_panics() {
let _ = distance_from_quality(f32::NAN);
}
}
#[cfg(test)]
mod encode_smoke_tests {
use super::*;
fn many_ink_half_coverage_fixture() -> Vec<u8> {
let mut rgb = vec![255u8; 256 * 256 * 3];
let mut slot = 0;
for r in [1u8, 65] {
for g in [1u8, 65] {
for b in [1u8, 65, 129, 193] {
let ink = [r, g, b];
let x0 = (slot % 4) * 64 + 5;
let y0 = (slot / 4) * 64 + 3;
for line in 0..3 {
for y in y0..y0 + 57 {
let x = x0 + line * 17 + (y - y0) / 16;
for dx in 0..5 {
let color = if dx == 0 || dx == 4 {
ink.map(|c| ((255 + u16::from(c)) / 2) as u8)
} else {
ink
};
let i = (y * 256 + x + dx) * 3;
rgb[i..i + 3].copy_from_slice(&color);
}
}
}
slot += 1;
}
}
}
rgb
}
fn antialiased_discs_fixture(size: usize) -> Vec<u8> {
let bg = [236.0f32, 240.0, 248.0];
let discs = [
(0.35, 0.4, 0.25, [40.0f32, 90.0, 200.0]),
(0.7, 0.65, 0.2, [230.0, 120.0, 30.0]),
];
let mut rgb = Vec::with_capacity(size * size * 3);
for y in 0..size {
for x in 0..size {
let mut color = bg;
for &(cx, cy, r, fill) in &discs {
let (dx, dy) = (
x as f32 + 0.5 - cx * size as f32,
y as f32 + 0.5 - cy * size as f32,
);
let edge = r * size as f32 - (dx * dx + dy * dy).sqrt();
let a = (edge + 0.5).clamp(0.0, 1.0);
for k in 0..3 {
color[k] += (fill[k] - color[k]) * a;
}
}
rgb.extend(color.map(|c| c.round() as u8));
}
}
rgb
}
#[test]
fn vector_layers_single_frame_never_exceeds_the_layered_encode() {
let size = 256;
let rgb = antialiased_discs_fixture(size);
let config = EncodeConfig::default()
.with_speed(Speed::Slow)
.with_num_threads(1);
let steps = crate::vector_layers::coverage_steps(1.5);
let split = crate::vector_layers::split(&rgb, size, size, steps).expect("flat art splits");
assert!(
split.coverage.iter().any(|&a| a != 0),
"the rims form a band"
);
let encoded = encode_vector_layers(split, size, size, &config).unwrap();
assert!(encoded.bytes.len() <= encoded.layered);
assert!(
encoded.bytes.len() < encoded.layered,
"{} vs {}",
encoded.bytes.len(),
encoded.layered
);
let source = crate::vector_layers::SplitSource::rgb(&rgb, size, size).unwrap();
let plan = source.prepare(None).unwrap();
let split = plan.split_for_retry(steps, true).expect("flat art splits");
let flattened = encode_vector_layers(split, size, size, &config).unwrap();
assert_eq!(flattened.bytes, encoded.bytes);
}
#[test]
fn high_quality_ink_palette_capacity_preserves_the_plain_path() {
let rgb = many_ink_half_coverage_fixture();
for distance in [0.05, 0.1, 0.3] {
let config = EncodeConfig::default()
.with_speed(Speed::Medium)
.with_distance(distance)
.with_num_threads(1)
.with_patches(false)
.with_vector_layers(false);
let inked = encode_image(&rgb, 256, 256, &config).unwrap();
let plain = encode_image(&rgb, 256, 256, &config.with_ink_layer(false)).unwrap();
assert_eq!(inked, plain, "distance {distance}");
}
}
#[test]
fn high_quality_ink_budget_cannot_fall_back_to_a_hard_mask() {
let rgb = many_ink_half_coverage_fixture();
let pool = crate::thread_pool::ThreadPool::new(1);
for (distance, accepted) in [(0.6, false), (1.0, true)] {
let plan = crate::ink_layer::InkCandidates::new_fast(&rgb, None, 256, 256, &pool)
.unwrap()
.select(distance, &pool)
.unwrap();
assert_eq!(plan.inks.len(), 16);
let config = EncodeConfig::default()
.with_speed(Speed::Medium)
.with_distance(distance)
.with_num_threads(1);
let stack = InkStack::encode(plan, 256, 256, &config, None, 0.0).unwrap();
assert_eq!(stack.is_some(), accepted, "distance {distance}");
}
}
fn clipped_text_strip_fixture() -> (Vec<u8>, usize, usize) {
let (width, height) = (288, 32);
let mut rgb = vec![255u8; width * height * 3];
let mut seed = 2u64;
let mut rnd = move || {
seed = (seed * 1_103_515_245 + 12_345) & 0x7fff_ffff;
(seed >> 16) as usize
};
let mut x = 2;
while x < width - 6 {
if rnd() % 4 != 0 {
for y in 0..7 - rnd() % 3 {
rgb[(y * width + x) * 3..][..9].fill(102);
}
}
x += 6 + rnd() % 4;
}
(rgb, width, height)
}
#[test]
fn icc_tagged_xyb_encodes_stay_single_frame() {
let rgb = many_ink_half_coverage_fixture();
let icc = b"\0\0\x02\x18appl\x04\0\0\0mntrRGB XYZ ".to_vec();
let layered_off =
|config: EncodeConfig| config.with_ink_layer(false).with_crop_frames(false);
let config = EncodeConfig::default()
.with_speed(Speed::Medium)
.with_distance(1.0)
.with_num_threads(1);
let plain = encode_image(&rgb, 256, 256, &config).unwrap();
let flat = encode_image(&rgb, 256, 256, &layered_off(config.clone())).unwrap();
assert_ne!(plain, flat, "the fixture must take a layered arm");
let tagged = config.with_icc_profile(icc);
assert!(!tagged.xyb_layers_decodable());
assert_eq!(
encode_image(&rgb, 256, 256, &tagged).unwrap(),
encode_image(&rgb, 256, 256, &layered_off(tagged.clone())).unwrap()
);
}
#[test]
fn describable_icc_becomes_the_enum_and_keeps_the_layers() {
use crate::icc_match::tests::synthetic_profile;
let rgb = many_ink_half_coverage_fixture();
let config = EncodeConfig::default()
.with_speed(Speed::Medium)
.with_distance(1.0)
.with_num_threads(1);
let encode = |config: &EncodeConfig| encode_image(&rgb, 256, 256, config).unwrap();
let srgb_icc = synthetic_profile(crate::Primaries::Bt709, None, &[], 0);
let mut perceptual = config.clone();
perceptual.color_encoding.rendering_intent = RenderingIntent::Perceptual;
let tagged = encode(&config.clone().with_icc_profile(srgb_icc.clone()));
assert_eq!(tagged, encode(&perceptual));
assert_ne!(
tagged,
encode(
&perceptual
.clone()
.with_ink_layer(false)
.with_crop_frames(false)
)
);
let p3_icc = synthetic_profile(crate::Primaries::Smpte431, None, &[], 0);
let mismatched = config.clone().with_icc_profile(p3_icc);
assert_eq!(
encode(&mismatched),
encode(
&mismatched
.clone()
.with_ink_layer(false)
.with_crop_frames(false)
)
);
assert_ne!(encode(&mismatched), encode(&perceptual));
let lossless = EncodeConfig::default()
.with_lossless(true)
.with_num_threads(1);
assert_ne!(
encode(&lossless.clone().with_icc_profile(srgb_icc)),
encode(&lossless)
);
}
#[test]
fn ink_frame_without_a_decisive_tree_encodes() {
let (rgb, width, height) = clipped_text_strip_fixture();
let pool = crate::thread_pool::ThreadPool::new(1);
for speed in [Speed::Medium, Speed::Slow] {
for distance in [0.5, 1.0, 2.0, 4.0] {
let config = EncodeConfig::default()
.with_speed(speed)
.with_distance(distance)
.with_num_threads(1);
encode_image(&rgb, width, height, &config)
.unwrap_or_else(|e| panic!("{speed:?} d{distance}: {e:?}"));
let Some(plan) =
crate::ink_layer::InkCandidates::new_fast(&rgb, None, width, height, &pool)
.and_then(|candidates| candidates.select(distance, &pool))
else {
continue;
};
InkStack::encode(plan, width, height, &config, None, f64::INFINITY)
.unwrap_or_else(|e| panic!("ink stack {speed:?} d{distance}: {e:?}"));
}
}
}
#[test]
fn palette_ink_is_deterministic_across_workers_and_preserves_plain_fallback() {
let (width, height) = (600, 130);
let rgb: Vec<_> = (0..height)
.flat_map(|y| {
(0..width).flat_map(move |x| {
if (8..122).contains(&y) && (x % 40 < 2) {
[16; 3]
} else if y % 31 < 2 {
[150, 20, 40]
} else {
[240; 3]
}
})
})
.collect();
let config = EncodeConfig::default()
.with_distance(1.5)
.with_ink_layer(true)
.with_patches(false);
let single =
encode_image(&rgb, width, height, &config.clone().with_num_threads(1)).unwrap();
let parallel =
encode_image(&rgb, width, height, &config.clone().with_num_threads(4)).unwrap();
assert_eq!(single, parallel);
let flat = vec![200; 64 * 64 * 3];
assert_eq!(
encode_image(&flat, 64, 64, &config).unwrap(),
encode_image(&flat, 64, 64, &config.with_ink_layer(false)).unwrap()
);
}
const W: usize = 16;
const H: usize = 16;
fn rgb8() -> Vec<u8> {
(0..W * H * 3).map(|i| (i % 256) as u8).collect()
}
fn box_payload<'a>(container: &'a [u8], wanted: &[u8; 4]) -> Option<&'a [u8]> {
let mut pos = 0usize;
while pos + 8 <= container.len() {
let small = u32::from_be_bytes(container[pos..pos + 4].try_into().unwrap()) as usize;
let kind: &[u8; 4] = container[pos + 4..pos + 8].try_into().unwrap();
let (header, size) = if small == 1 {
if pos + 16 > container.len() {
return None;
}
(
16,
u64::from_be_bytes(container[pos + 8..pos + 16].try_into().unwrap()) as usize,
)
} else {
(8, small)
};
if size < header || pos.checked_add(size)? > container.len() {
return None;
}
if kind == wanted {
return Some(&container[pos + header..pos + size]);
}
pos += size;
}
None
}
fn box_kinds(container: &[u8]) -> Vec<[u8; 4]> {
let mut kinds = Vec::new();
let mut pos = 0usize;
while pos + 8 <= container.len() {
let size = u32::from_be_bytes(container[pos..pos + 4].try_into().unwrap()) as usize;
kinds.push(container[pos + 4..pos + 8].try_into().unwrap());
assert!(size >= 8, "large boxes not expected in these tests");
pos += size;
}
assert_eq!(pos, container.len());
kinds
}
fn gain_map_metadata() -> crate::IsoGainMap {
crate::IsoGainMap::from_floats(&crate::GainMapFloats {
max: [2.0; 3],
alternate_hdr_headroom: 2.0,
..crate::GainMapFloats::default()
})
.unwrap()
}
fn gray_gain_map(w: usize, h: usize) -> crate::GainMap {
let gain: Vec<u8> = (0..w * h).map(|i| (i * 4) as u8).collect();
crate::GainMap::gray8(gain, w, h, gain_map_metadata())
}
#[test]
fn gain_map_is_written_to_a_jhgm_box_after_the_codestream() {
let md = gain_map_metadata();
let gm = gray_gain_map(8, 8)
.with_lossless(true)
.with_alternate_color_encoding(ColorEncoding::bt2020_pq());
let encoded = encode_image(&rgb8(), W, H, &lossy().with_gain_map(gm)).unwrap();
assert_eq!(
box_kinds(&encoded),
[*b"JXL ", *b"ftyp", *b"jxll", *b"jxlc", *b"jhgm"]
);
assert_eq!(box_payload(&encoded, b"jxll"), Some(&[5u8][..]));
let jhgm = box_payload(&encoded, b"jhgm").expect("jhgm box");
let p = crate::gain_map::parse_jhgm_bundle(jhgm).unwrap();
assert_eq!(p.version, 0);
assert_eq!(crate::IsoGainMap::from_metadata(p.metadata).unwrap(), md);
assert!(!p.color_encoding.is_empty());
assert!(p.alt_icc.is_empty());
assert_eq!(&p.codestream[..2], &[0xFF, 0x0A]);
assert_ne!(&p.codestream[..4], &[0, 0, 0, 0x0C]);
}
#[test]
fn gain_map_box_precedes_metadata_boxes() {
let exif = vec![b'I', b'I', 42, 0, 8, 0, 0, 0];
let encoded = encode_image(
&rgb8(),
W,
H,
&lossy()
.with_exif(exif.clone())
.with_xmp(b"<x/>".to_vec())
.with_gain_map(gray_gain_map(4, 4)),
)
.unwrap();
assert_eq!(
box_kinds(&encoded),
[
*b"JXL ", *b"ftyp", *b"jxll", *b"jxlc", *b"jhgm", *b"Exif", *b"xml "
]
);
assert_eq!(box_payload(&encoded, b"xml "), Some(&b"<x/>"[..]));
}
#[test]
fn gain_map_leaves_the_primary_encode_untouched() {
let gm = crate::GainMap::rgb8(
(0..W * H * 3).map(|i| (i * 5) as u8).collect(),
W,
H,
gain_map_metadata(),
);
for config in [lossy(), slow_lossy(1.5), lossless()] {
let with = config.clone().with_gain_map(gm.clone());
let encoded = encode_image(&rgb8(), W, H, &with).unwrap();
let plain = encode_image(&rgb8(), W, H, &config).unwrap();
let (codestream, _) = crate::gain_map::strip_container(plain).unwrap();
assert_eq!(box_payload(&encoded, b"jxlc"), Some(&codestream[..]));
let once = encode_gain_map(&with).unwrap().unwrap();
assert_eq!(box_payload(&encoded, b"jhgm"), Some(&once.bundle[..]));
}
}
#[test]
fn gain_map_attaches_after_a_codestream_box_running_to_the_end() {
let mut container = crate::gain_map::CONTAINER_SIGNATURE.to_vec();
push_container_box(&mut container, b"ftyp", b"jxl \0\0\0\0jxl ");
push_container_box(&mut container, b"jxll", &[5]);
container.extend_from_slice(&0u32.to_be_bytes());
container.extend_from_slice(b"jxlc");
container.extend_from_slice(&[0xFF, 0x0A, 1, 2, 3]);
let gain_map = EncodedGainMap {
bundle: vec![9, 8, 7],
needs_level_10: true,
};
let out = attach_gain_map(container, &gain_map).unwrap();
assert_eq!(
box_kinds(&out),
[*b"JXL ", *b"ftyp", *b"jxll", *b"jxlc", *b"jhgm"]
);
assert_eq!(box_payload(&out, b"jxll"), Some(&[10u8][..]));
assert_eq!(box_payload(&out, b"jxlc"), Some(&[0xFF, 0x0A, 1, 2, 3][..]));
assert_eq!(box_payload(&out, b"jhgm"), Some(&[9u8, 8, 7][..]));
}
#[test]
fn gain_map_attach_declares_level_10_when_the_container_has_no_level_box() {
let mut container = crate::gain_map::CONTAINER_SIGNATURE.to_vec();
push_container_box(&mut container, b"ftyp", b"jxl \0\0\0\0jxl ");
push_container_box(&mut container, b"jxlc", &[0xFF, 0x0A, 1]);
push_container_box(&mut container, b"Exif", &[0, 0, 0, 0, b'I', b'I']);
let level_10 = EncodedGainMap {
bundle: vec![9],
needs_level_10: true,
};
let out = attach_gain_map(container.clone(), &level_10).unwrap();
assert_eq!(
box_kinds(&out),
[*b"JXL ", *b"ftyp", *b"jxll", *b"jxlc", *b"jhgm", *b"Exif"]
);
assert_eq!(box_payload(&out, b"jxll"), Some(&[10u8][..]));
let level_5 = EncodedGainMap {
bundle: vec![9],
needs_level_10: false,
};
let out = attach_gain_map(container, &level_5).unwrap();
assert_eq!(
box_kinds(&out),
[*b"JXL ", *b"ftyp", *b"jxlc", *b"jhgm", *b"Exif"]
);
}
#[test]
fn level_10_gain_map_raises_the_file_level() {
let gain: Vec<u16> = (0..4 * 4).map(|i| (i * 4000) as u16).collect();
let gm = crate::GainMap::gray16(gain, 16, 4, 4, gain_map_metadata()).with_lossless(true);
let encoded = encode_image(&rgb8(), W, H, &lossy().with_gain_map(gm)).unwrap();
assert_eq!(box_payload(&encoded, b"jxll"), Some(&[10u8][..]));
let jhgm = box_payload(&encoded, b"jhgm").unwrap();
let p = crate::gain_map::parse_jhgm_bundle(jhgm).unwrap();
assert_eq!(&p.codestream[..2], &[0xFF, 0x0A]);
}
#[test]
fn gain_map_rides_along_every_primary_encoder_path() {
let cfg = lossy().with_gain_map(
crate::GainMap::rgb8(
(0..4 * 4 * 3).map(|i| i as u8).collect(),
4,
4,
gain_map_metadata(),
)
.with_distance(3.0),
);
let rgba: Vec<u8> = (0..W * H * 4).map(|i| i as u8).collect();
let gray: Vec<u8> = (0..W * H).map(|i| i as u8).collect();
let rgb16: Vec<u16> = (0..W * H * 3).map(|i| (i * 7) as u16).collect();
let f32s: Vec<f32> = (0..W * H * 3).map(|i| (i % 255) as f32 / 255.0).collect();
let outputs = [
encode_image_with_alpha(&rgba, W, H, &cfg).unwrap(),
encode_image_gray(&gray, W, H, &cfg).unwrap(),
encode_image_16bit(&rgb16, W, H, &cfg).unwrap(),
encode_image_f32(&f32s, W, H, &cfg).unwrap(),
encode_image(
&rgb8(),
W,
H,
&lossless().with_gain_map(gray_gain_map(4, 4)),
)
.unwrap(),
encode_image_16bit(&rgb16, W, H, &lossless().with_gain_map(gray_gain_map(4, 4)))
.unwrap(),
encode_image_f32(&f32s, W, H, &lossless().with_gain_map(gray_gain_map(4, 4))).unwrap(),
];
for out in &outputs {
let jhgm = box_payload(out, b"jhgm").expect("every path writes the jhgm box");
let p = crate::gain_map::parse_jhgm_bundle(jhgm).unwrap();
assert_eq!(&p.codestream[..2], &[0xFF, 0x0A]);
}
}
#[test]
fn invalid_gain_map_fails_the_encode() {
let gm = crate::GainMap::gray8(vec![0; 15], 4, 4, gain_map_metadata());
assert!(matches!(
encode_image(&rgb8(), W, H, &lossy().with_gain_map(gm)),
Err(EncodeError::GainMapSizeMismatch {
expected: 16,
actual: 15
})
));
}
#[test]
fn xmp_is_written_to_an_xml_box() {
let xmp = b"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"/>";
let encoded = encode_image(&rgb8(), W, H, &lossy().with_xmp(xmp.to_vec())).unwrap();
assert_eq!(box_payload(&encoded, b"xml "), Some(xmp.as_slice()));
}
#[test]
fn custom_brotli_writes_brob_metadata() {
struct MarkerCompressor;
impl BrotliCompression for MarkerCompressor {
fn compress(&self, data: &[u8]) -> Result<Vec<u8>, EncodeError> {
assert_eq!(data, b"<x:xmpmeta/>");
Ok(vec![0xAA, 0xBB])
}
}
let encoded = encode_image(
&rgb8(),
W,
H,
&lossy()
.with_xmp(b"<x:xmpmeta/>".to_vec())
.with_brotli_compression(Box::new(MarkerCompressor)),
)
.unwrap();
assert_eq!(box_payload(&encoded, b"brob"), Some(&b"xml \xAA\xBB"[..]));
assert!(box_payload(&encoded, b"xml ").is_none());
}
#[test]
fn custom_brotli_error_is_returned() {
struct FailingCompressor;
impl BrotliCompression for FailingCompressor {
fn compress(&self, _data: &[u8]) -> Result<Vec<u8>, EncodeError> {
Err(EncodeError::Brotli("backend failed".into()))
}
}
assert_eq!(
encode_image(
&rgb8(),
W,
H,
&lossy()
.with_xmp(b"<x:xmpmeta/>".to_vec())
.with_brotli_compression(Box::new(FailingCompressor)),
),
Err(EncodeError::Brotli("backend failed".into()))
);
}
fn rgba8() -> Vec<u8> {
(0..W * H * 4).map(|i| (i % 256) as u8).collect()
}
fn rgb16() -> Vec<u16> {
(0..W * H * 3).map(|i| (i % 65536) as u16).collect()
}
fn rgba16() -> Vec<u16> {
(0..W * H * 4).map(|i| (i % 65536) as u16).collect()
}
fn gray8() -> Vec<u8> {
(0..W * H).map(|i| (i % 256) as u8).collect()
}
fn gray_alpha8() -> Vec<u8> {
(0..W * H * 2).map(|i| (i % 256) as u8).collect()
}
fn gray16() -> Vec<u16> {
(0..W * H).map(|i| (i % 65536) as u16).collect()
}
fn gray_alpha16() -> Vec<u16> {
(0..W * H * 2).map(|i| (i % 65536) as u16).collect()
}
fn rgb10() -> Vec<u16> {
(0..W * H * 3).map(|i| (i % 1024) as u16).collect()
}
fn rgba10() -> Vec<u16> {
(0..W * H * 4).map(|i| (i % 1024) as u16).collect()
}
fn rgb12() -> Vec<u16> {
(0..W * H * 3).map(|i| (i % 4096) as u16).collect()
}
fn rgba12() -> Vec<u16> {
(0..W * H * 4).map(|i| (i % 4096) as u16).collect()
}
fn gray10() -> Vec<u16> {
(0..W * H).map(|i| (i % 1024) as u16).collect()
}
fn gray_alpha10() -> Vec<u16> {
(0..W * H * 2).map(|i| (i % 1024) as u16).collect()
}
fn gray12() -> Vec<u16> {
(0..W * H).map(|i| (i % 4096) as u16).collect()
}
fn gray_alpha12() -> Vec<u16> {
(0..W * H * 2).map(|i| (i % 4096) as u16).collect()
}
fn rgb_f32() -> Vec<f32> {
(0..W * H * 3).map(|i| (i % 256) as f32 / 255.0).collect()
}
fn rgba_f32() -> Vec<f32> {
(0..W * H * 4).map(|i| (i % 256) as f32 / 255.0).collect()
}
fn gray_f32() -> Vec<f32> {
(0..W * H).map(|i| (i % 256) as f32 / 255.0).collect()
}
fn lossy() -> EncodeConfig {
EncodeConfig::default().with_distance(1.0)
}
fn lossless() -> EncodeConfig {
EncodeConfig::default().with_lossless(true)
}
fn ok(r: Result<Vec<u8>, EncodeError>) {
let bytes = r.expect("encode failed");
assert!(!bytes.is_empty(), "encoded output is empty");
}
#[test]
fn rgb8_lossy() {
ok(encode_image(&rgb8(), W, H, &lossy()));
}
#[test]
fn progressive_dc_only_where_supported() {
let (w, h) = (160, 96);
let rgb: Vec<u8> = (0..w * h)
.flat_map(|i| {
let (x, y) = (i % w, i / w);
[(x + y) as u8, (2 * x) as u8, ((x * y) % 251) as u8]
})
.collect();
let rgba: Vec<u8> = rgb
.as_chunks::<3>()
.0
.iter()
.flat_map(|&[r, g, b]| [r, g, b, 200])
.collect();
let off = lossy().with_num_threads(1);
let on = off.clone().with_progressive_dc(true);
let full = encode_image(&rgb, w, h, &on).unwrap();
assert_ne!(encode_image(&rgb, w, h, &off).unwrap(), full);
let levels = |n| encode_image(&rgb, w, h, &on.clone().with_progressive_dc_levels(n));
assert_eq!(levels(99).unwrap(), full);
assert_eq!(levels(0).unwrap(), levels(1).unwrap());
assert_ne!(levels(1).unwrap(), full);
assert_eq!(
encode_image(&rgb8(), W, H, &off).unwrap(),
encode_image(&rgb8(), W, H, &on).unwrap()
);
assert_eq!(
encode_image_with_alpha(&rgba, w, h, &off).unwrap(),
encode_image_with_alpha(&rgba, w, h, &on).unwrap()
);
}
#[test]
fn invisible_fill_preserves_lossless_and_nontransparent_inputs() {
let on = lossy().with_num_threads(1);
let off = on.clone().with_fill_invisible(false);
assert_eq!(
encode_image(&rgb8(), W, H, &on).unwrap(),
encode_image(&rgb8(), W, H, &off).unwrap()
);
let mut rgba = rgba8();
for px in rgba.as_chunks_mut::<4>().0 {
px[3] = 128; }
for coding in [
CodingTransform::Xyb,
CodingTransform::Rgb,
CodingTransform::YCbCr,
] {
let on = on.clone().with_coding_transform(coding);
let off = off.clone().with_coding_transform(coding);
assert_eq!(
encode_image_with_alpha(&rgba, W, H, &on).unwrap(),
encode_image_with_alpha(&rgba, W, H, &off).unwrap()
);
}
let lossless_on = on.with_lossless(true);
let lossless_off = off.with_lossless(true);
assert_eq!(
encode_image_with_alpha(&rgba8(), W, H, &lossless_on).unwrap(),
encode_image_with_alpha(&rgba8(), W, H, &lossless_off).unwrap()
);
}
#[test]
fn public_lossy_paths_share_the_minimum_distance() {
for speed in [Speed::Fast, Speed::Slow] {
let at_floor = EncodeConfig::default()
.with_speed(speed)
.with_distance(0.05);
let below = at_floor.clone().with_distance(0.001);
macro_rules! same_output {
($encode:ident, $pixels:expr) => {
assert_eq!(
$encode(&$pixels, W, H, &below).unwrap(),
$encode(&$pixels, W, H, &at_floor).unwrap(),
"{}: speed={speed:?}",
stringify!($encode)
);
};
}
same_output!(encode_image, rgb8());
same_output!(encode_image_with_alpha, rgba8());
same_output!(encode_image_16bit, rgb16());
same_output!(encode_image_gray, gray8());
same_output!(encode_image_gray_16bit, gray16());
same_output!(encode_image_f32, rgb_f32());
same_output!(encode_image_gray_f32, gray_f32());
}
}
#[cfg(feature = "splines")]
fn line_art(size: usize) -> Vec<u8> {
let mut px = vec![0u8; size * size * 3];
for y in 0..size {
for x in 0..size {
let mut v = 150.0 + 60.0 * (x + y) as f32 / (2 * size) as f32;
for k in 0..6 {
let phase = k as f32 * 0.9;
let cy = size as f32 * (0.15 + 0.13 * k as f32)
+ 18.0 * (x as f32 * 0.035 + phase).sin();
let d = (y as f32 - cy).abs();
v -= 120.0 * (-0.5 * (d / 0.8).powi(2)).exp();
}
let i = (y * size + x) * 3;
px[i] = v.clamp(0.0, 255.0) as u8;
px[i + 1] = (v * 0.92).clamp(0.0, 255.0) as u8;
px[i + 2] = (v * 0.85).clamp(0.0, 255.0) as u8;
}
}
px
}
fn slow_lossy(distance: f32) -> EncodeConfig {
EncodeConfig::default()
.with_distance(distance)
.with_speed(Speed::Slow)
}
#[test]
#[cfg(feature = "splines")]
fn splines_reduce_thin_line_art() {
const S: usize = 256;
let pixels = line_art(S);
let plain = encode_image(&pixels, S, S, &slow_lossy(3.0)).unwrap();
let with = encode_image(&pixels, S, S, &slow_lossy(3.0).with_splines(true)).unwrap();
assert!(
with.len() * 100 < plain.len() * 90,
"splines should win clearly on thin line art: {} vs {}",
with.len(),
plain.len()
);
}
#[test]
#[cfg(feature = "splines")]
fn splines_are_inert_without_lines() {
const S: usize = 128;
let mut pixels = vec![0u8; S * S * 3];
for y in 0..S {
for x in 0..S {
let i = (y * S + x) * 3;
pixels[i] = (x * 2) as u8;
pixels[i + 1] = (y * 2) as u8;
pixels[i + 2] = (x + y) as u8;
}
}
let plain = encode_image(&pixels, S, S, &slow_lossy(2.0)).unwrap();
let with = encode_image(&pixels, S, S, &slow_lossy(2.0).with_splines(true)).unwrap();
assert_eq!(plain, with);
}
#[cfg(feature = "splines")]
fn star_field(size: usize) -> Vec<u8> {
let mut state = 0x2545_f491_u32;
let mut next = move || {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
state as f32 / u32::MAX as f32
};
let mut sky = vec![[0f32; 3]; size * size];
for (i, p) in sky.iter_mut().enumerate() {
let (x, y) = ((i % size) as f32, (i / size) as f32);
*p = [8.0 + 0.03 * x, 10.0 + 0.02 * y, 22.0 + 0.04 * (x + y)];
}
for _ in 0..size * size / 120 {
let (cx, cy) = (
6.0 + next() * (size - 12) as f32,
6.0 + next() * (size - 12) as f32,
);
let amp = 40.0 + 200.0 * next() * next();
let sigma = 0.6 + 0.4 * next();
let tint = [0.8 + 0.2 * next(), 0.9, 1.0];
for y in cy as usize - 4..=cy as usize + 4 {
for x in cx as usize - 4..=cx as usize + 4 {
let d2 = (x as f32 - cx).powi(2) + (y as f32 - cy).powi(2);
let v = amp * (-0.5 * d2 / (sigma * sigma)).exp();
for (c, t) in sky[y * size + x].iter_mut().zip(tint) {
*c += v * t;
}
}
}
}
sky.iter()
.flat_map(|p| p.map(|c| c.clamp(0.0, 255.0) as u8))
.collect()
}
#[test]
#[cfg(feature = "splines")]
fn dots_are_inert_without_points() {
const S: usize = 128;
let pixels = line_art(S);
let config = slow_lossy(2.0).with_splines(true);
assert_eq!(
encode_image(&pixels, S, S, &config).unwrap(),
encode_image(&pixels, S, S, &config.clone().with_dots(true)).unwrap()
);
}
#[test]
fn faint_smooth_tall_alpha_encodes() {
const W: usize = 400;
const H: usize = 850;
let rgba: Vec<u8> = (0..W * H)
.flat_map(|i| {
let (x, y) = ((i % W) as f32 / W as f32, (i / W) as f32 / H as f32);
let bump = (x * std::f32::consts::PI).sin() * (y * std::f32::consts::PI).sin();
[0, 0, 0, (51.0 * bump).round() as u8]
})
.collect();
for speed in [Speed::MediumSlow, Speed::Slow] {
let config = EncodeConfig::default()
.with_distance(3.0)
.with_speed(speed)
.with_num_threads(1);
encode_image_with_alpha(&rgba, W, H, &config).unwrap();
}
}
#[test]
#[cfg(feature = "splines")]
fn dots_only_run_at_slow_speed_and_default_decoding_speed() {
const S: usize = 128;
let pixels = star_field(S);
for config in [
EncodeConfig::default()
.with_distance(3.0)
.with_speed(Speed::Fast),
slow_lossy(3.0).with_decoding_speed(DecodingSpeed::Fast),
] {
assert_eq!(
encode_image(&pixels, S, S, &config).unwrap(),
encode_image(&pixels, S, S, &config.clone().with_dots(true)).unwrap()
);
}
}
#[test]
#[cfg(feature = "splines")]
fn splines_only_run_at_slow_speed_and_default_decoding_speed() {
const S: usize = 256;
let pixels = line_art(S);
let fast = EncodeConfig::default()
.with_distance(3.0)
.with_speed(Speed::Fast);
assert_eq!(
encode_image(&pixels, S, S, &fast).unwrap(),
encode_image(&pixels, S, S, &fast.clone().with_splines(true)).unwrap()
);
let quick_decode = slow_lossy(3.0).with_decoding_speed(DecodingSpeed::Fast);
assert_eq!(
encode_image(&pixels, S, S, &quick_decode).unwrap(),
encode_image(&pixels, S, S, &quick_decode.clone().with_splines(true)).unwrap()
);
}
#[test]
#[cfg(feature = "splines")]
fn edge_splines_are_inert_outside_supported_frames() {
const S: usize = 96;
let pixels = line_art(S);
let configurations = [
slow_lossy(1.0),
slow_lossy(3.0).with_speed(Speed::Fast),
slow_lossy(3.0).with_decoding_speed(DecodingSpeed::Fast),
slow_lossy(3.0).with_progressive(true),
slow_lossy(3.0).with_progressive_dc(true),
slow_lossy(3.0).with_coding_transform(CodingTransform::Rgb),
];
for config in configurations {
let without = encode_image(&pixels, S, S, &config).unwrap();
let with = encode_image(&pixels, S, S, &config.with_edge_splines(true)).unwrap();
assert_eq!(without, with);
}
}
#[test]
#[cfg(feature = "splines")]
fn edge_splines_leave_a_smooth_image_byte_identical() {
const S: usize = 96;
let pixels: Vec<u8> = (0..S * S)
.flat_map(|i| {
let (x, y) = (i % S, i / S);
[80 + x as u8 / 3, 100 + y as u8 / 3, 140 + (x + y) as u8 / 5]
})
.collect();
let config = slow_lossy(3.0).with_splines(true);
assert_eq!(
encode_image(&pixels, S, S, &config).unwrap(),
encode_image(&pixels, S, S, &config.with_edge_splines(true)).unwrap()
);
}
#[test]
#[cfg(feature = "splines")]
fn splines_coexist_with_patches() {
const S: usize = 256;
let pixels = line_art(S);
let both = slow_lossy(3.0).with_splines(true).with_patches(true);
let plain = encode_image(&pixels, S, S, &slow_lossy(3.0).with_patches(true)).unwrap();
let with = encode_image(&pixels, S, S, &both).unwrap();
assert!(with.len() < plain.len());
}
#[test]
fn lossy_patches_are_rate_safe() {
let plain = encode_image(&rgb8(), W, H, &lossy()).unwrap();
let checked = encode_image(&rgb8(), W, H, &lossy().with_patches(true)).unwrap();
assert!(checked.len() <= plain.len());
}
#[test]
fn fast_lossy_patches_are_rate_safe_on_unrepeated_ink() {
const SIZE: usize = 256;
let mut pixels = vec![0u8; SIZE * SIZE * 3];
for y in 0..SIZE {
for x in 0..SIZE {
if (x % 8 == 3 || y % 8 == 3) && x % 64 < 60 && y % 64 < 60 {
let blue = if (x / 64 + y / 64) % 2 == 0 { 254 } else { 0 };
pixels[(y * SIZE + x) * 3..][..3].copy_from_slice(&[252, 0, blue]);
}
}
}
let mut state = 0x2545_f491u32;
let mut next = || {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
state as usize
};
for _ in 0..60 {
let (x0, y0) = (next() % (SIZE - 8), next() % (SIZE - 8));
let (w, h) = (2 + next() % 6, 1 + next() % 5);
for y in y0..y0 + h {
pixels[(y * SIZE + x0) * 3..][..w * 3].fill(0);
}
}
let plain = EncodeConfig::default().with_distance(1.0);
let patched = plain.clone().with_patches(true);
let plain = encode_image(&pixels, SIZE, SIZE, &plain).unwrap();
let patched = encode_image(&pixels, SIZE, SIZE, &patched).unwrap();
assert!(
patched.len() * 10 <= plain.len() * 11,
"{} vs {}",
patched.len(),
plain.len()
);
}
fn glyph_text_pixels(size: usize) -> Vec<u8> {
let mut state = 0x2545_f491u32;
let mut next = || {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
state as usize
};
let glyphs: Vec<[[bool; 5]; 7]> = (0..60)
.map(|_| std::array::from_fn(|_| std::array::from_fn(|_| next() % 3 < 2)))
.collect();
let mut pixels = vec![255u8; size * size * 3];
for line in 0..size / 11 {
for column in 0..size / 7 - 1 {
let glyph = &glyphs[next() % glyphs.len()];
let (x0, y0) = (3 + column * 7 + line % 3, 2 + line * 11);
for (dy, row) in glyph.iter().enumerate() {
for (dx, &ink) in row.iter().enumerate() {
if ink {
pixels[((y0 + dy) * size + x0 + dx) * 3..][..3].fill(20);
}
}
}
}
}
pixels
}
#[test]
fn fast_lossy_glyph_patches_shrink_repeated_text() {
const SIZE: usize = 256;
let pixels = glyph_text_pixels(SIZE);
let plain = EncodeConfig::default().with_distance(1.0);
let patched = plain.clone().with_patches(true);
let plain = encode_image(&pixels, SIZE, SIZE, &plain).unwrap();
let patched = encode_image(&pixels, SIZE, SIZE, &patched).unwrap();
assert!(
patched.len() * 5 < plain.len(),
"{} vs {}",
patched.len(),
plain.len()
);
}
fn repeated_region_pixels() -> Vec<u8> {
const PW: usize = 256;
const PH: usize = 256;
let mut state = 0x89ab_cdefu32;
let mut pixels = vec![0u8; PW * PH * 3];
for sample in &mut pixels {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
*sample = (state >> 24) as u8;
}
let tile: Vec<u8> = (0..16)
.flat_map(|y| pixels[(y * PW * 3)..(y * PW * 3 + 48)].iter().copied())
.collect();
for ty in 0..16 {
for tx in 0..16 {
if (tx + ty) % 2 != 0 {
continue;
}
for y in 0..16 {
let dst = ((ty * 16 + y) * PW + tx * 16) * 3;
pixels[dst..dst + 48].copy_from_slice(&tile[y * 48..(y + 1) * 48]);
}
}
}
pixels
}
#[test]
fn lossy_patches_reduce_repeated_regions() {
const PW: usize = 256;
const PH: usize = 256;
let pixels = repeated_region_pixels();
let plain = encode_image(&pixels, PW, PH, &lossy().with_patches(false)).unwrap();
let patched = encode_image(&pixels, PW, PH, &lossy().with_patches(true)).unwrap();
assert!(
patched.len() < plain.len(),
"lossy patches should win on repeated complex regions: {} >= {}",
patched.len(),
plain.len()
);
}
#[test]
fn rgb8_slow_low_quality_lossy() {
const SIDE: usize = 64;
let pixels: Vec<u8> = (0..SIDE * SIDE * 3).map(|i| (i % 251) as u8).collect();
ok(encode_image(
&pixels,
SIDE,
SIDE,
&EncodeConfig::default()
.with_distance(3.0)
.with_speed(Speed::Slow),
));
}
#[test]
fn rgb8_slow_low_quality_rect_merge_lossy() {
let config = EncodeConfig::default()
.with_distance(2.5)
.with_speed(Speed::Slow);
for (width, height) in [(32, 64), (64, 32)] {
let pixels = vec![128u8; width * height * 3];
ok(encode_image(&pixels, width, height, &config));
}
}
#[test]
fn lossy_output_is_independent_of_thread_count() {
const WIDTH: usize = 257;
const HEIGHT: usize = 65;
let input: Vec<u8> = (0..WIDTH * HEIGHT * 3)
.map(|i| i.wrapping_mul(37).wrapping_add((i / 7).wrapping_mul(13)) as u8)
.collect();
let config = lossy().with_speed(Speed::Slow);
let single = encode_image(&input, WIDTH, HEIGHT, &config.clone().with_num_threads(1))
.expect("single-threaded lossy encode failed");
let threaded = encode_image(&input, WIDTH, HEIGHT, &config.with_num_threads(4))
.expect("multi-threaded lossy encode failed");
assert_eq!(single, threaded);
}
#[test]
fn rgba_lossy_patches_reduce_repeated_regions() {
const PW: usize = 256;
const PH: usize = 256;
let mut pixels = vec![0u8; PW * PH * 4];
for (i, px) in pixels.as_chunks_mut::<4>().0.iter_mut().enumerate() {
let (x, y) = (i % PW, i / PW);
let (tx, ty) = (x % 16, y % 16);
if (x / 16 + y / 16) % 4 != 0 {
px[0] = ((tx / 4) * 40 + (ty / 4) * 17) as u8;
px[1] = ((tx / 4) * 25 + (ty / 4) * 31) as u8;
px[2] = ((tx / 4) * 11 + (ty / 4) * 45) as u8;
} else {
let s = (i as u32)
.wrapping_mul(1_664_525)
.wrapping_add(1_013_904_223);
px[0] = (s >> 24) as u8;
px[1] = (s >> 16) as u8;
px[2] = (s >> 8) as u8;
}
px[3] = 255;
}
let cfg = |patches: bool| {
EncodeConfig::default()
.with_lossless(false)
.with_distance(1.0)
.with_patches(patches)
};
let plain = encode_image_with_alpha(&pixels, PW, PH, &cfg(false)).unwrap();
let patched = encode_image_with_alpha(&pixels, PW, PH, &cfg(true)).unwrap();
assert!(
patched.len() < plain.len(),
"alpha image patches should win: {} >= {}",
patched.len(),
plain.len()
);
}
#[test]
fn rgb8_lossless() {
ok(encode_image(&rgb8(), W, H, &lossless()));
}
#[test]
fn rgb8_lossless_patches_reduce_repeated_tiles() {
const PW: usize = 96;
const PH: usize = 64;
let mut pixels = vec![0u8; PW * PH * 3];
for y in 0..PH {
for x in 0..PW {
let tx = x % 16;
let ty = y % 16;
let p = (y * PW + x) * 3;
pixels[p] = (tx * 13 + ty * 3) as u8;
pixels[p + 1] = (tx * 5 + ty * 11) as u8;
pixels[p + 2] = (tx * 7 + ty * 9) as u8;
}
}
let plain = encode_image(&pixels, PW, PH, &lossless().with_patches(false)).unwrap();
let patched = encode_image(&pixels, PW, PH, &lossless().with_patches(true)).unwrap();
assert!(
patched.len() < plain.len(),
"patch dictionary should reduce a repeated-tile image: {} >= {}",
patched.len(),
plain.len()
);
}
#[test]
fn rgb8_lossless_patches_fall_back_when_rate_is_worse() {
const PW: usize = 64;
const PH: usize = 64;
let mut state = 0x1234_5678u32;
let mut pixels = vec![0u8; PW * PH * 3];
for sample in &mut pixels {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
*sample = (state >> 24) as u8;
}
for y in 0..16 {
for x in 0..48 {
let pixel = (y * PW + x) * 3;
for sample in &mut pixels[pixel..pixel + 3] {
*sample = 0;
}
}
}
let plain = encode_image(&pixels, PW, PH, &lossless()).unwrap();
let checked = encode_image(&pixels, PW, PH, &lossless().with_patches(true)).unwrap();
assert_eq!(checked, plain, "a losing patch candidate must be discarded");
}
#[test]
fn rgb8_lossless_slow_adaptive() {
ok(encode_image(
&rgb8(),
W,
H,
&lossless().with_speed(Speed::Slow),
));
}
#[test]
fn fastest_speed_encodes_dct8_only() {
const WIDTH: usize = 128;
const HEIGHT: usize = 128;
let input: Vec<u8> = (0..WIDTH * HEIGHT * 3)
.map(|i| (((i / 3) % WIDTH) / 2 + ((i / 3) / WIDTH) / 2) as u8)
.collect();
let config = lossy().with_distance(2.0);
let fastest = encode_image(
&input,
WIDTH,
HEIGHT,
&config.clone().with_speed(Speed::Fastest),
)
.expect("Fastest encode failed");
let fast = encode_image(
&input,
WIDTH,
HEIGHT,
&config.clone().with_speed(Speed::Fast),
)
.expect("Fast encode failed");
assert!(!fastest.is_empty());
assert_ne!(
fastest, fast,
"Fastest should skip the transform search Fast performs"
);
}
fn checkerboard_rgb(size: usize) -> Vec<u8> {
let mut pixels = Vec::with_capacity(size * size * 3);
for y in 0..size {
for x in 0..size {
let lit = (x / 4 + y / 4) % 2 == 1;
let drift = (x * 24 / size + y * 12 / size) as u8;
pixels.extend_from_slice(&if lit {
[210 - drift, 8, 6]
} else {
[40 - drift.min(40), 0, 0]
});
}
}
pixels
}
fn learned_rate_lossy(distance: f32) -> EncodeConfig {
EncodeConfig::default()
.with_distance(distance)
.with_speed(Speed::Slow)
.with_patches(false)
.with_ink_layer(false)
.with_vector_layers(false)
.with_learned_rate(true)
}
#[test]
fn learned_rate_encodes_images_smaller_than_a_region() {
for (size, distance) in [(8usize, 1.0f32), (40, 3.0), (63, 1.0), (100, 3.0)] {
let pixels = checkerboard_rgb(size);
let bytes = encode_image(&pixels, size, size, &learned_rate_lossy(distance))
.expect("encode failed");
assert!(!bytes.is_empty(), "{size} at d={distance}");
}
}
#[test]
fn learned_rate_shrinks_a_block_periodic_pattern() {
const SIZE: usize = 128;
let pixels = checkerboard_rgb(SIZE);
let learned = learned_rate_lossy(2.0).with_num_threads(1);
let fixed = learned.clone().with_learned_rate(false);
let learned = encode_image(&pixels, SIZE, SIZE, &learned).unwrap();
let fixed = encode_image(&pixels, SIZE, SIZE, &fixed).unwrap();
assert!(
learned.len() * 10 < fixed.len() * 8,
"{} vs {}",
learned.len(),
fixed.len()
);
}
#[test]
fn learned_rate_prices_every_transform_of_a_small_image() {
const SIZE: usize = 192;
let pixels = checkerboard_rgb(SIZE);
let learned = learned_rate_lossy(1.0).with_num_threads(1);
let fixed = learned.clone().with_learned_rate(false);
let learned = encode_image(&pixels, SIZE, SIZE, &learned).unwrap();
let fixed = encode_image(&pixels, SIZE, SIZE, &fixed).unwrap();
assert!(
learned.len() < fixed.len() * 2,
"{} vs {}",
learned.len(),
fixed.len()
);
}
#[test]
fn learned_rate_lets_fine_transforms_compete_with_merges() {
const SIZE: usize = 128;
let mut pixels = Vec::with_capacity(SIZE * SIZE * 3);
for y in 0..SIZE {
for x in 0..SIZE {
let ink = x % 8 == 3 || y % 8 == 3;
let magenta = (x / 64 + y / 64) % 2 == 0;
pixels.extend_from_slice(&match (ink, magenta) {
(false, _) => [0, 0, 0],
(true, true) => [252, 0, 254],
(true, false) => [252, 0, 0],
});
}
}
let learned = learned_rate_lossy(2.0).with_num_threads(1);
let fixed = learned.clone().with_learned_rate(false);
let learned = encode_image(&pixels, SIZE, SIZE, &learned).unwrap();
let fixed = encode_image(&pixels, SIZE, SIZE, &fixed).unwrap();
assert!(
learned.len() * 4 < fixed.len() * 3,
"{} vs {}",
learned.len(),
fixed.len()
);
}
#[test]
fn learned_rate_is_a_slow_speed_tool() {
const SIZE: usize = 128;
let pixels = checkerboard_rgb(SIZE);
for speed in [Speed::UltraFast, Speed::Fastest, Speed::Fast, Speed::Medium] {
let base = EncodeConfig::default()
.with_distance(2.0)
.with_speed(speed)
.with_learned_rate(false)
.with_num_threads(1);
assert_eq!(
encode_image(&pixels, SIZE, SIZE, &base).unwrap(),
encode_image(&pixels, SIZE, SIZE, &base.clone().with_learned_rate(true)).unwrap(),
"{speed:?}"
);
}
}
#[test]
fn rgb8_quality() {
ok(encode_image(
&rgb8(),
W,
H,
&EncodeConfig::default().with_quality(85.0),
));
}
#[test]
fn rgba8_lossy() {
ok(encode_image_with_alpha(&rgba8(), W, H, &lossy()));
}
#[test]
fn rgba8_lossless() {
ok(encode_image_with_alpha(&rgba8(), W, H, &lossless()));
}
#[test]
fn rgb16_lossy() {
ok(encode_image_16bit(&rgb16(), W, H, &lossy()));
}
#[test]
fn rgb16_lossless() {
ok(encode_image_16bit(&rgb16(), W, H, &lossless()));
}
#[test]
fn rgba16_lossy() {
ok(encode_image_with_alpha_16bit(&rgba16(), W, H, &lossy()));
}
#[test]
fn rgba16_lossless() {
ok(encode_image_with_alpha_16bit(&rgba16(), W, H, &lossless()));
}
#[test]
fn rgb10_lossy() {
ok(encode_image_10bit(&rgb10(), W, H, &lossy()));
}
#[test]
fn rgb10_lossless() {
ok(encode_image_10bit(&rgb10(), W, H, &lossless()));
}
#[test]
fn rgba10_lossy() {
ok(encode_image_with_alpha_10bit(&rgba10(), W, H, &lossy()));
}
#[test]
fn rgba10_lossless() {
ok(encode_image_with_alpha_10bit(&rgba10(), W, H, &lossless()));
}
#[test]
fn rgb12_lossy() {
ok(encode_image_12bit(&rgb12(), W, H, &lossy()));
}
#[test]
fn rgb12_lossless() {
ok(encode_image_12bit(&rgb12(), W, H, &lossless()));
}
#[test]
fn rgba12_lossy() {
ok(encode_image_with_alpha_12bit(&rgba12(), W, H, &lossy()));
}
#[test]
fn rgba12_lossless() {
ok(encode_image_with_alpha_12bit(&rgba12(), W, H, &lossless()));
}
#[test]
fn rgb_f32_lossy() {
ok(encode_image_f32(&rgb_f32(), W, H, &lossy()));
}
#[test]
fn rgb_f16_lossy() {
ok(encode_image_f16(&rgb_f32(), W, H, &lossy()));
}
#[test]
fn rgba_f32_lossy() {
ok(encode_image_with_alpha_f32(&rgba_f32(), W, H, &lossy()));
}
#[test]
fn rgba_f16_lossy() {
ok(encode_image_with_alpha_f16(&rgba_f32(), W, H, &lossy()));
}
#[test]
fn gray8_lossy() {
ok(encode_image_gray(&gray8(), W, H, &lossy()));
}
#[test]
fn gray8_lossless() {
ok(encode_image_gray(&gray8(), W, H, &lossless()));
}
#[test]
fn gray_alpha8_lossy() {
ok(encode_image_gray_alpha(&gray_alpha8(), W, H, &lossy()));
}
#[test]
fn gray_alpha8_lossless() {
ok(encode_image_gray_alpha(&gray_alpha8(), W, H, &lossless()));
}
#[test]
fn gray10_lossy() {
ok(encode_image_gray_10bit(&gray10(), W, H, &lossy()));
}
#[test]
fn gray10_lossless() {
ok(encode_image_gray_10bit(&gray10(), W, H, &lossless()));
}
#[test]
fn gray_alpha10_lossy() {
ok(encode_image_gray_alpha_10bit(
&gray_alpha10(),
W,
H,
&lossy(),
));
}
#[test]
fn gray_alpha10_lossless() {
ok(encode_image_gray_alpha_10bit(
&gray_alpha10(),
W,
H,
&lossless(),
));
}
#[test]
fn gray12_lossy() {
ok(encode_image_gray_12bit(&gray12(), W, H, &lossy()));
}
#[test]
fn gray12_lossless() {
ok(encode_image_gray_12bit(&gray12(), W, H, &lossless()));
}
#[test]
fn gray_alpha12_lossy() {
ok(encode_image_gray_alpha_12bit(
&gray_alpha12(),
W,
H,
&lossy(),
));
}
#[test]
fn gray_alpha12_lossless() {
ok(encode_image_gray_alpha_12bit(
&gray_alpha12(),
W,
H,
&lossless(),
));
}
#[test]
fn gray16_lossy() {
ok(encode_image_gray_16bit(&gray16(), W, H, &lossy()));
}
#[test]
fn gray16_lossless() {
ok(encode_image_gray_16bit(&gray16(), W, H, &lossless()));
}
#[test]
fn gray_alpha16_lossy() {
ok(encode_image_gray_alpha_16bit(
&gray_alpha16(),
W,
H,
&lossy(),
));
}
#[test]
fn gray_alpha16_lossless() {
ok(encode_image_gray_alpha_16bit(
&gray_alpha16(),
W,
H,
&lossless(),
));
}
#[test]
fn gray_f32_lossy() {
ok(encode_image_gray_f32(&gray_f32(), W, H, &lossy()));
}
#[test]
fn gray_f16_lossy() {
ok(encode_image_gray_f16(&gray_f32(), W, H, &lossy()));
}
#[test]
fn empty_width_rejected() {
assert!(matches!(
encode_image(&[], 0, H, &lossy()),
Err(EncodeError::EmptyImage)
));
}
#[test]
fn empty_height_rejected() {
assert!(matches!(
encode_image(&[], W, 0, &lossy()),
Err(EncodeError::EmptyImage)
));
}
#[test]
fn wrong_buffer_size_rejected() {
let short = vec![0u8; W * H]; assert!(matches!(
encode_image(&short, W, H, &lossy()),
Err(EncodeError::InputSizeMismatch { .. })
));
}
#[test]
fn invalid_distance_rejected() {
assert!(matches!(
encode_image(&rgb8(), W, H, &EncodeConfig::default().with_distance(-1.0)),
Err(EncodeError::InvalidDistance(_))
));
}
#[test]
fn dimension_too_large_rejected() {
let huge = MAX_DIMENSION + 1;
assert!(matches!(
encode_image(&[], huge, H, &lossy()),
Err(EncodeError::DimensionTooLarge { .. })
));
}
#[test]
fn one_by_one_rgb8() {
ok(encode_image(&[128u8, 64, 32], 1, 1, &lossy()));
}
#[test]
fn one_by_one_rgba8_lossless() {
ok(encode_image_with_alpha(
&[128u8, 64, 32, 255],
1,
1,
&lossless(),
));
}
#[test]
fn one_by_one_gray8() {
ok(encode_image_gray(&[200u8], 1, 1, &lossy()));
}
}
#[cfg(test)]
mod lossy_alpha_tests {
use super::*;
use crate::util::fast_hypot;
const W: usize = 160;
const H: usize = 136;
fn rgba(alpha: impl Fn(usize, usize) -> u8) -> Vec<u8> {
(0..W * H)
.flat_map(|i| {
let (x, y) = (i % W, i / W);
[(x * 255 / W) as u8, (y * 255 / H) as u8, 96, alpha(x, y)]
})
.collect()
}
fn disc_radius(x: usize, y: usize) -> f32 {
fast_hypot(x as f32 - W as f32 / 2.0, y as f32 - H as f32 / 2.0) / H as f32
}
fn soft(x: usize, y: usize) -> u8 {
(255.0 * ((0.45 - disc_radius(x, y)) / 0.25).clamp(0.0, 1.0)).round() as u8
}
fn hard(x: usize, y: usize) -> u8 {
if disc_radius(x, y) < 0.4 { 255 } else { 0 }
}
fn encode(pixels: &[u8], config: &EncodeConfig) -> Vec<u8> {
encode_image_with_alpha(pixels, W, H, config).unwrap()
}
fn fast() -> EncodeConfig {
EncodeConfig::default()
.with_distance(1.5)
.with_num_threads(1)
}
#[test]
fn solid_alpha_is_coded_as_before() {
for value in [255, 77, 0] {
let pixels = rgba(|_, _| value);
assert_eq!(
encode(&pixels, &fast()),
encode(&pixels, &fast().with_alpha_distance(0.0)),
"constant alpha {value}"
);
}
}
#[test]
fn soft_alpha_is_smaller_than_lossless() {
let pixels = rgba(soft);
for config in [fast(), fast().with_speed(Speed::Slow)] {
let lossy = encode(&pixels, &config).len();
let lossless = encode(&pixels, &config.clone().with_alpha_distance(0.0)).len();
assert!(
lossy * 10 < lossless * 9,
"{:?}: lossy alpha {lossy} vs lossless {lossless}",
config.speed
);
}
}
#[test]
fn lossy_alpha_never_grows_the_file() {
let noisy = |x: usize, y: usize| {
let h = (x * 7919 + y * 104_729) % 251;
if h < 5 { 200 + h as u8 } else { 255 }
};
for alpha in [hard as fn(usize, usize) -> u8, noisy] {
let pixels = rgba(alpha);
assert!(
encode(&pixels, &fast()).len()
<= encode(&pixels, &fast().with_alpha_distance(0.0)).len()
);
}
}
#[test]
fn alpha_distance_follows_the_frame_by_default() {
let pixels = rgba(soft);
assert_eq!(
encode(&pixels, &fast()),
encode(&pixels, &fast().with_alpha_distance(1.5))
);
let lossless = encode(&pixels, &fast().with_alpha_distance(0.0));
for odd in [f32::NAN, -1.0] {
assert_eq!(encode(&pixels, &fast().with_alpha_distance(odd)), lossless);
}
assert!(encode(&pixels, &fast().with_alpha_distance(4.0)).len() < lossless.len());
}
#[test]
fn gray_and_deep_alpha_go_lossy_too() {
let gray: Vec<u8> = rgba(soft)
.as_chunks::<4>()
.0
.iter()
.flat_map(|px| [px[0], px[3]])
.collect();
let gray_lossy = encode_image_gray_alpha(&gray, W, H, &fast()).unwrap();
let gray_lossless =
encode_image_gray_alpha(&gray, W, H, &fast().with_alpha_distance(0.0)).unwrap();
assert!(gray_lossy.len() < gray_lossless.len());
let deep: Vec<u16> = rgba(soft).iter().map(|&v| u16::from(v) * 257).collect();
let deep_lossy = encode_image_with_alpha_16bit(&deep, W, H, &fast()).unwrap();
let deep_lossless =
encode_image_with_alpha_16bit(&deep, W, H, &fast().with_alpha_distance(0.0)).unwrap();
assert!(deep_lossy.len() < deep_lossless.len());
}
#[test]
fn multi_group_alpha_keeps_values_where_the_palette_breaks_gradients() {
let (w, h) = (512, 64);
let mut rgba = Vec::with_capacity(w * h * 4);
for y in 0..h {
for x in 0..w {
let a = 80 + 17 * (x % 8) as i32 - 11 * (y % 8) as i32;
rgba.extend([120, 60, 200, a as u8]);
}
}
let config = EncodeConfig::default()
.with_speed(Speed::Fast)
.with_alpha_distance(0.0);
assert!(encode_image_with_alpha(&rgba, w, h, &config).unwrap().len() < 6000);
}
#[test]
fn color_under_partial_alpha_codes_coarser() {
let (w, h) = (256, 256);
let mut rgba = Vec::with_capacity(w * h * 4);
for y in 0..h {
for x in 0..w {
let r = (fast_hypot(x as f32 - 128.0, y as f32 - 128.0) / 128.0).min(1.0);
let a = (8.0 + 200.0 * (1.0 - r)) as u8;
let t = ((x * 7 + y * 13) % 64) as u8;
rgba.extend([120 + t, 80 + t / 2, 200 - t, a]);
}
}
let config = EncodeConfig::default()
.with_speed(Speed::Fast)
.with_distance(1.5);
let weighted = encode_image_with_alpha(&rgba, w, h, &config).unwrap().len();
let flat = encode_image_with_alpha(&rgba, w, h, &config.with_fill_invisible(false))
.unwrap()
.len();
assert!(weighted * 10 < flat * 9, "{weighted} vs {flat}");
}
#[test]
fn slow_lossy_alpha_codes_anti_aliased_cutouts_through_few_levels() {
let (w, h) = (320, 288);
let mut rgba = Vec::with_capacity(w * h * 4);
for y in 0..h {
for x in 0..w {
let a = [
(90.0, 90.0, 60.0),
(220.0, 150.0, 80.0),
(120.0, 220.0, 45.0),
]
.iter()
.map(|&(cx, cy, r): &(f32, f32, f32)| {
let d = fast_hypot(x as f32 - cx, y as f32 - cy) - r;
(0.5 - d / 1.5).clamp(0.0, 1.0)
})
.fold(0.0f32, f32::max);
rgba.extend([200, 60, 90, (a * 255.0 + 0.5) as u8]);
}
}
let config = EncodeConfig::default()
.with_speed(Speed::Slow)
.with_distance(3.0);
let bytes = encode_image_with_alpha(&rgba, w, h, &config).unwrap().len();
assert!(bytes < 1900, "{bytes}");
let config = EncodeConfig::default()
.with_speed(Speed::Slow)
.with_distance(1.5)
.with_coding_transform(crate::CodingTransform::YCbCr)
.with_lossy_modular(crate::LossyModular::Force);
let bytes = encode_image_with_alpha(&rgba, w, h, &config).unwrap().len();
assert!(bytes < 2800, "{bytes}");
let config = EncodeConfig::default()
.with_speed(Speed::Fast)
.with_distance(1.5);
let bytes = encode_image_with_alpha(&rgba, w, h, &config).unwrap().len();
assert!(bytes < 3150, "{bytes}");
}
#[test]
fn vertical_first_requires_forced_xyb_modular() {
let (w, h) = (37, 29);
let pixels: Vec<u8> = (0..w * h)
.flat_map(|i| {
let v = (i * 13 + i / w * 29) as u8;
[v, v, v]
})
.collect();
let config = EncodeConfig::default()
.with_distance(3.0)
.with_num_threads(1);
let encode = |config: &EncodeConfig| encode_image(&pixels, w, h, config).unwrap();
assert_eq!(
encode(&config),
encode(&config.clone().with_modular_vertical_first(true)),
);
let config = config.with_lossy_modular(LossyModular::Force);
assert_ne!(
encode(&config),
encode(&config.clone().with_modular_vertical_first(true)),
);
let config = config.with_coding_transform(crate::CodingTransform::Rgb);
assert_eq!(
encode(&config),
encode(&config.clone().with_modular_vertical_first(true)),
);
}
#[test]
fn patch_epf_follows_the_patch_speeds() {
let config = EncodeConfig::default().with_num_threads(1);
assert!(!lossy_context(&config, 1.5, XybMatrix::SPEC, 65536).patch_epf);
let config = config.with_speed(Speed::MediumSlow);
assert!(!lossy_context(&config, 1.5, XybMatrix::SPEC, 65536).patch_epf);
let config = config.with_speed(Speed::Slow);
assert!(lossy_context(&config, 1.5, XybMatrix::SPEC, 65536).patch_epf);
let config = config.with_patch_epf(false);
assert!(!lossy_context(&config, 1.5, XybMatrix::SPEC, 65536).patch_epf);
}
#[test]
fn function_fields_portable_is_opt_in() {
let config = EncodeConfig::default();
assert!(!config.function_fields_portable);
assert!(!lossy_context(&config, 1.5, XybMatrix::SPEC, 65536).function_fields_portable);
let portable = config.with_function_fields_portable(true);
assert!(lossy_context(&portable, 1.5, XybMatrix::SPEC, 65536).function_fields_portable);
}
#[test]
fn function_fields_default_on_where_supported() {
for (speed, supported) in [
(Speed::Fast, false),
(Speed::Medium, true),
(Speed::Slow, true),
(Speed::ExtraSlow, true),
] {
let config = EncodeConfig::default()
.with_speed(speed)
.with_num_threads(1);
assert!(config.function_fields);
assert_eq!(
lossy_context(&config, 1.5, XybMatrix::SPEC, 65536).function_fields,
supported,
"{speed:?}"
);
let off = config.with_function_fields(false);
assert!(!lossy_context(&off, 1.5, XybMatrix::SPEC, 65536).function_fields);
}
let config = EncodeConfig::default()
.with_speed(Speed::Slow)
.with_num_threads(1);
assert!(
!lossy_context(
&config.clone().with_progressive(true),
1.5,
XybMatrix::SPEC,
65536
)
.function_fields
);
assert!(
!lossy_context(
&config,
1.5,
XybMatrix::SPEC,
crate::function_fields::MAX_PIXELS + 1
)
.function_fields
);
}
}