Skip to content

Commit

Permalink
Added noise stage.
Browse files Browse the repository at this point in the history
  • Loading branch information
zond authored and mo271 committed Dec 3, 2024
1 parent a096a89 commit d6d0ea7
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 5 deletions.
Binary file added jxl/resources/test/8x8_noise.jxl
Binary file not shown.
1 change: 1 addition & 0 deletions jxl/src/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

pub mod noise;
pub mod spline;
20 changes: 20 additions & 0 deletions jxl/src/features/noise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

use crate::{bit_reader::BitReader, error::Result};
#[derive(Debug, PartialEq, Default)]
pub struct Noise {
pub lut: [f32; 8],
}

impl Noise {
pub fn read(br: &mut BitReader) -> Result<Noise> {
let mut noise = Noise::default();
for l in &mut noise.lut {
*l = (br.read(10)? as f32) / ((1 << 10) as f32);
}
Ok(noise)
}
}
27 changes: 22 additions & 5 deletions jxl/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::{
bit_reader::BitReader,
error::Result,
features::spline::Splines,
features::{noise::Noise, spline::Splines},
headers::{
color_encoding::ColorSpace,
encodings::UnconditionalCoder,
Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct LfGlobalState {
// TODO(veluca93): patches
// TODO(veluca93): splines
splines: Option<Splines>,
// TODO(veluca93): noise
noise: Option<Noise>,
lf_quant: LfQuantFactors,
// TODO(veluca93), VarDCT: HF quant matrices
// TODO(veluca93), VarDCT: block context map
Expand Down Expand Up @@ -145,10 +145,12 @@ impl Frame {
None
};

if self.header.has_noise() {
let noise = if self.header.has_noise() {
info!("decoding noise");
todo!("noise not implemented");
}
Some(Noise::read(br)?)
} else {
None
};

let lf_quant = LfQuantFactors::new(br)?;
debug!(?lf_quant);
Expand Down Expand Up @@ -180,6 +182,7 @@ impl Frame {

self.lf_global = Some(LfGlobalState {
splines,
noise,
lf_quant,
tree,
modular_global,
Expand All @@ -197,6 +200,7 @@ mod test_frame {
error::Error,
features::spline::Point,
headers::{FileHeader, JxlHeader},
util::test::assert_almost_eq,
};

use super::Frame;
Expand Down Expand Up @@ -262,4 +266,17 @@ mod test_frame {
assert_eq!(spline.sigma_dct, EXPECTED_SIGMA_DCT);
Ok(())
}

fn noise() -> Result<(), Error> {
let frame = read_frame(include_bytes!("../resources/test/8x8_noise.jxl"))?;
let lf_global = frame.lf_global.unwrap();
let noise = lf_global.noise.unwrap();
let want_noise = [
0.000000, 0.000977, 0.002930, 0.003906, 0.005859, 0.006836, 0.008789, 0.010742,
];
for (index, noise_param) in want_noise.iter().enumerate() {
assert_almost_eq!(noise.lut[index], *noise_param, 1e-6f32);
}
Ok(())
}
}
10 changes: 10 additions & 0 deletions jxl/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ impl<T: ImageDataType> Image<T> {
Ok(img)
}

#[cfg(test)]
pub fn new_range(size: (usize, usize), start: f32, step: f32) -> Result<Image<T>> {
let mut img = Self::new(size)?;
img.data
.iter_mut()
.enumerate()
.for_each(|(index, x)| *x = T::from_f64((start + step * index as f32) as f64));
Ok(img)
}

#[cfg(test)]
pub fn new_constant(size: (usize, usize), val: T) -> Result<Image<T>> {
let mut img = Self::new(size)?;
Expand Down
1 change: 1 addition & 0 deletions jxl/src/render/stages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
mod chroma_upsample;
mod convert;
mod nearest_neighbor;
mod noise;
mod save;
mod upsample;

Expand Down
Loading

0 comments on commit d6d0ea7

Please sign in to comment.