Yalantis/uCrop · error · CImgInstanceException

CImg<%s>::%s() [%u]: cubic_atXY_p(): Empty instance.

Error message

CImg<%s>::%s() [%u]: cubic_atXY_p(): Empty instance.

What it means

cubic_atXY_p() is the periodic-boundary bicubic interpolation accessor. It throws CImgInstanceException when is_empty() is true, i.e. the image instance holds no pixels, because periodic sampling still requires a non-empty buffer.

Source

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

    }

    //! Return clamped pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y-coordinates.
    /**
       Similar to cubic_atXY(float,float,int,int) const, except that the return value is clamped to stay in the
       min/max range of the datatype \c T.
    **/
    T cubic_atXY_c(const float fx, const float fy, const int z, const int c) const {
      return cimg::type<T>::cut(cubic_atXY(fx,fy,z,c));
    }

    T _cubic_atXY_c(const float fx, const float fy, const int z, const int c) const {
      return cimg::type<T>::cut(_cubic_atXY(fx,fy,z,c));
    }

    //! Return pixel value, using cubic interpolation and periodic boundary conditions for the X and Y-coordinates.
    Tfloat cubic_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "cubic_atXY_p(): Empty instance.",
                                    cimg_instance);
      return _cubic_atXY_p(fx,fy,z,c);
    }

    Tfloat _cubic_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
      const float
        nfx = cimg::type<float>::is_nan(fx)?0:cimg::mod(fx,_width - 0.5f),
        nfy = cimg::type<float>::is_nan(fy)?0:cimg::mod(fy,_height - 0.5f);
      const int x = (int)nfx, y = (int)nfy;
      const float dx = nfx - x, dy = nfy - y;
      const int
        px = cimg::mod(x - 1,width()), nx = cimg::mod(x + 1,width()), ax = cimg::mod(x + 2,width()),
        py = cimg::mod(y - 1,height()), ny = cimg::mod(y + 1,height()), ay = cimg::mod(y + 2,height());
      const Tfloat
        Ipp = (Tfloat)(*this)(px,py,z,c), Icp = (Tfloat)(*this)(x,py,z,c), Inp = (Tfloat)(*this)(nx,py,z,c),
        Iap = (Tfloat)(*this)(ax,py,z,c),
        Ip = Icp + 0.5f*(dx*(-Ipp + Inp) + dx*dx*(2*Ipp - 5*Icp + 4*Inp - Iap) + dx*dx*dx*(-Ipp + 3*Icp - 3*Inp + Iap)),

View on GitHub (pinned to f788b534b4)

Solutions

  1. Guard with if (!img.is_empty()) before periodic bicubic sampling.
  2. Validate dimensions coming from the caller (Java/Kotlin side) are >0 before constructing/cropping.
  3. Fail fast on load errors rather than continuing with an empty image.
  4. Wrap in try/catch for CImgInstanceException to convert it into an app-level error.

Example fix

// before
CImg<float> img(0, 0);
float v = img.cubic_atXY_p(3.4f, 2.2f); // throws
// after
CImg<float> img(0, 0);
if (img.width() > 0 && img.height() > 0) {
  float v = img.cubic_atXY_p(3.4f, 2.2f);
}
Defensive patterns

Strategy: validation

Validate before calling

if (img.width() == 0 || img.height() == 0) return fallback;
float v = img.cubic_atXY_p(fx, fy, z, c);

Type guard

bool ready(const CImg<T>& im) { return !im.is_empty() && im.width() && im.height(); }

Try / catch

try {
  float v = img.cubic_atXY_p(fx, fy);
} catch (const CImgInstanceException& e) {
  // report 'image not initialized' to caller
}

Prevention

When it happens

Trigger: Calling cubic_atXY_p(fx,fy,...) on an empty CImg (default-constructed, cleared, or failed load); tiling/seamless patterns generated from an image that never received data.

Common situations: Texture/tiling generation where the source load silently failed; unit tests constructing CImg without assign(); JNI crop code receiving zero-sized dimensions from the Java layer.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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