{"record":{"id":"bd688dd256ed2a34","repo":"Yalantis/uCrop","slug":"load-pnm-width-and-height-fields-undefined-in-f","errorCode":null,"errorMessage":"load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.","messagePattern":"load_pnm\\(\\): WIDTH and HEIGHT fields undefined in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57580,"sourceCode":"                                    cimg_instance);\n\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb\");\n      unsigned int ppm_type, W, H, D = 1, colormax = 255;\n      CImg<charT> item(16384,1,1,1,0);\n      int err, rval, gval, bval;\n      const longT cimg_iobuffer = (longT)24*1024*1024;\n      while ((err=std::fscanf(nfile,\"%16383[^\\n]\",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);\n      if (cimg_sscanf(item,\" P%u\",&ppm_type)!=1) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_pnm(): PNM 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,\" %u %u %u %u\",&W,&H,&D,&colormax))<2) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      }\n      if (ppm_type!=1 && ppm_type!=4) {\n        if (err==2 || (err==3 && (ppm_type==5 || ppm_type==7 || ppm_type==8 || ppm_type==9))) {\n          while ((err=std::fscanf(nfile,\" %16383[^\\n]\",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);\n          if (cimg_sscanf(item,\"%u\",&colormax)!=1)\n            cimg::warn(_cimg_instance\n                       \"load_pnm(): COLORMAX field is undefined in file '%s'.\",\n                       cimg_instance,\n                       filename?filename:\"(FILE*)\");\n        } else { colormax = D; D = 1; }\n      }\n      std::fgetc(nfile);\n\n      if (filename) { // Check that dimensions specified in file does not exceed the buffer dimension\n        const cimg_int64 siz = cimg::fsize(filename);","sourceCodeStart":57562,"sourceCodeEnd":57598,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57562-L57598","documentation":"After reading the PNM magic number, load_pnm() reads the next non-comment line and parses WIDTH, HEIGHT (and optionally DEPTH and COLORMAX) fields. If fewer than 2 unsigned integers are found (cimg_sscanf returns <2), the header is incomplete and the library throws CImgIOException.","triggerScenarios":"load_pnm() on a file with a valid 'P<digit>' magic line but missing or malformed width/height fields: header truncated after the magic number, non-numeric text where dimensions should be, extra comment lines not starting with '#' breaking the parser, or a file containing only 'P6'.","commonSituations":"Hand-written PNM generators that forgot the dimension line; truncation during network transfer or interrupted write; template files with placeholders like 'W H' left unsubstituted.","solutions":["Open the file and confirm the second header line contains two or more integers (e.g. '640 480').","Fix the generating code/exporter to emit 'width height' after the magic number.","Re-export the image with a standard tool (`convert in.png out.pnm`) to regenerate a valid header.","Check the file was fully written (compare size against expected dimensions); re-download if truncated.","Catch CImgIOException around load_pnm and fall back to another decoder."],"exampleFix":"// before\nf << \"P6\\n\";              // header incomplete\n// after\nf << \"P6\\n\" << width << \" \" << height << \"\\n255\\n\";","handlingStrategy":"validation","validationCode":"bool pnmHeaderComplete(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_pnm(path);\n} catch (cimg_library::CImgIOException& e) {\n  std::cerr << \"Malformed PNM header: \" << e.what() << \"\\n\";\n}","preventionTips":["Always emit 'width height' after the magic number when writing PNMs","Verify written files end with the full raster data","Round-trip test generated PNMs with a reference decoder","Validate header lines before loading"],"tags":["cimg","pnm","image-loading","malformed-header"],"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"}