PGM Files
PGM is the portable gray map format. It is a simple grayscale
image description. The definition is as follows:
-
A "magic number" for identifying the file type. A PGM file's magic
number is the two characters "P2".
-
Whitespace (blanks, TABs, CRs, LFs).
-
A width, formatted as ASCII characters in decimal.
-
Whitespace.
-
A height, again in ASCII decimal.
-
Whitespace.
-
The maximum gray value, again in ASCII decimal.
-
Whitespace.
-
Width * height gray values, each in ASCII decimal, between 0 and the
specified maximum value, separated by whitespace, starting at the top-
left corner of the graymap, proceeding in normal English reading order.
A value of 0 means black, and the maximum value means white.
Characters from a "#" to the next end-of-line are ignored (comments).
No line should be longer than 70 characters.
There is also a variant on the format, available by setting the RAWBITS
option at compile time. This variant is different in the following ways:
-
The "magic number" is "P5" instead of "P2".
-
The gray values are stored as plain bytes, instead of ASCII decimal.
-
No whitespace is allowed in the grays section, and only a single
character of whitespace (typically a newline) is allowed after the
MAXVAL.
The files are smaller and many times faster to read and write.
Note that this raw format can only be used for MAXVAL less than or equal
to 255. If you use the PGM library and try to write a file with a larger
MAXVAL, it will automatically fall back on the slower but more general
plain format.
EXAMPLE:
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
PGM files are:
-
ASCII or BINARY
-
Gray
-
2D
-
No compression
-
1 image
-
Reference:
-
Programs to view a PGM file include:
-
The AVS module read_any_image can read a PGM file;
-
GIMP
-
The
ImageVision
program imgview can display a PGM file.
-
XV
Routines to read or write a PGM file:
Programs to convert a PGM file to another format:
-
GIMP can read a PGM file
and write out a copy in a variety of formats.
-
The PBMPLUS program PGMTOFITS converts a PGM file to FITS format.
-
XV can read a PGM file
and write out a copy in BMP, FIT, GIF, JPG, PBM/PPM, RGB or TIF
format.
Programs to convert a file to PGM format:
-
GIMP can read a file in a
variety of formats, and write it out as a PGM file.
-
The PBMPLUS program FITSTOPGM converts a FITS file to PGM format.
-
XV can read a file in a
BMP, FIT, GIF, JPG, PBM/PPM, RGB or TIF format, and write it out
as a PGM file.
Files you may copy:
Back to the data home page.
Last revised on 10 May 2002.