{"record":{"id":"db895f5e620f4ebf","repo":"Yalantis/uCrop","slug":"load-bmp-unable-to-load-compressed-data-from","errorCode":null,"errorMessage":"load_bmp(): Unable to load compressed data from '(*FILE)' inputs.","messagePattern":"load_bmp\\(\\): Unable to load compressed data from '\\(\\*FILE\\)' inputs\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":56856,"sourceCode":"      const int xoffset = offset - 14 - header_size - 4*nb_colors;\n      if (xoffset<0 || xoffset>=file_size)\n        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): Malformed header in filename '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      cimg::fseek(nfile,xoffset,SEEK_CUR);\n\n      CImg<ucharT> buffer;\n      if (buf_size<cimg_iobuffer) {\n        buffer.assign(buf_size,1,1,1,0);\n        cimg::fread(buffer._data,buf_size,nfile);\n      } else buffer.assign(dx_bytes + align_bytes);\n      unsigned char *ptrs = buffer;\n\n      // Decompress buffer (if necessary).\n      if (compression==1 || compression==2) {\n        if (file)\n          throw CImgIOException(_cimg_instance\n                                \"load_bmp(): Unable to load compressed data from '(*FILE)' inputs.\",\n                                cimg_instance);\n        else {\n          if (!file) cimg::fclose(nfile);\n          return load_other(filename);\n        }\n      }\n\n      // Read pixel data.\n      assign(dx,cimg::abs(dy),1,3,0);\n      switch (bpp) {\n      case 1 : { // Monochrome\n        if (colormap._width>=2) for (int y = height() - 1; y>=0; --y) {\n          if (buf_size>=cimg_iobuffer) {\n            if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;\n            cimg::fseek(nfile,align_bytes,SEEK_CUR);\n          }\n          unsigned char mask = 0x80, val = 0;","sourceCodeStart":56838,"sourceCodeEnd":56874,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L56838-L56874","documentation":"BMP RLE compression (types 1 and 2, i.e. 8-bit and 4-bit RLE) cannot be decoded when the image was opened from a C stdio FILE* rather than a filename, because CImg falls back to a generic decoder that needs to reopen the file by path. It throws CImgIOException in this case.","triggerScenarios":"Calling load_bmp(std::FILE*) or load_bmp from an in-memory/FILE handle on an RLE-compressed BMP (biCompression == BI_RLE8 or BI_RLE4).","commonSituations":"Passing a memory-backed or already-opened FILE* (common in JNI/uCrop pipelines) instead of a filename when the BMP is RLE compressed.","solutions":["Pass the filename string instead of a FILE* so CImg can route to load_other()","Re-encode the BMP uncompressed (BI_RGB) before loading","Convert the image to PNG and load that","Read the FILE* into memory and decode with a different library"],"exampleFix":"// before\nstd::FILE* f = fopen(\"rle.bmp\",\"rb\");\nimg.load_bmp(f);\n// after\nimg.load_bmp(\"rle.bmp\"); // path allows RLE fallback path","handlingStrategy":"fallback","validationCode":"unsigned compression = readU32LE(path,30);\nbool isRle = compression == 1 || compression == 2;\nif (isRle) { /* prefer filename-based load or re-encode */ }","typeGuard":"bool isUncompressedBmp(unsigned biCompression) { return biCompression == 0; }","tryCatchPattern":"try { img.load_bmp(fileHandle); }\ncatch (CImgIOException&) { img.load_bmp(\"rle.bmp\"); // path-based fallback\n}\n// or catch (CImgIOException&) { img.assign(); }","preventionTips":["Pass filenames, not FILE*, when RLE BMPs are possible","Re-encode RLE BMPs to uncompressed BI_RGB","Convert to PNG for in-memory/asset loading","Document that FILE*-based loading is limited for compressed formats"],"tags":["bmp","image-loading","compression","cimg"],"backgroundTag":"unsupported-operation","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"}