#load-image

load_image

Load PNG or JPEG with color profile support

7 stable releases

Uses new Rust 2021

3.0.1 Nov 14, 2022
3.0.0 Sep 22, 2022
2.16.4 Jul 18, 2022
2.16.2 Jan 26, 2022
2.12.0 Mar 3, 2019
Download history 698/week @ 2022-08-08 773/week @ 2022-08-15 794/week @ 2022-08-22 765/week @ 2022-08-29 486/week @ 2022-09-05 672/week @ 2022-09-12 637/week @ 2022-09-19 478/week @ 2022-09-26 569/week @ 2022-10-03 512/week @ 2022-10-10 464/week @ 2022-10-17 334/week @ 2022-10-24 461/week @ 2022-10-31 422/week @ 2022-11-07 683/week @ 2022-11-14 421/week @ 2022-11-21

2,007 downloads per month
Used in 3 crates (2 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) AVIF image into memory, taking into accout color profile metadata in PNG chunks, EXIF data and app markers. Converts CMYK to RGB if needed.

For Rust 1.51 or later.

[dependencies]
load_image = "2"
fn main() {
    let path = std::env::args().nth(1).expect("File name");
    // the flag is for removing alpha channel
    let img = load_image::load_image(path, false).unwrap();
}

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.

The load_image function doesn't panic, but it uses unwinding internally. Error handling won't work in crates compiled with the panic = "abort" option.

Dependencies

~3–8MB
~155K SLoC