imagesize

Quickly probe the size of various image formats without reading the entire file.

Latest version: 0.14.0 registry icon
Maintenance score
63
Safety score
100
Popularity score
73
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
0.14.0 0 0 0 0 0
0.13.0 0 0 0 0 0
0.12.0 0 0 0 0 0
0.11.0 0 0 0 0 0
0.10.1 0 0 0 0 0
0.10.0 0 0 0 0 0
0.9.0 0 0 0 0 0
0.8.8 0 0 0 0 0
0.8.7 0 0 0 0 0
0.8.6 0 0 0 0 0
0.8.5 0 0 0 0 0
0.8.4 0 0 0 0 0
0.8.3 0 0 0 0 0
0.8.2 0 0 0 0 0
0.8.1 0 0 0 0 0
0.8.0 0 0 0 0 0
0.7.1 0 0 0 0 0
0.7.0 0 0 0 0 0
0.6.0 0 0 0 0 0
0.5.2 0 0 0 0 0
0.5.1 0 0 0 0 0
0.5.0 0 0 0 0 0
0.4.0 0 0 0 0 0
0.3.5 0 0 0 0 0
0.3.4 0 0 0 0 0
0.3.3 0 0 0 0 0
0.3.2 0 0 0 0 0
0.3.1 0 0 0 0 0
0.3.0 0 0 0 0 0
0.2.0 0 0 0 0 0
0.1.2 0 0 0 0 0
0.1.1 0 0 0 0 0
0.1.0 0 0 0 0 0

Stability
Latest release:

0.14.0 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



crates.io version docs-badge

imagesize

Quickly probe the size of various image formats without reading the entire file.

The goal of this crate is to be able to read the dimensions of a supported image without loading unnecessary data, and without pulling in more dependencies. Most reads only require 16 bytes or less, and more complex formats take advantage of skipping junk data.

Features

  • Fast: Reads only the necessary bytes to determine image dimensions
  • Lightweight: Minimal dependencies
  • Texture Format Support: Detects compression algorithms in DDS, PowerVR, PKM, and other texture containers
  • Cross-Container Queries: Helper methods to identify compression families across different container formats
  • Backward Compatible: All existing APIs remain unchanged

Usage

Add the following to your Cargo.toml:

[dependencies]
imagesize = "0.14"

Supported Image Formats

Simple Image Formats

  • Aseprite
  • Avif
  • BMP
  • EXR
  • Farbfeld
  • GIF
  • HDR
  • HEIC / HEIF
  • ICO*
  • ILBM (IFF)
  • JPEG
  • JPEG XL
  • PNG
  • PNM (PBM, PGM, PPM)
  • PSD / PSB
  • QOI
  • TGA
  • TIFF
  • VTF
  • WEBP

Texture Container Formats with Compression Detection

  • DDS - DirectDraw Surface with BC1-7 (DXT1-5) compression detection
  • PKM - ETC1/ETC2/EAC compressed textures
  • PowerVR - PVRTC, ETC2, and EAC compressed textures
  • ATC - Adaptive Texture Compression (Qualcomm Adreno)
  • ASTC - Adaptive Scalable Texture Compression
  • KTX2 - Khronos Texture Container

If you have a format you think should be added, feel free to create an issue.

*ICO files can contain multiple images, imagesize will give the dimensions of the largest one.

Examples

From a file

match imagesize::size("example.webp") {
    Ok(size) => println!("Image dimensions: {}x{}", size.width, size.height),
    Err(why) => println!("Error getting dimensions: {:?}", why)
}

From a vector

let data = vec![0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x64, 0x00, 0x64, 0x00];
match imagesize::blob_size(&data) {
    Ok(size) => println!("Image dimensions: {}x{}", size.width, size.height),
    Err(why) => println!("Error getting dimensions: {:?}", why),
}

Texture Format Detection

For texture container formats, you can detect both the container type and compression algorithm:

use imagesize::{image_type, ImageType, CompressionFamily};

let data = std::fs::read("texture.dds").unwrap();
match image_type(&data) {
    Ok(ImageType::Dds(compression)) => {
        println!("DDS texture with {:?} compression", compression);
    }
    Ok(ImageType::Pvrtc(compression)) => {
        println!("PowerVR texture with {:?} compression", compression);
    }
    Ok(other) => println!("Other format: {:?}", other),
    Err(e) => println!("Error: {:?}", e),
}

Cross-Container Compression Queries

Use helper methods to query compression information across different container formats:

use imagesize::{image_type, CompressionFamily};

let data = std::fs::read("texture.pvr").unwrap();
if let Ok(img_type) = image_type(&data) {
    // Group related compression algorithms regardless of container
    match img_type.compression_family() {
        Some(CompressionFamily::Etc) => println!("ETC family compression"),
        Some(CompressionFamily::BlockCompression) => println!("BC/DXT compression"),
        Some(CompressionFamily::Pvrtc) => println!("PVRTC compression"),
        _ => println!("Other or no compression"),
    }
    
    // Query container and compression properties
    if img_type.is_block_compressed() {
        println!("Uses block compression (BC1-7)");
    }
    
    if let Some(container) = img_type.container_format() {
        println!("Container format: {}", container);
    }
    
    if img_type.is_multi_compression_container() {
        println!("Container supports multiple compression types");
    }
}