On 20/05/2018 16:37, Dennis Lee Bieber wrote:
On Sun, 20 May 2018 12:38:59 +0100, bartc <b...@freeuk.com> declaimed the
Just for giggles, I decided to write the start of a PPM reader (it only
handles P6 binary, doesn't have the code for the other styles, and doesn't
incorporate PPM writer functions but...)
It successfully processed the PPM file my prior writing code
generated...
-=-=-=-=-=-
import struct
class PPM(object):
...
-=-=-=-=-=-
Type: b'P6' Width: 3 Height: 3 MaxVal: 255
[(0, 255, 0), (128, 128, 128), (255, 0, 0), (128, 128, 128), (255, 255,
255), (128, 128, 128), (0, 0, 255), (128, 128, 128), (0, 0, 0)]
Yes, that appears to work. (But I think it has a bug when there are two
successive #-comment lines.)
Meanwhile I've given up my campaign to have only line-oriented headers,
and spent the five minutes needed to allow for free-format headers, and
actually it's now simpler:
readln @f,sig
width := readnextint(f) # a 6-line function returning 0 on error
height := readnextint(f)
maxval := readnextint(f) # (for some file types)
However I don't think this works properly when a comment follows (on the
same line) the last format item, as a well-formed header will have an
extra white-space character to be skipped. I believe your program might
have the same problem; it will read the header OK, but not start reading
the data at the right place.
(A rather poor specification I think which could have been tightened up.)
--
bartc
--
https://mail.python.org/mailman/listinfo/python-list