Yalantis/uCrop · error · CImgIOException
load_cimg(): Unable to load compressed data from file '%s' u
Error message
load_cimg(): Unable to load compressed data from file '%s' unless zlib is enabled.
What it means
In CImgList::load_cimg(), the _cimgz_load_cimg_case macro is compiled to a throwing stub when CImg is built without zlib (cimg_use_zlib undefined). A .cimg.gz (compressed) file declares a csiz/compressed size, and decompression is impossible, so load_cimg throws immediately (CImg.h:66730).
Source
Thrown at ucrop/src/main/jni/CImg.h:66730
cimg::fread(cbuf,(size_t)csiz,nfile); \
if (is_bool) { \
CImg<ucharT> raw(W*H*D*C/8); \
uLongf destlen = (uLongf)raw.size(); \
uncompress((Bytef*)raw._data,&destlen,cbuf,(uLong)csiz); \
img.assign(W,H,D,C); \
img._uchar2bool(raw,raw.size(),false); \
} else { \
CImg<Tss> raw(W,H,D,C); \
uLongf destlen = (uLongf)(raw.size()*sizeof(Tss)); \
uncompress((Bytef*)raw._data,&destlen,cbuf,(uLong)csiz); \
if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
raw.move_to(img); \
} \
delete[] cbuf; \
}
#else
#define _cimgz_load_cimg_case(Tss) \
throw CImgIOException(_cimglist_instance \
"load_cimg(): Unable to load compressed data from file '%s' unless zlib is enabled.", \
cimglist_instance, \
filename?filename:"(FILE*)");
#endif
#define _cimg_load_cimg_case(Ts1,Ts2,Ts3,Tss) \
if (!loaded && ((Ts1 && !cimg::strcasecmp(Ts1,str_pixeltype)) || \
(Ts2 && !cimg::strcasecmp(Ts2,str_pixeltype)) || \
(Ts3 && !cimg::strcasecmp(Ts3,str_pixeltype)))) { \
const bool is_bool = cimg::type<Tss>::string()==cimg::type<bool>::string(); \
for (unsigned int l = 0; l<N; ++l) { \
j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0; \
W = H = D = C = 0; csiz = 0; \
if ((err = cimg_sscanf(tmp,"%u %u %u %u #" cimg_fuint64,&W,&H,&D,&C,&csiz))<4) \
throw CImgIOException(_cimglist_instance \
"load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'.", \
cimglist_instance, \
W,H,D,C,l,filename?filename:("(FILE*)")); \View on GitHub (pinned to f788b534b4)
Solutions
- Rebuild with zlib support: define cimg_use_zlib and link -lz (add CIMG_USE_ZLIB / find_package(ZLIB) in CMake).
- Decompress the file outside CImg (gunzip file.cimg.gz) and load the plain .cimg file instead.
- Re-save the dataset uncompressed (csiz==0) if you control the producer side.
Example fix
// before (CMakeLists.txt) target_link_libraries(native-lib log jnigraphics) // zlib missing // after find_package(ZLIB REQUIRED) target_compile_definitions(native-lib PRIVATE cimg_use_zlib) target_link_libraries(native-lib log jnigraphics ZLIB::ZLIB)
Defensive patterns
Strategy: validation
Validate before calling
bool cimgZlibAvailable() {
#ifdef cimg_use_zlib
return true;
#else
return false;
#endif
}
// before loading: reject .cimg.gz when unsupported
bool isGzipFile(const char* p) {
std::FILE* f = std::fopen(p, "rb"); if (!f) return false;
unsigned char m[2] = {0};
size_t n = std::fread(m, 1, 2, f); std::fclose(f);
return n == 2 && m[0] == 0x1F && m[1] == 0x8B;
} Try / catch
if (!cimgZlibAvailable() && isGzipFile(path)) {
std::string plain = gunzipToTemp(path); // external decompression
imgs.load_cimg(plain.c_str());
} else {
try { imgs.load_cimg(path); }
catch (cimg_library::CImgIOException& e) { logError("load_cimg failed: %s", e.what()); }
} Prevention
- Always build CImg with cimg_use_zlib defined and link -lz
- Add a startup self-check that zlib support is compiled in
- Prefer uncompressed .cimg for environments you don't control
- Detect gzip magic bytes (1F 8B) before calling load_cimg
When it happens
Trigger: Calling CImgList::load_cimg(filename) on a file whose header line contains a nonzero compressed-size field (csiz, i.e. a gzipped .cimg) while the library was compiled without zlib support.
Common situations: Deploying an Android/JNI build of uCrop/CImg where zlib support was not enabled at compile time; opening a .cimg.gz produced on another machine; a build system that silently dropped the -DCIMG_USE_ZLIB define.
Understand the failure class
Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.
Related errors
- save_cimg(): Unable to save compressed data in file '%s' unl
- save_cimg(): Failed to save compressed data for file '%s', s
- get_serialize(): Failed to save compressed data, saving them
- cimg::mod(): Specified modulo value is 0.
- CImgDisplay(): No display available.
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/aaec1874777f0661.
Report an issue: GitHub.