Yalantis/uCrop · error · CImgInstanceException

_cimg_instance "maxabs(): Empty instance."

Error message

_cimg_instance "maxabs(): Empty instance."

What it means

CImg throws CImgInstanceException from maxabs() when the image instance is empty (zero-size). maxabs() needs at least one pixel to find the maximum absolute value, so calling it on an empty CImg is a programming error guarded by this throw.

Solutions

  1. Check img.is_empty() (or !img) before calling maxabs() and handle the empty case explicitly
  2. Verify the image was actually loaded/assigned: check load() return value and img.size() > 0
  3. Fix the crop/ROI computation that produced a zero-sized image (clamp coordinates to image bounds)
  4. Wrap the call in try/catch (CImgInstanceException) as a last resort

Example fix

// before
const T& m = img.maxabs();
// after
if (!img.is_empty()) {
  const T& m = img.maxabs();
} else {
  // handle empty image
}
Defensive patterns

Strategy: validation

Validate before calling

if (img.is_empty()) throw std::runtime_error("image empty before maxabs()");

Type guard

bool usable = !img.is_empty() && img.size() > 0;

Try / catch

try { const T& m = img.maxabs(); } catch (const CImgInstanceException& e) { /* handle empty image */ }

Prevention

When it happens

Trigger: Calling CImg<T>::maxabs() on an instance where is_empty() is true — e.g. a default-constructed CImg, one built with dimensions 0, or the result of a failed load/crop that produced no pixels.

Common situations: An image file failed to load (wrong path/unsupported format) and the resulting empty image is passed on; a crop/sub-image selection with out-of-range coordinates yielding zero size; calling maxabs() before assigning image data.

Related errors


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

Appendix: source

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

    //! Return a reference to the maximum pixel value \const.
    const T& max() const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "max(): Empty instance.",
                                    cimg_instance);
      const T *ptr_max = _data;
      T max_value = *ptr_max;
      cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
      return *ptr_max;
    }

    //! Return a reference to the maximum pixel value in absolute value.
    /**
     **/
    T& maxabs() {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "maxabs(): Empty instance.",
                                    cimg_instance);
      T *ptr_maxabs = _data;
      T maxabs_value = *ptr_maxabs;
      cimg_for(*this,ptrs,T) {
        const T ma = cimg::abs(*ptrs);
        if (ma>maxabs_value) { maxabs_value = ma; ptr_maxabs = ptrs; }
      }
      return *ptr_maxabs;
    }

    //! Return a reference to the maximum pixel value in absolute value \const.
    const T& maxabs() const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "maxabs(): Empty instance.",
                                    cimg_instance);
      const T *ptr_maxabs = _data;

View on GitHub (pinned to f788b534b4)