{"record":{"id":"5e536c44431788d0","repo":"Yalantis/uCrop","slug":"load-cimg-invalid-specified-coordinates-u-u","errorCode":null,"errorMessage":"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).","messagePattern":"load_cimg\\(\\): Invalid specified coordinates \\[%u\\]\\(%u,%u,%u,%u\\) -> \\[%u\\]\\(%u,%u,%u,%u\\) because image \\[%u\\] in file '(.+?)' has size \\(%u,%u,%u,%u\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":66915,"sourceCode":"                      (Ts3 && !cimg::strcasecmp(Ts3,str_pixeltype)))) { \\\n        for (unsigned int l = 0; l<=nn1; ++l) { \\\n          j = 0; while ((i=std::fgetc(nfile))!='\\n' && i>=0) tmp[j++] = (char)i; tmp[j] = 0; \\\n          W = H = D = C = 0; \\\n          if (cimg_sscanf(tmp,\"%u %u %u %u\",&W,&H,&D,&C)!=4) \\\n            throw CImgIOException(_cimglist_instance \\\n                                  \"load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'\", \\\n                                  cimglist_instance, \\\n                                  W,H,D,C,l,filename?filename:\"(FILE*)\"); \\\n          if (W*H*D*C>0) { \\\n            if (l<nn0 || nx0>=W || ny0>=H || nz0>=D || nc0>=C) cimg::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \\\n            else { \\\n              const unsigned int \\\n                _nx1 = nx1==~0U?W - 1:nx1, \\\n                _ny1 = ny1==~0U?H - 1:ny1, \\\n                _nz1 = nz1==~0U?D - 1:nz1, \\\n                _nc1 = nc1==~0U?C - 1:nc1; \\\n              if (_nx1>=W || _ny1>=H || _nz1>=D || _nc1>=C) \\\n                throw CImgArgumentException(_cimglist_instance \\\n                                            \"load_cimg(): Invalid specified coordinates \" \\\n                                            \"[%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) \" \\\n                                            \"because image [%u] in file '%s' has size (%u,%u,%u,%u).\", \\\n                                            cimglist_instance, \\\n                                            n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,l,filename?filename:\"(FILE*)\",W,H,D,C); \\\n              CImg<Tss> raw(1 + _nx1 - nx0); \\\n              CImg<T> &img = _data[l - nn0]; \\\n              img.assign(1 + _nx1 - nx0,1 + _ny1 - ny0,1 + _nz1 - nz0,1 + _nc1 - nc0); \\\n              T *ptrd = img._data; \\\n              ulongT skipvb = nc0*W*H*D*sizeof(Tss); \\\n              if (skipvb) cimg::fseek(nfile,skipvb,SEEK_CUR); \\\n              for (unsigned int c = 1 + _nc1 - nc0; c; --c) { \\\n                const ulongT skipzb = nz0*W*H*sizeof(Tss); \\\n                if (skipzb) cimg::fseek(nfile,skipzb,SEEK_CUR); \\\n                for (unsigned int z = 1 + _nz1 - nz0; z; --z) { \\\n                  const ulongT skipyb = ny0*W*sizeof(Tss); \\\n                  if (skipyb) cimg::fseek(nfile,skipyb,SEEK_CUR); \\\n                  for (unsigned int y = 1 + _ny1 - ny0; y; --y) { \\","sourceCodeStart":66897,"sourceCodeEnd":66933,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L66897-L66933","documentation":"The ROI load_cimg() overload normalizes ~0U ('to the end') bounds to W-1/H-1/D-1/C-1 and throws CImgArgumentException when any upper bound still exceeds the actual image dimensions in the file (CImg.h:66915). The requested coordinate window is outside the stored image.","triggerScenarios":"Calling load_cimg(file, n0,n1, x0,x1, ...) where x1/y1/z1/c1 (explicit or ~0U) is >= the image's W/H/D/C read from the header, e.g. asking for channels 0..3 of a 3-channel image, or hardcoding ~0U for a dimension that is 0.","commonSituations":"Assuming RGBA (4 channels) when data was saved RGB; hardcoded crop sizes larger than the actual volume; images dimension-mismatched after a pipeline change; off-by-one using W instead of W-1 as inclusive max.","solutions":["Read the target image's W,H,D,C from the .cimg header first and clamp x1<=W-1, y1<=H-1, z1<=D-1, c1<=C-1.","Use ~0U only for 'until the end' and never on a dimension that may be 0.","Fix hardcoded ROI constants to match the actual dataset geometry."],"exampleFix":"// before\nimgs.load_cimg(path, 0,0, 0,255, 0,255, 0,0, 0,3); // image has 3 channels\n\n// after\nunsigned W,H,D,C; readCimgHeader(path, 0, W,H,D,C);\nimgs.load_cimg(path, 0,0,\n               0, std::min(255u, W-1),\n               0, std::min(255u, H-1),\n               0,0,\n               0, std::min(3u, C-1));","handlingStrategy":"validation","validationCode":"bool roiInBounds(const char* path, unsigned imgIdx,\n                 unsigned x0,unsigned x1, unsigned y0,unsigned y1,\n                 unsigned z0,unsigned z1, unsigned c0,unsigned c1) {\n  std::FILE* f = std::fopen(path, \"rb\"); if (!f) return false;\n  char line[256] = {0};\n  unsigned N = 0, W = 0, H = 0, D = 0, C = 0;\n  bool ok = std::fgets(line, sizeof line, f) && std::sscanf(line, \"%u\", &N) == 1;\n  for (unsigned l = 0; ok && l <= imgIdx; ++l)\n    ok = std::fgets(line, sizeof line, f) &&\n         std::sscanf(line, \"%u %u %u %u\", &W,&H,&D,&C) == 4;\n  std::fclose(f);\n  if (!ok) return false;\n  return (x1 == ~0U || x1 < W) && (y1 == ~0U || y1 < H) &&\n         (z1 == ~0U || z1 < D) && (c1 == ~0U || c1 < C) &&\n         x0 < W && y0 < H && z0 < D && c0 < C;\n}","typeGuard":null,"tryCatchPattern":"try {\n  imgs.load_cimg(path, n0,n1, x0,x1, y0,y1, z0,z1, c0,c1);\n} catch (cimg_library::CImgArgumentException& e) {\n  logError(\"ROI out of bounds: %s\", e.what());\n  // clamp ROI from actual header dims and retry\n}","preventionTips":["Read W,H,D,C from the header and clamp explicit upper bounds to dim-1","Treat ~0U as 'to the end' only; never on possibly-zero dimensions","Don't assume channel count (RGB vs RGBA) — read it from the header","Keep ROI constants in one config validated against dataset geometry"],"tags":["cimg","cpp","bounds","roi","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}