Crates.io | bmp |
lib.rs | bmp |
version | 0.5.0 |
source | src |
created_at | 2014-11-20 22:27:26.7224 |
updated_at | 2019-08-16 09:25:01.80287 |
description | Small library for reading and writing BMP images in Rust. |
homepage | https://github.com/sondrele/rust-bmp |
repository | https://github.com/sondrele/rust-bmp.git |
max_upload_size | |
id | 172 |
size | 48,319 |
Small module for reading and writing bitmap images. See the documentation for the current status of BMP encoding and decoding support.
An updated version of the library should be available on crates.io.
Add the following to your Cargo.toml
to get is a dependency.
[dependencies]
bmp = "*"
Initialize a new image with the new
function, by specifying width
and height
.
extern crate bmp;
use bmp::Image;
let mut img = Image::new(100, 100);
Edit image data using the get_pixel
and set_pixel
functions.
Save an image with the save
function, by specifying the path
. The function returns
an IoResult
which indicates whether the save was successful or not.
let pixel = img.get_pixel(0, 0);
img.set_pixel(50, 50, Pixel::new(255, 255, 255));
let _ = img.save("path/to/img.bmp");
Open an existing image with the open
function, by specifying the path
. The function
returns a BmpResult
, that contains either a Image
or a BmpError
.
extern crate bmp;
let img = bmp::open("path/to/img.bmp").unwrap_or_else(|e| {
panic!("Failed to open: {}", e);
});
The BMP images are accessed in row-major order, where point (0, 0) is defined to be in the upper left corner of the image. Example:
#[macro_use]
extern crate bmp;
use bmp::{Image, Pixel};
fn main() {
let mut img = Image::new(256, 256);
for (x, y) in img.coordinates() {
img.set_pixel(x, y, px!(x, y, 200));
}
let _ = img.save("img.bmp");
}