【发布时间】:2023-04-11 01:14:02
【问题描述】:
我正在尝试访问 QImage 中的图像颜色。
我在文档中发现最多的方法是基于扫描线函数...
我试过了,它在 RGB32 图像上有效。使用精确方法获取 8 位索引或单色图像的颜色数据时,我得到了令人惊讶且令人不快的结果。
这是我的代码:
// note RGBTriple is a struct containing unsigned R, G, B
// rgbImage.pixels is a RGBTriple* array
RGBTriple* pTriple = rgbImage.pixels;
for (int y = 0; y < source.height(); y++)
{
const unsigned char* pScanLine = source.scanLine(y);
for (int x = 0; x < source.width(); x++)
{
QRgb* color = (QRgb*)pScanLine;
pTriple->R = qRed(*color);
pTriple->G = qGreen(*color);
pTriple->B = qBlue(*color);
++pTriple;
pScanLine += 4;
}
}
使用 8 位索引或单色图像运行相同的代码,我在创建获取颜色时出错。文档说扫描线与 32b 的倍数对齐 - 但由于这是 8 和 2 的倍数,我认为这不是问题。
一旦我发现对于所有类型的输入图像都没有得到正确的结果,我将其更改为
RGBTriple* pTriple = rgbImage.pixels;
for (int y = 0; y < source.height(); y++)
{
for (int x = 0; x < source.width(); x++)
{
pTriple->R = qRed(source.pixel(x, y));
pTriple->G = qGreen(source.pixel(x, y));
pTriple->B = qBlue(source.pixel(x, y));
++pTriple;
}
}
完美运行...我想知道它是否更慢或会有其他意外行为?毕竟,我正在使用 pixel() 函数 - 即使在索引图像上 - 来获取颜色信息,实际上应该以不同的方式存储......这似乎应该失败......
有没有办法让第一个版本,使用扫描线,适用于其他图像类型?
为什么使用扫描线获取数据似乎是首选方法?
【问题讨论】:
-
关于“较慢”,至少调用一次
pixel并将返回值存储到临时变量...
标签: image qt colors pixel scanline