Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reader_size #18

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add reader_size
  • Loading branch information
kailes committed Jul 13, 2022

Verified

This commit was signed with the committer’s verified signature.
commit fcc1bb57d3fac2a6bc991b034b47e5f8cc847dde
49 changes: 42 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -150,12 +150,8 @@ where
P: AsRef<Path>,
{
let file = File::open(path)?;
let mut reader = BufReader::new(file);

let mut header = [0; 12];
reader.read_exact(&mut header)?;

dispatch_header(&mut reader, &header)
let reader = BufReader::new(file);
reader_size(reader)
}

/// Get the image size from a block of raw data.
@@ -186,8 +182,47 @@ where
///
/// [`ImageError`]: enum.ImageError.html
pub fn blob_size(data: &[u8]) -> ImageResult<ImageSize> {
let mut reader = Cursor::new(data);
let reader = Cursor::new(data);
reader_size(reader)
}

/// Get the image size from a reader
///
/// # Arguments
/// * `reader` - A reader for the data
///
/// # Error
///
/// This method will return an [`ImageError`] under the following conditions:
///
/// * The header isn't recognized as a supported image format
/// * The data isn't long enough to find the size for the given format
///
/// # Examples
///
/// ```
/// use std::io::Cursor;
/// use imagesize::reader_size;
///
/// // PNG Header with size 123x321
/// let reader = Cursor::new([
/// 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
/// 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
/// 0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x01, 0x41,
/// 0x08, 0x06, 0x00, 0x00, 0x00, 0x9A, 0x38, 0xC4
/// ]);
///
/// match reader_size(reader) {
/// Ok(dim) => {
/// assert_eq!(dim.width, 123);
/// assert_eq!(dim.height, 321);
/// }
/// Err(why) => println!("Error getting reader size: {:?}", why)
/// }
/// ```
///
/// [`ImageError`]: enum.ImageError.html
pub fn reader_size<R: BufRead + Seek>(mut reader: R) -> ImageResult<ImageSize> {
let mut header = [0; 12];
reader.read_exact(&mut header)?;

27 changes: 27 additions & 0 deletions tests/reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[cfg(test)]
use imagesize::reader_size;
use std::io::Cursor;

#[test]
fn reader_test() {
// PNG Header with size 123x321
let reader = Cursor::new([
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x01, 0x41,
0x08, 0x06, 0x00, 0x00, 0x00, 0x9A, 0x38, 0xC4
]);
let dim = reader_size(reader).unwrap();
assert_eq!(dim.width, 123);
assert_eq!(dim.height, 321);
}

#[test]
fn reader_test_fail() {
// only header part of webp
let webp_reader = Cursor::new([
0x52, 0x49, 0x46, 0x46, 0xD8, 0xA1, 0x00, 0x00,
0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x58
]);
assert!(reader_size(webp_reader).is_err());
}