Yalantis/uCrop · error · CImgIOException
load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %
Error message
load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'
What it means
In the region-of-interest overload CImgList::load_cimg(filename,n0,...,c1), each image's header line must parse as exactly four unsigned dimensions '%u %u %u %u'; otherwise this error is thrown (CImg.h:66902). Same corruption/truncation cause as the non-ROI variant, but no compressed-size field is expected.
Source
Thrown at ucrop/src/main/jni/CImg.h:66902
const unsigned int z1, const unsigned int c1) {
return CImgList<T>().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
}
CImgList<T>& _load_cimg(std::FILE *const file, const char *const filename,
const unsigned int n0, const unsigned int n1,
const unsigned int x0, const unsigned int y0,
const unsigned int z0, const unsigned int c0,
const unsigned int x1, const unsigned int y1,
const unsigned int z1, const unsigned int c1) {
#define _cimg_load_cimg_case2(Ts1,Ts2,Ts3,Tss) \
if (!loaded && ((Ts1 && !cimg::strcasecmp(Ts1,str_pixeltype)) || \
(Ts2 && !cimg::strcasecmp(Ts2,str_pixeltype)) || \
(Ts3 && !cimg::strcasecmp(Ts3,str_pixeltype)))) { \
for (unsigned int l = 0; l<=nn1; ++l) { \
j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0) tmp[j++] = (char)i; tmp[j] = 0; \
W = H = D = C = 0; \
if (cimg_sscanf(tmp,"%u %u %u %u",&W,&H,&D,&C)!=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*)"); \
if (W*H*D*C>0) { \
if (l<nn0 || nx0>=W || ny0>=H || nz0>=D || nc0>=C) cimg::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \
else { \
const unsigned int \
_nx1 = nx1==~0U?W - 1:nx1, \
_ny1 = ny1==~0U?H - 1:ny1, \
_nz1 = nz1==~0U?D - 1:nz1, \
_nc1 = nc1==~0U?C - 1:nc1; \
if (_nx1>=W || _ny1>=H || _nz1>=D || _nc1>=C) \
throw CImgArgumentException(_cimglist_instance \
"load_cimg(): Invalid specified coordinates " \
"[%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) " \
"because image [%u] in file '%s' has size (%u,%u,%u,%u).", \
cimglist_instance, \
n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,l,filename?filename:"(FILE*)",W,H,D,C); \View on GitHub (pinned to f788b534b4)
Solutions
- Re-obtain an intact .cimg file and verify header lines look like 'W H D C'.
- Confirm the file you opened is the CImg-format dataset, not another format.
- Hexdump the first lines and repair or regenerate the header.
Example fix
// before imgs.load_cimg(path, 0,1, 0,63, 0,63, 0,0, 0,2); // throws: truncated header // after if (verifyCimgHeaders(path)) // check each line parses as 4 uints imgs.load_cimg(path, 0,1, 0,63, 0,63, 0,0, 0,2); else reDownloadDataset(path);
Defensive patterns
Strategy: validation
Validate before calling
bool roiHeaderParses(const char* path) {
std::FILE* f = std::fopen(path, "rb"); if (!f) return false;
char line[256] = {0};
bool ok = std::fgets(line, sizeof line, f) != nullptr; // list header
unsigned N, W, H, D, C;
if (ok) std::sscanf(line, "%u", &N);
for (unsigned l = 0; ok && l < N; ++l) {
if (!std::fgets(line, sizeof line, f)) { ok = false; break; }
ok = std::sscanf(line, "%u %u %u %u", &W, &H, &D, &C) == 4;
}
std::fclose(f);
return ok;
} Try / catch
try {
imgs.load_cimg(path, n0,n1, x0,x1, y0,y1, z0,z1, c0,c1);
} catch (cimg_library::CImgIOException& e) {
logError("bad .cimg size header for ROI load: %s", e.what());
reExportDataset(path);
} Prevention
- Verify each per-image header line parses as 4 integers before ROI loads
- Checksum datasets after download
- Avoid opening partially written files (check writer finished marker)
- Keep .cimg handling in binary-safe I/O only
When it happens
Trigger: Calling load_cimg(file, n0,n1, x0,x1, y0,y1, z0,z1, c0,c1) on a .cimg file whose per-image header has missing or non-numeric dimension fields (sscanf != 4).
Common situations: Truncated downloads; concatenated partial .cimg files; wrong file passed while intending another; header lines altered by text-mode transfer.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %
- load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u)
- cimg::mod(): Specified modulo value is 0.
- load_bmp(): Invalid file_size %d specified in filename '%s'
- load_bmp(): Invalid header size %d specified in filename '%s
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/fef904eaca2e3d70.
Report an issue: GitHub.