Crates.io | simple-bmp |
lib.rs | simple-bmp |
version | 0.2.1 |
source | src |
created_at | 2024-03-16 07:07:54.60825 |
updated_at | 2024-04-05 06:06:13.207393 |
description | A simple library for writing RGB pixels as a valid BMP file. |
homepage | |
repository | https://github.com/Jayshua/simple-bmp |
max_upload_size | |
id | 1175462 |
size | 8,823 |
A simple library for writing RGB pixels as a valid BMP file.
Sometimes, mostly when debugging, all you want to do is write some data as an image so you can look at it. Every crate I could find was Way Too Complicated (TM). What I wanted was a function to write some pixels into an image file that can be opened by an image viewer. What I got were impossible-to-read types generic over the bit-depth, numeric type used to store the bits, color channel order, and the image format; crates requiring 15 function calls before writing any data; APIs forcing you to use stupid set_pixel functions instead of taking a simple slice of pixel data; and the kitchen sink.
This crate is the one function I wanted. Also it's no_std and no_alloc because requiring the standard library when the core purpose of the crate doesn't require it is annoying.
const WIDTH: usize = 500;
const HEIGHT: usize = 500;
let mut pixels = [0u8; WIDTH * HEIGHT * 3];
// Draw a simple gradient
for y in 0..HEIGHT {
for x in 0..WIDTH {
pixels[y * WIDTH * 3 + x * 3 + 0] = 240;
pixels[y * WIDTH * 3 + x * 3 + 1] = 100;
pixels[y * WIDTH * 3 + x * 3 + 2] = y as u8;
}
}
// Buffer for the BMP data
let mut image = [0u8; simple_bmp::buffer_length(500, 500)];
// Write the pixels into the BMP buffer
simple_bmp::write_bmp(&mut image, WIDTH, HEIGHT, &pixels).unwrap();
// Maybe you want to store the BMP on disk
// std::fs::write("./image.bmp", &image).unwrap();