#load-image

load_image

Load PNG or JPEG with color profile support

5 stable releases

2.15.1 May 31, 2021
2.14.1 May 31, 2021
2.12.1 Jul 6, 2020
2.12.0 Mar 3, 2019
Download history 220/week @ 2021-04-07 217/week @ 2021-04-14 619/week @ 2021-04-21 197/week @ 2021-04-28 263/week @ 2021-05-05 288/week @ 2021-05-12 223/week @ 2021-05-19 259/week @ 2021-05-26 375/week @ 2021-06-02 436/week @ 2021-06-09 297/week @ 2021-06-16 347/week @ 2021-06-23 410/week @ 2021-06-30 356/week @ 2021-07-07 519/week @ 2021-07-14 500/week @ 2021-07-21

938 downloads per month
Used in 2 crates (via dssim)

GPL-3.0+

420KB
952 lines

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

~8–12MB
~235K SLoC