Skip to content

Commit

Permalink
refactor decoder::refine_non_zeroes
Browse files Browse the repository at this point in the history
 - Fixes image-rs#132
 - improve readability
 - improve performance

 Criterion report on my computer:

 Benchmarking decode a 512x512 progressive JPEG: Collecting 100 samples in estimated 27.108 s (5050 i                                                                                                    decode a 512x512 progressive JPEG
                         time:   [5.3440 ms 5.3549 ms 5.3669 ms]
                         change: [-24.307% -23.085% -21.920%] (p = 0.00 < 0.05)
                         Performance has improved.
 Found 3 outliers among 100 measurements (3.00%)
   2 (2.00%) high mild
   1 (1.00%) high severe
  • Loading branch information
lovasoa committed Feb 13, 2020
1 parent 83368db commit a0c5c6d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,26 +739,19 @@ fn refine_non_zeroes<R: Read>(reader: &mut R,
bit: i16) -> Result<u8> {
debug_assert_eq!(coefficients.len(), 64);

let last = range.end - 1;
let mut last= range.start;
let mut zero_run_length = zrl;

for i in range {
last = i;
let index = UNZIGZAG[i as usize] as usize;
let coefficient = &mut coefficients[index];

if coefficients[index] == 0 {
if zero_run_length == 0 {
return Ok(i);
}

if *coefficient == 0 {
if zero_run_length == 0 { break; }
zero_run_length -= 1;
}
else if huffman.get_bits(reader, 1)? == 1 && coefficients[index] & bit == 0 {
if coefficients[index] > 0 {
coefficients[index] += bit;
}
else {
coefficients[index] -= bit;
}
} else if huffman.get_bits(reader, 1)? == 1 && *coefficient & bit == 0 {
*coefficient |= bit;
}
}

Expand Down
Binary file added tests/crashtest/images/issue132.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reftest/images/mozilla/jpg-progressive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/reftest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ fn reftest_decoder<T: std::io::Read>(mut decoder: jpeg::Decoder<T>, path: &Path,
}).collect();

if pixels.iter().any(|&a| a < 255) {
let output_path = path.with_file_name(format!("{}-actual.png", path.file_stem().unwrap().to_str().unwrap()));
let output = File::create(&output_path).unwrap();
let mut encoder = png::Encoder::new(output, info.width as u32, info.height as u32);
encoder.set(png::BitDepth::Eight);
encoder.set(png::ColorType::RGB);
encoder.write_header().expect("png failed to write header").write_image_data(&data).expect("png failed to write data");

let output_path = path.with_file_name(format!("{}-diff.png", path.file_stem().unwrap().to_str().unwrap()));
let output = File::create(&output_path).unwrap();
let mut encoder = png::Encoder::new(output, info.width as u32, info.height as u32);
Expand Down

0 comments on commit a0c5c6d

Please sign in to comment.