{"record":{"id":"35650b28e4ed3e63","repo":"Yalantis/uCrop","slug":"load-pfm-width-and-height-fields-are-undefined","errorCode":null,"errorMessage":"load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.","messagePattern":"load_pfm\\(\\): WIDTH and HEIGHT fields are undefined in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57791,"sourceCode":"                                    cimg_instance);\n\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb\");\n      char pfm_type;\n      CImg<charT> item(16384,1,1,1,0);\n      int W = 0, H = 0, err = 0;\n      double scale = 0;\n      while ((err=std::fscanf(nfile,\"%16383[^\\n]\",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);\n      if (cimg_sscanf(item,\" P%c\",&pfm_type)!=1) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_pfm(): PFM header not found in file '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      }\n      while ((err=std::fscanf(nfile,\" %16383[^\\n]\",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);\n      if ((err=cimg_sscanf(item,\" %d %d\",&W,&H))<2) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      } else if (W<=0 || H<=0) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in file '%s'.\",\n                              cimg_instance,W,H,\n                              filename?filename:\"(FILE*)\");\n      }\n      if (err==2) {\n        while ((err=std::fscanf(nfile,\" %16383[^\\n]\",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);\n        if (cimg_sscanf(item,\"%lf\",&scale)!=1)\n          cimg::warn(_cimg_instance\n                     \"load_pfm(): SCALE field is undefined in file '%s'.\",\n                     cimg_instance,\n                     filename?filename:\"(FILE*)\");\n      }","sourceCodeStart":57773,"sourceCodeEnd":57809,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57773-L57809","documentation":"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.","triggerScenarios":"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.","commonSituations":"Interrupted writes that flushed only the first header line; hand-generated PFMs missing the 'W H' line; template files with unfilled placeholders.","solutions":["Confirm the second header line contains two integers (e.g. '512 384').","Fix the writer to emit 'width height' after the magic line.","Regenerate the PFM with a standard tool to get a valid header.","Check for truncation (file too small for W*H*3 or W*H floats) and re-transfer.","Catch CImgIOException and treat the file as corrupt."],"exampleFix":"// before\nout << \"PF\\n\"; // missing dimensions\n// after\nout << \"PF\\n\" << w << \" \" << h << \"\\n-1.0\\n\";","handlingStrategy":"validation","validationCode":"bool pfmHeaderComplete(const char* path) {\n  std::ifstream f(path);\n  std::string line1, line2;\n  std::getline(f, line1); std::getline(f, line2);\n  int a=0, b=0;\n  return std::sscanf(line2.c_str(), \"%d %d\", &a, &b) == 2;\n}","typeGuard":"bool hasTwoInts(const std::string& s) {\n  int a, b;\n  return std::sscanf(s.c_str(), \" %d %d\", &a, &b) == 2;\n}","tryCatchPattern":"try {\n  img.load_pfm(path);\n} catch (cimg_library::CImgIOException& e) {\n  std::cerr << \"Malformed PFM header: \" << e.what() << \"\\n\";\n}","preventionTips":["Emit 'width height' as the second header line when writing PFMs","Verify PFM writers flush all three header lines","Round-trip test generated PFMs","Validate header lines before loading"],"tags":["cimg","pfm","malformed-header","image-loading"],"backgroundTag":"schema-validation-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}