{"record":{"id":"0bd4a994d0bdcdba","repo":"Yalantis/uCrop","slug":"cimg-instance-max-min-empty-instance","errorCode":null,"errorMessage":"_cimg_instance \"max_min(): Empty instance.\"","messagePattern":"_cimg_instance \"max_min\\(\\): Empty instance\\.\"","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":31924,"sourceCode":"        if (val<val_min) { val_min = val; ptr_min = ptrs; }\n        if (val>val_max) { val_max = val; ptr_max = ptrs; }\n      }\n    }\n\n    //! Return a reference to the minimum pixel value as well as the maximum pixel value \\const.\n    template<typename t>\n    const T& min_max(t& max_val) const {\n      return ((CImg<T>*)this)->min_max(max_val);\n    }\n\n    //! Return a reference to the maximum pixel value as well as the minimum pixel value.\n    /**\n       \\param[out] min_val Minimum pixel value.\n    **/\n    template<typename t>\n    T& max_min(t& min_val) {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"max_min(): Empty instance.\",\n                                    cimg_instance);\n      const T *ptr_min, *ptr_max;\n      _min_max(ptr_min,ptr_max);\n      min_val = (t)*ptr_min;\n      return (T&)*ptr_max;\n    }\n\n    //! Return a reference to the maximum pixel value as well as the minimum pixel value \\const.\n    template<typename t>\n    const T& max_min(t& min_val) const {\n      return ((CImg<T>*)this)->max_min(min_val);\n    }\n\n    //! Return the kth smallest pixel value.\n    /**\n       \\param k Rank of the smallest element searched.\n    **/","sourceCodeStart":31906,"sourceCodeEnd":31942,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L31906-L31942","documentation":"Instance guard in CImg<T>::max_min(t&): the method returns references to the maximum and minimum pixels, but the instance has zero pixels, so those references cannot be produced. Standard empty-instance sentinel for statistics functions.","triggerScenarios":"Calling CImg<T>::max_min(t& min_val) on an empty instance (is_empty() true): default construction, zero dimensions, or a failed load/transform leaving no data.","commonSituations":"Auto-contrast/threshold workflows on unloaded images; statistics on images produced by an earlier failed pipeline step.","solutions":["Verify the image is non-empty with is_empty() before calling max_min()","Check that loading/assignment succeeded before statistics","Correct ROI/crop math that yielded a zero-size image","Catch CImgInstanceException around the call"],"exampleFix":"// before\nT minv;\nconst T& maxv = img.max_min(minv);\n// after\nif (!img.is_empty()) {\n  T minv;\n  const T& maxv = img.max_min(minv);\n}","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image empty before max_min()\");","typeGuard":"bool usable = !img.is_empty() && img.size() > 0;","tryCatchPattern":"try { T minv; const T& maxv = img.max_min(minv); } catch (const CImgInstanceException& e) { /* handle empty */ }","preventionTips":["Guard all statistics calls with is_empty()","Confirm load success before processing","Keep image pipelines fail-fast so emptiness is caught early"],"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"}