A PFM file has only two parts: three lines of text, then the pixels. There is no compression, no comment syntax and no metadata. This page follows Paul Debevec’s description of the format, which is the usual reference.
Header
Each line ends with a single line feed (0A), not a carriage return and
line feed. The file has no comments, unlike the Netpbm formats that inspired it.
| Line | Content | Meaning |
|---|---|---|
| 1 | PF or Pf | The image type, shown below. |
| 2 | [width] [height] | The size in pixels, as positive decimal integers separated by a blank. |
| 3 | A signed, nonzero number, such as -1.0 | The sign gives the byte order of the floats; the size is a scale factor (discussed below). |
Type codes
| Line 1 | Bytes | Channels | Bytes per pixel |
|---|---|---|---|
PF | 50 46 | 3: red, green, blue | 12 |
Pf | 50 66 | 1: greyscale | 4 |
The third line
| Sign | Byte order of the floats |
|---|---|
Negative, such as -1.0 | Little-endian: the least significant byte of each float first |
Positive, such as 1.0 | Big-endian: the most significant byte of each float first |
Debevec’s page only uses the sign. Netpbm’s description says more: the number is nonzero and need not be an integer, and its absolute value is a scale factor that states the units of the sample values, so turning samples into physical quantities (watts per square metre, say) needs that unit information from elsewhere. Netpbm also says each header line is followed by one whitespace character, usually a line feed but not required to be, and that the width and height are positive decimal integers. Netpbm states that the format is not authoritatively documented and was deduced from an existing program, and that it appears to match what GIMP calls PFM. Because Debevec’s page uses line feeds and gives no comment syntax, a reader should take the byte order from the sign and treat the magnitude as the scale.
The Photoshop variant of PFM is identical except that its rows run from top to bottom, so images exchanged between the two appear upside down. Neither source says anything about NaN or infinity samples or about a maximum value.
Pixels
The pixels start at the byte after the line feed that ends the third line. Each sample is a 4-byte IEEE 754 single-precision float, with no padding between samples or rows.
- Rows run from the bottom of the image to the top, the opposite of most Netpbm formats.
- Within a row, pixels run from left to right.
- In a
PFfile each pixel is three floats in a row; red, then green, then blue is the universal reading, but neither Debevec’s page nor Netpbm’s states the channel order.
The size of the pixel data is width × height × channels × 4 bytes.
Sources
- Portable FloatMap (PFM) – Paul Debevec: header, byte order, row and pixel order
- PFM Format – Netpbm documentation: the scale factor, whitespace, the Photoshop variant
A complete file
This 2 by 1 pixel greyscale image holds the values 0.5 and 1.0 as little-endian floats. The three lines of text are marked, and the 8 bytes of pixels follow them.
00000000 50 66 0A 32 20 31 0A 2D 31 2E 30 0A 00 00 00 3F
00000010 00 00 80 3F
Pf, 2 1, -1.0, then the floats 0.5 and 1.0