Skip to content

Commit 36a16db

Browse files
authored
Merge pull request #285 from image-rs/read-into-buffer
Read into byte buffer
2 parents b589d10 + d70c31d commit 36a16db

File tree

5 files changed

+386
-39
lines changed

5 files changed

+386
-39
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ zune-jpeg = { version = "0.4.17", optional = true }
2727

2828
[dev-dependencies]
2929
criterion = "0.3.1"
30+
# We may already depend on this via flate2
31+
crc32fast = "1"
3032

3133
[features]
3234
default = ["deflate", "jpeg", "lzw"]

src/decoder/image.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
ColorType, Directory, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError, UsageError,
1111
};
1212
use std::io::{self, Cursor, Read, Seek};
13+
use std::num::NonZeroUsize;
1314
use std::sync::Arc;
1415

1516
#[derive(Debug)]
@@ -363,6 +364,22 @@ impl Image {
363364
}
364365
}
365366

367+
pub(crate) fn minimum_row_stride(&self, dims: (u32, u32)) -> Option<NonZeroUsize> {
368+
let (width, height) = dims;
369+
370+
let row_stride = u64::from(width)
371+
.saturating_mul(self.samples_per_pixel() as u64)
372+
.saturating_mul(self.bits_per_sample as u64)
373+
.div_ceil(8);
374+
375+
// Note: row stride should be smaller than the len if we have an actual buffer. If there
376+
// are no pixels in the buffer (height _or_ width is 0) then the stride is not well defined
377+
// and we return `None`.
378+
(height > 0)
379+
.then_some(row_stride as usize)
380+
.and_then(NonZeroUsize::new)
381+
}
382+
366383
fn create_reader<'r, R: 'r + Read>(
367384
reader: R,
368385
compression_method: CompressionMethod,

0 commit comments

Comments
 (0)