Yalantis/uCrop · error · CImgIOException
load_analyze(): Invalid Analyze7.5 or NIFTI header in file
Error message
load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.
What it means
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.
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.
Example fix
// before
img.load_analyze("scan.img"); // fails: scan.hdr missing
// after
if (cimg::fsize("scan.hdr") < 0) { /* copy/fix header first */ }
img.load_analyze("scan.img"); Defensive patterns
Strategy: validation
Validate before calling
bool canLoadAnalyze(const char* p) {
if (cimg::fsize(p) <= 0) return false;
CImg<char> body(p);
const char* dot = std::strrchr(p, '.');
if (dot && cimg::strncasecmp(dot, ".img", 4) == 0) {
CImg<char> hdrPath(1024); std::snprintf(hdrPath, 1024, "%.*s.hdr", (int)(dot - p), p);
return cimg::fsize(hdrPath) > 0;
}
return true;
} Type guard
bool fileReadable(const std::string& p) { return std::filesystem::exists(p) && std::filesystem::is_regular_file(p); } Try / catch
try { img.load_analyze(path.c_str()); }
catch (CImgIOException& e) { std::fprintf(stderr, "Analyze/NIfTI header open failed: %s\n", e.what()); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.
Related errors
- cimg::fclose(): Error code
- cimg::fclose(): Specified file is (null).
- cimg::fempty(): Specified filename is (null).
- cimg::fopen(): Failed to open file
- cimg::fopen(): File ' ', specified mode is (null).
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/62987affa7eba3a2.
Report an issue: GitHub.
Appendix: source
Thrown at ucrop/src/main/jni/CImg.h:58410
cimg_instance);
std::FILE *nfile_header = 0, *nfile = 0;
if (!file) {
CImg<charT> body(1024);
const char *const ext = cimg::split_filename(filename,body);
const unsigned int len = (unsigned int)std::strlen(body);
if (!cimg::strcasecmp(ext,"hdr")) { // File is an Analyze header file
nfile_header = cimg::fopen(filename,"rb");
cimg_snprintf(body._data + len,body._width - len,".img");
nfile = cimg::fopen(body,"rb");
} else if (!cimg::strcasecmp(ext,"img")) { // File is an Analyze data file
nfile = cimg::fopen(filename,"rb");
cimg_snprintf(body._data + len,body._width - len,".hdr");
nfile_header = cimg::fopen(body,"rb");
} else nfile_header = nfile = cimg::fopen(filename,"rb"); // File is a Niftii file
} else nfile_header = nfile = file; // File is a Niftii file
if (!nfile || !nfile_header)
throw CImgIOException(_cimg_instance
"load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.",
cimg_instance,
filename?filename:"(FILE*)");
// Read header.
bool endian = false;
unsigned int header_size;
cimg::fread(&header_size,1,nfile_header);
if (header_size>=4096) { endian = true; cimg::invert_endianness(header_size); }
if (header_size<128)
throw CImgIOException(_cimg_instance
"load_analyze(): Invalid header size (%u) specified in file '%s'.",
cimg_instance,
header_size,filename?filename:"(FILE*)");
unsigned char *const header = new unsigned char[header_size];
const size_t header_size_read = cimg::fread(header + 4,header_size - 4,nfile_header);
if (header_size_read!=header_size - 4)View on GitHub (pinned to f788b534b4)