From 8ad8ec679fe8ff970c075fb327d5b97f61a48220 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Tue, 7 Mar 2023 13:05:38 +0100 Subject: [PATCH] Merge pull request #22441 from vrabaud:hls_while In case of huge (and probably invalid) input, make sure we do not rely only on the while loops for truncation. ### 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 --- modules/imgproc/src/color_hsv.simd.hpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/modules/imgproc/src/color_hsv.simd.hpp b/modules/imgproc/src/color_hsv.simd.hpp index 424a6d1494..0c30564a12 100644 --- a/modules/imgproc/src/color_hsv.simd.hpp +++ b/modules/imgproc/src/color_hsv.simd.hpp @@ -419,6 +419,14 @@ inline void HSV2RGB_simd(const v_float32& h, const v_float32& s, const v_float32 } #endif +// Compute the sector and the new H for HSV and HLS 2 RGB conversions. +inline void ComputeSectorAndClampedH(float& h, int §or) { + sector = cvFloor(h); + h -= sector; + sector %= 6; + sector += sector < 0 ? 6 : 0; +} + inline void HSV2RGB_native(float h, float s, float v, float& b, float& g, float& r, @@ -433,14 +441,7 @@ inline void HSV2RGB_native(float h, float s, float v, float tab[4]; int sector; h *= hscale; - h = fmod(h, 6.f); - sector = cvFloor(h); - h -= sector; - if( (unsigned)sector >= 6u ) - { - sector = 0; - h = 0.f; - } + ComputeSectorAndClampedH(h, sector); tab[0] = v; tab[1] = v*(1.f - s); @@ -987,13 +988,7 @@ struct HLS2RGB_f float p1 = 2*l - p2; h *= hscale; - // We need both loops to clamp (e.g. for h == -1e-40). - while( h < 0 ) h += 6; - while( h >= 6 ) h -= 6; - - CV_DbgAssert( 0 <= h && h < 6 ); - sector = cvFloor(h); - h -= sector; + ComputeSectorAndClampedH(h, sector); tab[0] = p2; tab[1] = p1;