Yalantis/uCrop · warning
load_pnm(): COLORMAX field is undefined in file '%s'.
Error message
load_pnm(): COLORMAX field is undefined in file '%s'.
What it means
CImg<T>::load_pnm() parses Netpbm (PBM/PGM/PPM) headers token by token. For types other than P1/P4 (plain/binary bitmap, which have no COLORMAX), it expects a third header field, the maximum gray/color value; if fscanf of that field fails (cimg_sscanf != 1), it warns non-fatally and continues with an undefined/zero colormax, which later corrupts scaling of pixel values.
Source
Thrown at ucrop/src/main/jni/CImg.h:57589
if (!file) cimg::fclose(nfile);
throw CImgIOException(_cimg_instance
"load_pnm(): PNM 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," %u %u %u %u",&W,&H,&D,&colormax))<2) {
if (!file) cimg::fclose(nfile);
throw CImgIOException(_cimg_instance
"load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.",
cimg_instance,
filename?filename:"(FILE*)");
}
if (ppm_type!=1 && ppm_type!=4) {
if (err==2 || (err==3 && (ppm_type==5 || ppm_type==7 || ppm_type==8 || ppm_type==9))) {
while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
if (cimg_sscanf(item,"%u",&colormax)!=1)
cimg::warn(_cimg_instance
"load_pnm(): COLORMAX field is undefined in file '%s'.",
cimg_instance,
filename?filename:"(FILE*)");
} else { colormax = D; D = 1; }
}
std::fgetc(nfile);
if (filename) { // Check that dimensions specified in file does not exceed the buffer dimension
const cimg_int64 siz = cimg::fsize(filename);
if (W*H*D>siz)
throw CImgIOException(_cimg_instance
"load_pnm(): Specified image dimensions in file '%s' exceed file size.",
cimg_instance,
filename);
}
switch (ppm_type) {
case 1 : { // 2D B&W asciiView on GitHub (pinned to f788b534b4)
Solutions
- Fix the PNM file so its header contains the three required fields: magic, width height, and maxval (e.g. "P5\n640 480\n255\n")
- Ensure the maxval is a plain integer with no stray characters or misplaced comment lines between header tokens
- Regenerate the file with a conforming Netpbm tool (pnmtoplainpnm, ImageMagick convert) to normalize the header
- Add a pre-parse header check in your pipeline that validates the presence of the maxval line before calling load_pnm
Example fix
// before (broken header) P5 640 480 <raw data> // missing maxval line // after P5 640 480 255 <raw data>
Defensive patterns
Strategy: validation
Validate before calling
// validate a PGM/PPM header has magic, dimensions, and maxval before load_pnm
std::ifstream in(path);
std::string magic; int w = 0, h = 0, maxval = 0;
in >> magic >> w >> h >> maxval;
if (!(magic == "P2" || magic == "P3" || magic == "P5" || magic == "P6") ||
w <= 0 || h <= 0 || maxval <= 0 || maxval > 65535)
throw std::runtime_error("malformed PNM header (missing/invalid COLORMAX)"); Type guard
bool valid_pnm_header(const std::string& magic, int maxval) {
return (magic == "P2" || magic == "P3" || magic == "P5" || magic == "P6") && maxval > 0;
} Prevention
- Ensure PNM files carry the maxval line (except P1/P4 which have none)
- Regenerate headers with conforming Netpbm/ImageMagick tools
- Keep comments out of the middle of PNM headers
- Pre-parse the header in pipeline ingestion to fail fast
When it happens
Trigger: Loading a PGM/PPM (P2,P3,P5,P6...) file whose header lacks the COLORMAX field — e.g. only "P2\n640 480\n" then pixel data, or the field is replaced by a comment/garbage that sscanf('%u') cannot parse.
Common situations: Hand-written or generated PNM files missing the maxval line, files where a comment line is malformed and consumed incorrectly, third-party tools emitting non-conforming Netpbm headers.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.
- load_pnm(): PNM header not found in file '%s'.
- load_pnm(): Specified image dimensions in file '%s' exceed f
- load_pnm(): PNM type 'P%d' found, but type is not supported.
- load_pfm(): PFM header not found in file '%s'.
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/f4eb150a167df5aa.
Report an issue: GitHub.