16个版本
使用旧的Rust 2015
| 0.5.0 | 2019年8月16日 |
|---|---|
| 0.4.0 | 2018年4月17日 |
| 0.3.0 | 2017年8月6日 |
| 0.2.0 | 2016年12月1日 |
| 0.0.2 | 2014年11月20日 |
#309 in 图像
4,226 每月下载量
用于 11 crate
46KB
1.5K SLoC
rust-bmp
用于读取和写入位图图像的小型模块。请参阅文档,了解BMP编码和解码支持的当前状态。
使用方法
库的更新版本应在crates.io上提供。将以下内容添加到您的 Cargo.toml 中以将其作为依赖项。
[dependencies]
bmp = "*"
初始化
使用 new 函数初始化一个新的图像,通过指定 width 和 height。
extern crate bmp;
use bmp::Image;
let mut img = Image::new(100, 100);
编辑
使用 get_pixel 和 set_pixel 函数编辑图像数据。使用 save 函数保存图像,通过指定 path。该函数返回一个 IoResult,指示保存是否成功。
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 函数通过指定 path 打开现有的图像。该函数返回一个 BmpResult,其中包含一个 Image 或一个 BmpError。
extern crate bmp;
let img = bmp::open("path/to/img.bmp").unwrap_or_else(|e| {
panic!("Failed to open: {}", e);
});
坐标约定
BMP图像按行优先顺序访问,其中点 (0, 0) 被定义为图像的左上角。示例
#[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");
}
依赖项
~120KB