{"record":{"id":"03935c7814d87f39","repo":"Yalantis/uCrop","slug":"load-analyze-unable-to-load-datatype-d-in-file","errorCode":null,"errorMessage":"load_analyze(): Unable to load datatype %d in file '%s'","messagePattern":"load_analyze\\(\\): Unable to load datatype (.+?) in file '(.+?)'","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":58514,"sourceCode":"        delete[] buffer;\n      } break;\n      case 16 : {\n        float *const buffer = new float[pdim];\n        cimg::fread(buffer,pdim,nfile);\n        if (endian) cimg::invert_endianness(buffer,pdim);\n        cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);\n        delete[] buffer;\n      } break;\n      case 64 : {\n        double *const buffer = new double[pdim];\n        cimg::fread(buffer,pdim,nfile);\n        if (endian) cimg::invert_endianness(buffer,pdim);\n        cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);\n        delete[] buffer;\n      } break;\n      default :\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_analyze(): Unable to load datatype %d in file '%s'\",\n                              cimg_instance,\n                              datatype,filename?filename:\"(FILE*)\");\n      }\n      if (!file) cimg::fclose(nfile);\n      return *this;\n    }\n\n    //! Load image from a .cimg[z] file.\n    /**\n      \\param filename Filename, as a C-string.\n      \\param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.\n      \\param align Appending alignment.\n    **/\n    CImg<T>& load_cimg(const char *const filename, const char axis='z', const float align=0) {\n      CImgList<T> list;\n      list.load_cimg(filename);\n      if (list._width==1) return list[0].move_to(*this);","sourceCodeStart":58496,"sourceCodeEnd":58532,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L58496-L58532","documentation":"The Analyze/NIfTI datatype field in the header maps to known storage codes (2=uint8, 4=int16, 8=int32, 16=float32, 32/64=complex, 128=RGB, 256=int8, 512=uint16, 768=uint32, 1024=float64...). If the datatype value does not match any implemented case, load_analyze() throws after closing the file. Newer NIfTI datatype codes (e.g. 2304 RGB, 256/768 in very old CImg builds, or NIfTI-2 codes) may be unsupported by the bundled CImg version.","triggerScenarios":"Header declares a datatype code outside the switch handled by this CImg build — e.g. datatype=2304 (NIfTI RGB24), datatype=0 (unknown), or a corrupted/zeroed datatype field; encountered via load()/load_analyze() on an .img/.hdr/.nii volume.","commonSituations":"Images exported by newer conversion tools (dcm2niix with RGB, INT8, or float128 outputs) fed to an old CImg snapshot; headers hand-edited or zero-filled by buggy writers; NIfTI-2 files parsed as NIfTI-1 shifting the datatype field.","solutions":["Inspect datatype in the header (offset 70 in NIfTI-1/Analyze) and convert the volume to a supported type (e.g. int16/float32) with a converter tool (fslchfiletype, nibabel).","Update the CImg.h snapshot in ucrop/src/main/jni to a newer version supporting more datatypes.","If datatype==0, re-export with a proper writer; the field was never set.","Read the pixel data manually with known dims/datatype and construct the CImg yourself as a workaround.","Catch CImgIOException in batch pipelines and quarantine unsupported volumes for conversion."],"exampleFix":"# before: datatype 2304 (RGB) unsupported by old CImg\n# after: convert to uint8/int16 first\n$ fslchfiletype NIFTI_PAIR in.nii out   # or with nibabel:\nimport nibabel as nib\nimg = nib.load('in.nii'); nib.save(nib.Nifti1Image(img.get_fdata().mean(-1).astype('int16'), img.affine), 'out.nii')","handlingStrategy":"try-catch","validationCode":"bool datatypeSupported(std::FILE* hdr) {\n  // datatype at offset 70 in NIfTI-1/Analyze header\n  int dt = 0; if (std::fseek(hdr, 70, SEEK_SET) != 0) return false;\n  if (std::fread(&dt, sizeof(int), 1, hdr) != 1) return false;\n  switch (dt) { case 1: case 2: case 4: case 8: case 16: case 32: case 64:\n  case 128: case 256: case 512: case 768: case 1024: return true; default: return false; }\n}","typeGuard":"bool isSupportedDatatype(int datatype) { return datatype != 0 && datatype <= 1024 && (datatype & (datatype - 1)) == 0 || datatype == 128; }","tryCatchPattern":"try { img.load_analyze(path); }\ncatch (CImgIOException& e) {\n  fprintf(stderr, \"Unsupported datatype — converting volume\\n\");\n  convertWithNibabelOrFsl(path); // external conversion fallback\n}","preventionTips":["Pre-convert volumes to int16/float32 with dcm2niix defaults.","Update the bundled CImg.h to a recent version.","Reject datatype==0 headers at ingestion time.","Test pipelines against all datatypes your scanners emit."],"tags":["cimg","dtype","nifti","unsupported"],"backgroundTag":"unsupported-dtype","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"}