Convert PGM to binary format in buffer