Yalantis/uCrop · error · CImgInstanceException
magnitude(): Empty instance.
Error message
magnitude(): Empty instance.
What it means
Instance guard in CImg<T>::magnitude(): the image is treated as a matrix/vector to compute its Lp-norm, which requires allocated pixel data; the instance is empty so the norm is undefined and the method refuses to proceed.
Solutions
- Check is_empty() before calling magnitude()
- Ensure the image was loaded/populated (size() > 0)
- Fix upstream zero-size image creation
- Catch CImgInstanceException if the empty case is expected
Example fix
// before
double m = img.magnitude(2);
// after
if (!img.is_empty()) {
double m = img.magnitude(2);
} Defensive patterns
Strategy: validation
Validate before calling
if (img.is_empty()) throw std::runtime_error("image empty before magnitude()"); Type guard
bool usable = !img.is_empty() && img.size() > 0;
Try / catch
try { double m = img.magnitude(2); } catch (const CImgInstanceException& e) { /* handle empty */ } Prevention
- Check is_empty() before norm computations
- Validate images after load/derivative operations
- Fail fast on zero-size intermediates in pipelines
When it happens
Trigger: Calling CImg<T>::magnitude(magnitude_type) on an empty instance — default construction, zero dimensions, or a failed load/processing step.
Common situations: Gradient/edge-strength measurements on unloaded images; norm computations in pipelines where an earlier step yielded an empty buffer.
Related errors
- _cimg_instance "kth_smallest(): Empty instance."
- _cimg_instance "max_min(): Empty instance."
- _cimg_instance "maxabs(): Empty instance."
- _cimg_instance "median(): Empty instance."
- _cimg_instance "min_max(): Empty instance."
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/b95124ead0ecd8af.
Report an issue: GitHub.
Appendix: source
Thrown at ucrop/src/main/jni/CImg.h:32535
//@}
//-------------------------------------
//
//! \name Vector / Matrix Operations
//@{
//-------------------------------------
//! Compute norm of the image, viewed as a matrix.
/**
\param magnitude_type Can be:
- \c 0: L0-norm
- \c 1: L1-norm
- \c 2: L2-norm
- \c p>2 : Lp-norm
- \c ~0U: Linf-norm
**/
double magnitude(const float magnitude_type=2) const {
if (is_empty())
throw CImgInstanceException(_cimg_instance
"magnitude(): Empty instance.",
cimg_instance);
const ulongT siz = size();
double res = 0;
if (magnitude_type==2) { // L2
cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::sqr(_data[off]);
res = (double)std::sqrt(res);
} else if (magnitude_type==1) { // L1
cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::abs(_data[off]);
} else if (!magnitude_type) { // L0
cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
for (longT off = 0; off<(longT)siz; ++off) res+=(double)(_data[off]?1:0);
} else if (cimg::type<float>::is_inf(magnitude_type)) { // L-inf
cimg_for(*this,ptrs,T) { const double val = (double)cimg::abs(*ptrs); if (val>res) res = val; }
} else { // L-p
cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))View on GitHub (pinned to f788b534b4)