{"record":{"id":"4791826ed6f2e6aa","repo":"Yalantis/uCrop","slug":"load-bmp-invalid-bmp-file-s","errorCode":null,"errorMessage":"load_bmp(): Invalid BMP file '%s'.","messagePattern":"load_bmp\\(\\): Invalid BMP file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":56784,"sourceCode":"\n    //! Load image from a BMP file \\newinstance.\n    static CImg<T> get_load_bmp(std::FILE *const file) {\n      return CImg<T>().load_bmp(file);\n    }\n\n    CImg<T>& _load_bmp(std::FILE *const file, const char *const filename) {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"load_bmp(): Specified filename is (null).\",\n                                    cimg_instance);\n\n      const ulongT fsiz = (ulongT)(file?cimg::fsize(file):cimg::fsize(filename));\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb\");\n      CImg<ucharT> header(54);\n      cimg::fread(header._data,54,nfile);\n      if (*header!='B' || header[1]!='M') {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): Invalid BMP file '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      }\n\n      // Read header and pixel buffer.\n      int\n        file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24),\n        offset = header[0x0A] + (header[0x0B]<<8) + (header[0x0C]<<16) + (header[0x0D]<<24),\n        header_size = header[0x0E] + (header[0x0F]<<8) + (header[0x10]<<16) + (header[0x11]<<24),\n        dx = header[0x12] + (header[0x13]<<8) + (header[0x14]<<16) + (header[0x15]<<24),\n        dy = header[0x16] + (header[0x17]<<8) + (header[0x18]<<16) + (header[0x19]<<24),\n        compression = header[0x1E] + (header[0x1F]<<8) + (header[0x20]<<16) + (header[0x21]<<24),\n        nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24),\n        bpp = header[0x1C] + (header[0x1D]<<8);\n\n      if ((ulongT)file_size!=fsiz)\n        throw CImgIOException(_cimg_instance","sourceCodeStart":56766,"sourceCodeEnd":56802,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L56766-L56802","documentation":"load_bmp() reads the first 54 bytes and checks the 'BM' magic; a mismatch throws CImgIOException because the file is not a BMP. This fires before any header parsing, so it is purely a signature check.","triggerScenarios":"img.load_bmp(path) on a file whose first two bytes are not 'B','M' — e.g. a PNG/JPEG renamed to .bmp, a corrupted download, or a file starting with a BOM.","commonSituations":"Relying on file extension instead of actual content; build scripts copying wrong assets; partial writes that truncated the header.","solutions":["Verify the file's first two bytes are 'BM' before calling load_bmp.","Use img.load(path) to auto-detect the real format instead of forcing the BMP loader.","Re-export or re-download the image if it is corrupt.","Decode with a tolerant loader (e.g. BitmapFactory) and convert to BMP if BMP input is required."],"exampleFix":"// before\nimg.load_bmp(path);\n// after\nstd::ifstream f(path, std::ios::binary); char m[2]; f.read(m,2);\nif (m[0]=='B' && m[1]=='M') img.load_bmp(path);\nelse img.load(path); // auto-detect","handlingStrategy":"validation","validationCode":"bool isBmpMagic(const char* p){ std::ifstream f(p,std::ios::binary); char m[2]={0}; f.read(m,2); return m[0]=='B'&&m[1]=='M'; }","typeGuard":null,"tryCatchPattern":"try { img.load_bmp(path); } catch (cimg_library::CImgIOException& e) { /* not a BMP: fall back to auto-detect */ img.load(path); }","preventionTips":["Validate 'BM' magic bytes before calling load_bmp","Never trust file extensions; check content","Use img.load() auto-detection when format is uncertain"],"tags":["cimg","bmp","file-format","magic-number"],"backgroundTag":"invalid-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"}