Files
opencv-MIRROR/modules
pranayr710 34b8868876 core: saturate when narrowing 64-bit and 32U sources in convertTo
v_pack() narrows 64-bit lanes by truncation: at the 8/16/32-bit widths
v_pack saturates, but the 64->32 forms are defined with static_cast (see
OPENCV_HAL_IMPL_C_PACK in intrin_cpp.hpp) because no backend offers a
saturating 64->32 pack. The vectorized conversion helpers packed straight
from a 64-bit source, so a value outside the 32-bit range wrapped, and the
saturating narrow that followed then clamped the WRAPPED value - landing at
the wrong end of the destination's range. The same happened one width down,
where a CV_32U source was reinterpreted as signed without clamping.

Only the vector loop was affected, so the answer depended on how long the
array was: below the vector width the scalar path saturated correctly.

  Mat src(1, n, CV_64U, Scalar(4294967295));
  src.convertTo(dst, CV_8U);       // n < 16: 255      n >= 16: 0
  src.convertTo(dst, CV_16U);      // n < 16: 65535    n >= 16: 0

Clamp into the destination's range before packing. The unsigned-source
helpers test the high bits with a shift instead of an ordering compare,
since unsigned 64-bit comparison is not uniformly available across the SIMD
backends, and v_select() has no 64-bit form so the blend goes through the
comparison mask by hand.

Affects CV_64U/CV_64S -> CV_8U/CV_8S/CV_16U/CV_16S/CV_32S and
CV_32U -> CV_8S/CV_16S.
2026-10-01 20:29:06 +05:30
..
2026-09-23 14:28:55 +02:00
2026-07-09 12:17:24 +03:00
2026-07-09 12:17:24 +03:00
2026-09-23 14:28:55 +02:00
2026-09-23 11:18:41 +02:00
2026-08-19 17:38:39 +02:00