jxl-encoder 0.3.0

JPEG XL encoder in pure Rust
Documentation
// Copyright (c) Imazen LLC and the JPEG XL Project Authors.
// Algorithms and constants derived from libjxl (BSD-3-Clause).
// Licensed under AGPL-3.0-or-later. Commercial licenses at https://www.imazen.io/pricing

//! DCT constants and utility functions.

/// sqrt(2) constant used in DCT
pub const SQRT2: f32 = 1.41421356237;

/// 1/sqrt(2), used in inverse DCT to avoid division
pub const INV_SQRT2: f32 = 1.0 / 1.41421356237;

/// Multipliers for DCT computation.
/// Generated by: `1.0 / (2 * cos((i + 0.5) * pi / N))` for i in 0..N/2
// Copied directly from libjxl-tiny dct_scales.h
pub const WC_MULTIPLIERS_4: [f32; 2] = [0.541196100146197, 1.3065629648763764];

/// Reciprocals of WC_MULTIPLIERS_4 for inverse DCT (multiply instead of divide)
pub const INV_WC_MULTIPLIERS_4: [f32; 2] = [1.0 / 0.541196100146197, 1.0 / 1.3065629648763764];

// Copied directly from libjxl-tiny dct_scales.h
pub const WC_MULTIPLIERS_8: [f32; 4] = [
    0.5097955791041592,
    0.6013448869350453,
    0.8999762231364156,
    2.5629154477415055,
];

/// Reciprocals of WC_MULTIPLIERS_8 for inverse DCT (multiply instead of divide)
pub const INV_WC_MULTIPLIERS_8: [f32; 4] = [
    1.0 / 0.5097955791041592,
    1.0 / 0.6013448869350453,
    1.0 / 0.8999762231364156,
    1.0 / 2.5629154477415055,
];

// Copied directly from libjxl-tiny dct_scales.h
pub const WC_MULTIPLIERS_16: [f32; 8] = [
    0.5024192861881557,
    0.5224986149396889,
    0.5669440348163577,
    0.6468217833599901,
    0.7881546234512502,
    1.060677685990347,
    1.7224470982383342,
    5.101148618689155,
];

/// Reciprocals of WC_MULTIPLIERS_16 for inverse DCT (multiply instead of divide)
pub const INV_WC_MULTIPLIERS_16: [f32; 8] = [
    1.0 / 0.5024192861881557,
    1.0 / 0.5224986149396889,
    1.0 / 0.5669440348163577,
    1.0 / 0.6468217833599901,
    1.0 / 0.7881546234512502,
    1.0 / 1.060677685990347,
    1.0 / 1.7224470982383342,
    1.0 / 5.101148618689155,
];

/// Resample scales for DC extraction from larger DCTs.
// Copied directly from libjxl-tiny dct_scales.h
pub const DCT_RESAMPLE_SCALE_16_TO_2: [f32; 2] = [1.000000000000000000, 0.901764195028874394];

pub const DCT_RESAMPLE_SCALE_2_TO_16: [f32; 2] = [1.000000000000000000, 1.108937353592731823];

/// WC multipliers for N=32.
/// Generated by: `1.0 / (2 * cos((i + 0.5) * pi / 32))` for i in 0..16.
/// Copied from libjxl dct_scales.h.
pub const WC_MULTIPLIERS_32: [f32; 16] = [
    0.5006029982351963,
    0.5054709598975436,
    0.5154473099226246,
    0.5310425910897841,
    0.5531038960344445,
    0.5829349682061339,
    0.6225041230356648,
    0.6748083414550057,
    0.7445362710022986,
    0.8393496454155268,
    0.9725682378619608,
    1.1694399334328847,
    1.4841646163141662,
    2.057781009953411,
    3.407608418468719,
    10.190008123548033,
];

