16 releases

Uses old Rust 2015

0.5.0 Aug 16, 2019
0.4.0 Apr 17, 2018
0.3.0 Aug 6, 2017
0.2.0 Dec 1, 2016
0.0.2 Nov 20, 2014

#285 in Images

Download history 852/week @ 2023-12-16 569/week @ 2023-12-23 355/week @ 2023-12-30 669/week @ 2024-01-06 1033/week @ 2024-01-13 952/week @ 2024-01-20 650/week @ 2024-01-27 508/week @ 2024-02-03 1134/week @ 2024-02-10 1555/week @ 2024-02-17 1609/week @ 2024-02-24 1298/week @ 2024-03-02 1051/week @ 2024-03-09 1295/week @ 2024-03-16 1139/week @ 2024-03-23 1075/week @ 2024-03-30

4,753 downloads per month
Used in 11 crates

MIT license

46KB
1.5K SLoC

rust-bmp

Build Status

Full documentation

Small module for reading and writing bitmap images. See the documentation for the current status of BMP encoding and decoding support.

Usage

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 = "*"

Initializing

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);

Editing

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");

Opening

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);
});

Coordinate convention

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");
}

Dependencies

~120KB