Crates.io | load_image |
lib.rs | load_image |
version | 3.2.1 |
source | src |
created_at | 2019-03-03 19:02:37.913983 |
updated_at | 2024-09-19 23:30:04.545193 |
description | Load PNG or JPEG with color profile support |
homepage | https://imageoptim.com/api |
repository | https://gitlab.com/kornelski/load_image |
max_upload_size | |
id | 118487 |
size | 669,146 |
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.