{"record":{"id":"13d33e4b2a42854a","repo":"Yalantis/uCrop","slug":"load-pfm-scale-field-is-undefined-in-file-s","errorCode":null,"errorMessage":"load_pfm(): SCALE field is undefined in file '%s'.","messagePattern":"load_pfm\\(\\): SCALE field is undefined in file '(.+?)'\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57805,"sourceCode":"      }\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);\n        cimg_forY(*this,y) {\n          cimg::fread(buf._data,3*W,nfile);\n          if (is_inverted) cimg::invert_endianness(buf._data,3*W);\n          const float *ptrs = buf._data;\n          cimg_forX(*this,x) {\n            *(ptr_r++) = (T)*(ptrs++);\n            *(ptr_g++) = (T)*(ptrs++);\n            *(ptr_b++) = (T)*(ptrs++);","sourceCodeStart":57787,"sourceCodeEnd":57823,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57787-L57823","documentation":"CImg<T>::load_pfm() parses Portable FloatMap headers: magic (PF/Pf), width height, and a SCALE line (a positive scale means big-endian samples, negative little-endian). If the SCALE field fails to parse (cimg_sscanf('%lf') != 1), it warns non-fatally and continues with an uninitialized scale, which then yields wrong byte-order interpretation (is_inverted) and bogus pixel values.","triggerScenarios":"Loading a .pfm whose header omits the scale line (e.g. \"PF\\n640 480\\n\" then data) or whose scale line is malformed/comment-corrupted so %lf parsing fails.","commonSituations":"HDR pipeline tools writing non-conforming PFM headers, hand-trimmed or generated PFM files missing the scale, comment lines between header fields confusing the token scanner.","solutions":["Fix the PFM header to include a proper scale line, e.g. \"1.0\" for big-endian data or \"-1.0\" for little-endian, right after the dimensions","Ensure no comments or stray characters sit between the width/height line and the scale line","Regenerate the file with a conforming writer (ImageMagick, PFSTools) to normalize the header","Pre-validate the PFM header in your pipeline (magic, two integers, one float) before handing it to load_pfm"],"exampleFix":"// before (broken header)\nPF\n640 480\n<binary float data>   // missing scale line\n// after\nPF\n640 480\n-1.0\n<binary float data>   // little-endian samples","handlingStrategy":"validation","validationCode":"// validate PFM header: magic, w h, scale before load_pfm\nstd::ifstream in(path);\nstd::string magic; int w = 0, h = 0; double scale = 0;\nin >> magic >> w >> h >> scale;\nif (!((magic == \"PF\" || magic == \"Pf\") && w > 0 && h > 0 && scale != 0))\n  throw std::runtime_error(\"malformed PFM header (missing/invalid SCALE)\");","typeGuard":"bool valid_pfm_header(const std::string& magic, double scale) {\n  return (magic == \"PF\" || magic == \"Pf\") && scale != 0.0;\n}","tryCatchPattern":null,"preventionTips":["Always emit a scale line (e.g. \"-1.0\") when writing PFM files","Match the scale sign to actual byte order of the samples","Validate the header (magic, ints, float) before loading HDR assets","Use conforming writers (PFSTools, ImageMagick) to produce PFM files"],"tags":["pfm","hdr","image-loading","malformed-header","byte-order","cimg"],"backgroundTag":"invalid-argument-format","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"}