Skip to content

Commit

Permalink
add basic spline test
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Bruse <[email protected]>
  • Loading branch information
mo271 and Martin Bruse committed Dec 3, 2024
1 parent 5f2c941 commit df10717
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
Binary file added jxl/resources/test/splines.jxl
Binary file not shown.
20 changes: 10 additions & 10 deletions jxl/src/features/spline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const NUM_SPLINE_CONTEXTS: usize = 6;

#[derive(Debug, Clone, Copy)]
pub struct Point {
x: f32,
y: f32,
pub x: f32,
pub y: f32,
}

impl Point {
Expand All @@ -51,12 +51,12 @@ pub struct Spline {
sigma_dct: [f32; 32],
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct QuantizedSpline {
// Double delta-encoded.
control_points: Vec<(i64, i64)>,
color_dct: [[i32; 32]; 3],
sigma_dct: [i32; 32],
pub control_points: Vec<(i64, i64)>,
pub color_dct: [[i32; 32]; 3],
pub sigma_dct: [i32; 32],
}

impl QuantizedSpline {
Expand Down Expand Up @@ -120,11 +120,11 @@ struct SplineSegment {
color: [f32; 3],
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Splines {
quantization_adjustment: i32,
splines: Vec<QuantizedSpline>,
starting_points: Vec<Point>,
pub quantization_adjustment: i32,
pub splines: Vec<QuantizedSpline>,
pub starting_points: Vec<Point>,
}

impl Splines {
Expand Down
75 changes: 75 additions & 0 deletions jxl/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,78 @@ impl Frame {
Ok(())
}
}

#[cfg(test)]
mod test_frame {
use crate::{
bit_reader::BitReader,
container::ContainerParser,
error::Error,
features::spline::Point,
headers::{FileHeader, JxlHeader},
};

use super::Frame;

fn read_frame(image: &[u8]) -> Result<Frame, Error> {
let codestream = ContainerParser::collect_codestream(image).unwrap();
let mut br = BitReader::new(&codestream);
let file_header = FileHeader::read(&mut br).unwrap();
let mut result = Frame::new(&mut br, &file_header)?;
result.decode_lf_global(&mut br)?;
Ok(result)
}

#[test]
fn splines() -> Result<(), Error> {
let frame = read_frame(include_bytes!("../resources/test/splines.jxl"))?;
let lf_global = frame.lf_global.unwrap();
let splines = lf_global.splines.unwrap();
assert_eq!(splines.quantization_adjustment, 0);
let expected_starting_points = [Point { x: 9.0, y: 54.0 }].to_vec();
assert_eq!(splines.starting_points, expected_starting_points);
assert_eq!(splines.splines.len(), 1);
let spline = splines.splines[0].clone();

let expected_control_points = [
(109, 105),
(-130, -261),
(-66, 193),
(227, -52),
(-170, 290),
]
.to_vec();
assert_eq!(spline.control_points.clone(), expected_control_points);

const EXPECTED_COLOR_DCT: [[i32; 32]; 3] = [
{
let mut row = [0; 32];
row[0] = 168;
row[1] = 119;
row
},
{
let mut row = [0; 32];
row[0] = 9;
row[2] = 7;
row
},
{
let mut row = [0; 32];
row[0] = -10;
row[1] = 7;
row
},
];
assert_eq!(spline.color_dct, EXPECTED_COLOR_DCT);

const EXPECTED_SIGMA_DCT: [i32; 32] = {
let mut dct = [0; 32];
dct[0] = 4;
dct[7] = 2;
dct
};
assert_eq!(spline.sigma_dct, EXPECTED_SIGMA_DCT);
Ok(())
}
}

0 comments on commit df10717

Please sign in to comment.