{"record":{"id":"20801ed8ef6b6209","repo":"Yalantis/uCrop","slug":"cimg-instance-kth-smallest-empty-instance","errorCode":null,"errorMessage":"_cimg_instance \"kth_smallest(): Empty instance.\"","messagePattern":"_cimg_instance \"kth_smallest\\(\\): Empty instance\\.\"","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":31945,"sourceCode":"      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    **/\n    T kth_smallest(const ulongT k) const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"kth_smallest(): Empty instance.\",\n                                    cimg_instance);\n      if (k>=size()) return max();\n      CImg<T> arr(*this,false);\n      ulongT l = 0, ir = size() - 1;\n      for ( ; ; ) {\n        if (ir<=l + 1) {\n          if (ir==l + 1 && arr[ir]<arr[l]) cimg::swap(arr[l],arr[ir]);\n          return arr[k];\n        } else {\n          const ulongT mid = (l + ir)>>1;\n          cimg::swap(arr[mid],arr[l + 1]);\n          if (arr[l]>arr[ir]) cimg::swap(arr[l],arr[ir]);\n          if (arr[l + 1]>arr[ir]) cimg::swap(arr[l + 1],arr[ir]);\n          if (arr[l]>arr[l + 1]) cimg::swap(arr[l],arr[l + 1]);\n          ulongT i = l + 1, j = ir;\n          const T pivot = arr[l + 1];\n          for ( ; ; ) {","sourceCodeStart":31927,"sourceCodeEnd":31963,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L31927-L31963","documentation":"kth_smallest(k) throws CImgInstanceException when the instance is empty. It performs a selection algorithm over the pixel array to find the k-th smallest value, which is impossible without data. Note that k >= size() falls back to max(), so only the empty case throws here.","triggerScenarios":"Calling CImg<T>::kth_smallest(k) with any k on an instance where is_empty() is true — no pixel buffer or zero-size dimensions.","commonSituations":"Percentile/threshold computation on images that failed to load; median-like filtering pipelines where an upstream step returned an empty image.","solutions":["Guard with img.is_empty() before calling kth_smallest()","Ensure the image contains data (size() > 0) before rank queries","Fix the upstream operation that produced an empty image","Catch CImgInstanceException to handle empty input gracefully"],"exampleFix":"// before\nconst T v = img.kth_smallest(10);\n// after\nif (!img.is_empty()) {\n  const T v = img.kth_smallest(std::min<size_t>(10, img.size() - 1));\n}","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image empty before kth_smallest()\");","typeGuard":"bool usable = !img.is_empty() && img.size() > 0;","tryCatchPattern":"try { const T v = img.kth_smallest(k); } catch (const CImgInstanceException& e) { /* handle empty */ }","preventionTips":["Also clamp k to size()-1 (k >= size() silently returns max())","Check is_empty() before rank/percentile queries","Validate loaded images before filtering pipelines"],"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"}