#bmp #pixel #rgb #pixel-format #debugging #write #file

simple-bmp

A simple library for writing RGB pixels as a valid BMP file

3 unstable releases

0.2.1 Apr 5, 2024
0.2.0 Mar 16, 2024
0.1.0 Mar 16, 2024

#752 in Images

Download history 15/week @ 2024-07-22 9/week @ 2024-09-23 71/week @ 2024-09-30 4/week @ 2024-10-07

187 downloads per month

MIT license

9KB
119 lines

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.

Example

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

No runtime deps