Comparing two identical images which are loaded in the format QImage::Format_Indexed8 have different pixel data only when running in Release mode.
The following code shows differences when running in Release, but not when running in 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;
}
Output:
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
A few notes:
- This is no longer reproducible When changing the format of the images to, for example QImage::Format_ARGB32, using
convertToFormat
- This is no longer reproducible when using
pixelIndex
to compare each pixel - It is always the same indices that fail
- The indices that fail change when running on a different machine.
My current guess would be that Qt does some optimization when loading images in this format. I can't explain why this doesn't result in the same data for two identical images however.
If you care to reproduce the issue, here is my input image: