Project Nayuki

Portable FloatMap format I/O (Java)

Cathedral photo in HDR

Here I provide a lightweight Java library for reading and writing Portable FloatMap (PFM) image files. Also included is a demo program that makes use of this library in order to convert PFM to PNG.

The usage of the code should be straightforward. The only notable caveat is that the pixel data is stored in bottom-to-top order (like BMP, unlike PNG, GIF, JPEG, etc.)

The PFM format allows one to process images at 32-bit floating-point precision per channel. The enormous range of values that can be represented is useful for high dynamic range (HDR) applications. The large precision is at least as good 24 integer bits per channel. Furthermore, the precision completely blows away any banding/rounding/dithering issues experienced by processing traditional 8-bits-per-channel images, and it provides much more safety margin than 10/16/etc.-bits-per-channel images.

The code is open-source under the MIT License.

Source code


Command line: java PfmToPng memorial.pfm memorial.png
Try customizing the mode in PfmToPng.floatToByte() to get different brightness/exposure mappings.

The “memorial” image is courtesy of Paul Debevec.

API summary

public class PortableFloatMap {
    // All these fields are used when reading or writing a file
    public int width;
    public int height;
    public Mode mode;  // COLOR or GRAYSCALE
    public float[] pixels;  // Bottom-to-top order
    public boolean bigEndian;
    // Reads from file and constructs a new PFM object
    public PortableFloatMap(File file) { ... }
    // Writes the state of this PFM object to file
    public void write(File file) { ... }