{"record":{"id":"b95124ead0ecd8af","repo":"Yalantis/uCrop","slug":"magnitude-empty-instance","errorCode":null,"errorMessage":"magnitude(): Empty instance.","messagePattern":"magnitude\\(\\): Empty instance\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32535,"sourceCode":"    //@}\n    //-------------------------------------\n    //\n    //! \\name Vector / Matrix Operations\n    //@{\n    //-------------------------------------\n\n    //! Compute norm of the image, viewed as a matrix.\n    /**\n       \\param magnitude_type Can be:\n       - \\c 0: L0-norm\n       - \\c 1: L1-norm\n       - \\c 2: L2-norm\n       - \\c p>2 : Lp-norm\n       - \\c ~0U: Linf-norm\n    **/\n    double magnitude(const float magnitude_type=2) const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"magnitude(): Empty instance.\",\n                                    cimg_instance);\n      const ulongT siz = size();\n      double res = 0;\n      if (magnitude_type==2) { // L2\n        cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))\n        for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::sqr(_data[off]);\n        res = (double)std::sqrt(res);\n      } else if (magnitude_type==1) { // L1\n        cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))\n        for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::abs(_data[off]);\n      } else if (!magnitude_type) { // L0\n        cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))\n        for (longT off = 0; off<(longT)siz; ++off) res+=(double)(_data[off]?1:0);\n      } else if (cimg::type<float>::is_inf(magnitude_type)) { // L-inf\n        cimg_for(*this,ptrs,T) { const double val = (double)cimg::abs(*ptrs); if (val>res) res = val; }\n      } else { // L-p\n        cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))","sourceCodeStart":32517,"sourceCodeEnd":32553,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32517-L32553","documentation":"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.","triggerScenarios":"Calling CImg<T>::magnitude(magnitude_type) on an empty instance — default construction, zero dimensions, or a failed load/processing step.","commonSituations":"Gradient/edge-strength measurements on unloaded images; norm computations in pipelines where an earlier step yielded an empty buffer.","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"],"exampleFix":"// before\ndouble m = img.magnitude(2);\n// after\nif (!img.is_empty()) {\n  double m = img.magnitude(2);\n}","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image empty before magnitude()\");","typeGuard":"bool usable = !img.is_empty() && img.size() > 0;","tryCatchPattern":"try { double m = img.magnitude(2); } catch (const CImgInstanceException& e) { /* handle empty */ }","preventionTips":["Check is_empty() before norm computations","Validate images after load/derivative operations","Fail fast on zero-size intermediates in pipelines"],"tags":["cimg","image-processing","empty-image","norm"],"backgroundTag":"empty-image-instance","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}