Skip to content

Commit 3afcdf2

Browse files
author
HeroicKatora
authored
Merge pull request #215 from paolobarbolini/array
Use arrays in more places
2 parents 55a4a9a + 911bbe6 commit 3afcdf2

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

src/decoder.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,8 @@ impl<R: Read> Decoder<R> {
668668
let block_offset = (block_y * component.block_size.width as usize + block_x) * 64;
669669
&mut mcu_row_coefficients[i][block_offset..block_offset + 64]
670670
} else {
671-
&mut dummy_block[..]
672-
};
671+
&mut dummy_block[..64]
672+
}.try_into().unwrap();
673673

674674
if scan.successive_approximation_high == 0 {
675675
decode_block(&mut self.reader,
@@ -755,16 +755,14 @@ impl<R: Read> Decoder<R> {
755755
}
756756

757757
fn decode_block<R: Read>(reader: &mut R,
758-
coefficients: &mut [i16],
758+
coefficients: &mut [i16; 64],
759759
huffman: &mut HuffmanDecoder,
760760
dc_table: Option<&HuffmanTable>,
761761
ac_table: Option<&HuffmanTable>,
762762
spectral_selection: Range<u8>,
763763
successive_approximation_low: u8,
764764
eob_run: &mut u16,
765765
dc_predictor: &mut i16) -> Result<()> {
766-
debug_assert_eq!(coefficients.len(), 64);
767-
768766
if spectral_selection.start == 0 {
769767
// Section F.2.2.1
770768
// Figure F.12
@@ -840,14 +838,12 @@ fn decode_block<R: Read>(reader: &mut R,
840838
}
841839

842840
fn decode_block_successive_approximation<R: Read>(reader: &mut R,
843-
coefficients: &mut [i16],
841+
coefficients: &mut [i16; 64],
844842
huffman: &mut HuffmanDecoder,
845843
ac_table: Option<&HuffmanTable>,
846844
spectral_selection: Range<u8>,
847845
successive_approximation_low: u8,
848846
eob_run: &mut u16) -> Result<()> {
849-
debug_assert_eq!(coefficients.len(), 64);
850-
851847
let bit = 1 << successive_approximation_low;
852848

853849
if spectral_selection.start == 0 {
@@ -926,13 +922,11 @@ fn decode_block_successive_approximation<R: Read>(reader: &mut R,
926922
}
927923

928924
fn refine_non_zeroes<R: Read>(reader: &mut R,
929-
coefficients: &mut [i16],
925+
coefficients: &mut [i16; 64],
930926
huffman: &mut HuffmanDecoder,
931927
range: Range<u8>,
932928
zrl: u8,
933929
bit: i16) -> Result<u8> {
934-
debug_assert_eq!(coefficients.len(), 64);
935-
936930
let last = range.end - 1;
937931
let mut zero_run_length = zrl;
938932

src/idct.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn test_choose_idct_size() {
4242
assert_eq!(choose_idct_size(Dimensions{width: 5472, height: 3648}, Dimensions{width: 16384, height: 16384}), 8);
4343
}
4444

45-
pub(crate) fn dequantize_and_idct_block(scale: usize, coefficients: &[i16], quantization_table: &[u16; 64], output_linestride: usize, output: &mut [u8]) {
45+
pub(crate) fn dequantize_and_idct_block(scale: usize, coefficients: &[i16; 64], quantization_table: &[u16; 64], output_linestride: usize, output: &mut [u8]) {
4646
match scale {
4747
8 => dequantize_and_idct_block_8x8(coefficients, quantization_table, output_linestride, output),
4848
4 => dequantize_and_idct_block_4x4(coefficients, quantization_table, output_linestride, output),
@@ -53,7 +53,7 @@ pub(crate) fn dequantize_and_idct_block(scale: usize, coefficients: &[i16], quan
5353
}
5454

5555
pub fn dequantize_and_idct_block_8x8(
56-
coefficients: &[i16],
56+
coefficients: &[i16; 64],
5757
quantization_table: &[u16; 64],
5858
output_linestride: usize,
5959
output: &mut [u8]
@@ -65,7 +65,7 @@ pub fn dequantize_and_idct_block_8x8(
6565

6666
// This is based on stb_image's 'stbi__idct_block'.
6767
fn dequantize_and_idct_block_8x8_inner<'a, I>(
68-
coefficients: &[i16],
68+
coefficients: &[i16; 64],
6969
quantization_table: &[u16; 64],
7070
output: I,
7171
) where
@@ -79,9 +79,6 @@ fn dequantize_and_idct_block_8x8_inner<'a, I>(
7979
output.len()
8080
);
8181

82-
// optimizer hint to eliminate bounds checks within loops
83-
assert!(coefficients.len() == 64);
84-
8582
let mut temp = [Wrapping(0); 64];
8683

8784
// columns
@@ -260,8 +257,7 @@ fn dequantize(c: i16, q: u16) -> Wrapping<i32> {
260257

261258
// 4x4 and 2x2 IDCT based on Rakesh Dugad and Narendra Ahuja: "A Fast Scheme for Image Size Change in the Compressed Domain" (2001).
262259
// http://sylvana.net/jpegcrop/jidctred/
263-
fn dequantize_and_idct_block_4x4(coefficients: &[i16], quantization_table: &[u16; 64], output_linestride: usize, output: &mut [u8]) {
264-
debug_assert_eq!(coefficients.len(), 64);
260+
fn dequantize_and_idct_block_4x4(coefficients: &[i16; 64], quantization_table: &[u16; 64], output_linestride: usize, output: &mut [u8]) {
265261
let mut temp = [Wrapping(0i32); 4 * 4];
266262

267263
const CONST_BITS: usize = 12;
@@ -317,9 +313,7 @@ fn dequantize_and_idct_block_4x4(coefficients: &[i16], quantization_table: &[u16
317313
}
318314
}
319315

320-
fn dequantize_and_idct_block_2x2(coefficients: &[i16], quantization_table: &[u16; 64], output_linestride: usize, output: &mut [u8]) {
321-
debug_assert_eq!(coefficients.len(), 64);
322-
316+
fn dequantize_and_idct_block_2x2(coefficients: &[i16; 64], quantization_table: &[u16; 64], output_linestride: usize, output: &mut [u8]) {
323317
const SCALE_BITS: usize = 3;
324318

325319
// Column 0
@@ -348,9 +342,7 @@ fn dequantize_and_idct_block_2x2(coefficients: &[i16], quantization_table: &[u16
348342
output[output_linestride + 1] = stbi_clamp((x2 - x3) >> SCALE_BITS);
349343
}
350344

351-
fn dequantize_and_idct_block_1x1(coefficients: &[i16], quantization_table: &[u16; 64], _output_linestride: usize, output: &mut [u8]) {
352-
debug_assert_eq!(coefficients.len(), 64);
353-
345+
fn dequantize_and_idct_block_1x1(coefficients: &[i16; 64], quantization_table: &[u16; 64], _output_linestride: usize, output: &mut [u8]) {
354346
let s0 = (Wrapping(coefficients[0] as i32 * quantization_table[0] as i32) + Wrapping(128 * 8)) / Wrapping(8);
355347
output[0] = stbi_clamp(s0);
356348
}

src/worker/immediate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use alloc::vec;
22
use alloc::vec::Vec;
33
use core::mem;
4+
use core::convert::TryInto;
45
use decoder::MAX_COMPONENTS;
56
use error::Result;
67
use idct::dequantize_and_idct_block;
@@ -46,7 +47,7 @@ impl ImmediateWorker {
4647
let x = (i % component.block_size.width as usize) * component.dct_scale;
4748
let y = (i / component.block_size.width as usize) * component.dct_scale;
4849

49-
let coefficients = &data[i * 64..(i + 1) * 64];
50+
let coefficients = data[i * 64..(i + 1) * 64].try_into().unwrap();
5051
let output = &mut self.results[index][self.offsets[index] + y * line_stride + x..];
5152

5253
dequantize_and_idct_block(component.dct_scale, coefficients, quantization_table, line_stride, output);

0 commit comments

Comments
 (0)