{"record":{"id":"23c4d328bba27b30","repo":"Yalantis/uCrop","slug":"load-analyze-invalid-header-size-u-specified","errorCode":null,"errorMessage":"load_analyze(): Invalid header size (%u) specified in file '%s'.","messagePattern":"load_analyze\\(\\): Invalid header size \\(%u\\) specified in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":58421,"sourceCode":"        } 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)\n        throw CImgIOException(_cimg_instance\n                              \"load_analyze(): Cannot read header (of size %u) in file '%s'.\",\n                              cimg_instance,\n                              header_size,filename?filename:\"(FILE*)\");\n\n      if (!file && nfile_header!=nfile) cimg::fclose(nfile_header);\n      if (endian) {\n        cimg::invert_endianness((short*)(header + 40),5);\n        cimg::invert_endianness((short*)(header + 70),1);\n        cimg::invert_endianness((short*)(header + 72),1);\n        cimg::invert_endianness((float*)(header + 76),4);","sourceCodeStart":58403,"sourceCodeEnd":58439,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L58403-L58439","documentation":"After opening the Analyze/NIfTI header, load_analyze() reads the first 4-byte 'sizeof_hdr' field. A valid header is at least 128 bytes (Analyze7.5 uses 348, NIfTI-1 uses 348). If the value read is below 128 it throws; >=4096 only triggers byte-order inversion, not this error. This almost always means the file is not actually an Analyze7.5/NIfTI file.","triggerScenarios":"load_analyze() given a file whose first 4 bytes, interpreted as a little-endian uint32, are < 128 — e.g. a truncated, empty, text, or wrongly-renamed file passed to CImg::load(\"x.img\") or load_analyze().","commonSituations":"Files downloaded incompletely; a non-medical file renamed to .hdr/.img; zero-byte placeholder files; loading a NIfTI-2 (.nii2, 540-byte different layout) or other format via the Analyze path.","solutions":["Check the file's first 4 bytes: sizeof_hdr must be 348 (NIfTI-1/Analyze) — validate before calling.","Re-download or re-export the file; verify non-zero, complete size.","Confirm the file is really Analyze7.5/NIfTI-1, not DICOM/NIfTI-2; use the matching loader.","If bytes look swapped, the endianness handling covers >=4096 only for sizes; a corrupt header still needs re-export.","Catch CImgIOException and fall back to format sniffing (e.g. try load_nii/load_analyze or file magic)."],"exampleFix":"// before\nimg.load(\"mystery.img\"); // header_size < 128 -> throw\n// after\nunsigned int h; std::FILE* f = std::fopen(\"mystery.img\",\"rb\");\nstd::fread(&h,4,1,f); std::fclose(f);\nif (h == 348) img.load_analyze(\"mystery.img\"); else throw std::runtime_error(\"not Analyze/NIfTI-1\");","handlingStrategy":"validation","validationCode":"bool looksLikeAnalyzeNifti(const char* p) {\n  std::FILE* f = std::fopen(p, \"rb\"); if (!f) return false;\n  unsigned int h = 0; bool ok = std::fread(&h, 4, 1, f) == 1; std::fclose(f);\n  if (!ok) return false;\n  unsigned int sw = h; cimg::invert_endianness(sw);\n  return h >= 128 || sw >= 128; // accept either byte order\n}","typeGuard":"bool hasValidHeaderSize(unsigned int header_size) { return header_size >= 128 && header_size < 4096 * 64; }","tryCatchPattern":"try { img.load_analyze(path); }\ncatch (CImgIOException& e) { fprintf(stderr, \"Bad sizeof_hdr field: %s\\n\", e.what()); /* try other loader */ }","preventionTips":["Sniff the first 4 bytes before dispatching to load_analyze().","Verify download completeness (size/checksum) for medical datasets.","Never rename foreign files to .img/.hdr to force loading.","Watch for NIfTI-2 files (sizeof_hdr=540) needing different handling."],"tags":["cimg","file-format","nifti","corrupt-file"],"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"}