Project Nayuki

Portable FloatMap format I/O (Java)

Cathedral photo in HDR

PFM files are suitable for representing high dynamic range (HDR) images. Here is a lightweight open-source library in Java for reading and writing such files. Also included is a demo program that utilizes this library to convert from 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 HDR applications. The large precision is at least as good 24 integer bits per channel. Furthermore, the precision thoroughly avoids any banding/rounding/dithering issues experienced by processing traditional 8-bits-per-channel images, and it provides considerably more safety margin than 10/12/16/etc. bits per channel.

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) { ... }

More info