Yalantis/uCrop · error · CImgIOException

load_pfm(): PFM header not found in file '%s'.

Error message

load_pfm(): PFM header not found in file '%s'.

What it means

load_pfm() parses Portable FloatMap headers, expecting the first non-comment line to match the magic 'P<character>' (Pf for grayscale, PF for RGB). If the leading line does not parse as a PFM magic token, the file is not recognized as PFM and CImgIOException is thrown.

Source

Thrown at ucrop/src/main/jni/CImg.h:57783

    static CImg<T> get_load_pfm(std::FILE *const file) {
      return CImg<T>().load_pfm(file);
    }

    CImg<T>& _load_pfm(std::FILE *const file, const char *const filename) {
      if (!file && !filename)
        throw CImgArgumentException(_cimg_instance
                                    "load_pfm(): Specified filename is (null).",
                                    cimg_instance);

      std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
      char pfm_type;
      CImg<charT> item(16384,1,1,1,0);
      int W = 0, H = 0, err = 0;
      double scale = 0;
      while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
      if (cimg_sscanf(item," P%c",&pfm_type)!=1) {
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimg_instance
                              "load_pfm(): PFM header not found in file '%s'.",
                              cimg_instance,
                              filename?filename:"(FILE*)");
      }
      while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
      if ((err=cimg_sscanf(item," %d %d",&W,&H))<2) {
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimg_instance
                              "load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.",
                              cimg_instance,
                              filename?filename:"(FILE*)");
      } else if (W<=0 || H<=0) {
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimg_instance
                              "load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in file '%s'.",
                              cimg_instance,W,H,
                              filename?filename:"(FILE*)");
      }

View on GitHub (pinned to f788b534b4)

Solutions

  1. Verify the file starts with 'PF' or 'Pf' (e.g. `head -c 2 file.pfm`).
  2. Use the correct loader for the actual format (load_pnm for P1-P6 files).
  3. Re-export the HDR image as PFM with a known tool (`convert hdr.exr out.pfm`).
  4. Strip BOM/leading whitespace before the magic token.
  5. Catch CImgIOException and report the file as non-PFM.

Example fix

// before
img.load_pfm(path); // actually a PGM
// after
std::ifstream f(path, std::ios::binary);
char m[2] = {0}; f.read(m, 2);
if (m[0]=='P' && (m[1]=='F' || m[1]=='f')) img.load_pfm(path);
else img.load_pnm(path);
Defensive patterns

Strategy: validation

Validate before calling

bool looksLikePfm(const char* path) {
  std::ifstream f(path, std::ios::binary);
  char m[2] = {0};
  f.read(m, 2);
  return f && m[0]=='P' && (m[1]=='F' || m[1]=='f');
}

Type guard

bool isPfmMagic(const unsigned char* b, size_t n) {
  return n >= 2 && b[0]=='P' && (b[1]=='F' || b[1]=='f');
}

Try / catch

try {
  img.load_pfm(path);
} catch (cimg_library::CImgIOException& e) {
  std::cerr << "Not a PFM file: " << e.what() << "\n";
}

Prevention

When it happens

Trigger: Calling load_pfm() on files whose first line is not 'PF' or 'Pf': a PNM/PGM file passed to the PFM loader, a corrupted or truncated PFM, a file with a BOM or leading junk, or an empty file.

Common situations: Mixing up load_pnm and load_pfm for similar-looking formats; HDRI pipeline files truncated mid-upload; tools writing PFM with a Windows BOM or leading whitespace.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/86662a2021a1cce5. Report an issue: GitHub.