The QImage
equality operator will report that two QImage
instances are different if the images have different formats, different sizes and/or different contents. For the benefit of others that might have trouble understanding why two QImage
instances are different, the following function prints out what the differences are (though it may generate a lot of output if there are a lot of differing pixels):
void displayDifferencesInImages(const QImage& image1, const QImage& image2)
{
if (image1 == image2)
{
qDebug("Images are identical");
return;
}
qDebug("Found the following differences:");
if (image1.size() != image2.size())
{
qDebug(" - Image sizes are different (%dx%d vs. %dx%d)",
image1.width(), image1.height(),
image2.width(), image2.height());
}
if (image1.format() != image2.format())
{
qDebug(" - Image formats are different (%d vs. %d)",
static_cast<int>(image1.format()),
static_cast<int>(image2.format()));
}
int smallestWidth = qMin(image1.width(), image2.width());
int smallestHeight = qMin(image1.height(), image2.height());
for (int i=0; i<smallestWidth; ++i)
{
for (int j=0; j<smallestHeight; ++j)
{
if (image1.pixel(i, j) != image2.pixel(i, j))
{
qDebug(" - Image pixel (%d, %d) is different (%x vs. %x)",
i, j, image1.pixel(i, j), image2.pixel(i, j));
}
}
}
}