@@ -39,36 +39,58 @@ struct RGB2HSV_b
3939 : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange)
4040 {
4141 CV_Assert ( hrange == 180 || hrange == 256 );
42+ initTables_ ();
4243 }
4344
44- void operator ()( const uchar* src, uchar* dst, int n) const
45+ void initTables_ ()
4546 {
46- CV_INSTRUMENT_REGION ();
47-
48- int i, bidx = blueIdx, scn = srccn;
4947 const int hsv_shift = 12 ;
5048
5149 static int sdiv_table[256 ];
5250 static int hdiv_table180[256 ];
5351 static int hdiv_table256[256 ];
52+ #ifdef CV_CXX11
53+ static std::atomic<bool > initialized (false );
54+ #else
5455 static volatile bool initialized = false ;
56+ #endif
5557
56- int hr = hrange;
57- const int * hdiv_table = hr == 180 ? hdiv_table180 : hdiv_table256 ;
58+ hdiv_table_ = hrange == 180 ? hdiv_table180 : hdiv_table256 ;
59+ sdiv_table_ = sdiv_table ;
5860
59- if ( !initialized )
61+ #ifdef CV_CXX11
62+ if (!initialized.load (std::memory_order_acquire))
63+ #else
64+ if (!initialized)
65+ #endif
6066 {
6167 sdiv_table[0 ] = hdiv_table180[0 ] = hdiv_table256[0 ] = 0 ;
62- for ( i = 1 ; i < 256 ; i++ )
68+ for ( int i = 1 ; i < 256 ; i++)
6369 {
6470 sdiv_table[i] = saturate_cast<int >((255 << hsv_shift)/(1 .*i));
6571 hdiv_table180[i] = saturate_cast<int >((180 << hsv_shift)/(6 .*i));
6672 hdiv_table256[i] = saturate_cast<int >((256 << hsv_shift)/(6 .*i));
6773 }
74+ #ifdef CV_CXX11
75+ initialized.store (true , std::memory_order_release);
76+ #else
6877 initialized = true ;
78+ #endif
6979 }
80+ }
81+
82+ void operator ()(const uchar* src, uchar* dst, int n) const
83+ {
84+ CV_INSTRUMENT_REGION ();
7085
71- i = 0 ;
86+ int bidx = blueIdx, scn = srccn;
87+ const int hsv_shift = 12 ;
88+
89+ int hr = hrange;
90+ const int * hdiv_table/* [256]*/ = hdiv_table_;
91+ const int * sdiv_table/* [256]*/ = sdiv_table_;
92+
93+ int i = 0 ;
7294
7395#if CV_SIMD
7496 const int vsize = v_uint8::nlanes;
@@ -231,6 +253,9 @@ struct RGB2HSV_b
231253 }
232254
233255 int srccn, blueIdx, hrange;
256+
257+ const int * hdiv_table_/* [256]*/ ;
258+ const int * sdiv_table_/* [256]*/ ;
234259};
235260
236261
0 commit comments