12 stable releases

3.2.1 Sep 19, 2024
3.1.5 Jul 7, 2024
3.1.4 Oct 21, 2023
3.0.3 Feb 9, 2023
2.12.0 Mar 3, 2019

#441 in Images

Download history 489/week @ 2024-07-17 786/week @ 2024-07-24 632/week @ 2024-07-31 482/week @ 2024-08-07 298/week @ 2024-08-14 440/week @ 2024-08-21 436/week @ 2024-08-28 284/week @ 2024-09-04 394/week @ 2024-09-11 530/week @ 2024-09-18 416/week @ 2024-09-25 554/week @ 2024-10-02 452/week @ 2024-10-09 359/week @ 2024-10-16 352/week @ 2024-10-23 402/week @ 2024-10-30

1,607 downloads per month
Used in 14 crates (6 directly)

GPL-3.0+

420KB
1K SLoC

Load image as sRGB

Glue code for a few libraries that correctly loads a JPEG, PNG, or (optionally) WebP or AVIF image into memory, taking into accout color profile metadata in PNG chunks, EXIF data and app markers. Converts CMYK to RGB if needed.

cargo add load_image
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let path = std::env::args().nth(1).ok_or("Please provide image file path")?;
    let img = load_image::load_image(path)?;
}

The returned Image is:

struct Image {
    pub width: usize,
    pub height: usize,
    pub bitmap: enum ImageData {
        RGB8(Vec<RGB8>),
        RGBA8(Vec<RGBA8>),
        RGB16(Vec<RGB16>),
        RGBA16(Vec<RGBA16>),
        GRAY8(Vec<GRAY8>),
        GRAY16(Vec<GRAY16>),
        GRAYA8(Vec<GRAYA8>),
        GRAYA16(Vec<GRAYA16>),
    }
}

The bitmap is packed, so x + y * width gives the pixel at x,y (use imgref for convenient manipulation).

The load_image function doesn't panic, but if you enable the mozjpeg feature, it will depend on unwinding internally, and won't be compatible with crates compiled with panic = "abort" option.

Dependencies

~2.5–7.5MB
~152K SLoC