{"record":{"id":"cbdf43ad4c7c4a20","repo":"Yalantis/uCrop","slug":"load-cimg-cimg-header-not-found-in-file-s","errorCode":null,"errorMessage":"load_cimg(): CImg header not found in file '%s'.","messagePattern":"load_cimg\\(\\): CImg header not found in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":66979,"sourceCode":"      unsigned int\n        nn0 = std::min(n0,n1), nn1 = std::max(n0,n1),\n        nx0 = std::min(x0,x1), nx1 = std::max(x0,x1),\n        ny0 = std::min(y0,y1), ny1 = std::max(y0,y1),\n        nz0 = std::min(z0,z1), nz1 = std::max(z0,z1),\n        nc0 = std::min(c0,c1), nc1 = std::max(c0,c1);\n\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb\");\n      bool loaded = false, endian = cimg::endianness();\n      CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);\n      *tmp = *str_pixeltype = *str_endian = 0;\n      unsigned int j, N, W, H, D, C;\n      int i, err;\n      j = 0; while ((i=std::fgetc(nfile))!='\\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;\n      err = cimg_sscanf(tmp,\"%u%*c%255[A-Za-z123468_]%*c%255[sA-Za-z_ ]\",\n                        &N,str_pixeltype._data,str_endian._data);\n      if (err<2) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimglist_instance\n                              \"load_cimg(): CImg header not found in file '%s'.\",\n                              cimglist_instance,\n                              filename?filename:\"(FILE*)\");\n      }\n      if (!cimg::strncasecmp(\"little\",str_endian,6)) endian = false;\n      else if (!cimg::strncasecmp(\"big\",str_endian,3)) endian = true;\n      nn1 = n1==~0U?N - 1:n1;\n      if (nn1>=N)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) \"\n                                    \"because file '%s' contains only %u images.\",\n                                    cimglist_instance,\n                                    n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,filename?filename:\"(FILE*)\",N);\n      assign(1 + nn1 - n0);\n      _cimg_load_cimg_case2(\"bool\",0,0,cimg_uint8);\n      _cimg_load_cimg_case2(\"uint8\",\"unsigned char\",\"uchar\",cimg_uint8);\n      _cimg_load_cimg_case2(\"int8\",0,0,cimg_int8);\n      _cimg_load_cimg_case2(\"char\",0,0,char);","sourceCodeStart":66961,"sourceCodeEnd":66997,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L66961-L66997","documentation":"Single-image CImg<T>::load_cimg() reads the first line expecting '<N><sep><pixeltype><sep><endian>'; if sscanf extracts fewer than 2 fields it throws CImgIOException saying the CImg header was not found (CImg.h:66979). The stream is not a valid .cimg file.","triggerScenarios":"Calling img.load_cimg(filename) on a non-CImg file (PNG/JPEG/raw), an empty file, or a .cimg whose first line was truncated, BOM-prefixed, or CRLF-mangled.","commonSituations":"Wrong extension/format mix-up; interrupted transfer; downloading a dataset URL that served an error page; using a gzip-compressed file directly instead of via load_cimg with zlib.","solutions":["Confirm the first line is 'W H D C'-style stream header like '1 unsigned_char little_endian' for the image list; fix or re-export the file.","Use the format-appropriate loader (CImg::load / load_png / load_raw) if the file is not .cimg.","Re-transfer in binary mode to avoid header mangling; strip any BOM.","If the file is .cimg.gz, decompress it or enable zlib support first."],"exampleFix":"// before\nimg.load_cimg(maybeGzipPath); // not a plain .cimg\n\n// after\nstd::string path = maybeGzipPath;\nif (hasGzipMagic(path)) path = gunzipToTemp(path);\nstd::ifstream f(path, std::ios::binary);\nchar head[64] = {0}; f.read(head, 63);\nunsigned n; char pt[64];\nif (std::sscanf(head, \"%u %63s\", &n, pt) >= 2) img.load_cimg(path.c_str());\nelse img.load(path.c_str());","handlingStrategy":"validation","validationCode":"bool looksLikeCimg(const char* path) {\n  std::FILE* f = std::fopen(path, \"rb\"); if (!f) return false;\n  char line[256] = {0};\n  bool got = std::fgets(line, sizeof line, f) != nullptr;\n  std::fclose(f);\n  unsigned N; char pt[64], en[64];\n  return got && std::sscanf(line, \"%u %63[A-Za-z123468_] %63[sA-Za-z_ ]\", &N, pt, en) >= 2;\n}","typeGuard":null,"tryCatchPattern":"try {\n  img.load_cimg(path);\n} catch (cimg_library::CImgIOException& e) {\n  if (!looksLikeCimg(path)) img.load(path); // format auto-detection fallback\n  else throw;\n}","preventionTips":["Validate the first line matches 'N pixeltype endian' before load_cimg","Route gzip files through decompression (or enable zlib) first","Keep .cimg data binary-safe end to end","Check downloaded files for HTML/empty content before loading"],"tags":["cimg","cpp","file-format","header-parsing","image-loading"],"backgroundTag":"unrecognized-file-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"}