Skip to content

Commit

Permalink
Added noise stage.
Browse files Browse the repository at this point in the history
  • Loading branch information
zond committed Dec 3, 2024
1 parent 5f2c941 commit 33be969
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 6 deletions.
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)
}
}
50 changes: 44 additions & 6 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,11 @@ impl Frame {
None
};

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

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

self.lf_global = Some(LfGlobalState {
splines,
noise,
lf_quant,
tree,
modular_global,
Expand All @@ -188,3 +190,39 @@ impl Frame {
Ok(())
}
}

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

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)?;
return Ok(result);
}

#[test]
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-4f32);
}
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 33be969

Please sign in to comment.