{"record":{"id":"9b4c7fc930f475c8","repo":"Yalantis/uCrop","slug":"cimg-instance-median-empty-instance","errorCode":null,"errorMessage":"_cimg_instance \"median(): Empty instance.\"","messagePattern":"_cimg_instance \"median\\(\\): Empty instance\\.\"","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":31982,"sourceCode":"            do ++i; while (arr[i]<pivot);\n            do --j; while (arr[j]>pivot);\n            if (j<i) break;\n            cimg::swap(arr[i],arr[j]);\n          }\n          arr[l + 1] = arr[j];\n          arr[j] = pivot;\n          if (j>=k) ir = j - 1;\n          if (j<=k) l = i;\n        }\n      }\n    }\n\n    //! Return the median pixel value.\n    /**\n     **/\n    T median() const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"median(): Empty instance.\",\n                                    cimg_instance);\n      const ulongT s = size();\n      switch (s) {\n      case 1 : return _data[0];\n      case 2 : return cimg::median(_data[0],_data[1]);\n      case 3 : return cimg::median(_data[0],_data[1],_data[2]);\n      case 5 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4]);\n      case 7 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6]);\n      case 9 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8]);\n      case 13 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8],\n                                    _data[9],_data[10],_data[11],_data[12]);\n      }\n      const T res = kth_smallest(s>>1);\n      return (s%2)?res:(T)((res + kth_smallest((s>>1) - 1))/2);\n    }\n\n    //! Return greatest common diviser of all image values.","sourceCodeStart":31964,"sourceCodeEnd":32000,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L31964-L32000","documentation":"Instance guard in CImg<T>::median(): computing the median requires a non-empty pixel array to sort/select from; the image is empty, so the quickselect-based median is undefined and the call is rejected.","triggerScenarios":"Calling CImg<T>::median() on an empty instance: default-constructed CImg, zero dimensions, or data lost after a failed load/transform.","commonSituations":"Median filtering or noise estimation on unloaded images; image statistics on the result of a crop that selected nothing.","solutions":["Check is_empty() before calling median()","Verify the image was populated (e.g. load succeeded, size() > 0)","Fix zero-size crops/ROIs feeding the median call","Catch CImgInstanceException around the operation"],"exampleFix":"// before\nconst T med = img.median();\n// after\nif (!img.is_empty()) {\n  const T med = img.median();\n} else {\n  // choose a default or propagate an error\n}","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image empty before median()\");","typeGuard":"bool usable = !img.is_empty() && img.size() > 0;","tryCatchPattern":"try { const T med = img.median(); } catch (const CImgInstanceException& e) { /* default or error */ }","preventionTips":["Check is_empty() before median filtering","Verify crop results are non-empty","Fail fast when a load returns a zero-size image"],"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"}