use crate::ColorSpace;
use crate::encode_image::MAX_DIMENSION;
use std::fmt;
use std::ops::{Deref, DerefMut};
pub(crate) fn try_vec_fill<T: Clone>(value: T, len: usize) -> Result<Vec<T>, EncodeError> {
let size = len
.checked_mul(size_of::<T>())
.ok_or(EncodeError::AllocationFailed { size: usize::MAX })?;
let mut values = Vec::new();
values
.try_reserve_exact(len)
.map_err(|_| EncodeError::AllocationFailed { size })?;
values.resize(len, value);
Ok(values)
}
macro_rules! try_vec {
($value:expr; $len:expr) => {
$crate::util::try_vec_fill($value, $len)
};
}
pub(crate) use try_vec;
pub(crate) fn heap_array<T: Clone, const N: usize>(value: T) -> Box<[T; N]> {
let slice = vec![value; N].into_boxed_slice();
boxed_slice_to_array(slice)
}
pub(crate) fn heap_array_from_fn<T, const N: usize>(make: impl FnMut(usize) -> T) -> Box<[T; N]> {
let slice = (0..N).map(make).collect::<Vec<_>>().into_boxed_slice();
boxed_slice_to_array(slice)
}
fn boxed_slice_to_array<T, const N: usize>(slice: Box<[T]>) -> Box<[T; N]> {
match slice.try_into() {
Ok(array) => array,
Err(_) => unreachable!("boxed slice length is the requested array length"),
}
}
#[derive(Clone)]
pub(crate) struct HeapMatrix<T, const ROWS: usize, const COLS: usize> {
data: Box<[T]>,
}
impl<T: Clone, const ROWS: usize, const COLS: usize> HeapMatrix<T, ROWS, COLS> {
pub(crate) fn new(value: T) -> Self {
Self {
data: vec![value; ROWS * COLS].into_boxed_slice(),
}
}
pub(crate) fn from_rows(rows: &[[T; COLS]; ROWS]) -> Self {
Self {
data: rows.as_flattened().to_vec().into_boxed_slice(),
}
}
}
impl<T, const ROWS: usize, const COLS: usize> Deref for HeapMatrix<T, ROWS, COLS> {
type Target = [[T; COLS]; ROWS];
fn deref(&self) -> &Self::Target {
let (rows, remainder) = self.data.as_chunks::<COLS>();
debug_assert!(remainder.is_empty());
rows.try_into()
.unwrap_or_else(|_| unreachable!("heap matrix has its declared dimensions"))
}
}
impl<T, const ROWS: usize, const COLS: usize> DerefMut for HeapMatrix<T, ROWS, COLS> {
fn deref_mut(&mut self) -> &mut Self::Target {
let (rows, remainder) = self.data.as_chunks_mut::<COLS>();
debug_assert!(remainder.is_empty());
rows.try_into()
.unwrap_or_else(|_| unreachable!("heap matrix has its declared dimensions"))
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum EncodeError {
EmptyImage,
InputSizeMismatch {
expected: usize,
actual: usize,
},
AlphaSizeMismatch {
expected: usize,
actual: usize,
},
InvalidDistance(f32),
QualityIsNaN,
DimensionTooLarge {
width: usize,
height: usize,
},
AllocationFailed {
size: usize,
},
UnsupportedAlphaBitDepth(u8),
IccProfileNotSupported,
Unsupported(&'static str),
BadChannelCount(usize),
BadBitDepth(u32),
IccNotSupported,
SizeOverflow,
InputLength {
expected: usize,
got: usize,
},
UnsupportedColorSpace(ColorSpace),
Jpeg(String),
Brotli(String),
GainMapSizeMismatch {
expected: usize,
actual: usize,
},
InvalidGainMapMetadata(&'static str),
GainMap(&'static str),
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyImage => write!(f, "image dimensions must be non-zero"),
Self::InputSizeMismatch { expected, actual } => write!(
f,
"input buffer size mismatch: expected {expected} bytes, got {actual}"
),
Self::AlphaSizeMismatch { expected, actual } => write!(
f,
"alpha plane size mismatch: expected {expected} pixels, got {actual}"
),
Self::InvalidDistance(d) => write!(
f,
"butteraugli distance must be a positive finite number, got {d}"
),
Self::QualityIsNaN => write!(f, "quality value must not be NaN"),
Self::DimensionTooLarge { width, height } => write!(
f,
"image dimensions {width}×{height} exceed the maximum ({})",
MAX_DIMENSION
),
Self::AllocationFailed { size } => {
write!(f, "failed to allocate an encoding buffer of {size} bytes")
}
Self::UnsupportedAlphaBitDepth(bits) => {
write!(
f,
"unsupported alpha bit depth: {bits} (must be 8, 10, or 12)"
)
}
Self::IccProfileNotSupported => {
write!(f, "ICC profile injection is not yet supported by jixel")
}
Self::Unsupported(msg) => write!(f, "unsupported: {msg}"),
Self::BadChannelCount(n) => write!(f, "channel count {} not in 1..=4", n),
Self::BadBitDepth(b) => write!(f, "bits_per_sample {} not in 1..=16", b),
Self::IccNotSupported => write!(
f,
"embedded ICC not yet supported; use an enumerated color space"
),
Self::SizeOverflow => write!(f, "image size overflows usize"),
Self::InputLength { expected, got } => {
write!(f, "input length {} != expected {}", got, expected)
}
Self::Jpeg(msg) => write!(f, "JPEG transcoding failed: {msg}"),
Self::Brotli(msg) => write!(f, "Brotli compression failed: {msg}"),
Self::GainMapSizeMismatch { expected, actual } => write!(
f,
"gain map buffer size mismatch: expected {expected} samples, got {actual}"
),
Self::InvalidGainMapMetadata(msg) => {
write!(f, "invalid gain map metadata: {msg}")
}
Self::GainMap(msg) => write!(f, "gain map: {msg}"),
EncodeError::UnsupportedColorSpace(colorspace) => {
f.write_fmt(format_args!("unsupported color space: {:?}", colorspace))
}
}
}
}
impl std::error::Error for EncodeError {}
pub(crate) trait FastRound {
fn fast_round(self) -> Self;
}
impl FastRound for f32 {
fn fast_round(self) -> Self {
self.round()
}
}
#[cfg(not(all(target_arch = "aarch64", feature = "neon")))]
#[inline]
fn f32_to_f16_bits_impl(v: f32) -> u16 {
let bits = v.to_bits();
let sign = ((bits >> 16) & 0x8000) as u16;
let exp = ((bits >> 23) & 0xFF) as i32;
let mant = bits & 0x7F_FFFF;
let e16 = exp - 112; if e16 >= 31 {
return sign | 0x7BFF; }
if e16 <= 0 {
if e16 < -10 {
return sign;
}
let m = mant | 0x80_0000;
let shift = (14 - e16) as u32;
let rounded = (m + (1 << (shift - 1)) - 1 + ((m >> shift) & 1)) >> shift;
return sign | rounded as u16;
}
let m = mant + 0xFFF + ((mant >> 13) & 1);
let mut e16 = e16 as u32;
let mut m16 = m >> 13;
if m16 == 0x400 {
m16 = 0;
e16 += 1;
if e16 >= 31 {
return sign | 0x7BFF;
}
}
sign | ((e16 as u16) << 10) | (m16 as u16)
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "f16c")]
fn f32_to_f16_bits_f16c(v: f32) -> u16 {
#[cfg(target_arch = "x86")]
use std::arch::x86::{_MM_FROUND_TO_NEAREST_INT, _mm_cvtps_ph, _mm_cvtsi128_si32, _mm_set_ss};
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::{
_MM_FROUND_TO_NEAREST_INT, _mm_cvtps_ph, _mm_cvtsi128_si32, _mm_set_ss,
};
_mm_cvtsi128_si32(_mm_cvtps_ph::<_MM_FROUND_TO_NEAREST_INT>(_mm_set_ss(v))) as u16
}
#[inline]
pub(crate) fn f32_to_f16_bits(v: f32) -> u16 {
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
{
use std::arch::aarch64::{vcvt_f16_f32, vdupq_n_f32, vget_lane_u16, vreinterpret_u16_f16};
unsafe { vget_lane_u16::<0>(vreinterpret_u16_f16(vcvt_f16_f32(vdupq_n_f32(v)))) }
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
let converter = F16_CONVERTERS.get_or_init(select_f16_converters);
unsafe { (converter.f32_to_f16_bits)(v) }
}
#[cfg(not(any(
all(target_arch = "aarch64", feature = "neon"),
target_arch = "x86",
target_arch = "x86_64"
)))]
{
f32_to_f16_bits_impl(v)
}
}
#[cfg(not(all(target_arch = "aarch64", feature = "neon")))]
#[inline]
fn f16_bits_to_f32_impl(b: u16) -> f32 {
let sign = (b >> 15) as u32;
let biased_exp = ((b >> 10) & 0x1F) as u32;
let mantissa = (b & 0x3FF) as u32;
if biased_exp == 0 {
let v = (1.0f32 / 16384.0) * (mantissa as f32 * (1.0 / 1024.0));
return if sign != 0 { -v } else { v };
}
f32::from_bits((sign << 31) | ((biased_exp + 112) << 23) | (mantissa << 13))
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "f16c")]
fn f16_bits_to_f32_f16c(b: u16) -> f32 {
#[cfg(target_arch = "x86")]
use std::arch::x86::{_mm_cvtph_ps, _mm_cvtsi32_si128, _mm_cvtss_f32};
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::{_mm_cvtph_ps, _mm_cvtsi32_si128, _mm_cvtss_f32};
_mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(b as i32)))
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[derive(Clone, Copy)]
struct F16Converters {
f32_to_f16_bits: unsafe fn(f32) -> u16,
f16_bits_to_f32: unsafe fn(u16) -> f32,
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn select_f16_converters() -> F16Converters {
if std::is_x86_feature_detected!("f16c") {
return F16Converters {
f32_to_f16_bits: f32_to_f16_bits_f16c,
f16_bits_to_f32: f16_bits_to_f32_f16c,
};
}
F16Converters {
f32_to_f16_bits: f32_to_f16_bits_impl,
f16_bits_to_f32: f16_bits_to_f32_impl,
}
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
static F16_CONVERTERS: std::sync::OnceLock<F16Converters> = std::sync::OnceLock::new();
#[inline]
pub(crate) fn f16_bits_to_f32(b: u16) -> f32 {
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
{
use std::arch::aarch64::{vcvt_f32_f16, vdup_n_u16, vgetq_lane_f32, vreinterpret_f16_u16};
unsafe { vgetq_lane_f32::<0>(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(b)))) }
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
let converter = F16_CONVERTERS.get_or_init(select_f16_converters);
unsafe { (converter.f16_bits_to_f32)(b) }
}
#[cfg(not(any(
all(target_arch = "aarch64", feature = "neon"),
target_arch = "x86",
target_arch = "x86_64"
)))]
{
f16_bits_to_f32_impl(b)
}
}
pub(crate) fn sort_small_by<T>(
items: &mut [T],
mut compare: impl FnMut(&T, &T) -> std::cmp::Ordering,
) {
for i in 1..items.len() {
let mut j = i;
while j > 0 && compare(&items[j], &items[j - 1]).is_lt() {
items.swap(j, j - 1);
j -= 1;
}
}
}
#[cfg(test)]
mod allocation_tests {
use super::*;
#[test]
fn try_vec_reports_requested_byte_size() {
let size = isize::MAX as usize + 1;
assert_eq!(
try_vec![0u8; size].unwrap_err(),
EncodeError::AllocationFailed { size }
);
}
}
#[cfg(test)]
mod f16_tests {
use super::{f16_bits_to_f32, f32_to_f16_bits};
#[test]
fn known_hdr_luminances() {
assert_eq!(f32_to_f16_bits(0.0), 0x0000);
assert_eq!(f32_to_f16_bits(255.0), 0x5BF8);
assert_eq!(f32_to_f16_bits(1000.0), 0x63D0);
assert_eq!(f32_to_f16_bits(4000.0), 0x6BD0);
assert_eq!(f32_to_f16_bits(10000.0), 0x70E2);
}
#[test]
fn known_binary16_values() {
assert_eq!(f16_bits_to_f32(0x0000).to_bits(), 0.0f32.to_bits());
assert_eq!(f16_bits_to_f32(0x8000).to_bits(), (-0.0f32).to_bits());
assert_eq!(f16_bits_to_f32(0x0001), 2.0f32.powi(-24));
assert_eq!(f16_bits_to_f32(0x3C00), 1.0);
assert_eq!(f16_bits_to_f32(0x5BF8), 255.0);
assert_eq!(f16_bits_to_f32(0xFBFF), -65504.0);
}
}
#[cfg(test)]
mod small_sort_tests {
#[test]
fn small_sort_preserves_stable_order_for_bounded_candidates() {
let mut state = 0x1234_5678u32;
for len in 0..=16 {
for _ in 0..128 {
let mut input: Vec<_> = (0..len)
.map(|ordinal| {
state = state.wrapping_mul(1664525).wrapping_add(1013904223);
((state >> 29) as u8, ordinal)
})
.collect();
let mut expected = input.clone();
expected.sort_by_key(|&(key, _)| key);
super::sort_small_by(&mut input, |a, b| a.0.cmp(&b.0));
assert_eq!(input, expected);
}
}
}
}