Yalantis/uCrop · error · CImgIOException
load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in
Error message
load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in file '%s'.
What it means
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.
Source
Thrown at ucrop/src/main/jni/CImg.h:57797
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*)");
}
std::fgetc(nfile);
const bool is_color = (pfm_type=='F'), is_inverted = (scale>0)!=cimg::endianness();
if (is_color) {
assign(W,H,1,3,(T)0);
CImg<floatT> buf(3*W);
T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);View on GitHub (pinned to f788b534b4)
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.
Example fix
// before out << "PF\n" << w << " " << scale << "\n"; // scale leaked into dims // after out << "PF\n" << w << " " << h << "\n" << scale << "\n";
Defensive patterns
Strategy: validation
Validate before calling
bool pfmDimsValid(const char* path) {
std::ifstream f(path);
std::string l1, l2; std::getline(f, l1); std::getline(f, l2);
int w=0, h=0;
return std::sscanf(l2.c_str(), "%d %d", &w, &h) == 2 && w > 0 && h > 0;
} Type guard
bool positiveDims(int w, int h) { return w > 0 && h > 0; } Try / catch
try {
img.load_pfm(path);
} catch (cimg_library::CImgIOException& e) {
std::cerr << "Invalid PFM dimensions: " << e.what() << "\n";
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- load_pfm(): Specified filename is (null).
- load_pfm(): PFM header not found in file '%s'.
- load_pfm(): WIDTH and HEIGHT fields are undefined in file '%
- load_pfm(): SCALE field is undefined in file '%s'.
- load(): Failed to open file '%s'.
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/3930af9da8abfd64.
Report an issue: GitHub.