display | more...

The Portable Graymap image format is the lowest common denominator in the grayscale world. It's not very flexible, and it is very inefficient with regard to its use of hard drive space, but it is so easy to write an image file that uses it that all of these negatives pale in comparison. I used it recently to do a quick and dirty fractal outputter, and it made everything really easy, and allowed me concentrate on the important parts rather than getting bogged down in crappy details. I highly recommend this format (and its cousins PPM and PBM) if you are interested in making a program that outputs pictures, but don't want to have to spend a lot of time at it.

Here is a quick spec for it (all in ASCII, by the way):

  • The first two chracters in the file must be the magic number "P2"
  • then whitespace (CR, space, TAB, and/or LF)
  • a decimal integers representing the width of the image
  • whitespace
  • another decimal number for height
  • whitespace
  • an integer specifying the maximum grayscale value allowed
  • whitespace
  • Now, just put a bunch of whitespace sparated data in the file.
Any characters after a #, and before a newline are treated as comments, and ignored. To be truly well behaved, lines should not be more than 70 characters, but most libraries out there don't enforce this behavior.

Anyone implementing a reader for this format is advised to be liberal in what they accept. Since it's used for quick and dirty hacks, if you try to be too picky, you'll just piss off a bunch of nerds - and you don't want that.

Here is a sample PGM file, in its entirety:

P2
# feep.pgm
24 7
15
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 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11  0  0  0 0 0 15  0  0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11  0 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11  0  0  0 0 0 15  0  0  0 0
0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15  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  0  0 0
Note the comment - it's ignored. Also, look at how readable it is, you can cut and paste the above text into a file called feep.pgm, and any PGM-capable image viewer should read it just fine, but you can probably guess how it looks, simply based on eyeballing the data.


Much of this was taken from the SunOS spec for the PGM format, a copy of which I found at http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/PGM.txt

Log in or register to write something here or to contact authors.