From e52f1ec02af159fc16541ed8e7e134de805d9758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Wed, 14 Nov 2018 21:55:37 +0000 Subject: [PATCH] Use integer math when rounding --- src/interlace.rs | 5 ++--- src/png/mod.rs | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/interlace.rs b/src/interlace.rs index cdf20a2d..e5a59129 100644 --- a/src/interlace.rs +++ b/src/interlace.rs @@ -93,9 +93,8 @@ pub fn deinterlace_image(png: &mut PngData) { let mut current_y: usize = pass_constants.y_shift as usize; for line in png.scan_lines() { let bit_vec = BitVec::from_bytes(&line.data); - let bits_in_line = ((png.ihdr_data.width - u32::from(pass_constants.x_shift)) as f32 - / f32::from(pass_constants.x_step)).ceil() as usize - * bits_per_pixel as usize; + let bits_in_line = ((png.ihdr_data.width - u32::from(pass_constants.x_shift) + u32::from(pass_constants.x_step) - 1) + / u32::from(pass_constants.x_step)) as usize * bits_per_pixel as usize; for (i, bit) in bit_vec.iter().enumerate() { // Avoid moving padded 0's into new image if i >= bits_in_line { diff --git a/src/png/mod.rs b/src/png/mod.rs index 9dbf8b4d..c3dc6fde 100644 --- a/src/png/mod.rs +++ b/src/png/mod.rs @@ -226,8 +226,7 @@ impl PngData { /// Reverse all filters applied on the image, returning an unfiltered IDAT bytestream pub fn unfilter_image(&self) -> Vec { let mut unfiltered = Vec::with_capacity(self.raw_data.len()); - let bpp = ((f32::from(self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel())) / 8f32) - .ceil() as usize; + let bpp = ((self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel() + 7) / 8) as usize; let mut last_line: Vec = Vec::new(); let mut last_pass = 1; for line in self.scan_lines() { @@ -254,8 +253,7 @@ impl PngData { /// 5: All (heuristically pick the best filter for each line) pub fn filter_image(&self, filter: u8) -> Vec { let mut filtered = Vec::with_capacity(self.raw_data.len()); - let bpp = ((f32::from(self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel())) / 8f32) - .ceil() as usize; + let bpp = ((self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel() + 7) / 8) as usize; let mut last_line: Vec = Vec::new(); let mut last_pass: Option = None; for line in self.scan_lines() {