Yalantis/uCrop · error · CImgArgumentException

[cimg_appname_math_parser] CImg<%s>::%s: %s: Type of first a

Error message

[cimg_appname_math_parser] CImg<%s>::%s: %s: Type of first arguments ('%s') does not match with third argument 'nb_channelsX=%u', in expression '%s'.

What it means

Per-pixel/channel operations in the parser that take both data vectors and channel counts check that the first argument's size p1 is divisible by nb_channelsX (arg3). If p1 % arg3 != 0, CImg throws this error naming the first argument's type and the channel count, because the data cannot be grouped into complete channel tuples.

Source

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

                arg3 = compile(s2,s1,depth1,0,block_flags);
                if (s1<se1) {
                  s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
                  arg4 = compile(s1,s2,depth1,0,block_flags);
                  arg5 = s2<se1?compile(++s2,se1,depth1,0,block_flags):0;
                }
              }
              _cimg_mp_check_type(arg1,1,2,0);
              _cimg_mp_check_type(arg2,2,2,0);
              _cimg_mp_check_const_scalar(arg3,3,3);
              _cimg_mp_check_const_scalar(arg4,4,3);
              _cimg_mp_check_type(arg5,5,1,0);
              p1 = size(arg1);
              p2 = size(arg2);
              arg3 = (unsigned int)mem[arg3];
              arg4 = (unsigned int)mem[arg4];
              if (p1%arg3) {
                _cimg_mp_strerr;
                throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                            "CImg<%s>::%s: %s: Type of first arguments ('%s') "
                                            "does not match with third argument 'nb_channelsX=%u', "
                                            "in expression '%s'.",
                                            pixel_type(),_cimg_mp_calling_function,s_op,
                                            s_type(arg1)._data,arg3,s0);
              }
              if (p2%arg4) {
                _cimg_mp_strerr;
                throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                            "CImg<%s>::%s: %s: Type of second arguments ('%s') "
                                            "does not match with fourth argument 'nb_channelsP=%u', "
                                            "in expression '%s'.",
                                            pixel_type(),_cimg_mp_calling_function,s_op,
                                            s_type(arg2)._data,arg4,s0);
              }
              pos = vector(p1*arg4);
              CImg<ulongT>::vector((ulongT)mp_map,pos,arg1,arg2,p1,p2,arg3,arg4,arg5).move_to(code);
              return_comp = true;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Ensure input X data length is an exact multiple of nb_channelsX before the call
  2. Pass the channel count matching your actual data layout (1 for gray, 3 for RGB, 4 for RGBA)
  3. Pad or truncate the input vector to a multiple of the channel count
  4. Derive nb_channelsX from the source image's spectrum() instead of hard-coding

Example fix

// before
img.fill("convert([v0,...,v7], 3, ...)"); // 8 not divisible by 3
// after
img.fill("convert([v0,...,v8], 3, ...)"); // 9 divisible by 3
Defensive patterns

Strategy: validation

Validate before calling

void checkChannelsX(double[] x, int nbChannelsX) {
  if (x.length % nbChannelsX != 0)
    throw new IllegalArgumentException("Input X size " + x.length + " not a multiple of nb_channelsX=" + nbChannelsX);
}

Try / catch

try { img.fill(expr); } catch (CImgArgumentException e) { if (e.getMessage().contains("nb_channelsX")) { /* fix channel count or input length */ } throw e; }

Prevention

When it happens

Trigger: Calling a multi-channel expression function (e.g. color-space conversion taking nb_channelsX) where the first input vector's length is not a multiple of the specified X channel count, e.g. 8 values with nb_channelsX=3.

Common situations: Converting between RGB and XYZ/Lab with stale channel counts; feeding grayscale data (1 channel) while specifying 3; truncated or padded buffers from earlier processing steps.

Related errors


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