Yalantis/uCrop · error · CImgArgumentException

[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': El

Error message

[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': Element to insert has invalid size %u (should be %u).

What it means

Dynamic-array insert functions (da_push/da_insert family) require the inserted element to have the same vector dimension as the array's spectrum (_dim == img._spectrum). When the pushed element's size differs from the array's channel count, this error reports both the actual and expected sizes.

Source

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

      }

      static double mp_da_insert_or_push(_cimg_math_parser& mp) {
        const bool is_push_heap = mp.opcode[3]==~0U - 1, is_push = mp.opcode[3]>=~0U - 1;
        const char *const s_op = is_push_heap?"da_push_heap":is_push?"da_push":"da_insert";
        mp_check_list(mp,s_op);
        const unsigned int
          dim = (unsigned int)mp.opcode[4],
          _dim = std::max(1U,dim),
          nb_elts = (unsigned int)mp.opcode[5] - 6,
          ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
        CImg<T> &img = mp.imglist[ind];
        const int
          siz = img?(int)cimg::float2uint((float)img[img._height - 1]):0,
          pos0 = is_push?siz:(int)_mp_arg(3),
          pos = pos0<0?pos0 + siz:pos0;

        if (img && _dim!=img._spectrum)
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
                                      "Element to insert has invalid size %u (should be %u).",
                                      mp.imgout.pixel_type(),s_op,_dim,img._spectrum);
        if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
                                      "Specified image #%u of size (%d,%d,%d,%d) cannot be used as dynamic array%s.",
                                      mp.imgout.pixel_type(),s_op,ind,
                                      img.width(),img.height(),img.depth(),img.spectrum(),
                                      img._width==1 && img._depth==1?"":" (contains invalid element counter)");
        if (pos<0 || pos>siz)
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
                                      "Invalid position %d (not in range -%d...%d).",
                                      mp.imgout.pixel_type(),s_op,pos0,siz,siz);

        if (siz + nb_elts + 1>=img._height) // Increase size of dynamic array, if necessary
          img.resize(1,2*siz + nb_elts + 1,1,_dim,0);

        if (pos!=siz) // Move existing data in dynamic array
          cimg_forC(img,c) std::memmove(img.data(0,pos + nb_elts,0,c),img.data(0,pos,0,c),(siz - pos)*sizeof(T));

View on GitHub (pinned to f788b534b4)

Solutions

  1. Match the element size to the array's spectrum: resize the element or recreate the array with the correct dim.
  2. Verify the dim argument passed to the push/insert opcode equals the array's channel count.
  3. Re-create the array with da_init(h,correctDim) if the element size legitimately changed.
  4. Audit all push sites in your expression to use a single consistent dim.

Example fix

// before
arr.fill("da_init(10,3)"); arr.fill("da_push([1,2])"); // dim 2 != 3
// after
arr.fill("da_init(10,3)"); arr.fill("da_push([1,2,3])");
Defensive patterns

Strategy: validation

Validate before calling

if (elemDim != arr.spectrum())
  throw std::invalid_argument("element size must match array spectrum");

Type guard

bool dimsMatch(unsigned elemDim, const CImg<T>& arr) { return elemDim == arr.spectrum(); }

Try / catch

try { img.fill(pushExpr); } catch (const cimg_library::CImgArgumentException& e) { /* align dim with spectrum */ }

Prevention

When it happens

Trigger: Pushing a 2-element vector into a dynamic array created with dimension 3, e.g. mismatched dim argument vs the array image's spectrum.

Common situations: Changing the tuple size mid-algorithm; copy-pasted push calls with stale dim values; arrays created with da_init(h,dim1) but pushed with dim2-element values.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


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