rximg/src/quantization.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

use crate::constants::TILE_SZ;
const ZIGZAG: [u8; TILE_SZ * TILE_SZ] = [
0 , 1 , 5 , 6 , 14, 15, 27, 28,
2 , 4 , 7 , 13, 16, 26, 29, 42,
3 , 8 , 12, 17, 25, 30, 41, 43,
9 , 11, 18, 24, 31, 40, 44, 53,
10, 19, 23, 32, 39, 45, 52, 54,
20, 22, 33, 38, 46, 51, 55, 60,
21, 34, 37, 47, 50, 56, 59, 61,
35, 36, 48, 49, 57, 58, 62, 63,
];
const DIVISORS: [u8; TILE_SZ * TILE_SZ] = [
// source: https://en.wikipedia.org/wiki/Quantization_(image_processing)
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68, 109, 103, 77,
24, 35, 55, 64, 81, 104, 113, 92,
49, 64, 78, 87, 103, 121, 120, 101,
72, 92, 95, 98, 112, 100, 103, 99,
];
pub fn to_quantized(
coefs: [i32; TILE_SZ * TILE_SZ]
) -> [i32; TILE_SZ * TILE_SZ] {
let mut quant: [i32; TILE_SZ * TILE_SZ] = [0; TILE_SZ * TILE_SZ];
for cf_ix in 0..TILE_SZ * TILE_SZ {
quant[ZIGZAG[cf_ix] as usize] =
coefs[cf_ix]
// (coefs[cf_ix] + DIVISORS[cf_ix] as i32 / 2) /
// (DIVISORS[cf_ix] as i32);
}
quant
}
pub fn from_quantized(
quant: [i32; TILE_SZ* TILE_SZ]
) -> [i32; TILE_SZ * TILE_SZ] {
let mut coefs: [i32; TILE_SZ * TILE_SZ] = [0; TILE_SZ * TILE_SZ];
for cf_ix in 0..TILE_SZ * TILE_SZ {
coefs[cf_ix] = quant[ZIGZAG[cf_ix] as usize]; // * DIVISORS[cf_ix] as i32;
}
coefs
}