Yalantis/uCrop · error · CImgArgumentException

[" cimg_appname "_math_parser] CImg<%s>::%s: %s%s %s%s (of t

Error message

[" cimg_appname "_math_parser] CImg<%s>::%s: %s%s %s%s (of type '%s') is not a constant, in expression '%s'.

What it means

The CImg math parser includes constructs (e.g. certain function arguments and compile-time values like image indices or sizes) that must be compile-time constants. check_constant() calls _cimg_mp_check_type and then rejects any argument for which is_const_scalar() is false, throwing CImgArgumentException that names the operator/function, argument position, and the actual type found.

Source

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

      }

      // Return size of specified value (0: scalar, N>0: vectorN).
      unsigned int size(const unsigned int arg) const {
        return is_scalar(arg)?0U:memtype[arg] - 1U;
      }

      // Check if a memory slot is a positive integer constant scalar value.
      // 'mode' can be:
      // { 0=constant | 1=integer constant | 2=positive integer constant | 3=strictly-positive integer constant }.
      void check_const_scalar(const unsigned int arg, const unsigned int n_arg,
                              const unsigned int mode,
                              char *const ss, char *const se, const char saved_char) {
        _cimg_mp_check_type(arg,n_arg,1,0);
        if (!is_const_scalar(arg)) {
          const char *const s_arg = s_argth(n_arg);
          char *s0;
          _cimg_mp_strerr;
          throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                      "CImg<%s>::%s: %s%s %s%s (of type '%s') is not a constant, "
                                      "in expression '%s'.",
                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
                                      s_arg,*s_arg?" argument":" Argument",s_type(arg)._data,s0);
        }
        const double val = mem[arg];

        if (!((!mode || (double)(int)mem[arg]==mem[arg]) &&
              (mode<2 || mem[arg]>=(mode==3)))) {
          const char *const s_arg = s_argth(n_arg);
          char *s0;
          _cimg_mp_strerr;
          throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                      "CImg<%s>::%s: %s%s %s%s (of type '%s' and value %g) is not a%s constant, "
                                      "in expression '%s'.",
                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
                                      s_arg,*s_arg?" argument":" Argument",s_type(arg)._data,val,
                                      !mode?"":mode==1?"n integer":

View on GitHub (pinned to f788b534b4)

Solutions

  1. Replace the non-constant argument with a literal numeric constant
  2. If the value depends on runtime data, compute it outside the expression (in C++ code) and inject it into the expression string as a formatted literal
  3. Check the function signature in CImg.h to confirm which arguments must be constant scalars
  4. Use is_const_scalar-compatible values: numeric literals or const-foldable subexpressions only

Example fix

// before
double f = get_factor();
img.fill("resize({f},2)"); // 'f' not usable as constant in this position
// after
double f = get_factor();
char expr[64];
snprintf(expr, sizeof(expr), "resize(%g,2)", f); // inject literal
img.fill(expr);
Defensive patterns

Strategy: validation

Validate before calling

// C++: ensure arguments that must be constants are formatted literals
std::string make_expr(double required_const) {
  char buf[64];
  std::snprintf(buf, sizeof(buf), "op(%g,2)", required_const);
  return std::string(buf); // value embedded as literal, not a runtime symbol
}

Try / catch

try {
  img.fill(expr.c_str());
} catch (const cimg_library::CImgArgumentException& e) {
  std::cerr << "Non-constant argument in expression: " << e.what() << std::endl;
}

Prevention

When it happens

Trigger: Passing a runtime-varying expression (a vector, a reference to image pixel data, or a computed non-scalar) where a constant scalar is required, e.g. using a variable or pixel value where a literal number is expected, such as an argument that must be fixed at compile time (resize factor, axis, index literal).

Common situations: Using image-dependent values (like I[x]) or user variables in positions that must be literal constants; porting scripts where a parameter was a literal in an older version but the call site now computes it; misunderstanding which arguments of a function are compile-time-only.

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