1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

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
This commit is contained in:
Vincent Rabaud
2023-03-07 13:05:38 +01:00
committed by GitHub
parent 6b5be4f1a7
commit 8ad8ec679f
+10 -15
View File
@@ -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 &sector) {
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;