Skip to content

Commit 152acff

Browse files
author
HeroicKatora
authored
Merge pull request #165 from okaneco/small-perf
Make small perf refactors, clippy and grammar fixes
2 parents 1358eaa + 96ee9cd commit 152acff

File tree

5 files changed

+15
-23
lines changed

5 files changed

+15
-23
lines changed

src/decoder.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ impl<R: Read> Decoder<R> {
128128
}
129129

130130
/// Configure the decoder to scale the image during decoding.
131-
///
131+
///
132132
/// This efficiently scales the image by the smallest supported scale
133133
/// factor that produces an image larger than or equal to the requested
134134
/// size in at least one axis. The currently implemented scale factors
135135
/// are 1/8, 1/4, 1/2 and 1.
136-
///
136+
///
137137
/// To generate a thumbnail of an exact size, pass the desired size and
138138
/// then scale to the final size using a traditional resampling algorithm.
139139
pub fn scale(&mut self, requested_width: u16, requested_height: u16) -> Result<(u16, u16)> {
@@ -155,7 +155,7 @@ impl<R: Read> Decoder<R> {
155155
return Ok(Vec::new());
156156
}
157157
else if self.frame.is_none() && (self.reader.read_u8()? != 0xFF || Marker::from_u8(self.reader.read_u8()?) != Some(Marker::SOI)) {
158-
return Err(Error::Format("first two bytes is not a SOI marker".to_owned()));
158+
return Err(Error::Format("first two bytes are not an SOI marker".to_owned()));
159159
}
160160

161161
let mut previous_marker = Marker::SOI;
@@ -399,7 +399,7 @@ impl<R: Read> Decoder<R> {
399399
while byte == 0xFF {
400400
byte = self.reader.read_u8()?;
401401
}
402-
402+
403403
if byte != 0x00 && byte != 0xFF {
404404
return Ok(Marker::from_u8(byte).unwrap());
405405
}
@@ -613,14 +613,11 @@ fn decode_block<R: Read>(reader: &mut R,
613613
let value = huffman.decode(reader, dc_table.unwrap())?;
614614
let diff = match value {
615615
0 => 0,
616+
1..=11 => huffman.receive_extend(reader, value)?,
616617
_ => {
617618
// Section F.1.2.1.1
618619
// Table F.1
619-
if value > 11 {
620-
return Err(Error::Format("invalid DC difference magnitude category".to_owned()));
621-
}
622-
623-
huffman.receive_extend(reader, value)?
620+
return Err(Error::Format("invalid DC difference magnitude category".to_owned()));
624621
},
625622
};
626623

@@ -809,8 +806,8 @@ fn compute_image(components: &[Component],
809806
output_size: Dimensions,
810807
is_jfif: bool,
811808
color_transform: Option<AdobeColorTransform>) -> Result<Vec<u8>> {
812-
if data.iter().any(|data| data.is_empty()) {
813-
return Err(Error::Format("not all components has data".to_owned()));
809+
if data.iter().any(Vec::is_empty) {
810+
return Err(Error::Format("not all components have data".to_owned()));
814811
}
815812

816813
if components.len() == 1 {
@@ -908,7 +905,7 @@ fn choose_color_convert_func(component_count: usize,
908905
match color_transform {
909906
Some(AdobeColorTransform::Unknown) => Ok(color_convert_line_cmyk),
910907
Some(_) => Ok(color_convert_line_ycck),
911-
None => Err(Error::Format("4 components without Adobe APP14 metadata to tell color space".to_owned())),
908+
None => Err(Error::Format("4 components without Adobe APP14 metadata to indicate color space".to_owned())),
912909
}
913910
},
914911
_ => panic!(),
@@ -971,6 +968,5 @@ fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8) -> (u8, u8, u8) {
971968

972969
fn clamp_to_u8(value: i32) -> i32 {
973970
let value = std::cmp::max(value, 0);
974-
let value = std::cmp::min(value, 255);
975-
value
971+
std::cmp::min(value, 255)
976972
}

src/huffman.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use error::{Error, Result};
33
use marker::Marker;
44
use parser::ScanInfo;
55
use std::io::Read;
6-
use std::iter::repeat;
76

87
const LUT_BITS: u8 = 8;
98

@@ -254,8 +253,7 @@ fn derive_huffman_codes(bits: &[u8; 16]) -> Result<(Vec<u16>, Vec<u8>)> {
254253
let huffsize = bits.iter()
255254
.enumerate()
256255
.fold(Vec::new(), |mut acc, (i, &value)| {
257-
let mut repeated_size: Vec<u8> = repeat((i + 1) as u8).take(value as usize).collect();
258-
acc.append(&mut repeated_size);
256+
acc.extend(std::iter::repeat((i + 1) as u8).take(value as usize));
259257
acc
260258
});
261259

src/idct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) fn choose_idct_size(full_size: Dimensions, requested_size: Dimensions
1616
}
1717
}
1818

19-
return 8;
19+
8
2020
}
2121

2222
#[test]

src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ pub fn parse_dqt<R: Read>(reader: &mut R) -> Result<[Option<[u16; 64]>; 4]> {
437437

438438
let mut table = [0u16; 64];
439439

440-
for i in 0 .. 64 {
441-
table[i] = match precision {
440+
for item in table.iter_mut() {
441+
*item = match precision {
442442
0 => reader.read_u8()? as u16,
443443
1 => reader.read_u16::<BigEndian>()?,
444444
_ => unreachable!(),

src/upsampler.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ impl Upsample for UpsamplerH1V1 {
128128
output: &mut [u8]) {
129129
let input = &input[row * row_stride ..];
130130

131-
for i in 0 .. output_width {
132-
output[i] = input[i];
133-
}
131+
output[..output_width].copy_from_slice(&input[..output_width]);
134132
}
135133
}
136134

0 commit comments

Comments
 (0)