use super::ma_sampling::{ma_channel_sample_count, sample_channel_ma, walk_channel_ma};
use super::predictor::{predictor_neighbors, predictor_value};
use crate::entropy::{pack_signed, uint_encode};
use crate::ma_tree::{MaSamples, NUM_MA_PREDS};
use crate::weighted_predictor::{WpParams, WpState};
#[test]
fn row_walk_matches_point_fetched_properties_and_predictions() {
for width in [0usize, 1, 2, 3, 4, 5, 17, 257] {
for height in [0usize, 1, 2, 7] {
for flat in [false, true] {
let plane: Vec<i32> = (0..width * height)
.map(|i| {
if flat {
8192
} else {
[i32::MIN, i32::MAX, -65536, 65535, -8192, 0, 8192]
[(i * 17 + i / width) % 7]
}
})
.collect();
let get = |x: usize, y: usize| plane[y * width + x];
for ¶ms in &WpParams::PRESETS {
for use_wp in [false, true] {
let mut wp = WpState::with_params(width, params);
let mut previous_gradient = 0i32;
let mut count = 0;
walk_channel_ma(
|y| &plane[y * width..][..width],
width,
height,
2,
0,
&[],
params,
use_wp,
|x, y, value, props, neighbors, weighted| {
assert_eq!(count, y * width + x);
assert_eq!(value, get(x, y) as i64);
count += 1;
if x == 0 {
previous_gradient = 0;
}
let n = predictor_neighbors(&get, x, y, width);
let expected_wp = if use_wp {
wp.predict(
x,
y,
n.top,
n.left,
n.top_right,
n.top_left,
n.top_top,
)
} else {
0
};
let gradient = (n.left + n.top - n.top_left) as i32;
assert!(props[16..].iter().all(|&v| v == 0));
assert_eq!(
props[..16],
[
2,
0,
y as i32,
x as i32,
n.top.abs() as i32,
n.left.abs() as i32,
n.top as i32,
n.left as i32,
(n.left - previous_gradient as i64) as i32,
gradient,
(n.left - n.top_left) as i32,
(n.top_left - n.top) as i32,
(n.top - n.top_right) as i32,
(n.top - n.top_top) as i32,
(n.left - n.left_left) as i32,
if use_wp { wp.wp_prop as i32 } else { 0 },
]
);
assert_eq!(weighted, expected_wp);
for pred in 0..NUM_MA_PREDS as u32 {
assert_eq!(
predictor_value(pred, neighbors, weighted),
predictor_value(pred, n, expected_wp),
);
}
previous_gradient = gradient;
if use_wp {
wp.update(value, x, y);
}
},
);
assert_eq!(count, width * height);
}
}
}
}
}
}
fn reference_samples(
plane: &[i32],
width: usize,
height: usize,
stride: usize,
params: WpParams,
) -> MaSamples {
let mut samples = MaSamples::new();
walk_channel_ma(
|y| &plane[y * width..][..width],
width,
height,
2,
0,
&[],
params,
true,
|x, y, value, p, n, wp_pred| {
if x % stride != y % stride {
return;
}
let mut tok = [0u8; NUM_MA_PREDS];
for pred in 0..NUM_MA_PREDS as u32 {
let prediction = predictor_value(pred, n, wp_pred);
let (token, _, _) = uint_encode(pack_signed((value - prediction) as i32));
tok[pred as usize] = token.min(u8::MAX as u32) as u8;
}
samples.push(*p, tok);
},
);
samples
}
#[test]
fn stride_aware_sampling_matches_full_property_walk() {
for &(width, height) in &[(0, 3), (1, 1), (1, 7), (2, 5), (9, 7), (17, 4)] {
let plane: Vec<i32> = (0..width * height)
.map(|i| {
let x = i % width.max(1);
let y = i / width.max(1);
(((x * 977 + y * 619) ^ (x * y * 37)) as i32 & 0xffff) - 0x7fff
})
.collect();
let get_row = |y: usize| &plane[y * width..][..width];
for &stride in &[1, 2, 3, 5, 11, width * height + 1] {
for ¶ms in &WpParams::PRESETS {
let expected = reference_samples(&plane, width, height, stride, params);
let mut actual = MaSamples::new();
sample_channel_ma(
get_row,
width,
height,
2,
0,
&[],
params,
true,
stride,
|p, t, _| actual.push(p, t),
);
assert_eq!(actual.len(), ma_channel_sample_count(width, height, stride));
assert_eq!(
actual.props, expected.props,
"properties: {width}x{height}, stride={stride}, params={params:?}"
);
assert_eq!(
actual.tok, expected.tok,
"tokens: {width}x{height}, stride={stride}, params={params:?}"
);
}
}
}
}