Yalantis/uCrop · error · CImgArgumentException

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

Error message

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

What it means

Same invalid-arguments check as the unnamed-target variant of 'polygon()', but this branch throws when a specific target image index is given: the message is prefixed with '#<ind>' followed by the offending argument values, telling you which image slot received the malformed polygon arguments.

Source

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

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

      static double mp_pow3(_cimg_math_parser& mp) {
        const double val = _mp_arg(2);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Read the '#ind' and argument list from the message to find the bad values, then fix them in the expression.
  2. Guard coordinate expressions so they never evaluate to NaN (e.g. clamp denominators before dividing).
  3. Match the argument count to the expected polygon format (pairs of coordinates plus optional opacity).
  4. Catch CImgArgumentException around draw expression evaluation to log the offending arguments.

Example fix

// before: polygon(#0,0,X,Y,0/0)
// after:  polygon(#0,0,X,Y,1)
Defensive patterns

Strategy: try-catch

Validate before calling

if (ind < 0 || ind >= (int)images.size()) throw std::runtime_error("bad #ind for polygon");
if (!std::all_of(args.begin(), args.end(), [](double v){ return std::isfinite(v); }))
  throw std::runtime_error("non-finite polygon argument");

Try / catch

try { img.evaluate(expr); } catch (const CImgArgumentException& e) { /* e.what() includes #ind and bad args */ }

Prevention

When it happens

Trigger: polygon(#ind,...) in a math expression where the argument list after the target index is invalid — non-finite coordinates, wrong arity, or values that fail the parser's argument validation (is_invalid_arguments set).

Common situations: Vectorized drawing loops where one iteration produces NaN coordinates; switching draw targets (renumbering #ind) without updating the argument layout; passing an extra or missing opacity argument.

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