{"record":{"id":"554b151f31dfe245","repo":"Yalantis/uCrop","slug":"load-cimg-file-or-cimg-header-not-found-in-file","errorCode":null,"errorMessage":"load_cimg(): File or CImg header not found in file '%s'.","messagePattern":"load_cimg\\(\\): File or CImg header not found in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":66801,"sourceCode":"                                    \"load_cimg(): Specified filename is (null).\",\n                                    cimglist_instance);\n\n      const ulongT cimg_iobuffer = (ulongT)24*1024*1024;\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 = 0, W, H, D, C;\n      cimg_uint64 csiz;\n      int i, err;\n      do {\n        j = 0; while ((i=std::fgetc(nfile))!='\\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0;\n      } while (*tmp=='#' && i>=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(): File or 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      assign(N);\n      _cimg_load_cimg_case(\"bool\",0,0,cimg_uint8);\n      _cimg_load_cimg_case(\"uint8\",\"unsigned_char\",\"uchar\",cimg_uint8);\n      _cimg_load_cimg_case(\"int8\",0,0,cimg_int8);\n      _cimg_load_cimg_case(\"char\",0,0,char);\n      _cimg_load_cimg_case(\"uint16\",\"unsigned_short\",\"ushort\",cimg_uint16);\n      _cimg_load_cimg_case(\"int16\",\"short\",0,cimg_int16);\n      _cimg_load_cimg_case(\"uint32\",\"unsigned_int\",\"uint\",cimg_uint32);\n      _cimg_load_cimg_case(\"int32\",\"int\",0,cimg_int32);\n      _cimg_load_cimg_case(\"unsigned_long\",\"ulong\",0,cimg_ulong);\n      _cimg_load_cimg_case(\"long\",0,0,cimg_long);\n      _cimg_load_cimg_case(\"uint64\",\"unsigned_int64\",0,cimg_uint64);","sourceCodeStart":66783,"sourceCodeEnd":66819,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L66783-L66819","documentation":"load_cimg() first reads a header line expected to decode as '<N><separator><pixeltype><separator><endian>'; if sscanf extracts fewer than 2 fields it concludes the stream is not a CImg-format file and throws (CImg.h:66801). Despite the message, the file usually exists — its content just isn't a valid .cimg header.","triggerScenarios":"Calling CImgList::load_cimg() on a file in a different format (PNG, raw, TIFF), an empty or HTML error-page saved as .cimg, or a .cimg file whose first line was stripped/truncated.","commonSituations":"Wrong file passed to the loader after a rename; downloading a dataset URL that returned a 404 page saved to disk; text-mode conversion inserting CRLF or BOM into the header; mixing up .cimg with .cimg.gz.","solutions":["Verify the file is genuinely CImg format: first line must be like '3 unsigned_char little_endian'.","Check you are not loading an HTML error page or empty file: inspect first bytes.","Use the loader matching the actual format (CImg::load_png, load_raw, etc.) or re-export the data as .cimg.","Re-transfer the file in binary mode to avoid header corruption."],"exampleFix":"// before\nimgs.load_cimg(url.c_str()); // may be a saved 404 page\n\n// after\nstd::ifstream f(url, std::ios::binary);\nchar head[64] = {0};\nf.read(head, 63);\nunsigned n; char ptype[64];\nif (std::sscanf(head, \"%u %63s\", &n, ptype) >= 2) imgs.load_cimg(url.c_str());\nelse imgs.load(url.c_str()); // fall back to format auto-detection","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  imgs.load_cimg(path);\n} catch (cimg_library::CImgIOException& e) {\n  if (!looksLikeCimg(path))\n    imgs.load(path); // fall back to extension-based format detection\n  else throw;\n}","preventionTips":["Probe the first header line before choosing the loader","Check downloaded files are not HTML error pages (look for '<' or '!DOCTYPE')","Keep .cimg files out of text-mode transfers","Don't rename files across formats without conversion"],"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"}