Hello everyone,
I'm still learning Rust, so most probably I've just made an easy mistake. However, since I was so far unable to solve my issue on my own, I hope to get help here.
My ultimate goal is to create and modify black-and-white (i.e. 1-bit in memory corresponds to 1 black dot on the display) bitmap graphics for an LCD display. When I came across 0.19.0 of the img
crate and read that the PNM module now supports also the PBM format (Netpbm's Portable BitMap, see the Netpbm article on Wikipedia for info) I wanted to give it a try.
So I generated a 10x10 pixel file (attached as a PNG, since PBM's are not allowed here) to get a feel for the crate. I wanted to start with a simple roundtrip, i.e. load the image and write it back to a new pbm file using the crates methods. Yet, I didn't succeed.
Using
extern crate image;
use std::fs::File;
use image::{
ImageOutputFormat,
pnm::{
PNMSubtype,
SampleEncoding
},
};
fn main() {
let img = image::open("test.pbm").unwrap();
let color = img.color();
let enc = img.write_to(&mut File::create("out.pbm").unwrap(),
ImageOutputFormat::PNM(PNMSubtype::Bitmap(SampleEncoding::Binary)));
println!("{:?}", img.raw_pixels().len());
println!("{:?}", color);
println!("{:?}", enc);
}
I get
668
Gray(8)
Err(IoError(Custom {kind: InvalidInput, error: StringError("Image buffer does not correspond to size and colour")}))
printed out.
I don't really understand why reading in a 10x10 bitmap results in a 668 bytes long Gray(8) array (I didn't post the full debug output of img.raw_pixels()
, but if you try for yourself, you'll see that the 0 and 255 values in that struct don't really correspond to the pixel pattern in the pbm file).
I assume that I have an error somewhere (i.e. didn't use the right methods in the beginning), but perhaps this might also be a bug in img::pnm
?
Cheers!