{"record":{"id":"3930af9da8abfd64","repo":"Yalantis/uCrop","slug":"load-pfm-width-d-and-height-d-fields-are","errorCode":null,"errorMessage":"load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in file '%s'.","messagePattern":"load_pfm\\(\\): WIDTH \\((.+?)\\) and HEIGHT \\((.+?)\\) fields are invalid in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57797,"sourceCode":"      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      }\n      std::fgetc(nfile);\n      const bool is_color = (pfm_type=='F'), is_inverted = (scale>0)!=cimg::endianness();\n      if (is_color) {\n        assign(W,H,1,3,(T)0);\n        CImg<floatT> buf(3*W);\n        T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);","sourceCodeStart":57779,"sourceCodeEnd":57815,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57779-L57815","documentation":"load_pfm() additionally validates that the parsed WIDTH and HEIGHT are positive. If either dimension is <= 0 (e.g. negative from a sign typo or zero from an unfilled field), it throws CImgIOException listing the offending values, since a zero/negative-sized image cannot be allocated.","triggerScenarios":"load_pfm() on a file whose dimension line parses as two numbers but with W<=0 or H<=0: dimension line accidentally merged with the scale line (e.g. '-1.0' parsed as height), hand-edited headers, or generator bugs emitting 0 or negative dimensions.","commonSituations":"Writing PFM by hand where the endianness/scale line '-1.0' got misplaced onto the dimension line; uninitialized variables in a custom PFM exporter; test fixtures with 0x0 dimensions.","solutions":["Inspect the header lines: line 2 must be 'W H' (positive ints) and line 3 the scale (+1.0 or -1.0).","Fix the exporter so the scale value is on its own line after the dimensions.","Correct hand-edited dimensions to positive integers.","Add a pre-check in the caller: read the header and reject W<=0 || H<=0 before load_pfm.","Catch CImgIOException to surface 'invalid PFM dimensions' with the parsed values."],"exampleFix":"// before\nout << \"PF\\n\" << w << \" \" << scale << \"\\n\"; // scale leaked into dims\n// after\nout << \"PF\\n\" << w << \" \" << h << \"\\n\" << scale << \"\\n\";","handlingStrategy":"validation","validationCode":"bool pfmDimsValid(const char* path) {\n  std::ifstream f(path);\n  std::string l1, l2; std::getline(f, l1); std::getline(f, l2);\n  int w=0, h=0;\n  return std::sscanf(l2.c_str(), \"%d %d\", &w, &h) == 2 && w > 0 && h > 0;\n}","typeGuard":"bool positiveDims(int w, int h) { return w > 0 && h > 0; }","tryCatchPattern":"try {\n  img.load_pfm(path);\n} catch (cimg_library::CImgIOException& e) {\n  std::cerr << \"Invalid PFM dimensions: \" << e.what() << \"\\n\";\n}","preventionTips":["Keep the scale (+/-1.0) on its own header line, not merged with W/H","Initialize width/height in custom PFM exporters","Reject 0/negative dimensions at file-generation time","Sanity-check hand-edited headers before loading"],"tags":["cimg","pfm","invalid-dimensions","image-loading"],"backgroundTag":"value-out-of-range","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"}