/// Reciprocals of WC_MULTIPLIERS_32 for inverse DCT (multiply instead of divide)
pub const INV_WC_MULTIPLIERS_32: [f32; 16] = [
    1.0 / 0.5006029982351963,
    1.0 / 0.5054709598975436,
    1.0 / 0.5154473099226246,
    1.0 / 0.5310425910897841,
    1.0 / 0.5531038960344445,
    1.0 / 0.5829349682061339,
    1.0 / 0.6225041230356648,
    1.0 / 0.6748083414550057,
    1.0 / 0.7445362710022986,
    1.0 / 0.8393496454155268,
    1.0 / 0.9725682378619608,
    1.0 / 1.1694399334328847,
    1.0 / 1.4841646163141662,
    1.0 / 2.057781009953411,
    1.0 / 3.407608418468719,
    1.0 / 10.190008123548033,
];

/// Resample scales for DC extraction from 32-point DCT to 4-point domain.
/// DCTTotalResampleScale<32, 4> from libjxl.
pub const DCT_RESAMPLE_SCALE_32_TO_4: [f32; 4] = [
    1.0,
    0.974886821136879522,
    0.901764195028874394,
    0.787054918159101335,
];

/// WcMultipliers<64> from libjxl dct_scales.h:280-291.
/// 32 constants for the 64-point DCT butterfly.
pub const WC_MULTIPLIERS_64: [f32; 32] = [
    0.500150636020651,
    0.5013584524464084,
    0.5037887256810443,
    0.5074711720725553,
    0.5124514794082247,
    0.5187927131053328,
    0.52657731515427,
    0.535909816907992,
    0.5469204379855088,
    0.5597698129470802,
    0.57465518403266,
    0.5918185358574165,
    0.6115573478825099,
    0.6342389366884031,
    0.6603198078137061,
    0.6903721282002123,
    0.7251205223771985,
    0.7654941649730891,
    0.8127020908144905,
    0.8683447152233481,
    0.9345835970364075,
    1.0144082649970547,
    1.1120716205797176,
    1.233832737976571,
    1.3892939586328277,
    1.5939722833856311,
    1.8746759800084078,
    2.282050068005162,
    2.924628428158216,
    4.084611078129248,
    6.796750711673633,
    20.373878167231453,
];

/// Reciprocals of WC_MULTIPLIERS_64 for inverse DCT (multiply instead of divide)
pub const INV_WC_MULTIPLIERS_64: [f32; 32] = [
    1.0 / 0.500150636020651,
    1.0 / 0.5013584524464084,
    1.0 / 0.5037887256810443,
    1.0 / 0.5074711720725553,
    1.0 / 0.5124514794082247,
    1.0 / 0.5187927131053328,
    1.0 / 0.52657731515427,
    1.0 / 0.535909816907992,
    1.0 / 0.5469204379855088,
    1.0 / 0.5597698129470802,
    1.0 / 0.57465518403266,
    1.0 / 0.5918185358574165,
    1.0 / 0.6115573478825099,
    1.0 / 0.6342389366884031,
    1.0 / 0.6603198078137061,
    1.0 / 0.6903721282002123,
    1.0 / 0.7251205223771985,
    1.0 / 0.7654941649730891,
    1.0 / 0.8127020908144905,
    1.0 / 0.8683447152233481,
    1.0 / 0.9345835970364075,
    1.0 / 1.0144082649970547,
    1.0 / 1.1120716205797176,
    1.0 / 1.233832737976571,
    1.0 / 1.3892939586328277,
    1.0 / 1.5939722833856311,
    1.0 / 1.8746759800084078,
    1.0 / 2.282050068005162,
    1.0 / 2.924628428158216,
    1.0 / 4.084611078129248,
    1.0 / 6.796750711673633,
    1.0 / 20.373878167231453,
];

/// Resample scales for DC extraction from 64-point DCT to 8-point domain.
/// DCTResampleScales<64, 8> from libjxl dct_scales.h:72-79.
pub const DCT_RESAMPLE_SCALE_64_TO_8: [f32; 8] = [
    1.0000000000000000,
    0.9936866130906366,
    0.9748868211368796,
    0.9440180941651672,
    0.9017641950288744,
    0.8490574973847023,
    0.7870549181591013,
    0.7171081282466044,
];

/// Transpose an NxM block in-place (actually copies to output).
pub fn transpose<const N: usize, const M: usize>(input: &[f32], output: &mut [f32]) {
    for row in 0..N {
        for col in 0..M {
            output[col * N + row] = input[row * M + col];
        }
    }
}