{"record":{"id":"4565c264aaf7e309","repo":"Yalantis/uCrop","slug":"save-cimg-cimg-header-not-found-in-file-s","errorCode":null,"errorMessage":"save_cimg(): CImg header not found in file '%s'.","messagePattern":"save_cimg\\(\\): CImg header not found in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":68313,"sourceCode":"                                    \"save_cimg(): Specified filename is (null).\",\n                                    cimglist_instance);\n      if (is_empty())\n        throw CImgInstanceException(_cimglist_instance\n                                    \"save_cimg(): Empty instance, for file '%s'.\",\n                                    cimglist_instance,\n                                    filename?filename:\"(FILE*)\");\n\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb+\");\n      bool saved = 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,str_pixeltype._data,str_endian._data);\n      if (err<2) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimglist_instance\n                              \"save_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      const unsigned int lmax = std::min(N,n0 + _width);\n      _cimg_save_cimg_case(\"bool\",0,0,cimg_uint8);\n      _cimg_save_cimg_case(\"uint8\",\"unsigned_char\",\"uchar\",cimg_uint8);\n      _cimg_save_cimg_case(\"int8\",0,0,cimg_int8);\n      _cimg_save_cimg_case(\"char\",0,0,char);\n      _cimg_save_cimg_case(\"uint16\",\"unsigned_short\",\"ushort\",cimg_uint16);\n      _cimg_save_cimg_case(\"int16\",\"short\",0,cimg_int16);\n      _cimg_save_cimg_case(\"uint32\",\"unsigned_int\",\"uint\",cimg_uint32);\n      _cimg_save_cimg_case(\"int32\",\"int\",0,cimg_int32);\n      _cimg_save_cimg_case(\"uint64\",\"unsigned_int64\",0,cimg_uint64);\n      _cimg_save_cimg_case(\"int64\",0,0,cimg_int64);\n      _cimg_save_cimg_case(\"float\",\"float32\",0,cimg_float32);","sourceCodeStart":68295,"sourceCodeEnd":68331,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L68295-L68331","documentation":"When saving (updating) into an existing .cimg file, save_cimg reads the first line expecting a CImg header of the form '<N>(-<pixeltype>)(-<endianness>)'. If sscanf cannot extract at least the image count and pixel type, the file is not a valid CImg file, so CImgIOException is thrown saying the header was not found.","triggerScenarios":"Pointing save_cimg at a file that is not a .cimg file (wrong extension, text/binary mismatch); a truncated file whose first line is missing; a file written by another tool with a different header format.","commonSituations":"Extension/content mismatch (renamed a raw file to .cimg); crash during a previous write left a zero-length file; cross-version format differences.","solutions":["Verify the target file actually starts with a valid CImg header (e.g. '3(unsigned_char)(little) ...') before updating it.","Recreate the file by saving with a fresh save_cimg call instead of updating an incompatible file.","Check that the previous writer used CImg and that the file is not truncated (non-zero size)."],"exampleFix":"// before\nlist.save_cimg(\"data.bin\"); // not a CImg file\n// after\nstd::FILE *f = std::fopen(\"data.bin\",\"rb\");\nbool ok = f && std::fgetc(f)!='#' ? false : true; // crude header sniff\nif (f) std::fclose(f);\nif (!ok) std::remove(\"data.bin\");\nlist.save_cimg(\"data.bin\");","handlingStrategy":"validation","validationCode":"// verify the file begins with a CImg header before updating\nstd::FILE *f = std::fopen(path, \"rb\");\nbool ok = false;\nif (f) { char h[16] = {0}; ok = std::fread(h,1,15,f) > 0 && h[0] >= '0' && h[0] <= '9'; std::fclose(f); }\nif (!ok) std::remove(path);","typeGuard":null,"tryCatchPattern":"try { list.save_cimg(path); } catch (CImgIOException &e) { std::remove(path); list.save_cimg(path); }","preventionTips":["Only point save_cimg at files previously written by CImg","Check file size > 0 before updating","Keep .cimg files written atomically (write temp, rename)"],"tags":["file-format","corrupt-file","image-io"],"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"}