Qt 4.7 QColor 构造函数:可能的错误?

发布于 2024-11-14 14:37:36 字数 917 浏览 4 评论 0原文

我从一段简单地访问图像坐标的代码中得到了意外的输出,这是一个片段:

QImage image(imageFileNames[frameNumber]);
QRgb qrgb(image.pixel(493,114));
QColor testCol(qrgb);

使用调试器来确保我获得正确的图像路径,我得到类似 ../seq/0001.png,这符合预期。但是,testCol 的值为:

[argb]
alpha 65535
blue 28013
green 24415
pad 0
red 59367

而不是 255,255,255 范围内的预期值。另外,qrb 的值为 4293353325。像素坐标在图像范围内(720x288 图像),图像采用 .png 格式(QImage::Format_RGB32)。

我还发现这个问题似乎是整个应用程序的全局问题,并且编写的任何新代码似乎都会继承此行为。

经过进一步的实验,使用以下代码:

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仍然有错误的值。然而,红色、绿色和蓝色均显示预期值。似乎这是 QColor 构造函数的问题?我不太确定到底发生了什么。

有什么想法可能导致这种行为吗?我在 Ubuntu 10.10 上运行 Qt 4.7。

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 (720x288 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-11-21 14:37:36

QColor 是不同结构的联合。每个结构由 5 个无符号短整型组成。

QT 使用内部位移来存储 RGB 值。因此,当您将“1”分配为蓝色分量时,它会将其移动为 257。当您检查 QColor 时,调试器将向您显示 257 作为蓝色分量,您会感到惊讶!

另一方面,当您使用 QColor 的 blue() 方法时,您应该得到 1 !

不要使用调试器来检查 QColor 使用其 getter 和 setter 方法。

以下是取自 QT 代码。您可以清楚地看到位移动。

QColor::QColor(QRgb color)
{
    cspec = Rgb;
    ct.argb.alpha = 0xffff;
    ct.argb.red   = qRed(color)   * 0x101;
    ct.argb.green = qGreen(color) * 0x101;
    ct.argb.blue  = qBlue(color)  * 0x101;
    ct.argb.pad   = 0;
}

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.

QColor::QColor(QRgb color)
{
    cspec = Rgb;
    ct.argb.alpha = 0xffff;
    ct.argb.red   = qRed(color)   * 0x101;
    ct.argb.green = qGreen(color) * 0x101;
    ct.argb.blue  = qBlue(color)  * 0x101;
    ct.argb.pad   = 0;
}
吃兔兔 2024-11-21 14:37:36

QColor 类的描述中(在整数与浮点精度部分)解释了颜色分量是使用 16 位整数存储的。所以RGBA值存储在0-65535的范围内。所以也许代码已经进化,现在返回一个 16 位整数而不是 8 位。

我现在无法测试,但问题是:函数结果错误还是文档错误?

您应该打开一个问题将其指向 Qt 开发团队。

In the description of the QColor class (in the Integer vs floating point Precision part) it is explains that color component are stored using 16-bit integer. So RGBA values are stored in the range of 0-65535. So maybe the code has evolved and now returned a 16-bit integer instead of a 8-bit.

I can't test now but the question is: Is the function result wrong or is the documentation wrong?

You should open an issue to point this to the Qt Dev Team.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文