{"record":{"id":"73b3cc1e0ae6abbc","repo":"Yalantis/uCrop","slug":"cimg-instance-maxabs-empty-instance","errorCode":null,"errorMessage":"_cimg_instance \"maxabs(): Empty instance.\"","messagePattern":"_cimg_instance \"maxabs\\(\\): Empty instance\\.\"","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":31833,"sourceCode":"\n    //! Return a reference to the maximum pixel value \\const.\n    const T& max() const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"max(): Empty instance.\",\n                                    cimg_instance);\n      const T *ptr_max = _data;\n      T max_value = *ptr_max;\n      cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);\n      return *ptr_max;\n    }\n\n    //! Return a reference to the maximum pixel value in absolute value.\n    /**\n     **/\n    T& maxabs() {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"maxabs(): Empty instance.\",\n                                    cimg_instance);\n      T *ptr_maxabs = _data;\n      T maxabs_value = *ptr_maxabs;\n      cimg_for(*this,ptrs,T) {\n        const T ma = cimg::abs(*ptrs);\n        if (ma>maxabs_value) { maxabs_value = ma; ptr_maxabs = ptrs; }\n      }\n      return *ptr_maxabs;\n    }\n\n    //! Return a reference to the maximum pixel value in absolute value \\const.\n    const T& maxabs() const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"maxabs(): Empty instance.\",\n                                    cimg_instance);\n      const T *ptr_maxabs = _data;","sourceCodeStart":31815,"sourceCodeEnd":31851,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L31815-L31851","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check img.is_empty() (or !img) before calling maxabs() and handle the empty case explicitly","Verify the image was actually loaded/assigned: check load() return value and img.size() > 0","Fix the crop/ROI computation that produced a zero-sized image (clamp coordinates to image bounds)","Wrap the call in try/catch (CImgInstanceException) as a last resort"],"exampleFix":"// before\nconst T& m = img.maxabs();\n// after\nif (!img.is_empty()) {\n  const T& m = img.maxabs();\n} else {\n  // handle empty image\n}","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image empty before maxabs()\");","typeGuard":"bool usable = !img.is_empty() && img.size() > 0;","tryCatchPattern":"try { const T& m = img.maxabs(); } catch (const CImgInstanceException& e) { /* handle empty image */ }","preventionTips":["Always check load() success and is_empty() before statistics calls","Assert image dimensions right after construction/loading","Clamp crop/ROI coordinates to image bounds","Centralize image validation in a helper used before any pixel scan"],"tags":["cimg","image-processing","empty-image"],"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"}