imagesize
Reads image dimensions from 25+ formats by parsing just the header bytes, no full decode required.
Repository Health
Technical Analysis
imagesize is a Rust crate that determines the width and height of an image without decoding the full file. It parses only the minimal header bytes needed for each format — often 16 bytes or less — which makes it useful for tools that need to validate or catalog images quickly without pulling in a full image-decoding dependency.
The crate supports over 25 simple image formats including PNG, JPEG, GIF, WebP, BMP, TIFF, HEIC/HEIF, and JPEG XL, plus texture container formats such as DDS, KTX2, PowerVR, and ASTC with compression-family detection (BC1-7, ETC1/ETC2/EAC, PVRTC, ASTC, ATC). Each format is gated behind its own Cargo feature flag, so consumers can compile in only the decoders they actually need.
What You Get
- Header-only parsing that reads a minimal byte range (often under 16 bytes) instead of decoding the full image
- Detection for 25+ simple formats: PNG, JPEG, GIF, WebP, BMP, TIFF, PSD/PSB, EXR, HDR, QOI, Farbfeld, Aseprite, ICO, ILBM, JPEG XL, HEIC/HEIF, and more
- Texture container support with compression detection for DDS (BC1-7), KTX2, PowerVR (PVRTC/ETC2/EAC), PKM (ETC1/ETC2/EAC), and ATC
- Per-format Cargo feature flags so unused decoders can be excluded from the compiled binary
- Cross-container helper methods (compression_family(), is_block_compressed(), container_format()) that normalize compression info across different texture containers
- An extensive test and fuzzing regression suite covering per-format sample images and previously discovered fuzz crashes
Common Use Cases
- Image gallery or asset-management tools that index thousands of files with their dimensions without loading each one fully into memory
- Web upload validation that checks image dimensions before accepting a file, without a heavyweight image-processing dependency
- Game asset pipelines that inspect texture files (DDS, KTX2, PowerVR) to report compression format and size for build audits
- Media/CDN services that need aspect ratio or dimension metadata to generate responsive image variants without decoding pixel data
Under The Hood
Architecture
The crate’s public surface is small and centralized in src/lib.rs, which exposes size(), blob_size(), and reader_size() plus the ImageType and ImageSize types; all three entry points funnel into reader_size(), which calls formats::image_type() to sniff a 12-byte header and then dispatches to the matched format’s size() function via ImageType::reader_size(). Detection logic lives in src/formats/mod.rs as an ordered chain of matches() checks (JPEG and PNG first as the most common cases, TGA deliberately last because its signature is the weakest), and each format gets its own module under src/formats/ (png.rs, jpeg.rs, gif.rs, etc.) implementing just a matches() and size() pair. Texture container formats that need deeper inspection beyond a simple header match — DDS, PowerVR, PKM, ATC, and HEIF — are split into src/container/ submodules, each exposing a detect_compression() alongside matches()/size(), keeping compression-family logic isolated from the simple-format dispatch table. Shared byte-reading primitives (big/little-endian integer reads, delimited/whitespace-terminated string reads) are centralized in src/util.rs so every format module reads through the same primitives rather than duplicating parsing code. What breaks if a core abstraction changes: reordering the matches() chain in formats::image_type() risks false-positive detection for formats with weak signatures (this is why TGA is checked last), and any change to the ImageResult/ImageError error type ripples through every format module since they all propagate it via ?.