Yalantis/uCrop · warning

operator(): Invalid pixel request, at coordinates (%d,%d,%d,

Error message

operator(): Invalid pixel request, at coordinates (%d,%d,%d,%d) [offset=%u].

What it means

CImg<T>::operator()(x,y,z,c) (debug build, cimg_verbosity>=3) validates the computed offset against the image size before returning the pixel reference. If the image is empty (_data==0) or the coordinates fall outside [width,height,depth,spectrum], it warns non-fatally and returns *_data — on an empty image this dereferences a null pointer, so the warning flags a bug in the calling code rather than recovering safely.

Source

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

         checking operations in this operator. In that case, warning messages will be printed on the error output
         when accessing out-of-bounds pixels.
       \par Example
       \code
       CImg<float> img(100,100,1,3,0); // Construct a 100x100x1x3 (color) image with pixels set to '0'
       const float
          valR = img(10,10,0,0), // Read red value at coordinates (10,10)
          valG = img(10,10,0,1), // Read green value at coordinates (10,10)
          valB = img(10,10,2), // Read blue value at coordinates (10,10) (Z-coordinate can be omitted)
          avg = (valR + valG + valB)/3; // Compute average pixel value
       img(10,10,0) = img(10,10,1) = img(10,10,2) = avg; // Replace the color pixel (10,10) by the average grey value
       \endcode
    **/
#if cimg_verbosity>=3
    T& operator()(const unsigned int x, const unsigned int y=0,
                  const unsigned int z=0, const unsigned int c=0) {
      const ulongT off = (ulongT)offset(x,y,z,c);
      if (!_data || off>=size()) {
        cimg::warn(_cimg_instance
                   "operator(): Invalid pixel request, at coordinates (%d,%d,%d,%d) [offset=%u].",
                   cimg_instance,
                   (int)x,(int)y,(int)z,(int)c,off);
        return *_data;
      }
      else return _data[off];
    }

    //! Access to a pixel value \const.
    const T& operator()(const unsigned int x, const unsigned int y=0,
                        const unsigned int z=0, const unsigned int c=0) const {
      return const_cast<CImg<T>*>(this)->operator()(x,y,z,c);
    }

    //! Access to a pixel value.
    /**
       \param x X-coordinate of the pixel value.
       \param y Y-coordinate of the pixel value.

View on GitHub (pinned to f788b534b4)

Solutions

  1. Fix the loop bounds to use x<img.width(), y<img.height(), z<img.depth(), c<img.spectrum()
  2. Check img.is_empty() (or !img) before pixel access, especially after load attempts
  3. Use img.at(x,y,z,c) or img.atXY() which clamp/out-of-range-handle instead of raw operator()
  4. Verify dimensions after every load/assign/resize before iterating

Example fix

// before
for (int x = 0; x <= img.width(); ++x) img(x,0,0,0);
// after
if (img.is_empty()) throw std::runtime_error("image not loaded");
for (int x = 0; x < img.width(); ++x) img(x,0,0,0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (img.is_empty()) throw std::runtime_error("image not loaded");
if (!(x < img.width() && y < img.height() && z < img.depth() && c < img.spectrum()))
  throw std::out_of_range("pixel coordinates out of bounds");

Type guard

template <typename T>
bool in_bounds(const CImg<T>& img, unsigned x, unsigned y, unsigned z = 0, unsigned c = 0) {
  return !img.is_empty() && x < img.width() && y < img.height() &&
         z < img.depth() && c < img.spectrum();
}

Try / catch

try {
  T& px = img.at(x, y, z, c); // at() clamps or throws safely
} catch (const std::exception& e) {
  // handle out-of-range access
}

Prevention

When it happens

Trigger: Indexing an empty (default-constructed or failed-load) CImg; passing x>=width(), y>=height(), z>=depth() or c>=spectrum(); loop bounds computed from a differently sized image; using stale dimensions after resize/assign.

Common situations: Off-by-one loops (e.g. `for x <= img.width()`), accessing pixels of an image whose load failed, iterating two images of different sizes in lockstep, forgetting that default-constructed CImg has size 0.

Related errors


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