{"record":{"id":"46abd04c7b382e61","repo":"Yalantis/uCrop","slug":"load-analyze-cannot-read-header-of-size-u-in","errorCode":null,"errorMessage":"load_analyze(): Cannot read header (of size %u) in file '%s'.","messagePattern":"load_analyze\\(\\): Cannot read header \\(of size %u\\) in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":58429,"sourceCode":"                              \"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);\n        cimg::invert_endianness((float*)(header + 108),1);\n        cimg::invert_endianness((float*)(header + 112),1);\n      }\n\n      if (nfile_header==nfile) {\n        const unsigned int vox_offset = (unsigned int)*(float*)(header + 108);\n        std::fseek(nfile,vox_offset,SEEK_SET);\n      }","sourceCodeStart":58411,"sourceCodeEnd":58447,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L58411-L58447","documentation":"load_analyze() allocates header_size bytes and reads header_size-4 more bytes from the header file. If fread returns fewer bytes than requested, the header file is truncated relative to its own declared sizeof_hdr, and this exception is thrown. It is a short-read/corrupt-file condition.","triggerScenarios":"Header file whose declared sizeof_hdr (e.g. 348) exceeds the actual file size — truncated download, partially-copied .hdr, or an .img file mistakenly opened as the header.","commonSituations":"Incomplete dataset transfers (ftp/usb cut off); backup tools skipping files; .hdr and .img pair mixed up so the data file is parsed as a header.","solutions":["Verify the .hdr file size is at least sizeof_hdr bytes (348 for standard Analyze7.5/NIfTI-1) before loading.","Re-transfer/re-export the truncated header file.","Ensure you point load_analyze() at the .img and let it find the matching .hdr, not at the raw data file as the header.","Compare checksums of source and copied headers to detect partial copies.","Catch CImgIOException and skip/re-fetch the offending volume in batch processing."],"exampleFix":"// before\nimg.load_analyze(\"t1.img\"); // t1.hdr truncated at 200 bytes, claims 348\n// after\nif (cimg::fsize(\"t1.hdr\") < 348) throw std::runtime_error(\"t1.hdr truncated\");\nimg.load_analyze(\"t1.img\");","handlingStrategy":"validation","validationCode":"bool headerComplete(const char* hdrPath) {\n  std::FILE* f = std::fopen(hdrPath, \"rb\"); if (!f) return false;\n  unsigned int h = 0; bool ok = std::fread(&h, 4, 1, f) == 1;\n  long size = -1; if (ok) { std::fseek(f, 0, SEEK_END); size = std::ftell(f); }\n  std::fclose(f);\n  return ok && size >= (long)h; // file at least sizeof_hdr bytes\n}","typeGuard":"bool headerFullyRead(size_t header_size_read, unsigned int header_size) { return header_size_read == header_size - 4; }","tryCatchPattern":"try { img.load_analyze(imgPath); }\ncatch (CImgIOException& e) { fprintf(stderr, \"Header short read — re-transfer dataset: %s\\n\", e.what()); }","preventionTips":["Checksum .hdr files after dataset transfers.","Keep .hdr/.img pairs from the same export together.","Don't point the loader at the data file as a header.","Monitor free space during dataset copies."],"tags":["cimg","file-format","truncated-file","nifti"],"backgroundTag":"file-read-failed","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"}