image icon indicating copy to clipboard operation
image copied to clipboard

PBM encoder is incorrect for ASCII, aka PnmSubtype::Bitmap(SampleEncoding::Ascii)

Open timboldt opened this issue 9 months ago • 1 comments

Encoding a PNM bitmap as binary (type P4) works correctly, but encoding it as ASCII (type P1) results in the wrong sample format.

Specification: https://netpbm.sourceforge.net/doc/pbm.html

Expected

I expected a file with samples of 1s and 0s.

P1
24 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0
etc.

NOTE: The spaces are optional.

Actual behaviour

I got samples with values between 0 and 255, i.e. the bits were combined into bytes and then written out as byte values in decimal.

P1
24 7
0 0 0
121  231 158
etc.

Reproduction steps

let f = File::create("pic.pbm")?;
let mut writer = BufWriter::new(f);
let encoder =
    PnmEncoder::new(&mut writer).with_subtype(PnmSubtype::Bitmap(SampleEncoding::Ascii));
imgbuf.write_with_encoder(encoder)?;

P.S. If you can point me in the right direction, I'm happy to send a PR to address this.

timboldt avatar May 23 '24 04:05 timboldt