Crates.io | pb-imgsize |
lib.rs | pb-imgsize |
version | 0.2.5 |
source | src |
created_at | 2023-06-20 02:15:05.095125 |
updated_at | 2023-06-20 14:39:01.048205 |
description | A Rust library to detect the dimensions of PNG and JPEG images. |
homepage | |
repository | https://github.com/pbevin/imgsize |
max_upload_size | |
id | 894510 |
size | 235,017 |
pb-imgsize
Fast JPEG and PNG image metadata reader in Rust.
This Rust library provides an efficient way to extract image dimensions (width and height) and comments embedded in JPEG and PNG image files without needing to decode the entire image. The primary focus of this library is to perform these operations as quickly as possible.
Add pb-imgsize
to your Cargo.toml
file:
[dependencies]
pb-imgsize = "0.1.0"
Include the library in your Rust file:
use pb_imgsize as imgsize;
To read metadata from an image file, use the read_file
function:
let metadata = imgsize::read_file("path/to/image.jpg").unwrap();
To read metadata from a byte slice, use the read_bytes
function:
let data = include_bytes!("path/to/image.jpg");
let metadata = imgsize::read_bytes(data).unwrap();
Both functions return an ImageMetadata
struct containing the width
, height
and comments
fields.
pub struct ImageMetadata {
pub width: u32,
pub height: u32,
pub comments: Vec<Vec<u8>>,
}
Here's an example that demonstrates how to use pb-imgsize
to read metadata from a JPEG file:
use pb_imgsize as imgsize;
fn main() -> Result<(), imgsize::Error> {
let metadata = imgsize::read_file("path/to/image.jpg")?;
println!("Width: {}", metadata.width);
println!("Height: {}", metadata.height);
for comment in metadata.comments {
println!("Comment: {}", String::from_utf8_lossy(&comment));
}
Ok(())
}
The library defines an Error
enum that encapsulates the various errors that can occur when trying to read image data. There are specific error types for I/O errors and decoding errors.
To run the tests:
cargo test
MIT
Contributions are always welcome! Please adhere to the project's Code of Conduct during your participation in this project.
Please feel free to open an issue or a pull request if you have any issues or feature requests.
For more details, check the Rust documentation for the pb-imgsize
library.