Yalantis/uCrop · error · CImgArgumentException

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

Error message

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

What it means

This is the same invalid-element-size check as error 158 (thrown by the da_insert/da_push family at a slightly different site in the same function). It fires when the element's vector size _dim differs from the dynamic-array image's spectrum, reporting both values.

Source

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

        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));

        if (!dim) // Scalar or vector1() elements
          for (unsigned int k = 0; k<nb_elts; ++k) {
            int index = pos + k;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Make the inserted element's size equal the array spectrum before inserting.
  2. Update the da_init call to create arrays with the dimension you actually insert.
  3. Centralize element construction in the expression so its width is consistent.
  4. Validate _dim against img.spectrum() in host code before evaluating the expression.

Example fix

// before
arr.fill("da_insert(4,[9,9])"); // array spectrum 3
// after
arr.fill("da_insert(4,[9,9,9])");
Defensive patterns

Strategy: validation

Validate before calling

if (element.size() != arr.spectrum())
  throw std::invalid_argument("inserted element size mismatch");

Type guard

bool insertOk(const CImg<T>& elem, const CImg<T>& arr) { return elem.spectrum() == arr.spectrum(); }

Try / catch

try { img.fill(insertExpr); } catch (const cimg_library::CImgArgumentException& e) { /* pad/trim element to spectrum */ }

Prevention

When it happens

Trigger: Calling an insert-at-position MP opcode with an element whose dimension does not equal the array image's spectrum, e.g. inserting [1,2] into an array with spectrum 4.

Common situations: Insert operations in expression loops where element construction size varies; mismatched da_init dimension vs insert payloads; refactored expressions that changed tuple width.

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/a034b653abadaf60. Report an issue: GitHub.