Files
opencv-MIRROR/modules/core
Shiven Solanki 8871a3408a Merge pull request #30386 from sshivensolanki:fix-ocl-convert-saturation
core: saturate OpenCL conversions to CV_32U, CV_64U and CV_64S - #30386

Fixes #30385

### Problem

`ocl::convertTypeStr()` uses `ddepth >= CV_32F` / `sdepth >= CV_32F` to mean "is a float type". The 5.x integer depths are numbered above `CV_32F`, so conversions to `CV_32U`, `CV_64U` and `CV_64S` are emitted without `_sat` (and without `_rte` from float sources). `UMat::convertTo` and other OpenCL kernels using `convertTypeStr()` then wrap around instead of saturating, e.g. `-5` → `CV_32U` gives `4294967291` instead of `0`.

### Fix

In `convertTypeStr()`:

- use `CV_IS_FLOAT_TYPE(ddepth)` / `CV_IS_FLOAT_TYPE(sdepth)` instead of comparing with `CV_32F`;
- for float → integer, always use `_sat_rte`, matching the CPU's saturating `cvRound` since #30007 (was: `_sat` only below `CV_32S`).

This also fixes `CV_64S`/`CV_64U`/`CV_32U` → `CV_32S` in `UMat::convertTo`, which converts through that float → `CV_32S` path. Conversions between the 4.x depths are otherwise unchanged.

### Tests

New `OCL_MatrixOperation/ConvertToSaturate.Accuracy` compares `UMat::convertTo` with `Mat::convertTo` in the destination type for `CV_8S`/`CV_16S`/`CV_32S`/`CV_64S`/`CV_32F` → `CV_32S`/`CV_32U`/`CV_64U`/`CV_64S`, with values that need clamping or rounding. On current 5.x it fails for 10 of the pairs; with this patch it passes. All `OCL_*` and `convertTo` tests in `opencv_test_core` pass locally (7522 tests).

`CV_64U` sources are left out for now: the CPU path for `CV_64U` → `CV_64S` is a bit copy until #30206 lands.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2026-10-21 08:08:29 +03:00
..