Yalantis/uCrop · error · CImgArgumentException

draw_polygon(): Invalid specified point set (%u,%u,%u,%u).

Error message

draw_polygon(): Invalid specified point set (%u,%u,%u,%u).

What it means

CImg's draw_polygon() expects the points image to be a 2xN matrix: height must be exactly 2 (row 0 = x coordinates, row 1 = y coordinates). This error means points.height()!=2, so the point set has an invalid layout and CImg throws CImgArgumentException.

Source

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

        draw_line(nx0,ny1 - 1,nx0,ny0 + 1,color,opacity,pattern,false);
    }

    //! Draw a filled 2D polygon.
    /**
       \param points Set of polygon vertices.
       \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
       \param opacity Drawing opacity.
     **/
    template<typename tp, typename tc>
    CImg<T>& draw_polygon(const CImg<tp>& points,
                          const tc *const color, const float opacity=1) {
      if (is_empty() || !points) return *this;
      if (!color)
        throw CImgArgumentException(_cimg_instance
                                    "draw_polygon(): Specified color is (null).",
                                    cimg_instance);
      if (points.height()!=2)
        throw CImgArgumentException(_cimg_instance
                                    "draw_polygon(): Invalid specified point set (%u,%u,%u,%u).",
                                    cimg_instance,
                                    points._width,points._height,points._depth,points._spectrum);
      CImg<intT> ipoints;
      if (cimg::type<tp>::is_float()) ipoints = points.get_round();
      else ipoints.assign(points,cimg::type<tp>::string()==cimg::type<int>::string());

      if (ipoints._width==1) return draw_point(ipoints(0,0),ipoints(0,1),color,opacity);
      if (ipoints._width==2) return draw_line(ipoints(0,0),ipoints(0,1),ipoints(1,0),ipoints(1,1),color,opacity);
      if (ipoints._width==3) return draw_triangle(ipoints(0,0),ipoints(0,1),ipoints(1,0),ipoints(1,1),
                                                  ipoints(2,0),ipoints(2,1),color,opacity);
      cimg_init_scanline(opacity);
      int
        xmin = 0, ymin = 0,
        xmax = ipoints.get_shared_row(0).max_min(xmin),
        ymax = ipoints.get_shared_row(1).max_min(ymin);
      if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this;
      if (ymin==ymax) return draw_line(xmin,ymin,xmax,ymax,color,opacity);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Reshape points to 2xN: build a CImg(2,N) with x in row 0 and y in row 1 (e.g. points.get_transpose() if you have Nx2).
  2. If points are 3D, drop the z row: keep only x,y rows.
  3. Validate points.height()==2 && points.width()>=3 before calling.

Example fix

// before
CImg<int> pts(2,4); // built with one vertex per column-slot but wrong axes; height=4
img.draw_polygon(pts, color, 1.0f); // throws: height != 2
// after
CImg<int> pts_ok(2,4); // row 0 = x coords, row 1 = y coords
pts_ok(0,0)=x0; pts_ok(1,0)=y0; /* ... */
img.draw_polygon(pts_ok, color, 1.0f);
Defensive patterns

Strategy: validation

Validate before calling

if (points.height() != 2)
  throw std::invalid_argument("draw_polygon expects a 2xN point image (row 0 = x, row 1 = y)");
img.draw_polygon(points, color, opacity);

Type guard

bool valid_point_set = (points.height() == 2) && (points.width() >= 3);

Try / catch

try {
  img.draw_polygon(points, color, opacity);
} catch (CImgArgumentException& e) {
  std::cerr << "Invalid point set: " << e.what() << std::endl;
}

Prevention

When it happens

Trigger: Calling img.draw_polygon(points, color, opacity) where points is a CImg with height() other than 2 — e.g. an Nx2 matrix stored transposed, a 3xN (x,y,z) set, or a scalar/list image.

Common situations: Storing polygon vertices as one-point-per-row (Nx2) instead of CImg's 2xN convention; passing 3D coordinates including z; reusing a point cloud image; transposing errors from get_transpose().

Related errors


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