#image #bitmap #reading #decoding #codec #encoding

bmp

用于在Rust中读取和写入BMP图像的小型库

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 图像

Download history 1193/week @ 2024-03-14 1315/week @ 2024-03-21 1105/week @ 2024-03-28 918/week @ 2024-04-04 819/week @ 2024-04-11 1028/week @ 2024-04-18 1014/week @ 2024-04-25 920/week @ 2024-05-02 1022/week @ 2024-05-09 1082/week @ 2024-05-16 1205/week @ 2024-05-23 945/week @ 2024-05-30 807/week @ 2024-06-06 1294/week @ 2024-06-13 1175/week @ 2024-06-20 744/week @ 2024-06-27

4,226 每月下载量
用于 11 crate

MIT 许可证

46KB
1.5K SLoC

rust-bmp

Build Status

完整文档

用于读取和写入位图图像的小型模块。请参阅文档,了解BMP编码和解码支持的当前状态。

使用方法

库的更新版本应在crates.io上提供。将以下内容添加到您的 Cargo.toml 中以将其作为依赖项。

[dependencies]
bmp = "*"

初始化

使用 new 函数初始化一个新的图像,通过指定 widthheight

extern crate bmp;
use bmp::Image;

let mut img = Image::new(100, 100);

编辑

使用 get_pixelset_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