You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, June 8th 2011, 11:21am

image.pixel() not returning expected value

Hi everyone.

I am getting unexpected output from a piece of code that is simply accessing image coordinates, here's a snippet:

Source code

1
2
3
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 (720x288 image) and image is in .png format (QImage::Format_RGB32). Any ideas what might be causing this behaviour? I am running Qt 4.7 on Ubuntu 10.10.

This post has been edited 4 times, last edit by "ObLoM" (Jun 8th 2011, 11:55am)


2

Wednesday, June 8th 2011, 2:03pm

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. What's going on? :(

Edit: Upon further experimentation, using the following code:

Source code

1
2
3
4
5
    QRgb qrgb(image.pixel(1,1));
    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.

This post has been edited 2 times, last edit by "ObLoM" (Jun 8th 2011, 2:19pm)


chang112x

Beginner

  • "chang112x" is male

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

3

Friday, June 10th 2011, 10:26am

How do you output color values ? Or do you see it in debugger ?

chang112x

Beginner

  • "chang112x" is male

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

4

Friday, June 10th 2011, 10:37am

I think is right and there some problem with signed or unsigned values, if you see in hex, 65535 is 0xFFFF, which is equivalent to 255 whit 2 bytes. 2^16 - 1 equals exactly 65535.
So it means that the alpha value uses 2 bytes instead of one, and so for other values. I think it might be so.
If you see the value of blue: 28013 in hex, is 0x6D6D, it means the value of 1 byte is repeated twice, filling a space of 2 bytes.
The same is for the other values:
green is 24415, in hex is 0x5F5F
red is 59367, in hex is 0xE7E7.

Probably when you pass a value to QColor, it uses 2 bytes per channel, so it is something like that:

Source code

1
channel1 = color_value1 << 16 | colour_value1

chang112x

Beginner

  • "chang112x" is male

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

5

Friday, June 10th 2011, 10:39am

I don't understand the reason to do such thing, anyway.

chang112x

Beginner

  • "chang112x" is male

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

6

Friday, June 10th 2011, 10:41am

So if you wan't to see the real values of the pixels, you have to filter them, channel by channel, with this:

Source code

1
value = channel_value (like blue) & 0xFF;