I am getting unexpected output from a piece of code that is simply accessing image coordinates, here’s a snippet:
QImage image(imageFileNames[frameNumber]);
QRgb qrgb(image.pixel(493,114));
QColor testCol(qrgb);
Using the debugger to make sure I get the right image path I get something like ../seq/0001.png, which is as expected. However, testCol has the values of:
[argb]
alpha 65535
blue 28013
green 24415
pad 0
red 59367
Instead of the expected value within the 255,255,255 range. Also, the value of qrb is 4293353325. The pixel coordinate is in image range (720×288 image) and image is in .png format (QImage::Format_RGB32).
I’ve also discovered that this problem seems to be global to the entire application and any new code that is written seems to inherit this behaviour.
Upon further experimentation, using the following code:
QRgb qrgb(image.pixel(1,1)); //again this is in range
int blue = qBlue(qrgb);
int red = qRed(qrgb);
int green = qGreen(qrgb);
QColor testCol(red,green,blue, 255);
testColor still has the wrong values. However, red, green and blue all show expected values. Seems like this is an issue with the QColor constructor? I’m not really too certain about what exactly is going on.
Any ideas what might be causing this behaviour? I am running Qt 4.7 on Ubuntu 10.10.
QColor is a union of different structures. Each structure is composed of 5 unsigned shorts.
QT uses internal bit shifting to store RGB values. So when you assign “1” as blue component it shifts it to make say 257. When you examine a QColor your debugger will show you 257 as blue component and you will get surprized !
On the other hand when you use the blue() method of QColor you should get 1 !
Don’t use the debugger to examine the QColor use its getter and setter methods.
Here is an example taken from QT code. You can see the bit shifting clearly.