{"record":{"id":"c31842276e3064fd","repo":"Yalantis/uCrop","slug":"load-pnm-pnm-header-not-found-in-file-s","errorCode":null,"errorMessage":"load_pnm(): PNM header not found in file '%s'.","messagePattern":"load_pnm\\(\\): PNM header not found in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57572,"sourceCode":"    static CImg<T> get_load_pnm(std::FILE *const file) {\n      return CImg<T>().load_pnm(file);\n    }\n\n    CImg<T>& _load_pnm(std::FILE *const file, const char *const filename) {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"load_pnm(): Specified filename is (null).\",\n                                    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'.\",","sourceCodeStart":57554,"sourceCodeEnd":57590,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57554-L57590","documentation":"CImg's load_pnm() parses Portable Anymap (PNM/PBM/PGM/PPM) headers. It first scans the leading line and requires it to match the magic number 'P<digit>' (e.g. P2, P6). If the first non-comment line does not parse as a PNM magic token, it throws CImgIOException, meaning the file is not a recognizable PNM image.","triggerScenarios":"Calling CImg<T>::load_pnm(filename) (or load() auto-detecting pnm) on a file whose first non-comment line lacks a valid 'P<type>' magic token: a truncated/corrupted PNM, a renamed non-PNM file (e.g. a JPEG renamed to .pnm), an empty file, or a file starting with garbage/extra bytes before the magic number.","commonSituations":"Files misnamed during download or conversion pipelines; text editors or scripts prepending BOM/whitespace; truncated uploads; passing an SVG/JPEG file path to load_pnm directly.","solutions":["Verify the file actually starts with a PNM magic number (P1-P7) by inspecting the first bytes, e.g. 'head -c 2 file.pnm'.","Re-export or re-convert the image to PNM with a known-good tool (e.g. `convert img.jpg img.pnm`).","Remove any leading BOM, whitespace, or junk lines before the 'P<digit>' token.","If the file is a different format, call the matching loader (load_jpeg, load_png, ...) or plain load() instead of load_pnm.","Wrap the load call in try/catch for CImgIOException and report the bad file to the user."],"exampleFix":"// before\nimg.load_pnm(path); // crashes on non-PNM input\n// after\nstd::ifstream f(path, std::ios::binary);\nchar magic[2] = {0};\nf.read(magic, 2);\nif (magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '7') {\n  img.load_pnm(path);\n} else {\n  img.load(path.c_str()); // fall back to format auto-detection\n}","handlingStrategy":"validation","validationCode":"bool looksLikePnm(const char* path) {\n  std::ifstream f(path, std::ios::binary);\n  char m[2] = {0};\n  f.read(m, 2);\n  return f && m[0]=='P' && m[1]>='1' && m[1]<='6';\n}","typeGuard":"bool isPnmMagic(const unsigned char* b, size_t n) {\n  return n >= 2 && b[0]=='P' && b[1]>='1' && b[1]<='6';\n}","tryCatchPattern":"try {\n  img.load_pnm(path);\n} catch (cimg_library::CImgIOException& e) {\n  std::cerr << \"Invalid PNM: \" << e.what() << \"\\n\";\n}","preventionTips":["Check the first two bytes equal P1-P6 before calling load_pnm","Never rename files between formats without converting","Strip BOMs and leading whitespace from generated PNMs","Use load() auto-detection when the format is unknown"],"tags":["cimg","pnm","image-loading","file-parsing"],"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"}