我正在尝试访问 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() 函数——即使是在索引图像上——来获取颜色信息,实际上应该以不同的方式存储......这似乎应该失败......
有没有办法使用扫描线制作第一个版本,适用于其他图像类型?
为什么使用扫描线获取数据似乎是首选方法?