Skip to main content

probe_geometry

Function probe_geometry 

Source
pub fn probe_geometry(path: &Path) -> Result<ImageGeometry, ValidationError>
Expand description

Reads the geometry of a container without decoding a single pixel.

Opens the file, reads its first [PNG_HEADER_LEN] bytes and applies the format and size gates to them. Nothing else is read, so the cost does not depend on how large the file is.

§What this is for

Deciding whether a file is worth decoding. load_and_validate reads the whole file into memory before it can answer the same question, which is wasted work for every image that was never the right size — and on a folder of photographs most of them are not. It is also the only way to refuse an image that is too large to analyse before allocating anything for it.

A geometry this accepts is not a usable container. It has passed two gates of five; the pixels still have to be decoded, screened for a JPEG grid, hashed and measured for texture.

§Errors

Returns ValidationError::IoError if the file cannot be read, ValidationError::JpegDetected, ValidationError::WebpDetected or ValidationError::NotPng if it is not a PNG, and ValidationError::ImageTooSmall or ValidationError::ImageTooLarge if its geometry is outside the accepted range.