Yalantis/uCrop · error · CImgArgumentException

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

Error message

"[" cimg_appname "_math_parser] CImg<%s>: Function 'polygon()': Invalid arguments '%s'. "

What it means

The math parser 'polygon()' draw command validates its argument list (the coordinates/points passed after the drawing parameters). When the arguments don't match the expected format, it collects the parsed argument values and throws a CImgArgumentException listing the invalid arguments. This variant fires when no target image index (ind==~0U) was specified.

Source

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

              if (i<i_end) opacity = (float)_mp_arg(i++);
              if (is_outlined && i<i_end) {
                double d_pattern = _mp_arg(i++);
                if (d_pattern<0) { d_pattern = -d_pattern; is_closed = false; }
                pattern = (unsigned int)d_pattern;
              }
              cimg_forX(color,k) if (i<i_end) color[k] = (T)_mp_arg(i++);
              else { color.resize(k,1,1,1,-1); break; }
              color.resize(img._spectrum,1,1,1,0,2);
              if (is_outlined) img.draw_polygon(points,color._data,opacity,pattern,is_closed);
              else img.draw_polygon(points,color._data,opacity);
            }
          }
        }
        if (is_invalid_arguments) {
          CImg<doubleT> args(i_end - 4);
          cimg_forX(args,k) args[k] = _mp_arg(4 + k);
          if (ind==~0U)
            throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'polygon()': "
                                        "Invalid arguments '%s'. ",
                                        mp.imgin.pixel_type(),args.value_string()._data);
          else
            throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'polygon()': "
                                        "Invalid arguments '#%u%s%s'. ",
                                        mp.imgin.pixel_type(),ind,args._width?",":"",args.value_string()._data);
        }
        return cimg::type<double>::nan();
      }

      static double mp_pow(_cimg_math_parser& mp) {
        const double v = _mp_arg(2), p = _mp_arg(3);
        return std::pow(v,p);
      }

      static double mp_pow0_25(_cimg_math_parser& mp) {
        const double val = _mp_arg(2);
        return std::sqrt(std::sqrt(val));

View on GitHub (pinned to f788b534b4)

Solutions

  1. Inspect the argument string in the error message and correct the malformed polygon arguments.
  2. Ensure coordinates are finite numbers (no NaN/Inf from divisions by zero upstream).
  3. Supply coordinate pairs (x,y[,z]) so the total argument count matches the expected geometry.
  4. If targeting a specific image, use the '#ind' form so the correct error variant and validation path applies.

Example fix

// before: polygon(-1,0,X,Y,Z, 0,0,0)   // wrong arg count/NaN
// after:  polygon(-1,0,X0,Y0,X1,Y1,opacity)
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure all coordinates finite and even count before building the expression
bool ok = !coords.empty() && coords.size() % 2 == 0 &&
          std::all_of(coords.begin(), coords.end(),
                      [](double v){ return std::isfinite(v); });

Type guard

auto isFiniteArgs = [](const std::vector<double>& a) {
  return std::all_of(a.begin(), a.end(), [](double v){ return std::isfinite(v); });
};

Try / catch

try { result = img.evaluate(drawExpr); } catch (const CImgArgumentException& e) { log("polygon args rejected: " << e.what()); }

Prevention

When it happens

Trigger: An expression like polygon(..., badArgs) where the argument tuple cannot be interpreted as a valid point sequence — wrong number of coordinates, non-numeric values, or the arguments resolved to NaN, causing is_invalid_arguments to be set.

Common situations: Building polygon point lists programmatically with a bug producing NaN; mismatched point counts (odd number of coordinate values when pairs are expected); passing image-index-targeted syntax incorrectly.

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