{"record":{"id":"38774b50ac945a03","repo":"Yalantis/uCrop","slug":"cimg-s-load-inr-inrimage-4-header-not-found","errorCode":null,"errorMessage":"CImg<%s>::load_inr(): INRIMAGE-4 header not found.","messagePattern":"CImg<(.+?)>::load_inr\\(\\): INRIMAGE-4 header not found\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":58649,"sourceCode":"    }\n\n    //! Load image from an INRIMAGE-4 file \\overloading.\n    CImg<T>& load_inr(std::FILE *const file, float *const voxel_size=0) {\n      return _load_inr(file,0,voxel_size);\n    }\n\n    //! Load image from an INRIMAGE-4 file \\newinstance.\n    static CImg<T> get_load_inr(std::FILE *const file, float *voxel_size=0) {\n      return CImg<T>().load_inr(file,voxel_size);\n    }\n\n    static void _load_inr_header(std::FILE *file, int out[8], float *const voxel_size) {\n      CImg<charT> item(1024), tmp1(64), tmp2(64);\n      *item = *tmp1 = *tmp2 = 0;\n      out[0] = std::fscanf(file,\"%63s\",item._data);\n      out[0] = out[1] = out[2] = out[3] = out[5] = 1; out[4] = out[6] = out[7] = -1;\n      if (cimg::strncasecmp(item,\"#INRIMAGE-4#{\",13)!=0)\n        throw CImgIOException(\"CImg<%s>::load_inr(): INRIMAGE-4 header not found.\",\n                              pixel_type());\n\n      while (std::fscanf(file,\" %63[^\\n]%*c\",item._data)!=EOF && std::strncmp(item,\"##}\",3)) {\n        cimg_sscanf(item,\" XDIM%*[^0-9]%d\",out);\n        cimg_sscanf(item,\" YDIM%*[^0-9]%d\",out + 1);\n        cimg_sscanf(item,\" ZDIM%*[^0-9]%d\",out + 2);\n        cimg_sscanf(item,\" VDIM%*[^0-9]%d\",out + 3);\n        cimg_sscanf(item,\" PIXSIZE%*[^0-9]%d\",out + 6);\n        if (voxel_size) {\n          cimg_sscanf(item,\" VX%*[^0-9.+-]%f\",voxel_size);\n          cimg_sscanf(item,\" VY%*[^0-9.+-]%f\",voxel_size + 1);\n          cimg_sscanf(item,\" VZ%*[^0-9.+-]%f\",voxel_size + 2);\n        }\n        if (cimg_sscanf(item,\" CPU%*[ =]%s\",tmp1._data)) out[7] = cimg::strncasecmp(tmp1,\"sun\",3)?0:1;\n        switch (cimg_sscanf(item,\" TYPE%*[ =]%s %s\",tmp1._data,tmp2._data)) {\n        case 0 : break;\n        case 2 :\n          out[5] = cimg::strncasecmp(tmp1,\"unsigned\",8)?1:0;","sourceCodeStart":58631,"sourceCodeEnd":58667,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L58631-L58667","documentation":"CImg's INR (INRIMAGE-4) loader requires the file to begin with the magic token '#INRIMAGE-4#{'. _load_inr_header() reads the first token via fscanf and, if strncasecmp against that magic fails, throws this exception. It means the file is not an INRIMAGE-4 file (or the magic line is corrupted).","triggerScenarios":"Calling CImg::load_inr() or load() on a file dispatched as .inr/.inr.gz whose first bytes are not '#INRIMAGE-4#{' — e.g. a DICOM/PNM file renamed to .inr, or an older INRIMAGE-3 header.","commonSituations":"Wrong extension routing in load(): a data file with .inr extension that is actually another format; gzipped INR handled elsewhere and passed raw; files re-saved by tools that dropped the header comment block.","solutions":["Check the first 13 bytes of the file equal '#INRIMAGE-4#{' before calling load_inr().","Convert the file to INRIMAGE-4 (or another supported format) using the original tool (e.g. InrImage/Greyscale conversion utilities).","Verify the extension matches the real format and let CImg auto-detect via load() with the correct extension.","If the file starts with a BOM or extra whitespace/newline, strip it — the magic must be the first token.","Catch CImgIOException and attempt alternative loaders based on magic-byte sniffing."],"exampleFix":"// before\nimg.load_inr(\"volume.inr\"); // actually a DICOM file renamed\n// after\nstd::FILE* f = std::fopen(\"volume.inr\",\"rb\"); char magic[14] = {0};\nstd::fread(magic,1,13,f); std::fclose(f);\nif (std::strncmp(magic, \"#INRIMAGE-4#{\", 13) == 0) img.load_inr(\"volume.inr\");\nelse img.load_medcon_external(\"volume.inr\"); // or proper converter","handlingStrategy":"validation","validationCode":"bool isInrImage(const std::string& path) {\n  std::FILE* f = std::fopen(path.c_str(), \"rb\"); if (!f) return false;\n  char magic[14] = {0}; std::fread(magic, 1, 13, f); std::fclose(f);\n  return cimg::strncasecmp(magic, \"#INRIMAGE-4#{\", 13) == 0;\n}","typeGuard":"bool hasInrMagic(const char* magic) { return magic && std::strncmp(magic, \"#INRIMAGE-4#{\", 13) == 0; }","tryCatchPattern":"try { img.load_inr(path); }\ncatch (CImgIOException& e) { fprintf(stderr, \"Not INRIMAGE-4, sniffing real format...\\n\"); img.load(path.c_str()); }","preventionTips":["Sniff magic bytes before choosing a loader.","Keep file extensions truthful — never rename to .inr.","Re-export via the original InrImage toolchain if headers were stripped.","Strip BOMs/leading whitespace from converted files."],"tags":["cimg","file-format","magic-bytes","inrimage"],"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"}