jixel 0.2.19

Tiny JPEG XL encoder
Documentation
/*
 * // Copyright (c) Radzivon Bartoshyk 7/2026. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
use core::arch::wasm32::*;

#[allow(clippy::too_many_arguments)]
#[target_feature(enable = "simd128")]
pub(crate) fn quantize_block_ac_wasm(
    block_in: &[f32],
    c: usize,
    qm: &[f32],
    quant: i32,
    scale: f32,
    qm_multiplier: f32,
    distance: f32,
    xsize: usize,
    ysize: usize,
    block_out: &mut [i32],
) {
    let width = xsize * 8;
    let height = ysize * 8;
    let n = width * height;

    debug_assert_eq!(qm.len(), n);
    debug_assert!(block_in.len() >= n, "block_in too small");
    debug_assert!(block_out.len() >= n, "block_out too small");
    debug_assert!(width.is_multiple_of(4));

    let qm = &qm[..n];
    let block_in = &block_in[..n];
    let block_out = &mut block_out[..n];

    let thr = crate::group::quantize_ac_thresholds(c, xsize, ysize, distance);
    let q_scaled = crate::group::quantize_ac_q_scaled(quant, scale, qm_multiplier);

    let half = width / 2;
    let qs = f32x4_splat(q_scaled);
    let zero_i = i32x4_splat(0);

    for (y, ((qm_row, in_row), out_row)) in qm
        .chunks_exact(width)
        .zip(block_in.chunks_exact(width))
        .zip(block_out.chunks_exact_mut(width))
        .take(height)
        .enumerate()
    {
        let yfix = if y >= height / 2 { 2 } else { 0 };
        let thr_lo = thr[yfix];
        let thr_hi = thr[yfix + 1];

        for (x0, ((qm4, in4), out4)) in qm_row
            .as_chunks::<4>()
            .0
            .iter()
            .zip(in_row.as_chunks::<4>().0.iter())
            .zip(out_row.as_chunks_mut::<4>().0.iter_mut())
            .enumerate()
        {
            let x = x0 * 4;
            let threshold = if x >= half { thr_hi } else { thr_lo };
            let qmv = unsafe { v128_load(qm4.as_ptr().cast()) };
            let inv = unsafe { v128_load(in4.as_ptr().cast()) };
            let val = f32x4_mul(f32x4_mul(qmv, qs), inv);
            let keep = f32x4_ge(f32x4_abs(val), f32x4_splat(threshold));
            let truncated = f32x4_trunc(val);
            let frac = f32x4_sub(val, truncated);
            let ge_half = f32x4_ge(f32x4_abs(frac), f32x4_splat(0.5));
            let signed_one = v128_or(f32x4_splat(1.0), v128_and(val, f32x4_splat(-0.0)));
            let q = i32x4_trunc_sat_f32x4(f32x4_add(truncated, v128_and(signed_one, ge_half)));
            let q = v128_bitselect(q, zero_i, keep);
            unsafe { v128_store(out4.as_mut_ptr().cast(), q) };
        }
    }
}

#[inline]
#[target_feature(enable = "simd128")]
fn round_ties_away_i32x4(value: v128) -> v128 {
    let truncated = f32x4_trunc(value);
    let fraction = f32x4_sub(value, truncated);
    let at_least_half = f32x4_ge(f32x4_abs(fraction), f32x4_splat(0.5));
    let signed_one = v128_or(f32x4_splat(1.0), v128_and(value, f32x4_splat(-0.0)));
    i32x4_trunc_sat_f32x4(f32x4_add(truncated, v128_and(signed_one, at_least_half)))
}

#[inline]
#[target_feature(enable = "simd128")]
fn apply_quant_field_gain_x4(input: v128, gain: v128) -> v128 {
    let value = f32x4_mul(f32x4_convert_u32x4(input), gain);
    let rounded = round_ties_away_i32x4(value);
    let clamped = i32x4_min(i32x4_max(rounded, i32x4_splat(1)), i32x4_splat(255));
    v128_bitselect(clamped, i32x4_splat(0), f32x4_eq(value, value))
}

#[inline]
#[target_feature(enable = "simd128")]
fn apply_quant_field_gain_x8(source: &mut [u8; 8], gain: v128) {
    let bytes = unsafe { v128_load64_zero(source.as_ptr().cast()) };
    let values16 = i16x8_extend_low_u8x16(bytes);
    let low = apply_quant_field_gain_x4(i32x4_extend_low_u16x8(values16), gain);
    let high = apply_quant_field_gain_x4(i32x4_extend_high_u16x8(values16), gain);
    let packed16 = i16x8_narrow_i32x4(low, high);
    let packed8 = u8x16_narrow_i16x8(packed16, packed16);
    unsafe { v128_store64_lane::<0>(packed8, source.as_mut_ptr().cast()) };
}

#[target_feature(enable = "simd128")]
pub(crate) fn apply_quant_field_gain_wasm(
    image: &mut crate::image::ImageB,
    x0: usize,
    y0: usize,
    width: usize,
    height: usize,
    gain: f32,
) {
    let gain = f32x4_splat(gain);
    for y in y0..y0 + height {
        let values = &mut image.row_mut(y)[x0..x0 + width];
        let (values8, tail) = values.as_chunks_mut::<8>();
        for values in values8 {
            apply_quant_field_gain_x8(values, gain);
        }
        if !tail.is_empty() {
            let mut values = [0u8; 8];
            values[..tail.len()].copy_from_slice(tail);
            apply_quant_field_gain_x8(&mut values, gain);
            tail.copy_from_slice(&values[..tail.len()]);
        }
    }
}

#[inline]
#[target_feature(enable = "simd128")]
fn apply_structure_aq_x4(correction: v128, input: v128, scale: v128, center: v128) -> v128 {
    let delta = f32x4_mul(f32x4_sub(correction, center), scale);
    let clamped = f32x4_max(f32x4_min(delta, f32x4_splat(0.22)), f32x4_splat(-0.18));
    let delta = v128_bitselect(clamped, delta, f32x4_eq(delta, delta));
    apply_quant_field_gain_x4(input, super::adaptive_quant::fast_exp2_x4(delta))
}

#[inline]
#[target_feature(enable = "simd128")]
fn apply_structure_aq_x8(corrections: &[f32; 8], dest: &mut [u8; 8], scale: v128, center: v128) {
    let bytes = unsafe { v128_load64_zero(dest.as_ptr().cast()) };
    let values16 = i16x8_extend_low_u8x16(bytes);
    let correction_low = unsafe { v128_load(corrections.as_ptr().cast()) };
    let correction_high = unsafe { v128_load(corrections.as_ptr().add(4).cast()) };
    let low = apply_structure_aq_x4(
        correction_low,
        i32x4_extend_low_u16x8(values16),
        scale,
        center,
    );
    let high = apply_structure_aq_x4(
        correction_high,
        i32x4_extend_high_u16x8(values16),
        scale,
        center,
    );
    let packed16 = i16x8_narrow_i32x4(low, high);
    let packed8 = u8x16_narrow_i16x8(packed16, packed16);
    unsafe { v128_store64_lane::<0>(packed8, dest.as_mut_ptr().cast()) };
}

#[target_feature(enable = "simd128")]
pub(crate) fn apply_structure_aq_wasm(
    corrections: &[f32],
    field: &mut crate::image::ImageB,
    amount: f32,
    center: f32,
    inv_stddev: f32,
) {
    let values = field.as_mut_slice();
    debug_assert_eq!(corrections.len(), values.len());
    let (corrections8, correction_tail) = corrections.as_chunks::<8>();
    let (values8, value_tail) = values.as_chunks_mut::<8>();
    let scale = f32x4_splat(-amount * inv_stddev);
    let center_vector = f32x4_splat(center);
    for (corrections, values) in corrections8.iter().zip(values8) {
        apply_structure_aq_x8(corrections, values, scale, center_vector);
    }
    if !correction_tail.is_empty() {
        let mut corrections = [center; 8];
        corrections[..correction_tail.len()].copy_from_slice(correction_tail);
        let mut values = [0u8; 8];
        values[..value_tail.len()].copy_from_slice(value_tail);
        apply_structure_aq_x8(&corrections, &mut values, scale, center_vector);
        value_tail.copy_from_slice(&values[..value_tail.len()]);
    }
}

#[inline]
#[target_feature(enable = "simd128")]
fn store_i16x4(output: &mut [i16; 4], value: v128) {
    let packed = i16x8_narrow_i32x4(value, value);
    unsafe { v128_store64_lane::<0>(packed, output.as_mut_ptr().cast()) };
}

#[target_feature(enable = "simd128")]
pub(crate) fn quantize_dc_wasm(input: &[f32], scale: f32, output: &mut [i16]) {
    debug_assert_eq!(input.len(), output.len());
    let (input4, input_tail) = input.as_chunks::<4>();
    let (output4, output_tail) = output.as_chunks_mut::<4>();
    let scale = f32x4_splat(scale);

    for (source, target) in input4.iter().zip(output4) {
        let value = unsafe { v128_load(source.as_ptr().cast()) };
        store_i16x4(target, round_ties_away_i32x4(f32x4_mul(value, scale)));
    }

    if !input_tail.is_empty() {
        let mut source = [0.0; 4];
        source[..input_tail.len()].copy_from_slice(input_tail);
        let value = unsafe { v128_load(source.as_ptr().cast()) };
        let mut target = [0i16; 4];
        store_i16x4(&mut target, round_ties_away_i32x4(f32x4_mul(value, scale)));
        output_tail.copy_from_slice(&target[..input_tail.len()]);
    }
}

#[target_feature(enable = "simd128")]
pub(crate) fn quantize_dc_cfl_wasm(
    input: &[f32],
    y_quant: &[i16],
    scale: f32,
    cfl: f32,
    output: &mut [i16],
) {
    debug_assert_eq!(input.len(), y_quant.len());
    debug_assert_eq!(input.len(), output.len());
    let (input4, input_tail) = input.as_chunks::<4>();
    let (y4, y_tail) = y_quant.as_chunks::<4>();
    let (output4, output_tail) = output.as_chunks_mut::<4>();
    let scale = f32x4_splat(scale);
    let negative_cfl = f32x4_splat(-cfl);

    for ((source, y), target) in input4.iter().zip(y4).zip(output4) {
        let value = unsafe { v128_load(source.as_ptr().cast()) };
        let y = unsafe { v128_load64_zero(y.as_ptr().cast()) };
        let y = f32x4_convert_i32x4(i32x4_extend_low_i16x8(y));
        let value = f32x4_add(f32x4_mul(value, scale), f32x4_mul(y, negative_cfl));
        store_i16x4(target, round_ties_away_i32x4(value));
    }

    if !input_tail.is_empty() {
        let mut source = [0.0; 4];
        source[..input_tail.len()].copy_from_slice(input_tail);
        let mut y = [0i16; 4];
        y[..y_tail.len()].copy_from_slice(y_tail);
        let value = unsafe { v128_load(source.as_ptr().cast()) };
        let y = unsafe { v128_load64_zero(y.as_ptr().cast()) };
        let y = f32x4_convert_i32x4(i32x4_extend_low_i16x8(y));
        let value = f32x4_add(f32x4_mul(value, scale), f32x4_mul(y, negative_cfl));
        let mut target = [0i16; 4];
        store_i16x4(&mut target, round_ties_away_i32x4(value));
        output_tail.copy_from_slice(&target[..input_tail.len()]);
    }
}