{"record":{"id":"62987affa7eba3a2","repo":"Yalantis/uCrop","slug":"load-analyze-invalid-analyze7-5-or-nifti-header","errorCode":null,"errorMessage":"load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.","messagePattern":"load_analyze\\(\\): Invalid Analyze7\\.5 or NIFTI header in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":58410,"sourceCode":"                                    cimg_instance);\n\n      std::FILE *nfile_header = 0, *nfile = 0;\n      if (!file) {\n        CImg<charT> body(1024);\n        const char *const ext = cimg::split_filename(filename,body);\n        const unsigned int len = (unsigned int)std::strlen(body);\n        if (!cimg::strcasecmp(ext,\"hdr\")) { // File is an Analyze header file\n          nfile_header = cimg::fopen(filename,\"rb\");\n          cimg_snprintf(body._data + len,body._width - len,\".img\");\n          nfile = cimg::fopen(body,\"rb\");\n        } else if (!cimg::strcasecmp(ext,\"img\")) { // File is an Analyze data file\n          nfile = cimg::fopen(filename,\"rb\");\n          cimg_snprintf(body._data + len,body._width - len,\".hdr\");\n          nfile_header = cimg::fopen(body,\"rb\");\n        } else nfile_header = nfile = cimg::fopen(filename,\"rb\"); // File is a Niftii file\n      } else nfile_header = nfile = file; // File is a Niftii file\n      if (!nfile || !nfile_header)\n        throw CImgIOException(_cimg_instance\n                              \"load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n\n      // Read header.\n      bool endian = false;\n      unsigned int header_size;\n      cimg::fread(&header_size,1,nfile_header);\n      if (header_size>=4096) { endian = true; cimg::invert_endianness(header_size); }\n      if (header_size<128)\n        throw CImgIOException(_cimg_instance\n                              \"load_analyze(): Invalid header size (%u) specified in file '%s'.\",\n                              cimg_instance,\n                              header_size,filename?filename:\"(FILE*)\");\n\n      unsigned char *const header = new unsigned char[header_size];\n      const size_t header_size_read = cimg::fread(header + 4,header_size - 4,nfile_header);\n      if (header_size_read!=header_size - 4)","sourceCodeStart":58392,"sourceCodeEnd":58428,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L58392-L58428","documentation":"CImg's load_analyze() reads medical images in Analyze7.5 or NIfTI format. Before parsing, it tries to open the image file and, for .img files, the companion .hdr header file. If either fopen fails (returns null), it throws CImgIOException, meaning the header/data files could not be opened at all.","triggerScenarios":"Calling CImg::load_analyze() (or load() with an .img/.hdr/.nii file) when the file path does not exist, is misspelled, is unreadable, or when an Analyze .img file is given without its .hdr companion present in the same directory.","commonSituations":"Dataset paths changed after moving data; .hdr file not copied alongside the .img; wrong extension case (.HDR vs .hdr) on case-sensitive filesystems; Android/JNI app missing the asset in packaged resources.","solutions":["Verify the file path exists and is readable (e.g. check std::FILE* or std::filesystem::exists).","For Analyze7.5 .img files, ensure the .hdr file with the same basename is in the same directory.","Confirm correct file extension so CImg dispatches to the right loader (.nii for NIfTI single-file, .img/.hdr for Analyze pairs).","On Android, ensure the file is extracted from assets to a real filesystem path before loading.","Catch CImgIOException and report which of the two files was missing."],"exampleFix":"// before\nimg.load_analyze(\"scan.img\"); // fails: scan.hdr missing\n// after\nif (cimg::fsize(\"scan.hdr\") < 0) { /* copy/fix header first */ }\nimg.load_analyze(\"scan.img\");","handlingStrategy":"validation","validationCode":"bool canLoadAnalyze(const char* p) {\n  if (cimg::fsize(p) <= 0) return false;\n  CImg<char> body(p);\n  const char* dot = std::strrchr(p, '.');\n  if (dot && cimg::strncasecmp(dot, \".img\", 4) == 0) {\n    CImg<char> hdrPath(1024); std::snprintf(hdrPath, 1024, \"%.*s.hdr\", (int)(dot - p), p);\n    return cimg::fsize(hdrPath) > 0;\n  }\n  return true;\n}","typeGuard":"bool fileReadable(const std::string& p) { return std::filesystem::exists(p) && std::filesystem::is_regular_file(p); }","tryCatchPattern":"try { img.load_analyze(path.c_str()); }\ncatch (CImgIOException& e) { std::fprintf(stderr, \"Analyze/NIfTI header open failed: %s\\n\", e.what()); }","preventionTips":["Always ship .img and .hdr pairs together.","Validate paths exist before calling any CImg loader.","On Android, extract assets to filesDir before loading.","Use consistent lowercase extensions.","Log CImgIOException messages with the filename for diagnosis."],"tags":["cimg","file-io","medical-imaging","nifti"],"backgroundTag":"file-not-found","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"}