Yalantis/uCrop · error · CImgArgumentException

draw_triangle(): Specified color is (null).

Error message

draw_triangle(): Specified color is (null).

What it means

Argument guard in CImg<T>::draw_triangle(): the 'color' parameter is a null pointer, but the renderer needs spectrum() consecutive color values of type T to fill the triangle, so there is no color to draw with.

Solutions

  1. Pass a valid color array sized >= image.spectrum().
  2. Guard with if (color) before the draw call.
  3. Default to a fixed color when the requested one is unavailable.
  4. Fix the upstream color producer to never return null on expected paths.

Example fix

// before
img.draw_triangle(0,0, 50,10, 25,60, matColor, 1.0f); // matColor==nullptr
// after
unsigned char fallback[3] = {128,128,128};
img.draw_triangle(0,0, 50,10, 25,60, matColor?matColor:fallback, 1.0f);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!color) {
  static const unsigned char red[3] = {255,0,0};
  color = red;
}
img.draw_triangle(x0,y0, x1,y1, x2,y2, color, opacity);

Type guard

template<typename T> bool nonNullColor(const T* c) { return c != nullptr; }

Try / catch

try {
  img.draw_triangle(x0,y0, x1,y1, x2,y2, color, opacity);
} catch (const cimg_library::CImgArgumentException& e) {
  std::fprintf(stderr, "draw_triangle: %s\n", e.what());
}

Prevention

When it happens

Trigger: Calling draw_triangle with color==nullptr, typically from an uninitialized pointer or a null-returning color lookup.

Common situations: Mesh rendering helpers where material color failed to load; templated drawing code forwarding optional tc* colors.

Related errors


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

Appendix: source

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

    //! Draw a filled 2D triangle.
    /**
       \param x0 X-coordinate of the first vertex.
       \param y0 Y-coordinate of the first vertex.
       \param x1 X-coordinate of the second vertex.
       \param y1 Y-coordinate of the second vertex.
       \param x2 X-coordinate of the third vertex.
       \param y2 Y-coordinate of the third vertex.
       \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
       \param opacity Drawing opacity.
     **/
    template<typename tc>
    CImg<T>& draw_triangle(const int x0, const int y0,
                           const int x1, const int y1,
                           const int x2, const int y2,
                           const tc *const color, const float opacity=1) {
      if (is_empty()) return *this;
      if (!color)
        throw CImgArgumentException(_cimg_instance
                                    "draw_triangle(): Specified color is (null).",
                                    cimg_instance);
      _draw_triangle(x0,y0,x1,y1,x2,y2,color,opacity,1);
      return *this;
    }

    //! Draw a outlined 2D triangle.
    /**
       \param x0 X-coordinate of the first vertex.
       \param y0 Y-coordinate of the first vertex.
       \param x1 X-coordinate of the second vertex.
       \param y1 Y-coordinate of the second vertex.
       \param x2 X-coordinate of the third vertex.
       \param y2 Y-coordinate of the third vertex.
       \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
       \param opacity Drawing opacity.
       \param pattern An integer whose bits describe the outline pattern.
     **/

View on GitHub (pinned to f788b534b4)