Function imagesize::reader_size

source ·
pub fn reader_size<R: BufRead + Seek>(reader: R) -> ImageResult<ImageSize>
Expand description

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

The minimum data required is 12 bytes. Anything shorter will return ImageError::IoError.

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)
}