Yalantis/uCrop · error · CImgIOException

load_pfm(): WIDTH and HEIGHT fields are undefined in file '%

Error message

load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.

What it means

After the PFM magic, load_pfm() reads a line expected to contain the WIDTH and HEIGHT as two integers. If fewer than two integers parse out (cimg_sscanf < 2), the header is incomplete and CImgIOException is thrown.

Source

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

                                    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*)");
      }
      if (err==2) {
        while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
        if (cimg_sscanf(item,"%lf",&scale)!=1)
          cimg::warn(_cimg_instance
                     "load_pfm(): SCALE field is undefined in file '%s'.",
                     cimg_instance,
                     filename?filename:"(FILE*)");
      }

View on GitHub (pinned to f788b534b4)

Solutions

  1. Confirm the second header line contains two integers (e.g. '512 384').
  2. Fix the writer to emit 'width height' after the magic line.
  3. Regenerate the PFM with a standard tool to get a valid header.
  4. Check for truncation (file too small for W*H*3 or W*H floats) and re-transfer.
  5. Catch CImgIOException and treat the file as corrupt.

Example fix

// before
out << "PF\n"; // missing dimensions
// after
out << "PF\n" << w << " " << h << "\n-1.0\n";
Defensive patterns

Strategy: validation

Validate before calling

bool pfmHeaderComplete(const char* path) {
  std::ifstream f(path);
  std::string line1, line2;
  std::getline(f, line1); std::getline(f, line2);
  int a=0, b=0;
  return std::sscanf(line2.c_str(), "%d %d", &a, &b) == 2;
}

Type guard

bool hasTwoInts(const std::string& s) {
  int a, b;
  return std::sscanf(s.c_str(), " %d %d", &a, &b) == 2;
}

Try / catch

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

Prevention

When it happens

Trigger: load_pfm() on a file with a valid 'PF'/'Pf' line but a missing or non-numeric dimension line: header truncated after magic, dimension line replaced by comment/garbage, or a file containing only the magic token.

Common situations: Interrupted writes that flushed only the first header line; hand-generated PFMs missing the 'W H' line; template files with unfilled placeholders.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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