Yalantis/uCrop · error · CImgArgumentException

load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u)

Error message

load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) because file '%s' contains only %u images.

What it means

CImgList::load_cimg() reads a sequence of images from a .cimg raw file. Before reading it verifies the requested end index: the caller asked for images up to index nn1 but the file header says the file only contains N images. CImg throws CImgArgumentException to stop the load before reading out of bounds.

Source

Thrown at ucrop/src/main/jni/CImg.h:66988

      CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
      *tmp = *str_pixeltype = *str_endian = 0;
      unsigned int j, N, W, H, D, C;
      int i, err;
      j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;
      err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z123468_]%*c%255[sA-Za-z_ ]",
                        &N,str_pixeltype._data,str_endian._data);
      if (err<2) {
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimglist_instance
                              "load_cimg(): CImg header not found in file '%s'.",
                              cimglist_instance,
                              filename?filename:"(FILE*)");
      }
      if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
      else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
      nn1 = n1==~0U?N - 1:n1;
      if (nn1>=N)
        throw CImgArgumentException(_cimglist_instance
                                    "load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) "
                                    "because file '%s' contains only %u images.",
                                    cimglist_instance,
                                    n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,filename?filename:"(FILE*)",N);
      assign(1 + nn1 - n0);
      _cimg_load_cimg_case2("bool",0,0,cimg_uint8);
      _cimg_load_cimg_case2("uint8","unsigned char","uchar",cimg_uint8);
      _cimg_load_cimg_case2("int8",0,0,cimg_int8);
      _cimg_load_cimg_case2("char",0,0,char);
      _cimg_load_cimg_case2("uint16","unsigned_short","ushort",cimg_uint16);
      _cimg_load_cimg_case2("int16","short",0,cimg_int16);
      _cimg_load_cimg_case2("uint32","unsigned_int","uint",cimg_uint32);
      _cimg_load_cimg_case2("int32","int",0,cimg_int32);
      _cimg_load_cimg_case2("unsigned_long","ulong",0,cimg_ulong);
      _cimg_load_cimg_case2("long",0,0,cimg_long);
      _cimg_load_cimg_case2("uint64","unsigned_int64",0,cimg_uint64);
      _cimg_load_cimg_case2("int64",0,0,cimg_int64);
      _cimg_load_cimg_case2("float32","float",0,cimg_float32);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check the number of images in the .cimg file (its header stores N) and clamp n1 to at most N-1
  2. Use the default n1 = ~0U to load all images instead of a hardcoded upper bound
  3. Regenerate or re-export the .cimg file so it actually contains the images you request
  4. Verify you are loading the intended file (path/logged N) - a wrong or truncated file is common

Example fix

// before
imgs.load_cimg("seq.cimg", 0,0,0,0,0, 99,0,0,0,0); // assumes 100 images
// after
const unsigned int n1 = std::min(99u, countImagesInCimg("seq.cimg") - 1);
imgs.load_cimg("seq.cimg", 0,0,0,0,0, n1,0,0,0,0);
Defensive patterns

Strategy: validation

Validate before calling

unsigned int nImages = readCimgHeaderImageCount(path); // parse N from header
if (n1 != ~0U && n1 >= nImages) n1 = nImages - 1;

Try / catch

try { imgs.load_cimg(path, ...); }
catch (CImgArgumentException& e) { fprintf(stderr, "cimg range exceeds file: %s\n", e.what()); imgs.assign(); }

Prevention

When it happens

Trigger: Calling load_cimg(filename, n0,x0,y0,z0,c0, n1,x1,y1,z1,c1) where n1 (default ~0U meaning 'last image') resolves to nn1 >= N, the actual number of images stored in the file per its header.

Common situations: Hardcoded image-index ranges that assume a larger .cimg file; the file was truncated, regenerated with fewer images, or written by a different version; passing explicit n1 values copied from another dataset; off-by-one when N images means valid indices 0..N-1.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/ccf860884f56b0fe. Report an issue: GitHub.