Yalantis/uCrop · error · CImgArgumentException

draw_line(): Specified color is (null).

Error message

draw_line(): Specified color is (null).

What it means

This draw_line() overload renders a 3D line into an image with a Z-buffer and requires a color array whose length is at least the image's spectrum (e.g. 3 floats for RGB). If the color pointer is null the library cannot fill the drawn pixels and throws CImgArgumentException immediately after the early-return guards (empty image, z<=0, zero opacity/pattern).

Source

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

       \param y0 Y-coordinate of the starting point.
       \param z0 Z-coordinate of the starting point
       \param x1 X-coordinate of the ending point.
       \param y1 Y-coordinate of the ending point.
       \param z1 Z-coordinate of the ending point.
       \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 line pattern.
       \param init_hatch Tells if a reinitialization of the hash state must be done.
    **/
    template<typename tz, typename tc>
    CImg<T>& draw_line(CImg<tz>& zbuffer,
                       int x0, int y0, const float z0,
                       int x1, int y1, const float z1,
                       const tc *const color, const float opacity=1,
                       const unsigned int pattern=~0U, const bool init_hatch=true) {
      if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;
      if (!color)
        throw CImgArgumentException(_cimg_instance
                                    "draw_line(): Specified color is (null).",
                                    cimg_instance);
      if (!is_sameXY(zbuffer))
        throw CImgArgumentException(_cimg_instance
                                    "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
                                    "different dimensions.",
                                    cimg_instance,
                                    zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);

      if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0)
        return *this;
      int
        w1 = width() - 1, h1 = height() - 1,
        dx01 = x1 - x0, dy01 = y1 - y0;
      float iz0 = 1/z0, iz1 = 1/z1, diz01 = iz1 - iz0;

      const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
      if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Pass a valid color array sized to the image spectrum, e.g. `const unsigned char red[] = {255,0,0};` for RGB images.
  2. If color may legitimately be absent, decide the call: skip draw_line or substitute a default color instead of nullptr.
  3. Check the code isn't accidentally calling the texture-based draw_line signature where the 4th/5th ints are texture coordinates, not color.

Example fix

// before
img.draw_line(0,0,1,100,100,1,nullptr,1.0f);

// after
const unsigned char white[] = {255,255,255};
img.draw_line(0,0,1,100,100,1,white,1.0f);
Defensive patterns

Strategy: validation

Validate before calling

const unsigned char col[] = {255,255,255};
if (!color) return; // or substitute a default color before calling
img.draw_line(x0,y0,z0,x1,y1,z1,col,opacity);

Type guard

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

Try / catch

try {
  img.draw_line(x0,y0,z0,x1,y1,z1,color,opacity);
} catch (CImgArgumentException& e) {
  std::fprintf(stderr, "draw_line failed: %s\n", e.what());
}

Prevention

When it happens

Trigger: Calling img.draw_line(x0,y0,z0,x1,y1,z1,NULL,opacity) — i.e. passing nullptr/0 as the color argument to the Z-buffered line overload — on a non-empty image with positive z0/z1, non-zero opacity and pattern, since otherwise the function silently returns before the color check.

Common situations: Passing a null pointer returned from a failed allocation or lookup of a palette entry; a template deduction mismatch that makes the color argument nullptr; porting code from a texture-based draw_line overload (which takes no color) to the Z-buffered one and leaving the color argument empty; conditional color selection defaulting to null.

Related errors


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