Skip to content

Commit

Permalink
Merge pull request #42 from jayvdb/use-native-from-bytes
Browse files Browse the repository at this point in the history
Use u32 and u16 from_xx_bytes
  • Loading branch information
Roughsketch authored Nov 29, 2024
2 parents 51383ac + 96f9f61 commit c2933f5
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ pub fn read_u32<R: BufRead + Seek>(reader: &mut R, endianness: &Endian) -> Image
reader.read_exact(&mut buf)?;

match endianness {
Endian::Little => Ok(((buf[3] as u32) << 24)
| ((buf[2] as u32) << 16)
| ((buf[1] as u32) << 8)
| (buf[0] as u32)),
Endian::Big => Ok(((buf[0] as u32) << 24)
| ((buf[1] as u32) << 16)
| ((buf[2] as u32) << 8)
| (buf[3] as u32)),
Endian::Little => Ok(u32::from_le_bytes(buf)),
Endian::Big => Ok(u32::from_be_bytes(buf)),
}
}

Expand All @@ -47,8 +41,8 @@ pub fn read_u16<R: BufRead + Seek>(reader: &mut R, endianness: &Endian) -> Image
reader.read_exact(&mut buf)?;

match endianness {
Endian::Little => Ok(((buf[1] as u16) << 8) | (buf[0] as u16)),
Endian::Big => Ok(((buf[0] as u16) << 8) | (buf[1] as u16)),
Endian::Little => Ok(u16::from_le_bytes(buf)),
Endian::Big => Ok(u16::from_be_bytes(buf)),
}
}

Expand Down

0 comments on commit c2933f5

Please sign in to comment.