use super::chroma::configure_chroma;
use super::quantization::DistanceParams;
use super::variants::{
GLYPH_BITS_MARGIN, GlyphStage, LATTICE_BITS_MARGIN, lattice_variants, prepare_vardct_variant,
};
#[cfg(feature = "splines")]
use super::variants::{SplineStage, spline_quant_field};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::encode_image::AlphaPlane;
use crate::encoding_context::EncodingContext;
use crate::image::Image3F;
use crate::util::EncodeError;
#[allow(clippy::too_many_arguments)]
pub(super) fn encode_frame_vardct(
ctx: &EncodingContext,
scratch: &mut CoderScratch,
distance: f32,
distp: &DistanceParams,
xyb: Image3F,
is_achromatic: bool,
alpha: Option<&AlphaPlane>,
coeff_shifts: &[u32],
patches: bool,
writer: &mut BitWriter,
) -> Result<(), EncodeError> {
configure_chroma(ctx, distance, distp, &xyb, is_achromatic);
#[cfg(feature = "splines")]
let quant_field = if ctx.splines || ctx.dots {
spline_quant_field(ctx, scratch, &xyb, distp)?
} else {
Vec::new()
};
#[cfg(feature = "splines")]
let spline_candidates = if ctx.splines || ctx.dots {
crate::splines::find_candidates(ctx, scratch, distance, &xyb, &quant_field)
} else {
None
};
#[cfg(feature = "splines")]
let stage = spline_candidates.as_ref().map(|candidates| SplineStage {
candidates,
quant_field: &quant_field,
before_tiles: false,
});
if !patches {
let prepared = prepare_vardct_variant(
ctx,
scratch,
distance,
distp,
xyb,
alpha,
coeff_shifts,
false,
None,
true,
#[cfg(feature = "splines")]
stage.as_ref(),
)?;
#[cfg(feature = "splines")]
drop(spline_candidates);
return prepared.encode(ctx, scratch, distance, alpha, coeff_shifts, writer);
}
let lattice = crate::lattice_patches::find(&xyb);
let glyphs = GlyphStage::find(ctx, scratch, distance, &xyb, alpha);
let mut best = prepare_vardct_variant(
ctx,
scratch,
distance,
distp,
xyb.clone(),
alpha,
coeff_shifts,
true,
glyphs.as_ref(),
true,
#[cfg(feature = "splines")]
stage.as_ref(),
)?;
if !best.used_tiles && lattice.is_none() {
drop(xyb);
drop(glyphs);
#[cfg(feature = "splines")]
drop(spline_candidates);
return best.encode(ctx, scratch, distance, alpha, coeff_shifts, writer);
}
#[allow(unused_mut)]
let mut best_bits = best.estimate(ctx, scratch, distance, alpha, coeff_shifts)?;
if let Some(plan) = lattice {
drop(xyb);
drop(glyphs);
#[cfg(feature = "splines")]
drop(spline_candidates);
best_bits *= LATTICE_BITS_MARGIN;
for mut arm in lattice_variants(ctx, scratch, distance, distp, plan, alpha)? {
let bits = arm.estimate(ctx, scratch, distance, alpha, coeff_shifts)?;
if bits < best_bits {
best = arm;
best_bits = bits;
}
}
return best.encode(ctx, scratch, distance, alpha, coeff_shifts, writer);
}
#[cfg(feature = "splines")]
if best.tiles_touched {
let stage = SplineStage {
before_tiles: true,
..stage.expect("candidates touched a tile")
};
let mut splines_first = prepare_vardct_variant(
ctx,
scratch,
distance,
distp,
xyb.clone(),
alpha,
coeff_shifts,
true,
glyphs.as_ref(),
true,
Some(&stage),
)?;
let bits = splines_first.estimate(ctx, scratch, distance, alpha, coeff_shifts)?;
if bits < best_bits {
best = splines_first;
best_bits = bits;
}
}
let mut no_tiles = prepare_vardct_variant(
ctx,
scratch,
distance,
distp,
xyb,
alpha,
coeff_shifts,
true,
glyphs.as_ref(),
false,
#[cfg(feature = "splines")]
stage.as_ref(),
)?;
let no_tile_bits = no_tiles.estimate(ctx, scratch, distance, alpha, coeff_shifts)?;
if no_tile_bits * GLYPH_BITS_MARGIN < best_bits {
best = no_tiles;
} else {
drop(no_tiles);
}
drop(glyphs);
#[cfg(feature = "splines")]
drop(spline_candidates);
best.encode(ctx, scratch, distance, alpha, coeff_shifts, writer)
}