Switch to div_ceil for rounding implementation

This commit is contained in:
A. Molzer committed 2025-07-14 19:25:12 +02:00
1 parent ca74c03520
commit 23672b2490
3 files changed
+11 -10

No files matched your search

+4 -4
View File
@@ -29,10 +29,10 @@ pub(crate) struct TileAttributes {
impl TileAttributes { impl TileAttributes {
pub fn tiles_across(&self) -> usize { pub fn tiles_across(&self) -> usize {
(self.image_width + self.tile_width - 1) / self.tile_width self.image_width.div_ceil(self.tile_width)
} }
pub fn tiles_down(&self) -> usize { pub fn tiles_down(&self) -> usize {
(self.image_height + self.tile_length - 1) / self.tile_length self.image_height.div_ceil(self.tile_length)
} }
fn padding_right(&self) -> usize { fn padding_right(&self) -> usize {
(self.tile_width - self.image_width % self.tile_width) % self.tile_width (self.tile_width - self.image_width % self.tile_width) % self.tile_width
@@ -625,12 +625,12 @@ impl Image {
let chunk_row_bits = (u64::from(chunk_dims.0) * u64::from(self.bits_per_sample)) let chunk_row_bits = (u64::from(chunk_dims.0) * u64::from(self.bits_per_sample))
.checked_mul(samples as u64) .checked_mul(samples as u64)
.ok_or(TiffError::LimitsExceeded)?; .ok_or(TiffError::LimitsExceeded)?;
let chunk_row_bytes: usize = ((chunk_row_bits + 7) / 8).try_into()?; let chunk_row_bytes: usize = chunk_row_bits.div_ceil(8).try_into()?;
let data_row_bits = (u64::from(data_dims.0) * u64::from(self.bits_per_sample)) let data_row_bits = (u64::from(data_dims.0) * u64::from(self.bits_per_sample))
.checked_mul(samples as u64) .checked_mul(samples as u64)
.ok_or(TiffError::LimitsExceeded)?; .ok_or(TiffError::LimitsExceeded)?;
let data_row_bytes: usize = ((data_row_bits + 7) / 8).try_into()?; let data_row_bytes: usize = data_row_bits.div_ceil(8).try_into()?;
// TODO: Should these return errors instead? // TODO: Should these return errors instead?
assert!(output_row_stride >= data_row_bytes); assert!(output_row_stride >= data_row_bytes);
+4 -3
View File
@@ -856,7 +856,8 @@ impl<R: Read + Seek> Decoder<R> {
let row_samples = if bits_per_sample >= 8 { let row_samples = if bits_per_sample >= 8 {
width width
} else { } else {
((((width as u64) * bits_per_sample as u64) + 7) / 8) ((width as u64) * bits_per_sample as u64)
.div_ceil(8)
.try_into() .try_into()
.map_err(|_| TiffError::LimitsExceeded)? .map_err(|_| TiffError::LimitsExceeded)?
}; };
@@ -955,12 +956,12 @@ impl<R: Read + Seek> Decoder<R> {
let output_row_bits = (width as u64 * self.image.bits_per_sample as u64) let output_row_bits = (width as u64 * self.image.bits_per_sample as u64)
.checked_mul(samples as u64) .checked_mul(samples as u64)
.ok_or(TiffError::LimitsExceeded)?; .ok_or(TiffError::LimitsExceeded)?;
let output_row_stride: usize = ((output_row_bits + 7) / 8).try_into()?; let output_row_stride: usize = output_row_bits.div_ceil(8).try_into()?;
let chunk_row_bits = (chunk_dimensions.0 as u64 * self.image.bits_per_sample as u64) let chunk_row_bits = (chunk_dimensions.0 as u64 * self.image.bits_per_sample as u64)
.checked_mul(samples as u64) .checked_mul(samples as u64)
.ok_or(TiffError::LimitsExceeded)?; .ok_or(TiffError::LimitsExceeded)?;
let chunk_row_bytes: usize = ((chunk_row_bits + 7) / 8).try_into()?; let chunk_row_bytes: usize = chunk_row_bits.div_ceil(8).try_into()?;
let chunks_across = ((width - 1) / chunk_dimensions.0 + 1) as usize; let chunks_across = ((width - 1) / chunk_dimensions.0 + 1) as usize;
+3 -3
View File
@@ -532,11 +532,11 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind> ImageEncoder<'a, W, T,
let rows_per_strip = { let rows_per_strip = {
match compression.tag() { match compression.tag() {
CompressionMethod::PackBits => 1, // Each row must be packed separately. Do not compress across row boundaries CompressionMethod::PackBits => 1, // Each row must be packed separately. Do not compress across row boundaries
_ => (1_000_000 + row_bytes - 1) / row_bytes, _ => 1_000_000_u64.div_ceil(row_bytes),
} }
}; };
let strip_count = (u64::from(height) + rows_per_strip - 1) / rows_per_strip; let strip_count = u64::from(height).div_ceil(rows_per_strip);
encoder.write_tag(Tag::ImageWidth, width)?; encoder.write_tag(Tag::ImageWidth, width)?;
encoder.write_tag(Tag::ImageLength, height)?; encoder.write_tag(Tag::ImageLength, height)?;
@@ -705,7 +705,7 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind> ImageEncoder<'a, W, T,
self.encoder.write_tag(Tag::RowsPerStrip, value)?; self.encoder.write_tag(Tag::RowsPerStrip, value)?;
let value: u64 = value as u64; let value: u64 = value as u64;
self.strip_count = (self.height as u64 + value - 1) / value; self.strip_count = (self.height as u64).div_ceil(value);
self.rows_per_strip = value; self.rows_per_strip = value;
Ok(()) Ok(())