Yalantis/uCrop · error · CImgArgumentException

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

Error message

"[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': Specified image #%u of size (%d,%d,%d,%d) cannot be used as dynamic array%s."

What it means

CImg's math parser function (a dynamic-array operation like da_insert) requires the referenced image list entry to be a valid dynamic array: a single-column, single-depth image whose last row stores the element count. This error is thrown when the image #ind has wrong dimensions or a corrupt element counter, so it cannot be interpreted as a dynamic array. The suffix ' (contains invalid element counter)' is appended when width/depth are OK but the stored counter is out of range.

Source

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

          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;
            img[index] = (T)_mp_arg(6 + k);
            if (is_push_heap) while (index>0) { // Heapify-up
                const int index_parent = (index - 1)/2;
                if (img[index]<img[index_parent]) {
                  cimg::swap(img[index],img[index_parent]);
                  index = index_parent; }

View on GitHub (pinned to f788b534b4)

Solutions

  1. Verify the image index used in the expression points to an image actually created by dynamic-array functions (da_insert/da_remove/...).
  2. Ensure the image has dimensions (1,N,1,C): recreate it via da_* functions if it was resized or overwritten elsewhere.
  3. Check the last row of the image holds a valid element counter in 0..height-1; reset the array by re-initializing with da_size/da_insert.
  4. Print the image dimensions in the expression (e.g. size(#ind)) to confirm shape before the failing call.

Example fix

// before: using a plain image as dynamic array
foo: fill 0 resize 16,1,1,1 da_insert(#0,0,42)
// after: initialize via dynamic-array API
foo: da_insert(#0,0,42)
Defensive patterns

Strategy: validation

Validate before calling

// Check the image is a valid dynamic array before da_* calls
function isValidDa(img) { return img && img.width === 1 && img.depth === 1 && img.counter >= 0 && img.counter <= img.height - 1; }
if (!isValidDa(images[ind])) throw new Error('image ' + ind + ' is not a dynamic array');

Type guard

function isDynamicArray(img) { return Boolean(img) && img.width === 1 && img.depth === 1 && Number.isInteger(img.height) && img.lastRowCounter >= 0 && img.lastRowCounter <= img.height - 1; }

Try / catch

try { result = evalMath(expr); } catch (e) { if (String(e).includes('cannot be used as dynamic array')) { rebuildArray(ind); } else throw e; }

Prevention

When it happens

Trigger: Calling a dynamic-array math function (e.g. da_insert) with an image index pointing to an image that is not 1xNx1xC, or whose last stored value is negative or larger than height-1 (e.g. the image was created/modified without using da_* functions).

Common situations: Using plain fill/copy/resize operations on an image that da_* functions previously managed, destroying the element-counter row; passing the wrong image index into the math expression; reusing images from incompatible code paths in G'MIC-style scripts.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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