【发布时间】:2018-11-08 08:05:54
【问题描述】:
比较两个以 QImage::Format_Indexed8 格式加载的相同图像只有在发布模式下运行时具有不同的像素数据。
以下代码显示了在 Release 中运行时的差异,但在 Debug 中运行时却没有:
int main()
{
QImage _img1("C:\\tmp\\diff\\identicals\\file1.png");
QImage _img2("C:\\tmp\\diff\\identicals\\file2.png");
std::cout << QString("Format 1: %1").arg(_img1.format()).toStdString().c_str() << std::endl;
std::cout << QString("Format 2: %2").arg(_img2.format()).toStdString().c_str() << std::endl;
const unsigned char * _bits1 = _img1.bits();
const unsigned char * _bits2 = _img2.bits();
std::cout << QString("Byte count 1: %1 | Byte count 2: %2").arg(_img1.byteCount()).
arg(_img2.byteCount()).toStdString().c_str() << std::endl;
for (int _i = 0; _i < _img1.byteCount(); _i++)
{
if (_bits1[_i] != _bits2[_i])
{
std::cout << "--DIFFERENCE--" << std::endl;
std::cout << QString("i --> %1").arg(_i).toStdString().c_str() << std::endl;
std::cout << QString("Bit1: %1 | Bit2: %2").arg(_bits1[_i]).arg(_bits2[_i]).toStdString().c_str() << std::endl << std::endl;
}
}
std::cout << "BREAK" << std::endl;
}
输出:
Format 1: 3
Format 2: 3
Byte count 1: 23424 | Byte count 2: 23424
--DIFFERENCE--
i --> 1535
Bit1: 0 | Bit2: 217
--DIFFERENCE--
i --> 1663
Bit1: 0 | Bit2: 35
--DIFFERENCE--
i --> 1791
Bit1: 0 | Bit2: 94
--DIFFERENCE--
i --> 1919
Bit1: 0 | Bit2: 166
--DIFFERENCE--
i --> 2047
Bit1: 0 | Bit2: 143
--DIFFERENCE--
i --> 2175
Bit1: 0 | Bit2: 104
--DIFFERENCE--
i --> 2303
Bit1: 0 | Bit2: 240
--DIFFERENCE--
i --> 2431
Bit1: 0 | Bit2: 190
--DIFFERENCE--
i --> 2559
Bit1: 0 | Bit2: 129
--DIFFERENCE--
i --> 2687
Bit1: 0 | Bit2: 11
--DIFFERENCE--
i --> 2815
Bit1: 0 | Bit2: 30
--DIFFERENCE--
i --> 2943
Bit1: 0 | Bit2: 163
--DIFFERENCE--
i --> 3071
Bit1: 0 | Bit2: 206
--DIFFERENCE--
i --> 3199
Bit1: 0 | Bit2: 232
--DIFFERENCE--
i --> 3327
Bit1: 0 | Bit2: 124
--DIFFERENCE--
i --> 3455
Bit1: 0 | Bit2: 225
--DIFFERENCE--
i --> 12287
Bit1: 0 | Bit2: 240
--DIFFERENCE--
i --> 12415
Bit1: 0 | Bit2: 224
--DIFFERENCE--
i --> 12543
Bit1: 0 | Bit2: 240
几点说明:
- 当使用
convertToFormat将图像格式更改为例如 QImage::Format_ARGB32 时,这不再可重现 - 当使用
pixelIndex比较每个像素时,这不再可重现 - 失败的总是相同的索引
- 在不同机器上运行时失败的索引会发生变化。
我目前的猜测是 Qt 在以这种格式加载图像时会进行一些优化。但是,我无法解释为什么这不会导致两张相同图像的数据相同。
如果您想重现该问题,这是我的输入图像:
【问题讨论】:
-
格式2的打印需要使用
%1,顺便说一句