mirror of
https://github.com/opencv/opencv.git
synced 2026-11-20 08:15:26 -06:00
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.