[Interest] QColor methods saturation(), value() and getHsv() are incredibly slow

Constantin Makshin cmakshin at gmail.com
Thu Sep 20 19:52:53 CEST 2012


If you look at the code of QColor::toHsv(), you'll see that:
1) *all* calculations are done in floating point (probably to get better
accuracy, but I can't say for sure);
2) most of the time is spent calculating hue.

So no wonder your mostly-integer no-hue function is faster.

On 09/20/2012 09:19 PM, Jochen Pernsteiner wrote:
> Hello,
> 
> I'm playing around with an application were I use a QImage with a RGB32
> format.
> 
> I used the QColor method saturation() and value() to compute these
> values (I do not
> need hue), and found out that these functions (and naturally also
> getHSV()) are incredibly slow.
> 
> I wrote a straightforward implementation of a RGB-to-SV conversion,
> which on my
> PC is 12-15 times faster than using QColor's getHSV() method.
> 
> Wikipedia has a good explanation of HSV:
> http://en.wikipedia.org/wiki/HSL_and_HSV
> 
> The only difference between my function and the QColor method(s) is that
> the LSB of 'saturation' is not always the same. I guess this is a
> rounding issue.
> 
> So my question is:
> Are these QColor methods so slow or am I doing something wrong?
> (The Qt version I'm using is 4.8.2 (mingw open source) on Windows XP.)
> 
> 
> 
> My function looks like this:
> 
> typedef unsigned char u8;
> typedef unsigned short u16;
> typedef unsigned long u32;
> 
> //--------------------------------------------------------------
> u8 max3(u8 a, u8 b, u8 c)
> {
>   u8 max = (a > b) ? a : b;
>   max = (max > c) ? max : c;
>   return max;
> }
> //--------------------------------------------------------------
> u8 min3(u8 a, u8 b, u8 c)
> {
>   u8 min = (a > b) ? b : a;
>   min = (min > c) ? c : min;
>   return min;
> }
> //--------------------------------------------------------------
> u16 rgb_to_sv(u32 rgb)
> {
>   u8 red = (rgb >> 16) & 0xff;
>   u8 green = (rgb >> 8) & 0xff;
>   u8 blue = rgb & 0xff;
> 
>   u8 max = max3(red,green,blue);
>   u8 min = min3(red,green,blue);
>  
>   if(max == 0) return 0;
> 
>   double saturation = (double)(max - min)/max;
>   double tmp = saturation * 255;
>   tmp += 0.5;
>   u8 saturation_u8 = tmp;
>  
>   return (saturation_u8 << 8) + max; // value = max
> }
> //--------------------------------------------------------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120920/e51d8284/attachment.sig>


More information about the Interest mailing list